Skip to content

Commit d311f3f

Browse files
Merge pull request #179 from scverse/fix/xenium_radii
Fix xenium default instances
2 parents 6a8b968 + 9770a84 commit d311f3f

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/spatialdata_io/readers/xenium.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def xenium(
5656
*,
5757
cells_boundaries: bool = True,
5858
nucleus_boundaries: bool = True,
59-
cells_as_circles: bool = True,
59+
cells_as_circles: bool = False,
6060
cells_labels: bool = True,
6161
nucleus_labels: bool = True,
6262
transcripts: bool = True,
@@ -96,10 +96,8 @@ def xenium(
9696
nucleus_boundaries
9797
Whether to read nucleus boundaries (polygons).
9898
cells_as_circles
99-
Whether to read cells also as circles. Useful for performant visualization. The radii of the nuclei,
100-
not the ones of cells, will be used; using the radii of cells would make the visualization too cluttered
101-
(the cell boundaries are computed as a maximum expansion of the nuclei location and therefore the
102-
corresponding circles would show considerable overlap).
99+
Whether to read cells also as circles (the center and the radius of each circle is computed from the
100+
corresponding labels cell).
103101
cells_labels
104102
Whether to read cell labels (raster). The polygonal version of the cell labels are simplified
105103
for visualization purposes, and using the raster version is recommended for analysis.
@@ -128,6 +126,25 @@ def xenium(
128126
Returns
129127
-------
130128
:class:`spatialdata.SpatialData`
129+
130+
Notes
131+
-----
132+
Old versions. Until spatialdata-io v0.1.3post0: previously, `cells_as_circles` was `True` by default; the table was associated to the
133+
circles when `cells_as_circles` was `True`, and the table was associated to the polygons when `cells_as_circles`
134+
was `False`; the radii of the circles were computed form the nuclei instead of the cells.
135+
136+
Performance. You can improve visualization performance (at the cost of accuracy) by setting `cells_as_circles` to `True`.
137+
138+
Examples
139+
--------
140+
This code shows how to change the annotation target of the table from the cell circles to the cell labels.
141+
>>> from spatialdata_io import xenium
142+
>>> sdata = xenium("path/to/raw/data", cells_as_circles=True)
143+
>>> sdata["table"].obs["region"] = "cell_labels"
144+
>>> sdata.set_table_annotates_spatialelement(
145+
... table_name="table", region="cell_labels", region_key="region", instance_key="cell_labels"
146+
... )
147+
>>> sdata.write("path/to/data.zarr")
131148
"""
132149
image_models_kwargs, labels_models_kwargs = _initialize_raster_models_kwargs(
133150
image_models_kwargs, labels_models_kwargs
@@ -138,7 +155,7 @@ def xenium(
138155
# to trigger the warning if the version cannot be parsed
139156
version = _parse_version_of_xenium_analyzer(specs, hide_warning=False)
140157

141-
specs["region"] = "cell_circles" if cells_as_circles else "cell_boundaries"
158+
specs["region"] = "cell_circles" if cells_as_circles else "cell_labels"
142159

143160
# the table is required in some cases
144161
if not cells_table:
@@ -210,14 +227,16 @@ def xenium(
210227
if cell_labels_indices_mapping is not None and table is not None:
211228
if not pd.DataFrame.equals(cell_labels_indices_mapping["cell_id"], table.obs[str(XeniumKeys.CELL_ID)]):
212229
warnings.warn(
213-
"The cell_id column in the cell_labels_table does not match the cell_id column derived from the cell "
214-
"labels data. This could be due to trying to read a new version that is not supported yet. Please "
215-
"report this issue.",
230+
"The cell_id column in the cell_labels_table does not match the cell_id column derived from the "
231+
"cell labels data. This could be due to trying to read a new version that is not supported yet. "
232+
"Please report this issue.",
216233
UserWarning,
217234
stacklevel=2,
218235
)
219236
else:
220237
table.obs["cell_labels"] = cell_labels_indices_mapping["label_index"]
238+
if not cells_as_circles:
239+
table.uns[TableModel.ATTRS_KEY][TableModel.INSTANCE_KEY] = "cell_labels"
221240

222241
if nucleus_boundaries:
223242
polygons["nucleus_boundaries"] = _get_polygons(

tests/test_xenium.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_roundtrip_with_data_limits() -> None:
5151
"dataset,expected",
5252
[
5353
("Xenium_V1_human_Breast_2fov_outs", "{'y': (0, 3529), 'x': (0, 5792), 'z': (10, 25)}"),
54-
("Xenium_V1_human_Lung_2fov_outs", "{'y': (-1, 3556), 'x': (0, 5794), 'z': (7, 32)}"),
54+
("Xenium_V1_human_Lung_2fov_outs", "{'y': (0, 3553), 'x': (0, 5793), 'z': (7, 32)}"),
5555
],
5656
)
5757
def test_example_data(dataset: str, expected: str) -> None:

0 commit comments

Comments
 (0)