|
|
@ -1,161 +1,197 @@ |
|
|
|
import pandas as pd |
|
|
|
|
|
|
|
import numpy as np |
|
|
|
import numpy as np |
|
|
|
import itertools |
|
|
|
import itertools |
|
|
|
import networkx as nx |
|
|
|
import networkx as nx |
|
|
|
|
|
|
|
from networkx.readwrite import json_graph |
|
|
|
|
|
|
|
import json |
|
|
|
|
|
|
|
import typing |
|
|
|
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 sample_path as sp |
|
|
|
import structure as st |
|
|
|
import structure as st |
|
|
|
import network_graph as ng |
|
|
|
import network_graph as ng |
|
|
|
|
|
|
|
import conditional_intensity_matrix as condim |
|
|
|
import parameters_estimator as pe |
|
|
|
import parameters_estimator as pe |
|
|
|
import cache as ch |
|
|
|
import cache as ch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StructureEstimator: |
|
|
|
class StructureEstimator: |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Has the task of estimating the network structure given the trajectories in samplepath. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:sample_path: the sample_path object containing the trajectories and the real structure |
|
|
|
|
|
|
|
:exp_test_sign: the significance level for the exponential Hp test |
|
|
|
|
|
|
|
:chi_test_alfa: the significance level for the chi Hp test |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:nodes: the nodes labels |
|
|
|
|
|
|
|
:nodes_vals: the nodes cardinalities |
|
|
|
|
|
|
|
:nodes_indxs: the nodes indexes |
|
|
|
|
|
|
|
:complete_graph: the complete directed graph built using the nodes labels in nodes |
|
|
|
|
|
|
|
:cache: the cache object |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, sample_path, exp_test_alfa, chi_test_alfa): |
|
|
|
def __init__(self, sample_path: sp.SamplePath, exp_test_alfa: float, chi_test_alfa: float): |
|
|
|
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.nodes = np.array(self.sample_path.structure.nodes_labels) |
|
|
|
self.complete_graph = self.build_complete_graph(self.sample_path.structure.list_of_nodes_labels()) |
|
|
|
self.nodes_vals = self.sample_path.structure.nodes_values |
|
|
|
|
|
|
|
self.nodes_indxs = self.sample_path.structure.nodes_indexes |
|
|
|
|
|
|
|
self.complete_graph = self.build_complete_graph(self.sample_path.structure.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 |
|
|
|
self.cache = ch.Cache() |
|
|
|
self.cache = ch.Cache() |
|
|
|
|
|
|
|
|
|
|
|
def build_complete_graph_frame(self, node_ids): |
|
|
|
def build_complete_graph(self, node_ids: typing.List): |
|
|
|
complete_frame = pd.DataFrame(itertools.permutations(node_ids, 2)) |
|
|
|
""" |
|
|
|
complete_frame.columns = ['From', 'To'] |
|
|
|
Builds a complete directed graph (no self loops) given the nodes labels in the list node_ids: |
|
|
|
return complete_frame |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_complete_graph(self, node_ids): |
|
|
|
Parameters: |
|
|
|
|
|
|
|
node_ids: the list of nodes labels |
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
a complete Digraph Object |
|
|
|
|
|
|
|
""" |
|
|
|
complete_graph = nx.DiGraph() |
|
|
|
complete_graph = nx.DiGraph() |
|
|
|
complete_graph.add_nodes_from(node_ids) |
|
|
|
complete_graph.add_nodes_from(node_ids) |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
def complete_test(self, test_parent, test_child, parent_set): |
|
|
|
def complete_test(self, test_parent: str, test_child: str, parent_set: typing.List, child_states_numb: int, |
|
|
|
|
|
|
|
tot_vars_count: int): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Permorms a complete independence test on the directed graphs G1 = test_child U parent_set |
|
|
|
|
|
|
|
G2 = G1 U test_parent (added as an additional parent of the test_child). |
|
|
|
|
|
|
|
Generates all the necessary structures and datas to perform the tests. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters: |
|
|
|
|
|
|
|
test_parent: the node label of the test parent |
|
|
|
|
|
|
|
test_child: the node label of the child |
|
|
|
|
|
|
|
parent_set: the common parent set |
|
|
|
|
|
|
|
child_states_numb: the cardinality of the test_child |
|
|
|
|
|
|
|
tot_vars_count_ the total number of variables in the net |
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
True iff test_child and test_parent are independent given the sep_set parent_set |
|
|
|
|
|
|
|
False otherwise |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
#print("Test Parent:", test_parent) |
|
|
|
|
|
|
|
#print("Sep Set", parent_set) |
|
|
|
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) |
|
|
|
v2 = self.sample_path.structure.variables_frame.loc[ |
|
|
|
|
|
|
|
self.sample_path.structure.variables_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) |
|
|
|
parents = np.array(parent_set) |
|
|
|
"""d2 = tmp_df.loc[tmp_df['From'].isin(parent_set)] |
|
|
|
parents = np.append(parents, test_parent) |
|
|
|
v2 = self.sample_path.structure.variables_frame.loc[self.sample_path.structure.variables_frame['Name'].isin(parent_set)] |
|
|
|
#print("PARENTS", parents) |
|
|
|
s2 = st.Structure(d2, v2, self.sample_path.total_variables_count) |
|
|
|
#parents.sort() |
|
|
|
g2 = ng.NetworkGraph(s2) |
|
|
|
sorted_parents = self.nodes[np.isin(self.nodes, parents)] |
|
|
|
g2.init_graph()""" |
|
|
|
#print("SORTED PARENTS", sorted_parents) |
|
|
|
#parent_set.append(test_child) |
|
|
|
cims_filter = sorted_parents != test_parent |
|
|
|
sofc1 = None |
|
|
|
#print("PARENTS NO FROM MASK", cims_filter) |
|
|
|
#if not sofc1: |
|
|
|
#if not p_set: |
|
|
|
if not p_set: |
|
|
|
#print("EMPTY PSET TRYING TO FIND", test_child) |
|
|
|
sofc1 = self.cache.find(test_child) |
|
|
|
#sofc1 = self.cache.find(test_child) |
|
|
|
|
|
|
|
#else: |
|
|
|
|
|
|
|
sofc1 = self.cache.find(set(p_set)) |
|
|
|
|
|
|
|
|
|
|
|
if not sofc1: |
|
|
|
if not sofc1: |
|
|
|
#d1 = tmp_df.loc[tmp_df['From'].isin(parent_set)] |
|
|
|
#print("CACHE MISSS SOFC1") |
|
|
|
d1 = d2[d2.From != test_parent] |
|
|
|
bool_mask1 = np.isin(self.nodes,complete_info) |
|
|
|
|
|
|
|
#print("Bool mask 1", bool_mask1) |
|
|
|
#v1 = self.sample_path.structure.variables_frame.loc[ |
|
|
|
l1 = list(self.nodes[bool_mask1]) |
|
|
|
#self.sample_path.structure.variables_frame['Name'].isin(parent_set)] |
|
|
|
#print("L1", l1) |
|
|
|
v1 = v2[v2.Name != test_parent] |
|
|
|
indxs1 = self.nodes_indxs[bool_mask1] |
|
|
|
#print("D1", d1) |
|
|
|
#print("INDXS 1", indxs1) |
|
|
|
#print("V1", v1) |
|
|
|
vals1 = self.nodes_vals[bool_mask1] |
|
|
|
s1 = st.Structure(d1, v1, self.sample_path.total_variables_count) |
|
|
|
eds1 = list(itertools.product(parent_set,test_child)) |
|
|
|
|
|
|
|
s1 = st.Structure(l1, indxs1, vals1, eds1, tot_vars_count) |
|
|
|
g1 = ng.NetworkGraph(s1) |
|
|
|
g1 = ng.NetworkGraph(s1) |
|
|
|
g1.init_graph() |
|
|
|
g1.fast_init(test_child) |
|
|
|
p1 = pe.ParametersEstimator(self.sample_path, g1) |
|
|
|
p1 = pe.ParametersEstimator(self.sample_path, g1) |
|
|
|
p1.init_sets_cims_container() |
|
|
|
p1.fast_init(test_child) |
|
|
|
#print("Computing params for",test_child, test_parent, parent_set) |
|
|
|
sofc1 = p1.compute_parameters_for_node(test_child) |
|
|
|
p1.compute_parameters_for_node(test_child) |
|
|
|
#if not p_set: |
|
|
|
sofc1 = p1.sets_of_cims_struct.sets_of_cims[s1.get_positional_node_indx(test_child)] |
|
|
|
#self.cache.put(test_child, sofc1) |
|
|
|
self.cache.put(test_child,sofc1) |
|
|
|
#else: |
|
|
|
|
|
|
|
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: |
|
|
|
|
|
|
|
#print("FULL PSET TRYING TO FIND", p_set) |
|
|
|
#p_set.append(test_parent) |
|
|
|
#p_set.append(test_parent) |
|
|
|
#print("PSET ", p_set) |
|
|
|
#print("PSET ", p_set) |
|
|
|
set_p_set = set(p_set) |
|
|
|
#set_p_set = set(p_set) |
|
|
|
sofc2 = self.cache.find(set_p_set) |
|
|
|
sofc2 = self.cache.find(set(p_set)) |
|
|
|
#print("Sofc2 ", sofc2) |
|
|
|
#if sofc2: |
|
|
|
|
|
|
|
#print("Sofc2 in CACHE ", sofc2.actual_cims) |
|
|
|
#print(self.cache.list_of_sets_of_indxs) |
|
|
|
#print(self.cache.list_of_sets_of_indxs) |
|
|
|
|
|
|
|
if not sofc2: |
|
|
|
"""p2 = pe.ParametersEstimator(self.sample_path, g2) |
|
|
|
#print("Cache MISSS SOFC2") |
|
|
|
p2.init_sets_cims_container() |
|
|
|
complete_info.append(test_parent) |
|
|
|
#p2.compute_parameters() |
|
|
|
bool_mask2 = np.isin(self.nodes, complete_info) |
|
|
|
p2.compute_parameters_for_node(test_child) |
|
|
|
#print("BOOL MASK 2",bool_mask2) |
|
|
|
sofc2 = p2.sets_of_cims_struct.sets_of_cims[s2.get_positional_node_indx(test_child)]""" |
|
|
|
l2 = list(self.nodes[bool_mask2]) |
|
|
|
if not sofc2 or p_set: |
|
|
|
#print("L2", l2) |
|
|
|
print("Cache Miss SOC2") |
|
|
|
indxs2 = self.nodes_indxs[bool_mask2] |
|
|
|
#parent_set.append(test_parent) |
|
|
|
#print("INDXS 2", indxs2) |
|
|
|
#d2 = tmp_df.loc[tmp_df['From'].isin(p_set)] |
|
|
|
vals2 = self.nodes_vals[bool_mask2] |
|
|
|
#v2 = self.sample_path.structure.variables_frame.loc[ |
|
|
|
eds2 = list(itertools.product(p_set, test_child)) |
|
|
|
#self.sample_path.structure.variables_frame['Name'].isin(parent_set)] |
|
|
|
s2 = st.Structure(l2, indxs2, vals2, eds2, tot_vars_count) |
|
|
|
#print("D2", d2) |
|
|
|
|
|
|
|
#print("V2", v2) |
|
|
|
|
|
|
|
#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.fast_init(test_child) |
|
|
|
p2 = pe.ParametersEstimator(self.sample_path, g2) |
|
|
|
p2 = pe.ParametersEstimator(self.sample_path, g2) |
|
|
|
p2.init_sets_cims_container() |
|
|
|
p2.fast_init(test_child) |
|
|
|
# p2.compute_parameters() |
|
|
|
sofc2 = p2.compute_parameters_for_node(test_child) |
|
|
|
p2.compute_parameters_for_node(test_child) |
|
|
|
self.cache.put(set(p_set), sofc2) |
|
|
|
sofc2 = p2.sets_of_cims_struct.sets_of_cims[s2.get_positional_node_indx(test_child)] |
|
|
|
for cim1, p_comb in zip(sofc1.actual_cims, sofc1.p_combs): |
|
|
|
if p_set: |
|
|
|
#print("GETTING THIS P COMB", p_comb) |
|
|
|
#set_p_set = set(p_set) |
|
|
|
#if len(parent_set) > 1: |
|
|
|
self.cache.put(set_p_set, sofc2) |
|
|
|
cond_cims = sofc2.filter_cims_with_mask(cims_filter, p_comb) |
|
|
|
end = 0 |
|
|
|
#else: |
|
|
|
increment = self.sample_path.structure.get_states_number(test_parent) |
|
|
|
#cond_cims = sofc2.actual_cims |
|
|
|
for cim1 in sofc1.actual_cims: |
|
|
|
#print("COnd Cims", cond_cims) |
|
|
|
start = end |
|
|
|
for cim2 in cond_cims: |
|
|
|
end = start + increment |
|
|
|
|
|
|
|
for j in range(start, end): |
|
|
|
|
|
|
|
#cim2 = sofc2.actual_cims[j] |
|
|
|
#cim2 = sofc2.actual_cims[j] |
|
|
|
#print(indx) |
|
|
|
#print(indx) |
|
|
|
#print("Run Test", i, j) |
|
|
|
#print("Run Test", i, j) |
|
|
|
if not self.independence_test(test_child, cim1, sofc2.actual_cims[j]): |
|
|
|
if not self.independence_test(child_states_numb, cim1, cim2): |
|
|
|
return False |
|
|
|
return False |
|
|
|
return True |
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def independence_test(self, tested_child, cim1, cim2): |
|
|
|
def independence_test(self, child_states_numb: int, cim1: condim.ConditionalIntensityMatrix, |
|
|
|
# Fake exp test |
|
|
|
cim2: condim.ConditionalIntensityMatrix): |
|
|
|
r1s = cim1.state_transition_matrix.diagonal() |
|
|
|
""" |
|
|
|
r2s = cim2.state_transition_matrix.diagonal() |
|
|
|
Compute the actual independence test using two cims. |
|
|
|
F_stats = cim2.cim.diagonal() / cim1.cim.diagonal() |
|
|
|
It is performed first the exponential test and if the null hypothesis is not rejected, |
|
|
|
child_states_numb = self.sample_path.structure.get_states_number(tested_child) |
|
|
|
it is permormed also the chi_test. |
|
|
|
for val in range(0, child_states_numb): # i possibili valori di tested child TODO QUESTO CONTO DEVE ESSERE VETTORIZZATO |
|
|
|
|
|
|
|
#r1 = cim1.state_transition_matrix[val][val] |
|
|
|
Parameters: |
|
|
|
#r2 = cim2.state_transition_matrix[val][val] |
|
|
|
child_states_numb: the cardinality of the test child |
|
|
|
#print("No Test Parent:",cim1.cim[val][val],"With Test Parent", cim2.cim[val][val]) |
|
|
|
cim1: a cim belonging to the graph without test parent |
|
|
|
#F = cim2.cim[val][val] / cim1.cim[val][val] |
|
|
|
cim2: a cim belonging to the graph with test parent |
|
|
|
|
|
|
|
|
|
|
|
#print("Exponential test", F_stats[val], r1s[val], r2s[val]) |
|
|
|
Returns: |
|
|
|
#print(f_dist.ppf(1 - self.exp_test_sign / 2, r1, r2)) |
|
|
|
True iff both tests do NOT reject the null hypothesis of indipendence |
|
|
|
#print(f_dist.ppf(self.exp_test_sign / 2, r1, r2)) |
|
|
|
False otherwise |
|
|
|
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]): |
|
|
|
M1 = cim1.state_transition_matrix |
|
|
|
print("CONDITIONALLY DEPENDENT EXP") |
|
|
|
M2 = cim2.state_transition_matrix |
|
|
|
|
|
|
|
r1s = M1.diagonal() |
|
|
|
|
|
|
|
r2s = M2.diagonal() |
|
|
|
|
|
|
|
C1 = cim1.cim |
|
|
|
|
|
|
|
C2 = cim2.cim |
|
|
|
|
|
|
|
F_stats = C2.diagonal() / C1.diagonal() |
|
|
|
|
|
|
|
exp_alfa = self.exp_test_sign |
|
|
|
|
|
|
|
for val in range(0, child_states_numb): |
|
|
|
|
|
|
|
if F_stats[val] < f_dist.ppf(exp_alfa / 2, r1s[val], r2s[val]) or \ |
|
|
|
|
|
|
|
F_stats[val] > f_dist.ppf(1 - exp_alfa / 2, r1s[val], r2s[val]): |
|
|
|
|
|
|
|
#print("CONDITIONALLY DEPENDENT EXP") |
|
|
|
return False |
|
|
|
return False |
|
|
|
# fake chi test |
|
|
|
#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) |
|
|
|
M1_no_diag = M1[~np.eye(M1.shape[0], dtype=bool)].reshape(M1.shape[0], -1) |
|
|
|
#print("M1 no diag", M1_no_diag) |
|
|
|
M2_no_diag = M2[~np.eye(M2.shape[0], dtype=bool)].reshape( |
|
|
|
#print("M2 no diag", M2_no_diag) |
|
|
|
M2.shape[0], -1) |
|
|
|
chi_2_quantile = chi2_dist.ppf(1 - self.chi_test_alfa, child_states_numb - 1) |
|
|
|
chi_2_quantile = chi2_dist.ppf(1 - self.chi_test_alfa, child_states_numb - 1) |
|
|
|
""" |
|
|
|
""" |
|
|
|
Ks = np.sqrt(cim1.state_transition_matrix.diagonal() / cim2.state_transition_matrix.diagonal()) |
|
|
|
Ks = np.sqrt(cim1.state_transition_matrix.diagonal() / cim2.state_transition_matrix.diagonal()) |
|
|
@ -174,46 +210,52 @@ 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: str, tot_vars_count: int): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Performs an iteration of the CTPC algorithm using the node var_id as test_child. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters: |
|
|
|
|
|
|
|
var_id: the node label of the test child |
|
|
|
|
|
|
|
tot_vars_count: the number of nodes in the net |
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
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) |
|
|
|
#print(u) |
|
|
|
#complete_frame = self.complete_graph_frame |
|
|
|
|
|
|
|
#test_frame = complete_frame.loc[complete_frame['To'].isin([var_id])] |
|
|
|
|
|
|
|
child_states_numb = self.sample_path.structure.get_states_number(var_id) |
|
|
|
b = 0 |
|
|
|
b = 0 |
|
|
|
#parent_indx = 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) |
|
|
|
#print("Parent_indx",parent_indx) |
|
|
|
|
|
|
|
#print("LEN U", len(u)) |
|
|
|
|
|
|
|
|
|
|
|
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, u[parent_indx]) |
|
|
|
S = self.generate_possible_sub_sets_of_size(u, b, u[parent_indx]) |
|
|
|
#print("U Set", u) |
|
|
|
#print("U Set", u) |
|
|
|
#print("S", S) |
|
|
|
#print("S", S) |
|
|
|
|
|
|
|
test_parent = u[parent_indx] |
|
|
|
|
|
|
|
#print("Test Parent", test_parent) |
|
|
|
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", test_parent) |
|
|
|
if self.complete_test(u[parent_indx], var_id, parents_set): |
|
|
|
if self.complete_test(test_parent, var_id, parents_set, child_states_numb, tot_vars_count): |
|
|
|
#print("Removing EDGE:", u[parent_indx], var_id) |
|
|
|
#print("Removing EDGE:", test_parent, var_id) |
|
|
|
self.complete_graph.remove_edge(u[parent_indx], var_id) |
|
|
|
self.complete_graph.remove_edge(test_parent, var_id) |
|
|
|
#print(self.complete_graph_frame) |
|
|
|
u.remove(test_parent) |
|
|
|
"""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)""" |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
#print(self.complete_graph_frame) |
|
|
|
|
|
|
|
#u.remove(u[parent_indx]) |
|
|
|
|
|
|
|
del u[parent_indx] |
|
|
|
|
|
|
|
removed = True |
|
|
|
removed = True |
|
|
|
|
|
|
|
break |
|
|
|
#else: |
|
|
|
#else: |
|
|
|
#parent_indx += 1 |
|
|
|
#parent_indx += 1 |
|
|
|
if not removed: |
|
|
|
if not removed: |
|
|
@ -221,23 +263,55 @@ class StructureEstimator: |
|
|
|
b += 1 |
|
|
|
b += 1 |
|
|
|
self.cache.clear() |
|
|
|
self.cache.clear() |
|
|
|
|
|
|
|
|
|
|
|
def generate_possible_sub_sets_of_size(self, u, size, parent_id): |
|
|
|
def generate_possible_sub_sets_of_size(self, u: typing.List, size: int, parent_label: str): |
|
|
|
#print("Inside Generate subsets", u) |
|
|
|
""" |
|
|
|
#print("InsideGenerate Subsets", parent_id) |
|
|
|
Creates a list containing all possible subsets of the list u of size size, |
|
|
|
|
|
|
|
that do not contains a the node identified by parent_label. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters: |
|
|
|
|
|
|
|
u: the list of nodes |
|
|
|
|
|
|
|
size: the size of the subsets |
|
|
|
|
|
|
|
parent_label: the nodes to exclude in the subsets generation |
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
a Map Object containing a list of lists |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
list_without_test_parent = u[:] |
|
|
|
list_without_test_parent = u[:] |
|
|
|
list_without_test_parent.remove(parent_id) |
|
|
|
list_without_test_parent.remove(parent_label) |
|
|
|
# u.remove(parent_id) |
|
|
|
|
|
|
|
#print(list(map(list, itertools.combinations(list_without_test_parent, size)))) |
|
|
|
|
|
|
|
return map(list, itertools.combinations(list_without_test_parent, size)) |
|
|
|
return map(list, itertools.combinations(list_without_test_parent, size)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ctpc_algorithm(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Compute the CTPC algorithm. |
|
|
|
|
|
|
|
Parameters: |
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
ctpc_algo = self.one_iteration_of_CTPC_algorithm |
|
|
|
|
|
|
|
total_vars_numb = self.sample_path.total_variables_count |
|
|
|
|
|
|
|
[ctpc_algo(n, total_vars_numb) for n in self.nodes] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_results(self): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Save the estimated Structure to a .json file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters: |
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
res = json_graph.node_link_data(self.complete_graph) |
|
|
|
|
|
|
|
name = self.sample_path.importer.file_path.rsplit('/',1)[-1] |
|
|
|
|
|
|
|
#print(name) |
|
|
|
|
|
|
|
name = 'results_' + name |
|
|
|
|
|
|
|
with open(name, 'w') as f: |
|
|
|
|
|
|
|
json.dump(res, f) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_diagonal_elements(self, matrix): |
|
|
|
def remove_diagonal_elements(self, matrix): |
|
|
|
m = matrix.shape[0] |
|
|
|
m = matrix.shape[0] |
|
|
|
strided = np.lib.stride_tricks.as_strided |
|
|
|
strided = np.lib.stride_tricks.as_strided |
|
|
|
s0, s1 = matrix.strides |
|
|
|
s0, s1 = matrix.strides |
|
|
|
return strided(matrix.ravel()[1:], shape=(m - 1, m), strides=(s0 + s1, s1)).reshape(m, -1) |
|
|
|
return strided(matrix.ravel()[1:], shape=(m - 1, m), strides=(s0 + s1, s1)).reshape(m, -1) |
|
|
|
|
|
|
|
|
|
|
|
def ctpc_algorithm(self): |
|
|
|
|
|
|
|
for node_id in self.sample_path.structure.list_of_nodes_labels(): |
|
|
|
|
|
|
|
print("TESTING VAR:", node_id) |
|
|
|
|
|
|
|
self.one_iteration_of_CTPC_algorithm(node_id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|