diff --git a/main_package/classes/json_importer.py b/main_package/classes/json_importer.py index 4a73d13..6019ae3 100644 --- a/main_package/classes/json_importer.py +++ b/main_package/classes/json_importer.py @@ -30,6 +30,7 @@ class JsonImporter(AbstractImporter): def import_data(self): raw_data = self.read_json_file() self.import_trajectories(raw_data) + self.compute_row_delta_in_all_samples_frames() self.import_structure(raw_data) self.import_variables(raw_data) @@ -89,6 +90,21 @@ class JsonImporter(AbstractImporter): for sample_indx, sample in enumerate(raw_data[indx][trajectories_key]): self.df_samples_list.append(pd.json_normalize(raw_data[indx][trajectories_key][sample_indx])) + def compute_row_delta_sigle_samples_frame(self, sample_frame): + columns_header = list(sample_frame.columns.values) + # print(columns_header) + for col_name in columns_header: + if col_name == 'Time': + sample_frame[col_name + 'Delta'] = sample_frame[col_name].diff() + sample_frame['Time'] = sample_frame['TimeDelta'] + del sample_frame['TimeDelta'] + sample_frame['Time'] = sample_frame['Time'].shift(-1) + sample_frame.drop(sample_frame.tail(1).index, inplace=True) + + def compute_row_delta_in_all_samples_frames(self): + for sample in self.df_samples_list: + self.compute_row_delta_sigle_samples_frame(sample) + def build_list_of_samples_array(self, data_frame): """ Costruisce una lista contenente le colonne presenti nel dataframe data_frame convertendole in numpy_array @@ -120,4 +136,6 @@ ij.import_data() #print(ij.df_samples_list[7]) print(ij.df_structure) print(ij.df_variables) -print((ij.build_list_of_samples_array(0)[1].size))""" +#print((ij.build_list_of_samples_array(0)[1].size)) +ij.compute_row_delta_in_all_samples_frames() +print(ij.df_samples_list[0])""" diff --git a/main_package/classes/parameters_estimator.py b/main_package/classes/parameters_estimator.py index c687a05..5402754 100644 --- a/main_package/classes/parameters_estimator.py +++ b/main_package/classes/parameters_estimator.py @@ -22,18 +22,19 @@ class ParametersEstimator: def parameters_estimation(self): print("Starting computing") t0 = tm.time() - for indx, trajectory in enumerate(self.sample_path.trajectories): + for trajectory in self.sample_path.trajectories: + #tr_length = trajectory.size() self.parameters_estimation_single_trajectory(trajectory.get_trajectory()) #print("Finished Trajectory number", indx) t1 = tm.time() - t0 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): - if trajectory[indx][1] == -1: - break - if trajectory[indx + 1][1] != -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) @@ -54,7 +55,30 @@ class ParametersEstimator: 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) + 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] + for indx in range(1, row_length): + if current_row[indx] != next_row[indx] and next_row[indx] != -1: + transition = [indx - 1, (current_row[indx], next_row[indx])] + which_node = transition[0] + which_matrix = self.which_matrix_to_update(current_row, transition[0]) + which_element = transition[1] + self.amalgamated_cims_struct.update_state_transition_for_matrix(which_node, which_matrix, which_element) + which_element = transition[1][0] + self.amalgamated_cims_struct.update_state_residence_time_for_matrix(which_node, which_matrix, + which_element, + time) + else: + which_node = indx - 1 + which_matrix = self.which_matrix_to_update(current_row, which_node) + which_element = current_row[indx] + self.amalgamated_cims_struct.update_state_residence_time_for_matrix( + which_node, which_matrix, which_element, time) + def find_transition(self, current_row, next_row, row_length): diff --git a/main_package/classes/trajectory.py b/main_package/classes/trajectory.py index 7ee004c..bc4028b 100644 --- a/main_package/classes/trajectory.py +++ b/main_package/classes/trajectory.py @@ -20,6 +20,9 @@ class Trajectory(): def get_trajectory(self): return self.actual_trajectory + def size(self): + return self.actual_trajectory.shape[0] + def merge_columns(self, list_of_cols): return np.vstack(list_of_cols).T