{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Setup\n### Use this tool to look at the training sample velocity and seismic plots","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np  # linear algebra\nimport pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom pathlib import Path\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch import nn\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import mean_squared_error\nimport seaborn as sns\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# Ensure code runs on CPU to avoid libnccl error\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\"\ndevice = torch.device(\"cpu\")\n\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-24T18:12:37.203715Z","iopub.execute_input":"2025-04-24T18:12:37.204014Z","iopub.status.idle":"2025-04-24T18:12:43.699624Z","shell.execute_reply.started":"2025-04-24T18:12:37.203985Z","shell.execute_reply":"2025-04-24T18:12:43.698925Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load","metadata":{}},{"cell_type":"code","source":"# Collect input files\ninput_dir = Path('/kaggle/input/waveform-inversion/train_samples')\ndata_files = []\nmodel_files = []\nsource_paths = []  # New array to track data/model paths\n\n# Manually define Vel and Style subdirectories\nvel_style_dirs = [\n    'FlatVel_A', 'FlatVel_B', 'CurveVel_A', 'CurveVel_B',\n    'Style_A', 'Style_B'\n]\n\n# Add files from Vel and Style families\nfor subdir in vel_style_dirs:\n    data_subdir_files = sorted((input_dir / subdir / 'data').glob('*.npy'))\n    model_subdir_files = sorted((input_dir / subdir / 'model').glob('*.npy'))\n    data_files += data_subdir_files\n    model_files += model_subdir_files\n    source_paths += [(str(d), str(m)) for d, m in zip(data_subdir_files, model_subdir_files)]\n\n# Add files from Fault family\nfault_data_files = sorted(input_dir.rglob('seis*.npy'))\nfault_model_files = []\nfor f in fault_data_files:\n    model_path = Path(str(f).replace(\"seis\", \"vel\"))\n    if model_path.exists():\n        fault_model_files.append(model_path)\n        data_files.append(f)\n        model_files.append(model_path)\n        source_paths.append((str(f), str(model_path)))\n\nprint(\"Data files:\")\nfor f in data_files:\n    print(f)\n\nprint(\"Model files:\")\nfor f in model_files:\n    print(f)\n\n# Load data\nX_data = []\ny_data = []\nfor data_path, model_path in zip(data_files, model_files):\n    X = np.load(data_path)  # (500, 5, 1000, 70)\n    y = np.load(model_path)  # (500, 1, 70, 70) or (500, 70, 70)\n    if y.ndim == 4:\n        y = np.squeeze(y, axis=1)\n    X_data.append(X)\n    y_data.append(y)\n\nX_data = np.concatenate(X_data, axis=0)\ny_data = np.concatenate(y_data, axis=0)\nsource_paths = source_paths\n\nprint(\"Seismic Data shape:\", X_data.shape)\nprint(\"Velocity Data shape:\", y_data.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-24T18:25:56.900158Z","iopub.execute_input":"2025-04-24T18:25:56.903842Z","iopub.status.idle":"2025-04-24T18:27:37.044151Z","shell.execute_reply.started":"2025-04-24T18:25:56.903771Z","shell.execute_reply":"2025-04-24T18:27:37.042947Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualize","metadata":{}},{"cell_type":"code","source":"# Plot every 500 samples\nex_num = 0\n\nfor ex_num in range(0, len(X_data), 500):\n    X_example = X_data[ex_num]  # shape (5, 1000, 70)\n    y_example = y_data[ex_num]  # shape (70, 70)\n    file_num = int(ex_num/500)\n    source_pair = source_paths[file_num]\n    print(source_pair[1])\n\n    # Plot velocity model\n    from matplotlib.colors import ListedColormap\n    fig, ax = plt.subplots(1, 1, figsize=(11, 5))\n    img = ax.imshow(y_example, cmap='viridis')\n    ax.set_xticks(range(0, 70, 10))\n    ax.set_xticklabels(range(0, 700, 100))\n    ax.set_yticks(range(0, 70, 10))\n    ax.set_yticklabels(range(0, 700, 100))\n    ax.set_ylabel('Depth (m)', fontsize=12)\n    ax.set_xlabel('Offset (m)', fontsize=12)\n    clb = plt.colorbar(img, ax=ax)\n    clb.ax.set_title('km/s', fontsize=8)\n    plt.show()\n\n    print(source_pair[0])\n    # Plot seismic data channels\n    fig, ax = plt.subplots(1, 5, figsize=(20, 5))\n    for i in range(5):\n        ax[i].imshow(X_example[i], extent=[0, 70, 1000, 0], aspect='auto', cmap='gray', vmin=-0.5, vmax=0.5)\n        ax[i].set_xticks(range(0, 70, 10))\n        ax[i].set_xticklabels(range(0, 700, 100))\n        ax[i].set_yticks(range(0, 2000, 1000))\n        ax[i].set_yticklabels(range(0, 2, 1))\n        ax[i].set_ylabel('Time (s)', fontsize=12)\n        ax[i].set_xlabel('Offset (m)', fontsize=12)\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-24T18:50:47.303543Z","iopub.execute_input":"2025-04-24T18:50:47.304479Z","iopub.status.idle":"2025-04-24T18:51:05.179520Z","shell.execute_reply.started":"2025-04-24T18:50:47.304443Z","shell.execute_reply":"2025-04-24T18:51:05.178428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Seismic Data memory size (MB):\", X_data.nbytes / (1024 ** 2))\nprint(\"Velocity Data memory size (MB):\", y_data.nbytes / (1024 ** 2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-22T16:26:08.091510Z","iopub.execute_input":"2025-04-22T16:26:08.092767Z","iopub.status.idle":"2025-04-22T16:26:08.099402Z","shell.execute_reply.started":"2025-04-22T16:26:08.092718Z","shell.execute_reply":"2025-04-22T16:26:08.098419Z"}},"outputs":[],"execution_count":null}]}