Introduction to the InSituExperiment class#

Background#

../../_images/experiment_levels.jpg

In addition to the sample, cellular, and subcellular levels, a spatial transcriptomics dataset typically includes an experiment level that contains information about clinical and experimental cohorts, conditions, or treatments. Most current computational frameworks lack functionalities to incorporate this level into the analysis. To address this, InSituPy introduces the InSituExperiment class, which manages spatial transcriptomics data and the corresponding metadata of multiple samples simultaneously.

Structure#

../../_images/insituexperiment_structure.jpg

An InSituExperiment object consists of multiple InSituData objects paired with their corresponding metadata.

from pathlib import Path
from insitupy import InSituData, InSituExperiment, CACHE
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

Load Xenium data into InSituData object#

Now the Xenium data can be parsed by providing the data path to the InSituPy project folder.

insitupy_project = Path(CACHE / "out/demo_insitupy_project")
xd = InSituData.read(insitupy_project)
xd.load_all(skip="transcripts")
xd
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(25778, 35416)
       CD20:	(25778, 35416)
       HER2:	(25778, 35416)
       HE:	(25778, 35416, 3)
    ➤ cells
       matrix
           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', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotations
       TestKey:	8 annotations, 2 classes ('TestClass','points') ✔
       demo:	4 annotations, 2 classes ('Positive','Negative') ✔
       demo2:	5 annotations, 3 classes ('Negative','Positive','Other') ✔
       demo3:	7 annotations, 5 classes ('Stroma','Necrosis','Immune cells','unclassified','Tumor') ✔
       Demo:	28 annotations, 2 classes ('Tumor cells','Stroma') ✔
    ➤ regions
       test:	7 regions, 5 classes ('Stroma','Necrosis','Immune cells','unclassified','Tumor') ✔
       demo_regions:	3 regions, 3 classes ('Region1','Region2','Region3') ✔
       TMA:	6 regions, 6 classes ('B-2','A-3','B-1','B-3','A-1','A-2') ✔
       Demo:	3 regions, 3 classes ('Region 1','Region 3','Region 2') ✔

Create InSituExperiment#

../../_images/insituexperiment_generation.jpg

Options to generate an InSituExperiment object

Method 1: Manually add InSituData objects#

exp = InSituExperiment()
exp.add(
    data=xd,
    metadata={
        "slide_id": xd.slide_id,
        "sample_id": xd.sample_id,
        "patient": "A"
    }
    )
exp
InSituExperiment with 1 samples:
           uid slide_id    sample_id patient
0     55e87ed3  0001879  Replicate 1       A

In the same way also other datasets can be added. For demonstration purposes, we just add the same dataset again and change the metadata.

exp.add(
    data=xd,
    metadata={
        "slide_id": xd.slide_id,
        "sample_id": xd.sample_id,
        "patient": "A",
        "therapy": "drugB"
    }
    )
exp
InSituExperiment with 2 samples:
           uid slide_id    sample_id patient therapy
0     55e87ed3  0001879  Replicate 1       A     NaN
1     3b2f76f0  0001879  Replicate 1       A   drugB

Method 2: From config file#

As config file either a csv file or an excel file can be used.

Example of a valid configuration file:

directory

experiment_name

date

patient

/path/to/dataset1

Experiment 1

2023-09-01

Patient A

/path/to/dataset2

Experiment 2

2023-09-02

Patient B

import pandas as pd
from insitupy import CACHE

# Define the data
data = {
    "directory": [f"{CACHE}\\out\\demo_insitupy_project", f"{CACHE}\\out\\demo_insitupy_project"],
    "patient": ["A", "B"],
    "therapy": ["drugA", "drugB"]
}

# Create a DataFrame
config_df = pd.DataFrame(data)

# Write the DataFrame to a CSV file
config_df.to_csv('./demo_experiment/insituexperiment_config.csv', index=False)
exp = InSituExperiment.from_config(config_path="./demo_experiment/insituexperiment_config.csv")
exp
InSituExperiment with 2 samples:
           uid slide_id    sample_id patient therapy
0     96c8b4bc  0001879  Replicate 1       A   drugA
1     b7cc6c13  0001879  Replicate 1       B   drugB

Method 3: From regions#

We can also use regions from an InSituData object to split the data into separate datasets and create an InSituExperiment from them. This can be used to select the most interesting regions and focus on them for the analysis or to split a TMA dataset into separate datasets for each core.

exp = InSituExperiment.from_regions(
    data=xd, region_key="TMA"
)
exp
InSituExperiment with 6 samples:
           uid slide_id    sample_id region_key region_name
0     ad7fb04e  0001879  Replicate 1        TMA         A-1
1     3e5745db  0001879  Replicate 1        TMA         A-2
2     19139e8c  0001879  Replicate 1        TMA         A-3
3     ef587926  0001879  Replicate 1        TMA         B-1
4     f6228eda  0001879  Replicate 1        TMA         B-2
5     a7b6d5ba  0001879  Replicate 1        TMA         B-3
# visualize data
viewer = exp.show(3)

Example screenshot of data from one of the cropped cores:

../../_images/TMA_example.jpg

Plot overview of metadata and QC metrics#

exp.plot_overview()
../../_images/7ea43d70c457d47bbbffc0f7f8ff2d35d3bb7e918686bb65c2091c74d03f9b35.png
exp.plot_overview(["uid", "slide_id", "sample_id", "region_name", "region_key"], index=True)
../../_images/fe2503e1e16f60f51ff29ed98352d8b1e911f4fb7ba13ae9e39cd7cf26d3a3e2.png

Iterate through InSituExperiment using .iterdata() method#

for metadata, data in exp.iterdata():
    print(f"Metadata:\n{metadata[:3]}\nData:\n{data}")
Metadata:
uid             ad7fb04e
slide_id         0001879
sample_id    Replicate 1
Name: 0, dtype: object
Data:
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(4706, 4706)
       CD20:	(4706, 4706)
       HER2:	(4706, 4706)
       HE:	(4706, 4706, 3)
    ➤ cells
       matrix
           AnnData object with n_obs × n_vars = 4878 × 297
           obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotations
       demo:	4 annotations, 2 classes ('Stroma','Tumor cells') 
       Demo:	4 annotations, 2 classes ('Stroma','Tumor cells') 
    ➤ regions
       TMA:	1 regions, 1 class ('A-1') 
       Demo:	1 regions, 1 class ('Region 1') 
Metadata:
uid             3e5745db
slide_id         0001879
sample_id    Replicate 1
Name: 1, dtype: object
Data:
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(4706, 4705)
       CD20:	(4706, 4705)
       HER2:	(4706, 4705)
       HE:	(4706, 4705, 3)
    ➤ cells
       matrix
           AnnData object with n_obs × n_vars = 1795 × 297
           obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotationsregions
       TMA:	1 regions, 1 class ('A-2') 
Metadata:
uid             19139e8c
slide_id         0001879
sample_id    Replicate 1
Name: 2, dtype: object
Data:
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(4706, 4705)
       CD20:	(4706, 4705)
       HER2:	(4706, 4705)
       HE:	(4706, 4705, 3)
    ➤ cells
       matrix
           AnnData object with n_obs × n_vars = 1822 × 297
           obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotationsregions
       TMA:	1 regions, 1 class ('A-3') 
Metadata:
uid             ef587926
slide_id         0001879
sample_id    Replicate 1
Name: 3, dtype: object
Data:
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(4706, 4706)
       CD20:	(4706, 4706)
       HER2:	(4706, 4706)
       HE:	(4706, 4706, 3)
    ➤ cells
       matrix
           AnnData object with n_obs × n_vars = 3597 × 297
           obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotations
       demo2:	1 annotations, 1 class ('Other') 
    ➤ regions
       TMA:	1 regions, 1 class ('B-1') 
Metadata:
uid             f6228eda
slide_id         0001879
sample_id    Replicate 1
Name: 4, dtype: object
Data:
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(4706, 4706)
       CD20:	(4706, 4706)
       HER2:	(4706, 4706)
       HE:	(4706, 4706, 3)
    ➤ cells
       matrix
           AnnData object with n_obs × n_vars = 2317 × 297
           obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotations
       TestKey:	1 annotations, 1 class ('TestClass') 
       demo:	1 annotations, 1 class ('Stroma') 
       demo2:	1 annotations, 1 class ('Other') 
       Demo:	1 annotations, 1 class ('Stroma') 
    ➤ regions
       demo_regions:	1 regions, 1 class ('Region3') 
       TMA:	1 regions, 1 class ('B-2') 
       Demo:	1 regions, 1 class ('Region 2') 
Metadata:
uid             a7b6d5ba
slide_id         0001879
sample_id    Replicate 1
Name: 5, dtype: object
Data:
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(4706, 4706)
       CD20:	(4706, 4706)
       HER2:	(4706, 4706)
       HE:	(4706, 4706, 3)
    ➤ cells
       matrix
           AnnData object with n_obs × n_vars = 3411 × 297
           obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_tacco', 'cell_type_dc_sub'
           var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
           uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
           obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
           varm: 'OT', 'PCs'
           layers: 'counts', 'norm_counts'
           obsp: 'connectivities', 'distances'
       boundaries
           BoundariesData object with 2 entries:
               cells
               nucleiannotations
       demo:	3 annotations, 2 classes ('Stroma','Tumor cells') 
       demo2:	1 annotations, 1 class ('Negative') 
       Demo:	3 annotations, 2 classes ('Stroma','Tumor cells') 
    ➤ regions
       TMA:	1 regions, 1 class ('B-3') 
       Demo:	1 regions, 1 class ('Region 3')

Add new metadata#

In the following section different scenarios for adding new metadata are shown.

exp1 = InSituExperiment.from_regions(
    data=xd, region_key="demo_regions"
)
exp1
InSituExperiment with 3 samples:
           uid slide_id    sample_id    region_key region_name
0     f677c095  0001879  Replicate 1  demo_regions     Region1
1     5f43a73a  0001879  Replicate 1  demo_regions     Region2
2     eb39795e  0001879  Replicate 1  demo_regions     Region3
exp2 = exp1.copy()
exp2.append_metadata(
    new_metadata="./demo_experiment/insituexperiment_new_metadata.csv",
    by="region_name", overwrite=True
)
exp2
InSituExperiment with 3 samples:
           uid slide_id    sample_id region_name therapy   organ      test region_key
0     f677c095  0001879  Replicate 1     Region1   drugC   liver  negative       key1
1     5f43a73a  0001879  Replicate 1     Region2   drugD    lung      test       key3
2     eb39795e  0001879  Replicate 1     Region3   drugE  spleen  positive       key2
exp3 = exp1.copy()
exp3.append_metadata(
    new_metadata="./demo_experiment/insituexperiment_new_metadata.csv",
    by="region_name", overwrite=False
)
exp3
InSituExperiment with 3 samples:
           uid slide_id    sample_id    region_key region_name   organ      test therapy
0     f677c095  0001879  Replicate 1  demo_regions     Region1   liver  negative   drugC
1     5f43a73a  0001879  Replicate 1  demo_regions     Region2    lung      test   drugD
2     eb39795e  0001879  Replicate 1  demo_regions     Region3  spleen  positive   drugE
exp4 = exp1.copy()
exp4.append_metadata(
    new_metadata="./demo_experiment/insituexperiment_new_metadata2.csv",
    by="region_name", overwrite=False
)
exp4
InSituExperiment with 3 samples:
           uid slide_id    sample_id    region_key region_name   organ      test therapy
0     f677c095  0001879  Replicate 1  demo_regions     Region1   liver  negative   drugC
1     5f43a73a  0001879  Replicate 1  demo_regions     Region2     NaN       NaN     NaN
2     eb39795e  0001879  Replicate 1  demo_regions     Region3  spleen  positive   drugE
exp5 = exp1.copy()
exp5.append_metadata(
    new_metadata="./demo_experiment/insituexperiment_new_metadata2.csv",
    by="region_name", overwrite=True
)
exp5
InSituExperiment with 3 samples:
           uid slide_id    sample_id region_name therapy   organ      test region_key
0     f677c095  0001879  Replicate 1     Region1   drugC   liver  negative       key1
1     5f43a73a  0001879  Replicate 1     Region2     NaN     NaN       NaN        NaN
2     eb39795e  0001879  Replicate 1     Region3   drugE  spleen  positive       key2

Concatenate multiple InSituExperiment objects

exp
InSituExperiment with 6 samples:
           uid slide_id    sample_id region_key region_name
0     ad7fb04e  0001879  Replicate 1        TMA         A-1
1     3e5745db  0001879  Replicate 1        TMA         A-2
2     19139e8c  0001879  Replicate 1        TMA         A-3
3     ef587926  0001879  Replicate 1        TMA         B-1
4     f6228eda  0001879  Replicate 1        TMA         B-2
5     a7b6d5ba  0001879  Replicate 1        TMA         B-3
exp1
InSituExperiment with 3 samples:
           uid slide_id    sample_id    region_key region_name
0     f677c095  0001879  Replicate 1  demo_regions     Region1
1     5f43a73a  0001879  Replicate 1  demo_regions     Region2
2     eb39795e  0001879  Replicate 1  demo_regions     Region3
exp_concat = InSituExperiment.concat(
    objs={
        "exp_TMA": exp,
        "exp_demo_regions": exp1
    },
    new_col_name="name"
    )
exp_concat
InSituExperiment with 9 samples:
           uid slide_id    sample_id    region_key region_name            name
0     ad7fb04e  0001879  Replicate 1           TMA         A-1         exp_TMA
1     3e5745db  0001879  Replicate 1           TMA         A-2         exp_TMA
2     19139e8c  0001879  Replicate 1           TMA         A-3         exp_TMA
3     ef587926  0001879  Replicate 1           TMA         B-1         exp_TMA
4     f6228eda  0001879  Replicate 1           TMA         B-2         exp_TMA
5     a7b6d5ba  0001879  Replicate 1           TMA         B-3         exp_TMA
6     f677c095  0001879  Replicate 1  demo_regions     Region1  exp_demo_re...
7     5f43a73a  0001879  Replicate 1  demo_regions     Region2  exp_demo_re...
8     eb39795e  0001879  Replicate 1  demo_regions     Region3  exp_demo_re...
exp_concat = InSituExperiment.concat(
    objs=[exp, exp1])
exp_concat
InSituExperiment with 9 samples:
           uid slide_id    sample_id    region_key region_name
0     ad7fb04e  0001879  Replicate 1           TMA         A-1
1     3e5745db  0001879  Replicate 1           TMA         A-2
2     19139e8c  0001879  Replicate 1           TMA         A-3
3     ef587926  0001879  Replicate 1           TMA         B-1
4     f6228eda  0001879  Replicate 1           TMA         B-2
5     a7b6d5ba  0001879  Replicate 1           TMA         B-3
6     f677c095  0001879  Replicate 1  demo_regions     Region1
7     5f43a73a  0001879  Replicate 1  demo_regions     Region2
8     eb39795e  0001879  Replicate 1  demo_regions     Region3

Indexing and selection of data within InSituExperiment#

The InSituExperiment class allows simple selection and filtering of data using indices or True/False masks.

Example 1: Select datasets by index#

exp_indexed = exp_concat[:3]
exp_indexed
InSituExperiment with 3 samples:
           uid slide_id    sample_id region_key region_name
0     ad7fb04e  0001879  Replicate 1        TMA         A-1
1     3e5745db  0001879  Replicate 1        TMA         A-2
2     19139e8c  0001879  Replicate 1        TMA         A-3
exp_indexed.show(1)
Viewer(camera=Camera(center=(0.0, 499.90625, 499.8), zoom=0.09501781584047009, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(1.0, 1.0), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=2, ndisplay=2, last_used=0, range=((0.0, 1000.025, 0.2125), (0.0, 999.8125, 0.2125)), current_step=(2352, 2352), order=(0, 1), axis_labels=('0', '1')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'nuclei' at 0x2338e8957f0>, <Image layer 'CD20' at 0x2338162fe20>, <Image layer 'HER2' at 0x2338176dd60>, <Image layer 'HE' at 0x2338e68eaf0>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='0001879: Replicate 1', mouse_over_canvas=False, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x0000023318F95670>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={})

Example 2: Select only datasets with a “1” in the region name#

filtering_mask = exp_concat.metadata["region_name"].str.contains("1")
exp_filtered = exp_concat[filtering_mask]

Save InSituExperiment#

exp_filtered.saveas(CACHE / "out/test_insituexperiment", overwrite=True)

Reload InSituExperiment#

exp_reloaded = InSituExperiment.read(CACHE / "out/test_insituexperiment/")
exp_reloaded.load_all()
exp_reloaded
InSituExperiment with 3 samples:
           uid  slide_id    sample_id    region_key region_name
0     ad7fb04e      1879  Replicate 1           TMA         A-1
1     ef587926      1879  Replicate 1           TMA         B-1
2     f677c095      1879  Replicate 1  demo_regions     Region1
exp_reloaded.show(2)
Viewer(camera=Camera(center=(0.0, 286.875, 351.475), zoom=0.13510390556948074, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(1.0, 1.0), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=2, ndisplay=2, last_used=0, range=((0.0, 573.9625, 0.2125), (0.0, 703.1625, 0.2125)), current_step=(1350, 1654), order=(0, 1), axis_labels=('0', '1')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'nuclei' at 0x233b87bfc70>, <Image layer 'CD20' at 0x233b8c17790>, <Image layer 'HER2' at 0x233b8f27e50>, <Image layer 'HE' at 0x233bd4f48b0>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='0001879: Replicate 1', mouse_over_canvas=False, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x0000023318F95670>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={})