harpy.tb.allocate_intensity#
- harpy.tb.allocate_intensity(sdata, image_name=None, labels_name=None, output_table_name='table_intensities', channels=None, mode='mean', obs_stats=None, to_coordinate_system='global', chunks=None, append=False, calculate_center_of_mass=True, region_key='fov_labels', instance_key='cell_ID', spatial_key='spatial', instance_size_key='shapeSize', cell_index_name='cells', run_on_gpu=False, overwrite=True)#
Allocates intensity values from a specified image element to corresponding cells in a SpatialData object and returns an updated SpatialData object augmented with a table element (
sdata.tables[output_table_name])AnnDataobject with intensity values for each cell and each (specified) channel.It requires that the image element and the labels element have the same shape and alignment.
Internally this function uses
harpy.utils.RasterAggregator().- Parameters:
sdata (
SpatialData) – The SpatialData object containing spatial information about cells.image_name (
str|None(default:None)) – The name of the image element insdatathat contains the image data from which to extract intensity information. Both theimage_nameandlabels_nameshould have the same shape and alignment. If not provided, will use last image_name.labels_name (
str|None(default:None)) – The name of the labels element insdatacontaining the labels (segmentation) used to define the boundaries of cells. These labels correspond with regions in theimage_name. If not provided, will use last labels_name.output_table_name (str, optional) – The table element in
sdatain which to save theAnnDataobject with the intensity values per cell.channels (
int|str|Iterable[int] |Iterable[str] |None(default:None)) – Specifies the channels to be considered when extracting intensity information from theimage_name. This parameter can take a single integer or string or an iterable of integers or strings representing specific channels. If set to None (the default), intensity data will be aggregated from all available channels within the image element.mode (
Literal['sum','mean'] (default:'mean')) – When mode is set to"sum", the total intensity for each label will be added to.Xof the resultingoutput_table_name; if set to"mean", it calculates the average intensity per label.obs_stats (
list[str] |None(default:None)) –Statistics to add to
.obsofoutput_table_name. Supported values:["sum", "mean", "count", "var", "kurtosis", "skew", "max", "min"].If
obs_statscontains"mode", it will not be added to.obs.For each
statin["sum", "mean", "var", "kurtosis", "skew", "max", "min"], the result is stored as:{channel_name}_{stat}."count"is stored in.obsusing the name given byinstance_size_key.
to_coordinate_system (
str(default:'global')) – The coordinate system that holdsimage_nameandlabels_name. This should be the intrinsic coordinate system in pixels.chunks (
str|int|tuple[int,...] |None(default:None)) – The chunk size for processing the image data. If provided as a tuple, desired chunksize for (z), y, x should be provided.append (
bool(default:False)) – If set to True, and thelabels_namedoes not yet exist as aregion_keyinsdata.tables[output_table_name].obs, the intensity values extracted during the current function call will be appended (along axis=0) to any existing intensity data within the SpatialData object’s table attribute. If False, and overwrite is set to True any existing data insdata.tables[output_table_name]will be overwritten by the newly extracted intensity values. Note that we join theAnnDataobjects usingconcat()withjoin="inner".calculate_center_of_mass (
bool(default:True)) – IfTrue, the center of mass of the labels inlabels_namewill be calculated and added tosdata.tables[ output_table_name ].obsm[spatial_key]. The center of mass is computed usingscipy.ndimage.center_of_mass. Enablingcalculate_center_of_masswill cause thelabels_nameto be loaded into memory.instance_key (
str(default:'cell_ID')) – Instance key. The name of the column inAnnDatatable.obsthat will hold the instance ids.region_key (
str(default:'fov_labels')) – Region key. The name of the column inAnnDatatable.obsthat will hold the name of the element(s) that are annotated by the resulting table.spatial_key (
str(default:'spatial')) – The key in theAnnDatatable.obsmthat will hold thexandycenter of the instances. This center is calculated by calculating the center of mass of each cell inlabels_name.instance_size_key (
str(default:'shapeSize')) – The key in theAnnDatatable.obsthat will hold the size of the instances. Ignored if “count” not inobs_stats.cell_index_name (
str(default:'cells')) – The name of the index of the resultingAnnDatatable.run_on_gpu (
bool(default:False)) – Whether to run on gpu. If no installation of cupy could be detected, will fall back to cpu.overwrite (
bool(default:True)) – IfTrue, overwrites theoutput_table_nameif it already exists insdata.
- Return type:
- Returns:
: An updated version of the input SpatialData object augmented with a table element (
sdata.tables[output_table_name])AnnDataobject.
Notes
The function currently supports scenarios where the
image_nameandlabels_nameare aligned and have the same shape. Misalignments or differences in shape must be handled prior to invoking this function.Intensity calculation is performed per channel for each cell. The function aggregates this information and attaches it as a table (
AnnDataobject) within the SpatialData object.Due to the memory-intensive nature of the operation, especially for large datasets, the function implements chunk-based processing, aided by Dask. If sdata is backed by a Zarr store, we recommend using
chunks=Noneand ensuring that the on-disk Dask array chunks are optimized for both storage efficiency and computational performance.
Examples
Allocate intensity statistics into an AnnData table:
import harpy as hp sdata = hp.datasets.pixie_example() # Compute intensity statistics in coordinate system "fov0" sdata = hp.tb.allocate_intensity( sdata, image_name="raw_image_fov0", labels_name="label_whole_fov0", to_coordinate_system="fov0", output_table_name="my_table", mode="sum", obs_stats="count", # cell size overwrite=True, ) # Append intensity statistics in coordinate system "fov1" sdata = hp.tb.allocate_intensity( sdata, image_name="raw_image_fov1", labels_name="label_whole_fov1", to_coordinate_system="fov1", output_table_name="my_table", mode="sum", obs_stats="count", # cell size append=True, overwrite=True, )
See also
harpy.utils.RasterAggregatorout of core calculation of statistics from raster data.