Interactivity

Spatialproteomics has a variety of functions to ensure interoperability with existing tools. For example, we can export the spatialproteomics object into spatialdata, and the use napari for interactive visualiztations.

[5]:
%reload_ext autoreload
%autoreload 2

import spatialproteomics as sp
import xarray as xr
from napari_spatialdata import Interactive
import spatialdata
from spatialdata import SpatialData
import numpy as np
[2]:
# loading in a data set and performing some formatting for convenience
ds = xr.load_dataset('../../data/BNHL_166_4_I2_LK.zarr', engine='zarr')
ds["_properties"] = ds["_labels"]
ds = ds.pp.drop_layers("_labels")
ds
/opt/conda/lib/python3.10/site-packages/xarray/backends/plugins.py:159: RuntimeWarning: 'scipy' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
[2]:
<xarray.Dataset>
Dimensions:        (cells: 12560, channels: 56, y: 3000, x: 3000, features: 4,
                    labels: 8, props: 2)
Coordinates:
  * cells          (cells) int64 1 2 3 4 5 6 ... 12556 12557 12558 12559 12560
  * channels       (channels) <U11 'DAPI' 'Helios' 'CD10' ... 'CD79a' 'Ki-67'
  * features       (features) <U10 'centroid-0' 'centroid-1' ... '_original_'
  * labels         (labels) int64 1 2 3 4 5 6 7 8
  * props          (props) <U6 '_color' '_name'
  * x              (x) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999
  * y              (y) int64 0 1 2 3 4 5 6 ... 2994 2995 2996 2997 2998 2999
Data variables:
    _arcsinh_mean  (cells, channels) float64 3.111 0.0 1.391 ... 1.324 0.4174
    _arcsinh_sum   (cells, channels) float64 8.346 0.0 6.564 ... 6.625 5.224
    _image         (channels, y, x) uint8 4 4 4 4 5 4 4 3 4 ... 2 2 2 2 2 2 2 2
    _obs           (cells, features) float64 613.3 768.4 4.0 ... 8.0 7.0
    _raw_mean      (cells, channels) float64 56.02 0.0 9.426 ... 8.727 2.148
    _raw_sum       (cells, channels) float64 1.053e+04 0.0 ... 1.885e+03 464.0
    _segmentation  (y, x) int64 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0
    _properties    (labels, props) object 'C3' ... 'B (PAX5)'
[3]:
# turning the spatialproteomics object into a spatialdata object
sd_obj = ds.tl.convert_to_spatialdata(expression_matrix_key='_arcsinh_mean')
sd_obj
INFO     Transposing `data` of type: <class 'dask.array.core.Array'> to ('c', 'y', 'x').
INFO     Transposing `data` of type: <class 'dask.array.core.Array'> to ('y', 'x').
[3]:
SpatialData object
├── Images
│     └── 'image': DataArray[cyx] (56, 3000, 3000)
├── Labels
│     └── 'segmentation': DataArray[yx] (3000, 3000)
└── Tables
      └── 'table': AnnData (12560, 56)
with coordinate systems:
    ▸ 'global', with elements:
        image (Images), segmentation (Labels)
[6]:
interactive = Interactive(sd_obj)
interactive.run()