Patches
sopa.make_image_patches(sdata, patch_width=2000, patch_overlap=50, image_key=None, roi_key=SopaKeys.ROI, 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 | None
|
Width of the patches, in pixels. If |
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
|
roi_key
|
str | None
|
Optional name of the shapes that need to touch the patches. Patches that do not touch any shape will be ignored during segmentation. By default, uses |
ROI
|
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, roi_key=SopaKeys.ROI, 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 or Proseg.
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="auto"to use it (or, provide manually the column name and theunassigned_valueargument). - If you have already run segmentation with Sopa, use
prior_shapes_keyto denote the name of the shapes (GeoDataFrame) containing the boundaries, e.g.prior_shapes_key="cellpose_boundaries".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sdata
|
SpatialData
|
A |
required |
patch_width
|
float | int | None
|
Width of the patches, in microns. If |
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
|
Literal['auto'] | 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
|
roi_key
|
str | None
|
Optional name of the shapes that need to touch the patches. Patches that do not touch any shape will be ignored during segmentation. By default, uses |
ROI
|
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, data_parallel=False, roi_key=SopaKeys.ROI, key_added=None, **kwargs)
It creates patches, runs a computer vision model on each patch, and store the embeddings of each all patches as an AnnData object. This is mostly useful for WSI images.
Info
The AnnData object 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".
Warning
In addition to the WSI extra (pip install 'sopa[wsi]') and depending on the model used, you might need to install additional dependencies. Also, CONCH requires to be logged in Hugging Face and having approved their License.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sdata
|
SpatialData
|
A |
required |
model
|
Callable | str
|
A supported model name ( |
required |
patch_width
|
int
|
Width of the patches in pixels. |
required |
patch_overlap
|
int
|
Width of the overlap between the patches in pixels. |
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. By default, uses the only image (if only one) or the image used for cell or tissue segmentation. |
None
|
batch_size
|
int
|
Mini-batch size used during inference. |
32
|
device
|
str | None
|
Device used for the computer vision model. |
None
|
data_parallel
|
bool | list[int]
|
If |
False
|
roi_key
|
str | None
|
Optional name of the shapes that needs to touch the patches. Patches that do not touch any shape will be ignored. If |
ROI
|
key_added
|
str | None
|
Optional name of the spatial element that will be added (storing the embeddings). |
None
|
**kwargs
|
int
|
Additional keyword arguments passed to the |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The name of the |
Source code in sopa/patches/infer.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
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 | None
|
A |
required |
element
|
AnnData | str
|
The |
required |
method
|
Callable | str
|
Callable that takes as an AnnData object and returns an array of clusters of size |
'leiden'
|
key_added
|
str
|
The key containing the clusters to be added to the |
'cluster'
|
method_kwargs
|
str
|
kwargs provided to the method callable |
{}
|