Skip to content

Commit 6f68c5c

Browse files
committed
Add notebook
1 parent adee748 commit 6f68c5c

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 AnnData object containing an image in `uns`"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"Note: This approach to storing images within AnnData objects is no longer recommended now that [SpatialData](https://spatialdata.scverse.org/en/stable/) has been introduced."
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"import scanpy as sc\n",
33+
"import numpy as np\n",
34+
"from vitessce.data_utils import rgb_img_to_ome_zarr, VAR_CHUNK_SIZE\n",
35+
"from vitessce import (\n",
36+
" VitessceConfig,\n",
37+
" AnnDataWrapper,\n",
38+
" ImageOmeZarrWrapper,\n",
39+
")\n",
40+
"from os.path import join"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"output_img = join(\"data\", \"V1_Human_Lymph_Node.ome.zarr\")\n",
50+
"output_adata = join(\"data\", \"V1_Human_Lymph_Node.anndata.zarr\")"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"adata = sc.datasets.visium_sge(sample_id=\"V1_Human_Lymph_Node\", include_hires_tiff=True)\n",
60+
"\n",
61+
"# Write img_arr to OME-Zarr.\n",
62+
"# Need to convert images from interleaved to non-interleaved (color axis should be first).\n",
63+
"img_hires = adata.uns['spatial']['V1_Human_Lymph_Node']['images']['hires']\n",
64+
"img_arr = np.transpose(img_hires, (2, 0, 1))\n",
65+
"# Convert values from [0, 1] to [0, 255].\n",
66+
"img_arr *= 255.0\n",
67+
"\n",
68+
"# First, save the image to an OME-Zarr image format\n",
69+
"rgb_img_to_ome_zarr(img_arr, output_img, axes=\"cyx\", chunks=(1, 256, 256), img_name=\"Image\")\n",
70+
"# Second, save the AnnData object to Zarr format\n",
71+
"adata.write_zarr(output_adata, chunks=[adata.shape[0], VAR_CHUNK_SIZE])"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"vc = VitessceConfig(schema_version=\"1.0.17\", name=\"AnnData with image\")\n",
81+
"dataset = vc.add_dataset(\"My dataset\").add_object(\n",
82+
" AnnDataWrapper(\n",
83+
" adata_path=output_adata,\n",
84+
" \n",
85+
" )\n",
86+
").add_object(\n",
87+
" ImageOmeZarrWrapper(\n",
88+
" img_path=output_img,\n",
89+
" )\n",
90+
")\n",
91+
"\n",
92+
"spatial_view = vc.add_view(\"spatialBeta\", dataset=dataset)\n",
93+
"lc_view = vc.add_view(\"layerControllerBeta\", dataset=dataset)\n",
94+
"\n",
95+
"vc.layout(spatial_view | lc_view);"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"vw = vc.widget()\n",
105+
"vw"
106+
]
107+
}
108+
],
109+
"metadata": {
110+
"kernelspec": {
111+
"display_name": "Python 3 (ipykernel)",
112+
"language": "python",
113+
"name": "python3"
114+
},
115+
"language_info": {
116+
"codemirror_mode": {
117+
"name": "ipython",
118+
"version": 3
119+
},
120+
"file_extension": ".py",
121+
"mimetype": "text/x-python",
122+
"name": "python",
123+
"nbconvert_exporter": "python",
124+
"pygments_lexer": "ipython3",
125+
"version": "3.10.14"
126+
}
127+
},
128+
"nbformat": 4,
129+
"nbformat_minor": 4
130+
}

src/vitessce/data_utils/ome.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ def rgb_img_to_ome_zarr(img_arr, output_path, img_name="Image", chunks=(1, 256,
9999
z_root.attrs["omero"] = {
100100
"name": img_name,
101101
"version": "0.3",
102-
"rdefs": {},
102+
"rdefs": {
103+
"model": "color",
104+
},
103105
"channels": [
104106
{
105107
"label": "R",
@@ -156,7 +158,9 @@ def multiplex_img_to_ome_zarr(img_arr, channel_names, output_path, img_name="Ima
156158
z_root.attrs["omero"] = {
157159
"name": img_name,
158160
"version": "0.3",
159-
"rdefs": {},
161+
"rdefs": {
162+
"model": "greyscale",
163+
},
160164
"channels": [
161165
{
162166
"label": channel_name,

0 commit comments

Comments
 (0)