Skip to content

Commit 6762bdc

Browse files
authored
Merge pull request #78 from ax3l/fix-np-2.0-fixed-size-str-2.0.X
Numpy 2.0: `np.string_` -> `np.bytes_`
2 parents b59051f + b5a48e2 commit 6762bdc

File tree

2 files changed

+78
-78
lines changed

2 files changed

+78
-78
lines changed

openpmd_validator/check_h5.py

Lines changed: 41 additions & 41 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,18 +404,18 @@ 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]+$")
408-
result_array += test_attr(f, v, "required", "basePath", np.string_, "^\/data\/\%T\/$")
409-
result_array += test_attr(f, v, "required", "iterationEncoding", np.string_, "^groupBased|fileBased$")
410-
result_array += test_attr(f, v, "required", "iterationFormat", np.string_)
407+
result_array += test_attr(f, v, "required", "openPMD", np.bytes_, "^[0-9]+\.[0-9]+\.[0-9]+$")
408+
result_array += test_attr(f, v, "required", "basePath", np.bytes_, "^\/data\/\%T\/$")
409+
result_array += test_attr(f, v, "required", "iterationEncoding", np.bytes_, "^groupBased|fileBased$")
410+
result_array += test_attr(f, v, "required", "iterationFormat", np.bytes_)
411411

412412
# optional but required for extensions
413-
result_array += test_attr(f, v, "optional", "openPMDextension", np.string_,
413+
result_array += test_attr(f, v, "optional", "openPMDextension", np.bytes_,
414414
# allowed are a-Z 0-9 - ; (but no spaces!)
415415
"^[a-zA-Z0-9\-;]+$")
416416
# optional but required for data
417-
result_array += test_attr(f, v, "optional", "meshesPath", np.string_, "^.*\/$")
418-
result_array += test_attr(f, v, "optional", "particlesPath", np.string_, "^.*\/$")
417+
result_array += test_attr(f, v, "optional", "meshesPath", np.bytes_, "^.*\/$")
418+
result_array += test_attr(f, v, "optional", "particlesPath", np.bytes_, "^.*\/$")
419419

420420
# groupBased iteration encoding needs to match basePath
421421
if result_array[0] == 0 :
@@ -426,17 +426,17 @@ def check_root_attr(f, v):
426426
result_array += np.array([1,0])
427427

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

436436
# optional
437-
result_array += test_attr(f, v, "optional", "softwareDependencies", np.string_)
438-
result_array += test_attr(f, v, "optional", "machine", np.string_)
439-
result_array += test_attr(f, v, "optional", "comment", np.string_)
437+
result_array += test_attr(f, v, "optional", "softwareDependencies", np.bytes_)
438+
result_array += test_attr(f, v, "optional", "machine", np.bytes_)
439+
result_array += test_attr(f, v, "optional", "comment", np.bytes_)
440440

441441
return(result_array)
442442

@@ -619,18 +619,18 @@ def check_meshes(f, iteration, v, extensionStates):
619619
result_array += test_attr(field, v, "required",
620620
"gridUnitDimension", np.ndarray, np.double)
621621
result_array += test_attr(field, v, "required",
622-
"axisLabels", np.ndarray, np.string_)
622+
"axisLabels", np.ndarray, np.bytes_)
623623
# Specific check for geometry
624-
geometry_test = test_attr(field, v, "required", "geometry", np.string_)
624+
geometry_test = test_attr(field, v, "required", "geometry", np.bytes_)
625625
result_array += geometry_test
626626
# geometryParameters is required when using thetaMode
627627
if geometry_test[0] == 0 and field.attrs["geometry"] == b"thetaMode" :
628628
result_array += test_attr(field, v, "required",
629-
"geometryParameters", np.string_)
629+
"geometryParameters", np.bytes_)
630630
# otherwise it is optional
631631
else :
632632
result_array += test_attr(field, v, "optional",
633-
"geometryParameters", np.string_)
633+
"geometryParameters", np.bytes_)
634634

635635
# Attributes of the record's components
636636
if is_scalar_record(field) : # If the record is a scalar field
@@ -650,45 +650,45 @@ def check_meshes(f, iteration, v, extensionStates):
650650
if extensionStates['ED-PIC'] and len(list_meshes) > 0:
651651
# Check the attributes associated with the field solver
652652
result_array += test_attr(f[full_meshes_path], v, "required",
653-
"fieldSolver", np.string_)
653+
"fieldSolver", np.bytes_)
654654
valid, field_solver = get_attr(f[full_meshes_path], "fieldSolver")
655655
if (valid == True) and (field_solver in ["other", "GPSTD"]) :
656656
result_array += test_attr(f[full_meshes_path], v, "required",
657-
"fieldSolverParameters", np.string_)
657+
"fieldSolverParameters", np.bytes_)
658658

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

667667
# Check the attributes associated with the current smoothing
668668
result_array += test_attr(f[full_meshes_path], v, "required",
669-
"currentSmoothing", np.string_)
669+
"currentSmoothing", np.bytes_)
670670
valid, current_smoothing = get_attr(f[full_meshes_path], "currentSmoothing")
671671
if (valid == True) and (current_smoothing != b"none") :
672672
result_array += test_attr(f[full_meshes_path], v, "required",
673-
"currentSmoothingParameters", np.string_)
673+
"currentSmoothingParameters", np.bytes_)
674674

675675
# Check the attributes associated with the charge conservation
676676
result_array += test_attr(f[full_meshes_path], v, "required",
677-
"chargeCorrection", np.string_)
677+
"chargeCorrection", np.bytes_)
678678
valid, charge_correction = get_attr(f[full_meshes_path], "chargeCorrection")
679679
if valid == True and charge_correction != b"none":
680680
result_array += test_attr(f[full_meshes_path], v, "required",
681-
"chargeCorrectionParameters", np.string_)
681+
"chargeCorrectionParameters", np.bytes_)
682682

683683
# Check for the attributes of each record
684684
for field_name in list_meshes :
685685
field = f[full_meshes_path + field_name.encode('ascii')]
686686
result_array + test_attr(field, v, "required",
687-
"fieldSmoothing", np.string_)
687+
"fieldSmoothing", np.bytes_)
688688
valid, field_smoothing = get_attr(field, "fieldSmoothing")
689689
if (valid == True) and (field_smoothing != b"none") :
690690
result_array += test_attr(field,v, "required",
691-
"fieldSmoothingParameters", np.string_)
691+
"fieldSmoothingParameters", np.bytes_)
692692

693693
# Check the attributes in the SpeciesType extension
694694
if extensionStates['SpeciesType'] :
@@ -697,7 +697,7 @@ def check_meshes(f, iteration, v, extensionStates):
697697
field = f[full_meshes_path + field_name.encode('ascii')]
698698
# allowed are a-Z 0-9 - ; : (but no spaces!)
699699
result_array += test_attr(field, v, "optional",
700-
"speciesType", np.string_,
700+
"speciesType", np.bytes_,
701701
"^[a-zA-Z0-9\-;:]+$")
702702

703703
return(result_array)
@@ -834,33 +834,33 @@ def check_particles(f, iteration, v, extensionStates) :
834834
result_array += test_attr(species, v, "required",
835835
"particleShape", [np.single, np.double, np.longdouble])
836836
result_array += test_attr(species, v, "required",
837-
"currentDeposition", np.string_)
837+
"currentDeposition", np.bytes_)
838838
result_array += test_attr(species, v, "required",
839-
"particlePush", np.string_)
839+
"particlePush", np.bytes_)
840840
result_array += test_attr(species, v, "required",
841-
"particleInterpolation", np.string_)
841+
"particleInterpolation", np.bytes_)
842842

843843
# Check for the attributes associated with the particle boundaries
844844
result_array += test_attr(species, v, "required",
845-
"particleBoundary", np.ndarray, np.string_)
845+
"particleBoundary", np.ndarray, np.bytes_)
846846
valid, particle_boundary = get_attr(species, "particleBoundary")
847847
if (valid == True) and (np.any(particle_boundary == b"other")) :
848848
result_array += test_attr(species, v, "required",
849-
"particleBoundaryParameters", np.ndarray, np.string_)
849+
"particleBoundaryParameters", np.ndarray, np.bytes_)
850850

851851
# Check for the attributes associated with the particle smoothing
852852
result_array += test_attr(species, v, "required",
853-
"particleSmoothing", np.string_)
853+
"particleSmoothing", np.bytes_)
854854
valid, particle_smoothing = get_attr(species, "particleSmoothing")
855855
if valid == True and particle_smoothing != b"none":
856856
result_array += test_attr(species, v, "required",
857-
"particleSmoothingParameters", np.string_)
857+
"particleSmoothingParameters", np.bytes_)
858858

859859
# Check the attributes associated with the SpeciesType extension
860860
if extensionStates['SpeciesType'] :
861861
# allowed are a-Z 0-9 - ; : (but no spaces!)
862862
result_array += test_attr(species, v, "optional", "speciesType",
863-
np.string_, "^[a-zA-Z0-9\-;:]+$")
863+
np.bytes_, "^[a-zA-Z0-9\-;:]+$")
864864

865865
# Check attributes of each record of the particle
866866
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", "SpeciesType"]
9393

9494
# Required attributes
95-
f.attrs["openPMD"] = np.string_("2.0.0")
96-
f.attrs["openPMDextension"] = np.string_(";".join(ext_list))
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/")
95+
f.attrs["openPMD"] = np.bytes_("2.0.0")
96+
f.attrs["openPMDextension"] = np.bytes_(";".join(ext_list))
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_("2.0.0.0")
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_("2.0.0.0")
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,18 +133,18 @@ 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")
142-
rho.attrs["speciesType"] = np.string_("electron")
142+
rho.attrs["speciesType"] = np.bytes_("electron")
143143

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

149149
# Add information on the units of the data
150150
rho.attrs["unitSI"] = np.float64(1.0)
@@ -203,7 +203,7 @@ def write_b_2d_cartesian(meshes, data_ez):
203203
B.create_dataset(b"z", data_ez.shape, dtype=np.float32)
204204

205205
# Write the common metadata for the group
206-
B.attrs["geometry"] = np.string_("cartesian")
206+
B.attrs["geometry"] = np.bytes_("cartesian")
207207
B.attrs["gridSpacing"] = np.array([1.0, 1.0], dtype=np.float32) # dx, dy
208208
B.attrs["gridGlobalOffset"] = np.array([0.0, 0.0], dtype=np.float32)
209209
B.attrs["gridUnitSI"] = np.array([1.0, 1.0], dtype=np.float64)
@@ -268,7 +268,7 @@ def write_e_2d_cartesian(meshes, data_ex, data_ey, data_ez ):
268268
E.create_dataset(b"z", data_ez.shape, dtype=np.float32)
269269

270270
# Write the common metadata for the group
271-
E.attrs["geometry"] = np.string_("cartesian")
271+
E.attrs["geometry"] = np.bytes_("cartesian")
272272
E.attrs["gridSpacing"] = np.array([1.0, 1.0], dtype=np.float32) # dx, dy
273273
E.attrs["gridGlobalOffset"] = np.array([0.0, 0.0], dtype=np.float32)
274274
E.attrs["gridUnitSI"] = np.array([1.0, 1.0], dtype=np.float64 )
@@ -317,9 +317,9 @@ def add_EDPIC_attr_meshes(field):
317317
and Dataset for scalar meshes)
318318
319319
"""
320-
field.attrs["fieldSmoothing"] = np.string_("none")
320+
field.attrs["fieldSmoothing"] = np.bytes_("none")
321321
# field.attrs["fieldSmoothingParameters"] = \
322-
# np.string_("period=10;numPasses=4;compensator=true")
322+
# np.bytes_("period=10;numPasses=4;compensator=true")
323323

324324

325325
def add_EDPIC_attr_particles(particle):
@@ -333,17 +333,17 @@ def add_EDPIC_attr_particles(particle):
333333
The group of the particle that gets additional attributes.
334334
335335
"""
336-
particle.attrs["speciesType"] = np.string_("electron")
336+
particle.attrs["speciesType"] = np.bytes_("electron")
337337
particle.attrs["particleShape"] = 3.0
338-
particle.attrs["currentDeposition"] = np.string_("Esirkepov")
339-
# particle.attrs["currentDepositionParameters"] = np.string_("")
340-
particle.attrs["particlePush"] = np.string_("Boris")
341-
particle.attrs["particleInterpolation"] = np.string_("uniform")
338+
particle.attrs["currentDeposition"] = np.bytes_("Esirkepov")
339+
# particle.attrs["currentDepositionParameters"] = np.bytes_("")
340+
particle.attrs["particlePush"] = np.bytes_("Boris")
341+
particle.attrs["particleInterpolation"] = np.bytes_("uniform")
342342
particle.attrs["particleBoundary"] = np.array(
343343
[b"periodic", b"periodic", b"absorbing", b"absorbing"])
344-
particle.attrs["particleSmoothing"] = np.string_("none")
344+
particle.attrs["particleSmoothing"] = np.bytes_("none")
345345
# particle.attrs["particleSmoothingParameters"] = \
346-
# np.string_("period=1;numPasses=2;compensator=false")
346+
# np.bytes_("period=1;numPasses=2;compensator=false")
347347

348348

349349
def write_meshes(f, iteration):
@@ -352,13 +352,13 @@ def write_meshes(f, iteration):
352352
meshes = f[full_meshes_path]
353353

354354
# Extension: Additional attributes for ED-PIC
355-
meshes.attrs["fieldSolver"] = np.string_("Yee")
355+
meshes.attrs["fieldSolver"] = np.bytes_("Yee")
356356
meshes.attrs["fieldBoundary"] = np.array(
357357
[b"periodic", b"periodic", b"open", b"open"])
358-
meshes.attrs["currentSmoothing"] = np.string_("Binomial")
358+
meshes.attrs["currentSmoothing"] = np.bytes_("Binomial")
359359
meshes.attrs["currentSmoothingParameters"] = \
360-
np.string_("period=1;numPasses=2;compensator=false")
361-
meshes.attrs["chargeCorrection"] = np.string_("none")
360+
np.bytes_("period=1;numPasses=2;compensator=false")
361+
meshes.attrs["chargeCorrection"] = np.bytes_("none")
362362

363363
# (Here the data is randomly generated, but in an actual simulation,
364364
# this would be replaced by the simulation data.)
@@ -386,7 +386,7 @@ def write_particles(f, iteration):
386386

387387
globalNumParticles = 128 # example number of all particles
388388

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

391391
# Extension: ED-PIC Attributes
392392
# required

0 commit comments

Comments
 (0)