From c675c9d1c4b0f88693dbebad293b2b81df88d81c Mon Sep 17 00:00:00 2001 From: bion howard Date: Mon, 13 Jul 2020 19:40:31 -0400 Subject: [PATCH 1/3] add 'log' flag to env loaders sometimes we have custom loggers and don't want to see this stuff, so here's a simple flag to turn off the logging when you create a bsuite env --- bsuite/bsuite.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/bsuite/bsuite.py b/bsuite/bsuite.py index 073899ad..7fc173c8 100644 --- a/bsuite/bsuite.py +++ b/bsuite/bsuite.py @@ -100,13 +100,14 @@ def load( return EXPERIMENT_NAME_TO_ENVIRONMENT[experiment_name](**kwargs) -def load_from_id(bsuite_id: str) -> base.Environment: +def load_from_id(bsuite_id: str, log: bool = True) -> base.Environment: """Returns a bsuite environment given a bsuite_id.""" kwargs = sweep.SETTINGS[bsuite_id] experiment_name, _ = unpack_bsuite_id(bsuite_id) env = load(experiment_name, kwargs) - termcolor.cprint( - f'Loaded bsuite_id: {bsuite_id}.', color='white', attrs=['bold']) + if log: + termcolor.cprint( + f'Loaded bsuite_id: {bsuite_id}.', color='white', attrs=['bold']) return env @@ -131,7 +132,8 @@ def load_and_record(bsuite_id: str, def load_and_record_to_sqlite(bsuite_id: str, - db_path: str) -> dm_env.Environment: + db_path: str, + log: bool = True) -> dm_env.Environment: """Returns a bsuite environment that saves results to an SQLite database. The returned environment will automatically save the results required for @@ -159,10 +161,11 @@ def load_and_record_to_sqlite(bsuite_id: str, """ raw_env = load_from_id(bsuite_id) experiment_name, setting_index = unpack_bsuite_id(bsuite_id) - termcolor.cprint( - f'Logging results to SQLite database in {db_path}.', - color='yellow', - attrs=['bold']) + if log: + termcolor.cprint( + f'Logging results to SQLite database in {db_path}.', + color='yellow', + attrs=['bold']) return sqlite_logging.wrap_environment( env=raw_env, db_path=db_path, @@ -173,7 +176,8 @@ def load_and_record_to_sqlite(bsuite_id: str, def load_and_record_to_csv(bsuite_id: str, results_dir: str, - overwrite: bool = False) -> dm_env.Environment: + overwrite: bool = False, + log: bool = True) -> dm_env.Environment: """Returns a bsuite environment that saves results to CSV. To load the results, specify the file path in the provided notebook, or to @@ -196,10 +200,11 @@ def load_and_record_to_csv(bsuite_id: str, A bsuite environment determined by the bsuite_id. """ raw_env = load_from_id(bsuite_id) - termcolor.cprint( - f'Logging results to CSV file for each bsuite_id in {results_dir}.', - color='yellow', - attrs=['bold']) + if log: + termcolor.cprint( + f'Logging results to CSV file for each bsuite_id in {results_dir}.', + color='yellow', + attrs=['bold']) return csv_logging.wrap_environment( env=raw_env, bsuite_id=bsuite_id, @@ -208,9 +213,10 @@ def load_and_record_to_csv(bsuite_id: str, ) -def load_and_record_to_terminal(bsuite_id: str) -> dm_env.Environment: +def load_and_record_to_terminal(bsuite_id: str, log: bool = True) -> dm_env.Environment: """Returns a bsuite environment that logs to terminal.""" raw_env = load_from_id(bsuite_id) - termcolor.cprint( - 'Logging results to terminal.', color='yellow', attrs=['bold']) + if log: + termcolor.cprint( + 'Logging results to terminal.', color='yellow', attrs=['bold']) return terminal_logging.wrap_environment(raw_env) From e2138d4110d30dbadb55b2be8318933ebbd8c980 Mon Sep 17 00:00:00 2001 From: bion howard Date: Mon, 13 Jul 2020 19:44:08 -0400 Subject: [PATCH 2/3] propagate log flag in load_and_record --- bsuite/bsuite.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bsuite/bsuite.py b/bsuite/bsuite.py index 7fc173c8..e28bf24e 100644 --- a/bsuite/bsuite.py +++ b/bsuite/bsuite.py @@ -114,18 +114,19 @@ def load_from_id(bsuite_id: str, log: bool = True) -> base.Environment: def load_and_record(bsuite_id: str, save_path: str, logging_mode: str = 'csv', - overwrite: bool = False) -> dm_env.Environment: + overwrite: bool = False, + log: bool = True) -> dm_env.Environment: """Returns a bsuite environment wrapped with either CSV or SQLite logging.""" if logging_mode == 'csv': - return load_and_record_to_csv(bsuite_id, save_path, overwrite) + return load_and_record_to_csv(bsuite_id, save_path, overwrite, log) elif logging_mode == 'sqlite': if not save_path.endswith('.db'): save_path += '.db' if overwrite: print('WARNING: overwrite option is ignored for SQLite logging.') - return load_and_record_to_sqlite(bsuite_id, save_path) + return load_and_record_to_sqlite(bsuite_id, save_path, log) elif logging_mode == 'terminal': - return load_and_record_to_terminal(bsuite_id) + return load_and_record_to_terminal(bsuite_id, log) else: raise ValueError((f'Unrecognised logging_mode "{logging_mode}". ' 'Must be "csv", "sqlite", or "terminal".')) From b05ffea6e9f36c57faea913f259c74d6bbbece27 Mon Sep 17 00:00:00 2001 From: bion howard Date: Tue, 14 Jul 2020 12:20:21 -0400 Subject: [PATCH 3/3] rename 'log' to 'loading_message' & add docstring --- bsuite/bsuite.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/bsuite/bsuite.py b/bsuite/bsuite.py index e28bf24e..702f005c 100644 --- a/bsuite/bsuite.py +++ b/bsuite/bsuite.py @@ -100,12 +100,12 @@ def load( return EXPERIMENT_NAME_TO_ENVIRONMENT[experiment_name](**kwargs) -def load_from_id(bsuite_id: str, log: bool = True) -> base.Environment: +def load_from_id(bsuite_id: str, loading_message: bool = True) -> base.Environment: """Returns a bsuite environment given a bsuite_id.""" kwargs = sweep.SETTINGS[bsuite_id] experiment_name, _ = unpack_bsuite_id(bsuite_id) env = load(experiment_name, kwargs) - if log: + if loading_message: termcolor.cprint( f'Loaded bsuite_id: {bsuite_id}.', color='white', attrs=['bold']) return env @@ -115,18 +115,18 @@ def load_and_record(bsuite_id: str, save_path: str, logging_mode: str = 'csv', overwrite: bool = False, - log: bool = True) -> dm_env.Environment: + loading_message: bool = True) -> dm_env.Environment: """Returns a bsuite environment wrapped with either CSV or SQLite logging.""" if logging_mode == 'csv': - return load_and_record_to_csv(bsuite_id, save_path, overwrite, log) + return load_and_record_to_csv(bsuite_id, save_path, overwrite, loading_message) elif logging_mode == 'sqlite': if not save_path.endswith('.db'): save_path += '.db' if overwrite: print('WARNING: overwrite option is ignored for SQLite logging.') - return load_and_record_to_sqlite(bsuite_id, save_path, log) + return load_and_record_to_sqlite(bsuite_id, save_path, loading_message) elif logging_mode == 'terminal': - return load_and_record_to_terminal(bsuite_id, log) + return load_and_record_to_terminal(bsuite_id, loading_message) else: raise ValueError((f'Unrecognised logging_mode "{logging_mode}". ' 'Must be "csv", "sqlite", or "terminal".')) @@ -134,7 +134,7 @@ def load_and_record(bsuite_id: str, def load_and_record_to_sqlite(bsuite_id: str, db_path: str, - log: bool = True) -> dm_env.Environment: + loading_message: bool = True) -> dm_env.Environment: """Returns a bsuite environment that saves results to an SQLite database. The returned environment will automatically save the results required for @@ -156,13 +156,14 @@ def load_and_record_to_sqlite(bsuite_id: str, created if it does not already exist. When generating results using multiple different processes, specify the *same* db_path for every bsuite_id. + loading_message: boolean flag (default true) for env loading messages Returns: A bsuite environment determined by the bsuite_id. """ raw_env = load_from_id(bsuite_id) experiment_name, setting_index = unpack_bsuite_id(bsuite_id) - if log: + if loading_message: termcolor.cprint( f'Logging results to SQLite database in {db_path}.', color='yellow',