1
0
Fork 0

Complete Major changes in project structure

parallel_struct_est
philpMartin 4 years ago
parent 2859e531e1
commit 78e00ae3e4
  1. 68
      main_package/classes/dynamic_graph.py
  2. 32
      main_package/classes/importer.py
  3. 21
      main_package/classes/node.py
  4. 8
      main_package/classes/sample_path.py
  5. 15
      main_package/classes/trajectory.py

@ -0,0 +1,68 @@
import numpy as np
import sample_path as sp
import node
class DynamicGraph():
def __init__(self, sample_path):
self.sample_path = sample_path
self.graph = {}
self.states_number = 0
def initialize_graph(self, trajectory):
for(node_id,state) in enumerate(trajectory.get_states()):
n = node.Node(state, node_id)
self.graph[state] = {"Arcs":{}, "Time":0.0, "Node":n}
self.states_number += 1
def build_graph_from_first_traj(self, trajectory):
self.initialize_graph(trajectory)
matrix_traj = trajectory.get_trajectory_as_matrix()
for indx in range(len(matrix_traj) - 1):
self.add_transaction(matrix_traj, indx)
def add_transaction(self, matrix_traj, indx):
current_state = matrix_traj[indx][1]
next_state = matrix_traj[indx + 1][1]
self.graph[current_state]["Time"] += matrix_traj[indx + 1][0] - matrix_traj[indx][0]
next_node = self.graph[next_state]["Node"]
if next_node not in self.graph[current_state]["Arcs"].keys():
self.graph[current_state]["Arcs"][next_node] = 1
else:
self.graph[current_state]["Arcs"][next_node] += 1
def append_new_trajectory(self, trajectory):
matrix_traj = trajectory.get_trajectory_as_matrix()
for indx in range(len(matrix_traj) - 1):
current_state = matrix_traj[indx][1]
next_state = matrix_traj[indx + 1][1]
if current_state not in self.graph.keys():
current_node = node.Node(current_state, self.states_number)
self.graph[current_state] = {"Arcs":{}, "Time":0.0, "Node":current_node}
self.states_number += 1
if next_state not in self.graph.keys():
next_node = node.Node(next_state, self.states_number)
self.graph[next_state] = {"Arcs":{}, "Time":0.0, "Node":next_node}
self.states_number += 1
self.add_transaction(matrix_traj, indx)
def build_graph(self):
for indx, trajectory in enumerate(self.sample_path.trajectories):
if indx == 0:
self.build_graph_from_first_traj(trajectory)
else:
self.append_new_trajectory(trajectory)
######Veloci Tests#######
s1 = sp.SamplePath()
s1.build_trajectories()
print(s1.get_number_trajectories())
g1 = DynamicGraph(s1)
g1.build_graph()
print(g1.graph)
print(g1.states_number)

@ -33,8 +33,8 @@ class Importer():
for file in read_files:
my_df = pd.read_csv(file) #TODO:Aggiungere try-catch controllo correttezza dei tipi di dato presenti nel dataset e.g. i tipi di dato della seconda colonna devono essere float
self.df_list.append(my_df)
def merge_value_columns(self, df):
""" Effettua il merging di tutte le colonne che contengono i valori delle variabili in un unica colonna chiamata State.
@ -45,6 +45,15 @@ class Importer():
"""
df['State'] = df[df.columns[2:]].apply(lambda row: ''.join(row.values.astype(str)), axis=1)
def drop_unneccessary_columns(self, df):
cols = df.columns.values[2:-1]
print(cols)
df.drop(cols, axis=1, inplace=True)
def drop_unneccessary_columns_in_all_frames(self):
for data_frame in self.df_list:
self.drop_unneccessary_columns(data_frame)
def merge_value_columns_in_all_frames(self):
for data_frame in self.df_list:
self.merge_value_columns(data_frame)
@ -55,30 +64,9 @@ class Importer():
def clear_data_frames(self):
for data_frame in self.df_list:
data_frame = data_frame.iloc[0:0]
"""def build_trajectories(self):
for data_frame in self.df_list:
self.merge_value_columns(data_frame)
trajectory = data_frame[['Time','State']].to_numpy()
self.trajectories.append(trajectory)
#Clear the data_frame
data_frame = data_frame.iloc[0:0]"""
#imp = Importer("../data")
#imp.import_data_from_csv()
#imp.build_trajectories()
#print(imp.trajectories[0])

@ -0,0 +1,21 @@
class Node():
def __init__(self, state_id, node_id=-1):
self.state_id = state_id
self.node_id = node_id
def __key(self):
return (self.state_id)
def __hash__(self):
return hash(self.__key())
def __eq__(self, other):
if isinstance(other, Node):
return self.__key() == other.__key()
return NotImplemented
def __repr__(self):
return "<%s, %d>"% (self.state_id, self.node_id)

@ -1,12 +1,13 @@
import pandas as pd
import numpy as np
import os
import importer as imp
import trajectory as tr
class SamplePath():
def __init__(self, files_path):
def __init__(self, files_path=os.getcwd() + "/main_package/data"):
self.importer = imp.Importer(files_path)
self.trajectories = []
@ -14,6 +15,7 @@ class SamplePath():
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.trajectories.append(trajectory)
@ -26,9 +28,9 @@ class SamplePath():
######Veloci Tests#######
s1 = SamplePath("../data")
s1 = SamplePath()
s1.build_trajectories()
print(s1.get_number_trajectories())
print(type(s1.trajectories[0].get_trajectory()[0][1]))
#print(s1.trajectories[0].get_trajectory())

@ -1,16 +1,17 @@
import pandas as pd
import numpy as np
import importer
class Trajectory():
def __init__(self, data_frame):
self.actual_trajectory = self.build_trajectory(data_frame)
def build_trajectory(self, data_frame):
return data_frame[['Time','State']].to_numpy()
self.actual_trajectory = data_frame
def get_trajectory(self):
return self.actual_trajectory
def get_trajectory_as_matrix(self):
return self.actual_trajectory[['Time','State']].to_numpy()
def get_states(self):
return self.actual_trajectory['State'].unique()