Skip to content

Commit a6bd970

Browse files
authored
Add Visium HD nb (#463)
* Add nb * Add visium HD nb * Update VitessceWidget docs * Lint * Docs * Update marimo nb * Version
1 parent 2196ac1 commit a6bd970

File tree

7 files changed

+331
-32
lines changed

7 files changed

+331
-32
lines changed

docs/api_config.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ vitessce.config
1919
:exclude-members: VitessceConfig, VitessceChainableConfig
2020
:member-order: bysource
2121

22+
vitessce.widget
23+
***************
24+
25+
.. autoclass:: vitessce.widget.VitessceWidget()
26+
:members:
27+
:private-members:
28+
:exclude-members: close
29+
30+
.. autoclass:: vitessce.widget.VitesscePlugin
31+
:members:
32+
2233
vitessce.constants
2334
******************
2435

docs/api_data.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ vitessce.wrappers
1919
:members:
2020

2121
vitessce.export
22-
*****************
22+
***************
2323

2424
.. automodule:: vitessce.export
2525
:members:
2626

2727
vitessce.data_utils
28-
*****************
28+
*******************
2929

3030
.. automodule:: vitessce.data_utils.ome
3131
:members:

docs/notebooks/marimo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ def _(mo):
3333

3434
@app.cell
3535
def _(vw):
36-
vw.config
36+
vw._config
3737
return
3838

3939

4040
@app.cell
4141
def _(vw):
42-
vw.config["coordinationSpace"]["embeddingZoom"]
42+
vw._config["coordinationSpace"]["embeddingZoom"]
4343
return
4444

4545

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"## Import dependencies\n"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"import os\n",
33+
"from os.path import join, isfile, isdir\n",
34+
"from urllib.request import urlretrieve\n",
35+
"import zipfile\n",
36+
"\n",
37+
"from vitessce import (\n",
38+
" VitessceConfig,\n",
39+
" ViewType as vt,\n",
40+
" CoordinationType as ct,\n",
41+
" CoordinationLevel as CL,\n",
42+
" SpatialDataWrapper,\n",
43+
" get_initial_coordination_scope_prefix\n",
44+
")"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"data_dir = \"data\"\n",
54+
"zip_filepath = join(data_dir, \"visium_hd_3.0.0_io.zip\")\n",
55+
"spatialdata_filepath = join(data_dir, \"visium_hd_3.0.0.spatialdata.zarr\")"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"if not isdir(spatialdata_filepath):\n",
65+
" if not isfile(zip_filepath):\n",
66+
" os.makedirs(data_dir, exist_ok=True)\n",
67+
" urlretrieve('https://s3.embl.de/spatialdata/spatialdata-sandbox/visium_hd_3.0.0_io.zip', zip_filepath)\n",
68+
" with zipfile.ZipFile(zip_filepath,\"r\") as zip_ref:\n",
69+
" zip_ref.extractall(data_dir)\n",
70+
" os.rename(join(data_dir, \"data.zarr\"), spatialdata_filepath)"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"## Rasterize bins\n",
78+
"Reference: https://spatialdata.scverse.org/en/stable/tutorials/notebooks/notebooks/examples/technology_visium_hd.html#performant-on-the-fly-data-rasterization"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"from spatialdata import (\n",
88+
" read_zarr,\n",
89+
" rasterize_bins,\n",
90+
" rasterize_bins_link_table_to_labels\n",
91+
")"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"sdata = read_zarr(spatialdata_filepath)\n",
101+
"sdata"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"for bin_size in [\"016\", \"008\", \"002\"]:\n",
111+
" # rasterize_bins() requires a compresed sparse column (csc) matrix\n",
112+
" sdata.tables[f\"square_{bin_size}um\"].X = sdata.tables[f\"square_{bin_size}um\"].X.tocsc()\n",
113+
" rasterized = rasterize_bins(\n",
114+
" sdata,\n",
115+
" f\"Visium_HD_Mouse_Small_Intestine_square_{bin_size}um\",\n",
116+
" f\"square_{bin_size}um\",\n",
117+
" \"array_col\",\n",
118+
" \"array_row\",\n",
119+
" # We want to rasterize to a Labels element, rather than an Image element.\n",
120+
" return_region_as_labels=True\n",
121+
" )\n",
122+
" sdata[f\"rasterized_{bin_size}um\"] = rasterized\n",
123+
" rasterize_bins_link_table_to_labels(\n",
124+
" sdata,\n",
125+
" table_name=f\"square_{bin_size}um\",\n",
126+
" rasterized_labels_name=f\"rasterized_{bin_size}um\",\n",
127+
" )\n",
128+
" sdata.write_element(f\"rasterized_{bin_size}um\")"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": null,
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"sdata"
138+
]
139+
},
140+
{
141+
"cell_type": "markdown",
142+
"metadata": {},
143+
"source": [
144+
"## Configure Vitessce\n",
145+
"\n",
146+
"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."
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"vc = VitessceConfig(\n",
156+
" schema_version=\"1.0.18\",\n",
157+
" name='Visium HD SpatialData Demo',\n",
158+
")\n",
159+
"# Add data to the configuration:\n",
160+
"wrapper = SpatialDataWrapper(\n",
161+
" sdata_path=spatialdata_filepath,\n",
162+
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
163+
" image_path=\"images/Visium_HD_Mouse_Small_Intestine_full_image\",\n",
164+
" table_path=\"tables/square_016um\",\n",
165+
" obs_feature_matrix_path=\"tables/square_016um/X\",\n",
166+
" obs_segmentations_path=\"labels/rasterized_016um\",\n",
167+
" #region=\"CytAssist_FFPE_Human_Breast_Cancer\",\n",
168+
" coordinate_system=\"Visium_HD_Mouse_Small_Intestine\",\n",
169+
" coordination_values={\n",
170+
" # The following tells Vitessce to consider each observation as a \"bin\"\n",
171+
" \"obsType\": \"bin\",\n",
172+
" }\n",
173+
")\n",
174+
"dataset = vc.add_dataset(name='Visium HD').add_object(wrapper)\n",
175+
"\n",
176+
"# Add views (visualizations) to the configuration:\n",
177+
"spatial = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
178+
"feature_list = vc.add_view(\"featureList\", dataset=dataset)\n",
179+
"layer_controller = vc.add_view(\"layerControllerBeta\", dataset=dataset)\n",
180+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
181+
" 'imageLayer': CL([{\n",
182+
" 'photometricInterpretation': 'RGB',\n",
183+
" }]),\n",
184+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"image\"))\n",
185+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
186+
" 'segmentationLayer': CL([{\n",
187+
" 'segmentationChannel': CL([{\n",
188+
" 'spatialChannelOpacity': 0.5,\n",
189+
" 'obsColorEncoding': 'geneSelection',\n",
190+
" 'featureValueColormapRange': [0, 0.5],\n",
191+
" }])\n",
192+
" }]),\n",
193+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"obsSegmentations\"))\n",
194+
"obs_sets = vc.add_view(vt.OBS_SETS, dataset=dataset)\n",
195+
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType', 'featureSelection'], [wrapper.obs_type_label, ['AA986860']])\n",
196+
"\n",
197+
"# Layout the views\n",
198+
"vc.layout(spatial | (feature_list / layer_controller / obs_sets));"
199+
]
200+
},
201+
{
202+
"cell_type": "markdown",
203+
"metadata": {},
204+
"source": [
205+
"### Render the widget"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"metadata": {},
212+
"outputs": [],
213+
"source": [
214+
"vw = vc.widget()\n",
215+
"vw"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": null,
221+
"metadata": {},
222+
"outputs": [],
223+
"source": []
224+
}
225+
],
226+
"metadata": {
227+
"kernelspec": {
228+
"display_name": "Python 3 (ipykernel)",
229+
"language": "python",
230+
"name": "python3"
231+
},
232+
"language_info": {
233+
"codemirror_mode": {
234+
"name": "ipython",
235+
"version": 3
236+
},
237+
"file_extension": ".py",
238+
"mimetype": "text/x-python",
239+
"name": "python",
240+
"nbconvert_exporter": "python",
241+
"pygments_lexer": "ipython3",
242+
"version": "3.10.14"
243+
}
244+
},
245+
"nbformat": 4,
246+
"nbformat_minor": 4
247+
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "vitessce"
7-
version = "3.6.9"
7+
version = "3.7.0"
88
authors = [
99
{ name="Mark Keller", email="[email protected]" },
1010
]

src/vitessce/config.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,19 @@ def stop_server(self, port):
925925
del self.background_servers[port]
926926

927927
def stop_all_servers(self):
928+
"""
929+
Stop all background servers associated with this config instance.
930+
931+
.. code-block:: python
932+
:emphasize-lines: 5
933+
934+
from vitessce import VitessceConfig
935+
936+
vc = VitessceConfig(schema_version="1.0.18", name='My Config')
937+
vw = vc.widget()
938+
# ... do something with the widget ...
939+
vc.stop_all_servers()
940+
"""
928941
for server in self.background_servers.values():
929942
server.stop()
930943
self.background_servers = {}
@@ -1791,6 +1804,17 @@ def widget(self, **kwargs):
17911804
:param int height: The height of the widget, in pixels. By default, 600.
17921805
:param int port: The port to use when serving data objects on localhost. By default, 8000.
17931806
:param bool proxy: Is this widget being served through a proxy, for example with a cloud notebook (e.g. Binder)?
1807+
:param str js_package_version: The version of the NPM package ('vitessce' if not js_dev_mode else '@vitessce/dev').
1808+
:param bool js_dev_mode: Should @vitessce/dev be used (typically for debugging purposes)? By default, False.
1809+
:param str custom_js_url: A URL to a JavaScript file to use (instead of 'vitessce' or '@vitessce/dev' NPM package).
1810+
:param list[VitesscePlugin] plugins: A list of subclasses of VitesscePlugin. Optional.
1811+
:param bool remount_on_uid_change: Passed to the remountOnUidChange prop of the <Vitessce/> React component. By default, True.
1812+
:param bool prefer_local: Should local data be preferred (only applies to `*_artifact` data objects)? By default, True.
1813+
:param int invoke_timeout: The timeout in milliseconds for invoking Python functions from JavaScript. By default, 300000.
1814+
:param bool invoke_batched: Should invocations (Zarr gets) be submitted in batch, or individually? By default, True.
1815+
:param bool page_mode: Whether to render the <Vitessce/> component in grid-mode or page-mode. By default, False.
1816+
:param str page_esm: The ES module string for the page component creation function. Optional.
1817+
:param bool prevent_scroll: Should mouseover in the Vitessce widget prevent disable the scrolling of the notebook? By default, True.
17941818
17951819
:returns: The Jupyter widget.
17961820
:rtype: VitessceWidget

0 commit comments

Comments
 (0)