Skip to content

Commit 8792ef9

Browse files
committed
Add nb
1 parent 2196ac1 commit 8792ef9

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
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": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"# Test with spatialdata-plot"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"import spatialdata_plot"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"sdata.pl.render_images(\"Visium_HD_Mouse_Small_Intestine_full_image\").pl.render_labels(\"rasterized_016um\").pl.show(\"Visium_HD_Mouse_Small_Intestine\")"
165+
]
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"metadata": {},
170+
"source": [
171+
"## Configure Vitessce\n",
172+
"\n",
173+
"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."
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {},
180+
"outputs": [],
181+
"source": [
182+
"vc = VitessceConfig(\n",
183+
" schema_version=\"1.0.18\",\n",
184+
" name='Visium HD SpatialData Demo',\n",
185+
")\n",
186+
"# Add data to the configuration:\n",
187+
"wrapper = SpatialDataWrapper(\n",
188+
" sdata_path=spatialdata_filepath,\n",
189+
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
190+
" image_path=\"images/Visium_HD_Mouse_Small_Intestine_full_image\",\n",
191+
" table_path=\"tables/square_016um\",\n",
192+
" obs_feature_matrix_path=\"tables/square_016um/X\",\n",
193+
" obs_segmentations_path=\"labels/rasterized_016um\",\n",
194+
" #region=\"CytAssist_FFPE_Human_Breast_Cancer\",\n",
195+
" coordinate_system=\"Visium_HD_Mouse_Small_Intestine\",\n",
196+
" coordination_values={\n",
197+
" # The following tells Vitessce to consider each observation as a \"bin\"\n",
198+
" \"obsType\": \"bin\",\n",
199+
" }\n",
200+
")\n",
201+
"dataset = vc.add_dataset(name='Visium HD').add_object(wrapper)\n",
202+
"\n",
203+
"# Add views (visualizations) to the configuration:\n",
204+
"spatial = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
205+
"feature_list = vc.add_view(\"featureList\", dataset=dataset)\n",
206+
"layer_controller = vc.add_view(\"layerControllerBeta\", dataset=dataset)\n",
207+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
208+
" 'imageLayer': CL([{\n",
209+
" 'photometricInterpretation': 'RGB',\n",
210+
" }]),\n",
211+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"image\"))\n",
212+
"obs_sets = vc.add_view(vt.OBS_SETS, dataset=dataset)\n",
213+
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType'], [wrapper.obs_type_label])\n",
214+
"\n",
215+
"# Layout the views\n",
216+
"vc.layout(spatial | (feature_list / layer_controller / obs_sets));"
217+
]
218+
},
219+
{
220+
"cell_type": "markdown",
221+
"metadata": {},
222+
"source": [
223+
"### Render the widget"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": null,
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"vw = vc.widget()\n",
233+
"vw"
234+
]
235+
},
236+
{
237+
"cell_type": "code",
238+
"execution_count": null,
239+
"metadata": {},
240+
"outputs": [],
241+
"source": [
242+
"import json\n",
243+
"print(json.dumps(vc.to_dict(base_url=f\"http://localhost:{vw.port}\")))"
244+
]
245+
},
246+
{
247+
"cell_type": "code",
248+
"execution_count": null,
249+
"metadata": {},
250+
"outputs": [],
251+
"source": []
252+
}
253+
],
254+
"metadata": {
255+
"kernelspec": {
256+
"display_name": "Python 3 (ipykernel)",
257+
"language": "python",
258+
"name": "python3"
259+
},
260+
"language_info": {
261+
"codemirror_mode": {
262+
"name": "ipython",
263+
"version": 3
264+
},
265+
"file_extension": ".py",
266+
"mimetype": "text/x-python",
267+
"name": "python",
268+
"nbconvert_exporter": "python",
269+
"pygments_lexer": "ipython3",
270+
"version": "3.10.14"
271+
}
272+
},
273+
"nbformat": 4,
274+
"nbformat_minor": 4
275+
}

0 commit comments

Comments
 (0)