Created
April 27, 2022 17:27
-
-
Save oeway/4a3ec3866eae199f982b8b81bf72a3a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"orig_nbformat": 4, | |
"kernelspec": { | |
"display_name": "Pyolite", | |
"language": "python", | |
"name": "python" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "python", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8" | |
} | |
}, | |
"nbformat_minor": 4, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": "# ImJoy JupyterLab Extension Demo\n\nThis notebook demonstrate how to use [itk-vtk-viewer](https://github.com/Kitware/itk-vtk-viewer/) with the ImJoy JupyterLab Extension, please make sure you have installed `imjoy-jupyterlab-extension`. If you use https://jupyter.imjoy.io it will be installed automatically. You can find an ImJoy icon in the notebook toolbar if the extension is correctly installed.", | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": "# Create an OME Zarr image", | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": "import zarr\nimport numpy as np\nimport os\nfrom skimage import data\nfrom skimage.transform import pyramid_gaussian\n\n# Modified from https://github.com/ome/ome-zarr-py/blob/master/tests/create_test_data.py\ndef create_ome_zarr(zarr_directory, dtype=\"f4\"):\n\n base = np.tile(data.astronaut(), (4, 4, 1))\n gaussian = list(pyramid_gaussian(base, downscale=2, max_layer=3, multichannel=True))\n\n pyramid = []\n # convert each level of pyramid into 5D image (t, c, z, y, x)\n for pixels in gaussian:\n red = pixels[:, :, 0]\n green = pixels[:, :, 1]\n blue = pixels[:, :, 2]\n # wrap to make 5D: (t, c, z, y, x)\n pixels = np.array([np.array([red]), np.array([green]), np.array([blue])])\n pixels = np.array([pixels]).astype(dtype)\n pyramid.append(pixels)\n\n store = zarr.DirectoryStore(zarr_directory)\n grp = zarr.group(store, overwrite=True)\n paths = []\n for path, dataset in enumerate(pyramid):\n grp.create_dataset(str(path), data=pyramid[path])\n paths.append({\"path\": str(path)})\n\n image_data = {\n \"id\": 1,\n \"channels\": [\n {\n \"color\": \"FF0000\",\n \"window\": {\"start\": 0, \"end\": 1},\n \"label\": \"Red\",\n \"active\": True,\n },\n {\n \"color\": \"00FF00\",\n \"window\": {\"start\": 0, \"end\": 1},\n \"label\": \"Green\",\n \"active\": True,\n },\n {\n \"color\": \"0000FF\",\n \"window\": {\"start\": 0, \"end\": 1},\n \"label\": \"Blue\",\n \"active\": True,\n },\n ],\n \"rdefs\": {\n \"model\": \"color\",\n },\n }\n\n multiscales = [\n {\n \"version\": \"0.1\",\n \"datasets\": paths,\n }\n ]\n grp.attrs[\"multiscales\"] = multiscales\n grp.attrs[\"omero\"] = image_data\n\n# Save it to /tmp/astronaut.zarr\ncreate_ome_zarr(\"/tmp/astronaut.zarr\")", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"text": "/lib/python3.9/site-packages/scipy/linalg/special_matrices.py:845: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n if kind is 'lower':\n/lib/python3.9/site-packages/scipy/linalg/special_matrices.py:847: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n elif kind is 'upper':\n/lib/python3.9/site-packages/scipy/stats/_binned_statistic.py:469: SyntaxWarning: \"is not\" with a literal. Did you mean \"!=\"?\n if(statistic is not 'count' and Vlen != Dlen):\n", | |
"output_type": "stream" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": "# Load the OME Zarr image", | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": "import zarr\n\nmultiscale_astronaut = zarr.open(\"/tmp/astronaut.zarr\", mode=\"r\") # open the zarr created above in jupyter kernel\nmultiscale_astronaut", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"execution_count": 3, | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "<zarr.hierarchy.Group '/' read-only>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": "# Display the zarr image with itk-vtk-viewer", | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": "from imjoy import api\nimport zarr\nfrom js import console\n\ndef encode_zarr_store(zobj):\n def getItem(key):\n return zobj.store[key]\n\n def setItem(key, value):\n zobj.store[key] = value\n\n def containsItem(key):\n if key in zobj.store:\n return True\n\n return {\n \"_rintf\": True,\n \"_rtype\": 'zarr-array' if isinstance(zobj, zarr.Array) else 'zarr-group',\n \"getItem\": getItem,\n \"setItem\": setItem,\n \"containsItem\": containsItem,\n }\n\napi.registerCodec({'name': 'zarr-array', 'type': zarr.Array, \"encoder\": encode_zarr_store})\napi.registerCodec({'name': 'zarr-group', 'type': zarr.Group, \"encoder\": encode_zarr_store})\n\nclass ImJoyPlugin:\n async def setup(self):\n viewer = await api.createWindow(\n type=\"itk-vtk-viewer\", src=\"https://kitware.github.io/itk-vtk-viewer/app\"\n )\n await viewer.setImage(multiscale_astronaut)\n\n\napi.export(ImJoyPlugin())", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Javascript object>", | |
"application/javascript": "window.connectPlugin && window.connectPlugin(\"f93b4f06-4b57-485b-be2e-cf5b55d91de2\")" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.HTML object>", | |
"text/html": "<div id=\"0d6af45b-d4ca-4dfb-a501-8d4084897e7b\"></div>" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"execution_count": 11, | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "<_GatheringFuture pending>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": "# Display image with vizarr", | |
"metadata": {} | |
}, | |
{ | |
"cell_type": "code", | |
"source": "import zarr\nfrom imjoy import api\nfrom imjoy_rpc import register_default_codecs\n\nregister_default_codecs()\n\nclass Plugin:\n async def setup(self):\n viewer = await api.createWindow(\n type=\"vizarr\", src=\"https://hms-dbmi.github.io/vizarr\"\n )\n # Create Zarr \n astronaut_img = { \"source\": multiscale_astronaut, \"name\": \"astronaut\" }\n await viewer.add_image(astronaut_img)\n\n\napi.export(Plugin())", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.Javascript object>", | |
"application/javascript": "window.connectPlugin && window.connectPlugin(\"f93b4f06-4b57-485b-be2e-cf5b55d91de2\")" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "<IPython.core.display.HTML object>", | |
"text/html": "<div id=\"8f765a79-47db-4b31-a419-03ddbedb71e9\"></div>" | |
}, | |
"metadata": {} | |
}, | |
{ | |
"execution_count": 13, | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "<_GatheringFuture pending>" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "", | |
"metadata": {}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment