From 8272b39d67b66cd6eaefdfc5d7088d7b270561ba Mon Sep 17 00:00:00 2001 From: philpMartin Date: Sun, 28 Jun 2020 16:34:36 +0200 Subject: [PATCH] Add ParameterEstimator Class --- main_package/classes/amalgamated_cims.py | 17 +++--- main_package/classes/network_graph.py | 56 ++++++++++++++------ main_package/classes/parameters_estimator.py | 34 ++++++++++++ main_package/classes/set_of_cims.py | 10 ++-- main_package/classes/structure.py | 12 +++-- 5 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 main_package/classes/parameters_estimator.py diff --git a/main_package/classes/amalgamated_cims.py b/main_package/classes/amalgamated_cims.py index c4a6ce3..b02b308 100644 --- a/main_package/classes/amalgamated_cims.py +++ b/main_package/classes/amalgamated_cims.py @@ -4,18 +4,21 @@ import numpy as np class AmalgamatedCims: - def __init__(self, states_number,list_of_keys, nodes_value, list_of_vars_order): - self.actual_cims = {} - self.init_cims_structure(list_of_keys, nodes_value, list_of_vars_order) + def __init__(self, states_number,list_of_keys, list_of_vars_order): + self.sets_of_cims = {} + self.init_cims_structure(list_of_keys, states_number, list_of_vars_order) self.states_per_variable = states_number def init_cims_structure(self, keys, nodes_val, list_of_vars_order): - for key, vars_order in (keys, list_of_vars_order): - self.actual_cims[key] = socim.SetOfCims(key, vars_order, nodes_val) + print(keys) + print(list_of_vars_order) + for indx, key in enumerate(keys): + self.sets_of_cims[key] = socim.SetOfCims(key, list_of_vars_order[indx], nodes_val) - def compute_matrix_indx(self, row, col): - return self.state_per_variable * row + col + + def get_set_of_cims(self, node_id): + return self.sets_of_cims[node_id] def get_vars_order(self, node): return self.actual_cims[node][1] diff --git a/main_package/classes/network_graph.py b/main_package/classes/network_graph.py index 89d01e7..0b17fff 100644 --- a/main_package/classes/network_graph.py +++ b/main_package/classes/network_graph.py @@ -13,19 +13,19 @@ class NetworkGraph(): """ - def __init__(self, sample_path): - self.sample_path = sample_path + def __init__(self, graph_struct): + self.graph_struct = graph_struct self.graph = nx.DiGraph() def init_graph(self): - self.sample_path.build_trajectories() - self.sample_path.build_structure() - self.add_nodes(self.sample_path.structure.list_of_node_ids()) - self.add_edges(self.sample_path.structure.list_of_edge_ids()) + #self.sample_path.build_trajectories() + #self.sample_path.build_structure() + self.add_nodes(self.graph_struct.list_of_nodes()) + self.add_edges(self.graph_struct.list_of_edges()) - def add_nodes(self, list_of_node_ids): - for indx, id in enumerate(list_of_node_ids): + def add_nodes(self, list_of_nodes): + for indx, id in enumerate(list_of_nodes): #print(indx, id) self.graph.add_node(id) nx.set_node_attributes(self.graph, {id:indx}, 'indx') @@ -35,14 +35,32 @@ class NetworkGraph(): def add_edges(self, list_of_edges): self.graph.add_edges_from(list_of_edges) + def get_ordered_by_indx_set_of_parents(self, node): + ordered_set = {} + parents = self.get_parents_by_id(node) + for n in parents: + indx = self.graph_struct.get_node_indx(n) + ordered_set[n] = indx + {k: v for k, v in sorted(ordered_set.items(), key=lambda item: item[1])} + return list(ordered_set.keys()) + + def get_ord_set_of_par_of_all_nodes(self): + result = [] + for node in self.get_nodes(): + result.append(self.get_ordered_by_indx_set_of_parents(node)) + return result + + def get_nodes(self): + return list(self.graph.nodes) + def get_parents_by_id(self, node_id): return list(self.graph.predecessors(node_id)) - def get_node(self, node_id): - to_find = nd.Node(node_id) - for node in self.graph.nodes: - if node == to_find: - return node + def get_states_number(self): + return self.graph_struct.get_states_number() + + def get_node_by_index(self, node_id): + return self.graph_struct.get_node_indx(node_id) @@ -50,20 +68,24 @@ class NetworkGraph(): ######Veloci Tests####### -os.getcwd() +"""os.getcwd() os.chdir('..') path = os.getcwd() + '/data' s1 = sp.SamplePath(path) +s1.build_trajectories() +s1.build_structure() -g1 = NetworkGraph(s1) +g1 = NetworkGraph(s1.structure) g1.init_graph() print(g1.graph.number_of_nodes()) print(g1.graph.number_of_edges()) print(nx.get_node_attributes(g1.graph, 'indx')['X']) -for node in g1.get_parents_by_id('X'): - print(g1.sample_path.structure.get_node_indx(node)) +for node in g1.get_parents_by_id('Z'): + print(g1.get_node_by_index(node)) print(node) +print(g1.get_ordered_by_indx_set_of_parents('Z')) +print(g1.get_ord_set_of_par_of_all_nodes())""" diff --git a/main_package/classes/parameters_estimator.py b/main_package/classes/parameters_estimator.py new file mode 100644 index 0000000..5ba476c --- /dev/null +++ b/main_package/classes/parameters_estimator.py @@ -0,0 +1,34 @@ +import network_graph as ng +import sample_path as sp +import os +import amalgamated_cims as acims + + +class ParametersEstimator: + + def __init__(self, sample_path, net_graph): + self.sample_path = sample_path + self.net_graph = net_graph + self.amalgamated_cims_struct = None + + def init_amalgamated_cims_struct(self): + self.amalgamated_cims_struct = acims.AmalgamatedCims(self.net_graph.get_states_number(), + self.net_graph.get_nodes(), + self.net_graph.get_ord_set_of_par_of_all_nodes()) + +# Simple Test # +os.getcwd() +os.chdir('..') +path = os.getcwd() + '/data' + +s1 = sp.SamplePath(path) +s1.build_trajectories() +s1.build_structure() + +g1 = ng.NetworkGraph(s1.structure) +g1.init_graph() + +pe = ParametersEstimator(s1, g1) +pe.init_amalgamated_cims_struct() +print(pe.amalgamated_cims_struct.get_set_of_cims('X').get_cims_number()) +print(pe.amalgamated_cims_struct.get_set_of_cims('Y').get_cims_number()) diff --git a/main_package/classes/set_of_cims.py b/main_package/classes/set_of_cims.py index f61b7fe..547b9e1 100644 --- a/main_package/classes/set_of_cims.py +++ b/main_package/classes/set_of_cims.py @@ -9,6 +9,7 @@ class SetOfCims: self.ordered_parent_set = ordered_parent_set self.value = value_type self.actual_cims = None + self.build_actual_cims_structure() def build_actual_cims_structure(self): cims_number = self.value**len(self.ordered_parent_set) @@ -16,6 +17,9 @@ class SetOfCims: for indx, matrix in enumerate(self.actual_cims): self.actual_cims[indx] = cim.ConditionalIntensityMatrix(self.value) + def get_cims_number(self): + return len(self.actual_cims) + def indexes_converter(self, dict_of_indexes): # Si aspetta oggetti del tipo {X:1, Y:1, Z:0} literal_index = "" for node in self.ordered_parent_set: @@ -24,12 +28,10 @@ class SetOfCims: -sofc = SetOfCims('W', ['X','Y', 'Z'], 2) -#sofc.build_actual_cims_structure(sofc.ordered_parent_set, sofc.value) - +"""sofc = SetOfCims('W', ['X','Y', 'Z'], 2) sofc.build_actual_cims_structure() print(sofc.actual_cims) -print(sofc.indexes_converter({'X':1, 'Y':1, 'Z':0})) +print(sofc.indexes_converter({'X':1, 'Y':1, 'Z':0}))""" diff --git a/main_package/classes/structure.py b/main_package/classes/structure.py index 231cdaa..9d38c80 100644 --- a/main_package/classes/structure.py +++ b/main_package/classes/structure.py @@ -6,15 +6,14 @@ class Structure: self.structure_frame = structure self.variables_frame = variables - def list_of_edge_ids(self): + def list_of_edges(self): edges_list = [] for indx, row in self.structure_frame.iterrows(): - row_tuple = (row.From, row.To) - edges_list.append(row_tuple) + row_tuple = (row.From, row.To) + edges_list.append(row_tuple) return edges_list - def list_of_node_ids(self): - #print(self.variables_frame) + def list_of_nodes(self): return self.variables_frame['Name'] def get_node_id(self, node_indx): @@ -22,3 +21,6 @@ class Structure: def get_node_indx(self, node_id): return list(self.variables_frame['Name']).index(node_id) + + def get_states_number(self): + return self.variables_frame['Value'][0]