From fb9136eb262f58816ae62c088803b2e6cf7544c5 Mon Sep 17 00:00:00 2001 From: leufen1 <l.leufen@fz-juelich.de> Date: Tue, 22 Sep 2020 11:48:38 +0200 Subject: [PATCH] run environment can be named by kwarg name, this applies for instance for a Workflow --- mlair/run_modules/run_environment.py | 11 ++++++----- mlair/workflows/abstract_workflow.py | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mlair/run_modules/run_environment.py b/mlair/run_modules/run_environment.py index ecb55282..5414b21c 100644 --- a/mlair/run_modules/run_environment.py +++ b/mlair/run_modules/run_environment.py @@ -92,17 +92,18 @@ class RunEnvironment(object): logger = None tracker_list = [] - def __init__(self): + def __init__(self, name=None): """Start time tracking automatically and logs as info.""" if RunEnvironment.data_store is None: RunEnvironment.data_store = DataStoreObject() if RunEnvironment.logger is None: RunEnvironment.logger = Logger() - self.time = TimeTracking() - logging.info(f"{self.__class__.__name__} started") + self._name = name if name is not None else self.__class__.__name__ + self.time = TimeTracking(name=name) + logging.info(f"{self._name} started") # atexit.register(self.__del__) self.data_store.tracker.append({}) - self.tracker_list.extend([{self.__class__.__name__: self.data_store.tracker[-1]}]) + self.tracker_list.extend([{self._name: self.data_store.tracker[-1]}]) def __del__(self): """ @@ -114,7 +115,7 @@ class RunEnvironment(object): """ if not self.del_by_exit: self.time.stop() - logging.info(f"{self.__class__.__name__} finished after {self.time}") + logging.info(f"{self._name} finished after {self.time}") self.del_by_exit = True # copy log file and clear data store only if called as base class and not as super class if self.__class__.__name__ == "RunEnvironment": diff --git a/mlair/workflows/abstract_workflow.py b/mlair/workflows/abstract_workflow.py index d3fe480f..bced90bb 100644 --- a/mlair/workflows/abstract_workflow.py +++ b/mlair/workflows/abstract_workflow.py @@ -15,8 +15,9 @@ class Workflow: method is sufficient. It must be taken care for inter-stage dependencies, this workflow class only handles the execution but not the dependencies (workflow would probably fail in this case).""" - def __init__(self): + def __init__(self, name=None): self._registry = OrderedDict() + self._name = name if name is not None else self.__class__.__name__ def add(self, stage, **kwargs): """Add a new stage with optional kwargs.""" @@ -24,6 +25,6 @@ class Workflow: def run(self): """Run workflow embedded in a run environment and according to the stage's ordering.""" - with RunEnvironment(): + with RunEnvironment(name=self._name): for stage, kwargs in self._registry.items(): stage(**kwargs) -- GitLab