1
0
Fork 0

Modify Structure class members

parallel_struct_est
philpMartin 4 years ago
parent cef0280c09
commit f373a9fffc
  1. 3
      main_package/classes/cache.py
  2. 72
      main_package/classes/network_graph.py
  3. 6
      main_package/classes/sample_path.py
  4. 36
      main_package/classes/structure.py
  5. 109
      main_package/classes/structure_estimator.py
  6. 58
      main_package/tests/test_structure.py
  7. 3
      main_package/tests/test_structure_estimator.py

@ -10,6 +10,8 @@ class Cache:
def find(self, parents_comb: typing.Set): def find(self, parents_comb: typing.Set):
try: try:
#print("Cache State:", self.list_of_sets_of_indxs)
#print("Look For:", parents_comb)
result = self.actual_cache[self.list_of_sets_of_indxs.index(parents_comb)] result = self.actual_cache[self.list_of_sets_of_indxs.index(parents_comb)]
print("CACHE HIT!!!!") print("CACHE HIT!!!!")
return result return result
@ -17,6 +19,7 @@ class Cache:
return None return None
def put(self, parents_comb: typing.Set, socim: sofc.SetOfCims): def put(self, parents_comb: typing.Set, socim: sofc.SetOfCims):
#print("Putting in cache:", parents_comb)
self.list_of_sets_of_indxs.append(parents_comb) self.list_of_sets_of_indxs.append(parents_comb)
self.actual_cache.append(socim) self.actual_cache.append(socim)

@ -40,7 +40,6 @@ class NetworkGraph():
def add_nodes(self, list_of_nodes): def add_nodes(self, list_of_nodes):
#self.graph.add_nodes_from(list_of_nodes) #self.graph.add_nodes_from(list_of_nodes)
set_node_attr = nx.set_node_attributes
nodes_indxs = self.graph_struct.list_of_nodes_indexes() nodes_indxs = self.graph_struct.list_of_nodes_indexes()
nodes_vals = self.graph_struct.nodes_values() nodes_vals = self.graph_struct.nodes_values()
pos = 0 pos = 0
@ -59,19 +58,8 @@ class NetworkGraph():
#p_indxes= [] #p_indxes= []
#p_values = [] #p_values = []
get_node_indx = self.get_node_indx get_node_indx = self.get_node_indx
get_states_number_by_indx = self.get_states_number_by_indx
p_indxes = [get_node_indx(node) for node in sorted_parents] p_indxes = [get_node_indx(node) for node in sorted_parents]
p_values = [get_states_number_by_indx(indx) for indx in p_indxes] p_values = [self.get_states_number(node) for node in sorted_parents]
"""for n in parents:
#indx = self.graph_struct.get_node_indx(n)
#print(indx)
#ordered_set[n] = indx
node_indx = self.get_node_indx(n)
p_indxes.append(node_indx)
#p_values.append(self.graph_struct.get_states_number(n))
p_values.append(self.get_states_number_by_indx(node_indx))"""
#ordered_set = (sorted_parents, p_indxes, p_values)
return (sorted_parents, p_indxes, p_values) return (sorted_parents, p_indxes, p_values)
def get_ord_set_of_par_of_all_nodes(self): def get_ord_set_of_par_of_all_nodes(self):
@ -106,16 +94,6 @@ class NetworkGraph():
return states_number_list return states_number_list
def build_fancy_indexing_structure(self, start_indx): def build_fancy_indexing_structure(self, start_indx):
"""list_of_parents_list = self.get_ord_set_of_par_of_all_nodes()
#print(list_of_parents_list)
index_structure = []
for i, list_of_parents in enumerate(list_of_parents_list):
indexes_for_a_node = []
for j, node in enumerate(list_of_parents):
indexes_for_a_node.append(self.get_node_indx(node) + start_indx)
index_structure.append(np.array(indexes_for_a_node, dtype=np.int))
#print(index_structure)
return index_structure"""
if start_indx > 0: if start_indx > 0:
pass pass
else: else:
@ -123,35 +101,19 @@ class NetworkGraph():
return fancy_indx return fancy_indx
def build_time_scalar_indexing_structure_for_a_node(self, node_indx, parents_indxs): def build_time_scalar_indexing_structure_for_a_node(self, node_id, parents_indxs):
#print(node_indx) T_vector = np.array([self.get_states_number(node_id)])
#print("Parents_id", parents_indxs)
#T_vector = np.array([self.graph_struct.variables_frame.iloc[node_id, 1].astype(np.int)])
get_states_number_by_indx = self.graph_struct.get_states_number_by_indx
T_vector = np.array([get_states_number_by_indx(node_indx)])
#print(T_vector)
#T_vector = np.append(T_vector, [get_states_number_by_indx(x) for x in parents_indxs])
T_vector = np.append(T_vector, parents_indxs) T_vector = np.append(T_vector, parents_indxs)
#print(T_vector)
T_vector = T_vector.cumprod().astype(np.int) T_vector = T_vector.cumprod().astype(np.int)
return T_vector
# print(T_vector) # print(T_vector)
return T_vector
def build_time_scalar_indexing_structure(self):
#parents_indexes_list = self._fancy_indexing
"""for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing):
self._time_scalar_indexing_structure.append(
self.build_time_scalar_indexing_structure_for_a_node(node_indx, p_indxs))"""
build_time_scalar_indexing_structure_for_a_node = self.build_time_scalar_indexing_structure_for_a_node
self._time_scalar_indexing_structure = [build_time_scalar_indexing_structure_for_a_node(node_indx, p_indxs)
for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(),
self.get_ordered_by_indx_parents_values_for_all_nodes())]
def build_transition_scalar_indexing_structure_for_a_node(self, node_indx, parents_indxs): def build_transition_scalar_indexing_structure_for_a_node(self, node_id, parents_indxs):
#M_vector = np.array([self.graph_struct.variables_frame.iloc[node_id, 1], #M_vector = np.array([self.graph_struct.variables_frame.iloc[node_id, 1],
#self.graph_struct.variables_frame.iloc[node_id, 1].astype(np.int)]) #self.graph_struct.variables_frame.iloc[node_id, 1].astype(np.int)])
node_states_number = self.get_states_number_by_indx(node_indx) node_states_number = self.get_states_number(node_id)
get_states_number_by_indx = self.graph_struct.get_states_number_by_indx #get_states_number_by_indx = self.graph_struct.get_states_number_by_indx
M_vector = np.array([node_states_number, M_vector = np.array([node_states_number,
node_states_number]) node_states_number])
#M_vector = np.append(M_vector, [get_states_number_by_indx(x) for x in parents_indxs]) #M_vector = np.append(M_vector, [get_states_number_by_indx(x) for x in parents_indxs])
@ -159,32 +121,22 @@ class NetworkGraph():
M_vector = M_vector.cumprod().astype(np.int) M_vector = M_vector.cumprod().astype(np.int)
return M_vector return M_vector
def build_transition_scalar_indexing_structure(self):
#parents_indexes_list = self._fancy_indexing
"""for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing):
self._transition_scalar_indexing_structure.append(
self.build_transition_scalar_indexing_structure_for_a_node(node_indx, p_indxs))"""
build_transition_scalar_indexing_structure_for_a_node = self.build_transition_scalar_indexing_structure_for_a_node
self._transition_scalar_indexing_structure = \
[build_transition_scalar_indexing_structure_for_a_node(node_indx, p_indxs)
for node_indx, p_indxs in
zip(self.graph_struct.list_of_nodes_indexes(),
self.get_ordered_by_indx_parents_values_for_all_nodes())]
def build_time_columns_filtering_structure(self): def build_time_columns_filtering_structure(self):
#parents_indexes_list = self._fancy_indexing #parents_indexes_list = self._fancy_indexing
"""for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing): """for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing):
self._time_filtering.append(np.append(np.array([node_indx], dtype=np.int), p_indxs).astype(np.int))""" self._time_filtering.append(np.append(np.array([node_indx], dtype=np.int), p_indxs).astype(np.int))"""
nodes_indxs = self.graph_struct.list_of_nodes_indexes()
self._time_filtering = [np.append(np.array([node_indx], dtype=np.int), p_indxs).astype(np.int) self._time_filtering = [np.append(np.array([node_indx], dtype=np.int), p_indxs).astype(np.int)
for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing)] for node_indx, p_indxs in zip(nodes_indxs, self._fancy_indexing)]
def build_transition_columns_filtering_structure(self): def build_transition_columns_filtering_structure(self):
#parents_indexes_list = self._fancy_indexing #parents_indexes_list = self._fancy_indexing
nodes_number = self.graph_struct.total_variables_number nodes_number = self.graph_struct.total_variables_number
"""for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing): """for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), self._fancy_indexing):
self._transition_filtering.append(np.array([node_indx + nodes_number, node_indx, *p_indxs], dtype=np.int))""" self._transition_filtering.append(np.array([node_indx + nodes_number, node_indx, *p_indxs], dtype=np.int))"""
nodes_indxs = self.graph_struct.list_of_nodes_indexes()
self._transition_filtering = [np.array([node_indx + nodes_number, node_indx, *p_indxs], dtype=np.int) self._transition_filtering = [np.array([node_indx + nodes_number, node_indx, *p_indxs], dtype=np.int)
for node_indx, p_indxs in zip(self.graph_struct.list_of_nodes_indexes(), for node_indx, p_indxs in zip(nodes_indxs,
self._fancy_indexing)] self._fancy_indexing)]
def build_scalar_indexing_structures(self): def build_scalar_indexing_structures(self):
@ -194,7 +146,7 @@ class NetworkGraph():
aggr = [(build_transition_scalar_indexing_structure_for_a_node(node_indx, p_indxs), aggr = [(build_transition_scalar_indexing_structure_for_a_node(node_indx, p_indxs),
build_time_scalar_indexing_structure_for_a_node(node_indx, p_indxs)) build_time_scalar_indexing_structure_for_a_node(node_indx, p_indxs))
for node_indx, p_indxs in for node_indx, p_indxs in
zip(self.graph_struct.list_of_nodes_indexes(), zip(self._nodes_labels,
parents_values_for_all_nodes)] parents_values_for_all_nodes)]
self._transition_scalar_indexing_structure = [i[0] for i in aggr] self._transition_scalar_indexing_structure = [i[0] for i in aggr]
self._time_scalar_indexing_structure = [i[1] for i in aggr] self._time_scalar_indexing_structure = [i[1] for i in aggr]

@ -33,7 +33,11 @@ class SamplePath:
def build_structure(self): def build_structure(self):
self.total_variables_count = len(self.importer.sorter) self.total_variables_count = len(self.importer.sorter)
self._structure = st.Structure(self.importer.structure, self.importer.variables, labels = self.importer._df_variables['Name'].to_list()
indxs = self.importer._df_variables.index.to_numpy()
vals = self.importer._df_variables['Value'].to_numpy()
edges = list(self.importer._df_structure.to_records(index=False))
self._structure = st.Structure(labels, indxs, vals, edges,
self.total_variables_count) self.total_variables_count)
@property @property

@ -10,44 +10,48 @@ class Structure:
rispetto alle colonne del dataset rispetto alle colonne del dataset
""" """
def __init__(self, structure, variables, total_variables_number): def __init__(self, nodes_label_list, node_indexes_arr, nodes_vals_arr, edges_list, total_variables_number):
self.structure_frame = structure #self.structure_frame = structure
self.variables_frame = variables #self.variables_frame = variables
self.nodes_labels_list = nodes_label_list
self.nodes_indexes_arr = node_indexes_arr
self.nodes_vals_arr = nodes_vals_arr
self.edges_list = edges_list
self.total_variables_number = total_variables_number self.total_variables_number = total_variables_number
self.name_label = variables.columns.values[0] #self.name_label = variables.columns.values[0]
self.value_label = variables.columns.values[1] #self.value_label = variables.columns.values[1]
def list_of_edges(self): def list_of_edges(self):
records = self.structure_frame.to_records(index=False) #records = self.structure_frame.to_records(index=False)
edges_list = list(records) #edges_list = list(records)
return edges_list return self.edges_list
def list_of_nodes_labels(self): def list_of_nodes_labels(self):
return self.variables_frame[self.name_label].values.tolist() return self.nodes_labels_list
def list_of_nodes_indexes(self): def list_of_nodes_indexes(self):
return self.variables_frame.index.to_list() return self.nodes_indexes_arr
def get_node_id(self, node_indx): def get_node_id(self, node_indx):
return self.variables_frame[self.name_label][node_indx] return self.nodes_labels_list[node_indx]
def get_node_indx(self, node_id): def get_node_indx(self, node_id):
return self.variables_frame[self.name_label][self.variables_frame[self.name_label] == node_id].index[0] return self.nodes_indexes_arr[self.nodes_labels_list.index(node_id)]
def get_positional_node_indx(self, node_id): def get_positional_node_indx(self, node_id):
return np.flatnonzero(self.variables_frame[self.name_label] == node_id)[0] return self.nodes_labels_list.index(node_id)
def get_states_number(self, node): def get_states_number(self, node):
#print("node", node) #print("node", node)
return self.variables_frame[self.value_label][self.get_node_indx(node)] return self.nodes_vals_arr[self.get_positional_node_indx(node)]
def get_states_number_by_indx(self, node_indx): def get_states_number_by_indx(self, node_indx):
#print(self.value_label) #print(self.value_label)
#print("Node indx", node_indx) #print("Node indx", node_indx)
return self.variables_frame[self.value_label][node_indx] return self.nodes_vals_arr[node_indx]
def nodes_values(self): def nodes_values(self):
return self.variables_frame[self.value_label].to_list() return self.nodes_vals_arr
def total_variables_number(self): def total_variables_number(self):
return self.total_variables_number return self.total_variables_number

@ -5,10 +5,6 @@ import networkx as nx
from scipy.stats import f as f_dist from scipy.stats import f as f_dist
from scipy.stats import chi2 as chi2_dist from scipy.stats import chi2 as chi2_dist
import sample_path as sp
import structure as st import structure as st
import network_graph as ng import network_graph as ng
import parameters_estimator as pe import parameters_estimator as pe
@ -19,7 +15,10 @@ class StructureEstimator:
def __init__(self, sample_path, exp_test_alfa, chi_test_alfa): def __init__(self, sample_path, exp_test_alfa, chi_test_alfa):
self.sample_path = sample_path self.sample_path = sample_path
self.complete_graph_frame = self.build_complete_graph_frame(self.sample_path.structure.list_of_nodes_labels()) #self.complete_graph_frame = self.build_complete_graph_frame(self.sample_path.structure.list_of_nodes_labels())
self.nodes = np.array(self.sample_path.structure.list_of_nodes_labels())
self.nodes_vals = self.sample_path.structure.nodes_vals_arr
self.nodes_indxs = self.sample_path.structure.nodes_indexes_arr
self.complete_graph = self.build_complete_graph(self.sample_path.structure.list_of_nodes_labels()) self.complete_graph = self.build_complete_graph(self.sample_path.structure.list_of_nodes_labels())
self.exp_test_sign = exp_test_alfa self.exp_test_sign = exp_test_alfa
self.chi_test_alfa = chi_test_alfa self.chi_test_alfa = chi_test_alfa
@ -36,52 +35,23 @@ class StructureEstimator:
complete_graph.add_edges_from(itertools.permutations(node_ids, 2)) complete_graph.add_edges_from(itertools.permutations(node_ids, 2))
return complete_graph return complete_graph
#TODO Tutti i valori che riguardano il test child possono essere settati una volta sola #TODO Tutti i valori che riguardano il test child possono essere settati una volta sola
def complete_test(self, tmp_df, test_parent, test_child, parent_set, child_states_numb): def complete_test(self, test_parent, test_child, parent_set, child_states_numb, tot_vars_count):
p_set = parent_set[:] p_set = parent_set[:]
complete_info = parent_set[:] complete_info = parent_set[:]
complete_info.append(test_parent)
#tmp_df = self.complete_graph_frame.loc[self.complete_graph_frame['To'].isin([test_child])]
#tmp_df = self.complete_graph_frame.loc[np.in1d(self.complete_graph_frame['To'], test_child)]
d2 = tmp_df.loc[tmp_df['From'].isin(complete_info)]
complete_info.append(test_child) complete_info.append(test_child)
values_frame = self.sample_path.structure.variables_frame
v2 = values_frame.loc[
values_frame['Name'].isin(complete_info)]
#print(tmp_df)
#d1 = tmp_df.loc[tmp_df['From'].isin(parent_set)]
#parent_set.append(test_child)
#print(parent_set)
"""v1 = self.sample_path.structure.variables_frame.loc[self.sample_path.structure.variables_frame['Name'].isin(parent_set)]
s1 = st.Structure(d1, v1, self.sample_path.total_variables_count)
g1 = ng.NetworkGraph(s1)
g1.init_graph()"""
#parent_set.append(test_parent)
"""d2 = tmp_df.loc[tmp_df['From'].isin(parent_set)]
v2 = self.sample_path.structure.variables_frame.loc[self.sample_path.structure.variables_frame['Name'].isin(parent_set)]
s2 = st.Structure(d2, v2, self.sample_path.total_variables_count)
g2 = ng.NetworkGraph(s2)
g2.init_graph()"""
#parent_set.append(test_child)
#sofc1 = None
#if not sofc1:
if not p_set: if not p_set:
sofc1 = self.cache.find(test_child) sofc1 = self.cache.find(test_child)
else: else:
sofc1 = self.cache.find(set(p_set)) sofc1 = self.cache.find(set(p_set))
if not sofc1: if not sofc1:
#d1 = tmp_df.loc[tmp_df['From'].isin(parent_set)] bool_mask1 = np.isin(self.nodes,complete_info)
d1 = d2[d2.From != test_parent] l1 = list(self.nodes[bool_mask1])
indxs1 = self.nodes_indxs[bool_mask1]
#v1 = self.sample_path.structure.variables_frame.loc[ vals1 = self.nodes_vals[bool_mask1]
#self.sample_path.structure.variables_frame['Name'].isin(parent_set)] eds1 = list(itertools.product(parent_set,test_child))
v1 = v2[v2.Name != test_parent]
#print("D1", d1)
#print("V1", v1)
#TODO il numero di variabili puo essere passato dall'esterno #TODO il numero di variabili puo essere passato dall'esterno
s1 = st.Structure(d1, v1, self.sample_path.total_variables_count) s1 = st.Structure(l1, indxs1, vals1, eds1, tot_vars_count)
g1 = ng.NetworkGraph(s1) g1 = ng.NetworkGraph(s1)
g1.init_graph() g1.init_graph()
p1 = pe.ParametersEstimator(self.sample_path, g1) p1 = pe.ParametersEstimator(self.sample_path, g1)
@ -93,7 +63,8 @@ class StructureEstimator:
else: else:
self.cache.put(set(p_set), sofc1) self.cache.put(set(p_set), sofc1)
sofc2 = None sofc2 = None
p_set.append(test_parent) #p_set.append(test_parent)
p_set.insert(0, test_parent)
if p_set: if p_set:
#p_set.append(test_parent) #p_set.append(test_parent)
#print("PSET ", p_set) #print("PSET ", p_set)
@ -108,15 +79,13 @@ class StructureEstimator:
p2.compute_parameters_for_node(test_child) p2.compute_parameters_for_node(test_child)
sofc2 = p2.sets_of_cims_struct.sets_of_cims[s2.get_positional_node_indx(test_child)]""" sofc2 = p2.sets_of_cims_struct.sets_of_cims[s2.get_positional_node_indx(test_child)]"""
if not sofc2: if not sofc2:
#print("Cache Miss SOC2") complete_info.append(test_parent)
#parent_set.append(test_parent) bool_mask2 = np.isin(self.nodes, complete_info)
#d2 = tmp_df.loc[tmp_df['From'].isin(p_set)] l2 = list(self.nodes[bool_mask2])
#v2 = self.sample_path.structure.variables_frame.loc[ indxs2 = self.nodes_indxs[bool_mask2]
#self.sample_path.structure.variables_frame['Name'].isin(parent_set)] vals2 = self.nodes_vals[bool_mask2]
#print("D2", d2) eds2 = list(itertools.product(p_set, test_child))
#print("V2", v2) s2 = st.Structure(l2, indxs2, vals2, eds2, tot_vars_count)
#s2 = st.Structure(d2, v2, self.sample_path.total_variables_count)
s2 = st.Structure(d2, v2, self.sample_path.total_variables_count)
g2 = ng.NetworkGraph(s2) g2 = ng.NetworkGraph(s2)
g2.init_graph() g2.init_graph()
p2 = pe.ParametersEstimator(self.sample_path, g2) p2 = pe.ParametersEstimator(self.sample_path, g2)
@ -151,7 +120,7 @@ class StructureEstimator:
for val in range(0, child_states_numb): for val in range(0, child_states_numb):
if F_stats[val] < f_dist.ppf(self.exp_test_sign / 2, r1s[val], r2s[val]) or \ if F_stats[val] < f_dist.ppf(self.exp_test_sign / 2, r1s[val], r2s[val]) or \
F_stats[val] > f_dist.ppf(1 - self.exp_test_sign / 2, r1s[val], r2s[val]): F_stats[val] > f_dist.ppf(1 - self.exp_test_sign / 2, r1s[val], r2s[val]):
print("CONDITIONALLY DEPENDENT EXP") #print("CONDITIONALLY DEPENDENT EXP")
return False return False
#M1_no_diag = self.remove_diagonal_elements(cim1.state_transition_matrix) #M1_no_diag = self.remove_diagonal_elements(cim1.state_transition_matrix)
#M2_no_diag = self.remove_diagonal_elements(cim2.state_transition_matrix) #M2_no_diag = self.remove_diagonal_elements(cim2.state_transition_matrix)
@ -176,26 +145,26 @@ class StructureEstimator:
#print("Chi Quantile", chi_2_quantile) #print("Chi Quantile", chi_2_quantile)
if Chi > chi_2_quantile: if Chi > chi_2_quantile:
#if np.any(chi_stats > chi_2_quantile): #if np.any(chi_stats > chi_2_quantile):
print("CONDITIONALLY DEPENDENT CHI") #print("CONDITIONALLY DEPENDENT CHI")
return False return False
#print("Chi test", Chi) #print("Chi test", Chi)
return True return True
def one_iteration_of_CTPC_algorithm(self, var_id): def one_iteration_of_CTPC_algorithm(self, var_id, tot_vars_count):
print("TESTING VAR", var_id)
u = list(self.complete_graph.predecessors(var_id)) u = list(self.complete_graph.predecessors(var_id))
tests_parents_numb = len(u) #tests_parents_numb = len(u)
complete_frame = self.complete_graph_frame #complete_frame = self.complete_graph_frame
test_frame = complete_frame.loc[complete_frame['To'].isin([var_id])] #test_frame = complete_frame.loc[complete_frame['To'].isin([var_id])]
child_states_numb = self.sample_path.structure.get_states_number(var_id) child_states_numb = self.sample_path.structure.get_states_number(var_id)
b = 0 b = 0
while b < len(u): while b < len(u):
#for parent_id in u: #for parent_id in u:
parent_indx = 0 parent_indx = 0
while u and parent_indx < tests_parents_numb and b < len(u): while parent_indx < len(u):
# list_without_test_parent = u.remove(parent_id) # list_without_test_parent = u.remove(parent_id)
removed = False removed = False
#print("b", b)
#print("Parent Indx", parent_indx)
#if not list(self.generate_possible_sub_sets_of_size(u, b, u[parent_indx])): #if not list(self.generate_possible_sub_sets_of_size(u, b, u[parent_indx])):
#break #break
S = self.generate_possible_sub_sets_of_size(u, b, parent_indx) S = self.generate_possible_sub_sets_of_size(u, b, parent_indx)
@ -204,19 +173,9 @@ class StructureEstimator:
for parents_set in S: for parents_set in S:
#print("Parent Set", parents_set) #print("Parent Set", parents_set)
#print("Test Parent", u[parent_indx]) #print("Test Parent", u[parent_indx])
if self.complete_test(test_frame, u[parent_indx], var_id, parents_set, child_states_numb): if self.complete_test(u[parent_indx], var_id, parents_set, child_states_numb, tot_vars_count):
#print("Removing EDGE:", u[parent_indx], var_id) print("Removing EDGE:", u[parent_indx], var_id)
self.complete_graph.remove_edge(u[parent_indx], var_id) self.complete_graph.remove_edge(u[parent_indx], var_id)
#print(self.complete_graph_frame)
"""self.complete_graph_frame = \
self.complete_graph_frame.drop(
self.complete_graph_frame[(self.complete_graph_frame.From ==
u[parent_indx]) & (self.complete_graph_frame.To == var_id)].index)"""
complete_frame.drop(complete_frame[(complete_frame.From == u[parent_indx]) &
(complete_frame.To == var_id)].index, inplace=True)
#print(self.complete_graph_frame)
#u.remove(u[parent_indx])
del u[parent_indx] del u[parent_indx]
removed = True removed = True
#else: #else:
@ -227,8 +186,6 @@ class StructureEstimator:
self.cache.clear() self.cache.clear()
def generate_possible_sub_sets_of_size(self, u, size, parent_indx): def generate_possible_sub_sets_of_size(self, u, size, parent_indx):
#print("Inside Generate subsets", u)
#print("InsideGenerate Subsets", parent_id)
list_without_test_parent = u[:] list_without_test_parent = u[:]
del list_without_test_parent[parent_indx] del list_without_test_parent[parent_indx]
# u.remove(parent_id) # u.remove(parent_id)
@ -243,10 +200,10 @@ class StructureEstimator:
def ctpc_algorithm(self): def ctpc_algorithm(self):
ctpc_algo = self.one_iteration_of_CTPC_algorithm ctpc_algo = self.one_iteration_of_CTPC_algorithm
nodes = self.sample_path.structure.list_of_nodes_labels() total_vars_numb = self.sample_path.total_variables_count
#for node_id in self.sample_path.structure.list_of_nodes_labels(): #for node_id in self.sample_path.structure.list_of_nodes_labels():
#print("TESTING VAR:", node_id) #print("TESTING VAR:", node_id)
#self.one_iteration_of_CTPC_algorithm(node_id) #self.one_iteration_of_CTPC_algorithm(node_id)
#print(self.complete_graph_frame) #print(self.complete_graph_frame)
[ctpc_algo(n) for n in nodes] [ctpc_algo(n, total_vars_numb) for n in self.nodes]

@ -2,13 +2,18 @@ import sys
sys.path.append("/Users/Zalum/Desktop/Tesi/CTBN_Project/main_package/classes/") sys.path.append("/Users/Zalum/Desktop/Tesi/CTBN_Project/main_package/classes/")
import unittest import unittest
import pandas as pd import pandas as pd
import numpy as np
import sample_path as sp
import structure as st import structure as st
import network_graph as ng
import parameters_estimator as pe
class TestStructure(unittest.TestCase): class TestStructure(unittest.TestCase):
def setUp(self): def setUp(self):
self.structure_frame = pd.DataFrame([{"From":"X","To":"Z"}, {"From":"X","To":"Y"},{"From":"Y","To":"X"}, self.structure_frame = pd.DataFrame([{"From":"X","To":"Z"}, {"From":"Y","To":"Z"},
{"From":"Y","To":"Z"},{"From":"Z","To":"Y"}, {"From":"Z","To":"X"} ]) {"From":"Z","To":"Y"} ])
self.variables_frame = pd.DataFrame([{"Name":"X","Value":3},{"Name":"Y","Value":3},{"Name":"Z","Value":3}]) self.variables_frame = pd.DataFrame([{"Name":"X","Value":3},{"Name":"Y","Value":3},{"Name":"Z","Value":3}])
def test_init(self): def test_init(self):
@ -68,13 +73,52 @@ class TestStructure(unittest.TestCase):
for indx, row in self.variables_frame.iterrows(): for indx, row in self.variables_frame.iterrows():
self.assertEqual(row[1], s1.get_states_number_by_indx(indx)) self.assertEqual(row[1], s1.get_states_number_by_indx(indx))
def test_remove_node(self): def test_new_init(self):
s1 = st.Structure(self.structure_frame, self.variables_frame, len(self.variables_frame.index)) #self.variables_frame.drop(self.variables_frame[(self.variables_frame['Name'] == 'Y')].index, inplace=True)
s1.remove_node('Y') """labels = self.variables_frame['Name'].to_list()
print(s1.variables_frame) indxs = self.variables_frame.index.to_numpy()
print(s1.structure_frame) vals = self.variables_frame['Value'].to_numpy()
edges = list(self.structure_frame.to_records(index=False))
print(labels)
print(indxs)
print(vals)
print(edges)
s1 = st.Structure(labels, indxs, vals, edges, len(self.variables_frame.index))
#print(s1.get_node_id(2))
print(s1.get_node_indx('Z')) print(s1.get_node_indx('Z'))
print(s1.get_positional_node_indx('Z')) print(s1.get_positional_node_indx('Z'))
print(s1.get_states_number('Z'))
print(s1.get_states_number_by_indx(1))
[CIM:
[[-4.82318981 1.18421625 3.63997346]
[ 4.44726473 -9.20141291 4.755239 ]
[ 2.93950444 4.36292948 -7.30152554]], CIM:
[[-6.0336893 1.69212904 4.34235011]
[ 3.32692085 -5.03977237 1.7137923 ]
[ 3.65519241 3.81402509 -7.46819716]], CIM:
[[-6.78778897 1.98559721 4.80306557]
[ 1.23811008 -6.26366842 5.0265376 ]
[ 3.02720526 4.0256821 -7.05222539]]]
array([ 3, 9, 27])
array([3, 9])
array([1, 2])
array([4, 1, 2])
"""
sp1 = sp.SamplePath('../data', 'samples', 'dyn.str', 'variables', 'Time', 'Name')
sp1.build_trajectories()
sp1.build_structure()
st1 = st.Structure(['X', 'Y', 'Z'], np.array([0,1,2]), np.array([3,3,3]), [('X', 'Y'), ('Z', 'Y')], sp1.total_variables_count)
g1 = ng.NetworkGraph(st1)
g1.init_graph()
print(g1.transition_scalar_indexing_structure)
print(g1.time_scalar_indexing_strucure)
print(g1.time_filtering)
print(g1.transition_filtering)
p1 = pe.ParametersEstimator(sp1,g1)
p1.init_sets_cims_container()
p1.compute_parameters_for_node('Y')
print(p1.sets_of_cims_struct.sets_of_cims[1].actual_cims)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

@ -1,6 +1,5 @@
import unittest import unittest
from line_profiler import LineProfiler from line_profiler import LineProfiler
from multiprocessing import Pool
import sample_path as sp import sample_path as sp
import structure_estimator as se import structure_estimator as se
@ -31,7 +30,7 @@ class TestStructureEstimator(unittest.TestCase):
lp_wrapper() lp_wrapper()
lp.print_stats() lp.print_stats()
#se1.ctpc_algorithm() #se1.ctpc_algorithm()
print(se1.complete_graph.edges) #print(se1.complete_graph.edges)
def aux_test_complete_test(self, estimator, test_par, test_child, p_set): def aux_test_complete_test(self, estimator, test_par, test_child, p_set):
estimator.complete_test(test_par, test_child, p_set) estimator.complete_test(test_par, test_child, p_set)