Skip to content

Commit

Permalink
Merge pull request #647 from ISISNeutronMuon/chi/unit-test-reference-…
Browse files Browse the repository at this point in the history
…results

Update analysis and converter unit tests so they check against reference values
  • Loading branch information
ChiCheng45 authored Jan 31, 2025
2 parents afe78fe + ebbc436 commit f563fe7
Show file tree
Hide file tree
Showing 169 changed files with 611 additions and 216 deletions.
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/Converters/DCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def run_step(self, index):
conf.fold_coordinates()

# The current time.
time = (index + 1) * self.configuration["time_step"]["value"]
time = index * self.configuration["time_step"]["value"]

# Store a snapshot of the current configuration in the output trajectory.
self._trajectory.dump_configuration(
Expand Down
6 changes: 2 additions & 4 deletions MDANSE/Tests/UnitTests/Analysis/test_average_structure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import tempfile
import os
from os import path
Expand All @@ -8,11 +7,10 @@
from MDANSE.Framework.Jobs.IJob import IJob


sys.setrecursionlimit(100000)
short_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"short_trajectory_after_changes.mdt",
)

Expand All @@ -32,7 +30,7 @@ def trajectory():


@pytest.mark.parametrize("output_unit,output_format", inputs)
def test_temperature(trajectory, output_unit, output_format):
def test_avg_structure(trajectory, output_unit, output_format):
temp_name = tempfile.mktemp()
parameters = {}
parameters["frames"] = (0, 10, 1)
Expand Down
79 changes: 58 additions & 21 deletions MDANSE/Tests/UnitTests/Analysis/test_dynamics.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import sys
import tempfile
import os
from os import path

import h5py
import numpy as np
import pytest

from MDANSE.Framework.InputData.HDFTrajectoryInputData import HDFTrajectoryInputData
from MDANSE.Framework.Jobs.IJob import IJob


sys.setrecursionlimit(100000)
short_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"short_trajectory_after_changes.mdt",
)

mdmc_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"Ar_mdmc_h5md.h5",
)

com_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"com_trajectory.mdt",
)
result_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Results",
)


@pytest.mark.parametrize(
Expand All @@ -55,6 +58,20 @@ def test_vacf(interp_order, normalise):
vacf.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")

if normalise:
fname = f"vacf_{interp_order}_normalised.mda"
else:
fname = f"vacf_{interp_order}.mda"

result_file = os.path.join(result_dir, fname)

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/vacf_Cu"], desired["/vacf_Cu"])
np.testing.assert_array_almost_equal(actual["/vacf_S"], desired["/vacf_S"])
np.testing.assert_array_almost_equal(actual["/vacf_Sb"], desired["/vacf_Sb"])
np.testing.assert_array_almost_equal(actual["/vacf_total"], desired["/vacf_total"])

os.remove(temp_name + ".mda")
assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
Expand All @@ -72,6 +89,19 @@ def test_pps():
pps.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")

result_file = os.path.join(result_dir, "pps.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/pacf_Cu"], desired["/pacf_Cu"])
np.testing.assert_array_almost_equal(actual["/pacf_S"], desired["/pacf_S"])
np.testing.assert_array_almost_equal(actual["/pacf_Sb"], desired["/pacf_Sb"])
np.testing.assert_array_almost_equal(actual["/pacf_total"], desired["/pacf_total"])
np.testing.assert_array_almost_equal(actual["/pps_Cu"], desired["/pps_Cu"])
np.testing.assert_array_almost_equal(actual["/pps_S"], desired["/pps_S"])
np.testing.assert_array_almost_equal(actual["/pps_Sb"], desired["/pps_Sb"])
np.testing.assert_array_almost_equal(actual["/pps_total"], desired["/pps_total"])

os.remove(temp_name + ".mda")
assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
Expand Down Expand Up @@ -99,7 +129,7 @@ def parameters():
},
)
parameters["q_values"] = (0.0, 10.0, 0.1)
parameters["r_values"] = (0.0, 0.5, 0.01)
parameters["r_values"] = (0.0, 0.9, 0.01)
parameters["per_axis"] = False
parameters["reference_direction"] = (0, 0, 1)
parameters["instrument_resolution"] = ("Gaussian", {"sigma": 1.0, "mu": 0.0})
Expand All @@ -113,37 +143,44 @@ def parameters():

total_list = []

for tp in [short_traj, mdmc_traj, com_traj]:
for tp in [("short_traj", short_traj), ("mdmc_traj", mdmc_traj), ("com_traj", com_traj)]:
for jt in [
# "AngularCorrelation",
# "GeneralAutoCorrelationFunction",
"CurrentCorrelationFunction",
"DensityOfStates",
"MeanSquareDisplacement",
"VelocityAutoCorrelationFunction",
"VanHoveFunctionDistinct",
"VanHoveFunctionSelf",
("DensityOfStates", ["dos", "vacf"]),
("MeanSquareDisplacement", ["msd"]),
("VelocityAutoCorrelationFunction", ["vacf"]),
("VanHoveFunctionDistinct", ["g(r,t)"]),
("VanHoveFunctionSelf", ["g(r,t)"]),
# "OrderParameter",
"PositionAutoCorrelationFunction",
("PositionAutoCorrelationFunction", ["pacf"]),
("PositionPowerSpectrum", ["pacf", "pps"]),
]:
for rm in [("single-core", 1), ("multicore", -4)]:
for of in ["MDAFormat", "TextFormat"]:
total_list.append((tp, jt, rm, of))


@pytest.mark.parametrize("traj_path,job_type,running_mode,output_format", total_list)
@pytest.mark.parametrize("traj_info,job_info,running_mode,output_format", total_list)
def test_dynamics_analysis(
parameters, traj_path, job_type, running_mode, output_format
parameters, traj_info, job_info, running_mode, output_format
):
temp_name = tempfile.mktemp()
parameters["trajectory"] = traj_path
parameters["trajectory"] = traj_info[1]
parameters["running_mode"] = running_mode
parameters["output_files"] = (temp_name, (output_format,), "INFO")
job = IJob.create(job_type)
job = IJob.create(job_info[0])
job.run(parameters, status=True)
if output_format == "MDAFormat":
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")
result_file = os.path.join(result_dir, f"dynamics_analysis_{traj_info[0]}_{job_info[0]}.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
keys = [i for i in desired.keys() if any([j in i for j in job_info[1]])]
for key in keys:
np.testing.assert_array_almost_equal(actual[f"/{key}"], desired[f"/{key}"])

os.remove(temp_name + ".mda")
elif output_format == "TextFormat":
assert path.exists(temp_name + "_text.tar")
Expand Down
56 changes: 50 additions & 6 deletions MDANSE/Tests/UnitTests/Analysis/test_infrared.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import sys
import tempfile
import os
from os import path

from MDANSE.Framework.Jobs.IJob import IJob
import h5py
import numpy as np

from MDANSE.Framework.Jobs.IJob import IJob

sys.setrecursionlimit(100000)

short_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"named_molecules.mdt",
)
result_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Results",
)


def test_dacf_analysis():
Expand All @@ -23,13 +28,32 @@ def test_dacf_analysis():
parameters["output_files"] = (temp_name, ("MDAFormat",), "INFO")
parameters["running_mode"] = ("single-core", 1)
parameters["trajectory"] = short_traj
parameters["atom_charges"] = "{}"
parameters["atom_charges"] = (
'{"0": 0.72, "1": -0.36, "2": -0.36, "3": 0.72, "4": -0.36, '
'"5": -0.36, "6": 0.72, "7": -0.36, "8": -0.36, "9": 0.72, '
'"10": -0.36, "11": -0.36, "12": 0.72, "13": -0.36, "14": -0.36, '
'"15": 0.72, "16": -0.36, "17": -0.36, "18": 0.72, "19": -0.36, '
'"20": -0.36, "21": 0.72, "22": -0.36, "23": -0.36, "24": 0.72, '
'"25": -0.36, "26": -0.36, "27": 0.72, "28": -0.36, "29": -0.36, '
'"30": 0.72, "31": -0.36, "32": -0.36, "33": 0.72, "34": -0.36, '
'"35": -0.36, "36": 0.72, "37": -0.36, "38": -0.36, "39": 0.72, '
'"40": -0.36, "41": -0.36, "42": 0.72, "43": -0.36, "44": -0.36, '
'"45": 0.72, "46": -0.36, "47": -0.36, "48": 0.72, "49": -0.36, '
'"50": -0.36, "51": 0.72, "52": -0.36, "53": -0.36, "54": 0.72, '
'"55": -0.36, "56": -0.36, "57": 0.72, "58": -0.36, "59": -0.36}'
)
parameters["molecule_name"] = "C1_O2"
job = IJob.create("DipoleAutoCorrelationFunction")
job.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")
result_file = os.path.join(result_dir, "dacf_analysis.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/dacf"], desired["/dacf"])

os.remove(temp_name + ".mda")

assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
os.remove(temp_name + ".log")
Expand All @@ -44,13 +68,33 @@ def test_ir_analysis():
parameters["derivative_order"] = 3
parameters["running_mode"] = ("single-core", 1)
parameters["trajectory"] = short_traj
parameters["atom_charges"] = "{}"
parameters["atom_charges"] = (
'{"0": 0.72, "1": -0.36, "2": -0.36, "3": 0.72, "4": -0.36, '
'"5": -0.36, "6": 0.72, "7": -0.36, "8": -0.36, "9": 0.72, '
'"10": -0.36, "11": -0.36, "12": 0.72, "13": -0.36, "14": -0.36, '
'"15": 0.72, "16": -0.36, "17": -0.36, "18": 0.72, "19": -0.36, '
'"20": -0.36, "21": 0.72, "22": -0.36, "23": -0.36, "24": 0.72, '
'"25": -0.36, "26": -0.36, "27": 0.72, "28": -0.36, "29": -0.36, '
'"30": 0.72, "31": -0.36, "32": -0.36, "33": 0.72, "34": -0.36, '
'"35": -0.36, "36": 0.72, "37": -0.36, "38": -0.36, "39": 0.72, '
'"40": -0.36, "41": -0.36, "42": 0.72, "43": -0.36, "44": -0.36, '
'"45": 0.72, "46": -0.36, "47": -0.36, "48": 0.72, "49": -0.36, '
'"50": -0.36, "51": 0.72, "52": -0.36, "53": -0.36, "54": 0.72, '
'"55": -0.36, "56": -0.36, "57": 0.72, "58": -0.36, "59": -0.36}'
)
parameters["molecule_name"] = "C1_O2"
job = IJob.create("Infrared")
job.run(parameters, status=True)
assert path.exists(temp_name + ".mda")
assert path.isfile(temp_name + ".mda")
result_file = os.path.join(result_dir, "ir_analysis.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/ddacf"], desired["/ddacf"])
np.testing.assert_array_almost_equal(actual["/ir"], desired["/ir"])

os.remove(temp_name + ".mda")

assert path.exists(temp_name + ".log")
assert path.isfile(temp_name + ".log")
os.remove(temp_name + ".log")
5 changes: 1 addition & 4 deletions MDANSE/Tests/UnitTests/Analysis/test_mcstas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import tempfile
import os
from os import path
Expand All @@ -7,12 +6,10 @@
from MDANSE.Framework.Jobs.IJob import IJob


sys.setrecursionlimit(100000)

short_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"short_trajectory_after_changes.mdt",
)

Expand Down
26 changes: 17 additions & 9 deletions MDANSE/Tests/UnitTests/Analysis/test_mdmc_h5md.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import tempfile
import os
import pytest
Expand All @@ -9,13 +8,17 @@
from MDANSE.Framework.Jobs.IJob import IJob


sys.setrecursionlimit(100000)
short_traj = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Data",
"Converted",
"Ar_mdmc_h5md.h5",
)
result_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"..",
"Results",
)


@pytest.fixture(scope="module")
Expand All @@ -25,22 +28,27 @@ def trajectory():


@pytest.mark.parametrize("interp_order", [1, 3])
def test_temperature_nonzero(trajectory, interp_order):
def test_h5md_temperature(trajectory, interp_order):
pos = trajectory._data.coordinates(0)
print(f"Coordinates span: {pos.min()}, {pos.max()}")
print(f"Trajectory length: {len(trajectory.trajectory)}")
print(f"Positions array shape: {trajectory.trajectory.variable('position').shape}")
temp_name = tempfile.mktemp()
parameters = {}
parameters["frames"] = (0, 10, 1)
parameters["frames"] = (0, 39, 1)
parameters["interpolation_order"] = interp_order
parameters["output_files"] = (temp_name, ("MDAFormat",), "INFO")
parameters["running_mode"] = ("single-core",)
parameters["trajectory"] = short_traj
temp = IJob.create("Temperature")
temp.run(parameters, status=True)
with h5py.File(temp_name + ".mda") as results:
print(results.keys())
temperature = np.array(results["/temperature"])
result_file = os.path.join(
result_dir, f"h5md_temperature_{interp_order}.mda")

with h5py.File(temp_name + ".mda") as actual, h5py.File(result_file) as desired:
np.testing.assert_array_almost_equal(actual["/kinetic_energy"], desired["/kinetic_energy"])
np.testing.assert_array_almost_equal(actual["/temperature"], desired["/temperature"])
np.testing.assert_array_almost_equal(actual["/avg_kinetic_energy"], desired["/avg_kinetic_energy"])
np.testing.assert_array_almost_equal(actual["/avg_temperature"], desired["/avg_temperature"])

os.remove(temp_name + ".mda")
assert np.all(temperature > 0.0)
Loading

0 comments on commit f563fe7

Please sign in to comment.