From 2792c7aaaf95165cd1320c162670399efbf5f279 Mon Sep 17 00:00:00 2001 From: Pietro Date: Sat, 10 Apr 2021 10:21:31 +0200 Subject: [PATCH] Network Generator --- .gitignore | 1 + .../structure_graph/network_generator.py | 54 +++++++++++++++++++ .../test_trajectorygenerator.py | 11 ---- test_trajectorygenerator.py | 4 +- 4 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 PyCTBN/PyCTBN/structure_graph/network_generator.py delete mode 100644 PyCTBN/tests/structure_graph/test_trajectorygenerator.py diff --git a/.gitignore b/.gitignore index d3436cb..2e3feea 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ __pycache__ **/.scannerwork **/build test.py +test_1.py diff --git a/PyCTBN/PyCTBN/structure_graph/network_generator.py b/PyCTBN/PyCTBN/structure_graph/network_generator.py new file mode 100644 index 0000000..823bb13 --- /dev/null +++ b/PyCTBN/PyCTBN/structure_graph/network_generator.py @@ -0,0 +1,54 @@ +from .structure import Structure +from .network_graph import NetworkGraph +from .conditional_intensity_matrix import ConditionalIntensityMatrix +from .set_of_cims import SetOfCims +import numpy as np + +class NetworkGenerator(object): + def __init__(self, labels, vals): + self._labels = labels + self._vals = vals + self._indxs = np.array([i for i, l in enumerate(labels)]) + self._graph = None + self._cims = None + + def generate_graph(self): + edges = [(i, j) for i in self._labels for j in self._labels if np.random.binomial(1, 0.5) == 1 and i != j] + indxs = np.array([i for i, l in enumerate(self._labels)]) + s = Structure(self._labels, self._indxs, self._vals, edges, len(self._labels)) + self._graph = NetworkGraph(s) + self._graph.add_nodes(s.nodes_labels) + self._graph.add_edges(s.edges) + + def generate_cims(self, min_val, max_val): + self._cims = {} + + for i, l in enumerate(self._labels): + p_info = self._graph.get_ordered_by_indx_set_of_parents(l) + combs = self._graph.build_p_comb_structure_for_a_node(p_info[2]) + + node_cims = [] + for comb in combs: + cim = np.empty(shape=(self._vals[i], self._vals[i])) + cim[:] = np.nan + + for i, c in enumerate(cim): + diag = (max_val - min_val) * np.random.random_sample() + min_val + row = np.random.rand(1, len(cim))[0] + row /= (sum(row) - row[i]) + row *= diag + row[i] = -1 * diag + cim[i] = row + + node_cims.append(ConditionalIntensityMatrix(cim = cim)) + + self._cims[l] = SetOfCims(node_id = l, parents_states_number = p_info[2], node_states_number = self._vals[i], p_combs = combs, + cims = np.array(node_cims)) + + @property + def graph(self) -> NetworkGraph: + return self._graph + + @property + def cims(self) -> NetworkGraph: + return self._cims \ No newline at end of file diff --git a/PyCTBN/tests/structure_graph/test_trajectorygenerator.py b/PyCTBN/tests/structure_graph/test_trajectorygenerator.py deleted file mode 100644 index 171e152..0000000 --- a/PyCTBN/tests/structure_graph/test_trajectorygenerator.py +++ /dev/null @@ -1,11 +0,0 @@ -import unittest - -from ...PyCTBN.structure_graph.trajectory_generator import TrajectoryGenerator - -class TestTrajectoryGenerator(unittest.TestCase) - @classmethod - def setUpClass(cls) -> None: - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/test_trajectorygenerator.py b/test_trajectorygenerator.py index 4534a9c..a35c513 100644 --- a/test_trajectorygenerator.py +++ b/test_trajectorygenerator.py @@ -22,10 +22,10 @@ class TestTrajectoryGenerator(unittest.TestCase): tg = TrajectoryGenerator(self.j1) end_time = random.randint(5, 100) sigma = tg.CTBN_Sample(end_time) - self.assertLessEqual(sigma.loc[len(sigma) - 1].at["Time"], end_time) + self.assertLessEqual(sigma.times.loc[len(sigma) - 1].at["Time"], end_time) for index, row in sigma.iterrows(): if index > 0: - self.assertLess(sigma.loc[index - 1].at["Time"], row.at["Time"]) + self.assertLess(sigma.times.loc[index - 1].at["Time"], row.at["Time"]) diff = abs(sum(sigma.loc[index - 1, sigma.columns != "Time"]) - sum(sigma.loc[index, sigma.columns != "Time"])) self.assertEqual(diff, 1)