Old engine for Continuous Time Bayesian Networks. Superseded by reCTBN. 🐍
https://github.com/madlabunimib/PyCTBN
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.1 KiB
95 lines
3.1 KiB
4 years ago
|
|
||
4 years ago
|
# License: MIT License
|
||
|
|
||
|
|
||
4 years ago
|
import unittest
|
||
4 years ago
|
import numpy as np
|
||
3 years ago
|
from pyctbn.legacy.structure_graph.structure import Structure
|
||
4 years ago
|
|
||
|
|
||
|
class TestStructure(unittest.TestCase):
|
||
4 years ago
|
@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)
|
||
4 years ago
|
|
||
|
def test_init(self):
|
||
4 years ago
|
s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
|
||
4 years ago
|
self.assertListEqual(self.labels,s1.nodes_labels)
|
||
4 years ago
|
self.assertIsInstance(s1.nodes_indexes, np.ndarray)
|
||
4 years ago
|
self.assertTrue(np.array_equal(self.indxs, s1.nodes_indexes))
|
||
4 years ago
|
self.assertIsInstance(s1.nodes_values, np.ndarray)
|
||
4 years ago
|
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)
|
||
4 years ago
|
|
||
|
def test_get_node_id(self):
|
||
4 years ago
|
s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
|
||
4 years ago
|
for indx, var in enumerate(self.labels):
|
||
4 years ago
|
self.assertEqual(var, s1.get_node_id(indx))
|
||
|
|
||
4 years ago
|
def test_edges_operations(self):
|
||
|
s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
|
||
|
|
||
|
self.assertTrue(s1.contains_edge(('X','Z')))
|
||
|
s1.add_edge(('Z','X'))
|
||
|
self.assertTrue(s1.contains_edge(('Z','X')))
|
||
|
s1.remove_edge(('Z','X'))
|
||
|
self.assertFalse(s1.contains_edge(('Z','X')))
|
||
|
|
||
4 years ago
|
def test_get_node_indx(self):
|
||
4 years ago
|
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
|
||
4 years ago
|
s1 = Structure(l2, i2, v2, e2, n2)
|
||
4 years ago
|
for indx, var in zip(i2, l2):
|
||
4 years ago
|
self.assertEqual(indx, s1.get_node_indx(var))
|
||
|
|
||
4 years ago
|
def test_get_positional_node_indx(self):
|
||
4 years ago
|
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
|
||
4 years ago
|
s1 = Structure(l2, i2, v2, e2, n2)
|
||
4 years ago
|
for indx, var in enumerate(s1.nodes_labels):
|
||
4 years ago
|
self.assertEqual(indx, s1.get_positional_node_indx(var))
|
||
|
|
||
4 years ago
|
def test_get_states_number(self):
|
||
4 years ago
|
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
|
||
4 years ago
|
s1 = Structure(l2, i2, v2, e2, n2)
|
||
4 years ago
|
for val, node in zip(v2, l2):
|
||
|
self.assertEqual(val, s1.get_states_number(node))
|
||
4 years ago
|
|
||
4 years ago
|
def test_equality(self):
|
||
4 years ago
|
s1 = 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)
|
||
4 years ago
|
self.assertEqual(s1, s2)
|
||
4 years ago
|
self.assertNotEqual(s1,4)
|
||
4 years ago
|
|
||
|
def test_repr(self):
|
||
4 years ago
|
s1 = Structure(self.labels, self.indxs, self.vals, self.edges, self.vars_numb)
|
||
4 years ago
|
print(s1)
|
||
|
|
||
4 years ago
|
|
||
4 years ago
|
if __name__ == '__main__':
|
||
|
unittest.main()
|