|
|
|
@ -25,10 +25,10 @@ class TestNetworkGraph(unittest.TestCase): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
self.assertEqual(self.s1.structure, g1._graph_struct) |
|
|
|
|
self.assertIsInstance(g1._graph, nx.DiGraph) |
|
|
|
|
self.assertTrue(np.array_equal(g1._nodes_indexes, self.s1.structure.nodes_indexes)) |
|
|
|
|
self.assertListEqual(g1._nodes_labels, self.s1.structure.nodes_labels) |
|
|
|
|
self.assertTrue(np.array_equal(g1._nodes_values, self.s1.structure.nodes_values)) |
|
|
|
|
self.assertIsNone(g1._fancy_indexing) |
|
|
|
|
#self.assertTrue(np.array_equal(g1._nodes_indexes, self.s1.structure.nodes_indexes)) |
|
|
|
|
#self.assertListEqual(g1._nodes_labels, self.s1.structure.nodes_labels) |
|
|
|
|
#self.assertTrue(np.array_equal(g1._nodes_values, self.s1.structure.nodes_values)) |
|
|
|
|
#self.assertIsNone(g1._fancy_indexing) |
|
|
|
|
self.assertIsNone(g1.time_scalar_indexing_strucure) |
|
|
|
|
self.assertIsNone(g1.transition_scalar_indexing_structure) |
|
|
|
|
self.assertIsNone(g1.transition_filtering) |
|
|
|
@ -46,28 +46,40 @@ class TestNetworkGraph(unittest.TestCase): |
|
|
|
|
for e in self.s1.structure.edges: |
|
|
|
|
self.assertIn(tuple(e), g1.edges) |
|
|
|
|
|
|
|
|
|
def aux_aggregated_par_list_data(self, graph, node_id, sorted_par_list_aggregated_info): |
|
|
|
|
for indx, element in enumerate(sorted_par_list_aggregated_info): |
|
|
|
|
if indx == 0: |
|
|
|
|
self.assertEqual(graph.get_parents_by_id(node_id), element) |
|
|
|
|
for j in range(0, len(sorted_par_list_aggregated_info[0]) - 1): |
|
|
|
|
self.assertLess(self.s1.structure.get_node_indx(sorted_par_list_aggregated_info[0][j]), |
|
|
|
|
self.s1.structure.get_node_indx(sorted_par_list_aggregated_info[0][j + 1])) |
|
|
|
|
elif indx == 1: |
|
|
|
|
for node, node_indx in zip(sorted_par_list_aggregated_info[0], sorted_par_list_aggregated_info[1]): |
|
|
|
|
self.assertEqual(graph.get_node_indx(node), node_indx) |
|
|
|
|
else: |
|
|
|
|
for node, node_val in zip(sorted_par_list_aggregated_info[0], sorted_par_list_aggregated_info[2]): |
|
|
|
|
self.assertEqual(graph._graph_struct.get_states_number(node), node_val) |
|
|
|
|
|
|
|
|
|
def test_get_ord_set_of_par_of_all_nodes(self): |
|
|
|
|
def test_fast_init(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
g1.fast_init(node) |
|
|
|
|
self.assertIsNotNone(g1._graph.nodes) |
|
|
|
|
self.assertIsNotNone(g1._graph.edges) |
|
|
|
|
self.assertIsInstance(g1._time_scalar_indexing_structure, np.ndarray) |
|
|
|
|
self.assertIsInstance(g1._transition_scalar_indexing_structure, np.ndarray) |
|
|
|
|
self.assertIsInstance(g1._time_filtering, np.ndarray) |
|
|
|
|
self.assertIsInstance(g1._transition_filtering, np.ndarray) |
|
|
|
|
self.assertIsInstance(g1._p_combs_structure, np.ndarray) |
|
|
|
|
self.assertIsInstance(g1._aggregated_info_about_nodes_parents, tuple) |
|
|
|
|
|
|
|
|
|
def test_get_ordered_by_indx_set_of_parents(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
sorted_list_of_par_lists = g1.get_ord_set_of_par_of_all_nodes() |
|
|
|
|
for node, par_list in zip(g1.nodes, sorted_list_of_par_lists): |
|
|
|
|
self.aux_aggregated_par_list_data(g1, node, par_list) |
|
|
|
|
|
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
aggr_info = g1.get_ordered_by_indx_set_of_parents(node) |
|
|
|
|
for indx in range(len(aggr_info[0]) - 1 ): |
|
|
|
|
self.assertLess(g1.get_node_indx(aggr_info[0][indx]), g1.get_node_indx(aggr_info[0][indx + 1])) |
|
|
|
|
for par, par_indx in zip(aggr_info[0], aggr_info[1]): |
|
|
|
|
self.assertEqual(g1.get_node_indx(par), par_indx) |
|
|
|
|
for par, par_val in zip(aggr_info[0], aggr_info[2]): |
|
|
|
|
self.assertEqual(g1._graph_struct.get_states_number(par), par_val) |
|
|
|
|
|
|
|
|
|
def test_build_time_scalar_indexing_structure_for_a_node(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
aggr_info = g1.get_ordered_by_indx_set_of_parents(node) |
|
|
|
|
self.aux_build_time_scalar_indexing_structure_for_a_node(g1, node, aggr_info[1], |
|
|
|
|
aggr_info[0], aggr_info[2]) |
|
|
|
|
|
|
|
|
|
def aux_build_time_scalar_indexing_structure_for_a_node(self, graph, node_id, parents_indxs, parents_labels, parents_vals): |
|
|
|
|
time_scalar_indexing = graph.build_time_scalar_indexing_structure_for_a_node(node_id, parents_vals) |
|
|
|
@ -81,6 +93,15 @@ class TestNetworkGraph(unittest.TestCase): |
|
|
|
|
t_vec = t_vec.cumprod() |
|
|
|
|
self.assertTrue(np.array_equal(time_scalar_indexing, t_vec)) |
|
|
|
|
|
|
|
|
|
def test_build_transition_scalar_indexing_structure_for_a_node(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
aggr_info = g1.get_ordered_by_indx_set_of_parents(node) |
|
|
|
|
self.aux_build_transition_scalar_indexing_structure_for_a_node(g1, node, aggr_info[1], |
|
|
|
|
aggr_info[0], aggr_info[2]) |
|
|
|
|
|
|
|
|
|
def aux_build_transition_scalar_indexing_structure_for_a_node(self, graph, node_id, parents_indxs, parents_labels, |
|
|
|
|
parents_values): |
|
|
|
|
transition_scalar_indexing = graph.build_transition_scalar_indexing_structure_for_a_node(node_id, |
|
|
|
@ -96,93 +117,54 @@ class TestNetworkGraph(unittest.TestCase): |
|
|
|
|
m_vec = m_vec.cumprod() |
|
|
|
|
self.assertTrue(np.array_equal(transition_scalar_indexing, m_vec)) |
|
|
|
|
|
|
|
|
|
def test_build_transition_scalar_indexing_structure(self): |
|
|
|
|
def test_build_time_columns_filtering_structure_for_a_node(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
g1._aggregated_info_about_nodes_parents = g1.get_ord_set_of_par_of_all_nodes() |
|
|
|
|
p_labels = [i[0] for i in g1._aggregated_info_about_nodes_parents] |
|
|
|
|
p_vals = g1.get_ordered_by_indx_parents_values_for_all_nodes() |
|
|
|
|
fancy_indx = g1.build_fancy_indexing_structure(0) |
|
|
|
|
for node_id, p_i ,p_l, p_v in zip(g1._graph_struct.nodes_labels, fancy_indx, p_labels, p_vals): |
|
|
|
|
self.aux_build_transition_scalar_indexing_structure_for_a_node(g1, node_id, p_i ,p_l, p_v) |
|
|
|
|
|
|
|
|
|
def test_build_time_scalar_indexing_structure(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
g1._aggregated_info_about_nodes_parents = g1.get_ord_set_of_par_of_all_nodes() |
|
|
|
|
fancy_indx = g1.build_fancy_indexing_structure(0) |
|
|
|
|
p_labels = [i[0] for i in g1._aggregated_info_about_nodes_parents] |
|
|
|
|
p_vals = g1.get_ordered_by_indx_parents_values_for_all_nodes() |
|
|
|
|
#print(fancy_indx) |
|
|
|
|
for node_id, p_indxs, p_labels, p_v in zip(g1._graph_struct.nodes_labels, fancy_indx, p_labels, p_vals): |
|
|
|
|
self.aux_build_time_scalar_indexing_structure_for_a_node(g1, node_id, p_indxs, p_labels, p_v) |
|
|
|
|
|
|
|
|
|
def test_build_time_columns_filtering_structure(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
g1._aggregated_info_about_nodes_parents = g1.get_ord_set_of_par_of_all_nodes() |
|
|
|
|
g1._fancy_indexing = g1.build_fancy_indexing_structure(0) |
|
|
|
|
g1.build_time_columns_filtering_structure() |
|
|
|
|
t_filter = [] |
|
|
|
|
for node_id, p_indxs in zip(g1.nodes, g1._fancy_indexing): |
|
|
|
|
single_filter = [] |
|
|
|
|
single_filter.append(g1.get_node_indx(node_id)) |
|
|
|
|
single_filter.extend(p_indxs) |
|
|
|
|
t_filter.append(np.array(single_filter)) |
|
|
|
|
#print(t_filter) |
|
|
|
|
for a1, a2 in zip(g1.time_filtering, t_filter): |
|
|
|
|
self.assertTrue(np.array_equal(a1, a2)) |
|
|
|
|
|
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
aggr_info = g1.get_ordered_by_indx_set_of_parents(node) |
|
|
|
|
self.aux_build_time_columns_filtering_structure_for_a_node(g1, node, aggr_info[1]) |
|
|
|
|
|
|
|
|
|
def aux_build_time_columns_filtering_structure_for_a_node(self, graph, node_id, p_indxs): |
|
|
|
|
graph.build_time_columns_filtering_for_a_node(graph.get_node_indx(node_id), p_indxs) |
|
|
|
|
single_filter = [] |
|
|
|
|
single_filter.append(graph.get_node_indx(node_id)) |
|
|
|
|
single_filter.extend(p_indxs) |
|
|
|
|
self.assertTrue(np.array_equal(graph.build_time_columns_filtering_for_a_node(graph.get_node_indx(node_id), |
|
|
|
|
p_indxs),np.array(single_filter))) |
|
|
|
|
def test_build_transition_columns_filtering_structure(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
g1._aggregated_info_about_nodes_parents = g1.get_ord_set_of_par_of_all_nodes() |
|
|
|
|
g1._fancy_indexing = g1.build_fancy_indexing_structure(0) |
|
|
|
|
g1.build_transition_columns_filtering_structure() |
|
|
|
|
m_filter = [] |
|
|
|
|
for node_id, p_indxs in zip(g1.nodes, g1._fancy_indexing): |
|
|
|
|
single_filter = [] |
|
|
|
|
single_filter.append(g1.get_node_indx(node_id) + g1._graph_struct.total_variables_number) |
|
|
|
|
single_filter.append(g1.get_node_indx(node_id)) |
|
|
|
|
single_filter.extend(p_indxs) |
|
|
|
|
m_filter.append(np.array(single_filter)) |
|
|
|
|
for a1, a2 in zip(g1.transition_filtering, m_filter): |
|
|
|
|
self.assertTrue(np.array_equal(a1, a2)) |
|
|
|
|
|
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
aggr_info = g1.get_ordered_by_indx_set_of_parents(node) |
|
|
|
|
self.aux_build_time_columns_filtering_structure_for_a_node(g1, node, aggr_info[1]) |
|
|
|
|
|
|
|
|
|
def aux_build_transition_columns_filtering_structure(self, graph, node_id, p_indxs): |
|
|
|
|
single_filter = [] |
|
|
|
|
single_filter.append(graph.get_node_indx(node_id) + graph._graph_struct.total_variables_number) |
|
|
|
|
single_filter.append(graph.get_node_indx(node_id)) |
|
|
|
|
single_filter.extend(p_indxs) |
|
|
|
|
self.assertTrue(np.array_equal(graph.build_transition_filtering_for_a_node(graph.get_node_indx(node_id), |
|
|
|
|
|
|
|
|
|
p_indxs), np.array(single_filter))) |
|
|
|
|
def test_build_p_combs_structure(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.add_nodes(self.s1.structure.nodes_labels) |
|
|
|
|
g1.add_edges(self.s1.structure.edges) |
|
|
|
|
g1._aggregated_info_about_nodes_parents = g1.get_ord_set_of_par_of_all_nodes() |
|
|
|
|
p_vals = g1.get_ordered_by_indx_parents_values_for_all_nodes() |
|
|
|
|
p_combs = g1.build_p_combs_structure() |
|
|
|
|
|
|
|
|
|
for matrix, p_v in zip(p_combs, p_vals): |
|
|
|
|
p_possible_vals = [] |
|
|
|
|
for val in p_v: |
|
|
|
|
vals = [v for v in range(val)] |
|
|
|
|
p_possible_vals.extend(vals) |
|
|
|
|
comb_struct = set(itertools.product(p_possible_vals,repeat=len(p_v))) |
|
|
|
|
#print(comb_struct) |
|
|
|
|
for comb in comb_struct: |
|
|
|
|
self.assertIn(np.array(comb), matrix) |
|
|
|
|
|
|
|
|
|
def test_fast_init(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g2 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|
g1.init_graph() |
|
|
|
|
for indx, node in enumerate(g1.nodes): |
|
|
|
|
g2.fast_init(node) |
|
|
|
|
self.assertListEqual(g2._fancy_indexing, g1._fancy_indexing[indx]) |
|
|
|
|
self.assertTrue(np.array_equal(g2.time_scalar_indexing_strucure, g1.time_scalar_indexing_strucure[indx])) |
|
|
|
|
self.assertTrue(np.array_equal(g2.transition_scalar_indexing_structure, g1.transition_scalar_indexing_structure[indx])) |
|
|
|
|
self.assertTrue(np.array_equal(g2.time_filtering, g1.time_filtering[indx])) |
|
|
|
|
self.assertTrue(np.array_equal(g2.transition_filtering, g1.transition_filtering[indx])) |
|
|
|
|
self.assertTrue(np.array_equal(g2.p_combs, g1.p_combs[indx])) |
|
|
|
|
for node in self.s1.structure.nodes_labels: |
|
|
|
|
aggr_info = g1.get_ordered_by_indx_set_of_parents(node) |
|
|
|
|
self.aux_build_p_combs_structure(g1, aggr_info[2]) |
|
|
|
|
|
|
|
|
|
def aux_build_p_combs_structure(self, graph, p_vals): |
|
|
|
|
p_combs = graph.build_p_comb_structure_for_a_node(p_vals) |
|
|
|
|
p_possible_vals = [] |
|
|
|
|
for val in p_vals: |
|
|
|
|
vals = [v for v in range(val)] |
|
|
|
|
p_possible_vals.extend(vals) |
|
|
|
|
comb_struct = set(itertools.product(p_possible_vals,repeat=len(p_vals))) |
|
|
|
|
for comb in comb_struct: |
|
|
|
|
self.assertIn(np.array(comb), p_combs) |
|
|
|
|
|
|
|
|
|
def test_get_parents_by_id(self): |
|
|
|
|
g1 = ng.NetworkGraph(self.s1.structure) |
|
|
|
|