1
0
Fork 0

Add some tests on Trajectory class

parallel_struct_est
philpMartin 4 years ago
parent 3c91af18ca
commit d86e3af695
  1. 1
      main_package/classes/json_importer.py
  2. 4
      main_package/classes/sample_path.py
  3. 1
      main_package/classes/structure.py
  4. 10
      main_package/classes/trajectory.py
  5. 4
      main_package/tests/test_json_importer.py
  6. 24
      main_package/tests/test_trajectory.py

@ -118,7 +118,6 @@ class JsonImporter(AbstractImporter):
for indx, sample in enumerate(self.df_samples_list): for indx, sample in enumerate(self.df_samples_list):
self.df_samples_list[indx] = self.compute_row_delta_sigle_samples_frame(sample, self.df_samples_list[indx] = self.compute_row_delta_sigle_samples_frame(sample,
time_header_label, columns_header, shifted_cols_header) time_header_label, columns_header, shifted_cols_header)
#print(self.df_samples_list[indx])
self._concatenated_samples = pd.concat(self.df_samples_list) self._concatenated_samples = pd.concat(self.df_samples_list)
def build_list_of_samples_array(self, data_frame): def build_list_of_samples_array(self, data_frame):

@ -25,7 +25,9 @@ class SamplePath:
def build_trajectories(self): def build_trajectories(self):
self.importer.import_data() self.importer.import_data()
self._trajectories = tr.Trajectory(self.importer.build_list_of_samples_array(self.importer.concatenated_samples)) self._trajectories = \
tr.Trajectory(self.importer.build_list_of_samples_array(self.importer.concatenated_samples),
len(self.importer.sorter) + 1)
#self.trajectories.append(trajectory) #self.trajectories.append(trajectory)
self.importer.clear_data_frames() self.importer.clear_data_frames()

@ -12,7 +12,6 @@ class Structure:
def __init__(self, structure, variables): def __init__(self, structure, variables):
self.structure_frame = structure self.structure_frame = structure
self.variables_frame = variables self.variables_frame = variables
#self._nodes_indexes = self.list_of_nodes_indexes()
self.name_label = variables.columns.values[0] self.name_label = variables.columns.values[0]
self.value_label = variables.columns.values[1] self.value_label = variables.columns.values[1]

@ -14,15 +14,17 @@ class Trajectory:
""" """
def __init__(self, list_of_columns): def __init__(self, list_of_columns, original_cols_number):
print(list_of_columns) if type(list_of_columns[0][0]) != np.float64:
raise TypeError('The first array in the list has to be Times')
#TODO valutare se vale la pena ordinare la lista di numpy array per tipo
self.original_cols_number = original_cols_number
self._actual_trajectory = np.array(list_of_columns[1:], dtype=np.int).T self._actual_trajectory = np.array(list_of_columns[1:], dtype=np.int).T
self._times = np.array(list_of_columns[0], dtype=np.float) self._times = np.array(list_of_columns[0], dtype=np.float)
print(self._times)
@property @property
def trajectory(self): def trajectory(self):
return self._actual_trajectory[:, :4] return self._actual_trajectory[:, :self.original_cols_number]
@property @property
def complete_trajectory(self): def complete_trajectory(self):

@ -109,7 +109,11 @@ class TestJsonImporter(unittest.TestCase):
def test_import_data(self): def test_import_data(self):
j1 = ji.JsonImporter('../data', 'samples', 'dyn.str', 'variables', 'Time', 'Name') j1 = ji.JsonImporter('../data', 'samples', 'dyn.str', 'variables', 'Time', 'Name')
j1.import_data() j1.import_data()
self.assertEqual(list(j1.variables[j1.variables_key]),
list(j1.concatenated_samples.columns.values[1:len(j1.variables[j1.variables_key]) + 1]))
print(j1.variables) print(j1.variables)
print(j1.structure)
print(j1.concatenated_samples)

@ -0,0 +1,24 @@
import unittest
import numpy as np
import trajectory as tr
class TestTrajecotry(unittest.TestCase):
def test_init(self):
cols_list = [np.array([1.2,1.3,.14]), np.arange(1,4), np.arange(4,7)]
t1 = tr.Trajectory(cols_list, len(cols_list))
self.assertTrue(np.array_equal(cols_list[0], t1.times))
self.assertTrue(np.array_equal(np.ravel(t1.complete_trajectory[:, : 1]), cols_list[1]))
self.assertTrue(np.array_equal(np.ravel(t1.complete_trajectory[:, 1: 2]), cols_list[2]))
self.assertEqual(len(cols_list) - 1, t1.complete_trajectory.shape[1])
def test_init_first_array_not_float_type(self):
cols_list = [np.arange(1, 4), np.arange(4, 7), np.array([1.2, 1.3, .14])]
self.assertRaises(TypeError, tr.Trajectory, cols_list, len(cols_list))
if __name__ == '__main__':
unittest.main()