sdata#

Functions to work with spatialdata objects.

scportrait.tools.sdata.write_image(sdata: SpatialData, image: ndarray | DataArray | DataTree | Array, image_name: str, channel_names: list[str] = None, scale_factors: list[int] = None, chunks: tuple[int, int, int] | None = None, transform: None = None, rgb: bool = False, overwrite=False) None#

Add the image to the spatialdata object

Parameters:
  • sdata – SpatialData object to which the image will be added.

  • image – Image to be written to the spatialdata object. Can be an array (numpy or dask) or an already validated image model.

  • image_name – Name of the image to be written to the spatialdata object.

  • channel_names – List of channel names for the image. Default is None.

  • scale_factors – List of scale factors for the image. Default is [2, 4, 8]. This will load the image at 4 different resolutions to allow for fluid visualization.

  • chunks – Chunk size for the image. Default is (1, 1000, 1000).

  • transform – Transformation to be applied to the image. Uses the Identity transformation by default.

  • rgb – Whether the image is RGB. Default is False.

  • overwrite – Whether to overwrite existing data. Default is False.

Returns:

None The spatialdata object is updated on disk.

scportrait.tools.sdata.write_labels(sdata: SpatialData, labels: ndarray | DataArray | DataTree | Array, labels_name: str, scale_factors: list[int] = None, chunks: tuple[int, int] | None = None, overwrite=False, transform: None = None) None#

Add the labels to the spatialdata object

Parameters:
  • sdata – SpatialData object to which the labels will be added.

  • labels – Labels to be written to the spatialdata object. Can be an array (numpy or dask) or an already validated labels model.

  • labels_name – Name of the labels to be written to the spatialdata object.

  • scale_factors – List of scale factors for the labels. Default is [2, 4, 8]. This will load the labels at 4 different resolutions to allow for fluid visualization.

  • chunks – Chunk size for the labels.Default is (1000, 1000).

  • overwrite – Whether to overwrite existing data.

  • transform – Transformation to be applied to the labels. Uses the Identity transformation by default.

Returns:

None The spatialdata object is updated on disk.

scportrait.tools.sdata.add_element_sdata(sdata: SpatialData, element: Any, element_name: str, overwrite: bool = True)#

Add an element to the SpatialData object.

Parameters:
  • sdata – SpatialData object

  • element – Element to add

  • element_name – Name of the element to be added

  • overwrite – Whether to overwrite the element if it already exists

Returns:

the SpatialData object is updated on file

Return type:

None

scportrait.tools.sdata.rename_image_element(sdata: SpatialData, image_element: str, new_element_name: str) SpatialData#

Rename an image element in the sdata object.

Parameters:
  • sdata – SpatialData object

  • image_element – Image element to rename

  • new_element_name – New name for the image element

Returns:

Updated SpatialData object with the renamed image element.

Return type:

sdata

processing (pp)#

scportrait.tools.sdata.pp.percentile_normalize_image(sdata: SpatialData, image_name: str, lower_percentile: float | list[float] = 0.1, upper_percentile: float | list[float] = 99.9, rescaled_image_name: str | None = None, scale_factors: list[int] | None = None, overwrite: bool = True, chunks: tuple[int, int, int] | None = None) SpatialData#

Percentile Normalize an image in a spatialdata object.

Parameters:
  • sdata – SpatialData object containing the image to be percentile normalized.

  • image_name – Name of the image to be percentile normalized.

  • lower_percentile – Lower percentile for normalization. Default is 0.1.

  • upper_percentile – Upper percentile for normalization. Default is 99.9.

  • rescaled_image_name – Name of the rescaled image. Default is None.

  • overwrite – Whether to overwrite existing data. Default is True.

  • chunks – Chunk size for the image. Default is None.

Returns:

the SpatialData object is updated on file with a new element containing the rescaled image with the name “rescaled_image_name”. If this is not provided, the name will be “{image_name}_rescaled”. If the name is identical to the input image the input image will be overwritten.

scportrait.tools.sdata.pp.get_bounding_box_sdata(sdata: <module 'spatialdata' from '/opt/hostedtoolcache/Python/3.11.12/x64/lib/python3.11/site-packages/spatialdata/__init__.py'>, max_width: int, center_x: int, center_y: int) <module 'spatialdata' from '/opt/hostedtoolcache/Python/3.11.12/x64/lib/python3.11/site-packages/spatialdata/__init__.py'>#

apply bounding box to sdata object

Parameters:
  • sdata – spatialdata object

  • max_width – maximum width of the bounding box

  • center_x – x coordinate of the center of the bounding box

  • center_y – y coordinate of the center of the bounding box

Returns:

spatialdata object with bounding box applied

Return type:

spatialdata