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