{"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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# I share this notebook with you to help beginner in understanding input data\n# I hope this helps, Enjoy !","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport time\nfrom tqdm import tqdm\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-16T11:11:40.833375Z","iopub.execute_input":"2024-11-16T11:11:40.834313Z","iopub.status.idle":"2024-11-16T11:11:41.867734Z","shell.execute_reply.started":"2024-11-16T11:11:40.834272Z","shell.execute_reply":"2024-11-16T11:11:41.866698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install zarr","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:41.869156Z","iopub.execute_input":"2024-11-16T11:11:41.869641Z","iopub.status.idle":"2024-11-16T11:11:58.024988Z","shell.execute_reply.started":"2024-11-16T11:11:41.869592Z","shell.execute_reply":"2024-11-16T11:11:58.023812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zarr\nimport json\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:58.026563Z","iopub.execute_input":"2024-11-16T11:11:58.026949Z","iopub.status.idle":"2024-11-16T11:11:58.694840Z","shell.execute_reply.started":"2024-11-16T11:11:58.026909Z","shell.execute_reply":"2024-11-16T11:11:58.693842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Exp_path = '/kaggle/input/czii-cryo-et-object-identification/train/overlay/ExperimentRuns/'\nResolution = (184,630,630)   # Z, Y, X\nfilenames = [\n    'apo-ferritin.json',\n    'beta-amylase.json',\n    'beta-galactosidase.json',\n    'ribosome.json',\n    'thyroglobulin.json',\n    'virus-like-particle.json']\nprot_sizes = [60, 65, 90, 150, 130, 135]\nprot_weight = [1, 0, 2, 1, 2, 1]\nexperiment_list = ['TS_5_4', 'TS_69_2', 'TS_6_4', 'TS_6_6', 'TS_73_6', 'TS_86_3', 'TS_99_9']","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:58.697889Z","iopub.execute_input":"2024-11-16T11:11:58.698955Z","iopub.status.idle":"2024-11-16T11:11:58.704833Z","shell.execute_reply.started":"2024-11-16T11:11:58.698915Z","shell.execute_reply":"2024-11-16T11:11:58.703754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_jsons(path, json_list=filenames):\n    json_data = {}\n    for filename in filenames:\n        file_path = os.path.join(path, filename)\n        with open(file_path, 'r') as file:\n            json_data[filename] = json.load(file)\n    return json_data\n","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:58.706518Z","iopub.execute_input":"2024-11-16T11:11:58.707260Z","iopub.status.idle":"2024-11-16T11:11:58.718647Z","shell.execute_reply.started":"2024-11-16T11:11:58.707211Z","shell.execute_reply":"2024-11-16T11:11:58.717613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_mask(experiment):\n    # Load protein positions\n    protein_pos = load_jsons(Exp_path+experiment+'/Picks')\n    mask = np.zeros(Resolution, dtype=np.uint8)\n    \n    # Fill the mask\n    for i, prot in enumerate(protein_pos):\n        size = 1+prot_sizes[i]//20\n        print(i, prot, size)\n        for point in protein_pos[prot]['points']:\n            loc = point['location']\n            X, Y, Z = int(loc['x'])//10, int(loc['y'])//10, int(loc['z'])//10\n            mask[np.clip(Z-size,0,Resolution[0]): np.clip(Z+size,0,Resolution[0]), \n                 np.clip(Y-size,0,Resolution[1]): np.clip(Y+size,0,Resolution[1]), \n                 np.clip(X-size,0,Resolution[2]): np.clip(X+size,0,Resolution[2])] = i+1\n    \n    return mask\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:58.719987Z","iopub.execute_input":"2024-11-16T11:11:58.720316Z","iopub.status.idle":"2024-11-16T11:11:58.729055Z","shell.execute_reply.started":"2024-11-16T11:11:58.720281Z","shell.execute_reply":"2024-11-16T11:11:58.728082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualisation test","metadata":{}},{"cell_type":"code","source":"experiment = experiment_list[0]\nprint(experiment)\nmask = create_mask(experiment)","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:58.730259Z","iopub.execute_input":"2024-11-16T11:11:58.730669Z","iopub.status.idle":"2024-11-16T11:11:58.793335Z","shell.execute_reply.started":"2024-11-16T11:11:58.730622Z","shell.execute_reply":"2024-11-16T11:11:58.792275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zarr_file = zarr.open('/kaggle/input/czii-cryo-et-object-identification/test/static/ExperimentRuns/'+\n                      experiment+'/VoxelSpacing10.000/denoised.zarr', mode='r')\nprint(zarr_file.tree())\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:11:58.794802Z","iopub.execute_input":"2024-11-16T11:11:58.795308Z","iopub.status.idle":"2024-11-16T11:11:58.893813Z","shell.execute_reply.started":"2024-11-16T11:11:58.795259Z","shell.execute_reply":"2024-11-16T11:11:58.892732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.subplots as sp\nimport plotly.graph_objects as go\n\ndef display_mask(img1, img2, dir='h'):\n    if dir=='v':\n        row, col = 2, 1\n    else:\n        row, col = 1, 2\n    fig = sp.make_subplots(rows=row, cols=col, subplot_titles=(\"Zarr file\", \"Particle masks\"))\n    fig.add_trace(go.Heatmap(z=img1, zmax=50e-6, zmin=-150e-6, coloraxis=\"coloraxis1\"), row=1, col=1)\n    fig.add_trace(go.Heatmap(z=img2, coloraxis=\"coloraxis2\"), row=row, col=col)\n    fig.update_layout(title=\"Display mask\", coloraxis_showscale=False,\n                    xaxis=dict(scaleanchor=\"y\", matches=\"x2\"),\n                    yaxis=dict(scaleratio=1, matches=\"y2\"),\n                    xaxis2=dict(scaleanchor=\"y2\"),\n                    yaxis2=dict(scaleratio=1))\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:14:13.565305Z","iopub.execute_input":"2024-11-16T11:14:13.565772Z","iopub.status.idle":"2024-11-16T11:14:13.573676Z","shell.execute_reply.started":"2024-11-16T11:14:13.565731Z","shell.execute_reply":"2024-11-16T11:14:13.572613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Zcut = 50\ndisplay_mask(zarr_file[0][Zcut], mask[Zcut])","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:14:14.241436Z","iopub.execute_input":"2024-11-16T11:14:14.242412Z","iopub.status.idle":"2024-11-16T11:14:14.819004Z","shell.execute_reply.started":"2024-11-16T11:14:14.242369Z","shell.execute_reply":"2024-11-16T11:14:14.817902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Zcut = 70\ndisplay_mask(zarr_file[0][Zcut], mask[Zcut])","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:14:14.977761Z","iopub.execute_input":"2024-11-16T11:14:14.978505Z","iopub.status.idle":"2024-11-16T11:14:15.550218Z","shell.execute_reply.started":"2024-11-16T11:14:14.978444Z","shell.execute_reply":"2024-11-16T11:14:15.548244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Ycut = 120\ndisplay_mask(zarr_file[0][:,Ycut,:], mask[:,Ycut,:], dir='v')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:14:15.552143Z","iopub.execute_input":"2024-11-16T11:14:15.552608Z","iopub.status.idle":"2024-11-16T11:14:15.779110Z","shell.execute_reply.started":"2024-11-16T11:14:15.552561Z","shell.execute_reply":"2024-11-16T11:14:15.777798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xcut = 229\ndisplay_mask(zarr_file[0][:,:, Xcut], mask[:,:, Xcut], dir='v')","metadata":{"execution":{"iopub.status.busy":"2024-11-16T11:14:16.035945Z","iopub.execute_input":"2024-11-16T11:14:16.036336Z","iopub.status.idle":"2024-11-16T11:14:16.249607Z","shell.execute_reply.started":"2024-11-16T11:14:16.036298Z","shell.execute_reply":"2024-11-16T11:14:16.248563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}