imctoolkit.ImageSingleCellData

class ImageSingleCellData(img, mask, channel_names=None, region_properties=None)

Single-cell data accessor for multi-channel images

Variables
  • img – intensity image, as xarray.DataArray with dimensions (c, y, x) and, optionally, channel names as coordinate for the c dimension

  • mask – cell mask, as numpy.ndarray of shape (y, x), where 0 indicates background pixels and non-zero pixels indicate the cell ID

  • region_properties – list of RegionProperties computed by the current instance, see regionprops()

class RegionProperties(value)

Enumeration of regionprops properties supported by this class, see https://scikit-image.org/docs/0.17.x/api/skimage.measure.html#skimage.measure.regionprops

AREA = 'area'
BBOX = 'bbox'
BBOX_AREA = 'bbox_area'
CONVEX_AREA = 'convex_area'
CONVEX_IMAGE = 'convex_image'
COORDS = 'coords'
ECCENTRICITY = 'eccentricity'
EQUIVALENT_DIAMETER = 'equivalent_diameter'
EULER_NUMBER = 'euler_number'
EXTENT = 'extent'
FILLED_AREA = 'filled_area'
FILLED_IMAGE = 'filled_image'
IMAGE = 'image'
INERTIA_TENSOR = 'inertia_tensor'
INERTIA_TENSOR_EIGVALS = 'inertia_tensor_eigvals'
LOCAL_CENTROID = 'local_centroid'
MAJOR_AXIS_LENGTH = 'major_axis_length'
MINOR_AXIS_LENGTH = 'minor_axis_length'
MOMENTS = 'moments'
MOMENTS_CENTRAL = 'moments_central'
MOMENTS_HU = 'moments_hu'
MOMENTS_NORMALIZED = 'moments_normalized'
ORIENTATION = 'orientation'
PERIMETER = 'perimeter'
SLICE = 'slice'
SOLIDITY = 'solidity'
DEFAULT_REGION_PROPERTIES = [<RegionProperties.AREA: 'area'>, <RegionProperties.ECCENTRICITY: 'eccentricity'>, <RegionProperties.MAJOR_AXIS_LENGTH: 'major_axis_length'>, <RegionProperties.MINOR_AXIS_LENGTH: 'minor_axis_length'>, <RegionProperties.ORIENTATION: 'orientation'>]

List of RegionProperties computed by default, see __init__()

__init__(img, mask, channel_names=None, region_properties=None)
Parameters
  • img (image file path, MultichannelImage or array-like) – intensity image, shape: (c, y, x)

  • mask (mask file path or array-like) – (path to) cell mask of shape: (y, x)

  • channel_names (Optional[Sequence[str]]) – channel names

  • region_properties (Optional[Sequence[RegionProperties]]) – list of RegionProperties to compute, defaults to DEFAULT_REGION_PROPERTIES when None

property image_width

Image width in pixels

Return type

int

property image_height

Image height in pixels

Return type

int

property num_channels

Number of channels

Return type

int

property channel_names

Channel names

Return type

List[str]

property num_cells

Number of cells

Return type

int

property cell_ids

Integer cell IDs

Return type

ndarray

property cell_centroids

Cell centroids, shape: (cells, dimensions=2)

Return type

DataArray

property min_intensities

Minimum cell intensities

Returns

DataArray with coordinates (cell IDs, channel names)

property min_intensities_table

Minimum cell intensities

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: channel names)

property max_intensities

Maximum cell intensities

Returns

DataArray with coordinates (cell IDs, channel names)

property max_intensities_table

Maximum cell intensities

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: channel names)

property mean_intensities

Mean cell intensities

Returns

DataArray with coordinates (cell IDs, channel names)

property mean_intensities_table

Mean cell intensities

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: channel names)

property median_intensities

Median cell intensities

Returns

DataArray with coordinates (cell IDs, channel names)

property median_intensities_table

Median cell intensities

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: channel names)

property std_intensities

Standard deviations of cell intensities

Returns

DataArray with coordinates (cell IDs, channel names)

property std_intensities_table

Standard deviations of cell intensities

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: channel names)

property var_intensities

Variances of cell intensities

Returns

DataArray with coordinates (cell IDs, channel names)

property var_intensities_table

Variances of cell intensities

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: channel names)

property regionprops

Region properties

For a list of computed properties, see region_properties.

Return type

DataArray

Returns

DataArray with coordinates (cell_id, property_name)

property regionprops_table

Region properties

For a list of computed properties, see region_properties.

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: regionprops property names)

compute_cell_intensities(aggr)

Compute cell intensity values

Parameters

aggr (Callable[[ndarray], Any]) – function for aggregating the pixel values of a cell

Return type

DataArray

Returns

DataArray with coordinates (cell IDs, channel names)

compute_cell_border_distances()

Compute the pairwise Euclidean distances between cell borders

Return type

DataArray

Returns

symmetric border distance matrix

to_dataset(cell_properties=False, cell_channel_properties=False)

Returns an xarray.Dataset representation of the current instance

At least one of cell_properties or cell_channel_properties has to be True

Parameters
  • cell_properties (Union[bool, Sequence[str]]) – list of cell properties (e.g. regionprops) to include; set to True to include all

  • cell_channel_properties (Union[bool, Sequence[str]]) – list of cell channel properties (e.g. intensity values) to include; set to True to include all

Return type

Dataset

Returns

Dataset with cell ID, channel name and/or property name as coordinates

compute_cell_centroid_distances(metric='euclidean')

Compute the pairwise distances between cell centroids

Parameters

metric (str) – the distance metric to use, see https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html

Return type

DataArray

Returns

symmetric cell centroid distance matrix

to_anndata(cell_properties=False, cell_channel_properties=False, x_cell_channel_property=None)

Returns an anndata.AnnData representation of the current instance

Parameters
  • cell_properties – list of cell properties (e.g. regionprops) to include; set to True to include all

  • cell_channel_properties – list of cell channel properties (e.g. intensity values) to include; set to True to include all

  • x_cell_channel_property – cell channel property to use for the main AnnData data matrix (X)

Returns

AnnData object, in which cell channel properties (e.g. intensity values) are stored as layers and cell properties (e.g. regionprops) are stored as observations

to_dataframe(cell_properties=False, cell_channel_properties=False)

Returns a pandas.DataFrame representation of the current instance

Column names for cell channel properties (e.g. intensity values) are prefixed by the property name, e.g. a mean_intensities property would be included as 'mean_intensities_{channel_name}'. Column names for cell properties (e.g. regionprops) are not prefixed.

Parameters
  • cell_properties (Union[bool, Sequence[str]]) – list of cell properties (e.g. regionprops) to include; set to True to include all

  • cell_channel_properties (Union[bool, Sequence[str]]) – list of cell channel properties (e.g. intensity values) to include; set to True to include all

Return type

DataFrame

Returns

DataFrame (index: cell IDs, columns: see description)

write_csv(path, cell_properties=False, cell_channel_properties=False, **kwargs)

Writes a CSV file, see to_dataframe() for format specifications

Parameters
  • path (Union[str, Path]) – path to the .csv file to be written

  • cell_properties (Union[bool, Sequence[str]]) – list of cell properties (e.g. regionprops) to include; set to True to include all

  • cell_channel_properties (Union[bool, Sequence[str]]) – list of cell channel properties (e.g. intensity values) to include; set to True to include all

  • kwargs – other arguments passed to pandas.DataFrame.to_csv()

write_fcs(path, cell_properties=False, cell_channel_properties=False, **kwargs)

Writes an FCS file, see to_dataframe() for format specifications

Uses fcswrite.write_fcs() for writing FCS 3.0 files.

Parameters
  • path (Union[str, Path]) – path to the .fcs file to be written

  • cell_properties (Union[bool, Sequence[str]]) – list of cell properties (e.g. regionprops) to include; set to True to include all

  • cell_channel_properties (Union[bool, Sequence[str]]) – list of cell channel properties (e.g. intensity values) to include; set to True to include all

  • kwargs – other arguments passed to fcswrite.write_fcs()