{"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":"# CZII Static image and Overlay image","metadata":{}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/stpeteishii/czii-zarr-image-view\n\nhttps://www.kaggle.com/code/stpeteishii/czii-data-3d-view","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:31.431273Z","iopub.execute_input":"2024-11-09T08:24:31.431690Z","iopub.status.idle":"2024-11-09T08:24:31.436272Z","shell.execute_reply.started":"2024-11-09T08:24:31.431650Z","shell.execute_reply":"2024-11-09T08:24:31.435400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir image\n!mkdir mask","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:31.438269Z","iopub.execute_input":"2024-11-09T08:24:31.438990Z","iopub.status.idle":"2024-11-09T08:24:33.429466Z","shell.execute_reply.started":"2024-11-09T08:24:31.438945Z","shell.execute_reply":"2024-11-09T08:24:33.428253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def display_tree(root_dir, indent=\"\"):\n    print(indent + os.path.basename(root_dir) + \"/\")\n    indent += \"    \"\n    \n    try:\n        for item in os.listdir(root_dir):\n            item_path = os.path.join(root_dir, item)\n            if os.path.isdir(item_path):\n                display_tree(item_path, indent)\n            else:\n                print(indent + item)\n                \n    except PermissionError:\n        print(indent + \"[Access Denied]\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.431409Z","iopub.execute_input":"2024-11-09T08:24:33.432053Z","iopub.status.idle":"2024-11-09T08:24:33.439372Z","shell.execute_reply.started":"2024-11-09T08:24:33.432013Z","shell.execute_reply":"2024-11-09T08:24:33.438341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dir0=\"/kaggle/input/czii-cryo-et-object-identification/train/overlay/ExperimentRuns/TS_5_4\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.440678Z","iopub.execute_input":"2024-11-09T08:24:33.441620Z","iopub.status.idle":"2024-11-09T08:24:33.459923Z","shell.execute_reply.started":"2024-11-09T08:24:33.441577Z","shell.execute_reply":"2024-11-09T08:24:33.458743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"display_tree(dir0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.465525Z","iopub.execute_input":"2024-11-09T08:24:33.465895Z","iopub.status.idle":"2024-11-09T08:24:33.475811Z","shell.execute_reply.started":"2024-11-09T08:24:33.465854Z","shell.execute_reply":"2024-11-09T08:24:33.474740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"paths=[]\nfor dirname, _, filenames in os.walk(dir0):\n    for filename in filenames:\n        paths+=[(os.path.join(dirname, filename))]\nprint(paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.477098Z","iopub.execute_input":"2024-11-09T08:24:33.477475Z","iopub.status.idle":"2024-11-09T08:24:33.485657Z","shell.execute_reply.started":"2024-11-09T08:24:33.477433Z","shell.execute_reply":"2024-11-09T08:24:33.484643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\n\ndef load_from_json(file_path):\n    with open(file_path, 'r', encoding='utf-8') as f:\n        data = json.load(f)\n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.486982Z","iopub.execute_input":"2024-11-09T08:24:33.487361Z","iopub.status.idle":"2024-11-09T08:24:33.492897Z","shell.execute_reply.started":"2024-11-09T08:24:33.487321Z","shell.execute_reply":"2024-11-09T08:24:33.491880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import defaultdict\ncnt = defaultdict(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.494497Z","iopub.execute_input":"2024-11-09T08:24:33.494892Z","iopub.status.idle":"2024-11-09T08:24:33.501825Z","shell.execute_reply.started":"2024-11-09T08:24:33.494849Z","shell.execute_reply":"2024-11-09T08:24:33.500787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"L=[]\nfor i,path in enumerate(paths):\n    data=load_from_json(path)\n    cnt[i]=data['pickable_object_name']\n    X=[]\n    Y=[]\n    Z=[]\n    Z2=[]\n    for datai in data['points']:\n        xyz=datai['location']\n        X+=[xyz['x']]\n        Y+=[xyz['y']]\n        Z+=[xyz['z']]\n        Z2+=[xyz['z']//50]\n    L+=[(i,X,Y,Z,Z2)]\n\nprint(cnt)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.503340Z","iopub.execute_input":"2024-11-09T08:24:33.503682Z","iopub.status.idle":"2024-11-09T08:24:33.516234Z","shell.execute_reply.started":"2024-11-09T08:24:33.503641Z","shell.execute_reply":"2024-11-09T08:24:33.515254Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"# Overlay Image","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 1: 3D basic","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(13,13))\nax = fig.add_subplot(111, projection='3d')\n\nfor data in L:\n    i, X, Y, Z, Z2 = data  \n    scatter = ax.scatter(X, Y, Z, c=[i]*len(X), vmin=0, vmax=5, cmap='viridis', label=cnt[i]) \n\nax.set_xlabel('X Label')\nax.set_ylabel('Y Label')\nax.set_zlabel('Z Label')\ncbar = fig.colorbar(scatter, ax=ax)\ncbar.set_label('Label')\nax.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:33.528559Z","iopub.execute_input":"2024-11-09T08:24:33.528958Z","iopub.status.idle":"2024-11-09T08:24:34.117415Z","shell.execute_reply.started":"2024-11-09T08:24:33.528914Z","shell.execute_reply":"2024-11-09T08:24:34.116411Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 2: 3D with z_classes","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,10))\nax = fig.add_subplot(111, projection='3d')\n\nfor data in L:\n    i, X, Y, Z, Z2 = data  \n    scatter = ax.scatter(X, Y, Z2, c=[i]*len(X), vmin=0, vmax=5, cmap='viridis', label=cnt[i]) \n\nax.set_xlabel('X Label')\nax.set_ylabel('Y Label')\nax.set_zlabel('Z Label')\ncbar = fig.colorbar(scatter, ax=ax)\ncbar.set_label('Label')\nax.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:34.119199Z","iopub.execute_input":"2024-11-09T08:24:34.119925Z","iopub.status.idle":"2024-11-09T08:24:34.618472Z","shell.execute_reply.started":"2024-11-09T08:24:34.119876Z","shell.execute_reply":"2024-11-09T08:24:34.615302Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 3: 3D with z_filtered","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,10))\nax = fig.add_subplot(111, projection='3d')\n\nfor data in L:\n    i, X, Y, Z, Z2 = data \n\n    filtered_indices = [idx for idx, z in enumerate(Z2) if z==16]\n    print(filtered_indices)\n    \n    # Apply the filter to X, Y, and Z2\n    filtered_X = [X[idx] for idx in filtered_indices]\n    filtered_Y = [Y[idx] for idx in filtered_indices]\n    filtered_Z2 = [Z2[idx] for idx in filtered_indices]\n    \n    # Plot the filtered data\n    scatter = ax.scatter(filtered_X, filtered_Y, filtered_Z2, c=[i] * len(filtered_X),\n                         vmin=0, vmax=5, cmap='viridis', label=cnt[i])\n\nax.set_xlabel('X Label')\nax.set_ylabel('Y Label')\nax.set_zlabel('Z Label')\ncbar = fig.colorbar(scatter, ax=ax)\ncbar.set_label('Label')\nax.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:34.619953Z","iopub.execute_input":"2024-11-09T08:24:34.620644Z","iopub.status.idle":"2024-11-09T08:24:35.116091Z","shell.execute_reply.started":"2024-11-09T08:24:34.620596Z","shell.execute_reply":"2024-11-09T08:24:35.115208Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## 4: 2D with z_filtered","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,10))\nax = fig.add_subplot(111)\n\nfor data in L:\n    i, X, Y, Z, Z2 = data \n    filtered_indices = [idx for idx, z in enumerate(Z2) if z==16]\n    print(filtered_indices)\n    \n    # Apply the filter to X, Y, and Z2\n    filtered_X = [X[idx] for idx in filtered_indices]\n    filtered_Y = [Y[idx] for idx in filtered_indices]\n    filtered_Z2 = [Z2[idx] for idx in filtered_indices]\n    \n    # Plot the filtered data\n    scatter = ax.scatter(filtered_X, filtered_Y, c=[i] * len(filtered_X),\n                         vmin=0, vmax=5, cmap='viridis', label=cnt[i])\n\nax.set_xlabel('X Label')\nax.set_ylabel('Y Label')\ncbar = fig.colorbar(scatter, ax=ax)\ncbar.set_label('Label')\nax.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:35.121194Z","iopub.execute_input":"2024-11-09T08:24:35.121611Z","iopub.status.idle":"2024-11-09T08:24:35.535937Z","shell.execute_reply.started":"2024-11-09T08:24:35.121564Z","shell.execute_reply":"2024-11-09T08:24:35.534977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def z_filtered(j):\n    \n    fig = plt.figure(figsize=(8,8))\n    ax = fig.add_subplot(111)\n    \n    for data in L:\n        i, X, Y, Z, Z2 = data \n        filtered_indices = [idx for idx, z in enumerate(Z2) if z==j]\n        \n        # Apply the filter to X, Y, and Z2\n        filtered_X = [X[idx] for idx in filtered_indices]\n        filtered_Y = [Y[idx] for idx in filtered_indices]\n        filtered_Z2 = [Z2[idx] for idx in filtered_indices]\n        \n        # Plot the filtered data\n        scatter = ax.scatter(filtered_X, filtered_Y, c=[i] * len(filtered_X),\n                             vmin=0, vmax=5, cmap='viridis')\n    \n    ax.set_xlabel('X Label')\n    ax.set_ylabel('Y Label')\n    #cbar = fig.colorbar(scatter, ax=ax)\n    #cbar.set_label('Label')\n    plt.axis('off')\n    plt.savefig(f'mask/{j:02}.png', bbox_inches='tight', pad_inches=0)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:35.537037Z","iopub.execute_input":"2024-11-09T08:24:35.537322Z","iopub.status.idle":"2024-11-09T08:24:35.545380Z","shell.execute_reply.started":"2024-11-09T08:24:35.537290Z","shell.execute_reply":"2024-11-09T08:24:35.544360Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(30):\n    print(i)\n    z_filtered(i)\n    print()\n    print('-----------------------------'*2)\n    print()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:35.546483Z","iopub.execute_input":"2024-11-09T08:24:35.546885Z","iopub.status.idle":"2024-11-09T08:24:35.694870Z","shell.execute_reply.started":"2024-11-09T08:24:35.546823Z","shell.execute_reply":"2024-11-09T08:24:35.693917Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"# zarr images","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"!pip install zarr","metadata":{"trusted":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:36.470693Z","iopub.execute_input":"2024-11-09T08:24:36.471095Z","iopub.status.idle":"2024-11-09T08:24:47.733908Z","shell.execute_reply.started":"2024-11-09T08:24:36.471050Z","shell.execute_reply":"2024-11-09T08:24:47.732908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport zarr\nfrom PIL import Image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:47.735659Z","iopub.execute_input":"2024-11-09T08:24:47.736292Z","iopub.status.idle":"2024-11-09T08:24:47.741560Z","shell.execute_reply.started":"2024-11-09T08:24:47.736239Z","shell.execute_reply":"2024-11-09T08:24:47.740643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dir0='/kaggle/input/czii-cryo-et-object-identification/train/static/ExperimentRuns/TS_5_4/VoxelSpacing10.000'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:47.742633Z","iopub.execute_input":"2024-11-09T08:24:47.742941Z","iopub.status.idle":"2024-11-09T08:24:47.751790Z","shell.execute_reply.started":"2024-11-09T08:24:47.742907Z","shell.execute_reply":"2024-11-09T08:24:47.750952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files=os.listdir(dir0)\nZARR=[]\nfor file in files:\n    ZARR+=[os.path.join(dir0,file)]\nprint(ZARR)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:47.753035Z","iopub.execute_input":"2024-11-09T08:24:47.753617Z","iopub.status.idle":"2024-11-09T08:24:47.765816Z","shell.execute_reply.started":"2024-11-09T08:24:47.753583Z","shell.execute_reply":"2024-11-09T08:24:47.764980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"framesA=[]\nfor j,diri in enumerate(ZARR[0:1]):\n    print(ZARR[j].split('/')[-1]) \n    data = zarr.open(diri, mode='r') \n    fig = plt.figure(figsize=(12,20))\n\n    for i in range(35):\n        ax = plt.subplot(7, 5, i + 1)\n        plt.axis('off')\n        image=data[2][i]\n        min_val, max_val = image.min(), image.max()\n        simage = ((image - min_val) / (max_val - min_val) * 255).astype('uint8')\n        framesA+=[simage]\n        plt.imshow(simage)\n        cv2.imwrite(f'image/{i:02}.png', simage)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T08:24:47.766901Z","iopub.execute_input":"2024-11-09T08:24:47.767178Z","iopub.status.idle":"2024-11-09T08:24:50.349364Z","shell.execute_reply.started":"2024-11-09T08:24:47.767148Z","shell.execute_reply":"2024-11-09T08:24:50.348210Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# gif animation","metadata":{}},{"cell_type":"code","source":"# static image animation\nfrom PIL import Image\n\nframes = [Image.fromarray(frame) for frame in framesA]\nprint(frames[0].size)\n\nframes[0].save(\n    \"animation.gif\",\n    save_all=True,\n    append_images=frames[1:],  \n    duration=200,              \n    loop=0             \n)\n\noutput_path=\"animation.gif\"\nfrom IPython.display import Image\nImage(open(output_path, 'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# overlay image animation\nfrom PIL import Image\nimport glob\n\nfolder_path = \"./mask\" \noutput_size = frames[0].size\nimage_files = sorted(glob.glob(os.path.join(folder_path, \"*.png\")))\nframesV = [Image.open(image).resize(output_size, Image.LANCZOS) for image in image_files]\nprint(len(image_files))\n\nframesV[0].save(\n    \"animationV.gif\",\n    save_all=True,\n    append_images=framesV[1:],  \n    duration=300,              \n    loop=0                     \n)\n\noutput_path2=\"animationV.gif\"\nfrom IPython.display import Image\nImage(open(output_path2, 'rb').read())","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}