Skip to content

Commit

Permalink
Remove imports of ds9
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenam committed Mar 6, 2019
1 parent bd092f8 commit 84fd03c
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 72 deletions.
19 changes: 10 additions & 9 deletions examples/measurePsfTask.py
Expand Up @@ -29,7 +29,7 @@
import lsst.utils
import lsst.afw.table as afwTable
import lsst.afw.image as afwImage
import lsst.afw.display.ds9 as ds9
import lsst.afw.display as afwDisplay
import lsst.meas.algorithms as measAlg
from lsst.meas.algorithms.detection import SourceDetectionTask
from lsst.meas.base import SingleFrameMeasurementTask
Expand Down Expand Up @@ -101,26 +101,27 @@ def run(display=False):
result = measurePsfTask.run(exposure, sources)
print("psf=", result.psf)

if display: # display on ds9 (see also --debug argparse option)
if display:
frame = 1
ds9.mtv(exposure, frame=frame)
disp = afwDisplay.Display(frame=frame) # see also --debug argparse option
disp.mtv(exposure)

with ds9.Buffering():
with disp.Buffering():
for s in sources:
xy = s.getCentroid()
ds9.dot('+', *xy, frame=frame)
disp.dot('+', *xy)
if s.get("calib_psf_candidate"):
ds9.dot('x', *xy, ctype=ds9.YELLOW, frame=frame)
disp.dot('x', *xy, ctype=afwDisplay.YELLOW)
if s.get("calib_psf_used"):
ds9.dot('o', *xy, size=4, ctype=ds9.RED, frame=frame)
disp.dot('o', *xy, size=4, ctype=afwDisplay.RED)


if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Demonstrate the use of MeasurePsfTask")

parser.add_argument('--debug', '-d', action="store_true", help="Load debug.py?", default=False)
parser.add_argument('--ds9', action="store_true", help="Display sources on ds9", default=False)
parser.add_argument('--doDisplay', action="store_true", help="Display sources", default=False)

args = parser.parse_args()

Expand All @@ -130,4 +131,4 @@ def run(display=False):
except ImportError as e:
print(e, file=sys.stderr)

run(display=args.ds9)
run(display=args.doDisplay)
9 changes: 5 additions & 4 deletions python/lsst/pipe/tasks/exampleCmdLineTask.py
Expand Up @@ -19,7 +19,7 @@
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
from lsst.afw.display.ds9 import mtv
import lsst.afw.display as afwDisplay
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
from .exampleStatsTasks import ExampleSigmaClippedStatsTask
Expand Down Expand Up @@ -69,7 +69,7 @@ class ExampleCmdLineTask(pipeBase.CmdLineTask):
and \ref pipeTasks_writeCmdLineTask.
The task reads in a "calexp" (a calibrated science \ref lsst::afw::image::Exposure "exposure"),
computes statistics on the image plane, and logs and returns the statistics.
In addition, if debugging is enabled, it displays the image in ds9.
In addition, if debugging is enabled, it displays the image in current display backend.
The image statistics are computed using a subtask, in order to show how to call subtasks and how to
\ref pipeBase_argumentParser_retargetSubtasks "retarget" (replace) them with variant subtasks.
Expand All @@ -85,7 +85,7 @@ class ExampleCmdLineTask(pipeBase.CmdLineTask):
This task supports the following debug variables:
<dl>
<dt>`display`
<dd>If True then display the exposure in ds9
<dd>If True then display the exposure in current display backend
</dl>
To enable debugging, see \ref baseDebug.
Expand Down Expand Up @@ -137,7 +137,8 @@ def runDataRef(self, dataRef):
display = lsstDebug.Info(__name__).display
if display:
frame = 1
mtv(rawExp, frame=frame, title="exposure")
disp = afwDisplay.Display(frame=frame)
disp.mtv(rawExp, title="exposure")

# return the pipe_base Struct that is returned by self.stats.run
return self.stats.run(maskedImage)
Expand Down
11 changes: 6 additions & 5 deletions python/lsst/pipe/tasks/imageDifference.py
Expand Up @@ -42,6 +42,7 @@
DecorrelateALKernelSpatialTask, subtractAlgorithmRegistry
import lsst.ip.diffim.diffimTools as diffimTools
import lsst.ip.diffim.utils as diUtils
import lsst.afw.display as afwDisplay

FwhmPerSigma = 2 * math.sqrt(2 * math.log(2))
IqrToSigma = 0.741
Expand Down Expand Up @@ -762,16 +763,16 @@ def runDebug(self, exposure, subtractRes, selectSources, kernelSources, diaSourc
showDipoles = lsstDebug.Info(__name__).showDipoles
maskTransparency = lsstDebug.Info(__name__).maskTransparency
if display:
import lsst.afw.display.ds9 as ds9
disp = afwDisplay.getDisplay(frame=lsstDebug.frame)
if not maskTransparency:
maskTransparency = 0
ds9.setMaskTransparency(maskTransparency)
disp.setMaskTransparency(maskTransparency)

if display and showSubtracted:
ds9.mtv(subtractRes.subtractedExposure, frame=lsstDebug.frame, title="Subtracted image")
disp.mtv(subtractRes.subtractedExposure, title="Subtracted image")
mi = subtractRes.subtractedExposure.getMaskedImage()
x0, y0 = mi.getX0(), mi.getY0()
with ds9.Buffering():
with disp.Buffering():
for s in diaSources:
x, y = s.getX() - x0, s.getY() - y0
ctype = "red" if s.get("flags.negative") else "yellow"
Expand All @@ -783,7 +784,7 @@ def runDebug(self, exposure, subtractRes, selectSources, kernelSources, diaSourc
ptype = "+"
else:
ptype = "o"
ds9.dot(ptype, x, y, size=4, frame=lsstDebug.frame, ctype=ctype)
disp.dot(ptype, x, y, size=4, ctype=ctype)
lsstDebug.frame += 1

if display and showPixelResiduals and selectSources:
Expand Down
48 changes: 26 additions & 22 deletions python/lsst/pipe/tasks/measurePsf.py
Expand Up @@ -19,8 +19,8 @@
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
import lsst.afw.display as afwDisplay
import lsst.afw.math as afwMath
import lsst.afw.display.ds9 as ds9
import lsst.meas.algorithms as measAlg
import lsst.meas.algorithms.utils as maUtils
import lsst.pex.config as pexConfig
Expand Down Expand Up @@ -121,7 +121,7 @@ class MeasurePsfTask(pipeBase.Task):
This code is in @link measurePsfTask.py@endlink in the examples directory, and can be run as @em e.g.
@code
examples/measurePsfTask.py --ds9
examples/measurePsfTask.py --doDisplay
@endcode
@dontinclude measurePsfTask.py
Expand Down Expand Up @@ -171,7 +171,7 @@ class MeasurePsfTask(pipeBase.Task):
@skip psf
@until cellSet
If you specified @c --ds9 you can see the PSF candidates:
If you specified @c --doDisplay you can see the PSF candidates:
@skip display
@until RED
Expand Down Expand Up @@ -287,10 +287,11 @@ def run(self, exposure, sources, expId=0, matches=None):
self.log.info("Sending %d candidates to PSF determiner" % len(psfDeterminerList))

if display:
frame = display
frame = 1
if displayExposure:
ds9.mtv(exposure, frame=frame, title="psf determination")

disp = afwDisplay.Display(frame=frame)
disp.mtv(exposure, title="psf determination")
frame += 1
#
# Determine PSF
#
Expand All @@ -304,11 +305,12 @@ def run(self, exposure, sources, expId=0, matches=None):
if display:
frame = display
if displayExposure:
disp = afwDisplay.Display(frame=frame)
showPsfSpatialCells(exposure, cellSet, showBadCandidates, frame=frame)
frame += 1

if displayPsfCandidates: # Show a mosaic of PSF candidates
plotPsfCandidates(cellSet, showBadCandidates, frame)
plotPsfCandidates(cellSet, showBadCandidates=showBadCandidates, frame=frame)
frame += 1

if displayResiduals:
Expand All @@ -317,8 +319,9 @@ def run(self, exposure, sources, expId=0, matches=None):
normalizeResiduals=normalizeResiduals,
frame=frame)
if displayPsfMosaic:
maUtils.showPsfMosaic(exposure, psf, frame=frame, showFwhm=True)
ds9.scale(0, 1, "linear", frame=frame)
disp = afwDisplay.Display(frame=frame)
maUtils.showPsfMosaic(exposure, psf, display=disp, showFwhm=True)
disp.scale("linear", 0, 1)
frame += 1

return pipeBase.Struct(
Expand All @@ -337,20 +340,19 @@ def usesMatches(self):


def showPsfSpatialCells(exposure, cellSet, showBadCandidates, frame=1):
disp = afwDisplay.Display(frame=frame)
maUtils.showPsfSpatialCells(exposure, cellSet,
symb="o", ctype=ds9.CYAN, ctypeUnused=ds9.YELLOW,
size=4, frame=frame)
symb="o", ctype=afwDisplay.CYAN, ctypeUnused=afwDisplay.YELLOW,
size=4, display=disp)
for cell in cellSet.getCellList():
for cand in cell.begin(not showBadCandidates): # maybe include bad candidates
status = cand.getStatus()
ds9.dot('+', *cand.getSource().getCentroid(), frame=frame,
ctype=ds9.GREEN if status == afwMath.SpatialCellCandidate.GOOD else
ds9.YELLOW if status == afwMath.SpatialCellCandidate.UNKNOWN else ds9.RED)
disp.dot('+', *cand.getSource().getCentroid(),
ctype=afwDisplay.GREEN if status == afwMath.SpatialCellCandidate.GOOD else
afwDisplay.YELLOW if status == afwMath.SpatialCellCandidate.UNKNOWN else afwDisplay.RED)


def plotPsfCandidates(cellSet, showBadCandidates=False, frame=1):
import lsst.afw.display.utils as displayUtils

stamps = []
for cell in cellSet.getCellList():
for cand in cell.begin(not showBadCandidates): # maybe include bad candidates
Expand All @@ -369,7 +371,8 @@ def plotPsfCandidates(cellSet, showBadCandidates=False, frame=1):
except Exception:
continue

mos = displayUtils.Mosaic()
mos = afwDisplay.utils.Mosaic()
disp = afwDisplay.Display(frame=frame)
for im, label, status in stamps:
im = type(im)(im, True)
try:
Expand All @@ -378,22 +381,23 @@ def plotPsfCandidates(cellSet, showBadCandidates=False, frame=1):
pass

mos.append(im, label,
ds9.GREEN if status == afwMath.SpatialCellCandidate.GOOD else
ds9.YELLOW if status == afwMath.SpatialCellCandidate.UNKNOWN else ds9.RED)
afwDisplay.GREEN if status == afwMath.SpatialCellCandidate.GOOD else
afwDisplay.YELLOW if status == afwMath.SpatialCellCandidate.UNKNOWN else afwDisplay.RED)

if mos.images:
mos.makeMosaic(frame=frame, title="Psf Candidates")
disp.mtv(mos.makeMosaic(), title="Psf Candidates")


def plotResiduals(exposure, cellSet, showBadCandidates=False, normalizeResiduals=True, frame=2):
psf = exposure.getPsf()
disp = afwDisplay.Display(frame=frame)
while True:
try:
maUtils.showPsfCandidates(exposure, cellSet, psf=psf, frame=frame,
maUtils.showPsfCandidates(exposure, cellSet, psf=psf, display=disp,
normalize=normalizeResiduals,
showBadCandidates=showBadCandidates)
frame += 1
maUtils.showPsfCandidates(exposure, cellSet, psf=psf, frame=frame,
maUtils.showPsfCandidates(exposure, cellSet, psf=psf, display=disp,
normalize=normalizeResiduals,
showBadCandidates=showBadCandidates,
variance=True)
Expand Down
11 changes: 6 additions & 5 deletions python/lsst/pipe/tasks/mocks/visualization.py
Expand Up @@ -71,21 +71,22 @@ def plotTruth(catalog, wcs):


def displayImages(root):
"""Display coadd images with DS9 in different frames, with the bounding boxes of the
"""Display coadd images in different frames, with the bounding boxes of the
observations that went into them overlayed.
"""
import lsst.afw.display.ds9
import lsst.afw.display.utils
import lsst.afw.display as afwDisplay
afwDisplay.setDefaultMaskTransparency(75)

butler = lsst.daf.persistence.Butler(root=root)
skyMap = butler.get("deepCoadd_skyMap")
tractInfo = skyMap[0]
task = lsst.pipe.tasks.mocks.MockCoaddTask()
coadds = [patchRef.get("deepCoadd", immediate=True)
for patchRef in task.iterPatchRefs(butler, tractInfo)]
for n, coadd in enumerate(coadds):
lsst.afw.display.ds9.mtv(coadd, frame=n+1)
afwDisplay.Display(frame=n+1).mtv(coadd, title="displayImages: coadd")
for n, coadd in enumerate(coadds):
lsst.afw.display.utils.drawCoaddInputs(coadd, frame=n+1)
afwDisplay.utils.drawCoaddInputs(coadd, frame=n+1)
return butler


Expand Down
13 changes: 7 additions & 6 deletions python/lsst/pipe/tasks/photoCal.py
Expand Up @@ -31,7 +31,7 @@
from lsst.afw.image import abMagFromFlux, abMagErrFromFluxErr, Calib
import lsst.afw.table as afwTable
from lsst.meas.astrom import DirectMatchTask, DirectMatchConfigWithoutLoader
import lsst.afw.display.ds9 as ds9
import lsst.afw.display as afwDisplay
from lsst.meas.algorithms import getRefFluxField, ReserveSourcesTask
from .colorterms import ColortermLibrary

Expand Down Expand Up @@ -172,7 +172,7 @@ class PhotoCalTask(pipeBase.Task):
<DT> @c display
<DD> If True enable other debug outputs
<DT> @c displaySources
<DD> If True, display the exposure on ds9's frame 1 and overlay the source catalogue.
<DD> If True, display the exposure on the display's frame 1 and overlay the source catalogue.
<DL>
<DT> red o
<DD> Reserved objects
Expand Down Expand Up @@ -497,12 +497,13 @@ def displaySources(self, exposure, matches, reserved, frame=1):
frame : `int`
Frame number for display.
"""
ds9.mtv(exposure, frame=frame, title="photocal")
with ds9.Buffering():
disp = afwDisplay.getDisplay(frame=frame)
disp.mtv(exposure, title="photocal")
with disp.Buffering():
for mm, rr in zip(matches, reserved):
x, y = mm.second.getCentroid()
ctype = ds9.RED if rr else ds9.GREEN
ds9.dot("o", x, y, size=4, frame=frame, ctype=ctype)
ctype = afwDisplay.RED if rr else afwDisplay.GREEN
disp.dot("o", x, y, size=4, ctype=ctype)

def getZeroPoint(self, src, ref, srcErr=None, zp0=None):
"""!Flux calibration code, returning (ZeroPoint, Distribution Width, Number of stars)
Expand Down
23 changes: 10 additions & 13 deletions python/lsst/pipe/tasks/repair.py
Expand Up @@ -25,7 +25,7 @@
import lsst.meas.algorithms as measAlg
import lsst.pipe.base as pipeBase
from lsstDebug import getDebugFrame
from lsst.afw.display import getDisplay
import lsst.afw.display as afwDisplay
from lsst.pipe.tasks.interpImage import InterpImageTask


Expand Down Expand Up @@ -115,7 +115,7 @@ class RepairTask(pipeBase.Task):
<DD> display image after cosmic ray and defect correction
</DL>
<DT> @c displayCR
<DD> If True, display the exposure on ds9's frame 1 and overlay bounding boxes around detects CRs.
<DD> If True, display the exposure on display's frame 1 and overlay bounding boxes around detects CRs.
</DL>
@section pipe_tasks_repair_Example A complete example of using RepairTask
Expand Down Expand Up @@ -203,7 +203,7 @@ def run(self, exposure, defects=None, keepCRs=None):

frame = getDebugFrame(self._display, "repair.before")
if frame:
getDisplay(frame).mtv(exposure)
afwDisplay.Display(frame).mtv(exposure)

if defects is not None and self.config.doInterpolate:
self.interp.run(exposure, defects=defects)
Expand All @@ -213,7 +213,7 @@ def run(self, exposure, defects=None, keepCRs=None):

frame = getDebugFrame(self._display, "repair.after")
if frame:
getDisplay(frame).mtv(exposure)
afwDisplay.Display(frame).mtv(exposure)

def cosmicRay(self, exposure, keepCRs=None):
"""Mask cosmic rays
Expand Down Expand Up @@ -269,8 +269,7 @@ def cosmicRay(self, exposure, keepCRs=None):

except Exception:
if display:
import lsst.afw.display.ds9 as ds9
ds9.mtv(exposure0, title="Failed CR")
afwDisplay.Display().mtv(exposure0, title="Failed CR")
raise

num = 0
Expand All @@ -281,14 +280,12 @@ def cosmicRay(self, exposure, keepCRs=None):
num = len(crs)

if display and displayCR:
import lsst.afw.display.ds9 as ds9
import lsst.afw.display.utils as displayUtils
disp = afwDisplay.Display()
disp.incrDefaultFrame()
disp.mtv(exposure0, title="Post-CR")

ds9.incrDefaultFrame()
ds9.mtv(exposure0, title="Post-CR")

with ds9.Buffering():
with disp.Buffering():
for cr in crs:
displayUtils.drawBBox(cr.getBBox(), borderWidth=0.55)
afwDisplay.utils.drawBBox(cr.getBBox(), borderWidth=0.55)

self.log.info("Identified %s cosmic rays." % (num,))

0 comments on commit 84fd03c

Please sign in to comment.