Coverage for mlair/workflows/abstract_workflow.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2023-12-18 17:51 +0000

1"""Abstract workflow.""" 

2 

3__author__ = "Lukas Leufen" 

4__date__ = '2020-06-26' 

5 

6from mlair import RunEnvironment 

7 

8 

9class Workflow: 

10 """Abstract workflow class to handle sequence of stages (run modules). An inheriting class has to first initialise 

11 this mother class and can afterwards add an arbitrary number of stages by using the add method. The execution order 

12 is equal to the ordering of the stages have been added. To run the workflow, finally, a single call of the run 

13 method is sufficient. It must be taken care for inter-stage dependencies, this workflow class only handles the 

14 execution but not the dependencies (workflow would probably fail in this case).""" 

15 

16 def __init__(self, name=None, log_level_stream=None): 

17 self._registry_kwargs = {} 

18 self._registry = [] 

19 self._log_level_stream = log_level_stream 

20 self._name = name if name is not None else self.__class__.__name__ 

21 

22 def add(self, stage, **kwargs): 

23 """Add a new stage with optional kwargs.""" 

24 self._registry.append(stage) 

25 self._registry_kwargs[len(self._registry) - 1] = kwargs 

26 

27 def run(self): 

28 """Run workflow embedded in a run environment and according to the stage's ordering.""" 

29 with RunEnvironment(name=self._name, log_level_stream=self._log_level_stream): 

30 for pos, stage in enumerate(self._registry): 

31 stage(**self._registry_kwargs[pos])