1 """This is a useful module for debugging measurements.
3 It relies on the presence of a catalog already produced by running
4 measurements in the regular context. You provide the image and
5 catalog on the command-line, a list of source identifiers and the
6 measurement configuration in the config; the module reads the inputs,
7 subsets the catalog to contain only the sources of interest, and measures
8 those. This reduces the frustration of waiting for image processing
9 and the measurements to run on many other sources, greatly increasing
14 from argparse
import ArgumentParser, Namespace
17 from lsst.pipe.base import CmdLineTask, ConfigValueAction, ConfigFileAction, TaskRunner, Struct
21 from lsst.meas.algorithms.measurement
import SourceMeasurementTask
24 sourceId = ListField(dtype=int, default=[], doc=
"List of source identifiers to measure")
25 outputName = Field(dtype=str, default=
"catalog.fits", doc=
"Output name for source catalog")
26 measurement = ConfigurableField(target=SourceMeasurementTask, doc=
"Measurements")
29 """Provide the image and catalog names to the Task
31 We provide a dummy dataRef only to avoid further overrides
36 kwargs[
"image"] = parsedCmd.image
37 kwargs[
"catalog"] = parsedCmd.catalog
38 return [(Struct(dataId=
"<none>"), kwargs)]
41 """A stripped down version of the pipe_base ArgumentParser
43 We don't care about the butler, just the config, and log.
46 super(MeasurementDebuggerArgumentParser, self).
__init__(*args, **kwargs)
47 self.add_argument(
"image", help=
"Name of image to measure")
48 self.add_argument(
"catalog", help=
"Name of catalog to measure")
49 self.add_argument(
"-c",
"--config", nargs=
"*", action=ConfigValueAction,
50 help=
"config override(s), e.g. -c foo=newfoo bar.baz=3", metavar=
"NAME=VALUE")
51 self.add_argument(
"-C",
"--configfile", dest=
"configfile", nargs=
"*", action=ConfigFileAction,
52 help=
"config override file(s)")
53 self.add_argument(
"--doraise", action=
"store_true",
54 help=
"raise an exception on error (else log a message and continue)?")
55 self.add_argument(
"--logdest", help=
"logging destination")
57 def parse_args(self, config, args=None, log=None, override=None):
60 namespace = Namespace()
61 namespace.config = config
62 namespace.clobberConfig =
False
63 namespace.butler =
None
64 namespace.log = log
if log
is not None else pexLog.Log.getDefaultLog()
65 namespace = super(MeasurementDebuggerArgumentParser, self).
parse_args(args=args, namespace=namespace)
66 del namespace.configfile
71 _DefaultName =
"debugger"
72 ConfigClass = MeasurementDebuggerConfig
73 RunnerClass = MeasurementDebuggerRunner
76 super(MeasurementDebuggerTask, self).
__init__(**kwargs)
78 schema = afwTable.SourceTable.makeMinimalSchema()
80 self.makeSubtask(
"measurement", schema=schema)
86 def run(self, dataRef, image, catalog):
91 self.measurement.measure(exp, sources)
93 return Struct(exp=exp, sources=sources)
96 exp = afwImage.ExposureF(image)
97 self.log.info(
"Read %dx%d image" % (exp.getWidth(), exp.getHeight()))
101 sources = afwTable.SourceCatalog.readFits(catalog)
102 self.log.info(
"Read %d sources" % len(sources))
108 new = catalog.addNew()
109 new.setFootprint(ss.getFootprint())
110 for name
in self.schema.getNames():
111 if name
in ss.schema:
112 new.set(name, ss.get(name))
116 """Return a subset of the input catalog
118 The full catalog is used if the 'sourceId' list is empty.
120 Parent sources (in the deblending sense) are also added to the
121 subset so that they can be removed (via replaceWithNoise).
123 if not self.config.sourceId:
126 identifiers = set(self.config.sourceId)
128 while len(identifiers) > 0:
129 ident = identifiers.pop()
130 ss = sources.find(ident)
132 raise RuntimeError(
"Unable to find id=%d in catalog" % ident)
134 parent = ss.getParent()
136 identifiers.add(parent)
137 self.log.info(
"Subset to %d sources" % len(subset))
141 sources.writeFits(self.config.outputName)
142 self.log.info(
"Wrote %s" % self.config.outputName)
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.