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

# This file is part of ctrl_mpexec. 

# 

# 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/>. 

 

"""Few utility methods used by the rest of a package. 

""" 

 

__all__ = ["profile", "printTable", "filterTasks", "subTaskIter"] 

 

# ------------------------------- 

# Imports of standard modules -- 

# ------------------------------- 

import contextlib 

 

# ----------------------------- 

# Imports for other modules -- 

# ----------------------------- 

import lsst.pex.config as pexConfig 

 

# ---------------------------------- 

# Local non-exported definitions -- 

# ---------------------------------- 

 

# ------------------------ 

# Exported definitions -- 

# ------------------------ 

 

 

@contextlib.contextmanager 

def profile(filename, log=None): 

"""!Context manager for profiling with cProfile 

 

@param filename filename to which to write profile (profiling disabled if None or empty) 

@param log log object for logging the profile operations 

 

If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise 

it returns None), which allows additional control over profiling. You can obtain this using 

the "as" clause, e.g.: 

 

with profile(filename) as prof: 

runYourCodeHere() 

 

The output cumulative profile can be printed with a command-line like: 

 

python -c 'import pstats; pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)' 

""" 

if not filename: 

# Nothing to do 

yield 

return 

from cProfile import Profile 

 

prof = Profile() 

if log is not None: 

log.info("Enabling cProfile profiling") 

prof.enable() 

yield prof 

prof.disable() 

prof.dump_stats(filename) 

if log is not None: 

log.info("cProfile stats written to %s" % filename) 

 

 

def printTable(rows, header): 

"""Nice formatting of 2-column table. 

 

Parameters 

---------- 

rows : `list` of `tuple` 

Each item in the list is a 2-tuple containg left and righ column values 

header: `tuple` or `None` 

If `None` then table header are not prined, otherwise it's a 2-tuple 

with column headings. 

""" 

if not rows: 

return 

width = max(len(x[0]) for x in rows) 

if header: 

width = max(width, len(header[0])) 

print(header[0].ljust(width), header[1]) 

print("".ljust(width, "-"), "".ljust(len(header[1]), "-")) 

for col1, col2 in rows: 

print(col1.ljust(width), col2) 

 

 

def filterTasks(pipeline, name): 

"""Finds list of tasks matching given name. 

 

For matching task either task label or task name after last dot should 

be identical to `name`. If task label is non-empty then task name is not 

checked. 

 

Parameters 

---------- 

pipeline : `Pipeline` 

name : str or none 

If empty or None then all tasks are returned 

 

Returns 

------- 

Lsit of `TaskDef` instances. 

""" 

if not name: 

return list(pipeline.toExpandedPipeline()) 

tasks = [] 

for taskDef in pipeline.toExpandedPipeline(): 

if taskDef.label: 

if taskDef.label == name: 

tasks.append(taskDef) 

elif taskDef.taskName.split('.')[-1] == name: 

tasks.append(taskDef) 

return tasks 

 

 

def subTaskIter(config): 

"""Recursively generates subtask names. 

 

Parameters 

---------- 

config : `lsst.pex.config.Config` 

Configuration of the task 

 

Returns 

------- 

Iterator which returns tuples of (configFieldPath, taskName). 

""" 

for fieldName, field in sorted(config.items()): 

if hasattr(field, "value") and hasattr(field, "target"): 

subConfig = field.value 

if isinstance(subConfig, pexConfig.Config): 

try: 

taskName = "%s.%s" % (field.target.__module__, field.target.__name__) 

except Exception: 

taskName = repr(field.target) 

yield fieldName, taskName 

for subFieldName, taskName in subTaskIter(subConfig): 

yield fieldName + '.' + subFieldName, taskName