Skip to content

Commit

Permalink
Merge pull request #62 from ken-lauer/fix_numpy_2.0
Browse files Browse the repository at this point in the history
FIX: address np.string_ and ndarray.ptp deprecations in numpy 2.0
  • Loading branch information
ChristopherMayes authored Jun 19, 2024
2 parents 309bc04 + 1a095f2 commit b3ce1fc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pmd_beamphysics/interfaces/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def vprint(*a, **k):
z = -betac*particle_group['t']

# Get z span
z_ptp = z.ptp()
z_ptp = np.ptp(z)
# Add tiny padding
z_pad = 1e-20 # Tiny pad

Expand Down Expand Up @@ -546,7 +546,7 @@ def create_impact_solrf_fieldmap_fourier(field_mesh,
zmax = z.max()
else:
zmin = 0
zmax = z.ptp()
zmax = np.ptp(z)

fcoefs = dat['fcoefs']

Expand Down Expand Up @@ -628,7 +628,7 @@ def create_impact_solrf_fieldmap_derivatives(field_mesh,
rfdata = []

z = field_mesh.coord_vec('z')
L = z.ptp()
L = np.ptp(z)

info = {'format':'solrf'}
field = {}
Expand Down
2 changes: 1 addition & 1 deletion pmd_beamphysics/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def average_current(self):
Simple average `current = charge / dt` in [A], with `dt = (max_t - min_t)`
If particles are in $t$ coordinates, will try` dt = (max_z - min_z)*c_light*beta_z`
"""
dt = self.t.ptp() # ptp 'peak to peak' is max - min
dt = np.ptp(self.t) # ptp 'peak to peak' is max - min
if dt == 0:
# must be in t coordinates. Calc with
dt = self.z.ptp() / (self.avg('beta_z')*c_light)
Expand Down
8 changes: 4 additions & 4 deletions pmd_beamphysics/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def fstr(s):
"""
Makes a fixed string for h5 files
"""
return np.string_(s)
return np.bytes_(s)



Expand Down Expand Up @@ -43,7 +43,7 @@ def decode_attr(a):
return a.decode('utf-8')

if isinstance(a, np.ndarray):
if a.dtype.type is np.string_:
if a.dtype.type is np.bytes_:
a = a.astype(str)
if len(a) == 1:
return a[0]
Expand Down Expand Up @@ -71,9 +71,9 @@ def encode_attr(a):

if isinstance(a, np.ndarray):
if a.dtype.type is np.str_:
a = a.astype(np.string_)
a = a.astype(np.bytes_)

return a

def encode_attrs(attrs):
return {k:encode_attr(v) for k,v in attrs.items()}
return {k:encode_attr(v) for k,v in attrs.items()}
2 changes: 1 addition & 1 deletion pmd_beamphysics/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def nice_array(a):
x = a[0]
else:
a = np.array(a)
x = max(a.ptp(), abs(np.mean(a))) # Account for tiny spread
x = max(np.ptp(a), abs(np.mean(a))) # Account for tiny spread

fac, prefix = nice_scale_prefix( x )

Expand Down

0 comments on commit b3ce1fc

Please sign in to comment.