diff --git a/Makefile b/Makefile index 87f6939..9a148de 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ docs: PDOC_ALLOW_EXEC=0 ${PYTHON} -m pdoc ./pyMBE.py -o ./documentation --docformat google unit_tests: + ${PYTHON} testsuite/serialization_test.py ${PYTHON} testsuite/lj_tests.py ${PYTHON} testsuite/set_particle_acidity_test.py ${PYTHON} testsuite/bond_tests.py diff --git a/lib/analysis.py b/lib/analysis.py index f82745c..9cd119a 100644 --- a/lib/analysis.py +++ b/lib/analysis.py @@ -136,7 +136,7 @@ def get_params_from_dir_name(name): entries = name.split('_') params = {} for entry in entries: - sp_entry = entry.split('-') + sp_entry = entry.split('-', 1) params[sp_entry[0]] = sp_entry[-1] #float(sp_entry[-1]) # creates a dictionary of parameters and their values. return params @@ -343,7 +343,6 @@ def get_distribution_from_df(df, key): distribution_list (`lst`): list stored under `key` """ - import pandas as pd distribution_list=[] for row in df[key]: if pd.isnull(row): diff --git a/pyMBE.py b/pyMBE.py index eb1e5ba..daa664e 100644 --- a/pyMBE.py +++ b/pyMBE.py @@ -2157,10 +2157,10 @@ def print_reduced_units(self): unit_length=self.units.Quantity(1,'reduced_length') unit_energy=self.units.Quantity(1,'reduced_energy') unit_charge=self.units.Quantity(1,'reduced_charge') - print(unit_length.to('nm'), "=", unit_length) - print(unit_energy.to('J'), "=", unit_energy) - print('Temperature:', (self.kT/self.Kb).to("K")) - print(unit_charge.to('C'), "=", unit_charge) + print(f"{unit_length.to('nm'):.5g} = {unit_length}") + print(f"{unit_energy.to('J'):.5g} = {unit_energy}") + print(f"{unit_charge.to('C'):.5g} = {unit_charge}") + print(f"Temperature: {(self.kT/self.Kb).to('K'):.5g}") print() def propose_unused_type(self): diff --git a/testsuite/serialization_test.py b/testsuite/serialization_test.py new file mode 100644 index 0000000..eea3ad2 --- /dev/null +++ b/testsuite/serialization_test.py @@ -0,0 +1,52 @@ +import io +import json +import contextlib +import unittest as ut +import numpy as np +import pandas as pd +import pyMBE +import lib.analysis + + +class Serialization(ut.TestCase): + + def test_json_encoder(self): + encoder = pyMBE.pymbe_library.NumpyEncoder + # Python types + self.assertEqual(json.dumps(1, cls=encoder), "1") + self.assertEqual(json.dumps([1, 2], cls=encoder), "[1, 2]") + self.assertEqual(json.dumps((1, 2), cls=encoder), "[1, 2]") + self.assertEqual(json.dumps({1: 2}, cls=encoder), """{"1": 2}""") + # NumPy types + self.assertEqual(json.dumps(np.array([1, 2]), cls=encoder), "[1, 2]") + self.assertEqual(json.dumps(np.array(1), cls=encoder), "1") + self.assertEqual(json.dumps(np.int32(1), cls=encoder), "1") + # Pandas types + with self.assertRaisesRegex(TypeError, "Object of type Series is not JSON serializable"): + json.dumps(pd.Series([1, 2]), cls=encoder) + + def test_parameters_to_path(self): + params = {"kT": 2., "phi": -np.pi, "n": 3, "fene": True, "name": "pep"} + name = lib.analysis.built_output_name(params) + self.assertEqual(name, "kT-2_phi--3.14_n-3_fene-True_name-pep") + params_out = lib.analysis.get_params_from_dir_name(name) + params_ref = {"kT": "2", "phi": "-3.14", "n": "3", + "fene": "True", "name": "pep"} + self.assertEqual(params_out, params_ref) + + def test_pint_units(self): + ref_output = [ + "Current set of reduced units:", + "0.355 nanometer = 1 reduced_length", + "4.1164e-21 joule = 1 reduced_energy", + "1.6022e-19 coulomb = 1 reduced_charge", + "Temperature: 298.15 kelvin", + ] + pmb = pyMBE.pymbe_library(SEED=42) + with contextlib.redirect_stdout(io.StringIO()) as f: + pmb.print_reduced_units() + self.assertEqual(f.getvalue().strip("\n").split("\n"), ref_output) + + +if __name__ == "__main__": + ut.main()