|
| 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 | +} |
0 commit comments