Coverage for python/lsst/ctrl/mpexec/taskFactory.py: 27%

32 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-07-03 01:28 -0700

1# This file is part of ctrl_mpexec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ["TaskFactory"] 

25 

26import logging 

27from typing import TYPE_CHECKING, Optional 

28 

29from lsst.daf.butler import DatasetType 

30from lsst.pipe.base import TaskFactory as BaseTaskFactory 

31 

32if TYPE_CHECKING: 32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never true

33 from lsst.daf.butler import Butler 

34 from lsst.pex.config import Config 

35 from lsst.pipe.base import PipelineTask 

36 from lsst.pipe.base.configOverrides import ConfigOverrides 

37 

38_LOG = logging.getLogger(__name__) 

39 

40 

41class TaskFactory(BaseTaskFactory): 

42 """Class instantiating PipelineTasks.""" 

43 

44 def makeTask( 

45 self, 

46 taskClass: type[PipelineTask], 

47 label: Optional[str], 

48 config: Optional[Config], 

49 overrides: Optional[ConfigOverrides], 

50 butler: Optional[Butler], 

51 ) -> PipelineTask: 

52 """Create new PipelineTask instance from its class. 

53 

54 Parameters 

55 ---------- 

56 taskClass : type 

57 PipelineTask class. 

58 label : `str` or `None` 

59 The label of the new task; if `None` then use 

60 ``taskClass._DefaultName``. 

61 config : `pex.Config` or None 

62 Configuration object, if ``None`` then use task-defined 

63 configuration class to create new instance. 

64 overrides : `ConfigOverrides` or None 

65 Configuration overrides, this should contain all overrides to be 

66 applied to a default task config, including instrument-specific, 

67 obs-package specific, and possibly command-line overrides. 

68 butler : `lsst.daf.butler.Butler` or None 

69 Butler instance used to obtain initialization inputs for 

70 PipelineTasks. If None, some PipelineTasks will not be usable 

71 

72 Returns 

73 ------- 

74 Instance of a PipelineTask class or None on errors. 

75 

76 Raises 

77 ------ 

78 Any exceptions that are raised by PipelineTask constructor or its 

79 configuration class are propagated back to caller. 

80 """ 

81 

82 # configuration 

83 if config is None: 

84 config = taskClass.ConfigClass() 

85 if overrides: 

86 overrides.applyTo(config) 

87 elif overrides is not None: 

88 _LOG.warning( 

89 "Both config and overrides are specified for task %s, overrides are ignored", 

90 taskClass.__name__, 

91 ) 

92 

93 # if we don't have a butler, try to construct without initInputs; 

94 # let PipelineTasks raise if that's impossible 

95 if butler is None: 

96 initInputs = None 

97 else: 

98 connections = config.connections.ConnectionsClass(config=config) 

99 descriptorMap = {} 

100 for name in connections.initInputs: 

101 attribute = getattr(connections, name) 

102 dsType = DatasetType( 

103 attribute.name, butler.registry.dimensions.extract(set()), attribute.storageClass 

104 ) 

105 descriptorMap[name] = dsType 

106 initInputs = {k: butler.get(v) for k, v in descriptorMap.items()} 

107 

108 # Freeze the config 

109 config.freeze() 

110 

111 # make task instance 

112 task = taskClass(config=config, initInputs=initInputs, name=label) 

113 

114 return task