Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit b62a3fb

Browse files
committed
Save memory by removing figure information after plotting
1 parent a46904e commit b62a3fb

1 file changed

Lines changed: 62 additions & 28 deletions

File tree

03_Image_Display_and_Manipulation.ipynb

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"%matplotlib inline\n",
7373
"import matplotlib.pyplot as plt # imports matplotlib.pyplot as plt\n",
7474
"import warnings # imports the warnings library\n",
75-
"\n",
75+
"import gc # imports python's garbage collector\n",
7676
"from astropy.wcs import WCS # imports astropy's World Coordinate System function WCS"
7777
]
7878
},
@@ -95,6 +95,30 @@
9595
"warnings.simplefilter(\"ignore\", category=UserWarning)"
9696
]
9797
},
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+
},
98122
{
99123
"cell_type": "markdown",
100124
"metadata": {},
@@ -234,15 +258,17 @@
234258
"outputs": [],
235259
"source": [
236260
"# create a matplotlib.pyplot figure\n",
237-
"plt.figure()\n",
261+
"fig,ax = plt.subplots()\n",
238262
"# get an alias to the lsst.afw.display.Display() method\n",
239-
"display = afwDisplay.Display()\n",
263+
"display = afwDisplay.Display(frame=fig)\n",
240264
"# set the image stretch algorithm and range\n",
241265
"display.scale('asinh', 'zscale')\n",
242266
"# load the image into the display\n",
243267
"display.mtv(calexp.image)\n",
244268
"# show the corresponding pyplot figure\n",
245-
"plt.show()"
269+
"plt.show()\n",
270+
"# clean up memory\n",
271+
"remove_figure(fig)"
246272
]
247273
},
248274
{
@@ -260,17 +286,20 @@
260286
"metadata": {},
261287
"outputs": [],
262288
"source": [
263-
"plt.figure()\n",
289+
"fig = plt.figure()\n",
264290
"# Set the figure's projection to be the WCS of the calexp\n",
265291
"plt.subplot(projection=WCS(calexp.getWcs().getFitsMetadata()))\n",
266292
"# Display the calexp image data array using the gray colormap (cmap)\n",
267293
"# 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",
269295
"# Add solid white grid lines\n",
270296
"plt.grid(color='white', ls='solid')\n",
271297
"# Label axes\n",
272298
"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)"
274303
]
275304
},
276305
{
@@ -298,7 +327,9 @@
298327
"display2.mtv(calexp.mask)\n",
299328
"\n",
300329
"plt.tight_layout()\n",
301-
"plt.show()"
330+
"plt.show()\n",
331+
"\n",
332+
"remove_figure(fig)"
302333
]
303334
},
304335
{
@@ -314,11 +345,12 @@
314345
"metadata": {},
315346
"outputs": [],
316347
"source": [
317-
"fig = plt.figure()\n",
318-
"display = afwDisplay.Display()\n",
348+
"fig,ax = plt.subplots()\n",
349+
"display = afwDisplay.Display(frame=fig)\n",
319350
"display.scale('linear', 'zscale')\n",
320351
"display.mtv(calexp.maskedImage)\n",
321-
"plt.show()"
352+
"plt.show()\n",
353+
"remove_figure(fig)"
322354
]
323355
},
324356
{
@@ -355,9 +387,9 @@
355387
"outputs": [],
356388
"source": [
357389
"# create a matplotlib.pyplot figure\n",
358-
"plt.figure()\n",
390+
"fig,ax = plt.subplots()\n",
359391
"# 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",
361393
"# set the image stretch algorithm and range\n",
362394
"afw_display.scale('asinh', 'zscale')\n",
363395
"# set the transparency of the mask plane (1 = opaque)\n",
@@ -367,7 +399,9 @@
367399
"# load the image and mask plane into the display\n",
368400
"afw_display.mtv(calexp)\n",
369401
"# show the corresponding pyplot figure\n",
370-
"plt.show()"
402+
"plt.show()\n",
403+
"# clean up memory\n",
404+
"remove_figure(fig)"
371405
]
372406
},
373407
{
@@ -544,14 +578,16 @@
544578
"outputs": [],
545579
"source": [
546580
"# create a matplotlib.pyplot figure\n",
547-
"plt.figure()\n",
581+
"fig,ax = plt.subplots()\n",
548582
"# get an alias to the lsst.afw.display.Display() method\n",
549-
"display = afwDisplay.Display()\n",
583+
"display = afwDisplay.Display(frame=fig)\n",
550584
"# set the image stretch algorithm and range\n",
551585
"display.scale('asinh', 'zscale')\n",
552586
"# load the image into the display\n",
553587
"display.mtv(coadd_calexp.image)\n",
554-
"plt.show()"
588+
"plt.show()\n",
589+
"# clean up memory\n",
590+
"remove_figure(fig)"
555591
]
556592
},
557593
{
@@ -616,11 +652,13 @@
616652
"metadata": {},
617653
"outputs": [],
618654
"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",
621657
"afw_display.scale('asinh', 'zscale')\n",
622658
"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)"
624662
]
625663
},
626664
{
@@ -766,7 +804,10 @@
766804
"\n",
767805
"ax[0].set_axis_off()\n",
768806
"ax[1].set_axis_off()\n",
769-
"plt.show()"
807+
"plt.show()\n",
808+
"\n",
809+
"# clean up memory\n",
810+
"remove_figure(fig)"
770811
]
771812
},
772813
{
@@ -789,13 +830,6 @@
789830
"* [afw.display GitHub website](https://github.com/RobertLuptonTheGood/afw/tree/master/python/lsst/afw/display) \n",
790831
"* [Getting Started on Image Display (pipelines.lsst.io)](https://pipelines.lsst.io/getting-started/display.html)"
791832
]
792-
},
793-
{
794-
"cell_type": "code",
795-
"execution_count": null,
796-
"metadata": {},
797-
"outputs": [],
798-
"source": []
799833
}
800834
],
801835
"metadata": {

0 commit comments

Comments
 (0)