From cee1615157fba64c603123a39f6f6fbb8dc7e868 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 24 Apr 2023 07:01:09 -0500 Subject: [PATCH 1/3] Defer import of matplotlib in multipole.py --- openmc/data/multipole.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/openmc/data/multipole.py b/openmc/data/multipole.py index 642f2cb43..5e2279902 100644 --- a/openmc/data/multipole.py +++ b/openmc/data/multipole.py @@ -1,14 +1,12 @@ from numbers import Real from math import exp, erf, pi, sqrt from copy import deepcopy -import warnings import os import h5py import pickle import numpy as np from scipy.signal import find_peaks -import matplotlib.pyplot as plt import openmc.checkvalue as cv from ..exceptions import DataError @@ -364,6 +362,7 @@ def _vectfit_xs(energy, ce_xs, mts, rtol=1e-3, atol=1e-5, orders=None, for i, mt in enumerate(mts): if not test_xs_ref[i].any(): continue + import matplotlib.pyplot as plt fig, ax1 = plt.subplots() lns1 = ax1.loglog(test_energy, test_xs_ref[i], 'g', label="ACE xs") lns2 = ax1.loglog(test_energy, best_test_xs[i], 'b', label="VF xs") @@ -1154,7 +1153,7 @@ class WindowedMultipole(EqualityMixin): Returns ------- 3-tuple of Real - Scattering, absorption, and fission microscopic cross sections + Scattering, absorption, and fission microscopic cross sections at the given energy and temperature. """ From 664ae00cdc153e99cbe8aaa5204429565e96d319 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 26 Apr 2023 14:23:44 +0100 Subject: [PATCH 2/3] test to check matplotlib is deferred import --- tests/regression_tests/python_import/test.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/regression_tests/python_import/test.py diff --git a/tests/regression_tests/python_import/test.py b/tests/regression_tests/python_import/test.py new file mode 100644 index 000000000..d24a0cae1 --- /dev/null +++ b/tests/regression_tests/python_import/test.py @@ -0,0 +1,7 @@ +import sys +import openmc + + +def test_matplotlib_presence(): + """Checks that remains a deferred import""" + assert 'matplotlib.pyplot' not in sys.modules From f2549c9470efe891baf7c15b59f485a12236a8ea Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 26 Apr 2023 09:48:05 -0500 Subject: [PATCH 3/3] Defer a few more matplotlib imports --- openmc/model/surface_composite.py | 2 +- tests/regression_tests/python_import/test.py | 7 ------- tests/test_matplotlib_import.py | 6 ++++++ tests/unit_tests/test_plotter.py | 7 ++++--- 4 files changed, 11 insertions(+), 11 deletions(-) delete mode 100644 tests/regression_tests/python_import/test.py create mode 100644 tests/test_matplotlib_import.py diff --git a/openmc/model/surface_composite.py b/openmc/model/surface_composite.py index 25826723c..9508a1db8 100644 --- a/openmc/model/surface_composite.py +++ b/openmc/model/surface_composite.py @@ -6,7 +6,6 @@ import operator import numpy as np from scipy.spatial import ConvexHull, Delaunay -from matplotlib.path import Path import openmc from openmc.checkvalue import (check_greater_than, check_value, @@ -1021,6 +1020,7 @@ class Polygon(CompositeSurface): ------- surfsets : a list of lists of surface, operator pairs """ + from matplotlib.path import Path # Get centroids of all the simplices and determine if they are inside # the polygon defined by input vertices or not. diff --git a/tests/regression_tests/python_import/test.py b/tests/regression_tests/python_import/test.py deleted file mode 100644 index d24a0cae1..000000000 --- a/tests/regression_tests/python_import/test.py +++ /dev/null @@ -1,7 +0,0 @@ -import sys -import openmc - - -def test_matplotlib_presence(): - """Checks that remains a deferred import""" - assert 'matplotlib.pyplot' not in sys.modules diff --git a/tests/test_matplotlib_import.py b/tests/test_matplotlib_import.py new file mode 100644 index 000000000..d319976c4 --- /dev/null +++ b/tests/test_matplotlib_import.py @@ -0,0 +1,6 @@ +import sys +import openmc + +def test_matplotlib_presence(): + """Checks that matplotlib remains a deferred import""" + assert 'matplotlib' not in sys.modules diff --git a/tests/unit_tests/test_plotter.py b/tests/unit_tests/test_plotter.py index cabf9af6d..8520249ba 100644 --- a/tests/unit_tests/test_plotter.py +++ b/tests/unit_tests/test_plotter.py @@ -1,7 +1,6 @@ import numpy as np import openmc import pytest -from matplotlib.figure import Figure @pytest.fixture(scope='module') @@ -74,8 +73,10 @@ def test_calculate_cexs_with_materials(test_mat): @pytest.mark.parametrize("this", ["Be", "Be9"]) def test_plot_xs(this): - assert isinstance(openmc.plotter.plot_xs(this, types=['total']), Figure) + from matplotlib.figure import Figure + assert isinstance(openmc.plot_xs(this, types=['total']), Figure) def test_plot_xs_mat(test_mat): - assert isinstance(openmc.plotter.plot_xs(test_mat, types=['total']), Figure) + from matplotlib.figure import Figure + assert isinstance(openmc.plot_xs(test_mat, types=['total']), Figure)