Skip to content

Commit a8cee92

Browse files
committed
Refactor plot capture to defer matplotlib import and simplify logic
1 parent d25bd11 commit a8cee92

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

evaluation_function/evaluation.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,22 @@
1313

1414
_PREAMBLE_TEMPLATE = """\
1515
import os as _os
16-
import matplotlib.pyplot as _plt
1716
import atexit as _atexit
1817
1918
_plot_dir = {plot_dir!r}
2019
_plot_idx = [0]
2120
22-
def _patched_show(*args, **kwargs):
21+
def _capture_plots():
22+
import sys as _sys
23+
if 'matplotlib.pyplot' not in _sys.modules:
24+
return
25+
import matplotlib.pyplot as _plt
2326
for num in _plt.get_fignums():
2427
_plot_idx[0] += 1
25-
_plt.figure(num).savefig(_os.path.join(_plot_dir, str(_plot_idx[0]).zfill(4) + '.png'))
26-
_plt.close('all')
28+
_plt.figure(num).savefig(
29+
_os.path.join(_plot_dir, str(_plot_idx[0]).zfill(4) + '.png'))
2730
28-
_plt.show = _patched_show
29-
30-
def _capture_remaining():
31-
for num in _plt.get_fignums():
32-
_plot_idx[0] += 1
33-
_plt.figure(num).savefig(_os.path.join(_plot_dir, str(_plot_idx[0]).zfill(4) + '.png'))
34-
35-
_atexit.register(_capture_remaining)
31+
_atexit.register(_capture_plots)
3632
"""
3733

3834

@@ -97,6 +93,7 @@ def evaluation_function(response: Any, answer: Any, params: Params) -> Result:
9793
result.add_feedback("output", "\n\n".join(parts))
9894
return result
9995

96+
10097
passed = 0
10198

10299
for i, test in enumerate(tests, 1):

0 commit comments

Comments
 (0)