The label (la) accessor

The label accessor provides several functions associated with cell type annotations and gating.

class spatialproteomics.la.label.LabelAccessor(xarray_obj)

Adds functions for cell phenotyping.

add_label_property(array: Union[ndarray, list], prop: str)

Add a label property for each unique cell type label.

This method adds a property, specified by ‘prop’, for each unique cell type label in the data object. The property values are taken from the ‘array’ argument and assigned to each corresponding cell type label.

Parameters
  • array (numpy.ndarray or list) – An array or list containing property values to be assigned to each unique cell type label.

  • prop (str) – The name of the property to be added to the cell type labels.

Returns

The updated data object with the added label property.

Return type

any

Notes

  • The function ensures that ‘array’ is converted to a NumPy array.

  • It creates a DataArray ‘da’ with the given ‘array’ as the property values and unique cell type labels as coords.

  • The DataArray ‘da’ is then merged into the data object, associating properties with cell type labels.

  • If the label property already exists in the data object, it will be updated with the new property values.

add_label_type(name: str, color: str = 'w') Dataset

Add a new label type to the data object.

This method adds a new label type with the specified ‘name’ and ‘color’ to the data object. The label type is used to identify and categorize cells in the segmentation mask.

Parameters
  • name (str) – The name of the new label type to be added.

  • color (str, optional) – The color code to represent the new label type in the visualization. Default is “white” (“w”).

Returns

The updated data object with the newly added label type.

Return type

xr.Dataset

Raises

ValueError – If the segmentation mask or observation table is not found in the data object. If the provided ‘name’ already exists as a label type.

Notes

  • The function checks for the existence of the segmentation mask and observation table in the data object.

  • It ensures that the ‘name’ of the new label type does not already exist in the label types.

  • The function then adds the new label type with the given ‘name’ and ‘color’ to the data object.

deselect(indices)

Deselect specific label indices from the data object.

This method deselects specific label indices from the data object, effectively removing them from the selection. The deselection can be performed using slices, lists, tuples, or individual integers.

Parameters

indices (slice, list, tuple, or int) – The label indices to be deselected. Can be a slice, list, tuple, or an individual integer.

Returns

The updated data object with the deselected label indices.

Return type

any

Notes

  • The function uses ‘indices’ to specify which labels to deselect.

  • ‘indices’ can be provided as slices, lists, tuples, or an integer.

  • The function then updates the data object to remove the deselected label indices.

predict_cell_types_argmax(marker_dict: dict, key: str = '_intensity', overwrite_existing_labels: bool = False, cell_col: str = 'cell', label_col: str = 'label')

Predicts cell types based on the argmax classification of marker intensities.

Parameters
  • marker_dict (dict) – A dictionary mapping cell types to markers.

  • key (str, optional) – The key of the quantification layer to use for classification. Defaults to Layers.INTENSITY.

  • overwrite_existing_labels (bool, optional) – Whether to overwrite existing labels. Defaults to False.

  • cell_col (str, optional) – The name of the column to store cell IDs in the output dataframe. Defaults to “cell”.

  • label_col (str, optional) – The name of the column to store predicted cell types in the output dataframe. Defaults to “label”.

Returns

A new SpatialData object with the predicted cell types added as labels.

Return type

spatial_data.SpatialData

Raises
  • AssertionError – If the quantification layer with the specified key is not found.

  • AssertionError – If any of the markers specified in the marker dictionary are not present in the quantification layer.

remove_label_type(cell_type: Union[int, List[int]]) Dataset

Remove specific cell type label(s) from the data object.

This method removes specific cell type label(s) identified by ‘cell_type’ from the data object. The cell type label(s) are effectively removed, and their associated cells are assigned to the ‘Unlabeled’ category.

Parameters

cell_type (int or list of int) – The ID(s) of the cell type label(s) to be removed.

Returns

The updated data object with the specified cell type label(s) removed.

Return type

xr.Dataset

Raises

ValueError – If the data object does not contain any cell type labels. If the specified ‘cell_type’ is not found among the existing cell type labels.

Notes

  • The function first checks for the existence of cell type labels in the data object.

  • It then removes the specified ‘cell_type’ from the cell type labels, setting their cells to the ‘Unlabeled’ category.

set_label_colors(labels: Union[str, List[str]], colors: Union[str, List[str]])

Set the color of a specific cell type label.

This method sets the ‘color’ of a specific cell type label identified by the ‘label’. The ‘label’ can be either a label ID or the name of the cell type label.

Parameters
  • label (int or str) – The ID or name of the cell type label whose color will be updated.

  • color (any) – The new color to be assigned to the specified cell type label.

Returns

Return type

None

Notes

  • The function converts the ‘label’ from its name to the corresponding ID for internal processing.

  • It updates the color of the cell type label in the data object to the new ‘color’.

set_label_name(label, name)

Set the name of a specific cell type label.

This method sets the ‘name’ of a specific cell type label identified by the ‘label’. The ‘label’ can be either a label ID or the name of the cell type label.

Parameters
  • label (int or str) – The ID or name of the cell type label whose name will be updated.

  • name (str) – The new name to be assigned to the specified cell type label.

Returns

Return type

None

Notes

  • The function converts the ‘label’ from its name to the corresponding ID for internal processing.

  • It updates the name of the cell type label in the data object to the new ‘name’.

threshold_labels(threshold_dict: dict, label: Optional[str] = None, layer_key: str = '_intensity')

Binarise based on a threshold. If a label is specified, the binarization is only applied to this cell type.

Parameters
  • threshold_dict (dict) – A dictionary mapping channels to threshold values.

  • label (str, optional) – The specified cell type for which the binarization is applied, by default None.

  • layer_key (str, optional) – The key for the new binary feature layer, by default “_percentage_positive_intensity”.

Returns

A new dataset object with the binary features added.

Return type

xr.Dataset

Notes

  • If a label is specified, the binarization is only applied to the cells of that specific cell type.

  • The binary feature is computed by comparing the intensity values of each channel to the threshold value.

  • The binary feature is added as a new layer to the dataset object.