Skip to content

Commit 037a60f

Browse files
authored
Add another spatialdata example notebook (#458)
* Add another spatialdata example notebook * Update nb
1 parent 3a5cfda commit 037a60f

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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, \"merfish_2.spatialdata.zarr.zip\")\n",
55+
"spatialdata_filepath = join(data_dir, \"merfish_2.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/merfish.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+
"## Configure Vitessce\n",
78+
"\n",
79+
"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."
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"vc = VitessceConfig(\n",
89+
" schema_version=\"1.0.17\",\n",
90+
" name='MERFISH SpatialData Demo',\n",
91+
")\n",
92+
"# Add data to the configuration:\n",
93+
"wrapper = SpatialDataWrapper(\n",
94+
" sdata_path=spatialdata_filepath,\n",
95+
" # The following paths are relative to the root of the SpatialData zarr store on-disk.\n",
96+
" image_path=\"images/rasterized\",\n",
97+
" table_path=\"tables/table\",\n",
98+
" obs_feature_matrix_path=\"tables/table/X\",\n",
99+
" obs_spots_path=\"shapes/cells\",\n",
100+
" coordinate_system=\"global\",\n",
101+
" coordination_values={\n",
102+
" # The following tells Vitessce to consider each observation as a \"spot\"\n",
103+
" \"obsType\": \"cell\",\n",
104+
" }\n",
105+
")\n",
106+
"dataset = vc.add_dataset(name='MERFISH').add_object(wrapper)\n",
107+
"\n",
108+
"# Add views (visualizations) to the configuration:\n",
109+
"spatial = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
110+
"feature_list = vc.add_view(\"featureList\", dataset=dataset)\n",
111+
"layer_controller = vc.add_view(\"layerControllerBeta\", dataset=dataset)\n",
112+
"obs_sets = vc.add_view(\"obsSets\", dataset=dataset)\n",
113+
"\n",
114+
"vc.link_views_by_dict([spatial, layer_controller], {\n",
115+
" 'spotLayer': CL([{\n",
116+
" 'obsType': 'cell',\n",
117+
" }]),\n",
118+
"}, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"obsSpots\"))\n",
119+
"\n",
120+
"vc.link_views([spatial, layer_controller, feature_list, obs_sets], ['obsType'], [wrapper.obs_type_label])\n",
121+
"\n",
122+
"# Layout the views\n",
123+
"vc.layout(spatial | (feature_list / layer_controller / obs_sets));"
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"metadata": {},
129+
"source": [
130+
"### Render the widget"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": null,
136+
"metadata": {},
137+
"outputs": [],
138+
"source": [
139+
"vw = vc.widget()\n",
140+
"vw"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {},
147+
"outputs": [],
148+
"source": []
149+
}
150+
],
151+
"metadata": {
152+
"kernelspec": {
153+
"display_name": "Python 3 (ipykernel)",
154+
"language": "python",
155+
"name": "python3"
156+
},
157+
"language_info": {
158+
"codemirror_mode": {
159+
"name": "ipython",
160+
"version": 3
161+
},
162+
"file_extension": ".py",
163+
"mimetype": "text/x-python",
164+
"name": "python",
165+
"nbconvert_exporter": "python",
166+
"pygments_lexer": "ipython3",
167+
"version": "3.10.14"
168+
}
169+
},
170+
"nbformat": 4,
171+
"nbformat_minor": 4
172+
}

0 commit comments

Comments
 (0)