LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
imageDisplay.ipynb
Go to the documentation of this file.
1 {
2  "metadata": {
3  "name": ""
4  },
5  "nbformat": 3,
6  "nbformat_minor": 0,
7  "worksheets": [
8  {
9  "cells": [
10  {
11  "cell_type": "code",
12  "collapsed": false,
13  "input": [
14  "#\n",
15  "# Users should not usually set the default backend in a script.\n",
16  "#\n",
17  "# If you want to explicitly set the backend (the default is \"ds9\" \n",
18  "# if you've setup display_ds9, or \"virtualDevice\" otherwise), put \n",
19  "# something like this in your $PYTHONSTARTUP file\n",
20  "#\n",
21  "try:\n",
22  " import lsst.afw.display as afwDisplay\n",
23  "except ImportError:\n",
24  " afwDisplay = None\n",
25  "\n",
26  "if afwDisplay:\n",
27  " try:\n",
28  " afwDisplay.setDefaultBackend(\"ds9\" if True else \"virtualDevice\")\n",
29  " except RuntimeError as e:\n",
30  " print e\n",
31  "\n",
32  " afwDisplay.setDefaultMaskTransparency(75)"
33  ],
34  "language": "python",
35  "metadata": {},
36  "outputs": [],
37  "prompt_number": 6
38  },
39  {
40  "cell_type": "markdown",
41  "metadata": {},
42  "source": [
43  "You don't usually need to do this, but if we have changed the defaultBackend we need a clean slate of displays"
44  ]
45  },
46  {
47  "cell_type": "code",
48  "collapsed": false,
49  "input": [
50  "afwDisplay.delAllDisplays()"
51  ],
52  "language": "python",
53  "metadata": {},
54  "outputs": [],
55  "prompt_number": 2
56  },
57  {
58  "cell_type": "code",
59  "collapsed": false,
60  "input": [
61  "import lsst.afw.image as afwImage\n",
62  "\n",
63  "fileName = \"../tests/data/HSC-0908120-056-small.fits\"\n",
64  "exp = afwImage.ExposureF(fileName)"
65  ],
66  "language": "python",
67  "metadata": {},
68  "outputs": [],
69  "prompt_number": 3
70  },
71  {
72  "cell_type": "code",
73  "collapsed": false,
74  "input": [
75  "display0 = afwDisplay.getDisplay() # frame=0, verbose=True)"
76  ],
77  "language": "python",
78  "metadata": {},
79  "outputs": [],
80  "prompt_number": 4
81  },
82  {
83  "cell_type": "markdown",
84  "metadata": {},
85  "source": [
86  "The workhorse \"display my image\" routine"
87  ]
88  },
89  {
90  "cell_type": "code",
91  "collapsed": false,
92  "input": [
93  "display0.mtv(exp, title=\"parent\")"
94  ],
95  "language": "python",
96  "metadata": {},
97  "outputs": [],
98  "prompt_number": 5
99  },
100  {
101  "cell_type": "markdown",
102  "metadata": {},
103  "source": [
104  "Configure the mask plane transparency (alpha); in percent and draw the CROSSTALK plane in orange"
105  ]
106  },
107  {
108  "cell_type": "code",
109  "collapsed": false,
110  "input": [
111  "display0.setMaskTransparency(50)\n",
112  "display0.setMaskPlaneColor(\"CROSSTALK\", \"orange\")"
113  ],
114  "language": "python",
115  "metadata": {},
116  "outputs": [],
117  "prompt_number": 7
118  },
119  {
120  "cell_type": "markdown",
121  "metadata": {},
122  "source": [
123  "Now redisplay that image with some of the mask planes disabled"
124  ]
125  },
126  {
127  "cell_type": "code",
128  "collapsed": false,
129  "input": [
130  "for frame in (0, 1):\n",
131  " disp = afwDisplay.getDisplay(frame, verbose=True)\n",
132  " \n",
133  " disp.setMaskTransparency(50)\n",
134  "\n",
135  " if frame == 1:\n",
136  " disp.setMaskPlaneColor(\"CROSSTALK\", \"ignore\")\n",
137  " disp.mtv(exp, title=\"parent\")\n",
138  " \n",
139  " disp.erase()\n",
140  " disp.dot('o', 205, 180, size=6, ctype=afwDisplay.RED)"
141  ],
142  "language": "python",
143  "metadata": {},
144  "outputs": [],
145  "prompt_number": 8
146  },
147  {
148  "cell_type": "markdown",
149  "metadata": {},
150  "source": [
151  "Zoom and pan works too"
152  ]
153  },
154  {
155  "cell_type": "code",
156  "collapsed": false,
157  "input": [
158  "display0.pan(205, 180)\n",
159  "display0.zoom(4)\n",
160  "\n",
161  "afwDisplay.getDisplay(1).zoom(4, 205, 180)"
162  ],
163  "language": "python",
164  "metadata": {},
165  "outputs": [],
166  "prompt_number": 9
167  },
168  {
169  "cell_type": "markdown",
170  "metadata": {},
171  "source": [
172  "Now overlay something, in this case symbols and lines"
173  ]
174  },
175  {
176  "cell_type": "code",
177  "collapsed": false,
178  "input": [
179  "display0.show() # Un-iconise and raise the display to the top of the stacking order if appropriate\n",
180  "\n",
181  "display0.erase()\n",
182  "\n",
183  "with display0.Buffering():\n",
184  " display0.dot('o', 200, 220)\n",
185  " vertices = [(200, 220), (210, 230), (224, 230), (214, 220), (200, 220)]\n",
186  " display0.line(vertices, ctype=afwDisplay.CYAN)\n",
187  " display0.line(vertices[:-1], symbs=\"+x+x\", size=3)"
188  ],
189  "language": "python",
190  "metadata": {},
191  "outputs": [],
192  "prompt_number": 10
193  },
194  {
195  "cell_type": "markdown",
196  "metadata": {},
197  "source": [
198  "Now control the stretch."
199  ]
200  },
201  {
202  "cell_type": "code",
203  "collapsed": false,
204  "input": [
205  "display0.show()\n",
206  "\n",
207  "display0.scale(\"linear\", \"zscale\")"
208  ],
209  "language": "python",
210  "metadata": {},
211  "outputs": [],
212  "prompt_number": 11
213  },
214  {
215  "cell_type": "markdown",
216  "metadata": {},
217  "source": [
218  "Demonstrate the utility routine to generate mask plane colours (used by e.g. the ds9 implementation of _mtv)"
219  ]
220  },
221  {
222  "cell_type": "code",
223  "collapsed": false,
224  "input": [
225  "colorGenerator = display0.maskColorGenerator(omitBW=True)\n",
226  "for i in range(10):\n",
227  " print i, next(colorGenerator),"
228  ],
229  "language": "python",
230  "metadata": {},
231  "outputs": [
232  {
233  "output_type": "stream",
234  "stream": "stdout",
235  "text": [
236  "0 red 1 green 2 blue 3 cyan 4 magenta 5 yellow 6 red 7 green 8 blue 9 cyan\n"
237  ]
238  }
239  ],
240  "prompt_number": 12
241  },
242  {
243  "cell_type": "markdown",
244  "metadata": {},
245  "source": [
246  "Check that we can display a range of types of image"
247  ]
248  },
249  {
250  "cell_type": "code",
251  "collapsed": false,
252  "input": [
253  "dummy = afwDisplay.getDisplay(\"dummy\", \"virtualDevice\")\n",
254  "\n",
255  "for imageType in [afwImage.DecoratedImageF,\n",
256  " afwImage.ExposureF,\n",
257  " afwImage.ImageU, \n",
258  " afwImage.ImageI,\n",
259  " afwImage.ImageF,\n",
260  " afwImage.MaskedImageF,\n",
261  " ]:\n",
262  " im = imageType(fileName)\n",
263  " dummy.mtv(im)\n",
264  " \n",
265  "im = afwImage.MaskU(fileName, 3)\n",
266  "dummy.mtv(im)"
267  ],
268  "language": "python",
269  "metadata": {},
270  "outputs": [],
271  "prompt_number": 13
272  },
273  {
274  "cell_type": "markdown",
275  "metadata": {},
276  "source": [
277  "Now the make-an-image-mosaic code. Start by creating a set of 30x30 images with labels"
278  ]
279  },
280  {
281  "cell_type": "code",
282  "collapsed": false,
283  "input": [
284  "images = []\n",
285  "labels = []\n",
286  "for i in range(1, 4):\n",
287  " im = afwImage.ImageF(30, 30); im[:] = 100*i\n",
288  " images.append(im)\n",
289  " labels.append(\"Label %d\" % i)"
290  ],
291  "language": "python",
292  "metadata": {},
293  "outputs": [],
294  "prompt_number": 14
295  },
296  {
297  "cell_type": "code",
298  "collapsed": false,
299  "input": [
300  "m = afwDisplay.Mosaic()\n",
301  "\n",
302  "mosaic = m.makeMosaic(images)\n",
303  "disp = afwDisplay.getDisplay(frame=2)\n",
304  "disp.mtv(mosaic)\n",
305  "m.drawLabels(labels, display=disp)"
306  ],
307  "language": "python",
308  "metadata": {},
309  "outputs": [],
310  "prompt_number": 15
311  },
312  {
313  "cell_type": "code",
314  "collapsed": false,
315  "input": [
316  "m = afwDisplay.Mosaic()\n",
317  " \n",
318  "m.setGutter(5)\n",
319  "m.setBackground(10)\n",
320  "m.setMode(\"x\")\n",
321  " \n",
322  "for im, lab in zip(images, labels):\n",
323  " m.append(im, lab)\n",
324  " \n",
325  "mos = m.makeMosaic(frame=3) # it's really better to pass a Display object"
326  ],
327  "language": "python",
328  "metadata": {},
329  "outputs": [],
330  "prompt_number": 16
331  }
332  ],
333  "metadata": {}
334  }
335  ]
336 }