{"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":"markdown","source":"# Overview\nThis notebook visualizes particle data with correct radii for all data resolutions (high, medium, low).","metadata":{}},{"cell_type":"code","source":"!pip install zarr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T15:50:42.163727Z","iopub.execute_input":"2024-11-28T15:50:42.164237Z","iopub.status.idle":"2024-11-28T15:50:42.170405Z","shell.execute_reply.started":"2024-11-28T15:50:42.164159Z","shell.execute_reply":"2024-11-28T15:50:42.169067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport json\nimport matplotlib.pyplot as plt\nimport zarr","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-28T15:50:48.679839Z","iopub.execute_input":"2024-11-28T15:50:48.680284Z","iopub.status.idle":"2024-11-28T15:50:48.793599Z","shell.execute_reply.started":"2024-11-28T15:50:48.680244Z","shell.execute_reply":"2024-11-28T15:50:48.792315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"### Paths to data\nTRAIN_BASE = '/kaggle/input/czii-cryo-et-object-identification/train/static/ExperimentRuns'\nTEST_BASE = '/kaggle/input/czii-cryo-et-object-identification/test/static/ExperimentRuns'\nOVERLAY_BASE = '/kaggle/input/czii-cryo-et-object-identification/train/overlay/ExperimentRuns'\n\n### Radii of particles of different types\n### (taken from public notebook provided by the team)\nPARTICLE_RADIUS = {\n    'ribosome': 150.0,\n    'virus-like-particle': 135.0,\n    'apo-ferritin': 60.0,\n    'beta-galactosidase': 90.0,\n    'thyroglobulin': 130.0,\n}\n\n### For visualization\nPARTICLE_COLOR = {\n    'ribosome': 'yellow',\n    'virus-like-particle': 'blue',\n    'apo-ferritin': 'green',\n    'beta-galactosidase': 'red',\n    'thyroglobulin': 'pink',\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T15:53:04.030696Z","iopub.execute_input":"2024-11-28T15:53:04.031660Z","iopub.status.idle":"2024-11-28T15:53:04.038056Z","shell.execute_reply.started":"2024-11-28T15:53:04.031617Z","shell.execute_reply":"2024-11-28T15:53:04.037043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"### Utils\n\ndef get_training_experiments():\n    return os.listdir(TRAIN_BASE)\n    \ndef get_test_experiments():\n    return os.listdir(TEST_BASE)\n\ndef get_experiment_path(experiment, type='denoised', test=False):\n    base = TEST_BASE if test else TRAIN_BASE\n    return base + '/' + experiment + '/VoxelSpacing10.000/' + type + '.zarr'\n\ndef open_experiment(experiment, test=False):\n    return zarr.open(get_experiment_path(experiment, test=test), mode='r')\n\ndef get_label_path(experiment, particle_type):\n    return OVERLAY_BASE + '/' + experiment + '/Picks/' + particle_type + '.json'\n\ndef open_labels_file(experiment, particle_type):\n    path = get_label_path(experiment, particle_type)\n    with open(path, 'r') as f:\n        data = json.load(f)\n    return data\n\ndef get_labels(experiment, scaling_factor=10):\n    labels = {}\n    for particle_type in PARTICLE_RADIUS.keys():\n    \n        ### Load file with labels\n        raw_data = open_labels_file(experiment, particle_type)\n    \n        ### Save\n        labels[particle_type] = {}\n        for item in raw_data['points']:\n            layer = int(item['location']['z'] // scaling_factor)\n        \n            if layer not in labels[particle_type].keys():\n                labels[particle_type][layer] = []\n            labels[particle_type][layer].append({'x': int(item['location']['x'] // scaling_factor), 'y': int(item['location']['y'] // scaling_factor),})\n    return labels\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T16:37:38.555669Z","iopub.execute_input":"2024-11-28T16:37:38.556098Z","iopub.status.idle":"2024-11-28T16:37:38.567655Z","shell.execute_reply.started":"2024-11-28T16:37:38.556051Z","shell.execute_reply":"2024-11-28T16:37:38.566176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"### Utils for vizualization\n\nquality_to_scaling_factor = {\n    'high': 10,\n    'medium': 20,\n    'low': 40,\n}\nquality_to_dataset_index = {\n    'high': 0,\n    'medium': 1,\n    'low': 2,\n}\n\ndef viz_annotation(image, layer, quality):\n    scaling_factor = quality_to_scaling_factor[quality] \n    i = quality_to_dataset_index[quality]\n    \n    print('Image size: {}'.format(image[i].shape))\n    assert layer < image[i].shape[0]\n    \n    labels = get_labels(experiment, scaling_factor=scaling_factor)\n    \n    fig = plt.figure(figsize=(10,10))\n    \n    plt.title(\"Layer {}, {} quality\".format(str(layer), quality))\n    plt.imshow(image[i][layer], cmap='gray', vmin=-0.00005, vmax=0.00005)\n    ax = plt.gca()\n    \n    for particle_type in labels.keys():\n        coordinates = labels[particle_type].get( int(layer) )\n        radius = PARTICLE_RADIUS[particle_type] / scaling_factor\n        \n        if coordinates:\n            for xy in coordinates:\n\n                circle = plt.Circle((xy['x'], xy['y']), radius, color=PARTICLE_COLOR[particle_type], \n                                    fill=False, label=particle_type)\n                ax.add_patch(circle)\n                \n                \n    ax.legend()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T16:43:29.088506Z","iopub.execute_input":"2024-11-28T16:43:29.089192Z","iopub.status.idle":"2024-11-28T16:43:29.099210Z","shell.execute_reply.started":"2024-11-28T16:43:29.089149Z","shell.execute_reply":"2024-11-28T16:43:29.097953Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualization\n\nEach tomographic sample comes in 3 resolutions:\n* High: 184 layers, image size (630, 630)\n* Medium: 92 layers, image size (315, 315)\n* Low: 46 layers, image size (158, 158)\n\nIn the visualization below, we show one layer at the resolution you've selected.\n\n**Note:** Lower resolutions display more annotated particles, as annotations from multiple layers are combined into a single layer.","metadata":{}},{"cell_type":"code","source":"experiment = 'TS_5_4'\nlayer = 80\nquality = 'high'\n\ndataset = open_experiment('TS_5_4', test=False)\nviz_annotation(dataset, layer, quality)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T16:43:29.684433Z","iopub.execute_input":"2024-11-28T16:43:29.684830Z","iopub.status.idle":"2024-11-28T16:43:30.858406Z","shell.execute_reply.started":"2024-11-28T16:43:29.684795Z","shell.execute_reply":"2024-11-28T16:43:30.857313Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"experiment = 'TS_5_4'\nlayer = 20\nquality = 'low'\n\ndataset = open_experiment('TS_5_4', test=False)\nviz_annotation(dataset, layer, quality)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T16:43:34.316844Z","iopub.execute_input":"2024-11-28T16:43:34.317224Z","iopub.status.idle":"2024-11-28T16:43:34.825726Z","shell.execute_reply.started":"2024-11-28T16:43:34.317192Z","shell.execute_reply":"2024-11-28T16:43:34.824421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}