sopa.patches
sopa.patches.Patches2D
Compute 2D-patches with overlaps. This can be done on an image or a DataFrame.
Attributes:
Name | Type | Description |
---|---|---|
polygons |
list[Polygon]
|
List of |
bboxes |
ndarray
|
Array of shape |
ilocs |
ndarray
|
Array of shape |
Source code in sopa/patches/patches.py
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
__init__(sdata, element_name, patch_width, patch_overlap=50)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata |
SpatialData
|
A |
required |
element_name |
str
|
Name of the element on with patches will be made (image or points) |
required |
patch_width |
float | int
|
Width of the patches (in the unit of the coordinate system of the element) |
required |
patch_overlap |
float | int
|
Overlap width between the patches |
50
|
Source code in sopa/patches/patches.py
__len__()
patchify_transcripts(temp_dir, cell_key=None, unassigned_value=None, use_prior=False, config={}, config_path=None, config_name=SopaFiles.TOML_CONFIG_FILE, csv_name=SopaFiles.TRANSCRIPTS_FILE, min_transcripts_per_patch=4000, shapes_key=SopaKeys.CELLPOSE_BOUNDARIES)
Creation of patches for the transcripts.
Prior segmentation usage
To save assign a prior segmentation to each transcript, you can either use cell_key
or use_prior
:
- If a segmentation has already been performed (for example an existing 10X-Genomics segmentation), use
cell_key
to denote the column of the transcript dataframe containing the cell IDs (and optionalyunassigned_value
). - If you have already run Cellpose with Sopa, use
use_prior
(no need to providecell_key
andunassigned_value
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
temp_dir |
str
|
Temporary directory where each patch will be stored. Note that each patch will have its own subdirectory. |
required |
cell_key |
str
|
Optional key of the transcript dataframe containing the cell IDs. This is useful if a prior segmentation has been run, assigning each transcript to a cell. |
None
|
unassigned_value |
int | str
|
If |
None
|
use_prior |
bool
|
Whether to use Cellpose as a prior segmentation. If |
False
|
config |
dict
|
Dictionnary of segmentation parameters |
{}
|
config_path |
str | None
|
Path to the segmentation config file (you can also directly provide the argument via the |
None
|
config_name |
str
|
Name of the config file to be saved in each patch subdirectory |
TOML_CONFIG_FILE
|
csv_name |
str
|
Name of the CSV file to be saved in each patch subdirectory |
TRANSCRIPTS_FILE
|
min_transcripts_per_patch |
int
|
Minimum number of transcripts for a patch to be considered for segmentation |
4000
|
Returns:
Type | Description |
---|---|
list[int]
|
A list of patches indices. Each index correspond to the name of a subdirectory inside |
Source code in sopa/patches/patches.py
write(overwrite=True, shapes_key=None)
Save patches in sdata.shapes["sopa_patches"]
(or by the key specified)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
overwrite |
bool
|
Whether to overwrite patches if existing |
True
|
shapes_key |
str | None
|
Optional name of the shapes to be saved. By default, uses "sopa_patches". |
None
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
The saved GeoDataFrame |
Source code in sopa/patches/patches.py
sopa.patches.infer.infer_wsi_patches(sdata, model, patch_width, patch_overlap=0, level=0, magnification=None, image_key=None, batch_size=32, device=None)
Create an image made of patch based predictions of a WSI image.
Info
The image will be saved into the SpatialData
object with the key sopa_{model_name}
(see the argument below).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata |
SpatialData
|
A |
required |
model |
Callable | str
|
Callable that takes as an input a tensor of size (batch_size, channels, x, y) and returns a vector for each tile (batch_size, emb_dim), or a string with the name of one of the available models ( |
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 WSI image, unecessary if there is only one image. |
None
|
batch_size |
int
|
Mini-batch size used during inference. |
32
|
device |
str
|
Device used for the computer vision model. |
None
|
Returns:
Type | Description |
---|---|
DataArray | bool
|
If the processing was successful, returns the |
Source code in sopa/patches/infer.py
sopa.patches.cluster.cluster_embeddings(sdata, element, method='leiden', key_added='cluster', **method_kwargs)
Cluster the patches embeddings using a clustering method
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 |
{}
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
The patches |