Skip to content

Commit 34609e6

Browse files
authored
Merge pull request #77 from ax3l/fix-np-2.0-fixed-size-str-1.1.X
Numpy 2.0: `np.string_` -> `np.bytes_`
2 parents dd7518d + bf6fe49 commit 34609e6

File tree

2 files changed

+76
-76
lines changed

2 files changed

+76
-76
lines changed

openpmd_validator/check_h5.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_attr(f, v, request, name, is_type=None, type_format=None):
239239
240240
type_format: (numpy or python) data type
241241
Used with is_type to specify numpy ndarray dtypes or a
242-
base np.string_ format regex. Can be a list of data types
242+
base np.bytes_ format regex. Can be a list of data types
243243
for ndarrays where at least one data type must match.
244244
245245
Returns
@@ -257,7 +257,7 @@ def test_attr(f, v, request, name, is_type=None, type_format=None):
257257
# test type
258258
if is_type is not None:
259259
type_format_names = None
260-
if not type_format is None and not is_type is np.string_ and \
260+
if not type_format is None and not is_type is np.bytes_ and \
261261
not isinstance(type_format, Iterable):
262262
type_format = [type_format]
263263
type_format_names = map(lambda x: x.__name__, type_format)
@@ -266,8 +266,8 @@ def test_attr(f, v, request, name, is_type=None, type_format=None):
266266
is_type_names = "' or '".join(map(lambda x: str(x.__name__), is_type))
267267
# add for each type in is_type -> wrong, need to add this at the comparison level!
268268
if type(value) in is_type:
269-
# np.string_ format or general ndarray dtype text
270-
if type(value) is np.string_ and type_format is not None:
269+
# np.bytes_ format or general ndarray dtype text
270+
if type(value) is np.bytes_ and type_format is not None:
271271
regEx = re.compile(type_format) # Python3 only: re.ASCII
272272
if regEx.match(value.decode()) :
273273
result_array = np.array([0,0])
@@ -404,15 +404,15 @@ def check_root_attr(f, v):
404404

405405
# STANDARD.md
406406
# required
407-
result_array += test_attr(f, v, "required", "openPMD", np.string_, "^[0-9]+\.[0-9]+\.[0-9]+$")
407+
result_array += test_attr(f, v, "required", "openPMD", np.bytes_, "^[0-9]+\.[0-9]+\.[0-9]+$")
408408
result_array += test_attr(f, v, "required", "openPMDextension", np.uint32)
409-
result_array += test_attr(f, v, "required", "basePath", np.string_, "^\/data\/\%T\/$")
410-
result_array += test_attr(f, v, "required", "iterationEncoding", np.string_, "^groupBased|fileBased$")
411-
result_array += test_attr(f, v, "required", "iterationFormat", np.string_)
409+
result_array += test_attr(f, v, "required", "basePath", np.bytes_, "^\/data\/\%T\/$")
410+
result_array += test_attr(f, v, "required", "iterationEncoding", np.bytes_, "^groupBased|fileBased$")
411+
result_array += test_attr(f, v, "required", "iterationFormat", np.bytes_)
412412

413413
# optional but required for data
414-
result_array += test_attr(f, v, "optional", "meshesPath", np.string_, "^.*\/$")
415-
result_array += test_attr(f, v, "optional", "particlesPath", np.string_, "^.*\/$")
414+
result_array += test_attr(f, v, "optional", "meshesPath", np.bytes_, "^.*\/$")
415+
result_array += test_attr(f, v, "optional", "particlesPath", np.bytes_, "^.*\/$")
416416

417417
# groupBased iteration encoding needs to match basePath
418418
if result_array[0] == 0 :
@@ -423,17 +423,17 @@ def check_root_attr(f, v):
423423
result_array += np.array([1,0])
424424

425425
# recommended
426-
result_array += test_attr(f, v, "recommended", "author", np.string_)
427-
result_array += test_attr(f, v, "recommended", "software", np.string_)
426+
result_array += test_attr(f, v, "recommended", "author", np.bytes_)
427+
result_array += test_attr(f, v, "recommended", "software", np.bytes_)
428428
result_array += test_attr(f, v, "recommended",
429-
"softwareVersion", np.string_)
430-
result_array += test_attr(f, v, "recommended", "date", np.string_,
429+
"softwareVersion", np.bytes_)
430+
result_array += test_attr(f, v, "recommended", "date", np.bytes_,
431431
"^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} [\+|-][0-9]{4}$")
432432

433433
# optional
434-
result_array += test_attr(f, v, "optional", "softwareDependencies", np.string_)
435-
result_array += test_attr(f, v, "optional", "machine", np.string_)
436-
result_array += test_attr(f, v, "optional", "comment", np.string_)
434+
result_array += test_attr(f, v, "optional", "softwareDependencies", np.bytes_)
435+
result_array += test_attr(f, v, "optional", "machine", np.bytes_)
436+
result_array += test_attr(f, v, "optional", "comment", np.bytes_)
437437

438438
return(result_array)
439439

@@ -614,20 +614,20 @@ def check_meshes(f, iteration, v, extensionStates):
614614
result_array += test_attr(field, v, "required",
615615
"gridUnitSI", np.float64)
616616
result_array += test_attr(field, v, "required",
617-
"dataOrder", np.string_)
617+
"dataOrder", np.bytes_)
618618
result_array += test_attr(field, v, "required",
619-
"axisLabels", np.ndarray, np.string_)
619+
"axisLabels", np.ndarray, np.bytes_)
620620
# Specific check for geometry
621-
geometry_test = test_attr(field, v, "required", "geometry", np.string_)
621+
geometry_test = test_attr(field, v, "required", "geometry", np.bytes_)
622622
result_array += geometry_test
623623
# geometryParameters is required when using thetaMode
624624
if geometry_test[0] == 0 and field.attrs["geometry"] == b"thetaMode" :
625625
result_array += test_attr(field, v, "required",
626-
"geometryParameters", np.string_)
626+
"geometryParameters", np.bytes_)
627627
# otherwise it is optional
628628
else :
629629
result_array += test_attr(field, v, "optional",
630-
"geometryParameters", np.string_)
630+
"geometryParameters", np.bytes_)
631631

632632
# Attributes of the record's components
633633
if is_scalar_record(field) : # If the record is a scalar field
@@ -647,53 +647,53 @@ def check_meshes(f, iteration, v, extensionStates):
647647
if extensionStates['ED-PIC'] and len(list_meshes) > 0:
648648
# Check the attributes associated with the field solver
649649
result_array += test_attr(f[full_meshes_path], v, "required",
650-
"fieldSolver", np.string_)
650+
"fieldSolver", np.bytes_)
651651
valid, field_solver = get_attr(f[full_meshes_path], "fieldSolver")
652652
if (valid == True) and (field_solver in ["other", "GPSTD"]) :
653653
result_array += test_attr(f[full_meshes_path], v, "required",
654-
"fieldSolverParameters", np.string_)
654+
"fieldSolverParameters", np.bytes_)
655655

656656
# Check for the attributes associated with the field boundaries
657657
result_array += test_attr(f[full_meshes_path], v, "required",
658-
"fieldBoundary", np.ndarray, np.string_)
658+
"fieldBoundary", np.ndarray, np.bytes_)
659659
valid, field_boundary = get_attr(f[full_meshes_path], "fieldBoundary")
660660
if (valid == True) and (np.any(field_boundary == b"other")) :
661661
result_array += test_attr(f[full_meshes_path], v, "required",
662-
"fieldBoundaryParameters", np.ndarray, np.string_)
662+
"fieldBoundaryParameters", np.ndarray, np.bytes_)
663663

664664
# Check for the attributes associated with the field boundaries
665665
result_array += test_attr(f[full_meshes_path], v, "required",
666-
"particleBoundary", np.ndarray, np.string_)
666+
"particleBoundary", np.ndarray, np.bytes_)
667667
valid, particle_boundary = get_attr(f[full_meshes_path], "particleBoundary")
668668
if (valid == True) and (np.any(particle_boundary == b"other")) :
669669
result_array += test_attr(f[full_meshes_path], v, "required",
670-
"particleBoundaryParameters", np.ndarray, np.string_)
670+
"particleBoundaryParameters", np.ndarray, np.bytes_)
671671

672672
# Check the attributes associated with the current smoothing
673673
result_array += test_attr(f[full_meshes_path], v, "required",
674-
"currentSmoothing", np.string_)
674+
"currentSmoothing", np.bytes_)
675675
valid, current_smoothing = get_attr(f[full_meshes_path], "currentSmoothing")
676676
if (valid == True) and (current_smoothing != b"none") :
677677
result_array += test_attr(f[full_meshes_path], v, "required",
678-
"currentSmoothingParameters", np.string_)
678+
"currentSmoothingParameters", np.bytes_)
679679

680680
# Check the attributes associated with the charge conservation
681681
result_array += test_attr(f[full_meshes_path], v, "required",
682-
"chargeCorrection", np.string_)
682+
"chargeCorrection", np.bytes_)
683683
valid, charge_correction = get_attr(f[full_meshes_path], "chargeCorrection")
684684
if valid == True and charge_correction != b"none":
685685
result_array += test_attr(f[full_meshes_path], v, "required",
686-
"chargeCorrectionParameters", np.string_)
686+
"chargeCorrectionParameters", np.bytes_)
687687

688688
# Check for the attributes of each record
689689
for field_name in list_meshes :
690690
field = f[full_meshes_path + field_name.encode('ascii')]
691691
result_array + test_attr(field, v, "required",
692-
"fieldSmoothing", np.string_)
692+
"fieldSmoothing", np.bytes_)
693693
valid, field_smoothing = get_attr(field, "fieldSmoothing")
694694
if (valid == True) and (field_smoothing != b"none") :
695695
result_array += test_attr(field,v, "required",
696-
"fieldSmoothingParameters", np.string_)
696+
"fieldSmoothingParameters", np.bytes_)
697697
return(result_array)
698698

699699

@@ -827,19 +827,19 @@ def check_particles(f, iteration, v, extensionStates) :
827827
result_array += test_attr(species, v, "required",
828828
"particleShape", [np.single, np.double, np.longdouble])
829829
result_array += test_attr(species, v, "required",
830-
"currentDeposition", np.string_)
830+
"currentDeposition", np.bytes_)
831831
result_array += test_attr(species, v, "required",
832-
"particlePush", np.string_)
832+
"particlePush", np.bytes_)
833833
result_array += test_attr(species, v, "required",
834-
"particleInterpolation", np.string_)
834+
"particleInterpolation", np.bytes_)
835835

836836
# Check for the attributes associated with the particle smoothing
837837
result_array += test_attr(species, v, "required",
838-
"particleSmoothing", np.string_)
838+
"particleSmoothing", np.bytes_)
839839
valid, particle_smoothing = get_attr(species, "particleSmoothing")
840840
if valid == True and particle_smoothing != b"none":
841841
result_array += test_attr(species, v, "required",
842-
"particleSmoothingParameters", np.string_)
842+
"particleSmoothingParameters", np.bytes_)
843843

844844
# Check attributes of each record of the particle
845845
for record in list(species.keys()) :

openpmd_validator/createExamples_h5.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def get_basePath(f, iteration):
3737
-------
3838
A string with a in-file path.
3939
"""
40-
iteration_str = np.string_(str(iteration))
41-
return np.string_(f.attrs["basePath"]).replace(b"%T", iteration_str)
40+
iteration_str = np.bytes_(str(iteration))
41+
return np.bytes_(f.attrs["basePath"]).replace(b"%T", iteration_str)
4242

4343
def setup_base_path(f, iteration):
4444
"""
@@ -67,7 +67,7 @@ def get_software_dependencies():
6767
Returns the software dependencies of this script as a semicolon
6868
separated string.
6969
"""
70-
return np.string_(
70+
return np.bytes_(
7171
"python@{0}.{1}.{2};".format(
7272
sys.version_info.major,
7373
sys.version_info.minor,
@@ -92,25 +92,25 @@ def setup_root_attr(f):
9292
ext_list = [["ED-PIC", np.uint32(1)]]
9393

9494
# Required attributes
95-
f.attrs["openPMD"] = np.string_("1.1.0")
95+
f.attrs["openPMD"] = np.bytes_("1.1.0")
9696
f.attrs["openPMDextension"] = ext_list[0][1] # ED-PIC extension is used
97-
f.attrs["basePath"] = np.string_("/data/%T/")
98-
f.attrs["meshesPath"] = np.string_("meshes/")
99-
f.attrs["particlesPath"] = np.string_("particles/")
100-
f.attrs["iterationEncoding"] = np.string_("groupBased")
101-
f.attrs["iterationFormat"] = np.string_("/data/%T/")
97+
f.attrs["basePath"] = np.bytes_("/data/%T/")
98+
f.attrs["meshesPath"] = np.bytes_("meshes/")
99+
f.attrs["particlesPath"] = np.bytes_("particles/")
100+
f.attrs["iterationEncoding"] = np.bytes_("groupBased")
101+
f.attrs["iterationFormat"] = np.bytes_("/data/%T/")
102102

103103
# Recommended attributes
104-
f.attrs["author"] = np.string_("Axel Huebl <a.huebl@hzdr.de>")
105-
f.attrs["software"] = np.string_("openPMD Example Script")
106-
f.attrs["softwareVersion"] = np.string_("1.1.0.4")
104+
f.attrs["author"] = np.bytes_("Axel Huebl <a.huebl@hzdr.de>")
105+
f.attrs["software"] = np.bytes_("openPMD Example Script")
106+
f.attrs["softwareVersion"] = np.bytes_("1.1.0.4")
107107
f.attrs["softwareDependencies"] = get_software_dependencies()
108-
f.attrs["machine"] = np.string_(socket.gethostname())
109-
f.attrs["date"] = np.string_(
108+
f.attrs["machine"] = np.bytes_(socket.gethostname())
109+
f.attrs["date"] = np.bytes_(
110110
datetime.datetime.now(tzlocal()).strftime('%Y-%m-%d %H:%M:%S %z'))
111111

112112
# Optional
113-
f.attrs["comment"] = np.string_("This is a dummy file for test purposes.")
113+
f.attrs["comment"] = np.bytes_("This is a dummy file for test purposes.")
114114

115115

116116
def write_rho_cylindrical(meshes, mode0, mode1):
@@ -133,17 +133,17 @@ def write_rho_cylindrical(meshes, mode0, mode1):
133133
(The first axis corresponds to r, and the second axis corresponds to z)
134134
"""
135135
# Path to the rho meshes, within the h5py file
136-
full_rho_path = np.string_("rho")
136+
full_rho_path = np.bytes_("rho")
137137
meshes.create_dataset( full_rho_path, (3, mode0.shape[0], mode0.shape[1]), \
138138
dtype=np.float32)
139139
rho = meshes[full_rho_path]
140-
rho.attrs["comment"] = np.string_(
140+
rho.attrs["comment"] = np.bytes_(
141141
"Density of electrons in azimuthal decomposition")
142142

143143
# Create the dataset (cylindrical with azimuthal modes up to m=1)
144144
# The first axis has size 2m+1
145-
rho.attrs["geometry"] = np.string_("thetaMode")
146-
rho.attrs["geometryParameters"] = np.string_("m=1; imag=+")
145+
rho.attrs["geometry"] = np.bytes_("thetaMode")
146+
rho.attrs["geometryParameters"] = np.bytes_("m=1; imag=+")
147147

148148
# Add information on the units of the data
149149
rho.attrs["unitSI"] = np.float64(1.0)
@@ -160,7 +160,7 @@ def write_rho_cylindrical(meshes, mode0, mode1):
160160
rho.attrs["gridGlobalOffset"] = np.array([0.0, 0.0], dtype=np.float32)
161161
rho.attrs["position"] = np.array([0.0, 0.0], dtype=np.float32)
162162
rho.attrs["gridUnitSI"] = np.float64(1.0)
163-
rho.attrs["dataOrder"] = np.string_("C")
163+
rho.attrs["dataOrder"] = np.bytes_("C")
164164
rho.attrs["axisLabels"] = np.array([b"r",b"z"])
165165

166166
# Add specific information for PIC simulations
@@ -199,11 +199,11 @@ def write_b_2d_cartesian(meshes, data_ez):
199199
B.create_dataset(b"z", data_ez.shape, dtype=np.float32)
200200

201201
# Write the common metadata for the group
202-
B.attrs["geometry"] = np.string_("cartesian")
202+
B.attrs["geometry"] = np.bytes_("cartesian")
203203
B.attrs["gridSpacing"] = np.array([1.0, 1.0], dtype=np.float32) # dx, dy
204204
B.attrs["gridGlobalOffset"] = np.array([0.0, 0.0], dtype=np.float32)
205205
B.attrs["gridUnitSI"] = np.float64(1.0)
206-
B.attrs["dataOrder"] = np.string_("C")
206+
B.attrs["dataOrder"] = np.bytes_("C")
207207
B.attrs["axisLabels"] = np.array([b"x",b"y"])
208208
B.attrs["unitDimension"] = \
209209
np.array([0.0, 1.0, -2.0, -1.0, 0.0, 0.0, 0.0 ], dtype=np.float64)
@@ -261,11 +261,11 @@ def write_e_2d_cartesian(meshes, data_ex, data_ey, data_ez ):
261261
E.create_dataset(b"z", data_ez.shape, dtype=np.float32)
262262

263263
# Write the common metadata for the group
264-
E.attrs["geometry"] = np.string_("cartesian")
264+
E.attrs["geometry"] = np.bytes_("cartesian")
265265
E.attrs["gridSpacing"] = np.array([1.0, 1.0], dtype=np.float32) # dx, dy
266266
E.attrs["gridGlobalOffset"] = np.array([0.0, 0.0], dtype=np.float32)
267267
E.attrs["gridUnitSI"] = np.float64(1.0)
268-
E.attrs["dataOrder"] = np.string_("C")
268+
E.attrs["dataOrder"] = np.bytes_("C")
269269
E.attrs["axisLabels"] = np.array([b"x",b"y"])
270270
E.attrs["unitDimension"] = \
271271
np.array([1.0, 1.0, -3.0, -1.0, 0.0, 0.0, 0.0 ], dtype=np.float64)
@@ -307,9 +307,9 @@ def add_EDPIC_attr_meshes(field):
307307
and Dataset for scalar meshes)
308308
309309
"""
310-
field.attrs["fieldSmoothing"] = np.string_("none")
310+
field.attrs["fieldSmoothing"] = np.bytes_("none")
311311
# field.attrs["fieldSmoothingParameters"] = \
312-
# np.string_("period=10;numPasses=4;compensator=true")
312+
# np.bytes_("period=10;numPasses=4;compensator=true")
313313

314314

315315
def add_EDPIC_attr_particles(particle):
@@ -324,13 +324,13 @@ def add_EDPIC_attr_particles(particle):
324324
325325
"""
326326
particle.attrs["particleShape"] = 3.0
327-
particle.attrs["currentDeposition"] = np.string_("Esirkepov")
328-
# particle.attrs["currentDepositionParameters"] = np.string_("")
329-
particle.attrs["particlePush"] = np.string_("Boris")
330-
particle.attrs["particleInterpolation"] = np.string_("uniform")
331-
particle.attrs["particleSmoothing"] = np.string_("none")
327+
particle.attrs["currentDeposition"] = np.bytes_("Esirkepov")
328+
# particle.attrs["currentDepositionParameters"] = np.bytes_("")
329+
particle.attrs["particlePush"] = np.bytes_("Boris")
330+
particle.attrs["particleInterpolation"] = np.bytes_("uniform")
331+
particle.attrs["particleSmoothing"] = np.bytes_("none")
332332
# particle.attrs["particleSmoothingParameters"] = \
333-
# np.string_("period=1;numPasses=2;compensator=false")
333+
# np.bytes_("period=1;numPasses=2;compensator=false")
334334

335335

336336
def write_meshes(f, iteration):
@@ -339,15 +339,15 @@ def write_meshes(f, iteration):
339339
meshes = f[full_meshes_path]
340340

341341
# Extension: Additional attributes for ED-PIC
342-
meshes.attrs["fieldSolver"] = np.string_("Yee")
342+
meshes.attrs["fieldSolver"] = np.bytes_("Yee")
343343
meshes.attrs["fieldBoundary"] = np.array(
344344
[b"periodic", b"periodic", b"open", b"open"])
345345
meshes.attrs["particleBoundary"] = np.array(
346346
[b"periodic", b"periodic", b"absorbing", b"absorbing"])
347-
meshes.attrs["currentSmoothing"] = np.string_("Binomial")
347+
meshes.attrs["currentSmoothing"] = np.bytes_("Binomial")
348348
meshes.attrs["currentSmoothingParameters"] = \
349-
np.string_("period=1;numPasses=2;compensator=false")
350-
meshes.attrs["chargeCorrection"] = np.string_("none")
349+
np.bytes_("period=1;numPasses=2;compensator=false")
350+
meshes.attrs["chargeCorrection"] = np.bytes_("none")
351351

352352
# (Here the data is randomly generated, but in an actual simulation,
353353
# this would be replaced by the simulation data.)
@@ -375,7 +375,7 @@ def write_particles(f, iteration):
375375

376376
globalNumParticles = 128 # example number of all particles
377377

378-
electrons.attrs["comment"] = np.string_("My first electron species")
378+
electrons.attrs["comment"] = np.bytes_("My first electron species")
379379

380380
# Extension: ED-PIC Attributes
381381
# required

0 commit comments

Comments
 (0)