Skip to content

Commit 4ae49b2

Browse files
authored
Merge pull request #248 from arunkannawadi/u/arunkannawadi/np2
Compatibility with numpy 2
2 parents 534fda6 + 136f325 commit 4ae49b2

File tree

12 files changed

+79
-28
lines changed

12 files changed

+79
-28
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
name: tests
1212
strategy:
1313
matrix:
14-
pyver: ["3.9", "3.10", "3.11"]
14+
pyver: ["3.9", "3.10", "3.11", "3.12"]
1515

1616
runs-on: "ubuntu-latest"
1717

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## v2.4.0
2+
3+
### Compatibility
4+
5+
- Update to use either numpy>2. or 1.x
6+
7+
### Misc
8+
9+
- Raise an exception if negative integers are passed to ``get_flags_str``.
10+
111
## v2.3.2
212

313

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ngmix
22
=====
33

4-
[![Build Status](https://travis-ci.com/esheldon/ngmix.svg?branch=master)](https://travis-ci.com/esheldon/ngmix)
4+
[![Build Status](https://github.com/esheldon/ngmix/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/esheldon/ngmix/actions/workflows/test.yml)
55

66
Gaussian mixture models and other tools for working with 2d images, implemented
77
in python. The code is made fast using the numba package.

ngmix/defaults.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import numpy as np
22

3+
# Shim to support numpy >= 2 and < 2.0.0.
4+
if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
5+
copy_if_needed = None
6+
else:
7+
copy_if_needed = False
38

49
# default values
510
PDEF = -9.999e9 # parameter defaults

ngmix/fitting/galsim_results.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
from .results import FitModel, PSFFluxFitModel
77
from ..gexceptions import GMixRangeError
8-
from ..defaults import LOWVAL
8+
from ..defaults import copy_if_needed, LOWVAL
99
from .. import observation
1010
from ..observation import Observation, ObsList, MultiBandObsList
1111

@@ -269,7 +269,7 @@ def _check_guess(self, guess):
269269
exception
270270
"""
271271

272-
guess = np.array(guess, dtype="f8", copy=False)
272+
guess = np.array(guess, dtype="f8", copy=copy_if_needed)
273273
if guess.size != self.npars:
274274
raise ValueError(
275275
"expected %d entries in the "

ngmix/fitting/results.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
from .. import gmix
55
from ..gexceptions import GMixRangeError
6-
from ..defaults import PDEF, CDEF, LOWVAL, BIGVAL
6+
from ..defaults import PDEF, CDEF, LOWVAL, BIGVAL, copy_if_needed
77
from ..observation import Observation, ObsList, get_mb_obs
88
from ..gmix.gmix_nb import gmix_convolve_fill, fill_fdiff
99
from ..gmix import GMixList, MultiBandGMixList
@@ -361,7 +361,7 @@ def _setup_fit(self, guess):
361361
setup the mixtures based on the initial guess
362362
"""
363363

364-
guess = np.array(guess, dtype="f8", copy=False)
364+
guess = np.array(guess, dtype="f8", copy=copy_if_needed)
365365

366366
npars = guess.size
367367
mess = "guess has npars=%d, expected %d" % (npars, self.npars)

ngmix/flags.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numpy as np
2+
13
NO_ATTEMPT = 2**0
24
CEN_SHIFT = 2**1
35
NONPOS_FLUX = 2**2
@@ -61,7 +63,7 @@ def get_flags_str(val, name_map=None):
6163
Parameters
6264
----------
6365
val : int
64-
The flag value.
66+
The flag value. This must be non-negative.
6567
name_map : dict, optional
6668
A dictionary mapping values to names. Default is global at
6769
ngmix.flags.NAME_MAP.
@@ -74,8 +76,18 @@ def get_flags_str(val, name_map=None):
7476
if name_map is None:
7577
name_map = NAME_MAP
7678

79+
# Negative values are always invalid and cause confusion if cast to an
80+
# unsigned integer.
81+
if val < 0:
82+
raise ValueError(f"Flag value {val} must be non-negative.")
83+
84+
# Cast to uint32 is sufficient given the range of flags.
85+
# This is because the second argument to the bitwise AND operator (&) below
86+
# would get implicitly cast to the same type as `val`.
87+
val = np.array(val, dtype=np.uint32)
88+
7789
nstrs = []
78-
for pow in range(32):
90+
for pow in range(31):
7991
fval = 2**pow
8092
if ((val & fval) != 0):
8193
if fval in name_map:

ngmix/gaussap.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
from .gmix import GMixModel, GMixCM
77
from .gexceptions import GMixRangeError
8+
from ngmix.defaults import copy_if_needed
89
from ngmix.flags import NO_ATTEMPT, GMIX_RANGE_ERROR
910

1011
DEFAULT_FLUX = np.nan
@@ -117,19 +118,19 @@ def _prepare(pars,
117118
TdByTe=None,
118119
mask=None):
119120

120-
pars = np.array(pars, dtype='f8', ndmin=2, copy=False)
121+
pars = np.array(pars, dtype='f8', ndmin=2, copy=copy_if_needed)
121122

122123
if mask is not None:
123-
mask = np.array(mask, dtype=bool, ndmin=1, copy=False)
124+
mask = np.array(mask, dtype=bool, ndmin=1, copy=copy_if_needed)
124125
assert mask.shape[0] == pars.shape[0], \
125126
'mask and pars must be same length'
126127
else:
127128
mask = np.ones(pars.shape[0], dtype=bool)
128129

129130
if model == 'cm':
130131

131-
fracdev = np.array(fracdev, dtype='f8', ndmin=1, copy=False)
132-
TdByTe = np.array(TdByTe, dtype='f8', ndmin=1, copy=False)
132+
fracdev = np.array(fracdev, dtype='f8', ndmin=1, copy=copy_if_needed)
133+
TdByTe = np.array(TdByTe, dtype='f8', ndmin=1, copy=copy_if_needed)
133134
assert fracdev.size == pars.shape[0], 'fracdev/pars must be same size'
134135
assert TdByTe.size == pars.shape[0], 'TdByTe/pars must be same length'
135136

ngmix/priors/priors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ..gexceptions import GMixRangeError
88
from .random import make_rng
9-
from ..defaults import LOWVAL
9+
from ..defaults import LOWVAL, copy_if_needed
1010

1111

1212
class PriorBase(object):
@@ -260,7 +260,7 @@ def get_prob_array(self, vals):
260260
The locations at which to evaluate
261261
"""
262262

263-
vals = np.array(vals, ndmin=1, dtype="f8", copy=False)
263+
vals = np.array(vals, ndmin=1, dtype="f8", copy=copy_if_needed)
264264
pvals = np.zeros(vals.size)
265265

266266
for i in range(vals.size):
@@ -309,7 +309,7 @@ def _get_fdiff_array(self, vals):
309309
vals: number
310310
The locations at which to evaluate
311311
"""
312-
vals = np.array(vals, ndmin=1, dtype="f8", copy=False)
312+
vals = np.array(vals, ndmin=1, dtype="f8", copy=copy_if_needed)
313313
fdiff = np.zeros(vals.size)
314314

315315
for i in range(vals.size):
@@ -780,7 +780,7 @@ def get_lnprob_array(self, vals):
780780
vals: array
781781
The locations at which to evaluate
782782
"""
783-
vals = np.array(vals, dtype="f8", copy=False)
783+
vals = np.array(vals, dtype="f8", copy=copy_if_needed)
784784
if self.shift is not None:
785785
vals = vals - self.shift
786786

ngmix/priors/shape.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .priors import PriorBase
1111
import logging
1212
from ..util import print_pars
13-
from ..defaults import LOWVAL
13+
from ..defaults import LOWVAL, copy_if_needed
1414

1515
LOGGER = logging.getLogger(__name__)
1616

@@ -115,8 +115,8 @@ def get_lnprob_array2d(self, g1arr, g2arr):
115115
Get the 2d prior for the array inputs
116116
"""
117117

118-
g1arr = array(g1arr, dtype="f8", copy=False)
119-
g2arr = array(g2arr, dtype="f8", copy=False)
118+
g1arr = array(g1arr, dtype="f8", copy=copy_if_needed)
119+
g2arr = array(g2arr, dtype="f8", copy=copy_if_needed)
120120

121121
output = numpy.zeros(g1arr.size) + LOWVAL
122122
self.fill_lnprob_array2d(g1arr, g2arr, output)
@@ -133,8 +133,8 @@ def get_prob_array2d(self, g1arr, g2arr):
133133
Get the 2d prior for the array inputs
134134
"""
135135

136-
g1arr = array(g1arr, dtype="f8", copy=False)
137-
g2arr = array(g2arr, dtype="f8", copy=False)
136+
g1arr = array(g1arr, dtype="f8", copy=copy_if_needed)
137+
g2arr = array(g2arr, dtype="f8", copy=copy_if_needed)
138138

139139
output = numpy.zeros(g1arr.size)
140140
self.fill_prob_array2d(g1arr, g2arr, output)
@@ -151,7 +151,7 @@ def get_prob_array1d(self, garr):
151151
Get the 1d prior for the array inputs
152152
"""
153153

154-
garr = array(garr, dtype="f8", copy=False)
154+
garr = array(garr, dtype="f8", copy=copy_if_needed)
155155

156156
output = numpy.zeros(garr.size)
157157
self.fill_prob_array1d(garr, output)
@@ -726,8 +726,8 @@ def get_prob_array2d(self, x, y):
726726
"""
727727
get prob at the input positions
728728
"""
729-
x = numpy.array(x, dtype="f8", ndmin=1, copy=False)
730-
y = numpy.array(y, dtype="f8", ndmin=1, copy=False)
729+
x = numpy.array(x, dtype="f8", ndmin=1, copy=copy_if_needed)
730+
y = numpy.array(y, dtype="f8", ndmin=1, copy=copy_if_needed)
731731
out = numpy.zeros(x.size, dtype="f8")
732732

733733
r2 = x ** 2 + y ** 2

0 commit comments

Comments
 (0)