Skip to content

Commit 741b642

Browse files
committed
Add spatialdata notebook
1 parent d55d2a0 commit 741b642

File tree

2 files changed

+195
-3
lines changed

2 files changed

+195
-3
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"nbsphinx": "hidden"
7+
},
8+
"source": [
9+
"# Vitessce Widget Tutorial"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Visualization of a SpatialData object\n",
17+
"\n",
18+
"Data from https://github.com/scverse/spatialdata-notebooks/blob/main/notebooks/examples/transformations.ipynb"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {
24+
"tags": []
25+
},
26+
"source": [
27+
"## Import dependencies"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"import os\n",
37+
"from os.path import join, isfile, isdir\n",
38+
"from urllib.request import urlretrieve\n",
39+
"import zipfile\n",
40+
"\n",
41+
"from vitessce import (\n",
42+
" VitessceConfig,\n",
43+
" ViewType as vt,\n",
44+
" CoordinationType as ct,\n",
45+
" CoordinationLevel as CL,\n",
46+
" SpatialDataWrapper,\n",
47+
" get_initial_coordination_scope_prefix\n",
48+
")"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"data_dir = \"data\"\n",
58+
"zip_filepath = join(data_dir, \"mouse_liver.spatialdata.zarr.zip\")\n",
59+
"spatialdata_filepath = join(data_dir, \"mouse_liver.spatialdata.zarr\")"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"if not isdir(spatialdata_filepath):\n",
69+
" if not isfile(zip_filepath):\n",
70+
" os.makedirs(data_dir, exist_ok=True)\n",
71+
" urlretrieve('https://s3.embl.de/spatialdata/spatialdata-sandbox/mouse_liver.zip', zip_filepath)\n",
72+
" with zipfile.ZipFile(zip_filepath,\"r\") as zip_ref:\n",
73+
" zip_ref.extractall(data_dir)\n",
74+
" os.rename(join(data_dir, \"data.zarr\"), spatialdata_filepath)"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"## Configure Vitessce\n",
82+
"\n",
83+
"Vitessce needs to know which pieces of data we are interested in visualizing, the visualization types we would like to use, and how we want to coordinate (or link) the views."
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"vc = VitessceConfig(\n",
93+
" schema_version=\"1.0.17\",\n",
94+
" name='SpatialData Demo (mouse_liver)',\n",
95+
")\n",
96+
"# Add data to the configuration:\n",
97+
"wrapper = SpatialDataWrapper(\n",
98+
" sdata_store=spatialdata_filepath,\n",
99+
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
100+
" table_path=\"tables/table\",\n",
101+
" image_path=\"images/raw_image\",\n",
102+
" labels_path=\"labels/segmentation_mask\",\n",
103+
" #shapes_path=\"shapes/nucleus_boundaries\",\n",
104+
" #points_path=\"points/transcripts\",\n",
105+
" obs_feature_matrix_path=\"tables/table/X\",\n",
106+
" obs_set_paths=[\"tables/table/obs/annotation\"],\n",
107+
" obs_set_names=[\"Annotation\"],\n",
108+
" region=\"nucleus_boundaries\",\n",
109+
" coordinate_system=\"global\",\n",
110+
" coordination_values={\n",
111+
" \"obsType\": \"cell\" \n",
112+
" }\n",
113+
")\n",
114+
"dataset = vc.add_dataset(name='Mouse Liver').add_object(wrapper)\n",
115+
"\n",
116+
"# Add views (visualizations) to the configuration:\n",
117+
"spatial = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
118+
"feature_list = vc.add_view(vt.FEATURE_LIST, dataset=dataset)\n",
119+
"layer_controller = vc.add_view(\"layerControllerBeta\", dataset=dataset)\n",
120+
"obs_sets = vc.add_view(vt.OBS_SETS, dataset=dataset)\n",
121+
"\n",
122+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
123+
" 'imageLayer': CL([{\n",
124+
" 'photometricInterpretation': 'BlackIsZero',\n",
125+
" 'imageChannel': CL([{\n",
126+
" 'spatialChannelColor': [255, 255, 255],\n",
127+
" 'spatialChannelWindow': [0, 4000],\n",
128+
" }])\n",
129+
" }]),\n",
130+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"image\"))\n",
131+
"\n",
132+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
133+
" 'segmentationLayer': CL([{\n",
134+
" 'segmentationChannel': CL([{\n",
135+
" 'obsColorEncoding': 'cellSetSelection',\n",
136+
" }]),\n",
137+
" }]),\n",
138+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"obsSegmentations\"))\n",
139+
"\n",
140+
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType'], [wrapper.obs_type_label])\n",
141+
"\n",
142+
"# Layout the views\n",
143+
"vc.layout(spatial | (feature_list / layer_controller / obs_sets));"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"### Render the widget"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": null,
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"vw = vc.widget(custom_js_url=\"http://localhost:3001/packages/main/dev/dist/index.js\")\n",
160+
"vw"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": []
169+
}
170+
],
171+
"metadata": {
172+
"kernelspec": {
173+
"display_name": "Python 3 (ipykernel)",
174+
"language": "python",
175+
"name": "python3"
176+
},
177+
"language_info": {
178+
"codemirror_mode": {
179+
"name": "ipython",
180+
"version": 3
181+
},
182+
"file_extension": ".py",
183+
"mimetype": "text/x-python",
184+
"name": "python",
185+
"nbconvert_exporter": "python",
186+
"pygments_lexer": "ipython3",
187+
"version": "3.10.14"
188+
}
189+
},
190+
"nbformat": 4,
191+
"nbformat_minor": 4
192+
}

src/vitessce/widget.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class VitessceWidget(anywidget.AnyWidget):
463463

464464
next_port = DEFAULT_PORT
465465

466-
js_package_version = Unicode('3.5.4').tag(sync=True)
466+
js_package_version = Unicode('3.5.10').tag(sync=True)
467467
js_dev_mode = Bool(False).tag(sync=True)
468468
custom_js_url = Unicode('').tag(sync=True)
469469
plugin_esm = List(trait=Unicode(''), default_value=[]).tag(sync=True)
@@ -472,7 +472,7 @@ class VitessceWidget(anywidget.AnyWidget):
472472

473473
store_urls = List(trait=Unicode(''), default_value=[]).tag(sync=True)
474474

475-
def __init__(self, config, height=600, theme='auto', uid=None, port=None, proxy=False, js_package_version='3.5.4', js_dev_mode=False, custom_js_url='', plugins=None, remount_on_uid_change=True, prefer_local=True, invoke_timeout=30000):
475+
def __init__(self, config, height=600, theme='auto', uid=None, port=None, proxy=False, js_package_version='3.5.10', js_dev_mode=False, custom_js_url='', plugins=None, remount_on_uid_change=True, prefer_local=True, invoke_timeout=30000):
476476
"""
477477
Construct a new Vitessce widget.
478478
@@ -586,7 +586,7 @@ def _plugin_command(self, params, buffers):
586586
# Launch Vitessce using plain HTML representation (no ipywidgets)
587587

588588

589-
def ipython_display(config, height=600, theme='auto', base_url=None, host_name=None, uid=None, port=None, proxy=False, js_package_version='3.5.4', js_dev_mode=False, custom_js_url='', plugin_esm=DEFAULT_PLUGIN_ESM, remount_on_uid_change=True):
589+
def ipython_display(config, height=600, theme='auto', base_url=None, host_name=None, uid=None, port=None, proxy=False, js_package_version='3.5.10', js_dev_mode=False, custom_js_url='', plugin_esm=DEFAULT_PLUGIN_ESM, remount_on_uid_change=True):
590590
from IPython.display import display, HTML
591591
uid_str = "vitessce" + get_uid_str(uid)
592592

0 commit comments

Comments
 (0)