Patches
sopa.make_image_patches(sdata, patch_width=2000, patch_overlap=50, image_key=None, key_added=None)
Create overlapping patches on an image. This can be used for image-based segmentation methods such as Cellpose, which will run on each patch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
patch_width
|
int
|
Width of the patches, in pixels. |
2000
|
patch_overlap
|
int
|
Number of pixels of overlap between patches. |
50
|
image_key
|
str | None
|
Optional key of the image on which the patches will be made. If not provided, it is found automatically. |
None
|
key_added
|
str | None
|
Optional name of the patches to be saved. By default, uses |
None
|
Source code in sopa/patches/_factory.py
sopa.make_transcript_patches(sdata, patch_width=2000, patch_overlap=50, points_key=None, prior_shapes_key=None, unassigned_value=None, min_points_per_patch=4000, write_cells_centroids=False, key_added=None, **kwargs)
Create overlapping patches on a transcripts dataframe, and save it in a cache. This can be used for trancript-based segmentation methods such as Baysor.
Prior segmentation usage
To save assign a prior segmentation to each transcript, you can use the prior_shapes_key
argument:
- If a segmentation has already been performed (for example an existing 10X-Genomics segmentation), use
prior_shapes_key
to denote the column of the transcript dataframe containing the cell IDs (you can also optionaly use theunassigned_value
argument). - If you have already run segmentation with Sopa, use
prior_shapes_key
to denote the name of the shapes (GeoDataFrame) containing the boundaries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
patch_width
|
int
|
Width of the patches, in microns. |
2000
|
patch_overlap
|
int
|
Number of microns of overlap between patches. |
50
|
points_key
|
str | None
|
Optional key of the points on which the patches will be made. If not provided, it is found automatically. |
None
|
prior_shapes_key
|
str | None
|
Optional key of |
None
|
unassigned_value
|
int | str | None
|
If |
None
|
min_points_per_patch
|
int
|
Minimum number of points/transcripts for a patch to be considered for segmentation. |
4000
|
write_cells_centroids
|
bool
|
If |
False
|
key_added
|
str | None
|
Optional name of the patches to be saved. By default, uses |
None
|
**kwargs
|
int
|
Additional arguments passed to the |
{}
|
Source code in sopa/patches/_factory.py
sopa.patches.compute_embeddings(sdata, model, patch_width, patch_overlap=0, level=0, magnification=None, image_key=None, batch_size=32, device=None, key_added=None)
It creates patches, runs a computer vision model on each patch, and store the embeddings of each all patches as an image. This is mostly useful for WSI images.
Info
The image will be saved into the SpatialData
object with the key "{model_name}_embeddings"
(see the model_name
argument below), except if key_added
is provided.
The shapes of the patches will be saved with the key "embeddings_patches"
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
model
|
Callable | str
|
Callable that takes as an input a tensor of size |
required |
patch_width
|
int
|
Width (pixels) of the patches. |
required |
patch_overlap
|
int
|
Width (pixels) of the overlap between the patches. |
0
|
level
|
int | None
|
Image level on which the processing is performed. Either |
0
|
magnification
|
int | None
|
The target magnification on which the processing is performed. If |
None
|
image_key
|
str | None
|
Optional image key of the image, unecessary if there is only one image. |
None
|
batch_size
|
int
|
Mini-batch size used during inference. |
32
|
device
|
str | None
|
Device used for the computer vision model. |
None
|
key_added
|
str | None
|
Optional name of the spatial element that will be added (storing the embeddings). |
None
|
Source code in sopa/patches/infer.py
sopa.patches.cluster_embeddings(sdata, element, method='leiden', key_added='cluster', **method_kwargs)
Create clusters of the patches embeddings (obtained from sopa.patches.compute_embeddings).
Info
The clusters are added to the key_added
column of the "inference_patches" shapes (key_added='cluster'
by default).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
element
|
DataArray | str
|
The |
required |
method
|
Callable | str
|
Callable that takes as an input an array of size |
'leiden'
|
key_added
|
str
|
The key containing the clusters to be added to the patches |
'cluster'
|
method_kwargs
|
str
|
kwargs provided to the method callable |
{}
|