From da27fc7fa48409213bdd2ef21748a91151b27814 Mon Sep 17 00:00:00 2001 From: pciturri Date: Fri, 5 Jul 2024 16:58:13 +0200 Subject: [PATCH 1/2] fix: correctly showing in logs the end date of an experiment --- floatcsep/experiment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/floatcsep/experiment.py b/floatcsep/experiment.py index 7583113..21d3d76 100644 --- a/floatcsep/experiment.py +++ b/floatcsep/experiment.py @@ -154,7 +154,7 @@ def __init__(self, log.info(f'Setting up experiment {self.name}:') log.info(f'\tStart: {self.start_date}') - log.info(f'\tEnd: {self.start_date}') + log.info(f'\tEnd: {self.end_date}') log.info(f'\tTime windows: {len(self.timewindows)}') log.info(f'\tRegion: {self.region.name if self.region else None}') log.info(f'\tMagnitude range: [{numpy.min(self.magnitudes)},' @@ -957,7 +957,7 @@ def from_yml(cls, config_yml: str, reprdir=None, **kwargs): with open(config_yml, 'r') as yml: # experiment configuration file - _dict = yaml.safe_load(yml) + _dict = yaml.load(yml, NoAliasLoader) _dir_yml = dirname(config_yml) # uses yml path and append if a rel/abs path is given in config. _path = _dict.get('path', '') From 6f5e1d05c69cab41c1d2f3c12f818213f7a66b98 Mon Sep 17 00:00:00 2001 From: pciturri Date: Fri, 5 Jul 2024 16:58:13 +0200 Subject: [PATCH 2/2] refactor: added messages to the logfile that indicate the beginning and the end of a run refactor: the path of the log file is now handled by PathTree feat: logfile can be assigned a name from within config_file feat: logging is now by default False. A flag can be given when calling from CLI and/or within an experiment config file. The config file has priority. fix: correctly showing in logs the end date of an experiment --- floatcsep/cmd/main.py | 6 +++--- floatcsep/experiment.py | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/floatcsep/cmd/main.py b/floatcsep/cmd/main.py index 8e442eb..92720d3 100644 --- a/floatcsep/cmd/main.py +++ b/floatcsep/cmd/main.py @@ -29,7 +29,7 @@ def run(config, **kwargs): exp.make_repr() log.info('Finalized') - log.debug('') + log.debug(f'-------- END OF RUN --------') def plot(config, **kwargs): @@ -43,7 +43,7 @@ def plot(config, **kwargs): exp.plot_forecasts() exp.generate_report() - log.info('Finalized') + log.info('Finalized\n') log.debug('') @@ -77,7 +77,7 @@ def floatcsep(): help='Run a calculation') parser.add_argument('config', type=str, help='Experiment Configuration file') - parser.add_argument('-l', '--log', action='store_false', default=True, + parser.add_argument('-l', '--logging', action='store_true', default=False, help="Don't log experiment") parser.add_argument('-t', '--timestamp', action='store_true', default=False, help="Timestamp results") diff --git a/floatcsep/experiment.py b/floatcsep/experiment.py index 21d3d76..e8d312f 100644 --- a/floatcsep/experiment.py +++ b/floatcsep/experiment.py @@ -135,10 +135,6 @@ def __init__(self, rundir, f'run_{datetime.datetime.utcnow().date().isoformat()}') os.makedirs(os.path.join(workdir, rundir), exist_ok=True) - if kwargs.get(log, True): - logpath = os.path.join(workdir, rundir, 'experiment.log') - log.info(f'Logging at {logpath}') - add_fhandler(logpath) self.name = name if name else 'floatingExp' self.path = PathTree(workdir, rundir) @@ -152,6 +148,14 @@ def __init__(self, self.model_config = models if isinstance(models, str) else None self.test_config = tests if isinstance(tests, str) else None + logger = kwargs.get('logging', True) + if logger: + filename = 'experiment.log' if logger is True else logger + self.path.logger = os.path.join(workdir, rundir, filename) + log.info(f'Logging at {self.path.logger}') + add_fhandler(self.path.logger) + + log.debug(f'-------- BEGIN OF RUN --------') log.info(f'Setting up experiment {self.name}:') log.info(f'\tStart: {self.start_date}') log.info(f'\tEnd: {self.end_date}') @@ -977,5 +981,7 @@ def from_yml(cls, config_yml: str, reprdir=None, **kwargs): _dict['rundir'] = _dict.get('rundir', kwargs.pop('rundir', 'results')) _dict['config_file'] = relpath(config_yml, _dir_yml) - # print(_dict['rundir']) + if 'logging' in _dict: + kwargs.pop('logging') + return cls(**_dict, **kwargs)