{"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"},{"sourceId":10010692,"sourceType":"datasetVersion","datasetId":6162896}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -U git+https://github.com/copick/copick-utils.git@8936b5b9379bf4f58d49ac58a9669076baeeff5b copick==0.7.0 cupy-cuda12x==13.3.0 tqdm matplotlib -qqq","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:22:25.227654Z","iopub.execute_input":"2024-12-12T00:22:25.228159Z","iopub.status.idle":"2024-12-12T00:23:37.553880Z","shell.execute_reply.started":"2024-12-12T00:22:25.228072Z","shell.execute_reply":"2024-12-12T00:23:37.552374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import copick\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom copick_utils.segmentation import segmentation_from_picks\nfrom tqdm.auto import tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:24:32.760829Z","iopub.execute_input":"2024-12-12T00:24:32.761273Z","iopub.status.idle":"2024-12-12T00:24:32.767556Z","shell.execute_reply.started":"2024-12-12T00:24:32.761234Z","shell.execute_reply":"2024-12-12T00:24:32.765728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CONFIG_PATH = \"/kaggle/working/copick.config\"\nRESOLUTION = 2\nSESSION_ID = \"0\"\nTOMO_TYPE = \"wbp\" \nUSER_ID = \"curation\"\nVOXEL_SIZE =  10\nVOXEL_SPACING = VOXEL_SIZE *  (2**RESOLUTION)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:23:42.138634Z","iopub.execute_input":"2024-12-12T00:23:42.139675Z","iopub.status.idle":"2024-12-12T00:23:42.145973Z","shell.execute_reply.started":"2024-12-12T00:23:42.139619Z","shell.execute_reply":"2024-12-12T00:23:42.144688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"config_blob = \"\"\"{\n    \"name\": \"czii_cryoet_mlchallenge_2024\",\n    \"description\": \"2024 CZII CryoET ML Challenge training data.\",\n    \"version\": \"1.0.0\",\n    \"pickable_objects\": [\n        {\n            \"name\": \"apo-ferritin\",\n            \"identifier\": \"GO:0070288\",\n            \"is_particle\": true,\n            \"pdb_id\": \"4V1W\",\n            \"label\": 1,\n            \"color\": [  0, 117, 220, 128],\n            \"radius\": 60,\n            \"map_threshold\": 0.0418\n        },\n        {\n            \"name\": \"beta-amylase\",\n            \"identifier\": \"UniProtKB:P10537\",\n            \"is_particle\": true,\n            \"pdb_id\": \"1FA2\",\n            \"label\": 2,\n            \"color\": [153,  63,   0, 128],\n            \"radius\": 65,\n            \"map_threshold\": 0.035\n        },\n        {\n            \"name\": \"beta-galactosidase\",\n            \"identifier\": \"UniProtKB:P00722\",\n            \"is_particle\": true,\n            \"pdb_id\": \"6X1Q\",\n            \"label\": 3,\n            \"color\": [ 76,   0,  92, 128],\n            \"radius\": 90,\n            \"map_threshold\": 0.0578\n        },\n        {\n            \"name\": \"ribosome\",\n            \"identifier\": \"GO:0022626\",\n            \"is_particle\": true,\n            \"pdb_id\": \"6EK0\",\n            \"label\": 4,\n            \"color\": [  0,  92,  49, 128],\n            \"radius\": 150,\n            \"map_threshold\": 0.0374\n        },\n        {\n            \"name\": \"thyroglobulin\",\n            \"identifier\": \"UniProtKB:P01267\",\n            \"is_particle\": true,\n            \"pdb_id\": \"6SCJ\",\n            \"label\": 5,\n            \"color\": [ 43, 206,  72, 128],\n            \"radius\": 130,\n            \"map_threshold\": 0.0278\n        },\n        {\n            \"name\": \"virus-like-particle\",\n            \"identifier\": \"GO:0170047\",\n            \"is_particle\": true,\n            \"pdb_id\": \"6N4V\",            \n            \"label\": 6,\n            \"color\": [255, 204, 153, 128],\n            \"radius\": 135,\n            \"map_threshold\": 0.201\n        }\n    ],\n    \"overlay_root\": \"/kaggle/working/portal/overlay/\",\n    \"static_root\": \"/kaggle/input/czcdp10441/overlay/\",\n    \"overlay_fs_args\": {\n        \"auto_mkdir\": true\n    }\n}\"\"\"\nwith open(CONFIG_PATH, \"w\") as f:\n    f.write(config_blob)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:23:42.147450Z","iopub.execute_input":"2024-12-12T00:23:42.147875Z","iopub.status.idle":"2024-12-12T00:23:43.095555Z","shell.execute_reply.started":"2024-12-12T00:23:42.147838Z","shell.execute_reply":"2024-12-12T00:23:43.094260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"root = copick.from_file(CONFIG_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:23:43.098005Z","iopub.execute_input":"2024-12-12T00:23:43.098416Z","iopub.status.idle":"2024-12-12T00:23:43.112332Z","shell.execute_reply.started":"2024-12-12T00:23:43.098382Z","shell.execute_reply":"2024-12-12T00:23:43.111188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dicts = []\nfor run in tqdm(root.runs):\n    tomogram = run.get_voxel_spacing(VOXEL_SIZE).get_tomogram(TOMO_TYPE).numpy(str(RESOLUTION))\n    segmentation = np.zeros(tomogram.shape)\n    for obj in root.pickable_objects:\n        if not obj.is_particle:\n            continue\n        pick = run.get_picks(object_name=obj.name, user_id=USER_ID)\n        if len(pick):\n            segmentation = segmentation_from_picks.from_picks(\n                    pick[0], segmentation, obj.radius, obj.label, voxel_spacing=VOXEL_SPACING)\n            segmentation = (segmentation > 0).astype(int)\n    data_dicts.append({\"image\": tomogram, \"label\": segmentation.astype(\"uint8\")})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:24:37.025244Z","iopub.execute_input":"2024-12-12T00:24:37.025636Z","iopub.status.idle":"2024-12-12T00:24:43.937048Z","shell.execute_reply.started":"2024-12-12T00:24:37.025604Z","shell.execute_reply":"2024-12-12T00:24:43.935946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tomogram = data_dicts[0][\"image\"]\nmask = data_dicts[0][\"label\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:24:49.827423Z","iopub.execute_input":"2024-12-12T00:24:49.827847Z","iopub.status.idle":"2024-12-12T00:24:49.833627Z","shell.execute_reply.started":"2024-12-12T00:24:49.827808Z","shell.execute_reply":"2024-12-12T00:24:49.832135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(30, 50))\nfor i in range(3):\n    axes[i, 0].imshow(tomogram[27+i], cmap='gray')\n    axes[i, 0].set_title(\"Tomogram\")\n    \n    axes[i, 1].imshow(mask[27+i], cmap='gray')\n    axes[i, 1].set_title(\"Mask\")\n\nplt.tight_layout(rect=[0, 0, 1, 0.97])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T00:34:36.341886Z","iopub.execute_input":"2024-12-12T00:34:36.343456Z","iopub.status.idle":"2024-12-12T00:34:39.430377Z","shell.execute_reply.started":"2024-12-12T00:34:36.343392Z","shell.execute_reply":"2024-12-12T00:34:39.429039Z"}},"outputs":[],"execution_count":null}]}