Convert data from InSituPy into SpatialData#

Setup and Imports#

# Enable autoreload for development
%load_ext autoreload
%autoreload 2

Make sure SpatialData is installed#

If it is not installed yet, install it with:

pip install spatialdata[extra]

InSituPy pins spatialdata>=0.8.0,<0.9.0 (see pyproject.toml) - the export is developed and tested against spatialdata==0.8.0, paired with a pinned zarr>=3.2.1,<4.0.0 so the exported store is deterministically zarr v3. For more information on the installation of SpatialData see here.

from pathlib import Path

from insitupy import CACHE, InSituData, InSituExperiment
from insitupy.spatialdata import convert_to_spatialdata

Load InSituPy Data#

First, let’s load some example data. We’ll demonstrate conversion with both:

  • InSituData: A single spatial sample

  • InSituExperiment: A collection of multiple samples

Loading a Single Sample (InSituData)#

# Load a single InSituData object
data_dir = Path(CACHE / "out/demo_insitupy_project")
xd = InSituData.read(data_dir)
xd.load_all()
# Display the InSituData object
xd
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
UID:		None
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project

    ➤ images
       'CD20':     (25778, 35416)
       'HE':       (25778, 35416, 3)
       'HER2':     (25778, 35416)
       'nuclei':   (25778, 35416)
    ➤ cells
       MultiCellData with main layer 'main'
           table
               AnnData object with n_obs × n_vars = 157600 × 297
               obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden'
               var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
               uns: 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
               obsm: 'X_pca', 'X_umap', 'spatial'
               varm: 'PCs'
               layers: 'counts', 'norm_counts'
               obsp: 'connectivities', 'distances'
           boundaries
               BoundariesData object with 2 entries:
                   cells
                   nuclei
    ➤ annotations
       demo:	4 annotations, 2 names ('Negative', 'Positive')
       demo2:	5 annotations, 3 names ('Negative', 'Other', 'Positive')
       demo3:	7 annotations, 5 names ('Immune cells', 'Necrosis', 'Stroma', 'Tumor', 'unclassified')
    ➤ regions
       demo_regions:	3 regions, 3 names ('Region1', 'Region2', 'Region3')
       TMA:	6 regions, 6 names ('A-1', 'A-2', 'A-3', 'B-1', 'B-2', 'B-3')
    ➤ transcripts
       DataFrame with shape <dask_expr.expr.Scalar: expr=ReadParquetFSSpec(1026e34).size() // 8, dtype=int64> x 8

Create dataset with multiple samples (InSituExperiment)#

Note on Transcripts: Historically, a dask version conflict between spatialdata and dask-geopandas made transcript cropping with dask DataFrames very inefficient whenever spatialdata was also installed. As of spatialdata==0.8.0 (the version InSituPy now requires) this dependency conflict is resolved - both packages install and import together cleanly. The cropping performance itself has not been re-benchmarked here, so we still delete the transcripts beforehand to keep this demo fast and sidestep the workflow entirely for from_regions().

To avoid performance issues when creating an InSituExperiment from regions (which involves cropping), we delete the transcripts beforehand.

# del xd.transcripts
exp = InSituExperiment.from_regions(
    data=xd,
    region_key="TMA"  # Column in annotations containing region IDs
)
2026-07-11 21:59:40 | [INFO] Loading transcripts into memory...
2026-07-11 21:59:43 | [INFO] Transcripts loaded: 42,638,083 rows.
# Display the experiment
exp
InSituExperiment (insitupy mode)
Path: None
▶ data
6 samples
3 metadata columns:
"uid", "region_key", "region_name"
Loaded modalities
cells: 6/6
images: 6/6
transcripts: 6/6
annotations: 3/6
regions: 6/6
▶ filters
Base filters (0)

none

Composite filters (0)

none

▶ table
no tables built

Convert to SpatialData#

The convert_to_spatialdata() function handles the conversion of all data modalities into SpatialData elements. The full, versioned specification of this naming dialect lives in insitupy/spatialdata/DIALECT.md; a summary follows.

Element naming convention#

{SAMPLE.<uid>..}?<MODALITY>.<locator>[.<locator>...]

The SAMPLE.<uid>.. prefix (note the trailing double dot) is added only when converting an InSituExperiment; a single InSituData produces un-prefixed keys.

Single Sample (InSituData):

  • IMAGES.CD20 - CD20 staining image

  • CELLS.main.table - main cell expression table

  • CELLS.main.circles / CELLS.main.circles_sized - cell centroid circles

  • CELLS.main.boundaries.cells - cell boundary masks

  • UNITS.<key>.table / UNITS.<key>.shapes - spatial units (e.g. Visium spots), if present - see §3.3 below

  • TRANSCRIPTS - transcript point data (omit entirely with include_transcripts=False, see §3.4)

  • ANNOTATIONS.Demo / REGIONS.TMA - annotation / region shapes

Multiple Samples (InSituExperiment):

Every key above is prefixed with SAMPLE.<uid>.., e.g. SAMPLE.1f1bcf1f..IMAGES.CD20, SAMPLE.1f1bcf1f..CELLS.main.table.

The store also carries a versioned dialect descriptor at sdata.attrs["insitupy_spatialdata_dialect"], so a reader can detect “this store is InSituPy dialect, version N” without parsing element-name strings - demonstrated in §3.4.

3.1 Convert individual sample#

# Convert InSituData to SpatialData
sdata = convert_to_spatialdata(xd)
2026-07-11 22:00:48 | [INFO] No case-insensitive conflicts found.
# Display the SpatialData object
sdata
SpatialData object
├── Images
│     ├── 'IMAGES.CD20': DataTree[cyx] (1, 25778, 35416), (1, 12889, 17708), (1, 6444, 8854), (1, 3222, 4427), (1, 1611, 2213), (1, 805, 1106)
│     ├── 'IMAGES.HE': DataTree[cyx] (3, 25778, 35416), (3, 12889, 17708), (3, 6444, 8854), (3, 3222, 4427), (3, 1611, 2213), (3, 805, 1106)
│     ├── 'IMAGES.HER2': DataTree[cyx] (1, 25778, 35416), (1, 12889, 17708), (1, 6444, 8854), (1, 3222, 4427), (1, 1611, 2213), (1, 805, 1106)
│     └── 'IMAGES.nuclei': DataTree[cyx] (1, 25778, 35416), (1, 12889, 17708), (1, 6444, 8854), (1, 3222, 4427), (1, 1611, 2213), (1, 805, 1106)
├── Labels
│     ├── 'CELLS.main.boundaries.cells': DataTree[yx] (25778, 35416), (12889, 17708), (6444, 8854), (3222, 4427), (1611, 2213), (805, 1106)
│     └── 'CELLS.main.boundaries.nuclei': DataTree[yx] (25778, 35416), (12889, 17708), (6444, 8854), (3222, 4427), (1611, 2213), (805, 1106)
├── Points
│     └── 'TRANSCRIPTS': DataFrame with shape: (42638083, 8) (3D points)
├── Shapes
│     ├── 'ANNOTATIONS.demo': GeoDataFrame shape: (4, 7) (2D shapes)
│     ├── 'ANNOTATIONS.demo2': GeoDataFrame shape: (5, 7) (2D shapes)
│     ├── 'ANNOTATIONS.demo3': GeoDataFrame shape: (7, 7) (2D shapes)
│     ├── 'CELLS.main.circles': GeoDataFrame shape: (157600, 2) (2D shapes)
│     ├── 'CELLS.main.circles_sized': GeoDataFrame shape: (157600, 2) (2D shapes)
│     ├── 'REGIONS.TMA': GeoDataFrame shape: (6, 8) (2D shapes)
│     └── 'REGIONS.demo_regions': GeoDataFrame shape: (3, 7) (2D shapes)
└── Tables
      └── 'CELLS.main.table': AnnData (157600, 297)
with coordinate systems:
    ▸ 'global', with elements:
        IMAGES.CD20 (Images), IMAGES.HE (Images), IMAGES.HER2 (Images), IMAGES.nuclei (Images), CELLS.main.boundaries.cells (Labels), CELLS.main.boundaries.nuclei (Labels), TRANSCRIPTS (Points), ANNOTATIONS.demo (Shapes), ANNOTATIONS.demo2 (Shapes), ANNOTATIONS.demo3 (Shapes), CELLS.main.circles (Shapes), CELLS.main.circles_sized (Shapes), REGIONS.TMA (Shapes), REGIONS.demo_regions (Shapes)

3.2 Convert Experiment (Multiple Samples)#

# Convert InSituExperiment to SpatialData
sdexp = convert_to_spatialdata(exp)
2026-07-11 22:01:21 | [INFO] No case-insensitive conflicts found.
# Display the experiment SpatialData
sdexp
SpatialData object
├── Images
│     ├── 'SAMPLE.4bd78871..IMAGES.CD20': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.4bd78871..IMAGES.HE': DataTree[cyx] (3, 4706, 4706), (3, 2353, 2353), (3, 1176, 1176), (3, 588, 588), (3, 294, 294), (3, 147, 147)
│     ├── 'SAMPLE.4bd78871..IMAGES.HER2': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.4bd78871..IMAGES.nuclei': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.8bd49b5f..IMAGES.CD20': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.8bd49b5f..IMAGES.HE': DataTree[cyx] (3, 4706, 4706), (3, 2353, 2353), (3, 1176, 1176), (3, 588, 588), (3, 294, 294), (3, 147, 147)
│     ├── 'SAMPLE.8bd49b5f..IMAGES.HER2': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.8bd49b5f..IMAGES.nuclei': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.9dc755cc..IMAGES.CD20': DataTree[cyx] (1, 4706, 4705), (1, 2353, 2352), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.9dc755cc..IMAGES.HE': DataTree[cyx] (3, 4706, 4705), (3, 2353, 2352), (3, 1176, 1176), (3, 588, 588), (3, 294, 294), (3, 147, 147)
│     ├── 'SAMPLE.9dc755cc..IMAGES.HER2': DataTree[cyx] (1, 4706, 4705), (1, 2353, 2352), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.9dc755cc..IMAGES.nuclei': DataTree[cyx] (1, 4706, 4705), (1, 2353, 2352), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.b5ff08a0..IMAGES.CD20': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.b5ff08a0..IMAGES.HE': DataTree[cyx] (3, 4706, 4706), (3, 2353, 2353), (3, 1176, 1176), (3, 588, 588), (3, 294, 294), (3, 147, 147)
│     ├── 'SAMPLE.b5ff08a0..IMAGES.HER2': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.b5ff08a0..IMAGES.nuclei': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.b47c2bbb..IMAGES.CD20': DataTree[cyx] (1, 4706, 4705), (1, 2353, 2352), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.b47c2bbb..IMAGES.HE': DataTree[cyx] (3, 4706, 4705), (3, 2353, 2352), (3, 1176, 1176), (3, 588, 588), (3, 294, 294), (3, 147, 147)
│     ├── 'SAMPLE.b47c2bbb..IMAGES.HER2': DataTree[cyx] (1, 4706, 4705), (1, 2353, 2352), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.b47c2bbb..IMAGES.nuclei': DataTree[cyx] (1, 4706, 4705), (1, 2353, 2352), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.fd4307dd..IMAGES.CD20': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     ├── 'SAMPLE.fd4307dd..IMAGES.HE': DataTree[cyx] (3, 4706, 4706), (3, 2353, 2353), (3, 1176, 1176), (3, 588, 588), (3, 294, 294), (3, 147, 147)
│     ├── 'SAMPLE.fd4307dd..IMAGES.HER2': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
│     └── 'SAMPLE.fd4307dd..IMAGES.nuclei': DataTree[cyx] (1, 4706, 4706), (1, 2353, 2353), (1, 1176, 1176), (1, 588, 588), (1, 294, 294), (1, 147, 147)
├── Labels
│     ├── 'SAMPLE.4bd78871..CELLS.main.boundaries.cells': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.4bd78871..CELLS.main.boundaries.nuclei': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.8bd49b5f..CELLS.main.boundaries.cells': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.8bd49b5f..CELLS.main.boundaries.nuclei': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.9dc755cc..CELLS.main.boundaries.cells': DataTree[yx] (4706, 4705), (2353, 2352), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.9dc755cc..CELLS.main.boundaries.nuclei': DataTree[yx] (4706, 4705), (2353, 2352), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.b5ff08a0..CELLS.main.boundaries.cells': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.b5ff08a0..CELLS.main.boundaries.nuclei': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.b47c2bbb..CELLS.main.boundaries.cells': DataTree[yx] (4706, 4705), (2353, 2352), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.b47c2bbb..CELLS.main.boundaries.nuclei': DataTree[yx] (4706, 4705), (2353, 2352), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     ├── 'SAMPLE.fd4307dd..CELLS.main.boundaries.cells': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
│     └── 'SAMPLE.fd4307dd..CELLS.main.boundaries.nuclei': DataTree[yx] (4706, 4706), (2353, 2353), (1176, 1176), (588, 588), (294, 294), (147, 147)
├── Points
│     ├── 'SAMPLE.4bd78871..TRANSCRIPTS': DataFrame with shape: (<Delayed>, 8) (3D points)
│     ├── 'SAMPLE.8bd49b5f..TRANSCRIPTS': DataFrame with shape: (<Delayed>, 8) (3D points)
│     ├── 'SAMPLE.9dc755cc..TRANSCRIPTS': DataFrame with shape: (<Delayed>, 8) (3D points)
│     ├── 'SAMPLE.b5ff08a0..TRANSCRIPTS': DataFrame with shape: (<Delayed>, 8) (3D points)
│     ├── 'SAMPLE.b47c2bbb..TRANSCRIPTS': DataFrame with shape: (<Delayed>, 8) (3D points)
│     └── 'SAMPLE.fd4307dd..TRANSCRIPTS': DataFrame with shape: (<Delayed>, 8) (3D points)
├── Shapes
│     ├── 'SAMPLE.4bd78871..ANNOTATIONS.demo2': GeoDataFrame shape: (1, 7) (2D shapes)
│     ├── 'SAMPLE.4bd78871..CELLS.main.circles': GeoDataFrame shape: (3597, 2) (2D shapes)
│     ├── 'SAMPLE.4bd78871..CELLS.main.circles_sized': GeoDataFrame shape: (3597, 2) (2D shapes)
│     ├── 'SAMPLE.4bd78871..REGIONS.TMA': GeoDataFrame shape: (1, 8) (2D shapes)
│     ├── 'SAMPLE.8bd49b5f..ANNOTATIONS.demo': GeoDataFrame shape: (2, 7) (2D shapes)
│     ├── 'SAMPLE.8bd49b5f..ANNOTATIONS.demo2': GeoDataFrame shape: (1, 7) (2D shapes)
│     ├── 'SAMPLE.8bd49b5f..CELLS.main.circles': GeoDataFrame shape: (3411, 2) (2D shapes)
│     ├── 'SAMPLE.8bd49b5f..CELLS.main.circles_sized': GeoDataFrame shape: (3411, 2) (2D shapes)
│     ├── 'SAMPLE.8bd49b5f..REGIONS.TMA': GeoDataFrame shape: (1, 8) (2D shapes)
│     ├── 'SAMPLE.9dc755cc..CELLS.main.circles': GeoDataFrame shape: (1795, 2) (2D shapes)
│     ├── 'SAMPLE.9dc755cc..CELLS.main.circles_sized': GeoDataFrame shape: (1795, 2) (2D shapes)
│     ├── 'SAMPLE.9dc755cc..REGIONS.TMA': GeoDataFrame shape: (1, 8) (2D shapes)
│     ├── 'SAMPLE.b5ff08a0..CELLS.main.circles': GeoDataFrame shape: (4878, 2) (2D shapes)
│     ├── 'SAMPLE.b5ff08a0..CELLS.main.circles_sized': GeoDataFrame shape: (4878, 2) (2D shapes)
│     ├── 'SAMPLE.b5ff08a0..REGIONS.TMA': GeoDataFrame shape: (1, 8) (2D shapes)
│     ├── 'SAMPLE.b47c2bbb..CELLS.main.circles': GeoDataFrame shape: (1822, 2) (2D shapes)
│     ├── 'SAMPLE.b47c2bbb..CELLS.main.circles_sized': GeoDataFrame shape: (1822, 2) (2D shapes)
│     ├── 'SAMPLE.b47c2bbb..REGIONS.TMA': GeoDataFrame shape: (1, 8) (2D shapes)
│     ├── 'SAMPLE.fd4307dd..ANNOTATIONS.demo2': GeoDataFrame shape: (1, 7) (2D shapes)
│     ├── 'SAMPLE.fd4307dd..CELLS.main.circles': GeoDataFrame shape: (2317, 2) (2D shapes)
│     ├── 'SAMPLE.fd4307dd..CELLS.main.circles_sized': GeoDataFrame shape: (2317, 2) (2D shapes)
│     ├── 'SAMPLE.fd4307dd..REGIONS.TMA': GeoDataFrame shape: (1, 8) (2D shapes)
│     └── 'SAMPLE.fd4307dd..REGIONS.demo_regions': GeoDataFrame shape: (1, 7) (2D shapes)
└── Tables
      ├── 'SAMPLE.4bd78871..CELLS.main.table': AnnData (3597, 297)
      ├── 'SAMPLE.8bd49b5f..CELLS.main.table': AnnData (3411, 297)
      ├── 'SAMPLE.9dc755cc..CELLS.main.table': AnnData (1795, 297)
      ├── 'SAMPLE.b5ff08a0..CELLS.main.table': AnnData (4878, 297)
      ├── 'SAMPLE.b47c2bbb..CELLS.main.table': AnnData (1822, 297)
      └── 'SAMPLE.fd4307dd..CELLS.main.table': AnnData (2317, 297)
with coordinate systems:
    ▸ 'global', with elements:
        SAMPLE.4bd78871..IMAGES.CD20 (Images), SAMPLE.4bd78871..IMAGES.HE (Images), SAMPLE.4bd78871..IMAGES.HER2 (Images), SAMPLE.4bd78871..IMAGES.nuclei (Images), SAMPLE.8bd49b5f..IMAGES.CD20 (Images), SAMPLE.8bd49b5f..IMAGES.HE (Images), SAMPLE.8bd49b5f..IMAGES.HER2 (Images), SAMPLE.8bd49b5f..IMAGES.nuclei (Images), SAMPLE.9dc755cc..IMAGES.CD20 (Images), SAMPLE.9dc755cc..IMAGES.HE (Images), SAMPLE.9dc755cc..IMAGES.HER2 (Images), SAMPLE.9dc755cc..IMAGES.nuclei (Images), SAMPLE.b5ff08a0..IMAGES.CD20 (Images), SAMPLE.b5ff08a0..IMAGES.HE (Images), SAMPLE.b5ff08a0..IMAGES.HER2 (Images), SAMPLE.b5ff08a0..IMAGES.nuclei (Images), SAMPLE.b47c2bbb..IMAGES.CD20 (Images), SAMPLE.b47c2bbb..IMAGES.HE (Images), SAMPLE.b47c2bbb..IMAGES.HER2 (Images), SAMPLE.b47c2bbb..IMAGES.nuclei (Images), SAMPLE.fd4307dd..IMAGES.CD20 (Images), SAMPLE.fd4307dd..IMAGES.HE (Images), SAMPLE.fd4307dd..IMAGES.HER2 (Images), SAMPLE.fd4307dd..IMAGES.nuclei (Images), SAMPLE.4bd78871..CELLS.main.boundaries.cells (Labels), SAMPLE.4bd78871..CELLS.main.boundaries.nuclei (Labels), SAMPLE.8bd49b5f..CELLS.main.boundaries.cells (Labels), SAMPLE.8bd49b5f..CELLS.main.boundaries.nuclei (Labels), SAMPLE.9dc755cc..CELLS.main.boundaries.cells (Labels), SAMPLE.9dc755cc..CELLS.main.boundaries.nuclei (Labels), SAMPLE.b5ff08a0..CELLS.main.boundaries.cells (Labels), SAMPLE.b5ff08a0..CELLS.main.boundaries.nuclei (Labels), SAMPLE.b47c2bbb..CELLS.main.boundaries.cells (Labels), SAMPLE.b47c2bbb..CELLS.main.boundaries.nuclei (Labels), SAMPLE.fd4307dd..CELLS.main.boundaries.cells (Labels), SAMPLE.fd4307dd..CELLS.main.boundaries.nuclei (Labels), SAMPLE.4bd78871..TRANSCRIPTS (Points), SAMPLE.8bd49b5f..TRANSCRIPTS (Points), SAMPLE.9dc755cc..TRANSCRIPTS (Points), SAMPLE.b5ff08a0..TRANSCRIPTS (Points), SAMPLE.b47c2bbb..TRANSCRIPTS (Points), SAMPLE.fd4307dd..TRANSCRIPTS (Points), SAMPLE.4bd78871..ANNOTATIONS.demo2 (Shapes), SAMPLE.4bd78871..CELLS.main.circles (Shapes), SAMPLE.4bd78871..CELLS.main.circles_sized (Shapes), SAMPLE.4bd78871..REGIONS.TMA (Shapes), SAMPLE.8bd49b5f..ANNOTATIONS.demo (Shapes), SAMPLE.8bd49b5f..ANNOTATIONS.demo2 (Shapes), SAMPLE.8bd49b5f..CELLS.main.circles (Shapes), SAMPLE.8bd49b5f..CELLS.main.circles_sized (Shapes), SAMPLE.8bd49b5f..REGIONS.TMA (Shapes), SAMPLE.9dc755cc..CELLS.main.circles (Shapes), SAMPLE.9dc755cc..CELLS.main.circles_sized (Shapes), SAMPLE.9dc755cc..REGIONS.TMA (Shapes), SAMPLE.b5ff08a0..CELLS.main.circles (Shapes), SAMPLE.b5ff08a0..CELLS.main.circles_sized (Shapes), SAMPLE.b5ff08a0..REGIONS.TMA (Shapes), SAMPLE.b47c2bbb..CELLS.main.circles (Shapes), SAMPLE.b47c2bbb..CELLS.main.circles_sized (Shapes), SAMPLE.b47c2bbb..REGIONS.TMA (Shapes), SAMPLE.fd4307dd..ANNOTATIONS.demo2 (Shapes), SAMPLE.fd4307dd..CELLS.main.circles (Shapes), SAMPLE.fd4307dd..CELLS.main.circles_sized (Shapes), SAMPLE.fd4307dd..REGIONS.TMA (Shapes), SAMPLE.fd4307dd..REGIONS.demo_regions (Shapes)

3.3 Spatial units export#

Spatial units (e.g. Visium spots, or any other polygon-based unit layer added via InSituData.add_units()) are exported as a TableModel + ShapesModel pair, following the same region/region_key/instance_key linkage as cells - but using the units’ own real polygon geometries directly instead of synthesizing circles from centroids.

The demo project loaded above has no units attached, so this section loads a small, self-contained Visium dataset instead (images are dropped here purely to keep the cell fast and focused on the units modality).

from insitupy.datasets import visium_human_breast_cancer

visium = visium_human_breast_cancer()
del visium.images  # keep this demo focused on the units modality

sdata_units = convert_to_spatialdata(visium)
sdata_units
2026-07-11 22:01:38 | [INFO] H5 file exists. Checking md5sum...
2026-07-11 22:01:38 | [INFO] The h5 file md5sum matches. Download is skipped. To force download set `overwrite=True`.
2026-07-11 22:01:38 | [INFO] Spatial directory exists. Download is skipped. To force download set `overwrite=True`.
2026-07-11 22:01:38 | [INFO] Visium data structure is ready at C:\Users\ge37voy\.cache\InSituPy\demo_datasets\visium_hbreastcancer\CytAssist_FFPE_Human_Breast_Cancer
2026-07-11 22:01:38 | [INFO] Dataset contains:
2026-07-11 22:01:38 | [INFO] - filtered_feature_bc_matrix.h5
2026-07-11 22:01:38 | [INFO] - spatial/ directory
2026-07-11 22:01:38 | [INFO] Reading Visium data with spatialdata-io...
2026-07-11 22:01:39 | [INFO] Using 'visium' coordinate system for pixel size extraction.
2026-07-11 22:01:39 | [INFO] Adding images...
2026-07-11 22:01:40 | [INFO] Converting 4992 Point geometries with radius to circular polygons using buffer.
2026-07-11 22:01:40 | [WARNING] Indices in `.shapes` do not match `.data.obs_names`. Shapes will be renamed according to the `obs_names`. For this to be valid, please make sure that the order of elements in `.shapes` and `.data` matches.
2026-07-11 22:01:40 | [INFO] Cleared modality 'images'.
2026-07-11 22:01:40 | [INFO] No case-insensitive conflicts found.
SpatialData object
├── Shapes
│     └── 'UNITS.visium.shapes': GeoDataFrame shape: (4992, 2) (2D shapes)
└── Tables
      └── 'UNITS.visium.table': AnnData (4992, 18085)
with coordinate systems:
    ▸ 'visium', with elements:
        UNITS.visium.shapes (Shapes)
    ▸ 'visium_downscaled_hires', with elements:
        UNITS.visium.shapes (Shapes)
    ▸ 'visium_downscaled_lowres', with elements:
        UNITS.visium.shapes (Shapes)

3.4 Skipping transcripts and reading the dialect descriptor#

Transcript export (PointsModel.parse(..., sort=True)) is the most expensive step for large experiments. Pass include_transcripts=False to skip it entirely - useful when you only need cells, images, and/or shapes, or when transcript export is too slow for your use case.

Every export also carries a versioned dialect descriptor at sdata.attrs["insitupy_spatialdata_dialect"], letting a reader detect “this store is InSituPy dialect, version N” without parsing element-name strings.

from insitupy.datasets import xenium_test_dataset_v3_mm

xd_small = xenium_test_dataset_v3_mm()

sdata_with_tx = convert_to_spatialdata(xd_small)
sdata_without_tx = convert_to_spatialdata(xd_small, include_transcripts=False)

print(f"With transcripts (default):  {len(sdata_with_tx.points)} points element(s)")
print(f"include_transcripts=False:   {len(sdata_without_tx.points)} points element(s)")
print()
print("Dialect descriptor:", sdata_with_tx.attrs["insitupy_spatialdata_dialect"])
2026-07-11 22:04:52 | [INFO] This dataset exists already. Download is skipped. To force download set `overwrite=True`.
2026-07-11 22:04:52 | [INFO] Reading Xenium data with InSituPy backend...
2026-07-11 22:04:52 | [INFO] Loading cells...
2026-07-11 22:04:52 | [INFO] Loading images...
2026-07-11 22:04:52 | [WARNING] 'morphology_focus_0000.ome.tif' is part of a multi-file OME-TIFF. Axes are inferred from this file only and only data from this file will be returned.
2026-07-11 22:04:52 | [INFO] Loading transcripts...
2026-07-11 22:04:52 | [INFO] For this dataset no additional images are available.
2026-07-11 22:04:52 | [INFO] No case-insensitive conflicts found.
2026-07-11 22:04:52 | [INFO] No case-insensitive conflicts found.
With transcripts (default):  1 points element(s)
include_transcripts=False:   0 points element(s)

Dialect descriptor: {'version': 1, 'modalities': ['cells', 'units', 'images', 'transcripts', 'annotations', 'regions'], 'sample_prefix_pattern': 'SAMPLE.<uid>..'}

Saving and Loading SpatialData#

SpatialData objects can be saved to disk in Zarr format for efficient storage and lazy loading.

# Define output paths
outpath = CACHE / "test_spatialdata.zarr"
exp_outpath = CACHE / "exp_spatialdata.zarr"
# Save single sample SpatialData
sdata.write(outpath, overwrite=True)
print(f"Saved to: {outpath}")
Saved to: C:\Users\ge37voy\.cache\InSituPy\test_spatialdata.zarr
# Save experiment SpatialData
sdexp.write(exp_outpath, overwrite=True)
print(f"Saved to: {exp_outpath}")
Saved to: C:\Users\ge37voy\.cache\InSituPy\exp_spatialdata.zarr

Load from Disk#

from spatialdata import SpatialData

# Load saved SpatialData
sdata_loaded = SpatialData.read(outpath)
sdata_loaded
SpatialData object, with associated Zarr store: C:\Users\ge37voy\.cache\InSituPy\test_spatialdata.zarr
├── Images
│     ├── 'IMAGES.CD20': DataTree[cyx] (1, 25778, 35416), (1, 12889, 17708), (1, 6444, 8854), (1, 3222, 4427), (1, 1611, 2213), (1, 805, 1106)
│     ├── 'IMAGES.HE': DataTree[cyx] (3, 25778, 35416), (3, 12889, 17708), (3, 6444, 8854), (3, 3222, 4427), (3, 1611, 2213), (3, 805, 1106)
│     ├── 'IMAGES.HER2': DataTree[cyx] (1, 25778, 35416), (1, 12889, 17708), (1, 6444, 8854), (1, 3222, 4427), (1, 1611, 2213), (1, 805, 1106)
│     └── 'IMAGES.nuclei': DataTree[cyx] (1, 25778, 35416), (1, 12889, 17708), (1, 6444, 8854), (1, 3222, 4427), (1, 1611, 2213), (1, 805, 1106)
├── Labels
│     ├── 'CELLS.main.boundaries.cells': DataTree[yx] (25778, 35416), (12889, 17708), (6444, 8854), (3222, 4427), (1611, 2213), (805, 1106)
│     └── 'CELLS.main.boundaries.nuclei': DataTree[yx] (25778, 35416), (12889, 17708), (6444, 8854), (3222, 4427), (1611, 2213), (805, 1106)
├── Points
│     └── 'TRANSCRIPTS': DataFrame with shape: (42638083, 8) (3D points)
├── Shapes
│     ├── 'ANNOTATIONS.demo': GeoDataFrame shape: (4, 7) (2D shapes)
│     ├── 'ANNOTATIONS.demo2': GeoDataFrame shape: (5, 7) (2D shapes)
│     ├── 'ANNOTATIONS.demo3': GeoDataFrame shape: (7, 7) (2D shapes)
│     ├── 'CELLS.main.circles': GeoDataFrame shape: (157600, 2) (2D shapes)
│     ├── 'CELLS.main.circles_sized': GeoDataFrame shape: (157600, 2) (2D shapes)
│     ├── 'REGIONS.TMA': GeoDataFrame shape: (6, 8) (2D shapes)
│     └── 'REGIONS.demo_regions': GeoDataFrame shape: (3, 7) (2D shapes)
└── Tables
      └── 'CELLS.main.table': AnnData (157600, 297)
with coordinate systems:
    ▸ 'global', with elements:
        IMAGES.CD20 (Images), IMAGES.HE (Images), IMAGES.HER2 (Images), IMAGES.nuclei (Images), CELLS.main.boundaries.cells (Labels), CELLS.main.boundaries.nuclei (Labels), TRANSCRIPTS (Points), ANNOTATIONS.demo (Shapes), ANNOTATIONS.demo2 (Shapes), ANNOTATIONS.demo3 (Shapes), CELLS.main.circles (Shapes), CELLS.main.circles_sized (Shapes), REGIONS.TMA (Shapes), REGIONS.demo_regions (Shapes)