From 4356eee147f62d3e92ffc5b3b8891574dbf0dce4 Mon Sep 17 00:00:00 2001 From: philpMartin Date: Sun, 28 Jun 2020 12:30:10 +0200 Subject: [PATCH] Add all necessary structures to place Cims values --- main_package/classes/amalgamated_cims.py | 22 +++++++----- .../classes/conditional_intensity_matrix.py | 8 +++-- main_package/classes/network_graph.py | 31 ++++++++++++++-- main_package/classes/sample_path.py | 11 ------ main_package/classes/set_of_cims.py | 35 +++++++++++++++++++ main_package/classes/structure.py | 10 +++++- 6 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 main_package/classes/set_of_cims.py diff --git a/main_package/classes/amalgamated_cims.py b/main_package/classes/amalgamated_cims.py index 169a571..c4a6ce3 100644 --- a/main_package/classes/amalgamated_cims.py +++ b/main_package/classes/amalgamated_cims.py @@ -1,20 +1,24 @@ -import conditional_intensity_matrix as cim +import set_of_cims as socim import numpy as np class AmalgamatedCims: - def __init__(self, states_number,list_of_keys, list_of_matrices_dims): + def __init__(self, states_number,list_of_keys, nodes_value, list_of_vars_order): self.actual_cims = {} - self.init_cims_structure(list_of_keys, list_of_matrices_dims) + self.init_cims_structure(list_of_keys, nodes_value, list_of_vars_order) self.states_per_variable = states_number - def init_cims_structure(self, keys, dims): - for key, dim in (keys, dims): - self.actual_cims[key] = np.empty(dim, dtype=cim.ConditionalIntensityMatrix) - for key in self.actual_cims.keys(): - for indx in range(len(self.actual_cims[key])): - self.actual_cims[key][indx] = cim.ConditionalIntensityMatrix(self.states_per_variable) + 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) + def compute_matrix_indx(self, row, col): return self.state_per_variable * row + col + + def get_vars_order(self, node): + return self.actual_cims[node][1] + + def update_state_transition_for_matrix(self, node, dict_of_indxs, element_indx): + self.actual_cims[node] diff --git a/main_package/classes/conditional_intensity_matrix.py b/main_package/classes/conditional_intensity_matrix.py index 781edab..49ad3f0 100644 --- a/main_package/classes/conditional_intensity_matrix.py +++ b/main_package/classes/conditional_intensity_matrix.py @@ -5,5 +5,9 @@ class ConditionalIntensityMatrix: def __init__(self, dimension): self.state_residence_times = np.zeros(shape=(1, dimension)) - self.state_transition_matrix = np.zeros(shape=(dimension, dimension)) - self.cim = np.zeros(shape=(dimension, dimension)) \ No newline at end of file + self.state_transition_matrix = np.zeros(shape=(dimension, dimension), dtype=int) + self.cim = np.zeros(shape=(dimension, dimension), dtype=float) + + def update_state_transition_count(self, positions_list): + self.state_transition_matrix[positions_list[0]][positions_list[1]] = self.state_transition_matrix[positions_list[0]][positions_list[1]] + 1 + diff --git a/main_package/classes/network_graph.py b/main_package/classes/network_graph.py index 5ac9387..89d01e7 100644 --- a/main_package/classes/network_graph.py +++ b/main_package/classes/network_graph.py @@ -1,4 +1,4 @@ -import numpy as np + import sample_path as sp import networkx as nx import os @@ -21,11 +21,29 @@ class NetworkGraph(): def init_graph(self): self.sample_path.build_trajectories() self.sample_path.build_structure() - self.add_edges(self.sample_path.structure.list_of_edges()) + self.add_nodes(self.sample_path.structure.list_of_node_ids()) + self.add_edges(self.sample_path.structure.list_of_edge_ids()) + + def add_nodes(self, list_of_node_ids): + for indx, id in enumerate(list_of_node_ids): + #print(indx, id) + self.graph.add_node(id) + nx.set_node_attributes(self.graph, {id:indx}, 'indx') + #for node in list(self.graph.nodes): + #print(node) def add_edges(self, list_of_edges): self.graph.add_edges_from(list_of_edges) + 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 + @@ -39,4 +57,13 @@ s1 = sp.SamplePath(path) g1 = NetworkGraph(s1) 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)) + print(node) + + diff --git a/main_package/classes/sample_path.py b/main_package/classes/sample_path.py index e530343..a2d1351 100644 --- a/main_package/classes/sample_path.py +++ b/main_package/classes/sample_path.py @@ -35,16 +35,5 @@ class SamplePath: def get_number_trajectories(self): return len(self.trajectories) -"""os.getcwd() -os.chdir('..') -path = os.getcwd() + '/data' -print(path) -sp = SamplePath(path) -sp.build_trajectories() -sp.build_structure() -print(sp.trajectories[7].actual_trajectory) -print(sp.importer.df_samples_list[7]) -print(sp.get_number_trajectories()) -print(list(sp.structure.list_of_edges()))""" diff --git a/main_package/classes/set_of_cims.py b/main_package/classes/set_of_cims.py new file mode 100644 index 0000000..f61b7fe --- /dev/null +++ b/main_package/classes/set_of_cims.py @@ -0,0 +1,35 @@ +import numpy as np +import conditional_intensity_matrix as cim + + +class SetOfCims: + + def __init__(self, node_id, ordered_parent_set, value_type): + self.node_id = node_id + self.ordered_parent_set = ordered_parent_set + self.value = value_type + self.actual_cims = None + + def build_actual_cims_structure(self): + cims_number = self.value**len(self.ordered_parent_set) + self.actual_cims = np.empty(cims_number, dtype=cim.ConditionalIntensityMatrix) + for indx, matrix in enumerate(self.actual_cims): + self.actual_cims[indx] = cim.ConditionalIntensityMatrix(self.value) + + 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: + literal_index = literal_index + str(dict_of_indexes[node]) + return int(literal_index, self.value) + + + +sofc = SetOfCims('W', ['X','Y', 'Z'], 2) +#sofc.build_actual_cims_structure(sofc.ordered_parent_set, sofc.value) + +sofc.build_actual_cims_structure() +print(sofc.actual_cims) +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 110c4f7..231cdaa 100644 --- a/main_package/classes/structure.py +++ b/main_package/classes/structure.py @@ -6,11 +6,19 @@ class Structure: self.structure_frame = structure self.variables_frame = variables - def list_of_edges(self): + def list_of_edge_ids(self): edges_list = [] for indx, row in self.structure_frame.iterrows(): row_tuple = (row.From, row.To) edges_list.append(row_tuple) return edges_list + def list_of_node_ids(self): + #print(self.variables_frame) + return self.variables_frame['Name'] + def get_node_id(self, node_indx): + return self.variables_frame['Name'][node_indx] + + def get_node_indx(self, node_id): + return list(self.variables_frame['Name']).index(node_id)