22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"displayAstrometry",
"plotAstrometry"]
26 from builtins
import zip
36 def displayAstrometry(refCat=None, sourceCat=None, distortedCentroidKey=None, bbox=None, exposure=None,
37 matches=None, frame=1, title="", pause=True):
38 """Show an astrometry debug image 40 - reference objects in refCat are shown as red X 41 - sources in sourceCat are shown as green + 42 - distorted sources in sourceCat (position given by distortedCentroidKey) are shown as green o 43 - matches are shown as a yellow circle around the source and a yellow line 44 connecting the reference object and source 45 - if both exposure and bbox are None, no image is displayed 47 @param[in] refCat reference object catalog; must have fields "centroid_x" and "centroid_y" 48 @param[in] sourceCat source catalog; must have field "slot_Centroid_x" and "slot_Centroid_y" 49 @param[in] distortedCentroidKey key for sourceCat with field to use for distorted positions, or None 50 @param[in] exposure exposure to display, or None for a blank exposure 51 @param[in] bbox bounding box of exposure; used if exposure is None for a blank image 52 @param[in] matches a list of lsst.afw.table.ReferenceMatch, or None 53 @param[in] frame frame number for ds9 display 54 @param[in] title title for ds9 display 55 @param[in] pause pause for inspection of display? This is done by dropping into pdb. 57 disp = afwDisplay.getDisplay(frame)
59 if exposure
is not None:
60 disp.mtv(exposure, title=title)
61 elif bbox
is not None:
62 disp.mtv(exposure=ExposureF(bbox), title=title)
64 with disp.Buffering():
65 if refCat
is not None:
66 refCentroidKey =
Point2DKey(refCat.schema[
"centroid"])
68 rx, ry = refObj.get(refCentroidKey)
69 disp.dot(
"x", rx, ry, size=10, ctype=afwDisplay.RED)
71 if sourceCat
is not None:
72 sourceCentroidKey =
Point2DKey(sourceCat.schema[
"slot_Centroid"])
73 for source
in sourceCat:
74 sx, sy = source.get(sourceCentroidKey)
75 disp.dot(
"+", sx, sy, size=10, ctype=afwDisplay.GREEN)
76 if distortedCentroidKey
is not None:
77 dx, dy = source.get(distortedCentroidKey)
78 disp.dot(
"o", dx, dy, size=10, ctype=afwDisplay.GREEN)
79 disp.line([(sx, sy), (dx, dy)], ctype=afwDisplay.GREEN)
81 if matches
is not None:
82 refCentroidKey =
Point2DKey(matches[0].first.schema[
"centroid"])
83 sourceCentroidKey =
Point2DKey(matches[0].second.schema[
"slot_Centroid"])
84 radArr = np.ndarray(len(matches))
86 for i, m
in enumerate(matches):
87 refCentroid = m.first.get(refCentroidKey)
88 sourceCentroid = m.second.get(sourceCentroidKey)
89 radArr[i] = math.hypot(*(refCentroid - sourceCentroid))
90 sx, sy = sourceCentroid
91 disp.dot(
"o", sx, sy, size=10, ctype=afwDisplay.YELLOW)
92 disp.line([refCentroid, sourceCentroid], ctype=afwDisplay.YELLOW)
94 print(
"<match radius> = %.4g +- %.4g [%d matches]" %
95 (radArr.mean(), radArr.std(), len(matches)))
98 print(
"Dropping into debugger to allow inspection of display. Type 'continue' when done.")
113 """Plot reference objects, sources and matches 116 - reference objects in refCat are shown as red X 117 - sources in sourceCat are shown as green + 118 - matches are shown as a yellow circle around the source and a line 119 connecting the reference object to the source 121 @param[in] matches list of matches 122 @param[in] refCat reference object catalog, or None to not plot reference objects 123 @param[in] sourceCat source catalog, or None to not plot sources 124 @param[in] refMarker pyplot marker for reference objects 125 @param[in] refColor pyplot color for reference objects 126 @param[in] sourceMarker pyplot marker for sources 127 @param[in] sourceColor pyplot color for sources 128 @param[in] matchColor color for matches; can be a constant 129 or a function taking one match and returning a string 132 import matplotlib.pyplot
as plt
133 refSchema = matches[0][0].schema
134 refCentroidKey =
Point2DKey(refSchema[
"centroid"])
135 srcSchema = matches[0][1].schema
136 srcCentroidKey =
Point2DKey(srcSchema[
"slot_Centroid"])
138 if refCat
is not None:
139 refXArr, refYArr = list(zip(*[ref.get(refCentroidKey)
for ref
in refCat]))
140 plt.plot(refXArr, refYArr, marker=refMarker, color=refColor, linestyle=
"")
142 if sourceCat
is not None:
143 srcXArr, srcYArr = list(zip(*[src.get(srcCentroidKey)
for src
in sourceCat]))
144 plt.plot(srcXArr, srcYArr, marker=sourceMarker, color=sourceColor, linestyle=
"")
146 def makeLineSegmentData(refXYArr, srcXYArr, colorArr):
147 """Make a list of line segement data 149 This is used to draw line segements between ref and src positions in the specified color 151 The returned data has the format: 152 [(refX0, srcX0), (refY0, srcY0), color0, (refX1, srcX1), (refY1, srcY1), color1,...] 154 if len(refXYArr) != len(srcXYArr):
155 raise RuntimeError(
"len(refXYArr) = %d != %d = len(srcXYArr)" %
156 (len(refXYArr), len(srcXYArr)))
157 if len(refXYArr) != len(colorArr):
158 raise RuntimeError(
"len(refXYArr) = %d != %d = len(colorArr)" %
159 (len(refXYArr), len(colorArr)))
161 refXArr, refYArr = list(zip(*refXYArr))
162 srcXArr, srcYArr = list(zip(*srcXYArr))
163 refSrcXArr = list(zip(refXArr, srcXArr))
164 refSrcYArr = list(zip(refYArr, srcYArr))
166 for xycolor
in zip(refSrcXArr, refSrcYArr, colorArr):
171 refXYArr, srcXYArr = \
172 list(zip(*[(match[0].get(refCentroidKey), match[1].get(srcCentroidKey))
for match
in matches]))
174 def plotSourceCircles(matches, color):
175 srcXYArr = [match[1].get(srcCentroidKey)
for match
in matches]
176 srcXArr, srcYArr = list(zip(*srcXYArr))
177 plt.plot(srcXArr, srcYArr,
"o", mec=color, mfc=
"none", ms=10,)
179 if callable(matchColor):
181 matchColorArr = [matchColor(match)
for match
in matches]
184 matchColorSet = set(matchColorArr)
185 for color
in matchColorSet:
186 subMatches = [match
for match
in matches
if matchColor(match) == color]
187 plotSourceCircles(subMatches, color=color)
189 matchColorArr = [matchColor]*len(refXYArr)
190 plotSourceCircles(matches, color=matchColor)
192 lineSegData = makeLineSegmentData(refXYArr, srcXYArr, matchColorArr)
193 plt.plot(*lineSegData)
def plotAstrometry(matches, refCat=None, sourceCat=None, refMarker="x", refColor="r", sourceMarker="+", sourceColor="g", matchColor="y")
def displayAstrometry(refCat=None, sourceCat=None, distortedCentroidKey=None, bbox=None, exposure=None, matches=None, frame=1, title="", pause=True)