Skip to content

Commit

Permalink
Merge pull request #93 from bcalford/main
Browse files Browse the repository at this point in the history
Live ipywidget functionality
  • Loading branch information
bcalford authored Nov 5, 2024
2 parents 2130716 + 04f6d98 commit 7beeadc
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ __pycache__
src/ipyniivue/static

launch.json

# OS Specific
.DS_Store
2 changes: 1 addition & 1 deletion examples/additive_voxels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
7 changes: 7 additions & 0 deletions examples/basic_multiplanar.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
"nv.is_colorbar = True\n",
"nv.view_mode_hot_key = \"KeyN\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
82 changes: 6 additions & 76 deletions examples/mesh_layers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,10 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "6606a337-3243-4041-a7b7-ec64136f245c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2024-06-26 10:16:33-- https://github.com/niivue/niivue/raw/main/demos/images/BrainMesh_ICBM152.lh.mz3\n",
"Resolving github.com (github.com)... 140.82.112.4\n",
"Connecting to github.com (github.com)|140.82.112.4|:443... connected.\n",
"HTTP request sent, awaiting response... 302 Found\n",
"Location: https://raw.githubusercontent.com/niivue/niivue/main/demos/images/BrainMesh_ICBM152.lh.mz3 [following]\n",
"--2024-06-26 10:16:33-- https://raw.githubusercontent.com/niivue/niivue/main/demos/images/BrainMesh_ICBM152.lh.mz3\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 779119 (761K) [application/octet-stream]\n",
"Saving to: ‘../images/BrainMesh_ICBM152.lh.mz3’\n",
"\n",
"BrainMesh_ICBM152.l 100%[===================>] 760.86K --.-KB/s in 0.1s \n",
"\n",
"2024-06-26 10:16:33 (7.67 MB/s) - ‘../images/BrainMesh_ICBM152.lh.mz3’ saved [779119/779119]\n",
"\n",
"--2024-06-26 10:16:33-- https://github.com/niivue/niivue/raw/main/demos/images/CIT168.mz3\n",
"Resolving github.com (github.com)... 140.82.112.4\n",
"Connecting to github.com (github.com)|140.82.112.4|:443... connected.\n",
"HTTP request sent, awaiting response... 302 Found\n",
"Location: https://raw.githubusercontent.com/niivue/niivue/main/demos/images/CIT168.mz3 [following]\n",
"--2024-06-26 10:16:34-- https://raw.githubusercontent.com/niivue/niivue/main/demos/images/CIT168.mz3\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8001::154, 2606:50c0:8002::154, 2606:50c0:8003::154, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8001::154|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 406916 (397K) [application/octet-stream]\n",
"Saving to: ‘../images/CIT168.mz3’\n",
"\n",
"CIT168.mz3 100%[===================>] 397.38K --.-KB/s in 0.06s \n",
"\n",
"2024-06-26 10:16:34 (6.22 MB/s) - ‘../images/CIT168.mz3’ saved [406916/406916]\n",
"\n",
"--2024-06-26 10:16:34-- https://github.com/niivue/niivue/raw/main/demos/images/BrainMesh_ICBM152.lh.motor.mz3\n",
"Resolving github.com (github.com)... 140.82.112.4\n",
"Connecting to github.com (github.com)|140.82.112.4|:443... connected.\n",
"HTTP request sent, awaiting response... 302 Found\n",
"Location: https://raw.githubusercontent.com/niivue/niivue/main/demos/images/BrainMesh_ICBM152.lh.motor.mz3 [following]\n",
"--2024-06-26 10:16:34-- https://raw.githubusercontent.com/niivue/niivue/main/demos/images/BrainMesh_ICBM152.lh.motor.mz3\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8002::154, 2606:50c0:8003::154, 2606:50c0:8000::154, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8002::154|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 150304 (147K) [application/octet-stream]\n",
"Saving to: ‘../images/BrainMesh_ICBM152.lh.motor.mz3’\n",
"\n",
"BrainMesh_ICBM152.l 100%[===================>] 146.78K --.-KB/s in 0.04s \n",
"\n",
"2024-06-26 10:16:35 (3.38 MB/s) - ‘../images/BrainMesh_ICBM152.lh.motor.mz3’ saved [150304/150304]\n",
"\n"
]
}
],
"outputs": [],
"source": [
"!mkdir -p ../images/\n",
"path=\"https://github.com/niivue/niivue/raw/main/demos/images/\"\n",
Expand All @@ -71,25 +16,10 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "36d80248-e8a6-4f27-92c5-98dabf2aec81",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1992eb6742714197bde1dd598675cdde",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"NiiVue()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"from ipyniivue import NiiVue\n",
"\n",
Expand Down Expand Up @@ -122,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "956fe2ef-3180-44cc-a81e-05efc783ee05",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -155,7 +85,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.12.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
166 changes: 166 additions & 0 deletions examples/widgets.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "f26da1e3-877f-4893-8baa-8b9460601205",
"metadata": {},
"outputs": [],
"source": [
"!mkdir -p ../images/\n",
"path=\"https://github.com/niivue/niivue/raw/main/demos/images/\"\n",
"!wget {path}mni152.nii.gz -P ../images/\n",
"!wget {path}hippo.nii.gz -P ../images/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cda3a5f6-5bed-4190-82a7-057e17fa6635",
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets\n",
"\n",
"from ipyniivue import NiiVue, SliceType, WidgetObserver\n",
"\n",
"volumes = [\n",
" { \"path\": \"../images/mni152.nii.gz\", \"colormap\": \"gray\",\n",
" \"visible\": True, \"opacity\": 1.0 },\n",
" { \"path\": \"../images/hippo.nii.gz\", \"colormap\": \"red\",\n",
" \"visible\": True, \"opacity\": 1.0 },\n",
"]\n",
"\n",
"nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n",
"nv.load_volumes(volumes)\n",
"\n",
"widgetArray = []\n",
"\n",
"widget_slice_type = {\n",
"\n",
" \"widget\": ipywidgets.RadioButtons(\n",
" options=[('Axial', 0), ('Coronal', 1), ('Sagittal', 2),\n",
" ('Multiplanar', 3), ('Render', 4)],\n",
" value=3,\n",
" description='Slice Type:'),\n",
"\n",
" \"object\": nv,\n",
"\n",
" \"attribute\": \"slice_type\"\n",
"}\n",
"widgetArray.append(widget_slice_type)\n",
"\n",
"widget_scan_opacity = {\n",
"\n",
" \"widget\": ipywidgets.FloatSlider(\n",
" value=1.0,\n",
" min=0.0,\n",
" max=1.0,\n",
" step=0.1,\n",
" description='Scan Opacity:',\n",
" orientation='horizontal'),\n",
"\n",
" \"object\": nv.volumes[0],\n",
"\n",
" \"attribute\": \"opacity\"\n",
"}\n",
"widgetArray.append(widget_scan_opacity)\n",
"\n",
"widget_hippo_opacity = {\n",
"\n",
" \"widget\": ipywidgets.FloatSlider(\n",
" value=1.0,\n",
" min=0.0,\n",
" max=1.0,\n",
" step=0.1,\n",
" description='Hippocampus Opacity:',\n",
" orientation='horizontal'),\n",
"\n",
" \"object\": nv.volumes[1],\n",
"\n",
" \"attribute\": \"opacity\"\n",
"}\n",
"widgetArray.append(widget_hippo_opacity)\n",
"\n",
"widget_scan_colormap = {\n",
"\n",
" \"widget\": ipywidgets.Select(\n",
" options=['Gray', 'Red', 'Blue', 'Green'],\n",
" value='Gray',\n",
" description='Scan Colormap:'),\n",
"\n",
" \"object\": nv.volumes[0],\n",
"\n",
" \"attribute\": \"colormap\"\n",
"}\n",
"widgetArray.append(widget_scan_colormap)\n",
"\n",
"widget_hippo_colormap = {\n",
"\n",
" \"widget\": ipywidgets.Select(\n",
" options=['Red', 'Blue', 'Green', 'Gray'],\n",
" value='Red',\n",
" description='Hippocampus Colormap:'),\n",
"\n",
" \"object\": nv.volumes[1],\n",
"\n",
" \"attribute\": \"colormap\"\n",
"}\n",
"widgetArray.append(widget_hippo_colormap)\n",
"\n",
"for widget in widgetArray:\n",
" WidgetObserver(**widget)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cce13351-2f24-49fb-b8f6-4bac07f7b2dc",
"metadata": {},
"outputs": [],
"source": [
"nv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66b82692-c7b0-471d-8134-f399cf9ac399",
"metadata": {},
"outputs": [],
"source": [
"for widget in widgetArray:\n",
" display(widget[\"widget\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c94695d-b936-472e-a4f1-7dc216833388",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion src/ipyniivue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import importlib.metadata

from ._constants import DragMode, MuliplanarType, SliceType # noqa: F401
from ._widget import NiiVue # noqa: F401
from ._widget import NiiVue, WidgetObserver # noqa: F401

__version__ = importlib.metadata.version("ipyniivue")
15 changes: 15 additions & 0 deletions src/ipyniivue/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,18 @@ def add_mesh(self, mesh: Mesh):
def meshes(self):
"""Returns the list of meshes."""
return list(self._meshes)

class WidgetObserver:
"""Sets an observed for `widget` on the `attribute` of `object`."""

def __init__(self, widget, object, attribute):
self.widget = widget
self.object = object
self.attribute = attribute
self._observe()

def _widget_change(self, change):
setattr(self.object, self.attribute, change["new"])

def _observe(self):
self.widget.observe(self._widget_change, names=["value"])

0 comments on commit 7beeadc

Please sign in to comment.