From b5e7f8c01335da65c4003128cf03a00cc44e6fbc Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Wed, 3 Jul 2024 10:48:33 -0400 Subject: [PATCH] Fix numpy 2.0 errors --- pixell/enmap.py | 5 ++++- pixell/enplot.py | 8 ++++++++ pixell/tilemap.py | 5 ++++- tests/test_pixell.py | 4 ---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pixell/enmap.py b/pixell/enmap.py index 76c8306f..837bff90 100644 --- a/pixell/enmap.py +++ b/pixell/enmap.py @@ -48,7 +48,10 @@ def __array_finalize__(self, obj): def __repr__(self): return "ndmap(%s,%s)" % (np.asarray(self), wcsutils.describe(self.wcs)) def __str__(self): return repr(self) - def __array_wrap__(self, arr, context=None): + def __array_wrap__(self, arr, context=None, return_scalar=False): + # In the future need to support `return_scalar`, but that is seemingly + # undocumented and not actually supported in numpy 2.0? So for now we + # just ignore it. if arr.ndim < 2: return arr return ndmap(arr, self.wcs) def __reduce__(self): diff --git a/pixell/enplot.py b/pixell/enplot.py index 11a4b763..f1d5bae3 100644 --- a/pixell/enplot.py +++ b/pixell/enplot.py @@ -542,6 +542,14 @@ def draw_colorbar(crange, width, args): fmt = "%g" labels, boxes = [], [] for val in crange: + # Val could be a one-element array. In NumPy 1.25 this is not + # acceptable to string formatters. + + try: + val = val[0] + except (TypeError, IndexError): + pass + labels.append(fmt % val) boxes.append(font.getbbox(labels[-1])[-2:]) boxes = np.array(boxes,int) diff --git a/pixell/tilemap.py b/pixell/tilemap.py index cab00540..22766ce9 100644 --- a/pixell/tilemap.py +++ b/pixell/tilemap.py @@ -71,7 +71,10 @@ def __array_finalize__(self, obj): def __repr__(self): return "TileMap(%s,%s)" % (np.asarray(self), str(self.geometry)) def __str__(self): return repr(self) - def __array_wrap__(self, arr, context=None): + def __array_wrap__(self, arr, context=None, return_scalar=False): + # In the future need to support `return_scalar`, but that is seemingly + # undocumented and not actually supported in numpy 2.0? So for now we + # just ignore it. return TileMap(arr, self.geometry) def __getitem__(self, sel): # Split sel into normal and wcs parts. diff --git a/tests/test_pixell.py b/tests/test_pixell.py index a21caf04..f55aa346 100644 --- a/tests/test_pixell.py +++ b/tests/test_pixell.py @@ -1121,7 +1121,3 @@ def test_tilemap(self): assert m1c.geometry.nactive == 2 assert np.allclose(m1c, 1) - -if __name__ == '__main__': - unittest.main() - test_sim_slice()