athena.plotting.visualization module

Summary

Functions:

get_cmap

Return the cmap and cmap labels for a given attribute if available, else a default

infiltration

Visualises a heatmap of the featuer intensity.

interactions

Visualise results from interactions() results.

napari_viewer

Starts interactive Napari viewer to visualise raw images and explore samples.

ripleysK

Visualise results from ripleysK() results.

spatial

Various functionalities to visualise samples.

Reference

spatial(so, spl, attr, *, mode='scatter', node_size=4, coordinate_keys=['x', 'y'], mask_key='cellmasks', graph_key='knn', edges=False, edge_width=0.5, edge_color='black', edge_zorder=2, background_color='white', ax=None, norm=None, set_title=True, cmap=None, cmap_labels=None, cbar=True, cbar_title=True, show=True, save=None, tight_layout=True)[source]

Various functionalities to visualise samples. Allows to visualise the samples and color observations according to features in either so.X or so.obs by setting the attr parameter accordingly. Furthermore, observations (cells) within a sample can be quickly visualised using scatter plots (requires extraction of centroids with extract_centroids()) or by their actual segmentatio mask by setting mode accordingly. Finally, the graph representation of the sample can be overlayed by setting edges=True and specifing the graph_key as in so.G[spl][graph_key].

For more examples on how to use this function have a look at the tutorial section.

Parameters
  • so – SpatialOmics instance

  • spl (str) – sample to visualise

  • attr (str) – feature to visualise

  • mode (str) – {scatter, mask}. In scatter mode, observations are represented by their centroid, in mask mode by their actual segmentation mask

  • node_size (float) – size of the node when plotting the graph representation

  • coordinate_keys (list) – column names in SpatialOmics.obs[spl] that indicates the x and y coordinates

  • mask_key (str) – key for the segmentation masks when in mask mode

  • graph_key (str) – which graph representation to use

  • edges (bool) – whether to plot the graph or not

  • edge_width (float) – width of edges

  • edge_color (str) – color of edges as string

  • edge_zorder (int) – z-order of edges

  • background_color (str) – background color of plot

  • ax (Optional[Axes]) – axes object in which to plot

  • norm – normalisation instance to normalise the values of attr

  • set_title (bool) – title of plot

  • cmap – colormap to use

  • cmap_labels (Optional[list]) – colormap labels to use

  • cbar (bool) – whether to plot a colorbar or not

  • cbar_title (bool) – whether to plot the attr name as title of the colorbar

  • show (bool) – whether to show the plot or not

  • save (Optional[str]) – path to the file in which the plot is saved

  • tight_layout (bool) – whether to apply tight_layout or not.

Examples

so = sh.dataset.imc()
sh.pl.spatial(so, 'slide_7_Cy2x2', 'meta_id', mode='mask')
sh.pl.spatial(so, 'slide_7_Cy2x2', 'meta_id', mode='scatter', edges=True)
napari_viewer(so, spl, attrs, censor=0.95, add_masks='cellmasks', attrs_key='target', index_key='fullstack_index')[source]

Starts interactive Napari viewer to visualise raw images and explore samples. attrs are measured features in the high dimensional images in so.images[spl]. All specified attrs should be in so.var[spl][attrs_key] along with the index in the high dimensional images. The column with the index in the high dimensional image where the measurement of an attribute is stored.

Parameters
  • so – SpatialOmics instance

  • spl (str) – sample to visualise

  • attrs (list) – list of attributes/features to add as channels to the viewer

  • censor (float) – percentil to use to censore pixle values in the raw images

  • add_masks – segmentation masks to add as channels to the viewer

  • attrs_key – key in so.var[spl] that defines the attrs names

  • index_key (str) – key in so.var[spl] that specifies the layer index in the high dimensional image in so.images[spl].

Examples:

so = sh.dataset.imc()
spl = so.spl.index[0]

# add all measured features to the napari viewer
sh.pl.napari_viewer(so, spl, attrs=so.var[spl]['target'], add_masks=so.masks[spl].keys())

# specify the column containing the ``attrs`` names
sh.pl.napari_viewer(so, spl, attrs=so.var[spl]['target'], attrs_key='target')

# specify the column containing the ``attrs`` names and the columns that specifies the layer index
sh.pl.napari_viewer(so, spl, attrs=so.var[spl]['target'], attrs_key='target', index_key='fullstack_index')
interactions(so, spl, attr, mode='proportion', prediction_type='diff', graph_key='knn', linewidths=0.5, cmap=None, norm=None, ax=None, show=True, cbar=True)[source]

Visualise results from interactions() results.

Parameters
  • so – SpatialOmics instance

  • spl – Spl for which to compute the metric

  • attr – Categorical feature in SpatialOmics.obs to use for the grouping

  • mode – One of {classic, histoCAT, proportion}, see notes

  • prediction_type – prediction_type: One of {observation, pvalue, diff}

  • graph_key – Specifies the graph representation to use in so.G[spl]

  • linewidths – Space between tiles

  • cmap – colormap to use

  • norm – normalisation to use

  • ax – axes object to use

  • show – whether to show the plot

  • cbar – wheter to show the colorbar

Examples

# compute Ripley's K
so = sh.dataset.imc()
spl = so.spl.index[0]

# build graph
sh.graph.build_graph(so, spl, builder_type='knn', mask_key='cellmasks')

# compute & plot interactions
sh.neigh.interactions(so, spl, 'meta_id', mode='proportion', prediction_type='observation')
sh.pl.interactions(so, spl, 'meta_id', mode='proportion', prediction_type='observation')
get_cmap(so, attr, data)[source]

Return the cmap and cmap labels for a given attribute if available, else a default

Parameters
  • so (IMCData) – so object form which to fetch the data

  • spl (str) – spl for which to get data

  • attr (str) – attribute for which to get the cmap and cmap labels if available

Returns

Return type

cmap and cmap labels for attribute

ripleysK(so, spl, attr, ids, *, mode='K', correction='ripley', key=None, ax=None, legend='auto')[source]

Visualise results from ripleysK() results.

Parameters
  • so – SpatialOmics instance

  • spl (str) – Spl for which to compute the metric

  • attr (str) – Categorical feature in SpatialOmics.obs to use for the grouping

  • ids – The category in the categorical feature attr, for which Ripley’s K should be plotted

  • mode – {K, csr-deviation}. If K, Ripley’s K is estimated, with csr-deviation the deviation from a poission process is computed.

  • correction – Correction method to use to correct for boarder effects, see [1].

  • key – key to use in so.uns[‘ripleysK’] for the plot, if None it is constructed from spl,attr,ids,mode and correction

  • ax – axes to use for the plot

Examples

# compute Ripley's K
so = sh.dataset.imc()
sh.neigh.ripleysK(so, so.spl.index[0], 'meta_id', 1, mode='csr-deviation', radii=radii)
sh.pl.ripleysK(so, so.spl.index[0], 'meta_id', [1], mode='csr-deviation')
infiltration(so, spl, attr='infiltration', step_size=10, interpolation='gaussian', cmap='plasma', collision_strategy='mean', ax=None, show=True)[source]

Visualises a heatmap of the featuer intensity.

Approximates the sample with a grid representation and colors each grid square according to the value of the attribute. If multiple observations map to the same grid square a the aggregation specified in collision_strategy is employed (any value accepted by pandas aggregate function, i.e. ‘mean’, ‘max’, …)

Parameters
  • so – SpatialOmics instance

  • spl (str) – Spl for which to compute the metric

  • attr (str) – feature in SpatialOmics.obs to plot

  • step_size (int) – grid step size

  • interpolation (str) – interpolation method to use between grid values, see [1]

  • cmap (str) – colormap to use

  • collision_strategy – aggragation strategy to use if multiple obseravtion values map to the same grid value

  • ax – axes to use for the plot

  • show – whether to show the plot or not. Will be set to False if axes is provided.

Examples

so = sh.dataset.imc()
spl = so.spl.index[0]

# build graph
sh.graph.build_graph(so, spl, builder_type='knn', mask_key='cellmasks')

sh.neigh.infiltration(so, spl, 'meta_id', graph_key='knn')
sh.pl.infiltration(so, spl, step_size=10)

Notes

1

https://matplotlib.org/stable/gallery/images_contours_and_fields/interpolation_methods.html