Skip to content

Commit

Permalink
Avoiding closing plots that are meant to be interactive (#1978)
Browse files Browse the repository at this point in the history
The plt.show() options are intended for:

1. Interactive use on ipython or notebooks
2. Automated use during sphinx-gallery generation

Running plt.show() followed by plt.close() prevents at
least the 2nd use case from working, as seen in #1977.

This change puts the close() only in the code branches
that are expected to be run non-interactively, e.g.
compute code. That allows the system to keep memory
clear during big runs while still allowing interactive
and gallery usage.

Fixes #1977
  • Loading branch information
ntouran authored Oct 26, 2024
1 parent 2743df2 commit 8a43001
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 16 deletions.
6 changes: 2 additions & 4 deletions armi/nuclearDataIO/xsNuclides.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ def plotScatterMatrix(scatterMatrix, scatterTypeLabel="", fName=None):
pyplot.colorbar()
if fName:
pyplot.savefig(fName)
pyplot.close()
else:
pyplot.show()

pyplot.close()


def plotCompareScatterMatrix(scatterMatrix1, scatterMatrix2, fName=None):
"""Compares scatter matrices graphically between libraries."""
Expand All @@ -260,7 +259,6 @@ def plotCompareScatterMatrix(scatterMatrix1, scatterMatrix2, fName=None):
pyplot.colorbar()
if fName:
pyplot.savefig(fName)
pyplot.close()
else:
pyplot.show()

pyplot.close()
2 changes: 1 addition & 1 deletion armi/reactor/converters/blockConverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ def plotConvertedBlock(self, fName=None):
fig.tight_layout()
if fName:
plt.savefig(fName)
plt.close()
else:
plt.show()
plt.close()
return fName


Expand Down
16 changes: 9 additions & 7 deletions armi/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ def update(i):

if fName:
plt.savefig(fName, dpi=150)
plt.close()
else:
plt.show()

plt.close()
return fName


Expand Down Expand Up @@ -402,13 +402,16 @@ def plotFaceMap(
"Cannot update facemap at {0}: IOError. Is the file open?"
"".format(fName)
)
plt.close(fig)
elif referencesToKeep:
# Don't show yet, since it will be updated.
return fName
else:
# Never close figures after a .show()
# because they're being used interactively e.g.
# in a live tutorial or by the doc gallery
plt.show()

plt.close(fig)
return fName


Expand Down Expand Up @@ -823,8 +826,8 @@ def plotAssemblyTypes(
if fileName:
fig.savefig(fileName)
runLog.debug("Writing assem layout {} in {}".format(fileName, os.getcwd()))
plt.close(fig)

plt.close(fig)
return fig


Expand Down Expand Up @@ -1092,11 +1095,11 @@ def getTable(self):
os.path.abspath(fName),
report.FLUX_PLOT,
)
plt.close()
else:
# Never close interactive plots
plt.show()

plt.close()


def makeHistogram(x, y):
"""
Expand Down Expand Up @@ -1503,7 +1506,6 @@ def plotNucXs(

if fName:
plt.savefig(fName)
plt.close()
elif not noShow:
plt.show()

plt.close()
1 change: 0 additions & 1 deletion doc/gallery-src/analysis/run_hexReactorToRZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@
figs = converter.plotConvertedReactor()

plt.show()
plt.close()
2 changes: 0 additions & 2 deletions doc/gallery-src/framework/run_fuelManagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
# sphinx_gallery_thumbnail_number = 2
import math

import matplotlib.pyplot as plt

from armi import configure
from armi.physics.fuelCycle import fuelHandlers
Expand Down Expand Up @@ -73,4 +72,3 @@

# show final burnup distribution
plotting.plotFaceMap(reactor.core, param="percentBu")
plt.close()
1 change: 0 additions & 1 deletion doc/gallery-src/framework/run_reactorFacemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@
b.p.pdens = x**2 + y**2 + z**2

plotting.plotFaceMap(reactor.core, param="pdens", labelFmt="{0:.1e}")
plotting.close()

0 comments on commit 8a43001

Please sign in to comment.