{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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":84969,"databundleVersionId":10033515,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"fe9fc38a-56ef-42bc-b3b4-18ed52350fa5","cell_type":"code","source":"! pip install zarr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:08:44.709802Z","iopub.execute_input":"2024-12-28T15:08:44.710143Z","iopub.status.idle":"2024-12-28T15:08:55.343654Z","shell.execute_reply.started":"2024-12-28T15:08:44.710116Z","shell.execute_reply":"2024-12-28T15:08:55.342143Z"}},"outputs":[],"execution_count":null},{"id":"initial_id","cell_type":"code","source":"from pathlib import Path\nimport json\nimport numpy as np\nimport zarr\nimport matplotlib.pyplot as plt","metadata":{"ExecuteTime":{"end_time":"2024-12-28T14:53:39.890707Z","start_time":"2024-12-28T14:53:39.052419Z"},"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:08:55.345814Z","iopub.execute_input":"2024-12-28T15:08:55.346294Z","iopub.status.idle":"2024-12-28T15:08:55.456984Z","shell.execute_reply.started":"2024-12-28T15:08:55.346244Z","shell.execute_reply":"2024-12-28T15:08:55.455887Z"}},"outputs":[],"execution_count":null},{"id":"c3d246fbfc4a241e","cell_type":"code","source":"def get_patch_31x31(tomogram: np.ndarray, x: int, y: int, z: int):\n    \"\"\"\n    tomogram  (numpy.ndarray, shape = (D, H, W))\n    x, y, z   (int)\n    \n    tomogram[z] から 2D スライスを取り出し、\n    その上で x, y を中心とした 31×31 のパッチを返す。\n    範囲外なら False を返す。\n    \"\"\"\n    # tomogram の形状\n    depth, height, width = tomogram.shape\n    \n    # z 座標のチェック\n    if z < 0 or z >= depth:\n        return False\n    \n    # 31 × 31 のパッチは、それぞれの方向に 15ピクセルずつ余裕が必要\n    half_patch = 15\n    \n    # y 方向・x 方向が範囲をはみ出していないか確認\n    if (y - half_patch < 0) or (y + half_patch >= height):\n        return False\n    if (x - half_patch < 0) or (x + half_patch >= width):\n        return False\n    \n    # 範囲内ならパッチを切り出して返す\n    patch = tomogram[z, y - half_patch : y + half_patch + 1,\n                     x - half_patch : x + half_patch + 1]\n    \n    return patch","metadata":{"ExecuteTime":{"end_time":"2024-12-28T14:59:50.593793Z","start_time":"2024-12-28T14:59:50.590729Z"},"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:08:55.458600Z","iopub.execute_input":"2024-12-28T15:08:55.459372Z","iopub.status.idle":"2024-12-28T15:08:55.466692Z","shell.execute_reply.started":"2024-12-28T15:08:55.459326Z","shell.execute_reply":"2024-12-28T15:08:55.465224Z"}},"outputs":[],"execution_count":null},{"id":"7459ccac7d4c1ce3","cell_type":"code","source":"mode = \"train\"\nroot_dir = Path(\"/kaggle/input/czii-cryo-et-object-identification\")\n\njson_names = [\n    \"apo-ferritin.json\",\n    \"beta-amylase.json\",\n    \"beta-galactosidase.json\",\n    \"ribosome.json\",\n    \"thyroglobulin.json\",\n    \"virus-like-particle.json\",\n]\nexperiments = [\"TS_5_4\", \"TS_69_2\", \"TS_6_4\", \"TS_6_6\", \"TS_73_6\", \"TS_86_3\", \"TS_99_9\"]\n","metadata":{"ExecuteTime":{"end_time":"2024-12-28T14:59:50.209648Z","start_time":"2024-12-28T14:59:50.207029Z"},"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:19:30.843746Z","iopub.execute_input":"2024-12-28T15:19:30.844124Z","iopub.status.idle":"2024-12-28T15:19:30.849294Z","shell.execute_reply.started":"2024-12-28T15:19:30.844092Z","shell.execute_reply":"2024-12-28T15:19:30.848131Z"}},"outputs":[],"execution_count":null},{"id":"6e4483b8-a223-4f38-bee6-f1aa7583813f","cell_type":"code","source":"for json_name in json_names:\n    for offset in [0, 0.5, 1]:\n        patches = []\n        \n        for exp_name in experiments:\n            exp_dir = root_dir.joinpath(mode, \"static\", \"ExperimentRuns\", exp_name)\n            zarr_path = exp_dir.joinpath(\"VoxelSpacing10.000\", \"denoised.zarr\")\n            zarr_file = zarr.open(str(zarr_path))\n            tomogram = zarr_file[\"0\"][:]    \n            json_dir = root_dir.joinpath(mode, \"overlay\", \"ExperimentRuns\", exp_name, \"Picks\")\n            json_path = json_dir.joinpath(json_name)\n            \n            with open(json_path) as f:\n                picks = json.load(f)\n                for point in picks[\"points\"]:\n                    x = int(point[\"location\"][\"x\"] / 10.012444537618887 + offset)\n                    y = int(point[\"location\"][\"y\"] / 10.012444196428572 + offset)\n                    z = int(point[\"location\"][\"z\"] / 10.012444196428572 + offset)\n                    \n                    patch = get_patch_31x31(tomogram, x, y, z)\n                    \n                    if patch is not False:\n                        patches.append(patch)\n\n        mean_patch = np.mean(patches, axis=0)\n        mean_patch[:, 15] = 0\n        mean_patch[15, :] = 0\n\n        plt.imshow(mean_patch, cmap=\"gray\")\n        plt.title(f\"{json_name}, {offset}\")\n        plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:19:41.577011Z","iopub.execute_input":"2024-12-28T15:19:41.577432Z"}},"outputs":[],"execution_count":null},{"id":"aa3c0f83ca50786b","cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}