Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Fix readability + minor semantic issues in Python files #5

Merged
merged 2 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions planning_simulator_launcher/scenario_generator.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env python

from copy import deepcopy
import datetime
import itertools
import json
import math
import os
import re
import shutil
import uuid
from copy import deepcopy

import yaml


Expand Down Expand Up @@ -36,30 +32,30 @@ def __init__(self, params, path):
with open(path, "r+") as file:
self.yaml_str = file.read()

self.__yaml_generate_dir = self.generated_directory + "/" + scenario_name + "/"
self._yaml_generate_dir = self.generated_directory + "/" + scenario_name + "/"

if os.path.exists(self.__yaml_generate_dir):
if os.path.exists(self._yaml_generate_dir):
print('')
print('Cleanup target directory ' + os.path.relpath(self.__yaml_generate_dir))
print('Cleanup target directory ' + os.path.relpath(self._yaml_generate_dir))

for each in os.listdir(self.__yaml_generate_dir):
for each in os.listdir(self._yaml_generate_dir):
print(' Removing ' + each)
os.remove(os.path.join(self.__yaml_generate_dir, each))
os.remove(os.path.join(self._yaml_generate_dir, each))
else:
os.makedirs(self.__yaml_generate_dir)
os.makedirs(self._yaml_generate_dir)

print('')
print("Generating scenario " + scenario_name)
self.__generate()
self._generate()

def __del__(self):
self.generate_log.close()

def getScenarioFilesPath(self):
return self.__yaml_files_path
return self._yaml_files_path

def __generate(self):
self.__yaml_files_path = []
def _generate(self):
self._yaml_files_path = []
self.num_scenarios = 0

print(' Parameters')
Expand All @@ -86,8 +82,8 @@ def __generate(self):
for bindings in itertools.product(*list_of_list_of_bindings):
scenario_id = str(uuid.uuid4())

yaml_path = self.__yaml_generate_dir + scenario_id + ".yaml"
self.__yaml_files_path.append(yaml_path)
yaml_path = self._yaml_generate_dir + scenario_id + ".yaml"
self._yaml_files_path.append(yaml_path)
print('')
print(' ' + os.path.relpath(yaml_path))

Expand Down Expand Up @@ -122,8 +118,8 @@ def __generate(self):
else:
scenario_id = str(uuid.uuid4())

yaml_path = self.__yaml_generate_dir + scenario_id + ".yaml"
self.__yaml_files_path.append(yaml_path)
yaml_path = self._yaml_generate_dir + scenario_id + ".yaml"
self._yaml_files_path.append(yaml_path)
print(' ' + os.path.relpath(yaml_path))

result = deepcopy(self.yaml_str)
Expand All @@ -143,7 +139,7 @@ def __generate(self):

json.dump(generate_logs, self.generate_log)

def __getDefaultValueKeys(self, used_key):
def _getDefaultValueKeys(self, used_key):
variables = []

for variable in self.params.keys():
Expand All @@ -156,8 +152,8 @@ def __getDefaultValueKeys(self, used_key):

return variables

def __update_default_params(self, name, yaml_string):
for variable in self.__getDefaultValueKeys(name):
def _update_default_params(self, name, yaml_string):
for variable in self._getDefaultValueKeys(name):
print(' '),
print(variable),
print('=>'),
Expand All @@ -169,8 +165,8 @@ def __update_default_params(self, name, yaml_string):
return yaml_string

def evaluate(self, match):
pi = math.pi
return match.group(1) + str(eval(match.group(2))) + match.group(3)

def macroexpand(self, source):
return re.sub(r"^(.*)\${{\s+((?:pi|[\d\s+\-\*/%\(\).])*)\s+}}(.*)$", self.evaluate, source, flags=re.M)
regex = r"^(.*)\${{\s+((?:pi|[\d\s+\-\*/%\(\).])*)\s+}}(.*)$"
return re.sub(regex, self.evaluate, source, flags=re.M)
24 changes: 11 additions & 13 deletions planning_simulator_launcher/scenario_parameter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

import numpy as np


Expand All @@ -17,29 +15,29 @@ def __init__(self, yaml):
return

if 'Min' in yaml:
self.__min = yaml['Min']
self._min = yaml['Min']
elif 'Value' in yaml:
self.__min = yaml['Value']
self._min = yaml['Value']
else:
self.__min = 0
self._min = 0

print(' Min: ' + str(self.__min))
print(' Min: ' + str(self._min))

if 'Max' in yaml:
self.__max = yaml['Max']
self._max = yaml['Max']
else:
self.__max = self.__min
self._max = self._min

print(' Max: ' + str(self.__max))
print(' Max: ' + str(self._max))

if 'NumDivisions' in yaml:
self.__num_divisions = yaml['NumDivisions']
self._num_divisions = yaml['NumDivisions']
else:
self.__num_divisions = 1
self._num_divisions = 1

print(' NumDivisions: ' + str(self.__num_divisions))
print(' NumDivisions: ' + str(self._num_divisions))

self.values = np.linspace(self.__min, self.__max, self.__num_divisions)
self.values = np.linspace(self._min, self._max, self._num_divisions)
# print(' Values:'),
# print(self.values)

Expand Down
9 changes: 2 additions & 7 deletions planning_simulator_launcher/scenario_parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python
import os
import yaml

from planning_simulator_launcher.scenario_generator import ScenarioGenerator
from planning_simulator_launcher.scenario_parameter import ScenarioParameter
import os
import yaml


class ScenarioParser:
Expand Down Expand Up @@ -50,7 +49,3 @@ def parseScenarioParameter(self):
return False

return True


if __name__ == "__main__":
pass
68 changes: 49 additions & 19 deletions planning_simulator_launcher/show_result.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python

import argparse
import glob
import json

import termcolor


class Viewer:
def __init__(self,scenario_generated_path,log_path,verbose,quiet,output):
def __init__(self, scenario_generated_path, log_path, verbose, quiet, output):
self.quiet = quiet
self.verbose = verbose
self.log_path = log_path
Expand All @@ -23,21 +23,23 @@ def __init__(self,scenario_generated_path,log_path,verbose,quiet,output):
self.raw_data[scenario_id] = json_load
if self.getGenerateLog(scenario_id) is not None:
self.output_data.append(self.getResultDict(scenario_id))
if self.quiet == False:
if not self.quiet:
self.outputResult(output)
self.printResult()

def printResult(self):
if self.verbose:
self.printVerboseResult()
else:
self.printSimpleResult()
self.printSummary()

def printVerboseResult(self):
i = 1
for data in self.output_data:
message = "[" + str(i) + "/" + str(len(self.output_data)) + "] "
if data["passed"]:
message = message + "OK \n"
message = message + "OK \n"
else:
message = message + "NG \n"
message = message + "id = " + data["id"] + "\n"
Expand All @@ -53,7 +55,8 @@ def printVerboseResult(self):
message = message + " no scenario parameters"
param_index = 1
for param in data["params"]:
message = message + " (" + str(param_index) + "/" + str(len(data["params"])) + ") " + param + " : " + str(data["params"][param])
message = message + " (" + str(param_index) + "/" + str(len(data["params"])) \
+ ") " + param + " : " + str(data["params"][param])
param_index = param_index + 1
message = message + "\n"
if data["passed"]:
Expand All @@ -62,12 +65,13 @@ def printVerboseResult(self):
message = termcolor.colored(message, 'red')
print(message)
i = i + 1

def printSimpleResult(self):
i = 1
for data in self.output_data:
message = "[" + str(i) + "/" + str(len(self.output_data)) + "] "
if data["passed"]:
message = message + "OK "
message = message + "OK "
else:
message = message + "NG "
message = message + "id = " + data["id"]
Expand All @@ -77,23 +81,27 @@ def printSimpleResult(self):
message = termcolor.colored(message, 'red')
print(message)
i = i + 1

def printSummary(self):
print("[summary]")
i = 0
for data in self.output_data:
if data["passed"]:
i = i + 1
print("passed " + str(i) + "/" + str(len(self.output_data)) + " test cases.")
def outputResult(self,output):
if output != None:

def outputResult(self, output):
if output is not None:
with open(output, 'w') as f:
json.dump(self.output_data, f, indent=4)
def getGenerateLog(self,id):

def getGenerateLog(self, id):
for log in self.generate_log:
if log["id"] == id:
return log
return None
def getResultDict(self,id):

def getResultDict(self, id):
ret = {}
if self.getResult(id):
ret["passed"] = True
Expand All @@ -102,7 +110,7 @@ def getResultDict(self,id):
gen_log = self.getGenerateLog(id)
ret["parent"] = gen_log["parent_path"]
ret["id"] = id
ret["json_log"] = self.log_path + id + ".json"
ret["json_log"] = self.log_path + id + ".json"
ret["rosbag"] = self.log_path + id + ".bag"
metadata = self.raw_data[id]["metadata"]
ret["metadata"] = metadata
Expand All @@ -113,7 +121,8 @@ def getResultDict(self,id):
else:
ret["params"] = {}
return ret
def getResult(self,id):

def getResult(self, id):
logs = self.raw_data[id]["log"]
scenario_succeded = False
for log in logs:
Expand All @@ -128,13 +137,34 @@ def getResult(self,id):

def main():
parser = argparse.ArgumentParser(description='Show result')
parser.add_argument('scenario_generated_path', type=str, help='path of the generated scenario')
parser.add_argument('log_path', type=str,help='path of the log')
parser.add_argument('--verbose', help='show detail or not', action="store_true")
parser.add_argument('--quiet', help='does not show output', action="store_true")
parser.add_argument('--output',type=str,help='output path of the result(if not set, only output result to the screen)')
parser.add_argument(
'scenario_generated_path',
type=str,
help='path of the generated scenario'
)
parser.add_argument(
'log_path',
type=str,
help='path of the log'
)
parser.add_argument(
'--verbose',
help='show detail or not',
action="store_true"
)
parser.add_argument(
'--quiet',
help='does not show output',
action="store_true"
)
parser.add_argument(
'--output',
type=str,
help='output path of the result(if not set, only output result to the screen)'
)
args = parser.parse_args()
viwer = Viewer(args.scenario_generated_path,args.log_path,args.verbose,args.quiet,args.output)
Viewer(args.scenario_generated_path, args.log_path, args.verbose, args.quiet, args.output)


if __name__ == "__main__":
main()