From e126993555fe599ef13e5c0c8a235fd1848fac15 Mon Sep 17 00:00:00 2001 From: philpMartin Date: Sun, 5 Jul 2020 00:59:54 +0200 Subject: [PATCH] Add Nested Cim Structure --- main_package/classes/network_graph.py | 2 +- main_package/classes/parameters_estimator.py | 38 ++++---------- main_package/classes/set_of_cims.py | 55 ++++++++++++++------ 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/main_package/classes/network_graph.py b/main_package/classes/network_graph.py index 616e381..52dea7d 100644 --- a/main_package/classes/network_graph.py +++ b/main_package/classes/network_graph.py @@ -74,7 +74,7 @@ class NetworkGraph(): 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(indexes_for_a_node) + index_structure.append(tuple(indexes_for_a_node)) return index_structure def get_nodes(self): diff --git a/main_package/classes/parameters_estimator.py b/main_package/classes/parameters_estimator.py index 5402754..509b02d 100644 --- a/main_package/classes/parameters_estimator.py +++ b/main_package/classes/parameters_estimator.py @@ -1,5 +1,7 @@ import os import time as tm +from line_profiler import LineProfiler + import network_graph as ng import sample_path as sp @@ -30,34 +32,11 @@ class ParametersEstimator: print("Elapsed Time ", t1) def parameters_estimation_single_trajectory(self, trajectory): - #t0 = tm.time() + row_length = trajectory.shape[1] for indx, row in enumerate(trajectory[:-1]): self.compute_sufficient_statistics_for_row(trajectory[indx], trajectory[indx + 1], row_length) - """if trajectory[indx + 1][1] != -1: - transition = self.find_transition(trajectory[indx], trajectory[indx + 1], row_length) - which_node = transition[0] - # print(which_node) - which_matrix = self.which_matrix_to_update(row, transition[0]) - which_element = transition[1] - self.amalgamated_cims_struct.update_state_transition_for_matrix(which_node, which_matrix, which_element) - #changed_node = which_node - time = self.compute_time_delta(trajectory[indx], trajectory[indx + 1]) - which_element = transition[1][0] - self.amalgamated_cims_struct.update_state_residence_time_for_matrix(which_node, which_matrix, which_element, - time) - - for node_indx in range(0, 3): - if node_indx != transition[0]: - # print(node) - which_node = node_indx - which_matrix = self.which_matrix_to_update(row, node_indx) - which_element = row[node_indx + 1] - self.amalgamated_cims_struct.update_state_residence_time_for_matrix( - which_node, which_matrix, which_element, time)""" - #t1 = tm.time() - t0 - #print("Elapsed Time ", t1) def compute_sufficient_statistics_for_row(self, current_row, next_row, row_length): #time = self.compute_time_delta(current_row, next_row) time = current_row[0] @@ -80,7 +59,6 @@ class ParametersEstimator: which_node, which_matrix, which_element, time) - def find_transition(self, current_row, next_row, row_length): for indx in range(1, row_length): if current_row[indx] != next_row[indx]: @@ -90,8 +68,8 @@ class ParametersEstimator: def compute_time_delta(self, current_row, next_row): return next_row[0] - current_row[0] - def which_matrix_to_update(self, current_row, node_indx): # produce strutture {'X':1, 'Y':2} dove X e Y sono i parent di node_id - return current_row[self.fancy_indexing_structure[node_indx]] + def which_matrix_to_update(self, current_row, node_indx): + return tuple(current_row.take(self.fancy_indexing_structure[node_indx])) @@ -116,6 +94,12 @@ print(pe.amalgamated_cims_struct.get_set_of_cims(1).get_cims_number()) print(pe.amalgamated_cims_struct.get_set_of_cims(2).get_cims_number()) #pe.parameters_estimation_single_trajectory(pe.sample_path.trajectories[0].get_trajectory()) pe.parameters_estimation() +"""lp = LineProfiler() +lp.add_function(pe.compute_sufficient_statistics_for_row) # add additional function to profile +lp_wrapper = lp(pe.parameters_estimation_single_trajectory) +#lp_wrapper = lp(pe.parameters_estimation) +lp_wrapper(pe.sample_path.trajectories[0].get_trajectory()) +lp.print_stats()""" for matrix in pe.amalgamated_cims_struct.get_set_of_cims(1).actual_cims: print(matrix.state_residence_times) print(matrix.state_transition_matrix) diff --git a/main_package/classes/set_of_cims.py b/main_package/classes/set_of_cims.py index 2a970a6..b3cbedb 100644 --- a/main_package/classes/set_of_cims.py +++ b/main_package/classes/set_of_cims.py @@ -1,4 +1,5 @@ import numpy as np +from numba import njit, int32 import conditional_intensity_matrix as cim @@ -20,41 +21,65 @@ class SetOfCims: self.build_actual_cims_structure() def build_actual_cims_structure(self): - cims_number = 1 - for state_number in self.parents_states_number: - cims_number = cims_number * state_number - 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.node_states_number) + #cims_number = 1 + #for state_number in self.parents_states_number: + #cims_number = cims_number * state_number + if not self.parents_states_number: + self.actual_cims = np.empty(1, dtype=cim.ConditionalIntensityMatrix) + self.actual_cims[0] = cim.ConditionalIntensityMatrix(self.node_states_number) + else: + self.actual_cims = np.empty(self.parents_states_number, dtype=cim.ConditionalIntensityMatrix) + self.build_actual_cims(self.actual_cims) + #for indx, matrix in enumerate(self.actual_cims): + #self.actual_cims[indx] = cim.ConditionalIntensityMatrix(self.node_states_number) def update_state_transition(self, indexes, element_indx_tuple): - matrix_indx = self.indexes_converter(indexes) - self.actual_cims[matrix_indx].update_state_transition_count(element_indx_tuple) + #matrix_indx = self.indexes_converter(indexes) + #print(indexes) + if not indexes: + self.actual_cims[0].update_state_transition_count(element_indx_tuple) + else: + self.actual_cims[indexes].update_state_transition_count(element_indx_tuple) def update_state_residence_time(self, which_matrix, which_element, time): - matrix_indx = self.indexes_converter(which_matrix) - self.actual_cims[matrix_indx].update_state_residence_time_for_state(which_element, time) + #matrix_indx = self.indexes_converter(which_matrix) + #print(type(which_matrix)) + if not which_matrix: + self.actual_cims[0].update_state_residence_time_for_state(which_element, time) + else: + self.actual_cims[which_matrix].update_state_residence_time_for_state(which_element, time) + def build_actual_cims(self, cim_structure): + for indx in range(len(cim_structure)): + if cim_structure[indx] is None: + cim_structure[indx] = cim.ConditionalIntensityMatrix(self.node_states_number) + else: + self.build_actual_cims(cim_structure[indx]) def get_cims_number(self): return len(self.actual_cims) + def indexes_converter(self, indexes): # Si aspetta array del tipo [2,2] dove #print(type(indexes)) + #print(indexes) + #print(type(bases)) + vector_index = 0 if indexes.size == 0: - return 0 + return vector_index else: - vector_index = 0 for indx, value in enumerate(indexes): vector_index = vector_index*self.parents_states_number[indx] + indexes[indx] return vector_index -""" -sofc = SetOfCims('W', [], 2) + +"""sofc = SetOfCims('Z', [3, 3], 3) sofc.build_actual_cims_structure() print(sofc.actual_cims) -print(sofc.indexes_converter([]))""" +print(sofc.actual_cims[0,0]) +print(sofc.actual_cims[1,2]) +#print(sofc.indexes_converter([]))"""