|
72 | 72 | "%matplotlib inline\n", |
73 | 73 | "import matplotlib.pyplot as plt # imports matplotlib.pyplot as plt\n", |
74 | 74 | "import warnings # imports the warnings library\n", |
75 | | - "\n", |
| 75 | + "import gc # imports python's garbage collector\n", |
76 | 76 | "from astropy.wcs import WCS # imports astropy's World Coordinate System function WCS" |
77 | 77 | ] |
78 | 78 | }, |
|
95 | 95 | "warnings.simplefilter(\"ignore\", category=UserWarning)" |
96 | 96 | ] |
97 | 97 | }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "Matplotlib stores the data array associated with an image that is plotted. Since the LSST images are large (~4k x 4k pixels), this can eventually lead to a memory overflow, which will cause the notebook kernel to die. To mitigate this issue, we define a function to clean up after we plot them." |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": null, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "def remove_figure(fig):\n", |
| 112 | + " \"\"\"Remove a figure to reduce memory footprint. \"\"\"\n", |
| 113 | + " # get the axes and clear their images\n", |
| 114 | + " for ax in fig.get_axes():\n", |
| 115 | + " for im in ax.get_images():\n", |
| 116 | + " im.remove()\n", |
| 117 | + " fig.clf() # clear the figure\n", |
| 118 | + " plt.close(fig) # close the figure\n", |
| 119 | + " gc.collect() # call the garbage collector" |
| 120 | + ] |
| 121 | + }, |
98 | 122 | { |
99 | 123 | "cell_type": "markdown", |
100 | 124 | "metadata": {}, |
|
178 | 202 | "metadata": {}, |
179 | 203 | "outputs": [], |
180 | 204 | "source": [ |
181 | | - "# Note that the keys are slightly different for DC2/LSSTCam\n", |
182 | | - "# You can view all the keys by creating the butler and calling:\n", |
183 | | - "# print(butler.getKeys('calexp'))\n", |
184 | | - "\n", |
185 | 205 | "dataId = {'visit': 192350, 'detector': 175, 'band': 'i'}\n", |
186 | 206 | "# Note: because the combination of visit+detector already uniquely identifies\n", |
187 | 207 | "# the exposure, specifying \"band\" above is unnecessary.\n", |
|
234 | 254 | "outputs": [], |
235 | 255 | "source": [ |
236 | 256 | "# create a matplotlib.pyplot figure\n", |
237 | | - "plt.figure()\n", |
| 257 | + "fig, ax = plt.subplots()\n", |
238 | 258 | "# get an alias to the lsst.afw.display.Display() method\n", |
239 | | - "display = afwDisplay.Display()\n", |
| 259 | + "display = afwDisplay.Display(frame=fig)\n", |
240 | 260 | "# set the image stretch algorithm and range\n", |
241 | 261 | "display.scale('asinh', 'zscale')\n", |
242 | 262 | "# load the image into the display\n", |
243 | 263 | "display.mtv(calexp.image)\n", |
244 | 264 | "# show the corresponding pyplot figure\n", |
245 | | - "plt.show()" |
| 265 | + "plt.show()\n", |
| 266 | + "# clean up memory\n", |
| 267 | + "remove_figure(fig)" |
246 | 268 | ] |
247 | 269 | }, |
248 | 270 | { |
|
260 | 282 | "metadata": {}, |
261 | 283 | "outputs": [], |
262 | 284 | "source": [ |
263 | | - "plt.figure()\n", |
| 285 | + "fig = plt.figure()\n", |
264 | 286 | "# Set the figure's projection to be the WCS of the calexp\n", |
265 | 287 | "plt.subplot(projection=WCS(calexp.getWcs().getFitsMetadata()))\n", |
266 | 288 | "# Display the calexp image data array using the gray colormap (cmap)\n", |
267 | 289 | "# and use approximately the same min and max scale values as above\n", |
268 | | - "plt.imshow(calexp.image.array, cmap='gray', vmin=-200.0, vmax=400, origin='lower')\n", |
| 290 | + "im = plt.imshow(calexp.image.array, cmap='gray', vmin=-200.0, vmax=400, origin='lower')\n", |
269 | 291 | "# Add solid white grid lines\n", |
270 | 292 | "plt.grid(color='white', ls='solid')\n", |
271 | 293 | "# Label axes\n", |
272 | 294 | "plt.xlabel('Right Ascension')\n", |
273 | | - "plt.ylabel('Declination')" |
| 295 | + "plt.ylabel('Declination')\n", |
| 296 | + "plt.show()\n", |
| 297 | + "# Clean up memory\n", |
| 298 | + "remove_figure(fig)" |
274 | 299 | ] |
275 | 300 | }, |
276 | 301 | { |
|
287 | 312 | "outputs": [], |
288 | 313 | "source": [ |
289 | 314 | "fig, ax = plt.subplots(1, 2, figsize=(14, 7))\n", |
290 | | - "\n", |
291 | 315 | "plt.sca(ax[0]) # set the first axis as current\n", |
292 | 316 | "display1 = afwDisplay.Display(frame=fig)\n", |
293 | 317 | "display1.scale('linear', 'zscale')\n", |
294 | 318 | "display1.mtv(calexp.image)\n", |
295 | | - "\n", |
296 | 319 | "plt.sca(ax[1]) # set the second axis as current\n", |
297 | 320 | "display2 = afwDisplay.Display(frame=fig)\n", |
298 | 321 | "display2.mtv(calexp.mask)\n", |
299 | | - "\n", |
300 | 322 | "plt.tight_layout()\n", |
301 | | - "plt.show()" |
| 323 | + "plt.show()\n", |
| 324 | + "remove_figure(fig)" |
302 | 325 | ] |
303 | 326 | }, |
304 | 327 | { |
|
314 | 337 | "metadata": {}, |
315 | 338 | "outputs": [], |
316 | 339 | "source": [ |
317 | | - "fig = plt.figure()\n", |
318 | | - "display = afwDisplay.Display()\n", |
| 340 | + "fig, ax = plt.subplots()\n", |
| 341 | + "display = afwDisplay.Display(frame=fig)\n", |
319 | 342 | "display.scale('linear', 'zscale')\n", |
320 | 343 | "display.mtv(calexp.maskedImage)\n", |
321 | | - "plt.show()" |
| 344 | + "plt.show()\n", |
| 345 | + "remove_figure(fig)" |
322 | 346 | ] |
323 | 347 | }, |
324 | 348 | { |
|
355 | 379 | "outputs": [], |
356 | 380 | "source": [ |
357 | 381 | "# create a matplotlib.pyplot figure\n", |
358 | | - "plt.figure()\n", |
| 382 | + "fig, ax = plt.subplots()\n", |
359 | 383 | "# get an alias to the lsst.afw.display.Display() method\n", |
360 | | - "afw_display = afwDisplay.Display()\n", |
| 384 | + "afw_display = afwDisplay.Display(frame=fig)\n", |
361 | 385 | "# set the image stretch algorithm and range\n", |
362 | 386 | "afw_display.scale('asinh', 'zscale')\n", |
363 | 387 | "# set the transparency of the mask plane (1 = opaque)\n", |
|
367 | 391 | "# load the image and mask plane into the display\n", |
368 | 392 | "afw_display.mtv(calexp)\n", |
369 | 393 | "# show the corresponding pyplot figure\n", |
370 | | - "plt.show()" |
| 394 | + "plt.show()\n", |
| 395 | + "# clean up memory\n", |
| 396 | + "remove_figure(fig)" |
371 | 397 | ] |
372 | 398 | }, |
373 | 399 | { |
|
544 | 570 | "outputs": [], |
545 | 571 | "source": [ |
546 | 572 | "# create a matplotlib.pyplot figure\n", |
547 | | - "plt.figure()\n", |
| 573 | + "fig, ax = plt.subplots()\n", |
548 | 574 | "# get an alias to the lsst.afw.display.Display() method\n", |
549 | | - "display = afwDisplay.Display()\n", |
| 575 | + "display = afwDisplay.Display(frame=fig)\n", |
550 | 576 | "# set the image stretch algorithm and range\n", |
551 | 577 | "display.scale('asinh', 'zscale')\n", |
552 | 578 | "# load the image into the display\n", |
553 | 579 | "display.mtv(coadd_calexp.image)\n", |
554 | | - "plt.show()" |
| 580 | + "plt.show()\n", |
| 581 | + "# clean up memory\n", |
| 582 | + "remove_figure(fig)" |
555 | 583 | ] |
556 | 584 | }, |
557 | 585 | { |
|
616 | 644 | "metadata": {}, |
617 | 645 | "outputs": [], |
618 | 646 | "source": [ |
619 | | - "fig = plt.figure()\n", |
620 | | - "afw_display = afwDisplay.Display(1)\n", |
| 647 | + "fig, ax = plt.subplots()\n", |
| 648 | + "afw_display = afwDisplay.Display(frame=fig)\n", |
621 | 649 | "afw_display.scale('asinh', 'zscale')\n", |
622 | 650 | "afw_display.mtv(cutout_image.image)\n", |
623 | | - "plt.gca().axis('off')" |
| 651 | + "ax.axis('off')\n", |
| 652 | + "plt.show()\n", |
| 653 | + "remove_figure(fig)" |
624 | 654 | ] |
625 | 655 | }, |
626 | 656 | { |
|
766 | 796 | "\n", |
767 | 797 | "ax[0].set_axis_off()\n", |
768 | 798 | "ax[1].set_axis_off()\n", |
769 | | - "plt.show()" |
| 799 | + "plt.show()\n", |
| 800 | + "\n", |
| 801 | + "# clean up memory\n", |
| 802 | + "remove_figure(fig)" |
770 | 803 | ] |
771 | 804 | }, |
772 | 805 | { |
|
789 | 822 | "* [afw.display GitHub website](https://github.com/RobertLuptonTheGood/afw/tree/master/python/lsst/afw/display) \n", |
790 | 823 | "* [Getting Started on Image Display (pipelines.lsst.io)](https://pipelines.lsst.io/getting-started/display.html)" |
791 | 824 | ] |
792 | | - }, |
793 | | - { |
794 | | - "cell_type": "code", |
795 | | - "execution_count": null, |
796 | | - "metadata": {}, |
797 | | - "outputs": [], |
798 | | - "source": [] |
799 | 825 | } |
800 | 826 | ], |
801 | 827 | "metadata": { |
|
0 commit comments