{"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":"gpu","dataSources":[{"sourceId":84969,"databundleVersionId":10033515,"sourceType":"competition"},{"sourceId":9855563,"sourceType":"datasetVersion","datasetId":6048040},{"sourceId":10055923,"sourceType":"datasetVersion","datasetId":6196366},{"sourceId":11384,"sourceType":"modelInstanceVersion","modelInstanceId":6216,"modelId":3301}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Install necessary libraries\n!pip install zarr plotly","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:03.413511Z","iopub.execute_input":"2025-01-12T11:25:03.413859Z","iopub.status.idle":"2025-01-12T11:25:14.718813Z","shell.execute_reply.started":"2025-01-12T11:25:03.413826Z","shell.execute_reply":"2025-01-12T11:25:14.717948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import necessary libraries\nimport numpy as np\nimport pandas as pd\nimport zarr\nimport json\nimport os\nimport random\nimport matplotlib.pyplot as plt\nimport cupy as cp\nfrom matplotlib.animation import FuncAnimation\nfrom IPython.display import Image, display\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom sklearn.model_selection import train_test_split\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:14.720604Z","iopub.execute_input":"2025-01-12T11:25:14.720880Z","iopub.status.idle":"2025-01-12T11:25:27.172967Z","shell.execute_reply.started":"2025-01-12T11:25:14.720852Z","shell.execute_reply":"2025-01-12T11:25:27.172008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the data\ndef load_tomogram(experiment_path):\n    tomogram = zarr.open(experiment_path, mode='r')\n    return tomogram[0]  # Assuming 0 is the highest resolution\n\ndef load_particle_locations(json_path):\n    with open(json_path, 'r') as f:\n        data = json.load(f)\n    if 'points' in data:\n        return np.array(data['points'])\n    else:\n        print(f\"Key 'points' not found in {json_path}. Available keys: {data.keys()}\")\n        return np.array([])\n\n# Example paths (you need to adjust these based on your actual directory structure)\ntrain_path = '/kaggle/input/czii-cryo-et-object-identification/train/'\ntest_path = '/kaggle/input/czii-cryo-et-object-identification/test/'\nsample_submission_path = '/kaggle/input/resnet34d-scanner-tta/submission.csv'\n\n# gemma_2b_model_path = '/kaggle/input/gemma/transformers/2b/2'\n\n# Load training data manually\ntrain_experiments = ['TS_5_4', 'TS_69_2', 'TS_6_4', 'TS_6_6', 'TS_73_6', 'TS_86_3', 'TS_99_9']\nparticle_types = ['apo-ferritin', 'beta-amylase', 'beta-galactosidase', 'ribosome', 'thyroglobulin', 'virus-like-particle']\n\nX_train = []\ny_train_x = []\ny_train_y = []\ny_train_z = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:27.174184Z","iopub.execute_input":"2025-01-12T11:25:27.174918Z","iopub.status.idle":"2025-01-12T11:25:27.182276Z","shell.execute_reply.started":"2025-01-12T11:25:27.174886Z","shell.execute_reply":"2025-01-12T11:25:27.181260Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Here is Our Work","metadata":{}},{"cell_type":"markdown","source":"## Part 1 loading tomograms","metadata":{}},{"cell_type":"code","source":"# # Manually load each experiment and particle type\n# for exp in train_experiments:\n#     tomogram_path = os.path.join(train_path, 'static/ExperimentRuns', exp, 'VoxelSpacing10.000', 'denoised.zarr')\n#     print(f\"Loading tomogram from {tomogram_path}\")\n#     tomogram = load_tomogram(tomogram_path)\n#     for ptype in particle_types:\n#         json_path = os.path.join(train_path, 'overlay/ExperimentRuns', exp, 'Picks', f'{ptype}.json')\n        \n#         print(f\"Loading particle locations from {json_path}\")\n#         locations = load_particle_locations(json_path)\n        \n#         tomogram_shape = tomogram.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:27.183599Z","iopub.execute_input":"2025-01-12T11:25:27.183948Z","iopub.status.idle":"2025-01-12T11:25:27.206948Z","shell.execute_reply.started":"2025-01-12T11:25:27.183907Z","shell.execute_reply":"2025-01-12T11:25:27.206080Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Visulizing one tomogram as a gif","metadata":{}},{"cell_type":"code","source":"tomogram_path = os.path.join(train_path, 'static/ExperimentRuns', 'TS_5_4', 'VoxelSpacing10.000', 'denoised.zarr')\njson_path = os.path.join(train_path, 'overlay/ExperimentRuns', 'TS_5_4', 'Picks', 'virus-like-particle.json')\nvoxel_spacing = 10 #* this means one voxel = 10 units\n# print(f\"Loading tomogram from {tomogram_path}\")\ntomogram = load_tomogram(tomogram_path)\n\n# print(f\"Loading particle locations from {json_path}\")\n#locations = load_particle_locations(json_path)\nlocations = {}\nfor ptype in particle_types:\n        json_path = os.path.join(train_path, 'overlay/ExperimentRuns', 'TS_5_4', 'Picks', f'{ptype}.json')\n        # print(ptype)\n        # print(f\"Loading particle locations from {json_path}\")\n        locations[ptype] = load_particle_locations(json_path)\n# print(locations)\n# Convert the tomogram data to a CuPy array to leverage GPU\ntomogram_gpu = cp.asarray(tomogram[:])  # This will copy the entire dataset to the GPU","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:27.208763Z","iopub.execute_input":"2025-01-12T11:25:27.209095Z","iopub.status.idle":"2025-01-12T11:25:30.997602Z","shell.execute_reply.started":"2025-01-12T11:25:27.209067Z","shell.execute_reply":"2025-01-12T11:25:30.996638Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## axis \nz axis is layers while x should be forward(in the screen) and y should be towo","metadata":{}},{"cell_type":"code","source":"# Check the shape of the tomogram\nprint(\"Tomogram Shape:\", tomogram_gpu.shape)\n\n# Define the axis to visualize (z-axis slices in this example)\nnum_slices = tomogram_gpu.shape[0]\n\n# Create the figure for animation\nfig, ax = plt.subplots()\nax.set_title(\"Tomogram Animation\")\nax.axis(\"off\")\n\n# Display the first slice as a starting point\nslice_img = ax.imshow(cp.asnumpy(tomogram_gpu[0, :, :]), cmap=\"gray\")\n\n# Update function for the animation\ndef update(frame):\n    slice_data = cp.asnumpy(tomogram_gpu[frame, :, :])  # Transfer the GPU array to CPU for plotting\n    slice_img.set_data(slice_data)\n    ax.set_title(f\"Slice {frame + 1}/{num_slices}\")\n    return slice_img,\n\n# Create the animation\nanim = FuncAnimation(fig, update, frames=num_slices, interval=100, blit=True)\n\n# Save the animation as a GIF (alternative: use 'mp4' format)\nanimation_path = \"/kaggle/working/tomogram_animation_gpu.gif\"\nanim.save(animation_path, writer=\"imagemagick\", fps=10)\n\n# Display the saved GIF in the notebook\ndisplay(Image(filename=animation_path))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:44.042397Z","iopub.execute_input":"2025-01-12T11:25:44.042773Z","iopub.status.idle":"2025-01-12T11:25:56.668377Z","shell.execute_reply.started":"2025-01-12T11:25:44.042738Z","shell.execute_reply":"2025-01-12T11:25:56.667475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-12T11:25:35.357277Z","iopub.status.idle":"2025-01-12T11:25:35.357614Z","shell.execute_reply.started":"2025-01-12T11:25:35.357436Z","shell.execute_reply":"2025-01-12T11:25:35.357452Z"}},"outputs":[],"execution_count":null}]}