-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautorun.py
More file actions
224 lines (196 loc) · 9.75 KB
/
Copy pathautorun.py
File metadata and controls
224 lines (196 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import argparse
import subprocess
import yaml
import csv
import time
import psutil
import os
import glob
import rosbag
from datetime import datetime, timedelta
def load_yaml_file(file_path):
with open(file_path, 'r') as file:
return yaml.safe_load(file)
def save_yaml_file(data, file_path):
with open(file_path, 'w') as file:
yaml.safe_dump(data, file, default_flow_style=False)
def save_csv_file(csv_header, data, file_path):
with open(file_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(csv_header)
writer.writerows(data)
class AutoRunner:
def __init__(self):
self.waiting_time = 3
self.results_folder = ""
self.datasets_config = {}
self.algorithms_config = {}
self.dataset_cache = {}
self.runner_cache = {}
self.processes = []
self.roscore_process = None
def create_process(self, args, stdout_log=None, stderr_log=None):
if stdout_log and stderr_log:
with open(stdout_log, 'w') as f:
with open(stderr_log, 'w') as e:
return subprocess.Popen(args, shell=True, stdout=f, stderr=e)
elif stdout_log:
with open(stdout_log, 'w') as f:
return subprocess.Popen(args, shell=True, stdout=f, stderr=subprocess.DEVNULL)
else:
return subprocess.Popen(args, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def terminate_process(self, process):
if process is None or not psutil.pid_exists(process.pid):
return
parent = psutil.Process(process.pid)
children = parent.children(recursive=True)
for child in children:
child.terminate()
parent.terminate()
def cleanup_sequence(self):
for process in self.processes:
self.terminate_process(process)
self.processes = []
time.sleep(self.waiting_time)
def cleanup_all(self):
for process in self.processes:
self.terminate_process(process)
self.terminate_process(self.roscore_process)
time.sleep(3)
def get_sequence_duration(self, dataset, sequence):
# Get the rosbag files for this sequence
sequence_path = os.path.join(dataset['data_folder'], sequence)
if os.path.isfile(f"{sequence_path}.bag"):
rosbag_files = [f"{sequence_path}.bag"]
elif os.path.isdir(sequence_path):
rosbag_files = glob.glob(os.path.join(sequence_path, '*.bag'))
else:
print(f"No rosbag file or directory found for sequence {sequence_path}.")
# Calculate the duration for all rosbags in the sequence
sequence_duration = 0
for rosbag_file in rosbag_files:
try:
with rosbag.Bag(rosbag_file, 'r') as bag:
duration = bag.get_end_time() - bag.get_start_time()
sequence_duration += duration
except Exception as e:
print(f"Could not read rosbag file '{rosbag_file}': {e}")
return sequence_duration
def estimate_total_time(self, datasets_config, algorithms_config):
total_time = 0
for dataset_name, dataset in datasets_config.items():
for sequence in dataset['sequences']:
# Get the total duration for this sequence
sequence_key = f"{dataset_name}_{sequence}"
if sequence_key in self.dataset_cache:
sequence_duration = self.dataset_cache[sequence_key]
else:
sequence_duration = self.get_sequence_duration(dataset, sequence)
# Update the cache with the total duration for this sequence
self.dataset_cache[sequence_key] = sequence_duration
total_time += sequence_duration
return total_time * len(algorithms_config)
def run_sequence(self, algorithm_name, algorithm, dataset_name, dataset, sequence):
sequence_folder = f"{self.results_folder}/{algorithm_name}/{dataset_name}/{sequence}"
os.makedirs(sequence_folder, exist_ok=True)
# Process 1: run algorithm
for i, command in enumerate(algorithm['launch_commands'], start=1):
stdout_log = f"{sequence_folder}/stdout_{i}.txt"
stderr_log = f"{sequence_folder}/stderr_{i}.txt"
process = self.create_process(command, stdout_log=stdout_log, stderr_log=stderr_log)
self.processes.append(process)
# Process 2: play rosbag (dataset sequences)
if os.path.exists(f"{dataset['data_folder']}/{sequence}.bag"):
rosbag_path = f"{dataset['data_folder']}/{sequence}.bag"
else:
rosbag_path = f"{dataset['data_folder']}/{sequence}/*.bag"
remap_lidar_topic = f"{dataset['lidar_topic']}:={algorithm['input_topics']['lidar_topic']}"
remap_imu_topic = f"{dataset['imu_topic']}:={algorithm['input_topics']['imu_topic']}"
more_args = f"-d {self.waiting_time} {' '.join(algorithm.get('rosbag_args') or [])}"
rosbag_play_cmd = " ".join(["rosbag play", rosbag_path, remap_lidar_topic, remap_imu_topic, more_args])
rosbag_play_process = self.create_process(rosbag_play_cmd)
self.processes.append(rosbag_play_process)
# Process 3: record rosbag (as the results from algorithms)
topics_to_record = ' '.join(algorithm.get('output_topics') or ['-a'])
output_bag = f"{sequence_folder}/results.bag"
rosbag_record_cmd = f"rosbag record {topics_to_record} -O {output_bag}"
rosbag_record_process = self.create_process(rosbag_record_cmd)
self.processes.append(rosbag_record_process)
# Print the estimated running time, while waiting for the rosbag play to finish
sequence_duration = self.dataset_cache[f"{dataset_name}_{sequence}"]
begin_time = time.time()
cpu_usage = []
while rosbag_play_process.poll() is None:
elapsed_time = time.time() - begin_time
print(f"Estimated running time: {elapsed_time:.2f} / {sequence_duration:.2f} seconds.", end = '\r', flush=True)
cpu_usage.append([elapsed_time, psutil.cpu_percent(interval=None)])
time.sleep(0.1)
# Update runner cache (to mark this sequence completed) and save cpu usage
self.runner_cache[f"{algorithm_name}_{dataset_name}_{sequence}"] = True
save_yaml_file(self.runner_cache, f"{self.results_folder}/.runner_cache.yaml")
save_csv_file(['Time Elapsed', 'CPU Usage (%)'], cpu_usage, f"{sequence_folder}/cpu_usage.csv")
# run cleanup commands if any
if 'cleanup_commands' in algorithm:
for command in algorithm['cleanup_commands']:
os.environ['ALG_FOLDER'] = f"{self.results_folder}/{algorithm_name}"
os.environ['SEQ_FOLDER'] = sequence_folder
subprocess.run(command, shell=True, executable='/bin/bash', env=os.environ.copy())
def run(self):
for algorithm_name, algorithm in self.algorithms_config.items():
for dataset_name, dataset in self.datasets_config.items():
for sequence in dataset['sequences']:
if f"{algorithm_name}_{dataset_name}_{sequence}" in self.runner_cache:
print(f"Skipping algorithm {algorithm_name}, dataset {dataset_name}, sequence {sequence}.")
continue
print(f"Running algorithm {algorithm_name}, dataset {dataset_name}, sequence {sequence}.")
self.run_sequence(algorithm_name, algorithm, dataset_name, dataset, sequence)
self.cleanup_sequence()
def init(self, results_folder, datasets_config_path, algorithms_config_path):
# Determine if we continue from a previous run, and create the results folder accordingly
if results_folder is not None and os.path.exists(results_folder):
self.results_folder = results_folder
if os.path.exists(f"{results_folder}/.runner_cache.yaml"):
self.runner_cache = load_yaml_file(f"{results_folder}/.runner_cache.yaml")
print("Found existing runner cache, skipping completed sequences.")
else:
self.results_folder = "results_" + datetime.now().strftime("%m%d%H%M")
os.makedirs(self.results_folder, exist_ok=True)
# Load the datasets configuration file
if os.path.exists(f"{self.results_folder}/datasets.yaml"):
self.datasets_config = load_yaml_file(f"{self.results_folder}/datasets.yaml")
else:
self.datasets_config = load_yaml_file(datasets_config_path)
save_yaml_file(self.datasets_config, f"{self.results_folder}/datasets.yaml")
# Load the algorithms configuration file
if os.path.exists(f"{self.results_folder}/algorithms.yaml"):
self.algorithms_config = load_yaml_file(f"{self.results_folder}/algorithms.yaml")
else:
self.algorithms_config = load_yaml_file(algorithms_config_path)
save_yaml_file(self.algorithms_config, f"{self.results_folder}/algorithms.yaml")
# Load the dataset cache and estimate the total running time
if os.path.exists('.dataset_cache.yaml'):
self.dataset_cache = load_yaml_file('.dataset_cache.yaml')
print(f"Estimating total running time...")
total_time = self.estimate_total_time(self.datasets_config, self.algorithms_config)
save_yaml_file(self.dataset_cache, '.dataset_cache.yaml')
print(f"Estimated total running time: {total_time:.2f} seconds or {total_time/3600.0:.2f} hours.")
print(f"Estimated completion time: {(datetime.now() + timedelta(seconds=total_time)).strftime('%Y-%m-%d %H:%M:%S')}")
# Start roscore
self.roscore_process = self.create_process("roscore")
time.sleep(3)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Load YAML configuration for datasets and algorithms.')
parser.add_argument('results_folder', type=str, nargs='?', help='Path to the results folder.')
parser.add_argument('--datasets', type=str, default='datasets.yaml', help='Path to the datasets configuration file.')
parser.add_argument('--algorithms', type=str, default='algorithms.yaml', help='Path to the algorithms configuration file.')
args = parser.parse_args()
auto_runner = AutoRunner()
auto_runner.waiting_time = 10
try:
auto_runner.init(args.results_folder, args.datasets, args.algorithms)
auto_runner.run()
print("Finished running all algorithms on all datasets.")
except KeyboardInterrupt:
print("KeyboardInterrupt: Shutting down...")
finally:
auto_runner.cleanup_all()