parent
9ad0cac265
commit
462e29bb17
@ -0,0 +1,71 @@ |
||||
import unittest |
||||
import networkx as nx |
||||
|
||||
import sample_path as sp |
||||
import network_graph as ng |
||||
|
||||
|
||||
class TestNetworkGraph(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self.s1 = sp.SamplePath('../data', 'samples', 'dyn.str', 'variables', 'Time', 'Name') |
||||
self.s1.build_trajectories() |
||||
self.s1.build_structure() |
||||
|
||||
def test_init(self): |
||||
g1 = ng.NetworkGraph(self.s1.structure) |
||||
self.assertEqual(self.s1.structure, g1.graph_struct) |
||||
self.assertIsInstance(g1.graph, nx.DiGraph) |
||||
#TODO MANCANO TUTTI I TEST DI INIZIALIZZAZIONE DEI DATI PRIVATI della classe aggiungere le property necessarie |
||||
|
||||
def test_add_nodes(self): |
||||
g1 = ng.NetworkGraph(self.s1.structure) |
||||
g1.add_nodes(self.s1.structure.list_of_nodes_labels()) |
||||
for n1, n2 in zip(g1.get_nodes(), self.s1.structure.list_of_nodes_labels()): |
||||
self.assertEqual(n1, n2) |
||||
|
||||
def test_add_edges(self): |
||||
g1 = ng.NetworkGraph(self.s1.structure) |
||||
g1.add_edges(self.s1.structure.list_of_edges()) |
||||
for e in self.s1.structure.list_of_edges(): |
||||
self.assertIn(tuple(e), g1.get_edges()) |
||||
|
||||
def test_get_ordered_by_indx_set_of_parents(self): |
||||
g1 = ng.NetworkGraph(self.s1.structure) |
||||
g1.add_nodes(self.s1.structure.list_of_nodes_labels()) |
||||
g1.add_edges(self.s1.structure.list_of_edges()) |
||||
sorted_par_list_aggregated_info = g1.get_ordered_by_indx_set_of_parents(g1.get_nodes()[2]) |
||||
self.test_aggregated_par_list_data(g1,g1.get_nodes()[2], sorted_par_list_aggregated_info) |
||||
|
||||
def test_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): |
||||
g1 = ng.NetworkGraph(self.s1.structure) |
||||
g1.add_nodes(self.s1.structure.list_of_nodes_labels()) |
||||
g1.add_edges(self.s1.structure.list_of_edges()) |
||||
sorted_list_of_par_lists = g1.get_ord_set_of_par_of_all_nodes() |
||||
for node, par_list in zip(g1.get_nodes_sorted_by_indx(), sorted_list_of_par_lists): |
||||
self.test_aggregated_par_list_data(g1, node, par_list) |
||||
|
||||
def test_get_ordered_by_indx_parents_values_for_all_nodes(self): |
||||
g1 = ng.NetworkGraph(self.s1.structure) |
||||
g1.add_nodes(self.s1.structure.list_of_nodes_labels()) |
||||
g1.add_edges(self.s1.structure.list_of_edges()) |
||||
g1.aggregated_info_about_nodes_parents = g1.get_ord_set_of_par_of_all_nodes() |
||||
print(g1.get_ordered_by_indx_parents_values_for_all_nodes()) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
@ -1,55 +1,70 @@ |
||||
import unittest |
||||
import pandas as pd |
||||
|
||||
import structure as st |
||||
|
||||
|
||||
class TestStructure(unittest.TestCase): |
||||
def setUp(self): |
||||
self.structure_frame = pd.DataFrame([{"From":"X","To":"Z"},{"From":"Y","To":"Z"},{"From":"Z","To":"Y"}]) |
||||
self.structure_frame = pd.DataFrame([{"From":"X","To":"Z"}, {"From":"X","To":"Y"},{"From":"Y","To":"X"}, |
||||
{"From":"Y","To":"Z"},{"From":"Z","To":"Y"}, {"From":"Z","To":"X"} ]) |
||||
self.variables_frame = pd.DataFrame([{"Name":"X","Value":3},{"Name":"Y","Value":3},{"Name":"Z","Value":3}]) |
||||
|
||||
def test_init(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
self.assertTrue(self.structure_frame.equals(s1.structure_frame)) |
||||
self.assertTrue(self.variables_frame.equals(s1.variables_frame)) |
||||
self.assertEqual(self.variables_frame.columns.values[0], s1.name_label) |
||||
self.assertEqual(self.variables_frame.columns.values[1], s1.value_label) |
||||
#print(len(self.variables_frame.index)) |
||||
self.assertEqual(len(self.variables_frame.index), s1.total_variables_number) |
||||
|
||||
def test_list_of_edges(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
records = self.structure_frame.to_records(index=False) |
||||
result = list(records) |
||||
for e1, e2 in zip(result, s1.list_of_edges()): |
||||
self.assertEqual(e1, e2) |
||||
|
||||
def test_list_of_nodes_labels(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
self.assertEqual(list(self.variables_frame['Name']), s1.list_of_nodes_labels()) |
||||
|
||||
def test_get_node_id(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
for indx, var in enumerate(list(self.variables_frame['Name'])): |
||||
self.assertEqual(var, s1.get_node_id(indx)) |
||||
|
||||
def test_get_node_indx(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
for indx, var in enumerate(list(self.variables_frame['Name'])): |
||||
filtered_frame = self.variables_frame.drop(self.variables_frame[self.variables_frame['Name'] == 'Y'].index) |
||||
#print(filtered_frame) |
||||
s1 = st.Structure(self.structure_frame, filtered_frame, len(self.variables_frame.index)) |
||||
for indx, var in zip(filtered_frame.index, filtered_frame['Name']): |
||||
self.assertEqual(indx, s1.get_node_indx(var)) |
||||
|
||||
def test_list_of_node_indxs(self): |
||||
filtered_frame = self.variables_frame.drop(self.variables_frame[self.variables_frame['Name'] == 'Y'].index) |
||||
# print(filtered_frame) |
||||
s1 = st.Structure(self.structure_frame, filtered_frame, len(self.variables_frame.index)) |
||||
|
||||
for indx1, indx2 in zip(filtered_frame.index, s1.list_of_nodes_indexes()): |
||||
self.assertEqual(indx1, indx2) |
||||
|
||||
def test_get_positional_node_indx(self): |
||||
filtered_frame = self.variables_frame.drop(self.variables_frame[self.variables_frame['Name'] == 'Y'].index) |
||||
# print(filtered_frame) |
||||
s1 = st.Structure(self.structure_frame, filtered_frame, len(self.variables_frame.index)) |
||||
for indx, var in enumerate(s1.list_of_nodes_labels()): |
||||
self.assertEqual(indx, s1.get_positional_node_indx(var)) |
||||
|
||||
def test_get_states_number(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
for indx, row in self.variables_frame.iterrows(): |
||||
self.assertEqual(row[1], s1.get_states_number(row[0])) |
||||
|
||||
def test_get_states_numeber_by_indx(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame) |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
for indx, row in self.variables_frame.iterrows(): |
||||
self.assertEqual(row[1], s1.get_states_number_by_indx(indx)) |
||||
|
||||
def test_list_of_node_indxs(self): |
||||
pass |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
||||
|
Reference in new issue