Segmentation
Cell segmentation
sopa.segmentation.cellpose(sdata, channels, diameter, model_type='cyto3', image_key=None, min_area=None, delete_cache=True, recover=False, flow_threshold=2, cellprob_threshold=-6, clip_limit=0.2, clahe_kernel_size=None, gaussian_sigma=1, key_added=SopaKeys.CELLPOSE_BOUNDARIES, cellpose_model_kwargs=None, **cellpose_eval_kwargs)
Run Cellpose segmentation on a SpatialData object, and add a GeoDataFrame containing the cell boundaries.
Cellpose installation
Make sure to install the cellpose extra (pip install 'sopa[cellpose]'
) for this method to work.
Diameter parameter
The diameter
parameter is used to estimate the expected cell diameter (in pixels). This is a crucial parameter for the segmentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
channels
|
list[str] | str
|
Name of the channels to be used for segmentation (or list of channel names). |
required |
diameter
|
int
|
The Cellpose parameter for the expected cell diameter (in pixel). |
required |
model_type
|
str
|
Cellpose model type. |
'cyto3'
|
image_key
|
str | None
|
Name of the image in |
None
|
min_area
|
int | None
|
Minimum area of a cell to be considered. By default, it is calculated based on the |
None
|
delete_cache
|
bool
|
Whether to delete the cache after segmentation. |
True
|
recover
|
bool
|
If |
False
|
flow_threshold
|
float
|
Cellpose |
2
|
cellprob_threshold
|
float
|
Cellpose |
-6
|
clip_limit
|
float
|
Parameter for skimage.exposure.equalize_adapthist (applied before running cellpose) |
0.2
|
clahe_kernel_size
|
int | list[int] | None
|
Parameter for skimage.exposure.equalize_adapthist (applied before running cellpose) |
None
|
gaussian_sigma
|
float
|
Parameter for scipy gaussian_filter (applied before running cellpose) |
1
|
key_added
|
str
|
Name of the shapes element to be added to |
CELLPOSE_BOUNDARIES
|
cellpose_model_kwargs
|
dict | None
|
Dictionary of kwargs to be provided to the |
None
|
**cellpose_eval_kwargs
|
int
|
Kwargs to be provided to |
{}
|
Source code in sopa/segmentation/methods/_cellpose.py
sopa.segmentation.stardist(sdata, channels=['r', 'g', 'b'], model_type='2D_versatile_he', image_key=None, min_area=0, delete_cache=True, recover=False, prob_thresh=0.5, nms_thresh=0.4, clip_limit=0, clahe_kernel_size=None, gaussian_sigma=0, key_added=SopaKeys.STARDIST_BOUNDARIES, **stardist_eval_kwargs)
Run Stardist segmentation on a SpatialData object, and add a GeoDataFrame containing the cell boundaries.
Stardist installation
Make sure to install the stardist extra (pip install 'sopa[stardist]'
) for this method to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
channels
|
list[str] | str
|
Name of the channels to be used for segmentation (or list of channel names). |
['r', 'g', 'b']
|
model_type
|
str
|
Stardist model name. |
'2D_versatile_he'
|
image_key
|
str | None
|
Name of the image in |
None
|
min_area
|
int
|
Minimum area of a cell to be considered. |
0
|
delete_cache
|
bool
|
Whether to delete the cache after segmentation. |
True
|
recover
|
bool
|
If |
False
|
prob_thresh
|
float
|
Stardist |
0.5
|
nms_thresh
|
float
|
Stardist |
0.4
|
clip_limit
|
float
|
Parameter for skimage.exposure.equalize_adapthist (applied before running stardist) |
0
|
clahe_kernel_size
|
int | list[int] | None
|
Parameter for skimage.exposure.equalize_adapthist (applied before running stardist) |
None
|
gaussian_sigma
|
float
|
Parameter for scipy gaussian_filter (applied before running stardist) |
0
|
key_added
|
str
|
Name of the shapes element to be added to |
STARDIST_BOUNDARIES
|
**stardist_eval_kwargs
|
int
|
Kwargs to be provided to |
{}
|
Source code in sopa/segmentation/methods/_stardist.py
sopa.segmentation.baysor(sdata, config=None, min_area=0, delete_cache=True, recover=False, force=False, scale=None, key_added=SopaKeys.BAYSOR_BOUNDARIES, patch_index=None)
Run Baysor segmentation on a SpatialData object, and add a GeoDataFrame containing the cell boundaries.
Baysor installation
Make sure to install Baysor, and either have the executable at ~/.julia/bin/baysor
, or create an alias called
baysor
that points to the binary executable. Also, you'll need to install
sopa with the baysor extra: pip install 'sopa[baysor]'
(basically, this installs toml
and loompy
).
Inferred config
If the config
argument is not provided, the configuration is inferred.
If sopa.make_transcript_patches was run with a prior_shapes_key
, the configuration is inferred based on the prior segmentation.
Otherwise, the configuration is inferred based on the scale
parameter (you'll need to provide it).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
config
|
dict | str | None
|
Optional configuration dictionary or path to a TOML file containing a valid Baysor config. By default, a configuration is inferred based on the cell area of the prior segmentation, or based on the |
None
|
min_area
|
int
|
Minimal area (in microns^2) of a cell to be considered. |
0
|
delete_cache
|
bool
|
Whether to delete the cache after segmentation. |
True
|
recover
|
bool
|
If |
False
|
force
|
bool
|
If |
False
|
scale
|
float | None
|
The typical cell radius in microns. If |
None
|
key_added
|
str
|
Name of the shapes element to be added to |
BAYSOR_BOUNDARIES
|
patch_index
|
int | None
|
Index of the patch to segment (we do not recommend to set this argument). By default, segment all patches. |
None
|
Source code in sopa/segmentation/methods/_baysor.py
sopa.segmentation.comseg(sdata, config=None, min_area=0, delete_cache=True, recover=False, key_added=SopaKeys.COMSEG_BOUNDARIES, patch_index=None)
Run ComSeg segmentation on a SpatialData object, and add a GeoDataFrame containing the cell boundaries.
ComSeg installation
Make sure to install ComSeg (pip install comseg
) for this method to work.
Transcript patches
To use ComSeg, make sure to run sopa.make_transcript_patches with a prior_shapes_key
and write_cells_centroids=True
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
config
|
dict | str | None
|
Optional configuration dictionary or path to a JSON file containing a valid ComSeg config. By default, a configuration is inferred based on the cell area of the prior segmentation. |
None
|
min_area
|
float
|
Minimal area (in microns^2) of a cell to be considered. |
0
|
delete_cache
|
bool
|
Whether to delete the cache after segmentation. |
True
|
recover
|
bool
|
If |
False
|
key_added
|
str
|
Name of the shapes element to be added to |
COMSEG_BOUNDARIES
|
patch_index
|
int | None
|
Index of the patch to segment (we do not recommend to set this argument). By default, segment all patches. |
None
|
Source code in sopa/segmentation/methods/_comseg.py
sopa.segmentation.custom_staining_based(sdata, method, channels, image_key=None, min_area=0, delete_cache=True, recover=False, clip_limit=0.2, clahe_kernel_size=None, gaussian_sigma=1, cache_dir_name=SopaKeys.CUSTOM_BOUNDARIES, key_added=SopaKeys.CUSTOM_BOUNDARIES)
Run a generic staining-based segmentation model, and add a GeoDataFrame containing the cell boundaries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
method
|
Callable
|
A segmentation |
required |
channels
|
list[str] | str
|
Name of the channels to be used for segmentation (or list of channel names). |
required |
image_key
|
str | None
|
Name of the image in |
None
|
min_area
|
int
|
Minimum area of a cell to be considered. |
0
|
delete_cache
|
bool
|
Whether to delete the cache after segmentation. |
True
|
recover
|
bool
|
If |
False
|
clip_limit
|
float
|
Parameter for skimage.exposure.equalize_adapthist (applied before running segmentation) |
0.2
|
clahe_kernel_size
|
int | list[int] | None
|
Parameter for skimage.exposure.equalize_adapthist (applied before running segmentation) |
None
|
gaussian_sigma
|
float
|
Parameter for scipy gaussian_filter (applied before running segmentation) |
1
|
cache_dir_name
|
str
|
Name of the cache directory. |
CUSTOM_BOUNDARIES
|
key_added
|
str
|
Name of the key to be added to |
CUSTOM_BOUNDARIES
|
Source code in sopa/segmentation/methods/_custom.py
Tissue segmentation
sopa.segmentation.tissue(sdata, image_key=None, level=-1, mode=None, expand_radius_ratio=0.05, channel=None, clip_parameters=(0.9, 5), blur_kernel_size=5, open_kernel_size=5, close_kernel_size=5, drop_threshold=0.01, key_added=SopaKeys.ROI)
Perform a contouring of the tissue (i.e., "tissue-segmentation"). The resulting regions-of-interest(s) are saved as shapes in the SpatialData
object. There are two
modes available: saturation
and staining
. The saturation
mode is used for H&E data, while the staining
mode
is used on staining images (more details below).
Saturation mode
This segmentation method first transforms the image from RBG color space to HSV. Then,
on the basis of the saturation channel, a median blurring is applied with an element of size blur_kernel_size
before running the Otsu method. Then a morphological opening and closing are applied as a prostprocessing
step with square elements of size open_kernel_size
and close_kernel_size
. Lastly, the connected components
with size less than drop_threshold * number_of_pixel_of_the_image
are removed, and the
rest are converted into polygons.
Staining mode
Instead of extracting the saturation channel, the image is converted to a grayscale image by taking the maximum
value of all channels (or the specified channel, if "channel"
is given). The rest of the steps are the same as in the saturation mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
image_key
|
str | None
|
Optional key of the H&E image |
None
|
level
|
int
|
Level of the multiscale image on which the segmentation will be performed (if the image is a |
-1
|
mode
|
str | None
|
Two modes are available: |
None
|
expand_radius_ratio
|
float
|
The ratio of the radius of the polygons that will be expanded. |
0.05
|
channel
|
str | None
|
The channel to use for the |
None
|
clip_parameters
|
tuple[float, float]
|
Parameters used to get the threshold used to clip the image before converting it to an 8-bit image (only used in "staining" mode). The first parameter is the quantile, and the second is the divisor. By default, the threshold is the 90th quantile divided by 5. |
(0.9, 5)
|
blur_kernel_size
|
int
|
The kernel size of the median bluring operation |
5
|
open_kernel_size
|
int
|
The kernel size of the morphological openning operation |
5
|
close_kernel_size
|
int
|
The kernel size of the morphological closing operation |
5
|
drop_threshold
|
float
|
Segments that cover less area than a ratio of |
0.01
|
key_added
|
str
|
Name of the spatial element that will be added, containing the segmented tissue polygons. |
ROI
|
Source code in sopa/segmentation/_tissue.py
Segmentation utils
sopa.segmentation.combine(sdata, elements, key_added, threshold=0.5)
Combine multiple segmentation boundaries into a single one.
Example
On the example below, we run Cellpose twice, once for nuclei and once for tumor cells. We then combine the two segmentations into a single one.
import sopa
sdata = sopa.io.toy_dataset(length=1000)
sopa.make_image_patches(sdata)
sopa.segmentation.cellpose(sdata, "DAPI", diameter=35, key_added="nuclei")
sopa.segmentation.cellpose(sdata, ["DAPI", "CK"], diameter=35, key_added="tumor_cells")
sopa.segmentation.combine(sdata, ["nuclei", "tumor_cells"], key_added="combined_cells")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
elements
|
list[str | GeoDataFrame]
|
List of name of the keys in |
required |
key_added
|
str
|
The name of the new key to be added to |
required |
threshold
|
float
|
When two cells are overlapping, we look at the area of intersection over the area of the smallest cell. If this value is higher than the |
0.5
|