The tool (tl
) accessor
The tool accessor contains methods that utilize third-party tools to enable operations such as segmentation or cell type prediction.
- class spatialproteomics.tl.tool.ToolAccessor(xarray_obj)
The tool accessor enables the application of external tools such as StarDist or Astir.
- astir(marker_dict: dict, key: str = '_intensity', threshold: float = 0, seed: int = 42, learning_rate: float = 0.001, batch_size: float = 64, n_init: int = 5, n_init_epochs: int = 5, max_epochs: int = 500, cell_id_col: str = 'cell_id', cell_type_col: str = 'cell_type', **kwargs)
This method predicts cell types from an expression matrix using the Astir algorithm.
- Parameters
marker_dict (dict) – Dictionary mapping markers to cell types. Can also include cell states. Example: {“cell_type”: {‘B’: [‘PAX5’], ‘T’: [‘CD3’], ‘Myeloid’: [‘CD11b’]}}
key (str, optional) – Layer to use as expression matrix.
threshold (float, optional) – Certainty threshold for astir to assign a cell type. Defaults to 0.
seed (int, optional) – Random seed. Defaults to 42.
learning_rate (float, optional) – Learning rate. Defaults to 0.001.
batch_size (float, optional) – Batch size. Defaults to 64.
n_init (int, optional) – Number of initializations. Defaults to 5.
n_init_epochs (int, optional) – Number of epochs for each initialization. Defaults to 5.
max_epochs (int, optional) – Maximum number of epochs. Defaults to 500.
cell_id_col (str, optional) – Column name for cell IDs. Defaults to “cell_id”.
cell_type_col (str, optional) – Column name for cell types. Defaults to “cell_type”.
- Raises
ValueError – If no expression matrix was present or the image is not of type uint8.
- Returns
A DataArray with the assigned cell types.
- Return type
DataArray
- cellpose(channel: ~typing.Optional[str] = None, key_added: ~typing.Optional[str] = '_cellpose_segmentation', diameter: int = 0, channel_settings: list = [0, 0], num_iterations: int = 2000, cellprob_threshold: float = 0.0, flow_threshold: float = 0.4, gpu: bool = True, model_type: str = 'cyto3', postprocess_func: ~typing.Callable = <function ToolAccessor.<lambda>>)
Segment cells using Cellpose. Adds a layer to the spatialproteomics object with dimension (X, Y) or (C, X, Y) dependent on whether channel argument is specified or not.
- Parameters
channel (str, optional) – Channel to use for segmentation. If None, all channels are used.
key_added (str, optional) – Key to assign to the segmentation results.
diameter (int, optional) – Expected cell diameter in pixels.
channel_settings (List[int], optional) – Channels for Cellpose to use for segmentation. If [0, 0], independent segmentation is performed on all channels. If it is anything else (e. g. [1, 2]), joint segmentation is attempted.
num_iterations (int, optional) – Maximum number of iterations for segmentation.
gpu (bool, optional) – Whether to use GPU for segmentation.
model_type (str, optional) – Type of Cellpose model to use.
postprocess_func (Callable, optional) – Function to apply to the segmentation masks after prediction.
- Returns
Dataset containing original data and segmentation mask.
- Return type
xr.Dataset
Notes
This method requires the ‘cellpose’ package to be installed.
- cellpose_denoise(key_added: List[str] = ['_cellpose_denoise', '_cellpose_denoise_segmentation'], diameter: int = 0, channel_settings: list = [0, 0], gpu: bool = True, model_type: str = 'cyto3', restore_type: str = 'denoise_cyto3', **kwargs)
Segment cells using Cellpose.
- Parameters
key_added (str, optional) – Key to assign to the segmentation results.
diameter (int, optional) – Expected cell diameter in pixels.
channel_settings (List[int], optional) – Channels for Cellpose to use for segmentation.
num_iterations (int, optional) – Maximum number of iterations for segmentation.
gpu (bool, optional) – Whether to use GPU for segmentation.
model_type (str, optional) – Type of Cellpose model to use.
- Returns
Dataset containing original data and segmentation mask.
- Return type
xr.Dataset
Notes
This method requires the ‘cellpose’ package to be installed.
- convert_to_anndata(expression_matrix_key: str = '_intensity', obs_key: str = '_obs', additional_layers: Optional[dict] = None, additional_uns: Optional[dict] = None)
Convert the spatialproteomics object to an anndata.AnnData object. The resulting AnnData object does not store the original image or segmentation mask.
- expression_matrix_keystr, optional
The key of the expression matrix in the spatialproteomics object. Default is Layers.INTENSITY.
- obs_keystr, optional
The key of the observation data in the spatialproteomics object. Default is Layers.OBS.
- additional_layersdict, optional
Additional layers to include in the anndata.AnnData object. The keys are the names of the layers and the values are the corresponding keys in the spatialproteomics object.
- additional_unsdict, optional
Additional uns data to include in the anndata.AnnData object. The keys are the names of the uns data and the values are the corresponding keys in the spatialproteomics object.
- adataanndata.AnnData
The converted anndata.AnnData object.
- AssertionError
If the expression matrix key or additional layers are not found in the spatialproteomics object.
The expression matrix is extracted from the spatialproteomics object using the provided expression matrix key.
If additional layers are specified, they are extracted from the spatialproteomics object and added to the anndata.AnnData object.
If obs_key is present in the spatialproteomics object, it is used to create the obs DataFrame of the anndata.AnnData object.
If additional_uns is specified, the corresponding uns data is extracted from the spatialproteomics object and added to the anndata.AnnData object.
- convert_to_spatialdata(image_key: str = '_image', segmentation_key: str = '_segmentation', **kwargs)
Convert the spatialproteomics object to a spatialdata object.
- Parameters
image_key (str) – The key of the image data in the object. Defaults to Layers.IMAGE.
segmentation_key (str) – The key of the segmentation data in the object. Defaults to Layers.SEGMENTATION.
**kwargs – Additional keyword arguments to be passed to the convert_to_anndata method.
- Returns
The converted spatialdata object.
- Return type
spatial_data_object (spatialdata.SpatialData)
- mesmer(key_added: ~typing.Optional[str] = '_mesmer_segmentation', postprocess_func: ~typing.Callable = <function ToolAccessor.<lambda>>, **kwargs)
Segment cells using Mesmer. Adds a layer to the spatialproteomics object with dimension (C, X, Y). Assumes C is two and has the order (nuclear, membrane).
- Parameters
key_added (str, optional) – Key to assign to the segmentation results.
postprocess_func (Callable, optional) – Function to apply to the segmentation masks after prediction.
- Returns
Dataset containing original data and segmentation mask.
- Return type
xr.Dataset
Notes
This method requires the ‘mesmer’ package to be installed.
- stardist(channel: ~typing.Optional[str] = None, key_added: ~typing.Optional[str] = '_stardist_segmentation', scale: float = 3, n_tiles: int = 12, normalize: bool = True, predict_big: bool = False, postprocess_func: ~typing.Callable = <function ToolAccessor.<lambda>>, **kwargs) Dataset
Apply StarDist algorithm to perform instance segmentation on the nuclear image.
- scalefloat, optional
Scaling factor for the StarDist model (default is 3).
- n_tilesint, optional
Number of tiles to split the image into for prediction (default is 12).
- normalizebool, optional
Flag indicating whether to normalize the nuclear image (default is True).
- nuclear_channelstr, optional
Name of the nuclear channel in the image (default is “DAPI”).
- predict_bigbool, optional
Flag indicating whether to use the ‘predict_instances_big’ method for large images (default is False).
- postprocess_funcCallable, optional
Function to apply to the segmentation masks after prediction (default is lambda x: x).
- **kwargsdict, optional
Additional keyword arguments to be passed to the StarDist prediction method.
- objxr.Dataset
Xarray dataset containing the segmentation mask and centroids.
- ValueError
If the object already contains a segmentation mask.