Skip to content

Instantly share code, notes, and snippets.

@oeway
Last active June 13, 2020 23:32
Show Gist options
  • Save oeway/ebedc17c9ab1f6aa5eee181679d85b5f to your computer and use it in GitHub Desktop.
Save oeway/ebedc17c9ab1f6aa5eee181679d85b5f to your computer and use it in GitHub Desktop.
numpy
pandas
scipy
matplotlib
ipywidgets>=7.0.0
imageio
scikit-image
imjoy>=0.10.0
tifffile
fsspec
imjoy-rpc
zarr
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Use vitessce-image-viewer in Jupyter Notebooks\n",
"(Revision: 0.2.2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"(Skip this step if you are running this notebook on Binder)\n",
"\n",
"Before start the demos here, you need to install the imjoy jupyter extension:\n",
"```\n",
"pip install -U imjoy-jupyter-extension\n",
"```\n",
"\n",
"Then **restart your jupyter notebook**, and open a new notebook.\n",
"\n",
"The following demo can only work if **see an ImJoy icon in the toolbar**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install tifffile\n",
"!pip install fsspec\n",
"!pip install -U imjoy-rpc\n",
"!conda install zarr -y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hello world for ImJoy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from imjoy import api\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" await api.alert(\"hello world\")\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run the vitessce-image-viewer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from imjoy import api\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(type=\"vitessce-image-viewer\", src=\"https://oeway.github.io/vitessce-image-viewer/\")\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## You can also run it as a dialog"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"api.showDialog(type=\"vitessce-image-viewer\", src=\"https://oeway.github.io/vitessce-image-viewer/\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download example data\n",
"(From Nicholas Sofroniew's Napari training course: https://github.com/sofroniewn/napari-training-course)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import urllib.request\n",
"urllib.request.urlretrieve(\n",
" 'https://github.com/sofroniewn/napari-training-course/raw/master/lessons/data/cell_membranes.tif', \n",
" 'cell_membranes.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import zarr\n",
"from tifffile import imread\n",
"image = imread('cell_membranes.tif')\n",
"image = (image*65536).astype('uint16')\n",
"z_array = zarr.array(image, chunks=(50, 50))\n",
"z_array.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Register a imjoy-rpc codec to encode zarr array/group\n",
"\n",
"This is to allow us sending Zarr array or group directly to the viewer in JS.\n",
"In the viewer, Zarr.js will use the encoded object as a custom store.\n",
"\n",
"This allows us send zarr array in a lazy way, the data can be requested on demand."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from imjoy import api\n",
"\n",
"def 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",
" return key in zobj.store\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",
"\n",
"api.registerCodec({'name': 'zarr-array', 'type': zarr.Array, \"encoder\": encode_zarr_store})\n",
"api.registerCodec({'name': 'zarr-group', 'type': zarr.hierarchy.Group, \"encoder\": encode_zarr_store})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare the object for the viewer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"staticInfo = {\n",
" # here we pass directly the zarr array, because we have registered a codec\n",
" \"url\": z_array,\n",
" \"isPublic\": False,\n",
" \"initialViewState\": {\n",
" \"zoom\": -1,\n",
" \"target\": [100, 100]\n",
" },\n",
" \"dimensions\": [\n",
" {\n",
" \"field\": 'mz',\n",
" \"type\": 'ordinal',\n",
" \"values\": [str(i) for i in range(60)]\n",
" },\n",
" { \"field\": 'y', \"type\": 'quantitative', \"values\": None },\n",
" { \"field\": 'x', \"type\": 'quantitative', \"values\": None }\n",
" ],\n",
" 'selections': [{'mz': 21}],\n",
" 'colormap': 'viridis',\n",
" \"description\": 'Membrane (zarr)'\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from imjoy import api\n",
"import asyncio\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(type=\"vitessce-image-viewer\", src=\"https://oeway.github.io/vitessce-image-viewer/\")\n",
" #await asyncio.sleep(10)\n",
" await viewer.loadSource('static', staticInfo)\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example Zarr dataset: Kidney IMS (zarr)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import zarr\n",
"from fsspec.implementations.http import HTTPFileSystem\n",
"fs = HTTPFileSystem()\n",
"http_map = fs.get_mapper('https://vitessce-data.storage.googleapis.com/0.0.25/master_release/spraggins/spraggins.ims.zarr')\n",
"z_array = zarr.open_array(http_map, mode='r')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"staticInfo = {\n",
" # here we pass directly the zarr array, because we have registered a codec\n",
" \"url\": z_array,\n",
" \"isPublic\": False,\n",
" \"initialViewState\": {\n",
" \"zoom\": -1,\n",
" \"target\": [1000, 500]\n",
" },\n",
" \"dimensions\": [\n",
" {\n",
" \"field\": 'mz',\n",
" \"type\": 'ordinal',\n",
" \"values\": [\n",
" '675.5366',\n",
" '703.5722',\n",
" '721.4766',\n",
" '725.5562',\n",
" '729.5892',\n",
" '731.606',\n",
" '734.5692',\n",
" '737.4524',\n",
" '739.4651',\n",
" '741.5302',\n",
" '745.4766',\n",
" '747.4938',\n",
" '749.5093',\n",
" '753.5892',\n",
" '756.5534',\n",
" '758.5706',\n",
" '772.5225',\n",
" '772.5506',\n",
" '776.5928',\n",
" '780.5528',\n",
" '782.5697',\n",
" '784.5841',\n",
" '786.6012',\n",
" '787.6707',\n",
" '790.5157',\n",
" '796.5259',\n",
" '798.54',\n",
" '804.5528',\n",
" '806.5683',\n",
" '808.5838',\n",
" '809.6518',\n",
" '810.6',\n",
" '811.6699',\n",
" '813.6847',\n",
" '815.699',\n",
" '820.5262',\n",
" '822.5394',\n",
" '824.5559',\n",
" '825.6241',\n",
" '828.5495',\n",
" '830.5666',\n",
" '832.5816',\n",
" '833.649',\n",
" '835.6666',\n",
" '837.6798',\n",
" '848.5577',\n",
" '851.6374'\n",
" ]\n",
" },\n",
" { \"field\": 'y', \"type\": 'quantitative', \"values\": None },\n",
" { \"field\": 'x', \"type\": 'quantitative', \"values\": None }\n",
" ],\n",
" 'selections': [{ \"mz\": '703.5722' }, { \"mz\": '721.4766' }],\n",
" \"description\": 'Kidney IMS (zarr)'\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from imjoy import api\n",
"import asyncio\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(type=\"vitessce-image-viewer\", src=\"https://oeway.github.io/vitessce-image-viewer/\")\n",
" #await asyncio.sleep(10)\n",
" await viewer.loadSource('static', staticInfo)\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example Zarr dataset: Kidney mxIF (zarr)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import zarr\n",
"from fsspec.implementations.http import HTTPFileSystem\n",
"fs = HTTPFileSystem()\n",
"http_map = fs.get_mapper('https://vitessce-data.storage.googleapis.com/0.0.25/master_release/spraggins/spraggins.mxif.zarr')\n",
"\n",
"z_group = zarr.open_group(http_map, mode=\"r\")\n",
"# z = zarr.open_array(http_map, path=\"0/\", mode='r')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"channelNames = [\n",
" 'DAPI - Hoechst (nuclei)',\n",
" 'FITC - Laminin (basement membrane)',\n",
" 'Cy3 - Synaptopodin (glomerular)',\n",
" 'Cy5 - THP (thick limb)'\n",
"]\n",
"\n",
"\n",
"channelNames = [\n",
" 'DAPI - Hoechst (nuclei)',\n",
" 'FITC - Laminin (basement membrane)',\n",
" 'Cy3 - Synaptopodin (glomerular)',\n",
" 'Cy5 - THP (thick limb)'\n",
"]\n",
"\n",
"\n",
"zarrInfo = {\n",
" # here we pass directly the zarr group, because we have registered a codec\n",
" \"url\": z_group,\n",
" \"description\": 'Kidney mxIF (zarr)',\n",
" \"dimensions\": [\n",
" { \"field\": 'channel', \"type\": 'nominal', \"values\": channelNames },\n",
" { \"field\": 'y', \"type\": 'quantitative', \"values\": None },\n",
" { \"field\": 'x', \"type\": 'quantitative', \"values\": None }\n",
" ],\n",
" \"isPublic\": True,\n",
" \"isPyramid\": True,\n",
" \"selections\": [{ \"channel\": name } for name in channelNames]\n",
"}\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from imjoy import api\n",
"import asyncio\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(type=\"vitessce-image-viewer\", src=\"https://oeway.github.io/vitessce-image-viewer/\")\n",
" #await asyncio.sleep(10)\n",
" await viewer.loadSource('static', zarrInfo)\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example Zarr dataset: IDR 9822151.zarr - SARS-CoV-2 in human instestinal cells (OME-ZARR)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import zarr\n",
"from fsspec.implementations.http import HTTPFileSystem\n",
"fs = HTTPFileSystem()\n",
"http_map = fs.get_mapper('https://vitessce-demo-data.storage.googleapis.com/test-data/9822151.zarr')\n",
"\n",
"z_group = zarr.open_group(http_map, mode=\"r\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"omeZarr = {\n",
" # here we pass directly the zarr group, because we have registered a codec\n",
" \"url\": z_group,\n",
" \"dimensions\": [\n",
" # TODO: Having the actual dimensions breaks the UI components currently\n",
" # ome_zarr images are all (t, c, z, y, x)\n",
" { \"field\": 'c', \"type\": 'nominal', \"values\": [0] },\n",
" { \"field\": 'y', \"type\": 'quantitative', \"values\": None },\n",
" { \"field\": 'x', \"type\": 'quantitative', \"values\": None }\n",
" ],\n",
" \"selections\": [{ \"c\": 0 }],\n",
" \"isPublic\": True,\n",
" \"isPyramid\": True,\n",
" \"description\":\n",
" 'IDR 9822151.zarr - SARS-CoV-2 in human instestinal cells (OME-ZARR)',\n",
" \"initialViewState\": {\n",
" \"zoom\": -5,\n",
" \"target\": [30000, 20000]\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from imjoy import api\n",
"import asyncio\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" pass\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(type=\"vitessce-image-viewer\", src=\"https://oeway.github.io/vitessce-image-viewer/\")\n",
" #await asyncio.sleep(10)\n",
" await viewer.loadSource('ome-zarr', omeZarr)\n",
"\n",
"api.export(ImJoyPlugin())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment