{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":39763,"databundleVersionId":11756775,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport sys\nimport json\nimport glob\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport torch\nimport zipfile\nimport io","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T15:10:29.792721Z","iopub.execute_input":"2025-04-09T15:10:29.793102Z","iopub.status.idle":"2025-04-09T15:10:29.798138Z","shell.execute_reply.started":"2025-04-09T15:10:29.793071Z","shell.execute_reply":"2025-04-09T15:10:29.797189Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TEST_DATA_DIR = '/kaggle/input/waveform-inversion/test'\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T15:14:48.806711Z","iopub.execute_input":"2025-04-09T15:14:48.807172Z","iopub.status.idle":"2025-04-09T15:14:48.811754Z","shell.execute_reply.started":"2025-04-09T15:14:48.807137Z","shell.execute_reply":"2025-04-09T15:14:48.810815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DEBUG = True \n\nDT = 0.001  #time step(sec)\nVELOCITY = 2500 # 音速 (m/s)\nDISTANCE_DECAY_POWER = 2.0 #n (1.0 to 3.0)\nZIP_FILENAME = \"seismic_images.zip\" \n\nGAIN = 1.0 ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T15:14:49.050149Z","iopub.execute_input":"2025-04-09T15:14:49.050553Z","iopub.status.idle":"2025-04-09T15:14:49.055604Z","shell.execute_reply.started":"2025-04-09T15:14:49.050518Z","shell.execute_reply":"2025-04-09T15:14:49.054410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"npy_files = sorted(glob.glob(os.path.join(TEST_DATA_DIR, '*.npy')))\ntotal_files = len(npy_files)\n\nprint(f\"Found {total_files} NPY files.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T15:14:49.290205Z","iopub.execute_input":"2025-04-09T15:14:49.290770Z","iopub.status.idle":"2025-04-09T15:14:49.471401Z","shell.execute_reply.started":"2025-04-09T15:14:49.290728Z","shell.execute_reply":"2025-04-09T15:14:49.470514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if DEBUG:\n    npy_files = npy_files[:10]\n    print(f\"DEBUG mode enabled: Processing only the first {len(npy_files)} files.\")\n\nprint(f\"Processing files and creating {ZIP_FILENAME}...\")\n\nprocessed_count = 0\nerror_count = 0\n\n# ZIPファイルを開く (圧縮あり)\ntry:\n    with zipfile.ZipFile(ZIP_FILENAME, 'w', zipfile.ZIP_DEFLATED) as zip_file:\n        for i, npy_path in enumerate(npy_files):\n            fig = None \n            try:\n                file_name = os.path.basename(npy_path)\n                file_id = os.path.splitext(file_name)[0]\n\n                seismic_data = np.load(npy_path)\n\n                num_sources, time_steps, num_receivers = seismic_data.shape\n\n                central_source_index = num_sources // 2\n                central_source_data = seismic_data[central_source_index]\n\n                # --- AGC (時間減衰補正 + RMS) ---\n                epsilon = 1e-8 \n\n                # 1. 時間減衰補正 AGC\n                times = np.arange(time_steps) * DT\n                gain_time = (times + epsilon)**DISTANCE_DECAY_POWER\n                agc_data_time = central_source_data * gain_time[:, np.newaxis]\n\n                # 2. RMS-AGC (時間減衰補正後のデータに適用)\n                rms = np.sqrt(np.mean(agc_data_time**2, axis=1))\n                gain_rms = GAIN / (rms + epsilon)\n                agc_data_final = agc_data_time * gain_rms[:, np.newaxis]\n\n\n                # プロットの準備 (6.4インチ x 6.4インチ、100 DPIで640x640ピクセル)\n                fig, ax = plt.subplots(figsize=(3.2, 3.2))\n\n                im = ax.imshow(agc_data_final, aspect='auto', cmap='gray')\n\n                ax.set_title(f\"Seismic Data (Time AGC n={DISTANCE_DECAY_POWER}, RMS AGC G={GAIN}) - ID {file_id}, Central Source\")\n                ax.set_xlabel(\"Receivers\")\n                ax.set_ylabel(\"Timesteps\")\n\n                cbar = fig.colorbar(im, ax=ax)\n                cbar.set_label(\"Amplitude\")\n\n                buffer = io.BytesIO()\n                plt.savefig(buffer, format='png', dpi=100)\n                buffer.seek(0)\n\n                zip_file.writestr(f\"{file_id}.png\", buffer.getvalue())\n                buffer.close() # バッファを閉じる\n\n                if i < 10:\n                    print(f\"Displaying plot for {file_id}...\")\n                    plt.show()\n                \n                plt.close(fig)\n                processed_count += 1\n\n            except Exception as e:\n                print(f\"Error processing file {npy_path}: {e}\")\n                error_count += 1\n                if fig is not None and plt.fignum_exists(fig.number):\n                    plt.close(fig)\n\n\nexcept Exception as e:\n    print(f\"An error occurred during ZIP file creation: {e}\")\n\n\nprint(f\"\\nProcessing finished.\")\nif DEBUG:\n    print(\"(Ran in DEBUG mode)\")\nprint(f\"Successfully processed and added to ZIP: {processed_count} files.\")\nprint(f\"Failed to process: {error_count} files.\")\nif processed_count > 0:\n    print(f\"Output saved to {ZIP_FILENAME}\")\nelse:\n    print(f\"{ZIP_FILENAME} was not created as no files were processed successfully.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T15:15:27.764389Z","iopub.execute_input":"2025-04-09T15:15:27.764786Z","iopub.status.idle":"2025-04-09T15:15:32.002589Z","shell.execute_reply.started":"2025-04-09T15:15:27.764757Z","shell.execute_reply":"2025-04-09T15:15:32.001428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"npy_files = sorted(glob.glob(os.path.join(TEST_DATA_DIR, '*.npy')))\ntotal_files = len(npy_files)\n\nprint(f\"Found {total_files} NPY files.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-09T15:14:52.702570Z","iopub.execute_input":"2025-04-09T15:14:52.702984Z","iopub.status.idle":"2025-04-09T15:14:52.873838Z","shell.execute_reply.started":"2025-04-09T15:14:52.702938Z","shell.execute_reply":"2025-04-09T15:14:52.872863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}