commit
1bdc8a7231
@ -0,0 +1,37 @@ |
||||
from abc import ABC, abstractmethod |
||||
import abstract_importer as ai |
||||
|
||||
|
||||
class AbstractSamplePath(ABC): |
||||
|
||||
def __init__(self, importer: ai.AbstractImporter): |
||||
self.importer = importer |
||||
self._trajectories = None |
||||
self._structure = None |
||||
super().__init__() |
||||
|
||||
@abstractmethod |
||||
def build_trajectories(self): |
||||
""" |
||||
Builds the Trajectory object that will contain all the trajectories. |
||||
Assigns the Trajectoriy object to the instance attribute _trajectories |
||||
Clears all the unused dataframes in Importer Object |
||||
|
||||
Parameters: |
||||
void |
||||
Returns: |
||||
void |
||||
""" |
||||
pass |
||||
|
||||
@abstractmethod |
||||
def build_structure(self): |
||||
""" |
||||
Builds the Structure object that aggregates all the infos about the net. |
||||
Assigns the Structure object to the instance attribuite _structure |
||||
Parameters: |
||||
void |
||||
Returns: |
||||
void |
||||
""" |
||||
pass |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,61 @@ |
||||
import unittest |
||||
import numpy as np |
||||
|
||||
import cache as ch |
||||
import set_of_cims as soci |
||||
|
||||
|
||||
class TestCache(unittest.TestCase): |
||||
|
||||
def test_init(self): |
||||
c1 = ch.Cache() |
||||
self.assertFalse(c1.list_of_sets_of_parents) |
||||
self.assertFalse(c1.actual_cache) |
||||
|
||||
def test_put(self): |
||||
c1 = ch.Cache() |
||||
pset1 = {'X', 'Y'} |
||||
sofc1 = soci.SetOfCims('Z', [], 3, np.array([])) |
||||
c1.put(pset1, sofc1) |
||||
self.assertEqual(1, len(c1.actual_cache)) |
||||
self.assertEqual(1, len(c1.list_of_sets_of_parents)) |
||||
self.assertEqual(sofc1, c1.actual_cache[0]) |
||||
pset2 = {'X'} |
||||
sofc2 = soci.SetOfCims('Z', [], 3, np.array([])) |
||||
c1.put(pset2, sofc2) |
||||
self.assertEqual(2, len(c1.actual_cache)) |
||||
self.assertEqual(2, len(c1.list_of_sets_of_parents)) |
||||
self.assertEqual(sofc2, c1.actual_cache[1]) |
||||
|
||||
def test_find(self): |
||||
c1 = ch.Cache() |
||||
pset1 = {'X', 'Y'} |
||||
sofc1 = soci.SetOfCims('Z', [], 3, np.array([])) |
||||
c1.put(pset1, sofc1) |
||||
self.assertEqual(1, len(c1.actual_cache)) |
||||
self.assertEqual(1, len(c1.list_of_sets_of_parents)) |
||||
self.assertIsInstance(c1.find(pset1), soci.SetOfCims) |
||||
self.assertEqual(sofc1, c1.find(pset1)) |
||||
self.assertIsInstance(c1.find({'Y', 'X'}), soci.SetOfCims) |
||||
self.assertEqual(sofc1, c1.find({'Y', 'X'})) |
||||
self.assertIsNone(c1.find({'X'})) |
||||
|
||||
def test_clear(self): |
||||
c1 = ch.Cache() |
||||
pset1 = {'X', 'Y'} |
||||
sofc1 = soci.SetOfCims('Z', [], 3, np.array([])) |
||||
c1.put(pset1, sofc1) |
||||
self.assertEqual(1, len(c1.actual_cache)) |
||||
self.assertEqual(1, len(c1.list_of_sets_of_parents)) |
||||
c1.clear() |
||||
self.assertFalse(c1.list_of_sets_of_parents) |
||||
self.assertFalse(c1.actual_cache) |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
@ -1,80 +1,73 @@ |
||||
import sys |
||||
sys.path.append("/Users/Zalum/Desktop/Tesi/CTBN_Project/main_package/classes/") |
||||
import unittest |
||||
import pandas as pd |
||||
import numpy as np |
||||
import structure as st |
||||
|
||||
|
||||
class TestStructure(unittest.TestCase): |
||||
def setUp(self): |
||||
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}]) |
||||
@classmethod |
||||
def setUpClass(cls): |
||||
cls.labels = ['X','Y','Z'] |
||||
cls.indxs = np.array([0,1,2]) |
||||
cls.vals = np.array([3,3,3]) |
||||
cls.edges = [('X','Z'),('Y','Z'), ('Z','Y')] |
||||
cls.vars_numb = len(cls.labels) |
||||
|
||||
def test_init(self): |
||||
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, 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, len(self.variables_frame.index)) |
||||
self.assertEqual(list(self.variables_frame['Name']), s1.list_of_nodes_labels()) |
||||
s1 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) |
||||
self.assertListEqual(self.labels,s1.nodes_labels) |
||||
self.assertIsInstance(s1.nodes_indexes, np.ndarray) |
||||
self.assertTrue(np.array_equal(self.indxs, s1.nodes_indexes)) |
||||
self.assertIsInstance(s1.nodes_values, np.ndarray) |
||||
self.assertTrue(np.array_equal(self.vals, s1.nodes_values)) |
||||
self.assertListEqual(self.edges, s1.edges) |
||||
self.assertEqual(self.vars_numb, s1.total_variables_number) |
||||
|
||||
def test_get_node_id(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
for indx, var in enumerate(list(self.variables_frame['Name'])): |
||||
s1 = st.Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb) |
||||
for indx, var in enumerate(self.labels): |
||||
self.assertEqual(var, s1.get_node_id(indx)) |
||||
|
||||
def test_get_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 zip(filtered_frame.index, filtered_frame['Name']): |
||||
l2 = self.labels[:] |
||||
l2.remove('Y') |
||||
i2 = self.indxs.copy() |
||||
np.delete(i2, 1) |
||||
v2 = self.vals.copy() |
||||
np.delete(v2, 1) |
||||
e2 = [('X','Z')] |
||||
n2 = self.vars_numb - 1 |
||||
s1 = st.Structure(l2, i2, v2, e2, n2) |
||||
for indx, var in zip(i2, l2): |
||||
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()): |
||||
l2 = self.labels[:] |
||||
l2.remove('Y') |
||||
i2 = self.indxs.copy() |
||||
np.delete(i2, 1) |
||||
v2 = self.vals.copy() |
||||
np.delete(v2, 1) |
||||
e2 = [('X', 'Z')] |
||||
n2 = self.vars_numb - 1 |
||||
s1 = st.Structure(l2, i2, v2, e2, n2) |
||||
for indx, var in enumerate(s1.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, 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, len(self.variables_frame.index)) |
||||
for indx, row in self.variables_frame.iterrows(): |
||||
self.assertEqual(row[1], s1.get_states_number_by_indx(indx)) |
||||
l2 = self.labels[:] |
||||
l2.remove('Y') |
||||
i2 = self.indxs.copy() |
||||
np.delete(i2, 1) |
||||
v2 = self.vals.copy() |
||||
np.delete(v2, 1) |
||||
e2 = [('X', 'Z')] |
||||
n2 = self.vars_numb - 1 |
||||
s1 = st.Structure(l2, i2, v2, e2, n2) |
||||
for val, node in zip(v2, l2): |
||||
self.assertEqual(val, s1.get_states_number(node)) |
||||
|
||||
def test_remove_node(self): |
||||
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) |
||||
s1.remove_node('Y') |
||||
print(s1.variables_frame) |
||||
print(s1.structure_frame) |
||||
print(s1.get_node_indx('Z')) |
||||
print(s1.get_positional_node_indx('Z')) |
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
||||
|
Reference in new issue