1
0
Fork 0

Network Generator

master
Pietro 4 years ago
parent a68b5e689d
commit 2792c7aaaf
  1. 1
      .gitignore
  2. 54
      PyCTBN/PyCTBN/structure_graph/network_generator.py
  3. 11
      PyCTBN/tests/structure_graph/test_trajectorygenerator.py
  4. 4
      test_trajectorygenerator.py

1
.gitignore vendored

@ -8,3 +8,4 @@ __pycache__
**/.scannerwork
**/build
test.py
test_1.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

@ -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()

@ -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)