Skip to content

Commit

Permalink
workaround for nan radii xenium
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaMarconato committed Aug 2, 2024
1 parent e77f981 commit 923db85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/spatialdata/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,14 @@ def validate(cls, data: GeoDataFrame) -> None:
if np.any(radii <= 0):
raise ValueError("Radii of circles must be positive.")
if np.any(np.isnan(radii)) or np.any(np.isinf(radii)):
raise ValueError("Radii of circles must not be nan or inf.")
# using logger.warning instead of warnings.warn to avoid the warning to being silenced in some cases
# (e.g. PyCharm console)
logger.warning(
"Radii of circles must not be nan or inf (this warning will be turned into a ValueError in the "
"next code release). If you are seeing this warning after reading previously saved Xenium data, "
"please see https://github.com/scverse/spatialdata/discussions/657 for a solution. Otherwise, "
"please correct the radii of the circles before calling the parser function.",
)
if cls.TRANSFORM_KEY not in data.attrs:
raise ValueError(f":class:`geopandas.GeoDataFrame` does not contain `{TRANSFORM_KEY}`." + SUGGESTION)
if len(data) > 0:
Expand Down
16 changes: 10 additions & 6 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,16 @@ def test_shapes_model(self, model: ShapesModel, path: Path) -> None:
poly[ShapesModel.RADIUS_KEY].iloc[0] = 0
with pytest.raises(ValueError, match="Radii of circles must be positive."):
ShapesModel.validate(poly)
poly[ShapesModel.RADIUS_KEY].iloc[0] = np.nan
with pytest.raises(ValueError, match="Radii of circles must not be nan or inf."):
ShapesModel.validate(poly)
poly[ShapesModel.RADIUS_KEY].iloc[0] = np.inf
with pytest.raises(ValueError, match="Radii of circles must not be nan or inf."):
ShapesModel.validate(poly)

# tests to be restored when the validation is re-enabled (now it just raises a warning, that is tricky to
# capture)
# poly[ShapesModel.RADIUS_KEY].iloc[0] = np.nan
# with pytest.raises(ValueError, match="Radii of circles must not be nan or inf."):
# ShapesModel.validate(poly)
#
# poly[ShapesModel.RADIUS_KEY].iloc[0] = np.inf
# with pytest.raises(ValueError, match="Radii of circles must not be nan or inf."):
# ShapesModel.validate(poly)

@pytest.mark.parametrize("model", [PointsModel])
@pytest.mark.parametrize("instance_key", [None, "cell_id"])
Expand Down

0 comments on commit 923db85

Please sign in to comment.