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# This file is part of ctrl_bps. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 <https://www.gnu.org/licenses/>. 

21 

22"""Misc supporting classes and functions for BPS. 

23""" 

24 

25import os 

26import contextlib 

27import logging 

28from enum import Enum 

29 

30 

31_LOG = logging.getLogger(__name__) 

32 

33 

34class WhenToSaveQuantumGraphs(Enum): 

35 """Values for when to save the job quantum graphs. 

36 """ 

37 QGRAPH = 1 # Must be using single_quantum_clustering algorithm. 

38 TRANSFORM = 2 

39 PREPARE = 3 

40 SUBMIT = 4 

41 NEVER = 5 # Always use full QuantumGraph. 

42 

43 

44@contextlib.contextmanager 

45def chdir(path): 

46 """A chdir function that can be used inside a context. 

47 

48 Parameters 

49 ---------- 

50 path : `str` 

51 Path to be made current working directory 

52 """ 

53 cur_dir = os.getcwd() 

54 os.chdir(path) 

55 try: 

56 yield 

57 finally: 

58 os.chdir(cur_dir) 

59 

60 

61def create_job_quantum_graph_filename(job, out_prefix=None): 

62 """Create a filename to be used when storing the QuantumGraph 

63 for a job. 

64 

65 Parameters 

66 ---------- 

67 job : `lsst.ctrl.bps.GenericWorkflowJob` 

68 Job for which the QuantumGraph file is being saved. 

69 out_prefix : `str`, optional 

70 Path prefix for the QuantumGraph filename. If no out_prefix is given, 

71 uses current working directory. 

72 

73 Returns 

74 ------- 

75 full_filename : `str` 

76 The filename for the job's QuantumGraph. 

77 """ 

78 name_parts = [] 

79 if out_prefix is not None: 

80 name_parts.append(out_prefix) 

81 name_parts.append("inputs") 

82 if job.label is not None: 

83 name_parts.append(job.label) 

84 name_parts.append(f"quantum_{job.name}.qgraph") 

85 full_filename = os.path.join("", *name_parts) 

86 return full_filename 

87 

88 

89def save_qg_subgraph(qgraph, out_filename, node_ids=None): 

90 """Save subgraph to file. 

91 

92 Parameters 

93 ---------- 

94 qgraph : `lsst.pipe.base.QuantumGraph` 

95 QuantumGraph to save. 

96 out_filename : `str` 

97 Name of the output file. 

98 node_ids : `list` [`lsst.pipe.base.NodeId`] 

99 NodeIds for the subgraph to save to file. 

100 """ 

101 if not os.path.exists(out_filename): 

102 _LOG.debug("Saving QuantumGraph with %d nodes to %s", len(qgraph), out_filename) 

103 if node_ids is None: 

104 qgraph.saveUri(out_filename) 

105 else: 

106 qgraph.subset(qgraph.getQuantumNodeByNodeId(nid) for nid in node_ids).saveUri(out_filename) 

107 else: 

108 _LOG.debug("Skipping saving QuantumGraph to %s because already exists.", out_filename)