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