1
0
Fork 0

Save Network

master
Pietro 4 years ago
parent dcda13297f
commit ff1765c3a2
  1. 77
      PyCTBN/PyCTBN/structure_graph/network_generator.py
  2. 27
      PyCTBN/PyCTBN/structure_graph/trajectory_generator.py
  3. 4
      test_networkgenerator.py

@ -12,8 +12,8 @@ class NetworkGenerator(object):
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]
def generate_graph(self, density):
edges = [(i, j) for i in self._labels for j in self._labels if np.random.binomial(1, density) == 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)
@ -30,24 +30,71 @@ class NetworkGenerator(object):
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
if len(p_info[0]) != 0:
node_cims = []
for comb in combs:
cim = self.__generate_cim(min_val, max_val, self._vals[i])
node_cims.append(ConditionalIntensityMatrix(cim = cim))
else:
node_cims = []
cim = self.__generate_cim(min_val, max_val, self._vals[i])
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))
def __generate_cim(self, min_val, max_val, shape):
cim = np.empty(shape=(shape, shape))
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
return cim
def out_json(self, filename):
dyn_str = [{"From": edge[0], "To": edge[1]} for edge in self._graph.edges]
variables = [{"Name": l, "Value": self._vals[i]} for i, l in enumerate(self._labels)]
dyn_cims = {}
for i, l in enumerate(self._labels):
dyn_cims[l] = {}
parents = self._graph.get_ordered_by_indx_set_of_parents(l)[0]
for j, comb in enumerate(self._cims[l].p_combs):
comb_key = ""
for k, val in enumerate(comb):
comb_key += parents[k] + "=" + str(val)
if k < len(comb) - 1:
comb_key += ","
cim = self._cims[l].filter_cims_with_mask(np.array([True for p in parents]), comb)
if len(parents) == 1:
cim = cim[comb[0]].cim
elif len(parents) == 0:
cim = cim[0].cim
else:
cim = cim.cim
dyn_cims[l][comb_key] = [dict([(str(i), val) for i, val in enumerate(row)]) for row in cim]
data = {
"dyn.str": dyn_str,
"variables": variables,
"dyn.cims": dyn_cims,
"samples": []
}
print(data)
""" path = os.getcwd()
with open(path + "/" + filename, "w") as json_file:
json.dump(data, json_file) """
@property
def graph(self) -> NetworkGraph:
return self._graph

@ -48,8 +48,15 @@ class TrajectoryGenerator(object):
for i in range(0, time.size):
if np.isnan(time[i]):
cim = self._cims[self._vnames[i]].filter_cims_with_mask(np.array([True for p in self._parents[self._vnames[i]]]),
[current_values.at[p] for p in self._parents[self._vnames[i]]])[0].cim
n_parents = len(self._parents[self._vnames[i]])
cim_obj = self._cims[self._vnames[i]].filter_cims_with_mask(np.array([True for p in self._parents[self._vnames[i]]]),
[current_values.at[p] for p in self._parents[self._vnames[i]]])
if n_parents == 1:
cim = cim_obj[current_values.at[self._parents[self._vnames[i]][0]]].cim
else:
cim = cim_obj[0].cim
param = -1 * cim[current_values.at[self._vnames[i]]][current_values.at[self._vnames[i]]]
time[i] = t + random.exponential(scale = param)
@ -62,8 +69,20 @@ class TrajectoryGenerator(object):
self._generated_trajectory = sigma
return sigma
else:
cim = self._cims[self._vnames[next]].filter_cims_with_mask(np.array([True for p in self._parents[self._vnames[next]]]),
[current_values.at[p] for p in self._parents[self._vnames[next]]])[0].cim
n_parents = len(self._parents[self._vnames[next]])
cim_obj = self._cims[self._vnames[next]].filter_cims_with_mask(np.array([True for p in self._parents[self._vnames[next]]]),
[current_values.at[p] for p in self._parents[self._vnames[next]]])
if n_parents == 1:
cim = cim_obj[current_values.at[self._parents[self._vnames[next]][0]]].cim
else:
cim = cim_obj[0].cim
""" print(self._vnames[next])
print([current_values.at[p] for p in self._parents[self._vnames[next]]])
print(current_values)
print(cim)
print() """
cim_row = np.array(cim[current_values.at[self._vnames[next]]])
cim_row[current_values.at[self._vnames[next]]] = 0
cim_row /= sum(cim_row)

@ -10,7 +10,7 @@ class TestNetworkGenerator(unittest.TestCase):
card = 3
vals = [card for l in labels]
ng = NetworkGenerator(labels, vals)
ng.generate_graph()
ng.generate_graph(0.3)
self.assertEqual(len(labels), len(ng.graph.nodes))
self.assertEqual(len([edge for edge in ng.graph.edges if edge[0] == edge[1]]), 0)
@ -21,7 +21,7 @@ class TestNetworkGenerator(unittest.TestCase):
cim_min = random.uniform(0.5, 5)
cim_max = random.uniform(0.5, 5) + cim_min
ng = NetworkGenerator(labels, vals)
ng.generate_graph()
ng.generate_graph(0.3)
ng.generate_cims(cim_min, cim_max)
self.assertEqual(len(ng.cims), len(labels))
self.assertListEqual(list(ng.cims.keys()), labels)