Coverage for python/lsst/source/injection/bin/make_injection_pipeline.py: 19%
37 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-04 12:35 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-04 12:35 +0000
1# This file is part of source_injection.
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/>.
22from __future__ import annotations
24import logging
25import os
26import time
27from argparse import SUPPRESS, ArgumentParser
29from ..utils.make_injection_pipeline import make_injection_pipeline
30from .source_injection_help_formatter import SourceInjectionHelpFormatter
33def build_argparser():
34 """Build an argument parser for this script."""
35 parser = ArgumentParser(
36 description="""Make an expanded source injection pipeline.
38This script takes a reference pipeline definition file in YAML format and
39prefixes all post-injection dataset type names with the injected prefix. If an
40optional injection pipeline definition YAML file is also provided, the
41injection task will be merged into the pipeline. Unless explicitly excluded,
42all subsets from the reference pipeline which contain the task which generates
43the injection dataset type will also be updated to include the injection task.
44""",
45 formatter_class=SourceInjectionHelpFormatter,
46 epilog="More information is available at https://pipelines.lsst.io.",
47 add_help=False,
48 argument_default=SUPPRESS,
49 )
50 parser.add_argument(
51 "-t",
52 "--dataset-type-name",
53 type=str,
54 help="Name of the datset type being injected into.",
55 required=True,
56 metavar="TEXT",
57 )
58 parser.add_argument(
59 "-r",
60 "--reference-pipeline",
61 type=str,
62 help="Location of a reference pipeline definition YAML file.",
63 required=True,
64 metavar="FILE",
65 )
66 parser.add_argument(
67 "-i",
68 "--injection-pipeline",
69 type=str,
70 help="Location of an injection pipeline definition YAML file stub. If "
71 "this is not explicitly provided, an attempt to infer the injection "
72 "pipeline stub will be made using the injected dataset type name.",
73 metavar="FILE",
74 )
75 parser.add_argument(
76 "-e",
77 "--exclude-subsets",
78 help="Do not update pipeline subsets to include the injection task.",
79 action="store_true",
80 )
81 parser.add_argument(
82 "-x",
83 "--excluded-tasks",
84 type=str,
85 help="Comma-separated set of task labels to exclude from the pipeline.",
86 metavar="task",
87 default="jointcal,gbdesAstrometricFit,fgcmBuildFromIsolatedStars,fgcmFitCycle,fgcmOutputProducts",
88 )
89 parser.add_argument(
90 "-f",
91 "--filename",
92 help="Path to save a modified pipeline definition YAML file.",
93 metavar="FILE",
94 )
95 parser.add_argument(
96 "--overwrite",
97 help="Overwrite the output saved pipeline definition file if it already exists.",
98 action="store_true",
99 )
100 parser.add_argument(
101 "--prefix",
102 type=str,
103 help="Prefix to prepend to each affected post-injection dataset type name.",
104 default="injected_",
105 )
106 parser.add_argument(
107 "--instrument",
108 type=str,
109 help="Add instrument overrides. Must be a fully qualified class name.",
110 metavar="instrument",
111 )
112 parser.add_argument(
113 "-c",
114 "--config",
115 type=str,
116 help="Config override for a task, in the format 'label:key=value'.",
117 action="append",
118 )
119 parser.add_argument(
120 "-h",
121 "--help",
122 action="help",
123 help="Show this help message and exit.",
124 )
125 return parser
128def main():
129 """Use this as the main entry point when calling from the command line."""
130 # Set up logging.
131 tz = time.strftime("%z")
132 logging.basicConfig(
133 format="%(levelname)s %(asctime)s.%(msecs)03d" + tz + " - %(message)s",
134 datefmt="%Y-%m-%dT%H:%M:%S",
135 )
136 logger = logging.getLogger(__name__)
137 logger.setLevel(logging.DEBUG)
139 args = build_argparser().parse_args()
140 if hasattr(args, "filename"):
141 if os.path.exists(args.filename):
142 if not hasattr(args, "overwrite"):
143 raise RuntimeError(f"File {args.filename} already exists; use --overwrite to write anyway.")
144 else:
145 logger.warning("File %s already exists; overwriting.", args.filename)
146 pipeline = make_injection_pipeline(
147 **{k: v for k, v in vars(args).items() if k not in ["filename", "overwrite"]}
148 )
149 pipeline.write_to_uri(args.filename)
150 logger.info(
151 "Modified pipeline definition YAML file saved at %s.",
152 os.path.realpath(args.filename),
153 )
154 else:
155 pipeline = make_injection_pipeline(
156 **{k: v for k, v in vars(args).items() if k not in ["filename", "overwrite"]}
157 )
158 print("\n", pipeline, sep="")