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_LOG = logging.getLogger() 

31 

32 

33class WhenToSaveQuantumGraphs(Enum): 

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

35 QGRAPH = 1 # Must be using single_quantum_clustering algorithm. 

36 TRANSFORM = 2 

37 PREPARE = 3 

38 SUBMIT = 4 

39 

40 

41@contextlib.contextmanager 

42def chdir(path): 

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

44 

45 Parameters 

46 ---------- 

47 path : `str` 

48 Path to be made current working directory 

49 """ 

50 cur_dir = os.getcwd() 

51 os.chdir(path) 

52 try: 

53 yield 

54 finally: 

55 os.chdir(cur_dir) 

56 

57 

58def create_job_quantum_graph_filename(job, out_prefix=None): 

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

60 for a job. 

61 

62 Parameters 

63 ---------- 

64 job : `~lsst.ctrl.bps.generic_workflow.GenericWorkflowJob` 

65 Job for which the QuantumGraph file is being saved. 

66 out_prefix : `str`, optional 

67 Path prefix for the QuantumGraph filename. If no 

68 out_prefix is given, uses current working directory. 

69 

70 Returns 

71 ------- 

72 full_filename : `str` 

73 The filename for the job's QuantumGraph. 

74 """ 

75 name_parts = [] 

76 if out_prefix is not None: 

77 name_parts.append(out_prefix) 

78 name_parts.append("inputs") 

79 if job.label is not None: 

80 name_parts.append(job.label) 

81 name_parts.append(f"quantum_{job.name}.pickle") 

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

83 return full_filename 

84 

85 

86def save_qg_subgraph(qgraph, out_filename): 

87 """Save subgraph to file. 

88 

89 Parameters 

90 ---------- 

91 qgraph : `~lsst.pipe.base.graph.QuantumGraph` 

92 QuantumGraph to save. 

93 out_filename : `str` 

94 Name of the output file. 

95 """ 

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

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

98 if len(os.path.dirname(out_filename)) > 0: 

99 os.makedirs(os.path.dirname(out_filename), exist_ok=True) 

100 with open(out_filename, "wb") as fh: 

101 qgraph.save(fh) 

102 else: 

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