{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q zarr\n!pip install -q ome-zarr\n!pip install -q copick","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:18:51.252348Z","iopub.execute_input":"2024-12-06T07:18:51.252937Z","iopub.status.idle":"2024-12-06T07:19:54.014680Z","shell.execute_reply.started":"2024-12-06T07:18:51.252875Z","shell.execute_reply":"2024-12-06T07:19:54.013480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# First, let's import the necessary libraries\nimport zarr\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport json","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:20:33.025061Z","iopub.execute_input":"2024-12-06T07:20:33.025482Z","iopub.status.idle":"2024-12-06T07:20:33.152949Z","shell.execute_reply.started":"2024-12-06T07:20:33.025443Z","shell.execute_reply":"2024-12-06T07:20:33.151329Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## ファイル構造\n\n```\n.\n├── train/\n│   ├── overlay(json)/\n│   │   └── exp/\n│   │       ├── TS_5_4(実験名)/\n│   │       │   └── Picks/\n│   │       │       ├── apo-ferritin.json <-\n│   │       │       ├── beta-amylase\n│   │       │       └── beta-galactosidase\n│   │       ├── TS_6_4\n│   │       ├── TS_6_6\n│   │       └── ...\n│   └── static/\n│       └── exp/\n│           ├── TS_5_4/\n│           │   └── Voxel/\n│           │       ├── denoised.zarr(テストデータにはこれだけ)/\n│           │       │   ├── 0(最高画質) <-\n│           │       │   ├── 1\n│           │       │   └── 2\n│           │       ├── ctfdeconvolved.zarr\n│           │       ├── isonetcorrected.zarr\n│           │       └── wbp.zarr\n│           ├── TS_6_4\n│           ├── TS_6_6\n│           └── ...\n└── test\n```\n\n種類がいろいろあるが、\n1. テストデータにはdenoisedしかないので他はいらなそう？\n2. 最高画質の0だけでよさそう","metadata":{}},{"cell_type":"code","source":"# example\nEXP_NAME = 'TS_6_4' # TS_5_4, TS_6_4, TS_6_6, TS_99_9, TS_73_6, TS_86_3, TS_99_9,\nPARTICLE_NAME = 'apo-ferritin' # apo-ferritin, beta-amylase, beta-galactosidase, ribosome, thyroglobulin, virus-like-particle","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:20:35.344630Z","iopub.execute_input":"2024-12-06T07:20:35.345439Z","iopub.status.idle":"2024-12-06T07:20:35.351230Z","shell.execute_reply.started":"2024-12-06T07:20:35.345390Z","shell.execute_reply":"2024-12-06T07:20:35.349974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tomogram_path = f\"/kaggle/input/czii-cryo-et-object-identification/train/static/ExperimentRuns/{EXP_NAME}/VoxelSpacing10.000/denoised.zarr\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:20:39.024561Z","iopub.execute_input":"2024-12-06T07:20:39.024974Z","iopub.status.idle":"2024-12-06T07:20:39.030502Z","shell.execute_reply.started":"2024-12-06T07:20:39.024936Z","shell.execute_reply":"2024-12-06T07:20:39.029190Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"zarr_store = zarr.open(tomogram_path)\ntomogram = zarr_store['0'][:] #これでarrayとして取り出せる.\nprint(type(tomogram))\nprint(tomogram.shape)\n\n# <class 'numpy.ndarray'>\n# (184, 630, 630) (z, x, y)であってるはず？ <- x\n# (184, 630, 630) (z, y, x) thank you for the coment @min fuka !","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:20:40.818893Z","iopub.execute_input":"2024-12-06T07:20:40.819306Z","iopub.status.idle":"2024-12-06T07:20:43.820087Z","shell.execute_reply.started":"2024-12-06T07:20:40.819270Z","shell.execute_reply":"2024-12-06T07:20:43.818851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(tomogram.shape)\nplt.figure(figsize=(16, 10))\nplt.subplot(131)\nplt.imshow(tomogram[0, :, :])\nplt.title(\"yx\")\nplt.subplot(132)\nplt.imshow(tomogram[:, 0, :])\nplt.title(\"zx\")\n\nplt.subplot(133)\nplt.imshow(tomogram[:, :, 0])\nplt.title(\"zy\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:10.259303Z","iopub.execute_input":"2024-12-06T07:28:10.260254Z","iopub.status.idle":"2024-12-06T07:28:11.042946Z","shell.execute_reply.started":"2024-12-06T07:28:10.260208Z","shell.execute_reply":"2024-12-06T07:28:11.041857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:14.922034Z","iopub.execute_input":"2024-12-06T07:28:14.922449Z","iopub.status.idle":"2024-12-06T07:28:14.927740Z","shell.execute_reply.started":"2024-12-06T07:28:14.922411Z","shell.execute_reply":"2024-12-06T07:28:14.926276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PARTICLE_PATH = f\"/kaggle/input/czii-cryo-et-object-identification/train/overlay/ExperimentRuns/{EXP_NAME}/Picks/{PARTICLE_NAME}.json\"\n\nwith open(PARTICLE_PATH, 'r', encoding='utf-8') as f:\n    data = json.load(f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:16.157881Z","iopub.execute_input":"2024-12-06T07:28:16.158261Z","iopub.status.idle":"2024-12-06T07:28:16.177119Z","shell.execute_reply.started":"2024-12-06T07:28:16.158227Z","shell.execute_reply":"2024-12-06T07:28:16.176101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:19.796904Z","iopub.execute_input":"2024-12-06T07:28:19.797263Z","iopub.status.idle":"2024-12-06T07:28:19.803885Z","shell.execute_reply.started":"2024-12-06T07:28:19.797232Z","shell.execute_reply":"2024-12-06T07:28:19.802700Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"* unitって何？\n* とりあえずlocationだけでよさそう\n* transformationは要らないそう","metadata":{}},{"cell_type":"code","source":"coords = []\n\nfor point in data['points']:\n    \n    point_x = point['location']['x']/10 # 10で割るのは単位が1Å(angstrom)だとかなんだとか\n    point_y = point['location']['y']/10\n    point_z = point['location']['z']//10 # 切り捨てでも大差なさそう？\n\n    coords.append([point_x, point_y, point_z])\n    \ncoords = sorted(coords, key=lambda x: x[2]) # zでソート\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:26.593791Z","iopub.execute_input":"2024-12-06T07:28:26.594177Z","iopub.status.idle":"2024-12-06T07:28:26.600846Z","shell.execute_reply.started":"2024-12-06T07:28:26.594142Z","shell.execute_reply":"2024-12-06T07:28:26.599572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(coords)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:28.966995Z","iopub.execute_input":"2024-12-06T07:28:28.967409Z","iopub.status.idle":"2024-12-06T07:28:28.973315Z","shell.execute_reply.started":"2024-12-06T07:28:28.967343Z","shell.execute_reply":"2024-12-06T07:28:28.972192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for coord in coords:\n    plt.imshow(tomogram[int(coord[2]), :, :], cmap='gray')\n    plt.scatter(coord[0], coord[1], edgecolors='red', facecolors='none')\n    plt.title(f'{EXP_NAME} {PARTICLE_NAME} z={int(coord[2])}')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T07:28:30.704643Z","iopub.execute_input":"2024-12-06T07:28:30.705054Z","iopub.status.idle":"2024-12-06T07:28:49.711621Z","shell.execute_reply.started":"2024-12-06T07:28:30.705017Z","shell.execute_reply":"2024-12-06T07:28:49.710312Z"}},"outputs":[],"execution_count":null}]}