|
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": {}, |
|
234 | 258 | "outputs": [], |
235 | 259 | "source": [ |
236 | 260 | "# create a matplotlib.pyplot figure\n", |
237 | | - "plt.figure()\n", |
| 261 | + "fig,ax = plt.subplots()\n", |
238 | 262 | "# get an alias to the lsst.afw.display.Display() method\n", |
239 | | - "display = afwDisplay.Display()\n", |
| 263 | + "display = afwDisplay.Display(frame=fig)\n", |
240 | 264 | "# set the image stretch algorithm and range\n", |
241 | 265 | "display.scale('asinh', 'zscale')\n", |
242 | 266 | "# load the image into the display\n", |
243 | 267 | "display.mtv(calexp.image)\n", |
244 | 268 | "# show the corresponding pyplot figure\n", |
245 | | - "plt.show()" |
| 269 | + "plt.show()\n", |
| 270 | + "# clean up memory\n", |
| 271 | + "remove_figure(fig)" |
246 | 272 | ] |
247 | 273 | }, |
248 | 274 | { |
|
260 | 286 | "metadata": {}, |
261 | 287 | "outputs": [], |
262 | 288 | "source": [ |
263 | | - "plt.figure()\n", |
| 289 | + "fig = plt.figure()\n", |
264 | 290 | "# Set the figure's projection to be the WCS of the calexp\n", |
265 | 291 | "plt.subplot(projection=WCS(calexp.getWcs().getFitsMetadata()))\n", |
266 | 292 | "# Display the calexp image data array using the gray colormap (cmap)\n", |
267 | 293 | "# 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", |
| 294 | + "im = plt.imshow(calexp.image.array, cmap='gray', vmin=-200.0, vmax=400, origin='lower')\n", |
269 | 295 | "# Add solid white grid lines\n", |
270 | 296 | "plt.grid(color='white', ls='solid')\n", |
271 | 297 | "# Label axes\n", |
272 | 298 | "plt.xlabel('Right Ascension')\n", |
273 | | - "plt.ylabel('Declination')" |
| 299 | + "plt.ylabel('Declination')\n", |
| 300 | + "plt.show()\n", |
| 301 | + "# Clean up memory\n", |
| 302 | + "remove_figure(fig)" |
274 | 303 | ] |
275 | 304 | }, |
276 | 305 | { |
|
298 | 327 | "display2.mtv(calexp.mask)\n", |
299 | 328 | "\n", |
300 | 329 | "plt.tight_layout()\n", |
301 | | - "plt.show()" |
| 330 | + "plt.show()\n", |
| 331 | + "\n", |
| 332 | + "remove_figure(fig)" |
302 | 333 | ] |
303 | 334 | }, |
304 | 335 | { |
|
314 | 345 | "metadata": {}, |
315 | 346 | "outputs": [], |
316 | 347 | "source": [ |
317 | | - "fig = plt.figure()\n", |
318 | | - "display = afwDisplay.Display()\n", |
| 348 | + "fig,ax = plt.subplots()\n", |
| 349 | + "display = afwDisplay.Display(frame=fig)\n", |
319 | 350 | "display.scale('linear', 'zscale')\n", |
320 | 351 | "display.mtv(calexp.maskedImage)\n", |
321 | | - "plt.show()" |
| 352 | + "plt.show()\n", |
| 353 | + "remove_figure(fig)" |
322 | 354 | ] |
323 | 355 | }, |
324 | 356 | { |
|
355 | 387 | "outputs": [], |
356 | 388 | "source": [ |
357 | 389 | "# create a matplotlib.pyplot figure\n", |
358 | | - "plt.figure()\n", |
| 390 | + "fig,ax = plt.subplots()\n", |
359 | 391 | "# get an alias to the lsst.afw.display.Display() method\n", |
360 | | - "afw_display = afwDisplay.Display()\n", |
| 392 | + "afw_display = afwDisplay.Display(frame=fig)\n", |
361 | 393 | "# set the image stretch algorithm and range\n", |
362 | 394 | "afw_display.scale('asinh', 'zscale')\n", |
363 | 395 | "# set the transparency of the mask plane (1 = opaque)\n", |
|
367 | 399 | "# load the image and mask plane into the display\n", |
368 | 400 | "afw_display.mtv(calexp)\n", |
369 | 401 | "# show the corresponding pyplot figure\n", |
370 | | - "plt.show()" |
| 402 | + "plt.show()\n", |
| 403 | + "# clean up memory\n", |
| 404 | + "remove_figure(fig)" |
371 | 405 | ] |
372 | 406 | }, |
373 | 407 | { |
|
544 | 578 | "outputs": [], |
545 | 579 | "source": [ |
546 | 580 | "# create a matplotlib.pyplot figure\n", |
547 | | - "plt.figure()\n", |
| 581 | + "fig,ax = plt.subplots()\n", |
548 | 582 | "# get an alias to the lsst.afw.display.Display() method\n", |
549 | | - "display = afwDisplay.Display()\n", |
| 583 | + "display = afwDisplay.Display(frame=fig)\n", |
550 | 584 | "# set the image stretch algorithm and range\n", |
551 | 585 | "display.scale('asinh', 'zscale')\n", |
552 | 586 | "# load the image into the display\n", |
553 | 587 | "display.mtv(coadd_calexp.image)\n", |
554 | | - "plt.show()" |
| 588 | + "plt.show()\n", |
| 589 | + "# clean up memory\n", |
| 590 | + "remove_figure(fig)" |
555 | 591 | ] |
556 | 592 | }, |
557 | 593 | { |
|
616 | 652 | "metadata": {}, |
617 | 653 | "outputs": [], |
618 | 654 | "source": [ |
619 | | - "fig = plt.figure()\n", |
620 | | - "afw_display = afwDisplay.Display(1)\n", |
| 655 | + "fig,ax = plt.subplots()\n", |
| 656 | + "afw_display = afwDisplay.Display(frame=fig)\n", |
621 | 657 | "afw_display.scale('asinh', 'zscale')\n", |
622 | 658 | "afw_display.mtv(cutout_image.image)\n", |
623 | | - "plt.gca().axis('off')" |
| 659 | + "ax.axis('off')\n", |
| 660 | + "plt.show()\n", |
| 661 | + "remove_figure(fig)" |
624 | 662 | ] |
625 | 663 | }, |
626 | 664 | { |
|
766 | 804 | "\n", |
767 | 805 | "ax[0].set_axis_off()\n", |
768 | 806 | "ax[1].set_axis_off()\n", |
769 | | - "plt.show()" |
| 807 | + "plt.show()\n", |
| 808 | + "\n", |
| 809 | + "# clean up memory\n", |
| 810 | + "remove_figure(fig)" |
770 | 811 | ] |
771 | 812 | }, |
772 | 813 | { |
|
789 | 830 | "* [afw.display GitHub website](https://github.com/RobertLuptonTheGood/afw/tree/master/python/lsst/afw/display) \n", |
790 | 831 | "* [Getting Started on Image Display (pipelines.lsst.io)](https://pipelines.lsst.io/getting-started/display.html)" |
791 | 832 | ] |
792 | | - }, |
793 | | - { |
794 | | - "cell_type": "code", |
795 | | - "execution_count": null, |
796 | | - "metadata": {}, |
797 | | - "outputs": [], |
798 | | - "source": [] |
799 | 833 | } |
800 | 834 | ], |
801 | 835 | "metadata": { |
|
0 commit comments