Skip to content

Commit

Permalink
Rewrite to use sample data, add (currently failing) test
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jun 7, 2024
1 parent cb118c7 commit 46b2f00
Showing 1 changed file with 57 additions and 18 deletions.
75 changes: 57 additions & 18 deletions examples/basic/ex07_convert_numpy_openpmd.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import mala

from mala.datahandling.data_repo import data_path
import os

parameters = mala.Parameters()

# First, convert from Numpy files to openPMD.

data_converter = mala.DataConverter(parameters)

for snapshot in range(2):
data_converter.add_snapshot(
descriptor_input_type="openpmd",
descriptor_input_path="Be_shuffled{}.in.bp4".format(snapshot),
target_input_type='openpmd',
target_input_path="Be_shuffled{}.out.bp4".format(snapshot),
descriptor_input_type="numpy",
descriptor_input_path=os.path.join(
data_path, "Be_snapshot{}.in.npy".format(snapshot)
),
target_input_type="numpy",
target_input_path=os.path.join(
data_path, "Be_snapshot{}.out.npy".format(snapshot)
),
additional_info_input_type=None,
additional_info_input_path=None,
target_units=None,
Expand All @@ -18,26 +28,22 @@
descriptor_save_path="./",
target_save_path="./",
additional_info_save_path="./",
naming_scheme="Be_snapshot*.bp4",
naming_scheme="converted_from_numpy_*.bp4",
descriptor_calculation_kwargs={"working_directory": "./"},
)

data_converter.convert_snapshots(
descriptor_save_path="./",
target_save_path="./",
additional_info_save_path="./",
naming_scheme="Be_snapshot*.npy",
descriptor_calculation_kwargs={"working_directory": "./"},
)
# Convert those files back to Numpy to verify the data stays the same.

data_converter = mala.DataConverter(parameters)

for snapshot in range(2):
data_converter.add_snapshot(
descriptor_input_type="numpy",
descriptor_input_path="Be_snapshot{}.in.npy".format(snapshot),
target_input_type='numpy',
target_input_path="Be_snapshot{}.out.npy".format(snapshot),
descriptor_input_type="openpmd",
descriptor_input_path="converted_from_numpy_{}.in.bp4".format(
snapshot
),
target_input_type="openpmd",
target_input_path="converted_from_numpy_{}.out.bp4".format(snapshot),
additional_info_input_type=None,
additional_info_input_path=None,
target_units=None,
Expand All @@ -47,14 +53,47 @@
descriptor_save_path="./",
target_save_path="./",
additional_info_save_path="./",
naming_scheme="Be_snapshot_from_numpy*.bp4",
naming_scheme="verify_against_original_numpy_data_*.npy",
descriptor_calculation_kwargs={"working_directory": "./"},
)

for snapshot in range(2):
for i_o in ["in", "out"]:
original = os.path.join(
data_path, "Be_snapshot{}.{}.npy".format(snapshot, i_o)
)
roundtrip = "verify_against_original_numpy_data_{}.{}.npy".format(
snapshot, i_o
)
import numpy as np

original_a = np.load(original)
roundtrip_a = np.load(roundtrip)
np.testing.assert_allclose(original_a, roundtrip_a)

# Now, convert some openPMD data back to Numpy.

data_converter = mala.DataConverter(parameters)

for snapshot in range(2):
data_converter.add_snapshot(
descriptor_input_type="openpmd",
descriptor_input_path=os.path.join(
data_path, "Be_snapshot{}.in.h5".format(snapshot)
),
target_input_type="openpmd",
target_input_path=os.path.join(
data_path, "Be_snapshot{}.out.h5".format(snapshot)
),
additional_info_input_type=None,
additional_info_input_path=None,
target_units=None,
)

data_converter.convert_snapshots(
descriptor_save_path="./",
target_save_path="./",
additional_info_save_path="./",
naming_scheme="Be_snapshot_from_numpy*.npy",
naming_scheme="converted_from_openpmd_*.npy",
descriptor_calculation_kwargs={"working_directory": "./"},
)

0 comments on commit 46b2f00

Please sign in to comment.