{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:45:45.420085Z","iopub.execute_input":"2024-12-01T14:45:45.420518Z","iopub.status.idle":"2024-12-01T14:45:45.671405Z","shell.execute_reply.started":"2024-12-01T14:45:45.420484Z","shell.execute_reply":"2024-12-01T14:45:45.670006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install zarr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:45:45.673586Z","iopub.execute_input":"2024-12-01T14:45:45.674005Z","iopub.status.idle":"2024-12-01T14:45:55.552502Z","shell.execute_reply.started":"2024-12-01T14:45:45.673959Z","shell.execute_reply":"2024-12-01T14:45:55.551147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\nimport zarr\nimport json\n\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-12-01T14:45:55.554230Z","iopub.execute_input":"2024-12-01T14:45:55.554710Z","iopub.status.idle":"2024-12-01T14:45:55.562674Z","shell.execute_reply.started":"2024-12-01T14:45:55.554655Z","shell.execute_reply":"2024-12-01T14:45:55.561506Z"}},"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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:45:55.564821Z","iopub.execute_input":"2024-12-01T14:45:55.565240Z","iopub.status.idle":"2024-12-01T14:45:55.577154Z","shell.execute_reply.started":"2024-12-01T14:45:55.565208Z","shell.execute_reply":"2024-12-01T14:45:55.575987Z"}},"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()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:45:55.578493Z","iopub.execute_input":"2024-12-01T14:45:55.578808Z","iopub.status.idle":"2024-12-01T14:45:55.594386Z","shell.execute_reply.started":"2024-12-01T14:45:55.578776Z","shell.execute_reply":"2024-12-01T14:45:55.593148Z"}},"outputs":[],"execution_count":null},{"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-12-01T14:45:55.595903Z","iopub.execute_input":"2024-12-01T14:45:55.596805Z","iopub.status.idle":"2024-12-01T14:45:59.590382Z","shell.execute_reply.started":"2024-12-01T14:45:55.596765Z","shell.execute_reply":"2024-12-01T14:45:59.589254Z"}},"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-12-01T15:22:26.465264Z","iopub.execute_input":"2024-12-01T15:22:26.466042Z","iopub.status.idle":"2024-12-01T15:22:27.099726Z","shell.execute_reply.started":"2024-12-01T15:22:26.466005Z","shell.execute_reply":"2024-12-01T15:22:27.098732Z"}},"outputs":[],"execution_count":null}]}