|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "4cb17f8e-d21c-44fe-91f1-91edb1440023", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### 1. Load a Janelia COSEM Dataset into Xarray\n", |
| 9 | + "\n", |
| 10 | + "This function, `open_cosem_dataset`, helps load a 3D image volume from the [Janelia COSEM](https://www.janelia.org/project-team/cosem) dataset into an `xarray.Dataset`. It does the following:\n", |
| 11 | + "\n", |
| 12 | + "- Constructs the full URL from the dataset root and group path.\n", |
| 13 | + "- Opens a Zarr array using anonymous access via `fsspec`.\n", |
| 14 | + "- Extracts voxel spacing metadata (in nanometers) and uses it to create physical coordinates (in meters).\n", |
| 15 | + "- Wraps the data as an `xarray.DataArray` and then into a `Dataset` for ease of use.\n", |
| 16 | + "\n", |
| 17 | + "The result is a spatially aware dataset, with proper coordinates, ready for visualization or analysis.\n" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "id": "362a7c99-9b51-40d5-82c0-c9e786e2dbc0", |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "import xarray as xr\n", |
| 28 | + "import fsspec\n", |
| 29 | + "import zarr\n", |
| 30 | + "import numpy as np\n", |
| 31 | + "\n", |
| 32 | + "\n", |
| 33 | + "def open_cosem_dataset(dataset_url, group_path, var_name=\"values\"):\n", |
| 34 | + " \"\"\"\n", |
| 35 | + " Open a COSEM Zarr volume as an xarray.Dataset with physical coordinates.\n", |
| 36 | + "\n", |
| 37 | + " Parameters\n", |
| 38 | + " ----------\n", |
| 39 | + " dataset_url : str\n", |
| 40 | + " e.g. \"s3://janelia-cosem-datasets/jrc_hela-1/jrc_hela-1.zarr\"\n", |
| 41 | + " group_path : str\n", |
| 42 | + " e.g. \"recon-1/em/fibsem-uint8/s4\"\n", |
| 43 | + " var_name : str\n", |
| 44 | + " Name to assign to the variable in the Dataset\n", |
| 45 | + "\n", |
| 46 | + " Returns\n", |
| 47 | + " -------\n", |
| 48 | + " xr.Dataset\n", |
| 49 | + " \"\"\"\n", |
| 50 | + " # Open remote store\n", |
| 51 | + " full_url = f\"{dataset_url}/{group_path}\"\n", |
| 52 | + " z = zarr.open_array(full_url, mode=\"r\", storage_options={\"anon\": True})\n", |
| 53 | + "\n", |
| 54 | + " # Default dims (Z, Y, X in COSEM, we'll reverse to X, Y, Z for xarray consistency)\n", |
| 55 | + " shape = z.shape\n", |
| 56 | + " dims = [\"z\", \"y\", \"x\"]\n", |
| 57 | + " if len(shape) != 3:\n", |
| 58 | + " raise ValueError(f\"Expected 3D data, got shape: {shape}\")\n", |
| 59 | + "\n", |
| 60 | + " # Try to get voxel spacing in nm\n", |
| 61 | + " voxel_size_nm = z.attrs.get(\"pixelResolution\", {}).get(\n", |
| 62 | + " \"dimensions\", [4.0, 4.0, 4.0]\n", |
| 63 | + " ) # [Z, Y, X]\n", |
| 64 | + "\n", |
| 65 | + " # Build coordinate arrays in physical units (meters)\n", |
| 66 | + " coords = {}\n", |
| 67 | + " for dim, size, spacing_nm in zip(dims, shape, voxel_size_nm):\n", |
| 68 | + " coords[dim] = np.arange(size) * spacing_nm * 1e-9 # convert nm → meters\n", |
| 69 | + "\n", |
| 70 | + " # Construct DataArray with coords\n", |
| 71 | + " da = xr.DataArray(z, dims=dims, coords=coords, attrs=dict(z.attrs))\n", |
| 72 | + "\n", |
| 73 | + " # Wrap into a Dataset\n", |
| 74 | + " ds = xr.Dataset({var_name: da})\n", |
| 75 | + " return ds" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "id": "251c71cd-7db9-4ab9-8d02-92dfe0f693c6", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "### 2. Retrieve the COSEM Dataset\n", |
| 84 | + "\n", |
| 85 | + "Here, we use the `open_cosem_dataset` function to load a specific FIB-SEM volume from the COSEM dataset collection:\n", |
| 86 | + "\n", |
| 87 | + "- `url` points to the dataset root in the S3 bucket.\n", |
| 88 | + "- `group` identifies the subvolume (at a particular resolution level).\n", |
| 89 | + "\n", |
| 90 | + "The result is assigned to `ds`, an `xarray.Dataset` that includes:\n", |
| 91 | + "\n", |
| 92 | + "- A single data variable called `\"values\"`,\n", |
| 93 | + "- Dimensions named `z`, `y`, and `x`,\n", |
| 94 | + "- Coordinates in physical space (meters), based on metadata." |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "id": "2f23562c-bc1b-4e60-b372-d660be6c6bb7", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "url = \"s3://janelia-cosem-datasets/jrc_hela-1/jrc_hela-1.zarr\"\n", |
| 105 | + "group = \"recon-1/em/fibsem-uint8/s4\"\n", |
| 106 | + "\n", |
| 107 | + "ds = open_cosem_dataset(url, group)" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "id": "234cd32a-2675-439e-9202-58ea492557e7", |
| 113 | + "metadata": {}, |
| 114 | + "source": [ |
| 115 | + "### 3. Visualize with Pan3D Viewer\n", |
| 116 | + "\n", |
| 117 | + "We now create a `Pan3D` viewer instance to visualize the dataset.\n", |
| 118 | + "\n", |
| 119 | + "- The dataset `ds` is passed to `XArrayViewer`, which sets up a Trame-based UI for interactive volume exploration.\n", |
| 120 | + "- Once `viewer.ui.ready` resolves, the viewer is fully initialized.\n", |
| 121 | + "- The `viewer.ui` object can be rendered directly in the notebook (if supported), or externally in a browser window.\n", |
| 122 | + "\n", |
| 123 | + "This viewer makes it easy to inspect and explore 3D microscopy datasets with physical scale preserved.\n" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "id": "264f7e80-578f-4b65-b247-dd5f87c2ba17", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "# Create the instance of the viewer, and pass the filter to the pipeline argument\n", |
| 134 | + "\n", |
| 135 | + "from pan3d.viewers.preview import XArrayViewer\n", |
| 136 | + "\n", |
| 137 | + "viewer = XArrayViewer(xarray=ds, server=\"preview\")\n", |
| 138 | + "await viewer.ui.ready\n", |
| 139 | + "\n", |
| 140 | + "viewer.ui" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "metadata": { |
| 145 | + "kernelspec": { |
| 146 | + "display_name": "Python 3 (ipykernel)", |
| 147 | + "language": "python", |
| 148 | + "name": "python3" |
| 149 | + }, |
| 150 | + "language_info": { |
| 151 | + "codemirror_mode": { |
| 152 | + "name": "ipython", |
| 153 | + "version": 3 |
| 154 | + }, |
| 155 | + "file_extension": ".py", |
| 156 | + "mimetype": "text/x-python", |
| 157 | + "name": "python", |
| 158 | + "nbconvert_exporter": "python", |
| 159 | + "pygments_lexer": "ipython3", |
| 160 | + "version": "3.13.5" |
| 161 | + } |
| 162 | + }, |
| 163 | + "nbformat": 4, |
| 164 | + "nbformat_minor": 5 |
| 165 | +} |
0 commit comments