{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":84969,"databundleVersionId":10033515,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport zarr\nimport json\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T15:58:23.707437Z","iopub.execute_input":"2024-11-09T15:58:23.708285Z","iopub.status.idle":"2024-11-09T15:58:23.712665Z","shell.execute_reply.started":"2024-11-09T15:58:23.708239Z","shell.execute_reply":"2024-11-09T15:58:23.711643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def visualize_tomogram_slices(tomogram, particles, slice_indices=None):\n    \"\"\"\n    Visualize three orthogonal slices of the tomogram with particle positions\n    \n    Parameters:\n    tomogram: zarr array\n    particles: numpy array of shape (n, 3) containing x,y,z coordinates\n    slice_indices: tuple of (x,y,z) indices for slices to show, defaults to middle\n    \"\"\"\n    if slice_indices is None:\n        # Default to middle slices\n        slice_indices = (tomogram.shape[0]//2, \n                        tomogram.shape[1]//2, \n                        tomogram.shape[2]//2)\n    \n    fig, axes = plt.subplots(1, 3, figsize=(15, 5))\n    \n    # XY plane\n    axes[0].imshow(tomogram[slice_indices[0], :, :], cmap='gray')\n    mask_z = np.abs(particles[:, 2] - slice_indices[0]) < 5\n    axes[0].scatter(particles[mask_z, 1], particles[mask_z, 0], \n                   c='red', s=20, alpha=0.6)\n    axes[0].set_title('XY Plane')\n    \n    # XZ plane\n    axes[1].imshow(tomogram[:, slice_indices[1], :], cmap='gray')\n    mask_y = np.abs(particles[:, 1] - slice_indices[1]) < 5\n    axes[1].scatter(particles[mask_y, 2], particles[mask_y, 0], \n                   c='red', s=20, alpha=0.6)\n    axes[1].set_title('XZ Plane')\n    \n    # YZ plane\n    axes[2].imshow(tomogram[:, :, slice_indices[2]], cmap='gray')\n    mask_x = np.abs(particles[:, 0] - slice_indices[2]) < 5\n    axes[2].scatter(particles[mask_x, 2], particles[mask_x, 1], \n                   c='red', s=20, alpha=0.6)\n    axes[2].set_title('YZ Plane')\n    \n    plt.tight_layout()\n    return fig","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T15:58:27.984629Z","iopub.execute_input":"2024-11-09T15:58:27.985512Z","iopub.status.idle":"2024-11-09T15:58:27.997001Z","shell.execute_reply.started":"2024-11-09T15:58:27.985462Z","shell.execute_reply":"2024-11-09T15:58:27.996086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_3d_particles(particles):\n    \"\"\"\n    Create a 3D scatter plot of particle positions\n    \n    Parameters:\n    particles: numpy array of shape (n, 3) containing x,y,z coordinates\n    \"\"\"\n    fig = plt.figure(figsize=(10, 10))\n    ax = fig.add_subplot(111, projection='3d')\n    \n    ax.scatter(particles[:, 0], \n              particles[:, 1], \n              particles[:, 2], \n              c='red', s=20, alpha=0.6)\n    \n    ax.set_xlabel('X')\n    ax.set_ylabel('Y')\n    ax.set_zlabel('Z')\n    ax.set_title('3D Particle Positions')\n    \n    return fig","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T15:58:35.968225Z","iopub.execute_input":"2024-11-09T15:58:35.968898Z","iopub.status.idle":"2024-11-09T15:58:35.97521Z","shell.execute_reply.started":"2024-11-09T15:58:35.968856Z","shell.execute_reply":"2024-11-09T15:58:35.974218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load and visualize the data\nexperiment_path = '/kaggle/input/czii-cryo-et-object-identification/train/static/ExperimentRuns/TS_5_4/VoxelSpacing10.000/denoised.zarr'\nparticle_path = '/kaggle/input/czii-cryo-et-object-identification/train/overlay/ExperimentRuns/TS_5_4/Picks/apo-ferritin.json'\n\n# Load zarr data\nzarr_array = zarr.open(experiment_path, mode='r')\ntomogram = zarr_array[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T15:59:02.157337Z","iopub.execute_input":"2024-11-09T15:59:02.158106Z","iopub.status.idle":"2024-11-09T15:59:02.227926Z","shell.execute_reply.started":"2024-11-09T15:59:02.158061Z","shell.execute_reply":"2024-11-09T15:59:02.227064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load particle data\nwith open(particle_path, 'r') as f:\n    particles_data = json.load(f)\n    \nparticles = np.array([[point['location']['x'], \n                      point['location']['y'], \n                      point['location']['z']] for point in particles_data['points']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T15:59:09.798604Z","iopub.execute_input":"2024-11-09T15:59:09.799318Z","iopub.status.idle":"2024-11-09T15:59:09.80956Z","shell.execute_reply.started":"2024-11-09T15:59:09.799273Z","shell.execute_reply":"2024-11-09T15:59:09.808435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create visualizations\nvisualize_tomogram_slices(tomogram, particles)\nplot_3d_particles(particles)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T15:59:14.649481Z","iopub.execute_input":"2024-11-09T15:59:14.650343Z","iopub.status.idle":"2024-11-09T15:59:18.317939Z","shell.execute_reply.started":"2024-11-09T15:59:14.650286Z","shell.execute_reply":"2024-11-09T15:59:18.316879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}