Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

# This file is part of pipe_base. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

 

"""Module defining connection types to be used within a 

`PipelineTaskConnections` class. 

""" 

 

__all__ = ["InitInput", "InitOutput", "Input", "PrerequisiteInput", 

"Output", "BaseConnection"] 

 

import dataclasses 

import typing 

 

from lsst.daf.butler import DatasetType, DimensionUniverse 

 

 

@dataclasses.dataclass(frozen=True) 

class BaseConnection: 

"""Base class used for declaring PipelineTask connections 

 

Parameters 

---------- 

name : `str` 

The name used to identify the dataset type 

storageClass : `str` 

The storage class used when (un)/persisting the dataset type 

multiple : `bool` 

Indicates if this connection should expect to contain multiple objects 

of the given dataset type 

""" 

name: str 

storageClass: str 

doc: str = "" 

multiple: bool = False 

 

def __get__(self, inst, klass): 

"""Descriptor method 

 

This is a method used to turn a connection into a descriptor. 

When a connection is added to a connection class, it is a class level 

variable. This method makes accessing this connection, on the 

instance of the connection class owning this connection, return a 

result specialized for that instance. In the case of connections 

this specifically means names specified in a config instance will 

be visible instead of the default names for the connection. 

""" 

# If inst is None, this is being accessed by the class and not an 

# instance, return this connection itself 

if inst is None: 

return self 

# If no object cache exists, create one to track the instances this 

# connection has been accessed by 

if not hasattr(inst, '_connectionCache'): 

object.__setattr__(inst, '_connectionCache', {}) 

# Look up an existing cached instance 

idSelf = id(self) 

if idSelf in inst._connectionCache: 

return inst._connectionCache[idSelf] 

# Accumulate the parameters that define this connection 

params = {} 

for field in dataclasses.fields(self): 

params[field.name] = getattr(self, field.name) 

# Get the name override defined by the instance of the connection class 

params['name'] = inst._nameOverrides[self.varName] 

# Return a new instance of this connection specialized with the 

# information provided by the connection class instance 

return inst._connectionCache.setdefault(idSelf, self.__class__(**params)) 

 

 

@dataclasses.dataclass(frozen=True) 

class DimensionedConnection(BaseConnection): 

"""Class used for declaring PipelineTask connections that includes 

dimensions 

 

Parameters 

---------- 

name : `str` 

The name used to identify the dataset type 

storageClass : `str` 

The storage class used when (un)/persisting the dataset type 

multiple : `bool` 

Indicates if this connection should expect to contain multiple objects 

of the given dataset type 

dimensions : iterable of `str` 

The `lsst.daf.butler.Butler` `lsst.daf.butler.Registry` dimensions used 

to identify the dataset type identified by the specified name 

""" 

dimensions: typing.Iterable[str] = () 

 

def makeDatasetType(self, universe: DimensionUniverse): 

"""Construct a true `DatasetType` instance with normalized dimensions. 

Parameters 

---------- 

universe : `lsst.daf.butler.DimensionUniverse` 

Set of all known dimensions to be used to normalize the dimension 

names specified in config. 

Returns 

------- 

datasetType : `DatasetType` 

The `DatasetType` defined by this connection. 

""" 

return DatasetType(self.name, 

universe.extract(self.dimensions), 

self.storageClass) 

 

 

@dataclasses.dataclass(frozen=True) 

class BaseInput(DimensionedConnection): 

"""Class used for declaring PipelineTask input connections 

 

Parameters 

---------- 

name : `str` 

The name used to identify the dataset type 

storageClass : `str` 

The storage class used when (un)/persisting the dataset type 

multiple : `bool` 

Indicates if this connection should expect to contain multiple objects 

of the given dataset type 

dimensions : iterable of `str` 

The `lsst.daf.butler.Butler` `lsst.daf.butler.Registry` dimensions used 

to identify the dataset type identified by the specified name 

deferLoad : `bool` 

Indicates that this dataset type will be loaded as a 

`lsst.daf.butler.DeferredDatasetHandle`. PipelineTasks can use this 

object to load the object at a later time. 

""" 

deferLoad: bool = False 

 

 

@dataclasses.dataclass(frozen=True) 

class Input(BaseInput): 

pass 

 

 

@dataclasses.dataclass(frozen=True) 

class PrerequisiteInput(BaseInput): 

pass 

 

 

@dataclasses.dataclass(frozen=True) 

class Output(DimensionedConnection): 

pass 

 

 

@dataclasses.dataclass(frozen=True) 

class InitInput(BaseConnection): 

pass 

 

 

@dataclasses.dataclass(frozen=True) 

class InitOutput(BaseConnection): 

pass