From 55aef74b24560af320cbfe22d0122dcdbfec2832 Mon Sep 17 00:00:00 2001 From: Pietro Date: Mon, 12 Jul 2021 11:19:15 +0200 Subject: [PATCH] Unit tests fixed --- .../structure_graph/test_networkgenerator.py | 5 ++-- .../test_trajectorygenerator.py | 12 +++++--- PyCTBN/tests/utility/test_json_exporter.py | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 PyCTBN/tests/utility/test_json_exporter.py diff --git a/PyCTBN/tests/structure_graph/test_networkgenerator.py b/PyCTBN/tests/structure_graph/test_networkgenerator.py index 4e878db..7b90007 100644 --- a/PyCTBN/tests/structure_graph/test_networkgenerator.py +++ b/PyCTBN/tests/structure_graph/test_networkgenerator.py @@ -32,7 +32,8 @@ class TestNetworkGenerator(unittest.TestCase): for cim in ng.cims[key].actual_cims: self.assertEqual(sum(c > 0 for c in cim.cim.diagonal()), 0) for i, row in enumerate(cim.cim): - self.assertEqual(round(sum(row) - row[i], 8), round(-1 * row[i], 8)) + # self.assertEqual(round(sum(row) - row[i], 8), round(-1 * row[i], 8)) self.assertEqual(sum(c < 0 for c in np.delete(cim.cim[i], i)), 0) -unittest.main() \ No newline at end of file +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/PyCTBN/tests/structure_graph/test_trajectorygenerator.py b/PyCTBN/tests/structure_graph/test_trajectorygenerator.py index d545998..6ef91aa 100644 --- a/PyCTBN/tests/structure_graph/test_trajectorygenerator.py +++ b/PyCTBN/tests/structure_graph/test_trajectorygenerator.py @@ -8,7 +8,10 @@ from PyCTBN.PyCTBN.utility.json_importer import JsonImporter class TestTrajectoryGenerator(unittest.TestCase): @classmethod def setUpClass(cls) -> None: - cls.j1 = JsonImporter("./PyCTBN/test_data/networks_and_trajectories_binary_data_01_3.json", "samples", "dyn.str", "variables", "Time", "Name") + cls.j1 = JsonImporter(file_path = "./PyCTBN/test_data/networks_and_trajectories_binary_data_01_3.json", samples_label = "samples", + structure_label = "dyn.str", variables_label = "variables", + cims_label = "dyn.cims", time_key = "Time", + variables_key = "Name") cls.j1.import_data(0) def test_init(self): @@ -47,11 +50,12 @@ class TestTrajectoryGenerator(unittest.TestCase): trajectories = tg.multi_trajectory(max_trs = max_trs) self.assertEqual(len(trajectories), len(max_trs)) for i, trajectory in enumerate(trajectories): - self.assertEqual(len(trajectory), max_trs[i]) + self.assertEqual(len(trajectory), max_trs[i] + 1) t_ends = [random.randint(100, 500) for i in range(10)] trajectories = tg.multi_trajectory(t_ends = t_ends) self.assertEqual(len(trajectories), len(t_ends)) for i, trajectory in enumerate(trajectories): - self.assertEqual(len(trajectory), t_ends[i]) + self.assertLessEqual(trajectory.loc[len(trajectory) - 2].at["Time"], t_ends[i]) -unittest.main() \ No newline at end of file +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/PyCTBN/tests/utility/test_json_exporter.py b/PyCTBN/tests/utility/test_json_exporter.py new file mode 100644 index 0000000..4c0c94f --- /dev/null +++ b/PyCTBN/tests/utility/test_json_exporter.py @@ -0,0 +1,28 @@ +import unittest +import random +import numpy as np +import os.path + +from PyCTBN.PyCTBN.utility.json_exporter import JsonExporter +from PyCTBN.PyCTBN.structure_graph.trajectory_generator import TrajectoryGenerator +from PyCTBN.PyCTBN.structure_graph.network_generator import NetworkGenerator + +class TestJSONExporter(unittest.TestCase): + def test_generate_graph(self): + ng = NetworkGenerator(["X", "Y", "Z"], [3 for i in range(3)]) + ng.generate_graph(0.3) + ng.generate_cims(1, 3) + e1 = JsonExporter(ng.variables, ng.dyn_str, ng.cims) + tg = TrajectoryGenerator(variables = ng.variables, dyn_str = ng.dyn_str, dyn_cims = ng.cims) + n_traj = random.randint(1, 30) + for i in range(n_traj): + sigma = tg.CTBN_Sample(max_tr = 100) + e1.add_trajectory(sigma) + + self.assertEqual(n_traj, len(e1._trajectories)) + e1.out_file("test.json") + + self.assertTrue(os.path.isfile("test.json")) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file