31 from .mockObject
import MockObjectTask
32 from .mockObservation
import MockObservationTask
33 from .mockSelect
import MockSelectImagesTask
36 makeSkyMap = lsst.pex.config.ConfigurableField(
37 doc =
"SkyMap builder subtask",
38 target = MakeSkyMapTask
40 mockObject = lsst.pex.config.ConfigurableField(
41 doc =
"Subtask that generates and draws the objects/sources in the mock images",
42 target = MockObjectTask
44 mockObservation = lsst.pex.config.ConfigurableField(
45 doc =
"Subtask that generates the Wcs, Psf, Calib, etc. of mock images",
46 target = MockObservationTask
48 coaddName = lsst.pex.config.Field(
49 doc =
"Coadd name used as a prefix for other datasets",
54 nObservations = lsst.pex.config.Field(
55 doc =
"Number of mock observations to generate.",
60 edgeBuffer = lsst.pex.config.Field(
61 doc = (
"Number of pixels by which to grow object bounding boxes when determining whether they land "
62 " completely on a generated image"),
68 def setupSkyMapPatches(self, nPatches=2, patchSize=400, pixelScale = 0.2*lsst.afw.geom.arcseconds):
70 Set the nested [discrete] skymap config parameters such that the full tract
71 has nPatches x nPatches patches of the given size and pixel scale.
73 self.makeSkyMap.skyMap[
'discrete'].patchInnerDimensions = [patchSize, patchSize]
74 self.makeSkyMap.skyMap[
'discrete'].pixelScale = pixelScale.asArcseconds()
77 radius = (0.5 * nPatches - 0.49) * patchSize * pixelScale.asDegrees()
78 self.makeSkyMap.skyMap[
'discrete'].radiusList = [radius]
81 self.makeSkyMap.skyMap.name =
'discrete'
82 self.makeSkyMap.skyMap[
'discrete'].raList = [90.0]
83 self.makeSkyMap.skyMap[
'discrete'].decList = [0.0]
84 self.makeSkyMap.skyMap[
'discrete'].patchBorder = 10
85 self.makeSkyMap.skyMap[
'discrete'].projection =
"TAN"
86 self.makeSkyMap.skyMap[
'discrete'].tractOverlap = 0.0
90 """MockCoaddTask is a driver task for creating mock coadds. As opposed to more realistic
91 simulations, MockCoadd generates and uses extremely simple "toy" data that can be used to more
92 rigorously test the behavior of high-level task code because the expected results are
93 more easily predicted. In particular, calexps are generated directly from the truth catalog,
94 and contain only zero-noise stars that are created using the same Psf, Calib, and Wcs that will
95 be attached to the mock calexp.
97 In addition to creating the mock calexps and truth catalogs, MockCoadd also contains driver
98 code to run the MakeSkyMap, MakeCoaddTempExp, and AssembleCoadd tasks on the mock calexps,
99 and code to directly create a mock coadd image using CoaddPsf, which can be compared to the
100 output of the regular coadd tasks to check that the coadd code and CoaddPsf are consistent.
102 Note that aside from MakeSkyMapTask, the coadd tasks are *not* subtasks of MockCoaddTasks,
103 and their configs are not part of MockCoaddConfig; these are created locally within
104 MockCoaddTask methods when needed, as not all coadd task config options are appropriate
105 for the mock data generated by MockCoadd.
108 ConfigClass = MockCoaddConfig
110 _DefaultName =
"MockCoadd"
113 """Construct a MockCoaddTask and the subtasks used for generating skymaps, objects,
114 and observations (i.e. calexp parameters).
116 lsst.pipe.base.CmdLineTask.__init__(self, **kwds)
117 self.makeSubtask(
"makeSkyMap")
118 self.makeSubtask(
"mockObject")
119 self.makeSubtask(
"mockObservation")
121 self.
objectIdKey = self.schema.addField(
"objectId", type=long, doc=
"foreign key to truth catalog")
123 doc=
"foreign key to observation catalog")
125 "centroidInBBox", type=
"Flag",
126 doc=
"set if this source's center position is inside the generated image's bbox"
129 "partialOverlap", type=
"Flag",
130 doc=
"set if this source was not completely inside the generated image"
134 """Build the skymap for the mock dataset."""
135 return self.makeSkyMap.run(butler.dataRef(self.config.coaddName +
"Coadd_skyMap")).skyMap
138 """Create and save (if butler is not None) a truth catalog containing all the mock objects.
140 Must be run after buildSkyMap.
142 Most of the work is delegated to the mockObject subtask.
145 skyMap = butler.get(self.config.coaddName +
"Coadd_skyMap")
146 catalog = self.mockObject.run(tractInfo=skyMap[tract])
147 if butler
is not None:
148 butler.put(catalog,
"truth", tract=tract)
152 """Create and save (if butler is not None) an ExposureCatalog of simulated observations,
153 containing the Psfs, Wcss, Calibs, etc. of the calexps to be simulated.
155 Must be run after buildSkyMap.
157 Most of the work is delegated to the mockObservation subtask.
160 skyMap = butler.get(self.config.coaddName +
"Coadd_skyMap")
162 camera = butler.get(
"camera")
163 catalog = self.mockObservation.run(butler=butler,
164 n=self.config.nObservations, camera=camera,
165 tractInfo=skyMap[tract])
166 if butler
is not None:
167 butler.put(catalog,
"observations", tract=tract)
171 """Use the truth catalog and observation catalog to create and save (if butler is not None)
172 mock calexps and an ExposureCatalog ('simsrc') that contains information about which objects
173 appear partially or fully in each exposure.
175 Must be run after buildTruthCatalog and buildObservationCatalog.
177 if obsCatalog
is None:
178 obsCatalog = butler.get(
"observations", tract=tract)
179 if truthCatalog
is None:
180 truthCatalog = butler.get(
"truth", tract=tract)
181 ccdKey = obsCatalog.getSchema().find(
"ccd").key
182 visitKey = obsCatalog.getSchema().find(
"visit").key
184 for obsRecord
in obsCatalog:
185 ccd = obsRecord.getI(ccdKey)
186 visit = obsRecord.getI(visitKey)
187 self.log.info(
"Generating image for visit={visit}, ccd={ccd}".
format(ccd=ccd, visit=visit))
188 exposure = lsst.afw.image.ExposureF(obsRecord.getBBox())
189 exposure.setCalib(obsRecord.getCalib())
190 exposure.setWcs(obsRecord.getWcs())
191 exposure.setPsf(obsRecord.getPsf())
192 for truthRecord
in truthCatalog:
193 status = self.mockObject.drawSource(truthRecord, exposure, buffer=self.config.edgeBuffer)
195 simSrcRecord = simSrcCatalog.addNew()
196 simSrcRecord.setCoord(truthRecord.getCoord())
197 simSrcRecord.setL(self.
objectIdKey, truthRecord.getId())
199 simSrcRecord.setFlag(self.
centroidInBBoxKey, obsRecord.contains(truthRecord.getCoord()))
201 self.log.info(
" added object {id}".
format(id=truthRecord.getId()))
202 exposure.getMaskedImage().getVariance().set(1.0)
203 if butler
is not None:
204 butler.put(exposure,
"calexp", ccd=ccd, visit=visit)
205 if butler
is not None:
206 butler.put(simSrcCatalog,
"simsrc", tract=tract)
210 """Convenience function that calls buildSkyMap, buildObservationCatalog, buildTruthCatalog,
211 and buildInputImages.
216 simSrcCatalog = self.
buildInputImages(butler, obsCatalog=observations, truthCatalog=truth)
219 """Helper function to create a Coadd task with configuration appropriate for the simulations.
221 MockCoaddTask does not include MakeCoaddTempExpTask or AssembleCoaddTask as subtasks, because
222 we want explicit control over their configs, rather than leaving this up to the user.
223 However, we have to install our own SelectImages task for both of these, so it made sense
224 to have a single method that would create one of these two tasks, set the config values we
225 want, and install the custom SelectImagesTask.
227 config = cls.ConfigClass()
228 config.coaddName = self.config.coaddName
229 config.select.retarget(MockSelectImagesTask)
230 if cls == MakeCoaddTempExpTask:
231 config.bgSubtracted =
True
232 config.doPsfMatch =
False
233 elif cls == AssembleCoaddTask:
234 config.doMatchBackgrounds =
False
238 """Generator that iterates over the patches in a tract, yielding dataRefs.
240 nPatchX, nPatchY = tractInfo.getNumPatches()
241 for iPatchX
in range(nPatchX):
242 for iPatchY
in range(nPatchY):
243 patchRef = butler.dataRef(self.config.coaddName +
"Coadd",
244 tract=tractInfo.getId(), patch=
"%d,%d" % (iPatchX,iPatchY),
249 """Run the coadd tasks (MakeCoaddTempExp and AssembleCoadd) on the mock data.
251 Must be run after buildInputImages.
254 skyMap = butler.get(self.config.coaddName +
"Coadd_skyMap")
255 tractInfo = skyMap[tract]
256 makeCoaddTempExpTask = self.
makeCoaddTask(MakeCoaddTempExpTask)
259 makeCoaddTempExpTask.run(patchRef)
261 assembleCoaddTask.run(patchRef)
264 """Directly create a simulation of the coadd, using the CoaddPsf of the coadd exposure
265 and the truth catalog.
267 Must be run after buildCoadd.
269 if truthCatalog
is None:
270 truthCatalog = butler.get(
"truth", tract=tract)
272 skyMap = butler.get(self.config.coaddName +
"Coadd_skyMap")
273 tractInfo = skyMap[tract]
274 tractWcs = tractInfo.getWcs()
276 exposure = patchRef.get(self.config.coaddName +
"Coadd")
277 exposure.getMaskedImage().getImage().set(0.0)
279 exposure.getInfo().getCoaddInputs().ccds, exposure.getWcs()
281 exposure.setPsf(coaddPsf)
282 for truthRecord
in truthCatalog:
283 self.mockObject.drawSource(truthRecord, exposure, buffer=0)
284 patchRef.put(exposure, self.config.coaddName +
"Coadd_mock")
287 """Convenience function to create and run MockCoaddTask with default settings.
289 from .simpleMapper
import makeDataRepo
292 task.buildAllInputs(butler)
293 task.buildCoadd(butler)
294 task.buildMockCoadd(butler)
static Schema makeMinimalSchema()
Return a minimal schema for Simple tables and records.
def buildObservationCatalog
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
CoaddPsf is the Psf derived to be used for non-PSF-matched Coadd images.