Skip to content

Instantly share code, notes, and snippets.

@oeway
Last active August 17, 2020 08:02
Show Gist options
  • Save oeway/54c85eebb4f5c320a7aa0b2501e1b767 to your computer and use it in GitHub Desktop.
Save oeway/54c85eebb4f5c320a7aa0b2501e1b767 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Before start this notebook, you need to:\n",
"1. Install ImJoy Jupyter Extension by run `pip install imjoy-jupyter-extension`, restart Jupyter notebook, and make sure you see an ImJoy icon in the toolbar in opened notebooks.\n",
"1. Download the lastest version of [micro-manager 2.0](https://micro-manager.org/wiki/Micro-Manager_Nightly_Builds)\n",
"1. Install pycro-manager using `pip install pycromanager`\n",
"1. Run Micro-Manager, select tools-options, and check the box that says Run server on port 4827 (you only need to do this once)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting pycromanager\n",
" Downloading pycromanager-0.4.0-py3-none-any.whl (33 kB)\n",
"Requirement already satisfied, skipping upgrade: zmq in ./miniconda3/lib/python3.7/site-packages (from pycromanager) (0.0.0)\n",
"Requirement already satisfied, skipping upgrade: dask[array]>=2.4.0 in ./miniconda3/lib/python3.7/site-packages (from pycromanager) (2.10.1)\n",
"Requirement already satisfied, skipping upgrade: numpy in ./miniconda3/lib/python3.7/site-packages (from pycromanager) (1.19.1)\n",
"Requirement already satisfied, skipping upgrade: pyzmq in ./miniconda3/lib/python3.7/site-packages (from zmq->pycromanager) (18.0.2)\n",
"Requirement already satisfied, skipping upgrade: toolz>=0.7.3; extra == \"array\" in ./miniconda3/lib/python3.7/site-packages (from dask[array]>=2.4.0->pycromanager) (0.10.0)\n",
"Installing collected packages: pycromanager\n",
" Attempting uninstall: pycromanager\n",
" Found existing installation: pycromanager 0.2.5\n",
" Uninstalling pycromanager-0.2.5:\n",
" Successfully uninstalled pycromanager-0.2.5\n",
"Successfully installed pycromanager-0.4.0\n"
]
}
],
"source": [
"!pip install -U pycromanager"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.connectPlugin && window.connectPlugin()"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div id=\"42ccc43b-b43e-4b57-be9a-6b49ee1e0497\"></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"from imjoy_rpc import api\n",
"from pycromanager import Bridge\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" bridge = Bridge()\n",
" self._core = bridge.get_core()\n",
" self._core.set_exposure(float(100))\n",
" exposure = self._core.get_exposure()\n",
" api.showMessage('MMcore loaded, exposure: ' + str(exposure))\n",
" \n",
" def snapImage(self):\n",
" self._core.snap_image()\n",
" tagged_image = self._core.get_tagged_image()\n",
" image_array = np.reshape(tagged_image.pix, newshape=[tagged_image.tags['Height'], tagged_image.tags['Width']])\n",
" image_array = (image_array/image_array.max()*255).astype('uint8')\n",
" return image_array\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.showDialog(type=\"itk-vtk-viewer\",\n",
" src=\"https://oeway.github.io/itk-vtk-viewer/?imjoy=1\")\n",
" api.showMessage('Acquiring 10 images')\n",
" for i in range(10):\n",
" await viewer.imshow(self.snapImage())\n",
" api.showMessage('Done.')\n",
" \n",
"api.export(ImJoyPlugin(), {\"name\": \"demo\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"window.connectPlugin && window.connectPlugin()"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div id=\"b7fd70f9-fb0c-4dfb-8b38-55247dc6929e\"></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import asyncio\n",
"import numpy as np\n",
"\n",
"class ImJoyPlugin():\n",
" async def setup(self):\n",
" bridge = Bridge()\n",
" self._core = bridge.get_core()\n",
" self._core.set_exposure(float(100))\n",
" exposure = self._core.get_exposure()\n",
" api.showMessage('MMcore loaded, exposure: ' + str(exposure))\n",
" \n",
" def snapImage(self):\n",
" self._core.snap_image()\n",
" tagged_image = self._core.get_tagged_image()\n",
" image_array = np.reshape(tagged_image.pix, newshape=[tagged_image.tags['Height'], tagged_image.tags['Width']])\n",
" image_array = (image_array/image_array.max()*255).astype('uint8')\n",
" return image_array\n",
"\n",
" async def run(self, ctx):\n",
" viewer = await api.createWindow(src=\"https://kaibu.org/#/app\")\n",
"\n",
" image = np.random.randint(0, 255, [500, 500], dtype='uint8')\n",
"\n",
" # view image\n",
" await viewer.view_image(self.snapImage(), type=\"itk-vtk\", name=\"new image\")\n",
"\n",
" async def snap_image():\n",
" await viewer.view_image(self.snapImage(), type=\"itk-vtk\", name=\"new image\")\n",
"\n",
" await viewer.set_ui({\"title\": \"Microscope Control\",\n",
" \"elements\": [\n",
" {\"_rintf\": True,\n",
" \"type\": \"button\",\n",
" \"label\": \"Snap Image\",\n",
" \"callback\": snap_image\n",
" }\n",
" ]\n",
" })\n",
"\n",
"api.export(ImJoyPlugin(), {\"name\": \"demo\"})"
]
},
{
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
import asyncio
import numpy as np
class ImJoyPlugin():
async def setup(self):
bridge = Bridge()
self._core = bridge.get_core()
self._core.set_exposure(float(100))
exposure = self._core.get_exposure()
api.showMessage('MMcore loaded, exposure: ' + str(exposure))
def snapImage(self):
self._core.snap_image()
tagged_image = self._core.get_tagged_image()
image_array = np.reshape(tagged_image.pix, newshape=[tagged_image.tags['Height'], tagged_image.tags['Width']])
image_array = (image_array/image_array.max()*255).astype('uint8')
return image_array
async def run(self, ctx):
viewer = await api.createWindow(src="https://kaibu.org/#/app")
image = np.random.randint(0, 255, [500, 500], dtype='uint8')
# view image
await viewer.view_image(self.snapImage(), type="itk-vtk", name="new image")
async def snap_image():
await viewer.view_image(self.snapImage(), type="itk-vtk", name="new image")
await viewer.set_ui({"title": "Microscope Control",
"elements": [
{"_rintf": True,
"type": "button",
"label": "Snap Image",
"callback": snap_image
}
]
})
api.export(ImJoyPlugin(), {"name": "demo"})
import numpy as np
from imjoy_rpc import api
from pycromanager import Bridge
class ImJoyPlugin():
async def setup(self):
bridge = Bridge()
self._core = bridge.get_core()
exp = await api.prompt('Exposure time')
self._core.set_exposure(float(exp))
exposure = self._core.get_exposure()
api.showMessage('MMcore loaded, exposure: ' + str(exposure))
def snapImage(self):
self._core.snap_image()
tagged_image = self._core.get_tagged_image()
image_array = np.reshape(tagged_image.pix, newshape=[tagged_image.tags['Height'], tagged_image.tags['Width']])
image_array = (image_array/image_array.max()*255).astype('uint8')
return image_array
async def run(self, ctx):
viewer = await api.showDialog(type="itk-vtk-viewer",
src="https://oeway.github.io/itk-vtk-viewer/?imjoy=1")
api.showMessage('Acquiring 10 images')
for i in range(10):
await viewer.imshow(self.snapImage())
api.showMessage('Done.')
api.export(ImJoyPlugin(), {"name": "demo"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment