Skip to content

Commit da7153f

Browse files
committed
Fix upgrade to numpy 2.0 issues
1. Numpy no longer allows assigning 1D array into a scalar element. We have to extract a matching value before assigning. 2. np.trapz was deprecated (removed in 2.4) - replace with np.trapezoid Signed-off-by: Patrick Avery <patrick.avery@kitware.com>
1 parent f40f031 commit da7153f

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

hexrd/core/instrument/hedm_instrument.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,10 +1118,10 @@ def extract_polar_maps(
11181118
# find indices
11191119
idx = np.zeros_like(active_hkls, dtype=int)
11201120
for i, input_hklID in enumerate(active_hkls):
1121-
try:
1122-
idx[i] = np.where(active_hklIDs == input_hklID)[0]
1123-
except ValueError:
1121+
matches = np.where(active_hklIDs == input_hklID)[0]
1122+
if len(matches) != 1:
11241123
raise RuntimeError(f"hklID '{input_hklID}' is invalid")
1124+
idx[i] = matches[0]
11251125
tth_ranges = tth_ranges[idx]
11261126

11271127
delta_eta = eta_edges[1] - eta_edges[0]

hexrd/core/material/crystallography.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,10 +1608,10 @@ def getHKLs(
16081608
idx = np.zeros(len(self.hklDataList), dtype=int)
16091609
for i, hkl_id in enumerate(hkl_ids):
16101610
# find ordinal index of current hklID
1611-
try:
1612-
idx[i] = int(np.where(all_hkl_ids == hkl_id)[0])
1613-
except TypeError:
1614-
raise RuntimeError(f"Requested hklID '{hkl_id}'is invalid!")
1611+
matches = np.where(all_hkl_ids == hkl_id)[0]
1612+
if len(matches) != 1:
1613+
raise RuntimeError(f"Requested hklID '{hkl_id}' is invalid!")
1614+
idx[i] = matches[0]
16151615
if sorted_excl[idx[i]] and not allHKLs:
16161616
raise RuntimeError(f"Requested hklID '{hkl_id}' is excluded!")
16171617
hkls.append(self.hklDataList[idx[i]]['hkl'])

hexrd/hedm/ipfcolor/sphere_sector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,14 @@ def calc_hue(self, dir3, switch):
686686
rho = self.calculate_rho(dir3, switch)
687687
r = np.linspace(0.0, 2 * np.pi, 1000)
688688
v = self.hue_speed(r)
689-
cons = np.trapz(v, r)
689+
cons = np.trapezoid(v, r)
690690

691691
h = np.zeros(rho.shape)
692692

693693
for i in range(rho.shape[0]):
694694
r = np.linspace(0.0, rho[i], 1000)
695695
v = self.hue_speed(r)
696-
h[i] = np.trapz(v, r) / cons
696+
h[i] = np.trapezoid(v, r) / cons
697697

698698
return h
699699

hexrd/powder/wppf/LeBailCalibration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ def computespectrum(self, instr_updated, lp_updated):
212212

213213
weighted_expt = np.nan_to_num(ww * v.spectrum_expt._y**2)
214214

215-
wss = np.trapz(evec, v.tth_list)
216-
den = np.trapz(weighted_expt, v.tth_list)
215+
wss = np.trapezoid(evec, v.tth_list)
216+
den = np.trapezoid(weighted_expt, v.tth_list)
217217
r = np.sqrt(wss / den) * 100.0
218218
if ~np.isnan(r):
219219
rwp.append(r)

hexrd/powder/wppf/WPPF.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ def total_area(self):
878878
to protect against nans in the values
879879
'''
880880
mask = np.isnan(total_intensity)
881-
sum_area = np.trapz(total_intensity[~mask], tth[~mask])
881+
sum_area = np.trapezoid(total_intensity[~mask], tth[~mask])
882882
return sum_area
883883

884884
@property

hexrd/powder/wppf/amorphous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def amorphous_lineout(self):
339339
def integrated_area(self):
340340
x = self.tth_list
341341
y = self.amorphous_lineout
342-
return np.trapz(y, x)
342+
return np.trapezoid(y, x)
343343

344344
@property
345345
def peak_model(self):

hexrd/powder/wppf/peakfunctions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def pvfcj(
483483
res += pv * fact
484484

485485
res = np.sin(tth_r) * res / den / 4.0 / HoL / SoL
486-
a = np.trapz(res, tth_list)
486+
a = np.trapezoid(res, tth_list)
487487
return res / a
488488

489489

@@ -580,8 +580,8 @@ def pvoight_pink_beam(
580580

581581
g = _gaussian_pink_beam(alpha_exp, beta_exp, fwhm_g, tth, tth_list)
582582
l_val = _lorentzian_pink_beam(alpha_exp, beta_exp, fwhm_l, tth, tth_list)
583-
ag = np.trapz(g, tth_list)
584-
al = np.trapz(l_val, tth_list)
583+
ag = np.trapezoid(g, tth_list)
584+
al = np.trapezoid(l_val, tth_list)
585585
if np.abs(ag) < 1e-6:
586586
ag = 1.0
587587
if np.abs(al) < 1e-6:
@@ -768,7 +768,7 @@ def calc_Iobs_pvfcj(
768768
y = Ic * pv
769769
y = y[mask]
770770

771-
Iobs[ii] = np.trapz(yo * y / yc, tth_list_mask)
771+
Iobs[ii] = np.trapezoid(yo * y / yc, tth_list_mask)
772772

773773
return Iobs
774774

@@ -820,7 +820,7 @@ def calc_Iobs_pvtch(
820820
y = Ic * pv
821821
y = y[mask]
822822

823-
Iobs[ii] = np.trapz(yo * y / yc, tth_list_mask)
823+
Iobs[ii] = np.trapezoid(yo * y / yc, tth_list_mask)
824824

825825
return Iobs
826826

@@ -887,7 +887,7 @@ def calc_Iobs_pvpink(
887887
y = Ic * pv
888888
y = y[mask]
889889

890-
Iobs[ii] = np.trapz(yo * y / yc, tth_list_mask)
890+
Iobs[ii] = np.trapezoid(yo * y / yc, tth_list_mask)
891891

892892
return Iobs
893893

0 commit comments

Comments
 (0)