Skip to content

Commit 9b8593d

Browse files
committed
Fix for numpy 2.x
1 parent eaa6eac commit 9b8593d

4 files changed

Lines changed: 34 additions & 18 deletions

File tree

python/magic/checker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
from magic import MagicTs, avgField, MagicSpectrum, MagicRadial, scanDir, MagicSetup
33
import numpy as np
44
from scipy.signal import argrelextrema
5-
import matplotlib.pyplot as plt
65
import os
76

7+
try:
8+
from numpy import trapz as trapz
9+
except:
10+
from numpy import trapezoid as trapz
11+
812
class bcolors:
913
HEADER = '\033[95m'
1014
OKBLUE = '\033[34m'
@@ -76,7 +80,7 @@ def MagicCheck(tstart=None):
7680
print(bcolors.MODERATE + 'Sudden variations detected in power balance!' + bcolors.ENDC)
7781
ones = np.ones_like(ts.time[mask])
7882
ones[~mask_spike] = 0.
79-
ttot_spikes = np.trapz(ones, ts.time[mask])
83+
ttot_spikes = trapz(ones, ts.time[mask])
8084
ttot = ts.time[-1]-ts.time[0]
8185
ratio = ttot_spikes/ttot
8286
print(' -Time fraction with spikes: {:.3f} %'.format(100.*ratio))

python/magic/coeff.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
from magic import npfile, scanDir, MagicSetup, hammer2cart, symmetrize, progressbar
33
from scipy.interpolate import interp1d
44
from scipy import signal
5-
from scipy.version import version
65
import os, re
76
import numpy as np
87
import matplotlib.pyplot as plt
98
from .spectralTransforms import SpectralTransforms
109
from magic.setup import labTex
1110
import copy
1211

12+
try:
13+
from numpy import trapz as trapz
14+
except:
15+
from numpy import trapezoid as trapz
1316

1417
def deriv(x, y, axis=0):
1518
"""
@@ -224,8 +227,8 @@ def __init__(self, tag=None, datadir='.', ratio_cmb_surface=1, scale_b=1,
224227
# Time-averaged Gauss coefficient
225228
if not ave:
226229
facT = 1./(self.time[-1]-self.time[0])
227-
self.glmM = facT * np.trapz(self.glm, self.time, axis=0)
228-
self.hlmM = facT * np.trapz(self.hlm, self.time, axis=0)
230+
self.glmM = facT * trapz(self.glm, self.time, axis=0)
231+
self.hlmM = facT * trapz(self.hlm, self.time, axis=0)
229232

230233
if len(self.time) > 3:
231234
self.dglmdt = deriv(self.time, self.glm.T, axis=1)
@@ -257,11 +260,11 @@ def __init__(self, tag=None, datadir='.', ratio_cmb_surface=1, scale_b=1,
257260

258261
if not ave:
259262
# Time-averaged energy
260-
self.ElM = facT * np.trapz(self.El, self.time, axis=0)
261-
self.EmM = facT * np.trapz(self.Em, self.time, axis=0)
263+
self.ElM = facT * trapz(self.El, self.time, axis=0)
264+
self.EmM = facT * trapz(self.Em, self.time, axis=0)
262265

263266
# Secular variation
264-
self.ESVlM = facT * np.trapz(self.ESVl, self.time, axis=0)
267+
self.ESVlM = facT * trapz(self.ESVl, self.time, axis=0)
265268
if abs(self.ESVlM[1:]).min() > 0.:
266269
self.taul = np.sqrt(self.ElM[1:]/self.ESVlM[1:])
267270
else:
@@ -838,10 +841,10 @@ def __init__(self, tag, datadir='.', ratio_cmb_surface=1, scale_b=1, iplot=True,
838841
# Time-averaged energy
839842
facT = 1./(self.time[-1]-self.time[0])
840843

841-
self.e_pol_lM = facT * np.trapz(self.e_pol_l, self.time, axis=0)
842-
self.e_tor_lM = facT * np.trapz(self.e_tor_l, self.time, axis=0)
843-
self.e_pol_axi_lM = facT * np.trapz(self.e_pol_axi_l, self.time, axis=0)
844-
self.e_tor_axi_lM = facT * np.trapz(self.e_tor_axi_l, self.time, axis=0)
844+
self.e_pol_lM = facT * trapz(self.e_pol_l, self.time, axis=0)
845+
self.e_tor_lM = facT * trapz(self.e_tor_l, self.time, axis=0)
846+
self.e_pol_axi_lM = facT * trapz(self.e_pol_axi_l, self.time, axis=0)
847+
self.e_tor_axi_lM = facT * trapz(self.e_tor_axi_l, self.time, axis=0)
845848

846849
def truncate(self, lCut):
847850
"""

python/magic/cyl.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import os
1212
import pickle
1313

14+
try:
15+
from numpy import trapz as trapz
16+
except:
17+
from numpy import trapezoid as trapz
18+
1419
if buildSo:
1520
try:
1621
from magic.cylavg import *
@@ -234,7 +239,7 @@ def zavg(input, radius, ns, minc, save=True, filename='vp.pickle',
234239
Z, S, out2D = sph2cyl_plane([input[0][iphi, ...]], radius, ns)
235240
S = S[:, 1:-1]
236241
Z = Z[:, 1:-1]
237-
output[iphi, :] = np.trapz(out2D[0][:, 1:-1], z, axis=0)
242+
output[iphi, :] = trapz(out2D[0][:, 1:-1], z, axis=0)
238243
if normed:
239244
output[iphi, :] /= height
240245

@@ -252,7 +257,7 @@ def zavg(input, radius, ns, minc, save=True, filename='vp.pickle',
252257
output = []
253258
outIntZ = np.zeros((ns-2), dtype=input[0].dtype)
254259
for k,out in enumerate(out2D):
255-
outIntZ = np.trapz(out[:, 1:-1], z, axis=0)
260+
outIntZ = trapz(out[:, 1:-1], z, axis=0)
256261
if normed:
257262
outIntZ /= height
258263
output.append(outIntZ)

python/magic/libmagic.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
except:
1010
from scipy.integrate import simpson as simps
1111

12+
try:
13+
from numpy import trapz as trapz
14+
except:
15+
from numpy import trapezoid as trapz
1216

1317
def selectField(obj, field, labTex=True, ic=False):
1418
"""
@@ -313,9 +317,9 @@ def avgField(time, field, tstart=None, std=False, fix_missing_series=False,
313317
stdField = 0
314318
else:
315319
fac = 1./(time[ind2]-time[ind1])
316-
avgField = fac*np.trapz(field[ind1:ind2+1, ...], time[ind1:ind2+1], axis=0)
320+
avgField = fac*trapz(field[ind1:ind2+1, ...], time[ind1:ind2+1], axis=0)
317321
if std:
318-
stdField = np.sqrt(fac*np.trapz((field[ind1:ind2+1, ...]-avgField)**2,
322+
stdField = np.sqrt(fac*trapz((field[ind1:ind2+1, ...]-avgField)**2,
319323
time[ind1:ind2+1], axis=0))
320324

321325
if std:
@@ -344,9 +348,9 @@ def writeVpEq(par, tstart):
344348
mask = np.where(abs(par.time-tstart) == min(abs(par.time-tstart)), 1, 0)
345349
ind = np.nonzero(mask)[0][0]
346350
fac = 1./(par.time.max()-par.time[ind])
347-
avgReEq = fac*np.trapz(par.reEquat[ind:], par.time[ind:])
351+
avgReEq = fac*trapz(par.reEquat[ind:], par.time[ind:])
348352
roEq = avgReEq*par.ek*(1.-par.radratio)
349-
avgRolC = fac*np.trapz(par.rolc[ind:], par.time[ind:])
353+
avgRolC = fac*trapz(par.rolc[ind:], par.time[ind:])
350354
st = '{:10.3e}{:5.2f}{:6.2f}{:11.3e}{:11.3e}{:11.3e}'.format(par.ek,
351355
par.strat, par.pr, par.ra, roEq, avgRolC)
352356

0 commit comments

Comments
 (0)