sddb.loader.Centroids#

class sddb.loader.Centroids#

Each cell's centre of mass in labels["cells"] as obsm["spatial"] (x, y), in table row order.

The pixel sums per label run block by block and are reduced over the blocks. No row is dropped. A row whose label has no pixel (a cell smaller than a pixel of the written grid) takes:

  • labels cells: its centre of mass in the source labels at native resolution (item.native_cells), mapped to the written grid by that element's own transformation. This costs one more block pass over the source labels, only when a cell has no pixel. The table's centroid is not used, since its frame is the reader's (microns for Xenium and Atera in spatialdata-io, against labels in pixels).

  • shape cells, in this order: the centroid of its polygon (item.shape_centroids, set by Rasterize); then the source table's centroid (obsm["spatial"], item.table_centroids). Readers put it in different frames (microns for Atera, pixels for CODEX, DNB units for Stereo-seq in spatialdata-io), and spatialdata gives a table no transformation, so Rasterize maps it as intrinsic coordinates of the element the table annotates (that element's transformation), and this step checks the mapping on up to _TABLE_CHECK_ROWS cells that have pixels: the median distance to their pixel centroid must be at most _TABLE_CENTROID_PX, else it raises, as obsm["spatial"] is then not in that element's frame.

The count goes to item.n_centroid_fallback and into the contract as n_centroid_fallback, and is logged.

A fallback centroid keeps its true position, also outside the written grid (a cell of the labels that lies beyond the H&E). obs["outside_image"] is True for every row whose centroid is not on the image's footprint: the quadrilateral of the source image's scale-0 extent mapped onto the grid (item.image_footprint, set by Rasterize with the affine it resamples the image with), points on its edge counting as on it, or whose centroid lies outside [0, w) x [0, h). With a rotated image the grid's corners are fill, so a cell there is flagged too. The count goes to item.n_outside_image and into the contract as n_outside_image.

Raises:

ValueError -- If a row has no pixel and no fallback (labels cells: no pixel in the native labels either; shape cells: no polygon centroid and no usable table centroid), or if the table centroids are not in the frame of the element the table annotates (the check above).

Attributes table#

Methods table#

build(sdata)

Return the item with obsm["spatial"] and obs["label_id"], one centroid per row.

from_record(rec)

Rebuild the step from to_record()'s output.

requirements()

labels["cells"].

to_record()

The step as an AdapterRecord that from_record() rebuilds.

validate(sdata)

Require labels["cells"].

validate_meta(meta)

No catalog metadata decides these steps; the checks run on the item in validate().

Attributes#

name: ClassVar[str] = 'Centroids'#
version: ClassVar[str] = '0.1.0'#
config: dict[str, Any]#

Methods#

build(sdata: LoaderItem) → LoaderItem#

Return the item with obsm["spatial"] and obs["label_id"], one centroid per row.

classmethod from_record(rec: AdapterRecord) → Self#

Rebuild the step from to_record()'s output.

Raises:

ValueError -- If the record is of another adapter or another version.

requirements() → dict[str, Any]#

labels["cells"].

to_record() → AdapterRecord#

The step as an AdapterRecord that from_record() rebuilds.

validate(sdata: LoaderItem) → list[str]#

Require labels["cells"].

validate_meta(meta: ObjectMeta) → list[str]#

No catalog metadata decides these steps; the checks run on the item in validate().