diff --git a/benchmarks/profile_all_examples.py b/benchmarks/profile_all_examples.py index 0c36410ef5336913fc8cf0a15d15d7bb8682d75d..51a2b33dbfc3d675694c67fd6fa8fbcf6eabaa66 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 d23cb58537b9005f40a182633e0556462d047f07..0d4b482ce7723e88f3497f2d7958ee8261f23205 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 e87b7852adad8650faa9633b470426d94493461f..6c6add683bf5b7694d939bbe1a590617fb069d3e 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 bdaaec33e8e4285856cbbdcd8787ff0adb07e405..92fbb37b2a0d8ff583b3056e418d11026f261032 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 0000000000000000000000000000000000000000..e2282698c7a8030a2d8637d7ef9acd8d80afb517 --- /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")