1
0
Fork 0

Refactor complete_test

parallel_struct_est
Filippo Martini 4 years ago
parent 81eb5ac0f9
commit 4ed17ccb85
  1. 73
      PyCTBN/PyCTBN/network_graph.py
  2. 29
      PyCTBN/PyCTBN/parameters_estimator.py
  3. 9
      PyCTBN/PyCTBN/structure.py
  4. 20
      PyCTBN/PyCTBN/structure_estimator.py
  5. 2
      PyCTBN/tests/test_networkgraph.py

@ -78,6 +78,20 @@ class NetworkGraph:
""" """
self._graph.add_edges_from(list_of_edges) self._graph.add_edges_from(list_of_edges)
def remove_node(self, node_id: str):
self._graph.remove_node(node_id)
self._graph_struct.remove_node(node_id)
self.clear_indexing_filtering_structures()
def clear_indexing_filtering_structures(self):
self._aggregated_info_about_nodes_parents = None
self._time_scalar_indexing_structure = None
self._transition_scalar_indexing_structure = None
self._time_filtering = None
self._transition_filtering = None
self._p_combs_structure = None
def get_ordered_by_indx_set_of_parents(self, node: str) -> typing.Tuple: def get_ordered_by_indx_set_of_parents(self, node: str) -> typing.Tuple:
"""Builds the aggregated structure that holds all the infos relative to the parent set of the node, namely """Builds the aggregated structure that holds all the infos relative to the parent set of the node, namely
(parents_labels, parents_indexes, parents_cardinalities). (parents_labels, parents_indexes, parents_cardinalities).
@ -235,62 +249,3 @@ class NetworkGraph:
def p_combs(self) -> np.ndarray: def p_combs(self) -> np.ndarray:
return self._p_combs_structure return self._p_combs_structure
"""
##############These Methods are actually unused but could become useful in the near future################
def init_graph(self):
self.add_nodes(self._nodes_labels)
self.add_edges(self._graph_struct.edges)
self._aggregated_info_about_nodes_parents = self.get_ord_set_of_par_of_all_nodes()
self._fancy_indexing = self.build_fancy_indexing_structure(0)
self.build_scalar_indexing_structures()
self.build_time_columns_filtering_structure()
self.build_transition_columns_filtering_structure()
self._p_combs_structure = self.build_p_combs_structure()
def build_time_columns_filtering_structure(self):
nodes_indxs = self._nodes_indexes
self._time_filtering = [np.append(np.array([node_indx], dtype=np.int), p_indxs).astype(np.int)
for node_indx, p_indxs in zip(nodes_indxs, self._fancy_indexing)]
def build_transition_columns_filtering_structure(self):
nodes_number = self._graph_struct.total_variables_number
nodes_indxs = self._nodes_indexes
self._transition_filtering = [np.array([node_indx + nodes_number, node_indx, *p_indxs], dtype=np.int)
for node_indx, p_indxs in zip(nodes_indxs,
self._fancy_indexing)]
def build_scalar_indexing_structures(self):
parents_values_for_all_nodes = self.get_ordered_by_indx_parents_values_for_all_nodes()
build_transition_scalar_indexing_structure_for_a_node = \
self.build_transition_scalar_indexing_structure_for_a_node
build_time_scalar_indexing_structure_for_a_node = self.build_time_scalar_indexing_structure_for_a_node
aggr = [(build_transition_scalar_indexing_structure_for_a_node(node_id, p_vals),
build_time_scalar_indexing_structure_for_a_node(node_id, p_vals))
for node_id, p_vals in
zip(self._nodes_labels,
parents_values_for_all_nodes)]
self._transition_scalar_indexing_structure = [i[0] for i in aggr]
self._time_scalar_indexing_structure = [i[1] for i in aggr]
def build_p_combs_structure(self):
parents_values_for_all_nodes = self.get_ordered_by_indx_parents_values_for_all_nodes()
p_combs_struct = [self.build_p_comb_structure_for_a_node(p_vals) for p_vals in parents_values_for_all_nodes]
return p_combs_struct
def get_ord_set_of_par_of_all_nodes(self):
get_ordered_by_indx_set_of_parents = self.get_ordered_by_indx_set_of_parents
result = [get_ordered_by_indx_set_of_parents(node) for node in self._nodes_labels]
return result
def get_ordered_by_indx_parents_values_for_all_nodes(self):
pars_values = [i[2] for i in self._aggregated_info_about_nodes_parents]
return pars_values
def build_fancy_indexing_structure(self, start_indx):
if start_indx > 0:
pass
else:
fancy_indx = [i[1] for i in self._aggregated_info_about_nodes_parents]
return fancy_indx
"""

@ -46,12 +46,12 @@ class ParametersEstimator:
node_indx = self._net_graph.get_node_indx(node_id) node_indx = self._net_graph.get_node_indx(node_id)
state_res_times = self._single_set_of_cims._state_residence_times state_res_times = self._single_set_of_cims._state_residence_times
transition_matrices = self._single_set_of_cims._transition_matrices transition_matrices = self._single_set_of_cims._transition_matrices
self.compute_state_res_time_for_node(node_indx, times, ParametersEstimator.compute_state_res_time_for_node(node_indx, times,
trajectories, trajectories,
self._net_graph.time_filtering, self._net_graph.time_filtering,
self._net_graph.time_scalar_indexing_strucure, self._net_graph.time_scalar_indexing_strucure,
state_res_times) state_res_times)
self.compute_state_transitions_for_a_node(node_indx, ParametersEstimator.compute_state_transitions_for_a_node(node_indx,
trajectories, trajectories,
self._net_graph.transition_filtering, self._net_graph.transition_filtering,
self._net_graph.transition_scalar_indexing_structure, self._net_graph.transition_scalar_indexing_structure,
@ -108,30 +108,7 @@ class ParametersEstimator:
M_raveled[diag_indices] = 0 M_raveled[diag_indices] = 0
M_raveled[diag_indices] = np.sum(M, axis=2).ravel() M_raveled[diag_indices] = np.sum(M, axis=2).ravel()
"""
##############These Methods are actually unused but could become useful in the near future################
def init_sets_cims_container(self):
self.sets_of_cims_struct = acims.SetsOfCimsContainer(self._net_graph.nodes,
self._net_graph.nodes_values,
self._net_graph.
get_ordered_by_indx_parents_values_for_all_nodes(),
self._net_graph.p_combs)
def compute_parameters(self):
for indx, aggr in enumerate(zip(self._net_graph.nodes, self.sets_of_cims_struct.sets_of_cims)):
self.compute_state_res_time_for_node(self._net_graph.get_node_indx(aggr[0]), self.sample_path.trajectories.times,
self.sample_path.trajectories.trajectory,
self._net_graph.time_filtering[indx],
self._net_graph.time_scalar_indexing_strucure[indx],
aggr[1]._state_residence_times)
self.compute_state_transitions_for_a_node(self._net_graph.get_node_indx(aggr[0]),
self.sample_path.trajectories.complete_trajectory,
self._net_graph.transition_filtering[indx],
self._net_graph.transition_scalar_indexing_structure[indx],
aggr[1]._transition_matrices)
aggr[1].build_cims(aggr[1]._state_residence_times, aggr[1]._transition_matrices)
"""

@ -27,6 +27,15 @@ class Structure:
self._edges_list = edges_list self._edges_list = edges_list
self._total_variables_number = total_variables_number self._total_variables_number = total_variables_number
def remove_node(self, node_id: str):
node_positional_indx = self._nodes_labels_list.index(node_id)
del self._nodes_labels_list[node_positional_indx]
self._nodes_indexes_arr = np.delete(self._nodes_indexes_arr, node_positional_indx)
self._nodes_vals_arr = np.delete(self._nodes_vals_arr, node_positional_indx)
self._edges_list = [(from_node, to_node) for (from_node, to_node) in self._edges_list if (from_node != node_id
and to_node != node_id)]
#self._total_variables_number -= 1
@property @property
def edges(self) -> ty.List: def edges(self) -> ty.List:
return self._edges_list return self._edges_list

@ -113,6 +113,8 @@ class StructureEstimator:
parents = np.append(parents, test_parent) parents = np.append(parents, test_parent)
sorted_parents = self._nodes[np.isin(self._nodes, parents)] sorted_parents = self._nodes[np.isin(self._nodes, parents)]
cims_filter = sorted_parents != test_parent cims_filter = sorted_parents != test_parent
"""
sofc1 = cache.find(set(p_set)) sofc1 = cache.find(set(p_set))
if not sofc1: if not sofc1:
@ -128,10 +130,11 @@ class StructureEstimator:
p1.fast_init(test_child) p1.fast_init(test_child)
sofc1 = p1.compute_parameters_for_node(test_child, self._times, self._trajectories) sofc1 = p1.compute_parameters_for_node(test_child, self._times, self._trajectories)
cache.put(set(p_set), sofc1) cache.put(set(p_set), sofc1)
sofc2 = None """
#sofc2 = None
p_set.insert(0, test_parent) p_set.insert(0, test_parent)
if p_set: sofc2 = cache.find(set(p_set))
sofc2 = cache.find(set(p_set))
if not sofc2: if not sofc2:
complete_info.append(test_parent) complete_info.append(test_parent)
bool_mask2 = np.isin(self._nodes, complete_info) bool_mask2 = np.isin(self._nodes, complete_info)
@ -146,6 +149,17 @@ class StructureEstimator:
p2.fast_init(test_child) p2.fast_init(test_child)
sofc2 = p2.compute_parameters_for_node(test_child, self._times, self._trajectories) sofc2 = p2.compute_parameters_for_node(test_child, self._times, self._trajectories)
cache.put(set(p_set), sofc2) cache.put(set(p_set), sofc2)
del p_set[0]
sofc1 = cache.find(set(p_set))
if not sofc1:
g2.remove_node(test_parent)
g2.fast_init(test_child)
p2 = ParametersEstimator(g2)
p2.fast_init(test_child)
sofc1 = p2.compute_parameters_for_node(test_child, self._times, self._trajectories)
cache.put(set(p_set), sofc1)
for cim1, p_comb in zip(sofc1.actual_cims, sofc1.p_combs): for cim1, p_comb in zip(sofc1.actual_cims, sofc1.p_combs):
cond_cims = sofc2.filter_cims_with_mask(cims_filter, p_comb) cond_cims = sofc2.filter_cims_with_mask(cims_filter, p_comb)
for cim2 in cond_cims: for cim2 in cond_cims:

@ -54,6 +54,8 @@ class TestNetworkGraph(unittest.TestCase):
self.assertIsInstance(g1._transition_filtering, np.ndarray) self.assertIsInstance(g1._transition_filtering, np.ndarray)
self.assertIsInstance(g1._p_combs_structure, np.ndarray) self.assertIsInstance(g1._p_combs_structure, np.ndarray)
self.assertIsInstance(g1._aggregated_info_about_nodes_parents, tuple) self.assertIsInstance(g1._aggregated_info_about_nodes_parents, tuple)
g1.remove_node(node)
print(self.s1.structure.nodes_labels)
def test_get_ordered_by_indx_set_of_parents(self): def test_get_ordered_by_indx_set_of_parents(self):
g1 = NetworkGraph(self.s1.structure) g1 = NetworkGraph(self.s1.structure)