Skip to content

Commit

Permalink
Added validation check for inf/nan radius for shapes (#653)
Browse files Browse the repository at this point in the history
added validation check for inf/nan radius for shapes
  • Loading branch information
LucaMarconato authored Aug 1, 2024
1 parent 91d57a2 commit 278810a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/spatialdata/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ def validate(cls, data: GeoDataFrame) -> None:
raise ValueError(f"Column `{cls.RADIUS_KEY}` not found." + SUGGESTION)
radii = data[cls.RADIUS_KEY].values
if np.any(radii <= 0):
raise ValueError("Radii of circles must be positive." + SUGGESTION)
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.")
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
6 changes: 6 additions & 0 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ 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)

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

0 comments on commit 278810a

Please sign in to comment.