Subselecting Data

You can select parts of your data based on channels, spatial coordinates, cell type labels, and neighborhoods.

If you want to follow along with this tutorial, you can download the data here.

[1]:
%reload_ext autoreload
%autoreload 2

import spatialproteomics
import pandas as pd
import xarray as xr

One of the key features of spatialproteomics is the ability to slice our image data quickly and intuitively. We start by loading our spatialproteomics object.

[2]:
ds = xr.open_zarr("../../data/LN_24_1.zarr")

Slicing Channels and Spatial Coordinates

To slice specific channels of the image we simply use .pp accessor together with the familiar bracket [] indexing.

[4]:
_ = ds.pp["CD4"].pl.show()
../_images/notebooks_Slicing_5_0.png

We can also select multiple channels by simply passing a list to the .pp accessor. As we will see later, this makes visualising image overlays easy.

[5]:
_ = ds.pp[["CD4", "CD8"]].pl.show()
../_images/notebooks_Slicing_7_0.png

The .pp accessor also understands x and y coordinates. When x and y coordinates are sliced, we get rid of all cells that do not belong to the respective image slice.

[9]:
_ = ds.pp[500:1500, 500:1500].pp["PAX5"].pl.show()
../_images/notebooks_Slicing_9_0.png

Note that we can also pass channels and x, y coordinates at the same time.

[12]:
_ = ds.pp[["CD4", "CD8"], 500:1500, 500:1500].pl.show()
../_images/notebooks_Slicing_11_0.png

Slicing Labels

The labels accessor .la allows to select specific cell types by their label number or name.

[15]:
_ = ds.la[1].pl.show(render_image=False, render_labels=True)
../_images/notebooks_Slicing_13_0.png
[14]:
_ = ds.la["B cell"].pl.show(render_image=False, render_labels=True)
../_images/notebooks_Slicing_14_0.png

Again it is possible to pass multiple cell labels.

[17]:
_ = ds.la[4, 5, 6].pl.show(render_image=False, render_labels=True)
../_images/notebooks_Slicing_16_0.png

Finally, we can select all cells except a cell type using la.deselect.

[18]:
_ = ds.la.deselect(["B cell"]).pl.show(render_image=False, render_labels=True)
../_images/notebooks_Slicing_18_0.png

Slicing Neighborhoods

We can also select by neighborhoods with the nh accessor. The syntax is identical to the one in the label subsetting.

[20]:
# subsetting only neighborhood 0
_ = ds.nh[1].pl.render_neighborhoods().pl.imshow()
../_images/notebooks_Slicing_20_0.png