1
0
Fork 0

Updated tests

master
Luca Moretti 4 years ago
parent 1d355362b1
commit 6be81aaec2
  1. 24
      main_package/classes/utility/cache.py
  2. 77
      main_package/tests/estimators/test_parameters_estimator.py
  3. 23
      main_package/tests/estimators/test_structure_constraint_based_estimator_server.py
  4. 17
      main_package/tests/estimators/test_structure_score_based_estimator.py
  5. 23
      main_package/tests/estimators/test_structure_score_based_estimator_server.py
  6. 15
      main_package/tests/structure_graph/test_cim.py
  7. 21
      main_package/tests/structure_graph/test_structure.py
  8. 60
      main_package/tests/utility/test_cache.py
  9. 60
      main_package/tests/utility/test_json_importer.py
  10. 17
      main_package/tests/utility/test_sample_importer.py

@ -7,16 +7,16 @@ from ..structure_graph.set_of_cims import SetOfCims
class Cache: class Cache:
"""This class acts as a cache of ``SetOfCims`` objects for a node. """This class acts as a cache of ``SetOfCims`` objects for a node.
:_list_of_sets_of_parents: a list of ``Sets`` objects of the parents to which the cim in cache at SAME :__list_of_sets_of_parents: a list of ``Sets`` objects of the parents to which the cim in cache at SAME
index is related index is related
:_actual_cache: a list of setOfCims objects :__actual_cache: a list of setOfCims objects
""" """
def __init__(self): def __init__(self):
"""Constructor Method """Constructor Method
""" """
self.list_of_sets_of_parents = [] self._list_of_sets_of_parents = []
self.actual_cache = [] self._actual_cache = []
def find(self, parents_comb: typing.Set): #typing.Union[typing.Set, str] def find(self, parents_comb: typing.Set): #typing.Union[typing.Set, str]
""" """
@ -25,14 +25,14 @@ class Cache:
:param parents_comb: the parents related to that ``SetOfCims`` :param parents_comb: the parents related to that ``SetOfCims``
:type parents_comb: Set :type parents_comb: Set
:return: A ``SetOfCims`` object if the ``parents_comb`` index is found in ``_list_of_sets_of_parents``. :return: A ``SetOfCims`` object if the ``parents_comb`` index is found in ``__list_of_sets_of_parents``.
None otherwise. None otherwise.
:rtype: SetOfCims :rtype: SetOfCims
""" """
try: try:
#print("Cache State:", self.list_of_sets_of_indxs) #print("Cache State:", self.list_of_sets_of_indxs)
#print("Look For:", parents_comb) #print("Look For:", parents_comb)
result = self.actual_cache[self.list_of_sets_of_parents.index(parents_comb)] result = self._actual_cache[self._list_of_sets_of_parents.index(parents_comb)]
#print("CACHE HIT!!!!", parents_comb) #print("CACHE HIT!!!!", parents_comb)
return result return result
except ValueError: except ValueError:
@ -40,7 +40,7 @@ class Cache:
def put(self, parents_comb: typing.Set, socim: SetOfCims): def put(self, parents_comb: typing.Set, socim: SetOfCims):
"""Place in cache the ``SetOfCims`` object, and the related symbolic index ``parents_comb`` in """Place in cache the ``SetOfCims`` object, and the related symbolic index ``parents_comb`` in
``_list_of_sets_of_parents``. ``__list_of_sets_of_parents``.
:param parents_comb: the symbolic set index :param parents_comb: the symbolic set index
:type parents_comb: Set :type parents_comb: Set
@ -48,11 +48,11 @@ class Cache:
:type socim: SetOfCims :type socim: SetOfCims
""" """
#print("Putting in cache:", parents_comb) #print("Putting in cache:", parents_comb)
self.list_of_sets_of_parents.append(parents_comb) self._list_of_sets_of_parents.append(parents_comb)
self.actual_cache.append(socim) self._actual_cache.append(socim)
def clear(self): def clear(self):
"""Clear the contents both of ``_actual_cache`` and ``_list_of_sets_of_parents``. """Clear the contents both of ``__actual_cache`` and ``__list_of_sets_of_parents``.
""" """
del self.list_of_sets_of_parents[:] del self._list_of_sets_of_parents[:]
del self.actual_cache[:] del self._actual_cache[:]

@ -1,90 +1,67 @@
import sys
sys.path.append("../../classes/")
import unittest import unittest
import numpy as np import numpy as np
import glob import glob
import os import os
import structure_graph.network_graph as ng from ...classes.structure_graph.network_graph import NetworkGraph
import structure_graph.sample_path as sp from ...classes.structure_graph.sample_path import SamplePath
import structure_graph.set_of_cims as sofc from ...classes.structure_graph.set_of_cims import SetOfCims
import estimators.parameters_estimator as pe from ...classes.estimators.parameters_estimator import ParametersEstimator
import utility.json_importer as ji from ...classes.utility.json_importer import JsonImporter
class TestParametersEstimatior(unittest.TestCase): class TestParametersEstimatior(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls) -> None: def setUpClass(cls) -> None:
cls.read_files = glob.glob(os.path.join('../../data', "*.json")) cls.read_files = glob.glob(os.path.join('./main_package/data', "*.json"))
cls.importer = ji.JsonImporter("../../data/networks_and_trajectories_ternary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') cls.array_indx = 0
cls.s1 = sp.SamplePath(cls.importer) cls.importer = JsonImporter(cls.read_files[0], 'samples', 'dyn.str', 'variables', 'Time', 'Name')
cls.importer.import_data(cls.array_indx)
cls.s1 = SamplePath(cls.importer)
cls.s1.build_trajectories() cls.s1.build_trajectories()
cls.s1.build_structure() cls.s1.build_structure()
cls.g1 = ng.NetworkGraph(cls.s1.structure) print(cls.s1.structure.edges)
cls.g1.init_graph() print(cls.s1.structure.nodes_values)
def test_fast_init(self): def test_fast_init(self):
for node in self.g1.nodes: for node in self.s1.structure.nodes_labels:
g = ng.NetworkGraph(self.s1.structure) g = NetworkGraph(self.s1.structure)
g.fast_init(node) g.fast_init(node)
p1 = pe.ParametersEstimator(self.s1, g) p1 = ParametersEstimator(self.s1.trajectories, g)
self.assertEqual(p1.sample_path, self.s1) self.assertEqual(p1._trajectories, self.s1.trajectories)
self.assertEqual(p1.net_graph, g) self.assertEqual(p1._net_graph, g)
self.assertIsNone(p1.single_set_of_cims) self.assertIsNone(p1._single_set_of_cims)
p1.fast_init(node) p1.fast_init(node)
self.assertIsInstance(p1.single_set_of_cims, sofc.SetOfCims) self.assertIsInstance(p1._single_set_of_cims, SetOfCims)
def test_compute_parameters_for_node(self): def test_compute_parameters_for_node(self):
for indx, node in enumerate(self.g1.nodes): for indx, node in enumerate(self.s1.structure.nodes_labels):
print(node) print(node)
g = ng.NetworkGraph(self.s1.structure) g = NetworkGraph(self.s1.structure)
g.fast_init(node) g.fast_init(node)
p1 = pe.ParametersEstimator(self.s1, g) p1 = ParametersEstimator(self.s1.trajectories, g)
p1.fast_init(node) p1.fast_init(node)
sofc1 = p1.compute_parameters_for_node(node) sofc1 = p1.compute_parameters_for_node(node)
sampled_cims = self.aux_import_sampled_cims('dyn.cims') sampled_cims = self.aux_import_sampled_cims('dyn.cims')
sc = list(sampled_cims.values()) sc = list(sampled_cims.values())
print(sampled_cims.values()) self.equality_of_cims_of_node(sc[indx], sofc1._actual_cims)
#print(sc[indx])
self.equality_of_cims_of_node(sc[indx], sofc1.actual_cims)
def equality_of_cims_of_node(self, sampled_cims, estimated_cims): def equality_of_cims_of_node(self, sampled_cims, estimated_cims):
#print(sampled_cims)
#print(estimated_cims)
self.assertEqual(len(sampled_cims), len(estimated_cims)) self.assertEqual(len(sampled_cims), len(estimated_cims))
for c1, c2 in zip(sampled_cims, estimated_cims): for c1, c2 in zip(sampled_cims, estimated_cims):
self.cim_equality_test(c1, c2.cim) self.cim_equality_test(c1, c2.cim)
def cim_equality_test(self, cim1, cim2): def cim_equality_test(self, cim1, cim2):
for r1, r2 in zip(cim1, cim2): for r1, r2 in zip(cim1, cim2):
self.assertTrue(np.all(np.isclose(r1, r2, 1e-01, 1e-01) == True)) self.assertTrue(np.all(np.isclose(r1, r2, 1e-01, 1e-01) is True))
def aux_import_sampled_cims(self, cims_label): def aux_import_sampled_cims(self, cims_label):
i1 = ji.JsonImporter(self.read_files[0], '', '', '', '', '') i1 = JsonImporter(self.read_files[0], '', '', '', '', '')
raw_data = i1.read_json_file() raw_data = i1.read_json_file()
return i1.import_sampled_cims(raw_data, 0, cims_label) return i1.import_sampled_cims(raw_data, self.array_indx, cims_label)
"""
def test_init(self):
self.aux_test_init(self.s1, self.g1)
def test_init_sets_of_cims_container(self):
self.aux_test_init_sets_cims_container(self.s1, self.g1)
def aux_test_init(self, sample_p, graph):
pe1 = pe.ParametersEstimator(sample_p, graph)
self.assertEqual(sample_p, pe1.sample_path)
self.assertEqual(graph, pe1.net_graph)
self.assertIsNone(pe1.sets_of_cims_struct)
def aux_test_init_sets_cims_container(self, sample_p, graph):
pe1 = pe.ParametersEstimator(sample_p, graph)
pe1.init_sets_cims_container()
self.assertIsInstance(pe1.sets_of_cims_struct, scc.SetsOfCimsContainer)
def test_compute_parameters(self):
self.aux_test_compute_parameters(self.s1, self.g1)
"""
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

@ -1,5 +1,4 @@
import sys
sys.path.append("../../classes/")
import glob import glob
import math import math
import os import os
@ -10,10 +9,10 @@ import numpy as np
import psutil import psutil
from line_profiler import LineProfiler from line_profiler import LineProfiler
import utility.cache as ch from ...classes.utility.cache import Cache
import structure_graph.sample_path as sp from ...classes.structure_graph.sample_path import SamplePath
import estimators.structure_constraint_based_estimator as se from ...classes.estimators.structure_constraint_based_estimator import StructureConstraintBasedEstimator
import utility.json_importer as ji from ...classes.utility.json_importer import JsonImporter
from multiprocessing import set_start_method from multiprocessing import set_start_method
@ -27,8 +26,8 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase):
def test_structure(self): def test_structure(self):
#cls.read_files = glob.glob(os.path.join('../../data', "*.json")) #cls.read_files = glob.glob(os.path.join('../../data', "*.json"))
self.importer = ji.JsonImporter("/home/alessandro/Documents/ctbn_cba/data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') self.importer = JsonImporter("./main_package/data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.s1 = sp.SamplePath(self.importer) self.s1 = SamplePath(self.importer)
self.s1.build_trajectories() self.s1.build_trajectories()
self.s1.build_structure() self.s1.build_structure()
@ -36,12 +35,12 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase):
true_edges = set(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
se1 = se.StructureConstraintBasedEstimator(self.s1,0.1,0.1) se1 = StructureConstraintBasedEstimator(self.s1,0.1,0.1)
edges = se1.estimate_structure(disable_multiprocessing=False) edges = se1.estimate_structure(disable_multiprocessing=False)
self.importer = ji.JsonImporter("/home/alessandro/Documents/ctbn_cba/data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') self.importer = JsonImporter("./main_package/data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.s1 = sp.SamplePath(self.importer) self.s1 = SamplePath(self.importer)
self.s1.build_trajectories() self.s1.build_trajectories()
self.s1.build_structure() self.s1.build_structure()
@ -49,7 +48,7 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase):
true_edges = set(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
se1 = se.StructureConstraintBasedEstimator(self.s1,0.1,0.1) se1 = StructureConstraintBasedEstimator(self.s1,0.1,0.1)
edges = se1.estimate_structure(disable_multiprocessing=True) edges = se1.estimate_structure(disable_multiprocessing=True)

@ -11,10 +11,11 @@ import psutil
from line_profiler import LineProfiler from line_profiler import LineProfiler
import copy import copy
import utility.cache as ch from ...classes.utility.cache import Cache
import structure_graph.sample_path as sp from ...classes.structure_graph.sample_path import SamplePath
import estimators.structure_score_based_estimator as se from ...classes.estimators.structure_score_based_estimator import StructureScoreBasedEstimator
import utility.sample_importer as si from ...classes.utility.json_importer import JsonImporter
from ...classes.utility.sample_importer import SampleImporter
import json import json
@ -26,7 +27,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
with open("../../data/networks_and_trajectories_binary_data_01_6.json") as f: with open("./main_package/data/networks_and_trajectories_binary_data_01_6.json") as f:
raw_data = json.load(f) raw_data = json.load(f)
trajectory_list_raw= raw_data[0]["samples"] trajectory_list_raw= raw_data[0]["samples"]
@ -37,7 +38,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
prior_net_structure = pd.DataFrame(raw_data[0]["dyn.str"]) prior_net_structure = pd.DataFrame(raw_data[0]["dyn.str"])
cls.importer = si.SampleImporter( cls.importer = SampleImporter(
trajectory_list=trajectory_list, trajectory_list=trajectory_list,
variables=variables, variables=variables,
prior_net_structure=prior_net_structure prior_net_structure=prior_net_structure
@ -49,7 +50,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
#cls.traj = cls.s1.concatenated_samples #cls.traj = cls.s1.concatenated_samples
# print(len(cls.traj)) # print(len(cls.traj))
cls.s1 = sp.SamplePath(cls.importer) cls.s1 = SamplePath(cls.importer)
cls.s1.build_trajectories() cls.s1.build_trajectories()
cls.s1.build_structure() cls.s1.build_structure()
@ -60,7 +61,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
true_edges = set(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
se1 = se.StructureScoreBasedEstimator(self.s1,known_edges = [('X','Q')]) se1 = StructureScoreBasedEstimator(self.s1,known_edges = [('X','Q')])
edges = se1.estimate_structure( edges = se1.estimate_structure(
max_parents = None, max_parents = None,
iterations_number = 100, iterations_number = 100,

@ -1,5 +1,4 @@
import sys
sys.path.append("../../classes/")
import glob import glob
import math import math
import os import os
@ -11,10 +10,10 @@ import psutil
from line_profiler import LineProfiler from line_profiler import LineProfiler
import copy import copy
import utility.cache as ch from ...classes.utility.cache import Cache
import structure_graph.sample_path as sp from ...classes.structure_graph.sample_path import SamplePath
import estimators.structure_score_based_estimator as se from ...classes.estimators.structure_score_based_estimator import StructureScoreBasedEstimator
import utility.json_importer as ji from ...classes.utility.json_importer import JsonImporter
@ -28,8 +27,8 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
def test_structure(self): def test_structure(self):
#cls.read_files = glob.glob(os.path.join('../../data', "*.json")) #cls.read_files = glob.glob(os.path.join('../../data', "*.json"))
self.importer = ji.JsonImporter("/home/alessandro/Documents/ctbn_cba/data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') self.importer = JsonImporter("./main_package/data/networks_and_trajectories_ternary_data_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.s1 = sp.SamplePath(self.importer) self.s1 = SamplePath(self.importer)
self.s1.build_trajectories() self.s1.build_trajectories()
self.s1.build_structure() self.s1.build_structure()
@ -37,7 +36,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
true_edges = set(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
se1 = se.StructureScoreBasedEstimator(self.s1) se1 = StructureScoreBasedEstimator(self.s1)
edges = se1.estimate_structure( edges = se1.estimate_structure(
max_parents = None, max_parents = None,
iterations_number = 100, iterations_number = 100,
@ -49,8 +48,8 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
) )
self.importer = ji.JsonImporter("/home/alessandro/Documents/ctbn_cba/data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') self.importer = JsonImporter("./main_package/data/networks_and_trajectories_ternary_data_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.s1 = sp.SamplePath(self.importer) self.s1 = SamplePath(self.importer)
self.s1.build_trajectories() self.s1.build_trajectories()
self.s1.build_structure() self.s1.build_structure()
@ -58,7 +57,7 @@ class TestStructureScoreBasedEstimator(unittest.TestCase):
true_edges = set(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
se1 = se.StructureScoreBasedEstimator(self.s1) se1 = StructureScoreBasedEstimator(self.s1)
edges = se1.estimate_structure( edges = se1.estimate_structure(
max_parents = None, max_parents = None,
iterations_number = 100, iterations_number = 100,

@ -1,9 +1,8 @@
import sys
sys.path.append("../../classes/")
import unittest import unittest
import numpy as np import numpy as np
import structure_graph.conditional_intensity_matrix as cim from ...classes.structure_graph.conditional_intensity_matrix import ConditionalIntensityMatrix
class TestConditionalIntensityMatrix(unittest.TestCase): class TestConditionalIntensityMatrix(unittest.TestCase):
@ -18,28 +17,28 @@ class TestConditionalIntensityMatrix(unittest.TestCase):
cls.state_transition_matrix[i, i] = np.sum(cls.state_transition_matrix[i]) cls.state_transition_matrix[i, i] = np.sum(cls.state_transition_matrix[i])
def test_init(self): def test_init(self):
c1 = cim.ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix) c1 = ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix)
self.assertTrue(np.array_equal(self.state_res_times, c1._state_residence_times)) self.assertTrue(np.array_equal(self.state_res_times, c1.state_residence_times))
self.assertTrue(np.array_equal(self.state_transition_matrix, c1.state_transition_matrix)) self.assertTrue(np.array_equal(self.state_transition_matrix, c1.state_transition_matrix))
self.assertEqual(c1.cim.dtype, np.float) self.assertEqual(c1.cim.dtype, np.float)
self.assertEqual(self.state_transition_matrix.shape, c1.cim.shape) self.assertEqual(self.state_transition_matrix.shape, c1.cim.shape)
def test_compute_cim_coefficients(self): def test_compute_cim_coefficients(self):
c1 = cim.ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix) c1 = ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix)
c2 = self.state_transition_matrix.astype(np.float) c2 = self.state_transition_matrix.astype(np.float)
np.fill_diagonal(c2, c2.diagonal() * -1) np.fill_diagonal(c2, c2.diagonal() * -1)
for i in range(0, len(self.state_res_times)): for i in range(0, len(self.state_res_times)):
for j in range(0, len(self.state_res_times)): for j in range(0, len(self.state_res_times)):
c2[i, j] = (c2[i, j] + 1) / (self.state_res_times[i] + 1) c2[i, j] = (c2[i, j] + 1) / (self.state_res_times[i] + 1)
c1.compute_cim_coefficients() c1.compute_cim_coefficients()
for i in range(0, len(c1._state_residence_times)): for i in range(0, len(c1.state_residence_times)):
self.assertTrue(np.isclose(np.sum(c1.cim[i]), 0.0, 1e-02, 1e-01)) self.assertTrue(np.isclose(np.sum(c1.cim[i]), 0.0, 1e-02, 1e-01))
for i in range(0, len(self.state_res_times)): for i in range(0, len(self.state_res_times)):
for j in range(0, len(self.state_res_times)): for j in range(0, len(self.state_res_times)):
self.assertTrue(np.isclose(c1.cim[i, j], c2[i, j], 1e-02, 1e-01)) self.assertTrue(np.isclose(c1.cim[i, j], c2[i, j], 1e-02, 1e-01))
def test_repr(self): def test_repr(self):
c1 = cim.ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix) c1 = ConditionalIntensityMatrix(self.state_res_times, self.state_transition_matrix)
print(c1) print(c1)

@ -1,8 +1,7 @@
import sys
sys.path.append("../../classes/")
import unittest import unittest
import numpy as np import numpy as np
import structure_graph.structure as st from ...classes.structure_graph.structure import Structure
class TestStructure(unittest.TestCase): class TestStructure(unittest.TestCase):
@ -15,7 +14,7 @@ class TestStructure(unittest.TestCase):
cls.vars_numb = len(cls.labels) cls.vars_numb = len(cls.labels)
def test_init(self): def test_init(self):
s1 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
self.assertListEqual(self.labels,s1.nodes_labels) self.assertListEqual(self.labels,s1.nodes_labels)
self.assertIsInstance(s1.nodes_indexes, np.ndarray) self.assertIsInstance(s1.nodes_indexes, np.ndarray)
self.assertTrue(np.array_equal(self.indxs, s1.nodes_indexes)) self.assertTrue(np.array_equal(self.indxs, s1.nodes_indexes))
@ -25,7 +24,7 @@ class TestStructure(unittest.TestCase):
self.assertEqual(self.vars_numb, s1.total_variables_number) self.assertEqual(self.vars_numb, s1.total_variables_number)
def test_get_node_id(self): def test_get_node_id(self):
s1 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
for indx, var in enumerate(self.labels): for indx, var in enumerate(self.labels):
self.assertEqual(var, s1.get_node_id(indx)) self.assertEqual(var, s1.get_node_id(indx))
@ -38,7 +37,7 @@ class TestStructure(unittest.TestCase):
np.delete(v2, 1) np.delete(v2, 1)
e2 = [('X','Z')] e2 = [('X','Z')]
n2 = self.vars_numb - 1 n2 = self.vars_numb - 1
s1 = st.Structure(l2, i2, v2, e2, n2) s1 = Structure(l2, i2, v2, e2, n2)
for indx, var in zip(i2, l2): for indx, var in zip(i2, l2):
self.assertEqual(indx, s1.get_node_indx(var)) self.assertEqual(indx, s1.get_node_indx(var))
@ -51,7 +50,7 @@ class TestStructure(unittest.TestCase):
np.delete(v2, 1) np.delete(v2, 1)
e2 = [('X', 'Z')] e2 = [('X', 'Z')]
n2 = self.vars_numb - 1 n2 = self.vars_numb - 1
s1 = st.Structure(l2, i2, v2, e2, n2) s1 = Structure(l2, i2, v2, e2, n2)
for indx, var in enumerate(s1.nodes_labels): for indx, var in enumerate(s1.nodes_labels):
self.assertEqual(indx, s1.get_positional_node_indx(var)) self.assertEqual(indx, s1.get_positional_node_indx(var))
@ -64,17 +63,17 @@ class TestStructure(unittest.TestCase):
np.delete(v2, 1) np.delete(v2, 1)
e2 = [('X', 'Z')] e2 = [('X', 'Z')]
n2 = self.vars_numb - 1 n2 = self.vars_numb - 1
s1 = st.Structure(l2, i2, v2, e2, n2) s1 = Structure(l2, i2, v2, e2, n2)
for val, node in zip(v2, l2): for val, node in zip(v2, l2):
self.assertEqual(val, s1.get_states_number(node)) self.assertEqual(val, s1.get_states_number(node))
def test_equality(self): def test_equality(self):
s1 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
s2 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) s2 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
self.assertEqual(s1, s2) self.assertEqual(s1, s2)
def test_repr(self): def test_repr(self):
s1 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
print(s1) print(s1)

@ -1,62 +1,56 @@
import sys
sys.path.append("../../classes/")
import unittest import unittest
import numpy as np import numpy as np
import utility.cache as ch from ...classes.utility.cache import Cache
import structure_graph.set_of_cims as soci from ...classes.structure_graph.set_of_cims import SetOfCims
class TestCache(unittest.TestCase): class TestCache(unittest.TestCase):
def test_init(self): def test_init(self):
c1 = ch.Cache() c1 = Cache()
self.assertFalse(c1.list_of_sets_of_parents) self.assertFalse(c1._list_of_sets_of_parents)
self.assertFalse(c1.actual_cache) self.assertFalse(c1._actual_cache)
def test_put(self): def test_put(self):
c1 = ch.Cache() c1 = Cache()
pset1 = {'X', 'Y'} pset1 = {'X', 'Y'}
sofc1 = soci.SetOfCims('Z', [], 3, np.array([])) sofc1 = SetOfCims('Z', [], 3, np.array([]))
c1.put(pset1, sofc1) c1.put(pset1, sofc1)
self.assertEqual(1, len(c1.actual_cache)) self.assertEqual(1, len(c1._actual_cache))
self.assertEqual(1, len(c1.list_of_sets_of_parents)) self.assertEqual(1, len(c1._list_of_sets_of_parents))
self.assertEqual(sofc1, c1.actual_cache[0]) self.assertEqual(sofc1, c1._actual_cache[0])
pset2 = {'X'} pset2 = {'X'}
sofc2 = soci.SetOfCims('Z', [], 3, np.array([])) sofc2 = SetOfCims('Z', [], 3, np.array([]))
c1.put(pset2, sofc2) c1.put(pset2, sofc2)
self.assertEqual(2, len(c1.actual_cache)) self.assertEqual(2, len(c1._actual_cache))
self.assertEqual(2, len(c1.list_of_sets_of_parents)) self.assertEqual(2, len(c1._list_of_sets_of_parents))
self.assertEqual(sofc2, c1.actual_cache[1]) self.assertEqual(sofc2, c1._actual_cache[1])
def test_find(self): def test_find(self):
c1 = ch.Cache() c1 = Cache()
pset1 = {'X', 'Y'} pset1 = {'X', 'Y'}
sofc1 = soci.SetOfCims('Z', [], 3, np.array([])) sofc1 = SetOfCims('Z', [], 3, np.array([]))
c1.put(pset1, sofc1) c1.put(pset1, sofc1)
self.assertEqual(1, len(c1.actual_cache)) self.assertEqual(1, len(c1._actual_cache))
self.assertEqual(1, len(c1.list_of_sets_of_parents)) self.assertEqual(1, len(c1._list_of_sets_of_parents))
self.assertIsInstance(c1.find(pset1), soci.SetOfCims) self.assertIsInstance(c1.find(pset1), SetOfCims)
self.assertEqual(sofc1, c1.find(pset1)) self.assertEqual(sofc1, c1.find(pset1))
self.assertIsInstance(c1.find({'Y', 'X'}), soci.SetOfCims) self.assertIsInstance(c1.find({'Y', 'X'}), SetOfCims)
self.assertEqual(sofc1, c1.find({'Y', 'X'})) self.assertEqual(sofc1, c1.find({'Y', 'X'}))
self.assertIsNone(c1.find({'X'})) self.assertIsNone(c1.find({'X'}))
def test_clear(self): def test_clear(self):
c1 = ch.Cache() c1 = Cache()
pset1 = {'X', 'Y'} pset1 = {'X', 'Y'}
sofc1 = soci.SetOfCims('Z', [], 3, np.array([])) sofc1 = SetOfCims('Z', [], 3, np.array([]))
c1.put(pset1, sofc1) c1.put(pset1, sofc1)
self.assertEqual(1, len(c1.actual_cache)) self.assertEqual(1, len(c1._actual_cache))
self.assertEqual(1, len(c1.list_of_sets_of_parents)) self.assertEqual(1, len(c1._list_of_sets_of_parents))
c1.clear() c1.clear()
self.assertFalse(c1.list_of_sets_of_parents) self.assertFalse(c1._list_of_sets_of_parents)
self.assertFalse(c1.actual_cache) self.assertFalse(c1._actual_cache)
if __name__ == '__main__': if __name__ == '__main__':

@ -1,11 +1,10 @@
import sys
sys.path.append("../../classes/")
import unittest import unittest
import os import os
import glob import glob
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from utility.json_importer import JsonImporter from ...classes.utility.json_importer import JsonImporter
import json import json
@ -15,16 +14,16 @@ class TestJsonImporter(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls) -> None: def setUpClass(cls) -> None:
cls.read_files = glob.glob(os.path.join('../../data', "*.json")) cls.read_files = glob.glob(os.path.join('./main_package/data', "*.json"))
def test_init(self): def test_init(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.assertEqual(j1._samples_label, 'samples') self.assertEqual(j1._samples_label, 'samples')
self.assertEqual(j1._structure_label, 'dyn.str') self.assertEqual(j1._structure_label, 'dyn.str')
self.assertEqual(j1._variables_label, 'variables') self.assertEqual(j1._variables_label, 'variables')
self.assertEqual(j1._time_key, 'Time') self.assertEqual(j1._time_key, 'Time')
self.assertEqual(j1._variables_key, 'Name') self.assertEqual(j1._variables_key, 'Name')
self.assertEqual(j1._file_path, "../../data/networks_and_trajectories_binary_data_01_3.json") self.assertEqual(j1._file_path, "./main_package/data/networks_and_trajectories_binary_data_01_3.json")
self.assertIsNone(j1._df_samples_list) self.assertIsNone(j1._df_samples_list)
self.assertIsNone(j1.variables) self.assertIsNone(j1.variables)
self.assertIsNone(j1.structure) self.assertIsNone(j1.structure)
@ -49,26 +48,26 @@ class TestJsonImporter(unittest.TestCase):
self.assertRaises(FileNotFoundError, JsonImporter, path, '', '', '', '', '') self.assertRaises(FileNotFoundError, JsonImporter, path, '', '', '', '', '')
def test_build_sorter(self): def test_build_sorter(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
df_samples_list = j1.normalize_trajectories(j1._raw_data, 0, j1._samples_label) df_samples_list = j1.normalize_trajectories(j1._raw_data, 0, j1._samples_label)
sorter = j1.build_sorter(df_samples_list[0]) sorter = j1.build_sorter(df_samples_list[0])
self.assertListEqual(sorter, list(df_samples_list[0].columns.values)[1:]) self.assertListEqual(sorter, list(df_samples_list[0].columns.values)[1:])
def test_normalize_trajectories(self): def test_normalize_trajectories(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
df_samples_list = j1.normalize_trajectories(j1._raw_data, 0, j1._samples_label) df_samples_list = j1.normalize_trajectories(j1._raw_data, 0, j1._samples_label)
self.assertEqual(len(df_samples_list), len(j1._raw_data[0][j1._samples_label])) self.assertEqual(len(df_samples_list), len(j1._raw_data[0][j1._samples_label]))
def test_normalize_trajectories_wrong_indx(self): def test_normalize_trajectories_wrong_indx(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.assertRaises(IndexError, j1.normalize_trajectories, j1._raw_data, 474, j1._samples_label) self.assertRaises(IndexError, j1.normalize_trajectories, j1._raw_data, 474, j1._samples_label)
def test_normalize_trajectories_wrong_key(self): def test_normalize_trajectories_wrong_key(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'sample', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'sample', 'dyn.str', 'variables', 'Time', 'Name')
self.assertRaises(KeyError, j1.normalize_trajectories, j1._raw_data, 0, j1._samples_label) self.assertRaises(KeyError, j1.normalize_trajectories, j1._raw_data, 0, j1._samples_label)
def test_compute_row_delta_single_samples_frame(self): def test_compute_row_delta_single_samples_frame(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1._array_indx = 0 j1._array_indx = 0
j1._df_samples_list = j1.import_trajectories(j1._raw_data) j1._df_samples_list = j1.import_trajectories(j1._raw_data)
sample_frame = j1._df_samples_list[0] sample_frame = j1._df_samples_list[0]
@ -90,7 +89,7 @@ class TestJsonImporter(unittest.TestCase):
np.array(original_copy.iloc[indx + 1][columns_header[1:]], dtype=int)) np.array(original_copy.iloc[indx + 1][columns_header[1:]], dtype=int))
def test_compute_row_delta_in_all_frames(self): def test_compute_row_delta_in_all_frames(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1._array_indx = 0 j1._array_indx = 0
j1._df_samples_list = j1.import_trajectories(j1._raw_data) j1._df_samples_list = j1.import_trajectories(j1._raw_data)
j1._sorter = j1.build_sorter(j1._df_samples_list[0]) j1._sorter = j1.build_sorter(j1._df_samples_list[0])
@ -100,13 +99,13 @@ class TestJsonImporter(unittest.TestCase):
self.assertEqual(list(j1.concatenated_samples.columns.values)[0], j1._time_key) self.assertEqual(list(j1.concatenated_samples.columns.values)[0], j1._time_key)
def test_compute_row_delta_in_all_frames_not_init_sorter(self): def test_compute_row_delta_in_all_frames_not_init_sorter(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1._array_indx = 0 j1._array_indx = 0
j1._df_samples_list = j1.import_trajectories(j1._raw_data) j1._df_samples_list = j1.import_trajectories(j1._raw_data)
self.assertRaises(RuntimeError, j1.compute_row_delta_in_all_samples_frames, j1._df_samples_list) self.assertRaises(RuntimeError, j1.compute_row_delta_in_all_samples_frames, j1._df_samples_list)
def test_clear_data_frame_list(self): def test_clear_data_frame_list(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1._array_indx = 0 j1._array_indx = 0
j1._df_samples_list = j1.import_trajectories(j1._raw_data) j1._df_samples_list = j1.import_trajectories(j1._raw_data)
j1._sorter = j1.build_sorter(j1._df_samples_list[0]) j1._sorter = j1.build_sorter(j1._df_samples_list[0])
@ -116,30 +115,13 @@ class TestJsonImporter(unittest.TestCase):
self.assertTrue(df.empty) self.assertTrue(df.empty)
def test_clear_concatenated_frame(self): def test_clear_concatenated_frame(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1.import_data(0) j1.import_data(0)
j1.clear_concatenated_frame() j1.clear_concatenated_frame()
self.assertTrue(j1.concatenated_samples.empty) self.assertTrue(j1.concatenated_samples.empty)
def test_build_list_of_samples_array(self):
data_set = {"key1": [1, 2, 3], "key2": [4.1, 5.2, 6.3]}
with open('data.json', 'w') as f:
json.dump(data_set, f)
path = os.getcwd()
path = path + '/data.json'
j1 = JsonImporter(path, '', '', '', '', '')
raw_data = j1.read_json_file()
frame = pd.DataFrame(raw_data)
col_list = j1.build_list_of_samples_array(frame)
forced_list = []
for key in data_set:
forced_list.append(np.array(data_set[key]))
for a1, a2 in zip(col_list, forced_list):
self.assertTrue(np.array_equal(a1, a2))
os.remove('data.json')
def test_import_variables(self): def test_import_variables(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
sorter = ['X', 'Y', 'Z'] sorter = ['X', 'Y', 'Z']
raw_data = [{'variables':{"Name": ['X', 'Y', 'Z'], "value": [3, 3, 3]}}] raw_data = [{'variables':{"Name": ['X', 'Y', 'Z'], "value": [3, 3, 3]}}]
j1._array_indx = 0 j1._array_indx = 0
@ -147,14 +129,14 @@ class TestJsonImporter(unittest.TestCase):
self.assertEqual(list(df_var[j1._variables_key]), sorter) self.assertEqual(list(df_var[j1._variables_key]), sorter)
def test_import_structure(self): def test_import_structure(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
raw_data = [{"dyn.str":[{"From":"X","To":"Z"},{"From":"Y","To":"Z"},{"From":"Z","To":"Y"}]}] raw_data = [{"dyn.str":[{"From":"X","To":"Z"},{"From":"Y","To":"Z"},{"From":"Z","To":"Y"}]}]
j1._array_indx = 0 j1._array_indx = 0
df_struct = j1.import_structure(raw_data) df_struct = j1.import_structure(raw_data)
self.assertIsInstance(df_struct, pd.DataFrame) self.assertIsInstance(df_struct, pd.DataFrame)
def test_import_sampled_cims(self): def test_import_sampled_cims(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
raw_data = j1.read_json_file() raw_data = j1.read_json_file()
j1._array_indx = 0 j1._array_indx = 0
j1._df_samples_list = j1.import_trajectories(raw_data) j1._df_samples_list = j1.import_trajectories(raw_data)
@ -163,17 +145,17 @@ class TestJsonImporter(unittest.TestCase):
self.assertEqual(list(cims.keys()), j1.sorter) self.assertEqual(list(cims.keys()), j1.sorter)
def test_dataset_id(self): def test_dataset_id(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
array_indx = 0 array_indx = 0
j1.import_data(array_indx) j1.import_data(array_indx)
self.assertEqual(array_indx, j1.dataset_id()) self.assertEqual(array_indx, j1.dataset_id())
def test_file_path(self): def test_file_path(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
self.assertEqual(j1.file_path, "../../data/networks_and_trajectories_binary_data_01_3.json") self.assertEqual(j1.file_path, "./main_package/data/networks_and_trajectories_binary_data_01_3.json")
def test_import_data(self): def test_import_data(self):
j1 = JsonImporter("../../data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = JsonImporter("./main_package/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1.import_data(0) j1.import_data(0)
self.assertEqual(list(j1.variables[j1._variables_key]), self.assertEqual(list(j1.variables[j1._variables_key]),
list(j1.concatenated_samples.columns.values[1:len(j1.variables[j1._variables_key]) + 1])) list(j1.concatenated_samples.columns.values[1:len(j1.variables[j1._variables_key]) + 1]))

@ -1,12 +1,11 @@
import sys
sys.path.append("../../classes/")
import unittest import unittest
import os import os
import glob import glob
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import utility.sample_importer as si from ...classes.utility.sample_importer import SampleImporter
import structure_graph.sample_path as sp from ...classes.structure_graph.sample_path import SamplePath
import json import json
@ -16,7 +15,7 @@ class TestSampleImporter(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls) -> None: def setUpClass(cls) -> None:
with open("../../data/networks_and_trajectories_binary_data_01_3.json") as f: with open("./main_package/data/networks_and_trajectories_binary_data_01_3.json") as f:
raw_data = json.load(f) raw_data = json.load(f)
trajectory_list_raw= raw_data[0]["samples"] trajectory_list_raw= raw_data[0]["samples"]
@ -28,7 +27,7 @@ class TestSampleImporter(unittest.TestCase):
def test_init(self): def test_init(self):
sample_importer = si.SampleImporter( sample_importer = SampleImporter(
trajectory_list=self.trajectory_list, trajectory_list=self.trajectory_list,
variables=self.variables, variables=self.variables,
prior_net_structure=self.prior_net_structure prior_net_structure=self.prior_net_structure
@ -36,7 +35,7 @@ class TestSampleImporter(unittest.TestCase):
sample_importer.import_data() sample_importer.import_data()
s1 = sp.SamplePath(sample_importer) s1 = SamplePath(sample_importer)
s1.build_trajectories() s1.build_trajectories()
s1.build_structure() s1.build_structure()
s1.clear_memory() s1.clear_memory()
@ -50,7 +49,7 @@ class TestSampleImporter(unittest.TestCase):
self.assertIsInstance(s1._importer._df_structure,pd.DataFrame) self.assertIsInstance(s1._importer._df_structure,pd.DataFrame)
def test_order(self): def test_order(self):
sample_importer = si.SampleImporter( sample_importer = SampleImporter(
trajectory_list=self.trajectory_list, trajectory_list=self.trajectory_list,
variables=self.variables, variables=self.variables,
prior_net_structure=self.prior_net_structure prior_net_structure=self.prior_net_structure
@ -58,7 +57,7 @@ class TestSampleImporter(unittest.TestCase):
sample_importer.import_data() sample_importer.import_data()
s1 = sp.SamplePath(sample_importer) s1 = SamplePath(sample_importer)
s1.build_trajectories() s1.build_trajectories()
s1.build_structure() s1.build_structure()
s1.clear_memory() s1.clear_memory()