Skip to content

Commit

Permalink
fix: support non float 64 longitudes and latitudes
Browse files Browse the repository at this point in the history
this was working before numpy 2.0 since the conversion was done without us knowing
  • Loading branch information
ManonMarchand committed Jan 6, 2025
1 parent 0a06199 commit d35b7fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Fixed

* support longitudes and latitudes that are not of `dtype` `np.float64`. This was broken
by numpy 2.0. Before that, the conversion was done silently. See [#35]

## 0.7.1

### Added
Expand Down
15 changes: 13 additions & 2 deletions python/cdshealpix/tests/test_nested_healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)


@pytest.mark.parametrize("size", [1, 10, 100, 1000, 10000, 100000])
@pytest.mark.parametrize("size", [1, 10, 100])
def test_lonlat_to_healpix(size):
depth = np.random.randint(30)
lon = Longitude(np.random.rand(size) * 360, u.deg)
Expand All @@ -52,7 +52,18 @@ def test_lonlat_to_healpix(size):
assert ((dy >= 0) & (dy <= 1)).all()


@pytest.mark.parametrize("size", [1, 10, 100, 1000, 10000, 100000])
# regression test for issue #35
def test_lonlat_to_healpix_float32():
healpix = lonlat_to_healpix(
lon=Longitude(np.array(3, dtype=np.float32), "deg"),
lat=Latitude(np.array(5, dtype=np.float32), "deg"),
depth=2,
)
assert len(healpix) == 1
assert healpix[0] == 76


@pytest.mark.parametrize("size", [1, 10, 100])
def test_healpix_to_lonlat(size):
depth = np.random.randint(30)
ipixels = np.random.randint(12 * 4**depth, size=size, dtype=np.uint64)
Expand Down
14 changes: 11 additions & 3 deletions python/cdshealpix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from . import cdshealpix

__all__ = ["to_ring", "from_ring"]
__all__ = ["from_ring", "to_ring"]

# Raise a ValueError exception if the input
# HEALPix cells array contains invalid values
Expand Down Expand Up @@ -42,8 +42,16 @@ def _validate_lonlat_wrap(lon, lat, *args, **kwargs):
f"'lon' and 'lat' should have the same shape but are of shapes {lon.shape} and {lat.shape}",
)
# convert into astropy objects
lon = lon if isinstance(lon, Longitude) else Longitude(lon)
lat = lat if isinstance(lat, Latitude) else Latitude(lat)
lon = (
lon
if (isinstance(lon, Longitude) and lon.dtype == np.float64)
else Longitude(lon, dtype=np.float64)
)
lat = (
lat
if (isinstance(lat, Latitude) and lat.dtype == np.float64)
else Latitude(lat, dtype=np.float64)
)
return function(lon, lat, *args, **kwargs)

return _validate_lonlat_wrap
Expand Down

0 comments on commit d35b7fc

Please sign in to comment.