LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
visualizeVisit.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 
3 import numpy as np
4 
5 import lsst.afw.math as afwMath
6 import lsst.afw.image as afwImage
7 
8 from lsst.afw.cameraGeom.utils import makeImageFromCamera
9 from lsst.pipe.base import ArgumentParser
10 from lsst.pex.config import Config, Field
11 from lsst.ctrl.pool.pool import Pool
12 from lsst.ctrl.pool.parallel import BatchPoolTask
13 
14 
15 def makeCameraImage(camera, exposures, binning):
16  """Make and write an image of an entire focal plane
17 
18  Parameters
19  ----------
20  camera : `lsst.afw.cameraGeom.Camera`
21  Camera description.
22  exposures : `dict` mapping detector ID to `lsst.afw.image.Exposure`
23  CCD exposures, binned by `binning`.
24  binning : `int`
25  Binning size that has been applied to images.
26  """
27  class ImageSource(object):
28  """Source of images for makeImageFromCamera"""
29  def __init__(self, exposures):
30  """Constructor
31 
32  Parameters
33  ----------
34  exposures : `dict` mapping detector ID to `lsst.afw.image.Exposure`
35  CCD exposures, already binned.
36  """
37  self.isTrimmed = True
38  self.exposures = exposures
39  self.background = np.nan
40 
41  def getCcdImage(self, detector, imageFactory, binSize):
42  """Provide image of CCD to makeImageFromCamera"""
43  detId = detector.getId()
44  if detId not in self.exposures:
45  dims = detector.getBBox().getDimensions()/binSize
46  image = imageFactory(*[int(xx) for xx in dims])
47  image.set(self.background)
48  else:
49  image = self.exposures[detector.getId()]
50  if hasattr(image, "getMaskedImage"):
51  image = image.getMaskedImage()
52  if hasattr(image, "getMask"):
53  mask = image.getMask()
54  isBad = mask.getArray() & mask.getPlaneBitMask("NO_DATA") > 0
55  image = image.clone()
56  image.getImage().getArray()[isBad] = self.background
57  if hasattr(image, "getImage"):
58  image = image.getImage()
59  return image, detector
60 
61  image = makeImageFromCamera(
62  camera,
63  imageSource=ImageSource(exposures),
64  imageFactory=afwImage.ImageF,
65  binSize=binning
66  )
67  return image
68 
69 
71  binning = Field(dtype=int, default=8, doc="Binning factor to apply")
72 
73 
75  ConfigClass = VisualizeVisitConfig
76  _DefaultName = "visualizeVisit"
77 
78  def __init__(self, *args, **kwargs):
79  BatchPoolTask.__init__(self, *args, **kwargs)
80  self._storedButler = False # Stored butler in the Pool? Doing this once increases efficiency
81 
82  @classmethod
83  def _makeArgumentParser(cls, *args, **kwargs):
84  kwargs.pop("doBatch", False)
85  parser = ArgumentParser(name="visualizeVisit", *args, **kwargs)
86  parser.add_id_argument("--id", datasetType="calexp", level="visit",
87  help="data ID, e.g. --id visit=12345")
88  return parser
89 
90  @classmethod
91  def batchWallTime(cls, time, parsedCmd, numCores):
92  """Return walltime request for batch job
93 
94  Subclasses should override if the walltime should be calculated
95  differently (e.g., addition of some serial time).
96 
97  Parameters
98  ----------
99  time : `float`
100  Requested time per iteration.
101  parsedCmd : `argparse.Namespace`
102  Results of argument parsing.
103  numCores : `int`
104  Number of cores.
105  """
106  numTargets = len(cls.RunnerClass.getTargetList(parsedCmd))
107  return time*numTargets
108 
109  def runDataRef(self, expRef):
110  """Generate an image of the entire visit
111 
112  Only the master node executes this method; it controls the slave nodes,
113  which do the data retrieval.
114 
115  Parameters
116  ----------
117  expRef : `lsst.daf.persistence.ButlerDataRef`
118  Data reference for exposure.
119  """
120  pool = Pool()
121 
122  if not self._storedButler:
123  pool.storeSet(butler=expRef.getButler())
124 
125  with self.logOperation("processing %s" % (expRef.dataId,)):
126  camera = expRef.get("camera")
127  dataIdList = [ccdRef.dataId for ccdRef in expRef.subItems("ccd") if
128  ccdRef.datasetExists("calexp")]
129 
130  exposures = pool.map(self.readImage, dataIdList)
131  exposures = dict(keyValue for keyValue in exposures if keyValue is not None)
132  image = makeCameraImage(camera, exposures, self.config.binning)
133  expRef.put(image, "calexp_camera")
134 
135  def readImage(self, cache, dataId):
136  """Collect original image for visualisation
137 
138  This method runs on the slave nodes.
139 
140  Parameters
141  ----------
142  cache : `lsst.pipe.base.Struct`
143  Process pool cache.
144  dataId : `dict`
145  Data identifier.
146 
147  Returns
148  -------
149  detId : `int`
150  Detector identifier.
151  image : `lsst.afw.image.MaskedImage`
152  Binned image.
153  """
154  exposure = cache.butler.get("calexp", dataId)
155  return (exposure.getDetector().getId(),
156  afwMath.binImage(exposure.getMaskedImage(), self.config.binning))
157 
158  def _getConfigName(self):
159  """It's not worth preserving the configuration"""
160  return None
161 
162  def _getMetadataName(self):
163  """There's no metadata to write out"""
164  return None
def makeImageFromCamera(camera, detectorNameList=None, background=numpy.nan, bufferSize=10, imageSource=FakeImageDataSource(), imageFactory=afwImage.ImageU, binSize=1)
Definition: utils.py:863
def __init__(self, minimum, dataRange, Q)
def batchWallTime(cls, time, parsedCmd, numCores)
def logOperation(self, operation, catch=False, trace=True)
Provide a context manager for logging an operation.
Definition: parallel.py:497
std::shared_ptr< ImageT > binImage(ImageT const &inImage, int const binsize, lsst::afw::math::Property const flags=lsst::afw::math::MEAN)
Definition: binImage.cc:38
def makeCameraImage(camera, exposures, binning)