1
0
Fork 0

Major changes in sample_path class

parallel_struct_est
philpMartin 4 years ago
parent f2bc57acf9
commit 71aacbd808
  1. 5
      main_package/classes/dynamic_graph.py
  2. 30
      main_package/classes/json_importer.py
  3. 35
      main_package/classes/sample_path.py
  4. 16
      main_package/classes/structure.py
  5. 27
      main_package/classes/trajectory.py

@ -1,9 +1,10 @@
import numpy as np import numpy as np
import sample_path as sp import sample_path as sp
import networkx as nx
import node import node
class DynamicGraph(): class NetworkGraph():
""" """
Rappresenta un grafo dinamico con la seguente struttura: Rappresenta un grafo dinamico con la seguente struttura:
{Key: {Arcs:{node_object: #in_arcs.....}, Time:t, Node:node_object}.....} {Key: {Arcs:{node_object: #in_arcs.....}, Time:t, Node:node_object}.....}
@ -20,7 +21,7 @@ class DynamicGraph():
def __init__(self, sample_path): def __init__(self, sample_path):
self.sample_path = sample_path self.sample_path = sample_path
self.graph = {} self.graph = {}
self.states_number = 0
def initialize_graph(self, trajectory): def initialize_graph(self, trajectory):
""" """

@ -15,11 +15,10 @@ class JsonImporter(AbstractImporter):
super(JsonImporter, self).__init__(files_path) super(JsonImporter, self).__init__(files_path)
def import_data(self): def import_data(self):
data = self.read_json_file() raw_data = self.read_json_file()
self.import_trajectories(data) self.import_trajectories(raw_data)
self.import_structure(data) self.import_structure(raw_data)
self.import_variables(data) self.import_variables(raw_data)
def import_trajectories(self, raw_data): def import_trajectories(self, raw_data):
self.normalize_trajectories(raw_data, 0, 'samples') self.normalize_trajectories(raw_data, 0, 'samples')
@ -36,7 +35,7 @@ class JsonImporter(AbstractImporter):
for file_name in read_files: for file_name in read_files:
with open(file_name) as f: with open(file_name) as f:
data = json.load(f) data = json.load(f)
return data return data
except ValueError as err: except ValueError as err:
print(err.args) print(err.args)
@ -45,7 +44,13 @@ class JsonImporter(AbstractImporter):
def normalize_trajectories(self, raw_data, indx, trajectories_key): def normalize_trajectories(self, raw_data, indx, trajectories_key):
for sample_indx, sample in enumerate(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): def clear_data_frames(self):
""" """
@ -55,13 +60,14 @@ class JsonImporter(AbstractImporter):
Returns: Returns:
void void
""" """
for data_frame in self.df_list: for indx in range(len(self.df_samples_list)):
data_frame = data_frame.iloc[0:0] #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() ij.import_data()
print(ij.df_samples_list[7]) #print(ij.df_samples_list[7])
print(ij.df_structure) print(ij.df_structure)
print(ij.df_variables) print(ij.df_variables)
print((ij.build_list_of_samples_array(0)[1].size))"""

@ -1,11 +1,12 @@
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import os import os
import importer as imp import json_importer as imp
import trajectory as tr import trajectory as tr
import structure as st
class SamplePath(): class SamplePath:
""" """
Rappresenta l'aggregazione di una o più traiettorie. Rappresenta l'aggregazione di una o più traiettorie.
Ha il compito dato di costruire tutte gli oggetti Trajectory a partire Ha il compito dato di costruire tutte gli oggetti Trajectory a partire
@ -15,23 +16,35 @@ class SamplePath():
:trajectories: lista contenente le traiettorie create :trajectories: lista contenente le traiettorie create
""" """
def __init__(self, files_path=os.getcwd() + "/main_package/data"): def __init__(self, files_path):
self.importer = imp.Importer(files_path) print()
self.importer = imp.JsonImporter(files_path)
self.trajectories = [] self.trajectories = []
self.structure = None
def build_trajectories(self): def build_trajectories(self):
self.importer.import_data_from_csv() self.importer.import_data()
self.importer.merge_value_columns_in_all_frames() for traj_data_frame in self.importer.df_samples_list:
self.importer.drop_unneccessary_columns_in_all_frames() trajectory = tr.Trajectory(self.importer.build_list_of_samples_array(traj_data_frame))
for data_frame in self.importer.get_data_frames():
trajectory = tr.Trajectory(data_frame)
self.trajectories.append(trajectory) self.trajectories.append(trajectory)
self.importer.clear_data_frames() 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): def get_number_trajectories(self):
return len(self.trajectories) 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()))

@ -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

@ -4,34 +4,27 @@ import numpy as np
class Trajectory(): 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. 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): def __init__(self, list_of_columns):
self.actual_trajectory = data_frame self.actual_trajectory = np.array(list_of_columns, dtype=object).T
def get_trajectory(self): def get_trajectory(self):
return self.actual_trajectory return self.actual_trajectory
def get_trajectory_as_matrix(self): def merge_columns(self, list_of_cols):
""" return np.vstack(list_of_cols).T
Converte il data_frame actual_trajectory in formato numpy.array
Parameters:
void
Returns:
numpy.array
"""
return self.actual_trajectory[['Time','State']].to_numpy()
def get_states(self): """def get_states(self):
"""
Identifica gli stati visitati nella traiettoria. Identifica gli stati visitati nella traiettoria.
Parameters: Parameters:
void void
Returns: Returns:
una lista contenente gli stati visitati nella traiettoria una lista contenente gli stati visitati nella traiettoria
"""
return self.actual_trajectory['State'].unique() return self.actual_trajectory['State'].unique()"""