Utils
Cache operations
sopa.utils.get_cache_dir(sdata)
Get the cache directory for a SpatialData object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
Returns:
Type | Description |
---|---|
Path
|
A |
Source code in sopa/utils/utils.py
sopa.utils.delete_cache(sdata=None)
Delete the cache directory (the entire cache, or the cache of one specific SpatialData object).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData | None
|
The SpatialData object whose cache is to be deleted. If None, the entire cache is deleted. |
None
|
Source code in sopa/utils/utils.py
sopa.utils.delete_transcripts_patches_dirs(sdata)
Delete the cache directories containing the transcript patches (for instance, for Baysor or ComSeg)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
Source code in sopa/utils/utils.py
Accessing the elements
sopa.utils.get_spatial_element(element_dict, key=None, return_key=False, as_spatial_image=False)
Gets an element from a SpatialData object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element_dict
|
dict[str, SpatialElement]
|
Dictionnary whose values are spatial elements (e.g., |
required |
key
|
str | None
|
Optional element key. If |
None
|
return_key
|
bool
|
Whether to also return the key of the element. |
False
|
as_spatial_image
|
bool
|
Whether to return the element as a |
False
|
Returns:
Type | Description |
---|---|
SpatialElement | tuple[str, SpatialElement]
|
If |
Source code in sopa/utils/utils.py
sopa.utils.get_spatial_image(sdata, key=None, return_key=False, valid_attr=SopaAttrs.CELL_SEGMENTATION)
Gets a DataArray from a SpatialData object (if the image has multiple scale, the scale0
is returned)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
SpatialData object. |
required |
key
|
str | None
|
Optional image key. If |
None
|
return_key
|
bool
|
Whether to also return the key of the image. |
False
|
valid_attr
|
str
|
Attribute that the image must have to be considered valid. |
CELL_SEGMENTATION
|
Returns:
Type | Description |
---|---|
DataArray | tuple[str, DataArray]
|
If |
Source code in sopa/utils/utils.py
sopa.utils.get_boundaries(sdata, return_key=False, warn=False, key=None)
Gets cell segmentation boundaries of a SpatialData object after running Sopa.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A SpatialData object |
required |
return_key
|
bool
|
Whether to return the key of the shapes or not. |
False
|
warn
|
bool
|
If |
False
|
key
|
str | None
|
A valid |
None
|
Returns:
Type | Description |
---|---|
GeoDataFrame | tuple[str, GeoDataFrame] | None
|
A |
Source code in sopa/utils/utils.py
sopa.utils.get_intensities(sdata)
Gets the intensity dataframe of shape n_obs x n_channels
Source code in sopa/utils/utils.py
sopa.utils.get_channel_names(image, image_key=None)
Get the channel names of an image or a SpatialData object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
DataArray | DataTree | SpatialData
|
Either a |
required |
image_key
|
str | None
|
If |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
An array of channel names. |
Source code in sopa/utils/image.py
sopa.utils.set_sopa_attrs(sdata, cell_segmentation_key=None, tissue_segmentation_key=None, transcripts_key=None, boundaries_key=None, bins_table_key=None)
Stores in the SpatialData
object the keys of the main elements used in Sopa.
This allows Sopa to retreive with elements should be used for each operation.
Info
The attrs are already stored in sdata.attrs
when reading data with sopa.io
.
Use this function only if you already stored on disk a SpatialData object without the attrs (with sopa<2.0.0
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
cell_segmentation_key
|
str | None
|
Name of the image to be used for cell segmentation (highest resolution image). |
None
|
tissue_segmentation_key
|
str | None
|
Name of the image to be used for tissue segmentation (medium/low resolution image). |
None
|
transcripts_key
|
str | None
|
Name of the points containing the transcripts. |
None
|
boundaries_key
|
str | None
|
Name of the shapes containing the cell boundaries. |
None
|
bins_table_key
|
str | None
|
Name of the table containing the bins (e.g., for Visium HD data). |
None
|
Source code in sopa/utils/utils.py
Transformations and scaling
sopa.utils.to_intrinsic(sdata, element, target_element)
Transforms a SpatialElement
into the intrinsic coordinate system of another SpatialElement
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A SpatialData object |
required |
element
|
SpatialElement | str
|
|
required |
target_element
|
SpatialElement | str
|
|
required |
Returns:
Type | Description |
---|---|
SpatialElement
|
The |
Source code in sopa/utils/utils.py
sopa.utils.scale_dtype(arr, dtype)
Change the dtype of an array but keep the scale compared to the type maximum value.
Example
For an array of dtype uint8
being transformed to np.uint16
, the value 255
will become 65535
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
A |
required |
dtype
|
dtype
|
Target |
required |
Returns:
Type | Description |
---|---|
ndarray
|
A scaled |
Source code in sopa/utils/image.py
sopa.utils.resize_numpy(arr, scale_factor, dims, output_shape)
Resize a numpy image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
a |
required |
scale_factor
|
float
|
Scale factor of resizing, e.g. |
required |
dims
|
list[str]
|
List of dimension names. Only |
required |
output_shape
|
list[int]
|
Size of the output array |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Resized array |
Source code in sopa/utils/image.py
Cell-type annotation
sopa.utils.tangram_annotate(sdata, adata_sc, cell_type_key, reference_preprocessing=None, bag_size=10000, max_obs_reference=10000, **kwargs)
Tangram multi-level annotation. Tangram is run on multiple bags of cells to decrease the RAM usage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sdata
|
SpatialData
|
A |
required |
adata_sc
|
AnnData
|
A scRNAseq annotated reference |
required |
cell_type_key
|
str
|
Key of |
required |
reference_preprocessing
|
str | None
|
Preprocessing method used on the reference. Can be |
None
|
bag_size
|
int
|
Size of each bag on which tangram will be run. Use smaller bags to lower the RAM usage |
10000
|
max_obs_reference
|
int
|
Maximum number of cells used in |
10000
|
Source code in sopa/utils/annotation.py
sopa.utils.higher_z_score(adata, marker_cell_dict, cell_type_key='cell_type')
Simple channel-based segmentation using a marker-to-population dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
An |
required |
marker_cell_dict
|
dict
|
Dictionary whose keys are channels, and values are the corresponding populations. |
required |
cell_type_key
|
str
|
Key of |
'cell_type'
|
Source code in sopa/utils/annotation.py
sopa.utils.preprocess_fluo(adata)
Preprocess fluorescence data. For each column \(X\), we compute \(asinh(\frac{X}{5Q(0.2, X)})\) and apply standardization
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
An |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
A dataframe of preprocessed channels intensities |