From 660aa2de39a6faaf9906564973c0a36ab0200481 Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Wed, 1 May 2024 20:53:31 -0400 Subject: [PATCH 1/2] Update __array__ to copy if needed --- regions/core/mask.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/regions/core/mask.py b/regions/core/mask.py index 084b49cf..a7531d9e 100644 --- a/regions/core/mask.py +++ b/regions/core/mask.py @@ -6,9 +6,12 @@ import astropy.units as u import numpy as np +from astropy.utils import minversion __all__ = ['RegionMask'] +COPY_IF_NEEDED = False if not minversion(np, '2.0.0.dev') else None + class RegionMask: """ @@ -40,12 +43,12 @@ def __init__(self, data, bbox): self.bbox = bbox self._mask = (self.data == 0) - def __array__(self, dtype=None, copy=None): + def __array__(self, dtype=None, copy=COPY_IF_NEEDED): """ Array representation of the mask data array (e.g., for matplotlib). """ - return np.asarray(self.data, dtype=dtype) + return np.array(self.data, dtype=dtype, copy=copy) @property def shape(self): From e6fb68d61d760a9bad5050f14e295eaf33d1acad Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Wed, 1 May 2024 21:01:28 -0400 Subject: [PATCH 2/2] Fix test --- regions/core/tests/test_mask.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/regions/core/tests/test_mask.py b/regions/core/tests/test_mask.py index 8935cece..bfb3f0a7 100644 --- a/regions/core/tests/test_mask.py +++ b/regions/core/tests/test_mask.py @@ -54,12 +54,6 @@ def test_mask_copy(): mask_copy[0, 0] = 100.0 assert mask.data[0, 0] == 100.0 - # needs to copy because of the dtype change - mask = RegionMask(np.ones((10, 10)), bbox) - mask_copy = np.array(mask, copy=False, dtype=int) - mask_copy[0, 0] = 100 - assert mask.data[0, 0] == 1.0 - # no copy mask = RegionMask(np.ones((10, 10)), bbox) mask_copy = np.asarray(mask)