From 71aacbd808946da88b2bda2963caa2c4abe34046 Mon Sep 17 00:00:00 2001 From: philpMartin Date: Wed, 24 Jun 2020 20:21:19 +0200 Subject: [PATCH] Major changes in sample_path class --- main_package/classes/dynamic_graph.py | 5 ++-- main_package/classes/json_importer.py | 30 ++++++++++++++--------- main_package/classes/sample_path.py | 35 ++++++++++++++++++--------- main_package/classes/structure.py | 16 ++++++++++++ main_package/classes/trajectory.py | 27 ++++++++------------- 5 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 main_package/classes/structure.py diff --git a/main_package/classes/dynamic_graph.py b/main_package/classes/dynamic_graph.py index 7a4585d..ed1835f 100644 --- a/main_package/classes/dynamic_graph.py +++ b/main_package/classes/dynamic_graph.py @@ -1,9 +1,10 @@ import numpy as np import sample_path as sp +import networkx as nx import node -class DynamicGraph(): +class NetworkGraph(): """ Rappresenta un grafo dinamico con la seguente struttura: {Key: {Arcs:{node_object: #in_arcs.....}, Time:t, Node:node_object}.....} @@ -20,7 +21,7 @@ class DynamicGraph(): def __init__(self, sample_path): self.sample_path = sample_path self.graph = {} - self.states_number = 0 + def initialize_graph(self, trajectory): """ diff --git a/main_package/classes/json_importer.py b/main_package/classes/json_importer.py index 2b435f3..2ebc086 100644 --- a/main_package/classes/json_importer.py +++ b/main_package/classes/json_importer.py @@ -15,11 +15,10 @@ class JsonImporter(AbstractImporter): super(JsonImporter, self).__init__(files_path) def import_data(self): - data = self.read_json_file() - self.import_trajectories(data) - self.import_structure(data) - self.import_variables(data) - + raw_data = self.read_json_file() + self.import_trajectories(raw_data) + self.import_structure(raw_data) + self.import_variables(raw_data) def import_trajectories(self, raw_data): self.normalize_trajectories(raw_data, 0, 'samples') @@ -36,7 +35,7 @@ class JsonImporter(AbstractImporter): for file_name in read_files: with open(file_name) as f: data = json.load(f) - return data + return data except ValueError as err: print(err.args) @@ -45,7 +44,13 @@ class JsonImporter(AbstractImporter): def normalize_trajectories(self, raw_data, indx, trajectories_key): 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])) + self.df_samples_list.append(pd.json_normalize(raw_data[indx][trajectories_key][sample_indx])) + + def build_list_of_samples_array(self, data_frame): + columns_list = [] + for column in data_frame: + columns_list.append(data_frame[column].to_numpy()) + return columns_list def clear_data_frames(self): """ @@ -55,13 +60,14 @@ class JsonImporter(AbstractImporter): Returns: void """ - for data_frame in self.df_list: - data_frame = data_frame.iloc[0:0] - + for indx in range(len(self.df_samples_list)): + #data_frame = data_frame.iloc[0:0] + self.df_samples_list[indx] = self.df_samples_list[indx].iloc[0:0] -ij = JsonImporter("../data") +"""ij = JsonImporter("../data") ij.import_data() -print(ij.df_samples_list[7]) +#print(ij.df_samples_list[7]) print(ij.df_structure) print(ij.df_variables) +print((ij.build_list_of_samples_array(0)[1].size))""" diff --git a/main_package/classes/sample_path.py b/main_package/classes/sample_path.py index 1e9d7ad..8d1bf83 100644 --- a/main_package/classes/sample_path.py +++ b/main_package/classes/sample_path.py @@ -1,11 +1,12 @@ import pandas as pd import numpy as np import os -import importer as imp +import json_importer as imp import trajectory as tr +import structure as st -class SamplePath(): +class SamplePath: """ Rappresenta l'aggregazione di una o piĆ¹ traiettorie. Ha il compito dato di costruire tutte gli oggetti Trajectory a partire @@ -15,23 +16,35 @@ class SamplePath(): :trajectories: lista contenente le traiettorie create """ - def __init__(self, files_path=os.getcwd() + "/main_package/data"): - self.importer = imp.Importer(files_path) + def __init__(self, files_path): + print() + self.importer = imp.JsonImporter(files_path) self.trajectories = [] - + self.structure = None def build_trajectories(self): - self.importer.import_data_from_csv() - self.importer.merge_value_columns_in_all_frames() - self.importer.drop_unneccessary_columns_in_all_frames() - for data_frame in self.importer.get_data_frames(): - trajectory = tr.Trajectory(data_frame) + self.importer.import_data() + for traj_data_frame in self.importer.df_samples_list: + trajectory = tr.Trajectory(self.importer.build_list_of_samples_array(traj_data_frame)) self.trajectories.append(trajectory) self.importer.clear_data_frames() + def build_structure(self): + self.structure = st.Structure(self.importer.df_structure, self.importer.df_variables) + 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/structure.py b/main_package/classes/structure.py new file mode 100644 index 0000000..110c4f7 --- /dev/null +++ b/main_package/classes/structure.py @@ -0,0 +1,16 @@ + + +class Structure: + + def __init__(self, structure, variables): + self.structure_frame = structure + self.variables_frame = variables + + 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) + return edges_list + + diff --git a/main_package/classes/trajectory.py b/main_package/classes/trajectory.py index cdb9adf..4b8866d 100644 --- a/main_package/classes/trajectory.py +++ b/main_package/classes/trajectory.py @@ -4,34 +4,27 @@ import numpy as np class Trajectory(): """ - Rappresenta una traiettoria come un data_frame contenente coppie (T_k,S_i) => (numpy.float64, string) + Rappresenta una traiettoria come un numpy_array contenente n-ple (indx, T_k,S_i,.....,Sj) Offre i metodi utili alla computazione sulla struttura stessa. - :actual_trajectory: il data_frame contenente la successione di coppie (T_k,S_i) + :actual_trajectory: il numpy_array contenente la successione di n-ple (indx, T_k,S_i,.....,Sj) """ - def __init__(self, data_frame): - self.actual_trajectory = data_frame + def __init__(self, list_of_columns): + self.actual_trajectory = np.array(list_of_columns, dtype=object).T def get_trajectory(self): return self.actual_trajectory - def get_trajectory_as_matrix(self): - """ - Converte il data_frame actual_trajectory in formato numpy.array - Parameters: - void - Returns: - numpy.array - """ - return self.actual_trajectory[['Time','State']].to_numpy() + def merge_columns(self, list_of_cols): + return np.vstack(list_of_cols).T - def get_states(self): - """ + """def get_states(self): + Identifica gli stati visitati nella traiettoria. Parameters: void Returns: una lista contenente gli stati visitati nella traiettoria - """ - return self.actual_trajectory['State'].unique() + + return self.actual_trajectory['State'].unique()"""