{"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":10104241,"sourceType":"datasetVersion","datasetId":6232544},{"sourceId":206640467,"sourceType":"kernelVersion"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install zarr","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:16.497246Z","iopub.execute_input":"2024-12-06T18:55:16.498271Z","iopub.status.idle":"2024-12-06T18:55:32.517821Z","shell.execute_reply.started":"2024-12-06T18:55:16.498228Z","shell.execute_reply":"2024-12-06T18:55:32.516618Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Introduction\n\nThis is a quick first look at the simulated data.","metadata":{}},{"cell_type":"code","source":"import fileinput\nimport json\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport zarr\n\nfrom glob import glob","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:32.520212Z","iopub.execute_input":"2024-12-06T18:55:32.520626Z","iopub.status.idle":"2024-12-06T18:55:33.936524Z","shell.execute_reply.started":"2024-12-06T18:55:32.520579Z","shell.execute_reply":"2024-12-06T18:55:33.935465Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Find the Zarrs\n\nThere are two zarrs regular data:\n* TS_0/TiltSeries/100/TS_0.zarr\n* TS_0/Reconstructions/VoxelSpacing10.000/Tomograms/100/TS_0.zarr\n\nThe first consists of only 31 layers whereas the second has 200.  Both zarrs have a mean of approximately zero, but the second has a much larger standard deviation than the first (3 vs. 0.1).  I suspect the first simulates raw tomogram data, and the second is it's reconstruction with a bunch of noise added.\n\nThe remaining zarrs are segmentation masks of each of different particles.","metadata":{}},{"cell_type":"code","source":"[f[55:] for f in glob('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/**/*.zarr', recursive=True)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:33.937873Z","iopub.execute_input":"2024-12-06T18:55:33.938469Z","iopub.status.idle":"2024-12-06T18:55:34.552367Z","shell.execute_reply.started":"2024-12-06T18:55:33.938434Z","shell.execute_reply":"2024-12-06T18:55:34.551230Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset1 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/TiltSeries/100/TS_0.zarr', mode='r')[0]\nprint(dataset1.shape)\ndataset2 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Tomograms/100/TS_0.zarr', mode='r')[0]\nprint(dataset2.shape)\n\ndataset3 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Annotations/101/ferritin_complex-1.0_segmentationmask.zarr', mode='r')[0]\nprint(dataset3.shape)\ndataset4 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Annotations/102/beta_amylase-1.0_segmentationmask.zarr', mode='r')[0]\nprint(dataset4.shape)\ndataset5 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Annotations/103/beta_galactosidase-1.0_segmentationmask.zarr', mode='r')[0]\nprint(dataset5.shape)\ndataset6 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Annotations/104/cytosolic_ribosome-1.0_segmentationmask.zarr', mode='r')[0]\nprint(dataset6.shape)\ndataset7 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Annotations/105/thyroglobulin-1.0_segmentationmask.zarr', mode='r')[0]\nprint(dataset7.shape)\ndataset8 = zarr.open('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/Reconstructions/VoxelSpacing10.000/Annotations/106/pp7_vlp-1.0_segmentationmask.zarr', mode='r')[0]\nprint(dataset8.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:34.554683Z","iopub.execute_input":"2024-12-06T18:55:34.555043Z","iopub.status.idle":"2024-12-06T18:55:34.677297Z","shell.execute_reply.started":"2024-12-06T18:55:34.555008Z","shell.execute_reply":"2024-12-06T18:55:34.675937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(np.mean(dataset1))\nprint(np.std(dataset1))\nprint(np.mean(dataset2))\nprint(np.std(dataset2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:34.678410Z","iopub.execute_input":"2024-12-06T18:55:34.678765Z","iopub.status.idle":"2024-12-06T18:55:40.742267Z","shell.execute_reply.started":"2024-12-06T18:55:34.678732Z","shell.execute_reply":"2024-12-06T18:55:40.741109Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Tilt Series Zarr\n\nAs mentioned above, the first zarr is only 31 layers, and it's possible to see detail in the images.","metadata":{}},{"cell_type":"code","source":"# temp = dataset1[1] - dataset1[0]\n# temp_orig = temp.copy()\n# alpha = 1\n# temp = 1 / (1 + np.exp(- alpha * temp**2))\n# temp = temp - np.min(temp)\n# temp = temp / np.max(temp)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T19:17:15.184677Z","iopub.execute_input":"2024-12-06T19:17:15.185088Z","iopub.status.idle":"2024-12-06T19:17:15.189969Z","shell.execute_reply.started":"2024-12-06T19:17:15.185045Z","shell.execute_reply":"2024-12-06T19:17:15.188779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = dataset1[15] - dataset1[5]\ntemp_orig = temp.copy()\ntemp = temp ** 2\nalpha = 10\ntemp = alpha * temp\ntemp = 1 / (1 + np.exp(-temp))\ntemp = (temp - 0.5) * 2\n\nplt.hist((temp).reshape(-1), bins=100);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T19:19:41.414321Z","iopub.execute_input":"2024-12-06T19:19:41.414743Z","iopub.status.idle":"2024-12-06T19:19:42.387104Z","shell.execute_reply.started":"2024-12-06T19:19:41.414705Z","shell.execute_reply":"2024-12-06T19:19:42.385627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.imshow(temp)\nplt.subplot(1,2,2)\nplt.imshow(temp_orig)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T19:19:44.514445Z","iopub.execute_input":"2024-12-06T19:19:44.514882Z","iopub.status.idle":"2024-12-06T19:19:45.142364Z","shell.execute_reply.started":"2024-12-06T19:19:44.514843Z","shell.execute_reply":"2024-12-06T19:19:45.140757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,14))\nfor i in range(31-1):\n    ax = plt.subplot(7, 5, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(dataset1[i+1] - dataset1[i], cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T19:02:26.443886Z","iopub.execute_input":"2024-12-06T19:02:26.444321Z","iopub.status.idle":"2024-12-06T19:02:47.702105Z","shell.execute_reply.started":"2024-12-06T19:02:26.444283Z","shell.execute_reply":"2024-12-06T19:02:47.700803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,14))\nfor i in range(31):\n    ax = plt.subplot(7, 5, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(dataset1[i], cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:40.743681Z","iopub.execute_input":"2024-12-06T18:55:40.744045Z","iopub.status.idle":"2024-12-06T18:55:53.817883Z","shell.execute_reply.started":"2024-12-06T18:55:40.744011Z","shell.execute_reply":"2024-12-06T18:55:53.816393Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Reconstructed Zarr\n\nThe second zarr with 200 layers is much closer to the dimensions of the training sets, however, it's very difficult to see any detail.  At first, I thought they were completely blank.","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,80))\nfor i in range(200-1):\n    ax = plt.subplot(40, 5, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.title('Layer' + str(i))\n    plt.imshow(dataset2[i+1] - dataset2[i], cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:58:57.485744Z","iopub.execute_input":"2024-12-06T18:58:57.486145Z","iopub.status.idle":"2024-12-06T19:02:16.333805Z","shell.execute_reply.started":"2024-12-06T18:58:57.486111Z","shell.execute_reply":"2024-12-06T19:02:16.330979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,80))\nfor i in range(200):\n    ax = plt.subplot(40, 5, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.title('Layer' + str(i))\n    plt.imshow(dataset2[i], cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T18:55:53.819417Z","iopub.execute_input":"2024-12-06T18:55:53.819891Z","iopub.status.idle":"2024-12-06T18:58:00.204890Z","shell.execute_reply.started":"2024-12-06T18:55:53.819835Z","shell.execute_reply":"2024-12-06T18:58:00.203824Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Apo-Ferritin Mask\n\nAs mentioned above the remaining zarrs are segmentation mask.  The following is for apo-ferritin.","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,80))\nfor i in range(200):\n    ax = plt.subplot(40, 5, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.title('Layer' + str(i))\n    plt.imshow(dataset3[i], cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:34:56.788916Z","iopub.execute_input":"2024-12-05T09:34:56.789307Z","iopub.status.idle":"2024-12-05T09:35:19.493474Z","shell.execute_reply.started":"2024-12-05T09:34:56.789272Z","shell.execute_reply":"2024-12-05T09:35:19.49222Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Apo-Ferritin\n\nSelecting layer 102, one of the busier, and comparing it with the reconstructed tomogram reveals the reconstructed tomogram is not actually blank, it just has a whole lot of noise.  The following section plot the other particles as well.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.subplot(1,2,2)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\n_ = plt.imshow(dataset3[102], cmap='Reds', alpha=0.3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:35:19.494804Z","iopub.execute_input":"2024-12-05T09:35:19.495154Z","iopub.status.idle":"2024-12-05T09:35:21.049965Z","shell.execute_reply.started":"2024-12-05T09:35:19.495102Z","shell.execute_reply":"2024-12-05T09:35:21.048836Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Beta-Amylase","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.subplot(1,2,2)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\n_ = plt.imshow(dataset4[102], cmap='Reds', alpha=0.3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:35:21.05139Z","iopub.execute_input":"2024-12-05T09:35:21.051717Z","iopub.status.idle":"2024-12-05T09:35:22.654913Z","shell.execute_reply.started":"2024-12-05T09:35:21.051685Z","shell.execute_reply":"2024-12-05T09:35:22.653813Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Beta-Galactosidase","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.subplot(1,2,2)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\n_ = plt.imshow(dataset5[102], cmap='Reds', alpha=0.3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:35:22.656393Z","iopub.execute_input":"2024-12-05T09:35:22.656799Z","iopub.status.idle":"2024-12-05T09:35:24.177452Z","shell.execute_reply.started":"2024-12-05T09:35:22.656758Z","shell.execute_reply":"2024-12-05T09:35:24.176405Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Ribosomes","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.subplot(1,2,2)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\n_ = plt.imshow(dataset6[102], cmap='Reds', alpha=0.3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:35:24.178845Z","iopub.execute_input":"2024-12-05T09:35:24.179243Z","iopub.status.idle":"2024-12-05T09:35:25.804393Z","shell.execute_reply.started":"2024-12-05T09:35:24.179203Z","shell.execute_reply":"2024-12-05T09:35:25.803389Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Thyroglobulin","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.subplot(1,2,2)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\n_ = plt.imshow(dataset7[102], cmap='Reds', alpha=0.3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:35:25.805523Z","iopub.execute_input":"2024-12-05T09:35:25.805801Z","iopub.status.idle":"2024-12-05T09:35:27.316784Z","shell.execute_reply.started":"2024-12-05T09:35:25.805774Z","shell.execute_reply":"2024-12-05T09:35:27.315731Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Virus-Like Particles","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.subplot(1,2,1)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.subplot(1,2,2)\nplt.xticks([])\nplt.yticks([])\nplt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\n_ = plt.imshow(dataset8[102], cmap='Reds', alpha=0.3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T09:35:27.318398Z","iopub.execute_input":"2024-12-05T09:35:27.318747Z","iopub.status.idle":"2024-12-05T09:35:28.891926Z","shell.execute_reply.started":"2024-12-05T09:35:27.318713Z","shell.execute_reply":"2024-12-05T09:35:28.890967Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# NDJSON (Newline Delimited JSON) Files\n\nAlso included in the dataset is a series of JSON files for each particle type.  Although the provided segmentation masks appear to outline the particles in a few cases, these files appear to provided the center points as demostrated below where the virus particles within 10 layers of 102 are plotted against the layer 102 mask.\n\nImmediately below are the locations of the .ndjson files:","metadata":{}},{"cell_type":"code","source":"[f[55:] for f in glob('/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0/**/*.ndjson', recursive=True)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T22:52:16.92752Z","iopub.execute_input":"2024-12-05T22:52:16.928579Z","iopub.status.idle":"2024-12-05T22:52:17.177938Z","shell.execute_reply.started":"2024-12-05T22:52:16.928531Z","shell.execute_reply":"2024-12-05T22:52:17.176715Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Next, I filter out the points within 10 layers of 102.  Note that these values appear to be unscaled so there's no need to divide by 10.  Also important, the scaling for the simulated data appears to be 10.000 instead of the 10.012 for the unsimulated data.","metadata":{}},{"cell_type":"code","source":"x = []\ny = []\nTS_0_ROOT = '/kaggle/input/czii-cryoet-simulated-training-data-ts-0/TS_0'\nfor line in fileinput.input(files=[TS_0_ROOT\n        + '/Reconstructions/VoxelSpacing10.000/Annotations/106/pp7_vlp-1.0_orientedpoint.ndjson']):\n    point = json.loads(line)['location']\n    layer = float(point['z'])\n    if layer > 92.0 and layer < 112.0:\n        print(point)\n        x.append(float(point['x']))\n        y.append(float(point['y']))\n        full_point = line","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T23:13:30.462409Z","iopub.execute_input":"2024-12-05T23:13:30.462869Z","iopub.status.idle":"2024-12-05T23:13:30.476082Z","shell.execute_reply.started":"2024-12-05T23:13:30.462821Z","shell.execute_reply":"2024-12-05T23:13:30.474689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(dataset2[102], cmap='gray', vmin=-3, vmax=3)\nplt.imshow(dataset8[102], cmap='Reds', alpha=0.3)\n_ = plt.scatter(x, y, color='yellow')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T23:19:24.192537Z","iopub.execute_input":"2024-12-05T23:19:24.193046Z","iopub.status.idle":"2024-12-05T23:19:25.148068Z","shell.execute_reply.started":"2024-12-05T23:19:24.193007Z","shell.execute_reply":"2024-12-05T23:19:25.14686Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Full JSON\n\nThe full JSON a point is shown below.  I'm not sure what to do with the non-identity xyz_rotation_matrix, but it seems like we may be able to ignore it.","metadata":{}},{"cell_type":"code","source":"lines = json.dumps(json.loads(full_point), indent=4).split('\\n')\nfor l in lines:\n    print(l)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T23:16:27.711726Z","iopub.execute_input":"2024-12-05T23:16:27.712117Z","iopub.status.idle":"2024-12-05T23:16:27.719225Z","shell.execute_reply.started":"2024-12-05T23:16:27.712086Z","shell.execute_reply":"2024-12-05T23:16:27.717684Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Generating Spherical Multi-Class Label Masks\n\nI believe the label masks used by many of the teams are spherical including all of the classes together as shown below from the excellent Notebook from @fnands, [https://www.kaggle.com/code/fnands/create-numpy-dataset-exp-name](https://www.kaggle.com/code/fnands/create-numpy-dataset-exp-name).  With that in mind, it seems reasonable to do the same with the simulated data instead of using the provided non-spherical maps.","metadata":{}},{"cell_type":"code","source":"mask = zarr.open('/kaggle/input/create-numpy-dataset-exp-name/overlay/ExperimentRuns/TS_86_3/Segmentations/10.000_copickUtils_0_paintedPicks-multilabel.zarr', mode='r')[0]\n_ = plt.imshow(mask[92])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T23:37:50.982618Z","iopub.execute_input":"2024-12-05T23:37:50.983046Z","iopub.status.idle":"2024-12-05T23:37:51.308994Z","shell.execute_reply.started":"2024-12-05T23:37:50.983009Z","shell.execute_reply":"2024-12-05T23:37:51.307816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TBD - Check back soon.","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}