fixing style problems

This commit is contained in:
Felipe Codevilla 2018-06-25 11:59:06 +02:00
parent 1f8d486fcb
commit 6396199a38
2 changed files with 93 additions and 109 deletions

View File

@ -4,12 +4,19 @@ import os
class Recording(object): class Recording(object):
"""
Class to record all the logs for the benchmark. It records individual episodes measurements
and also summary for each episodes.
"""
def __init__(self def __init__(self, name_to_save, continue_experiment, save_images):
, name_to_save """
, continue_experiment Recorder constructors
, save_images Args:
): name_to_save: Name of the log
continue_experiment: If you want to continue a previous experiment with the same names
save_images: If you want to save images to the disk.
"""
self._dict_summary = {'exp_id': -1, self._dict_summary = {'exp_id': -1,
'rep': -1, 'rep': -1,
@ -44,15 +51,13 @@ class Recording(object):
os.mkdir('_benchmarks_results') os.mkdir('_benchmarks_results')
# Generate the full path for the log files # Generate the full path for the log files
self._path = os.path.join('_benchmarks_results' self._path = os.path.join('_benchmarks_results', name_to_save)
, name_to_save
)
# Check for continuation of experiment, also returns the last line, used for test purposes # Check for continuation of experiment, also returns the last line, used for test purposes
# If you don't want to continue it will create a new path name with a number. # If you don't want to continue it will create a new path name with a number.
# Also returns the fieldnames for both measurements and summary, so you can keep the # Also returns the fieldnames for both measurements and summary, so you can keep the
# previous order # previous order
self._path, _, self._summary_fieldnames, self._measurements_fieldnames\ self._path, _, self._summary_fieldnames, self._measurements_fieldnames \
= self._continue_experiment(continue_experiment) = self._continue_experiment(continue_experiment)
self._create_log_files() self._create_log_files()
@ -72,20 +77,31 @@ class Recording(object):
return self._path return self._path
def log_poses(self, start_index, end_index, weather_id): def log_poses(self, start_index, end_index, weather_id):
"""
Log tested poses by the benchmark
"""
with open(self._internal_log_name, 'a+') as log: with open(self._internal_log_name, 'a+') as log:
log.write(' Start Poses (%d %d ) on weather %d \n ' % log.write(' Start Poses (%d %d ) on weather %d \n ' %
(start_index, end_index, weather_id)) (start_index, end_index, weather_id))
def log_poses_finish(self): def log_poses_finish(self):
"""
Log when a set of poses (task) is finished by the benchmark
"""
with open(self._internal_log_name, 'a+') as log: with open(self._internal_log_name, 'a+') as log:
log.write('Finished Task') log.write('Finished Task')
def log_start(self, id_experiment): def log_start(self, id_experiment):
"""
Log when a set of poses (task) is started by the benchmark
"""
with open(self._internal_log_name, 'a+') as log: with open(self._internal_log_name, 'a+') as log:
log.write('Start Task %d \n' % id_experiment) log.write('Start Task %d \n' % id_experiment)
def log_end(self): def log_end(self):
"""
Log when the benchmark is finished
"""
with open(self._internal_log_name, 'a+') as log: with open(self._internal_log_name, 'a+') as log:
log.write('====== Finished Entire Benchmark ======') log.write('====== Finished Entire Benchmark ======')
@ -110,7 +126,6 @@ class Recording(object):
with open(os.path.join(self._path, 'summary.csv'), 'a+') as ofd: with open(os.path.join(self._path, 'summary.csv'), 'a+') as ofd:
w = csv.DictWriter(ofd, self._dict_summary.keys()) w = csv.DictWriter(ofd, self._dict_summary.keys())
w.fieldnames = self._summary_fieldnames w.fieldnames = self._summary_fieldnames
w.writerow(self._dict_summary) w.writerow(self._dict_summary)
def write_measurements_results(self, experiment, rep, pose, reward_vec, control_vec): def write_measurements_results(self, experiment, rep, pose, reward_vec, control_vec):

View File

@ -1,9 +1,8 @@
import unittest import unittest
from carla.driving_benchmark.recording import Recording from carla.driving_benchmark.recording import Recording
class testRecording(unittest.TestCase):
class testRecording(unittest.TestCase):
def test_init(self): def test_init(self):
import os import os
@ -13,93 +12,81 @@ class testRecording(unittest.TestCase):
""" """
recording = Recording(name_to_save='Test1',
recording = Recording(name_to_save='Test1' continue_experiment=False, save_images=True
, continue_experiment=False, save_images=True
) )
_ = open(os.path.join(recording._path, 'summary.csv'), 'r')
_ = open(os.path.join(recording._path,'summary.csv'), 'r')
_ = open(os.path.join(recording._path, 'measurements.csv'), 'r') _ = open(os.path.join(recording._path, 'measurements.csv'), 'r')
# There should be three files in any newly created case # There should be three files in any newly created case
self.assertEqual(len(os.listdir(recording._path)), 3) self.assertEqual(len(os.listdir(recording._path)), 3)
def test_write_summary_results(self): def test_write_summary_results(self):
import os import os
from carla.driving_benchmark.experiment import Experiment from carla.driving_benchmark.experiment import Experiment
recording = Recording(name_to_save='Test1' recording = Recording(name_to_save='Test1',
, continue_experiment=False, save_images=True continue_experiment=False, save_images=True
) )
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
with open(os.path.join(recording._path, 'summary.csv'), 'r') as f: with open(os.path.join(recording._path, 'summary.csv'), 'r') as f:
header = f.readline().split(',') header = f.readline().split(',')
#Assert if header is header # Assert if header is header
self.assertIn('exp_id', header) self.assertIn('exp_id', header)
self.assertEqual(len(header), len(recording._dict_summary)) self.assertEqual(len(header), len(recording._dict_summary))
#Assert if there is something writen in the row # Assert if there is something writen in the row
written_row = f.readline().split(',') written_row = f.readline().split(',')
#Assert if the number of collums is correct # Assert if the number of collums is correct
self.assertEqual(len(written_row), len(recording._dict_summary)) self.assertEqual(len(written_row), len(recording._dict_summary))
def test_write_summary_results_good_order(self): def test_write_summary_results_good_order(self):
import os
from carla.driving_benchmark.experiment import Experiment from carla.driving_benchmark.experiment import Experiment
recording = Recording(name_to_save='Test_good_order' recording = Recording(name_to_save='Test_good_order',
, continue_experiment=False, save_images=True continue_experiment=False, save_images=True
) )
for i in range(0, 10):
recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1)
recording = Recording(name_to_save='Test_good_order',
continue_experiment=True, save_images=True
)
for i in range(0, 10): for i in range(0, 10):
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
recording = Recording(name_to_save='Test_good_order'
, continue_experiment=True, save_images=True
)
recording = Recording(name_to_save='Test_good_order',
continue_experiment=True, save_images=True
)
for i in range(0, 10): for i in range(0, 10):
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
recording = Recording(name_to_save='Test_good_order'
, continue_experiment=True, save_images=True
)
recording = Recording(name_to_save='Test_good_order',
continue_experiment=True, save_images=True
)
for i in range(0, 10): for i in range(0, 10):
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
recording = Recording(name_to_save='Test_good_order'
, continue_experiment=True, save_images=True
)
for i in range(0, 10):
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1,
path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1)
def test_write_measurements_results(self): def test_write_measurements_results(self):
@ -108,12 +95,10 @@ class testRecording(unittest.TestCase):
from carla.carla_server_pb2 import Measurements from carla.carla_server_pb2 import Measurements
from carla.carla_server_pb2 import Control from carla.carla_server_pb2 import Control
recording = Recording(name_to_save='Test1',
recording = Recording(name_to_save='Test1' continue_experiment=False, save_images=True
, continue_experiment=False, save_images=True
) )
reward_vec = [Measurements().player_measurements for x in range(20)] reward_vec = [Measurements().player_measurements for x in range(20)]
control_vec = [Control() for x in range(25)] control_vec = [Control() for x in range(25)]
@ -122,24 +107,22 @@ class testRecording(unittest.TestCase):
control_vec=control_vec) control_vec=control_vec)
with open(os.path.join(recording._path, 'measurements.csv'), 'r') as f: with open(os.path.join(recording._path, 'measurements.csv'), 'r') as f:
header = f.readline().split(',') header = f.readline().split(',')
#Assert if header is header # Assert if header is header
self.assertIn('exp_id', header) self.assertIn('exp_id', header)
self.assertEqual(len(header), len(recording._dict_measurements)) self.assertEqual(len(header), len(recording._dict_measurements))
#Assert if there is something writen in the row # Assert if there is something writen in the row
written_row = f.readline().split(',') written_row = f.readline().split(',')
#Assert if the number of collums is correct # Assert if the number of collums is correct
self.assertEqual(len(written_row), len(recording._dict_measurements)) self.assertEqual(len(written_row), len(recording._dict_measurements))
def test_continue_experiment(self): def test_continue_experiment(self):
recording = Recording( name_to_save='Test1' recording = Recording(name_to_save='Test1',
, continue_experiment=False, save_images=True continue_experiment=False, save_images=True
) )
# A just started case should return the continue experiment case # A just started case should return the continue experiment case
@ -150,31 +133,25 @@ class testRecording(unittest.TestCase):
from carla.driving_benchmark.experiment import Experiment from carla.driving_benchmark.experiment import Experiment
recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
# After writing two experiments it should return 2, so you could start writing os pos 3 # After writing two experiments it should return 2, so you could start writing os pos 3
self.assertEqual(recording._continue_experiment(True)[1], 3) self.assertEqual(recording._continue_experiment(True)[1], 3)
# If you dont want to continue, should return also one # If you dont want to continue, should return also one
self.assertEqual(recording._continue_experiment(False)[1], 1) self.assertEqual(recording._continue_experiment(False)[1], 1)
def test_get_pose_and_experiment(self): def test_get_pose_and_experiment(self):
recording = Recording(name_to_save='Test1',
continue_experiment=False, save_images=True
recording = Recording( name_to_save='Test1'
, continue_experiment=False, save_images=True
) )
from carla.driving_benchmark.experiment import Experiment from carla.driving_benchmark.experiment import Experiment
pose, experiment = recording.get_pose_and_experiment(25) pose, experiment = recording.get_pose_and_experiment(25)
# An starting experiment should return zero zero # An starting experiment should return zero zero
@ -182,13 +159,12 @@ class testRecording(unittest.TestCase):
self.assertEqual(pose, 0) self.assertEqual(pose, 0)
self.assertEqual(experiment, 0) self.assertEqual(experiment, 0)
recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1, path_distance=200, remaining_distance=0,
path_distance=200, remaining_distance=0, final_time=0.2, time_out=49, result=1)
final_time=0.2, time_out=49, result=1) recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
recording.write_summary_results( experiment=Experiment(), pose=[24,32], rep=1, path_distance=200, remaining_distance=0,
path_distance=200, remaining_distance=0, final_time=0.2, time_out=49, result=1)
final_time=0.2, time_out=49, result=1)
pose, experiment = recording.get_pose_and_experiment(25) pose, experiment = recording.get_pose_and_experiment(25)
self.assertEqual(pose, 2) self.assertEqual(pose, 2)
@ -214,11 +190,10 @@ class testRecording(unittest.TestCase):
def test_get_pose_and_experiment_corner(self): def test_get_pose_and_experiment_corner(self):
from carla.driving_benchmark.experiment import Experiment from carla.driving_benchmark.experiment import Experiment
recording = Recording( name_to_save='Test1' recording = Recording(name_to_save='Test1',
, continue_experiment=False, save_images=True continue_experiment=False, save_images=True
) )
pose, experiment = recording.get_pose_and_experiment(1) pose, experiment = recording.get_pose_and_experiment(1)
@ -232,44 +207,39 @@ class testRecording(unittest.TestCase):
self.assertEqual(pose, 0) self.assertEqual(pose, 0)
self.assertEqual(experiment, 0) self.assertEqual(experiment, 0)
recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
recording.write_summary_results( experiment=Experiment(), pose=[24, 32], rep=1, path_distance=200, remaining_distance=0,
path_distance=200, remaining_distance=0, final_time=0.2, time_out=49, result=1)
final_time=0.2, time_out=49, result=1)
pose, experiment = recording.get_pose_and_experiment(1) pose, experiment = recording.get_pose_and_experiment(1)
print (pose, experiment) print(pose, experiment)
self.assertEqual(pose, 0) self.assertEqual(pose, 0)
self.assertEqual(experiment, 1) self.assertEqual(experiment, 1)
pose, experiment = recording.get_pose_and_experiment(2) pose, experiment = recording.get_pose_and_experiment(2)
print (pose, experiment) print(pose, experiment)
# An starting experiment should return one # An starting experiment should return one
self.assertEqual(pose, 1) self.assertEqual(pose, 1)
self.assertEqual(experiment, 0) self.assertEqual(experiment, 0)
pose, experiment = recording.get_pose_and_experiment(3) pose, experiment = recording.get_pose_and_experiment(3)
print (pose, experiment) print(pose, experiment)
# An starting experiment should return one # An starting experiment should return one
self.assertEqual(pose, 1) self.assertEqual(pose, 1)
self.assertEqual(experiment, 0) self.assertEqual(experiment, 0)
recording.write_summary_results( experiment=Experiment(), pose=[24, 32], rep=1, recording.write_summary_results(experiment=Experiment(), pose=[24, 32], rep=1,
path_distance=200, remaining_distance=0, path_distance=200, remaining_distance=0,
final_time=0.2, time_out=49, result=1) final_time=0.2, time_out=49, result=1)
pose, experiment = recording.get_pose_and_experiment(2) pose, experiment = recording.get_pose_and_experiment(2)
self.assertEqual(pose, 0) self.assertEqual(pose, 0)
self.assertEqual(experiment, 1) self.assertEqual(experiment, 1)
pose, experiment = recording.get_pose_and_experiment(3) pose, experiment = recording.get_pose_and_experiment(3)
self.assertEqual(pose, 2) self.assertEqual(pose, 2)
@ -278,4 +248,3 @@ class testRecording(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()