From 8cf3013c492b21538d8ddd0f57c7e51369e35089 Mon Sep 17 00:00:00 2001 From: u214892 <u214892@sbb.ch> Date: Thu, 19 Sep 2019 17:44:59 +0200 Subject: [PATCH] #169 random guess --- benchmarks/profile_all_examples.py | 19 ++++--- benchmarks/run_all_examples.py | 52 ++++++++----------- ...ion_example_02_SingleAgentNavigationObs.py | 2 +- ...servation_example_03_ObservePredictions.py | 2 +- flatland/utils/misc.py | 3 ++ 5 files changed, 36 insertions(+), 42 deletions(-) create mode 100644 flatland/utils/misc.py diff --git a/benchmarks/profile_all_examples.py b/benchmarks/profile_all_examples.py index 0c36410e..51a2b33d 100644 --- a/benchmarks/profile_all_examples.py +++ b/benchmarks/profile_all_examples.py @@ -24,13 +24,12 @@ def profile(resource, entry): cProfile.run('my_func()', sort='time') -if __name__ == '__main__': - for entry in [entry for entry in importlib_resources.contents('examples') if - not pkg_resources.resource_isdir('examples', entry) - and entry.endswith(".py") - and '__init__' not in entry - and 'DELETE' not in entry - # TODO temporarily excluded simple_example_1.py since it hangs in ci - no idea why - and 'simple_example_1.py' not in entry - ]: - profile('examples', entry) +for entry in [entry for entry in importlib_resources.contents('examples') if + not pkg_resources.resource_isdir('examples', entry) + and entry.endswith(".py") + and '__init__' not in entry + and 'DELETE' not in entry + # TODO temporarily excluded simple_example_1.py since it hangs in ci - no idea why + and 'simple_example_1.py' not in entry + ]: + profile('examples', entry) diff --git a/benchmarks/run_all_examples.py b/benchmarks/run_all_examples.py index d23cb585..0d4b482c 100644 --- a/benchmarks/run_all_examples.py +++ b/benchmarks/run_all_examples.py @@ -8,33 +8,25 @@ from importlib_resources import path from benchmarks.benchmark_utils import swap_attr - -# https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python -def str2bool(v): - return v.lower() in ("yes", "true", "t", "1") - - -if __name__ == '__main__': - - for entry in [entry for entry in importlib_resources.contents('examples') if - not pkg_resources.resource_isdir('examples', entry) - and entry.endswith(".py") - and '__init__' not in entry - and 'DELETE' not in entry - # TODO temporarily excluded simple_example_1.py since it hangs in ci - no idea why - and 'simple_example_1.py' not in entry - ]: - with path('examples', entry) as file_in: - print("") - print("") - - print("") - print("*****************************************************************") - print("Running {}".format(entry)) - print("*****************************************************************") - with swap_attr(sys, "stdin", StringIO("q")): - runpy.run_path(file_in, run_name="__main__", init_globals={ - 'argv': ['--sleep-for-animation=False'] - }) - print("runpy done.") - print("Done with {}".format(entry)) +for entry in [entry for entry in importlib_resources.contents('examples') if + not pkg_resources.resource_isdir('examples', entry) + and entry.endswith(".py") + and '__init__' not in entry + and 'DELETE' not in entry + # TODO temporarily excluded simple_example_1.py since it hangs in ci - no idea why + and 'simple_example_1.py' not in entry + ]: + with path('examples', entry) as file_in: + print("") + print("") + + print("") + print("*****************************************************************") + print("Running {}".format(entry)) + print("*****************************************************************") + with swap_attr(sys, "stdin", StringIO("q")): + runpy.run_path(file_in, run_name="__main__", init_globals={ + 'argv': ['--sleep-for-animation=False'] + }) + print("runpy done.") + print("Done with {}".format(entry)) diff --git a/examples/custom_observation_example_02_SingleAgentNavigationObs.py b/examples/custom_observation_example_02_SingleAgentNavigationObs.py index e87b7852..6c6add68 100644 --- a/examples/custom_observation_example_02_SingleAgentNavigationObs.py +++ b/examples/custom_observation_example_02_SingleAgentNavigationObs.py @@ -5,12 +5,12 @@ import time import numpy as np -from benchmarks.run_all_examples import str2bool from flatland.core.grid.grid4_utils import get_new_position from flatland.envs.observations import TreeObsForRailEnv from flatland.envs.rail_env import RailEnv from flatland.envs.rail_generators import complex_rail_generator from flatland.envs.schedule_generators import complex_schedule_generator +from flatland.utils.misc import str2bool from flatland.utils.rendertools import RenderTool random.seed(100) diff --git a/examples/custom_observation_example_03_ObservePredictions.py b/examples/custom_observation_example_03_ObservePredictions.py index bdaaec33..92fbb37b 100644 --- a/examples/custom_observation_example_03_ObservePredictions.py +++ b/examples/custom_observation_example_03_ObservePredictions.py @@ -5,13 +5,13 @@ import time import numpy as np -from benchmarks.run_all_examples import str2bool from flatland.core.grid.grid_utils import coordinate_to_position from flatland.envs.observations import TreeObsForRailEnv from flatland.envs.predictions import ShortestPathPredictorForRailEnv from flatland.envs.rail_env import RailEnv from flatland.envs.rail_generators import complex_rail_generator from flatland.envs.schedule_generators import complex_schedule_generator +from flatland.utils.misc import str2bool from flatland.utils.ordered_set import OrderedSet from flatland.utils.rendertools import RenderTool diff --git a/flatland/utils/misc.py b/flatland/utils/misc.py new file mode 100644 index 00000000..e2282698 --- /dev/null +++ b/flatland/utils/misc.py @@ -0,0 +1,3 @@ +# https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python +def str2bool(v): + return v.lower() in ("yes", "true", "t", "1") -- GitLab