Skip to content

Commit

Permalink
applied code review, added test for xy swap bug
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaMarconato committed Oct 10, 2023
1 parent 1395fb5 commit a08e413
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
25 changes: 9 additions & 16 deletions src/spatialdata/_core/data_extent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Functions to compute the bounding box describing the extent of a SpatialElement or region.
from __future__ import annotations

from collections import defaultdict
Expand Down Expand Up @@ -32,6 +33,7 @@ def _get_extent_of_circles(circles: GeoDataFrame) -> BoundingBoxDescription:
Parameters
----------
circles
The circles represented as a GeoDataFrame with a radius column.
Returns
-------
Expand All @@ -54,10 +56,7 @@ def _get_extent_of_circles(circles: GeoDataFrame) -> BoundingBoxDescription:
bounds["maxx"] += circles["radius"]
bounds["maxy"] += circles["radius"]

extent = {}
for ax in axes:
extent[ax] = (bounds[f"min{ax}"].min(), bounds[f"max{ax}"].max())
return extent
return {ax: (bounds[f"min{ax}"].min(), bounds[f"max{ax}"].max()) for ax in axes}


def _get_extent_of_polygons_multipolygons(
Expand All @@ -69,6 +68,7 @@ def _get_extent_of_polygons_multipolygons(
Parameters
----------
shapes
The shapes represented as a GeoDataFrame.
Returns
-------
Expand All @@ -77,21 +77,14 @@ def _get_extent_of_polygons_multipolygons(
assert isinstance(shapes.geometry.iloc[0], (Polygon, MultiPolygon))
axes = get_axes_names(shapes)
bounds = shapes["geometry"].bounds
# NOTE: this implies the order x, y (which is probably correct?)
extent = {}
for ax in axes:
extent[ax] = (bounds[f"min{ax}"].min(), bounds[f"max{ax}"].max())
return extent
return {ax: (bounds[f"min{ax}"].min(), bounds[f"max{ax}"].max()) for ax in axes}


def _get_extent_of_points(e: DaskDataFrame) -> BoundingBoxDescription:
axes = get_axes_names(e)
min_coordinates = np.array([e[ax].min().compute() for ax in axes])
max_coordinates = np.array([e[ax].max().compute() for ax in axes])
extent = {}
for i, ax in enumerate(axes):
extent[ax] = (min_coordinates[i], max_coordinates[i])
return extent
mins = dict(e[list(axes)].min().compute())
maxs = dict(e[list(axes)].max().compute())
return {ax: (mins[ax], maxs[ax]) for ax in axes}


def _get_extent_of_data_array(e: DataArray, coordinate_system: str) -> BoundingBoxDescription:
Expand Down Expand Up @@ -129,7 +122,7 @@ def get_extent(
Parameters
----------
e
The SpatialData object or SpatialElement to computed the extent of.
The SpatialData object or SpatialElement to compute the extent of.
Returns
-------
Expand Down
12 changes: 11 additions & 1 deletion tests/core/test_data_extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import pandas as pd
import pytest
from geopandas import GeoDataFrame
from numpy.random import default_rng
from shapely.geometry import MultiPolygon, Point, Polygon
from spatialdata import SpatialData, get_extent, transform
from spatialdata._utils import _deepcopy_geodataframe
from spatialdata.datasets import blobs
from spatialdata.models import PointsModel, ShapesModel
from spatialdata.models import Image2DModel, PointsModel, ShapesModel
from spatialdata.transformations import Affine, Translation, remove_transformation, set_transformation

# for faster tests; we will pay attention not to modify the original data
sdata = blobs()
RNG = default_rng(seed=0)


def check_test_results0(extent, min_coordinates, max_coordinates, axes):
Expand Down Expand Up @@ -333,3 +335,11 @@ def test_get_extent_affine_sdata():
max_coordinates=max_coordinates1,
axes=("x", "y"),
)


def test_bug_get_extent_swap_xy_for_images():
# https://github.com/scverse/spatialdata/issues/335#issue-1842914360
x = RNG.random((1, 10, 20))
im = Image2DModel.parse(x, dims=("c", "x", "y"))
extent = get_extent(im)
check_test_results1(extent, {"x": (0.0, 10.0), "y": (0.0, 20.0)})

0 comments on commit a08e413

Please sign in to comment.