Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation check for inf/nan radius for shapes #653

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading