diff --git a/pySOT/experimental_design/lhd.py b/pySOT/experimental_design/lhd.py index 194693b..56d6962 100644 --- a/pySOT/experimental_design/lhd.py +++ b/pySOT/experimental_design/lhd.py @@ -1,7 +1,6 @@ import warnings import numpy as np -import pyDOE2 as pydoe from .experimental_design import ExperimentalDesign, _expdes_dist @@ -52,6 +51,8 @@ def generate_points(self, lb=None, ub=None, int_var=None): int_var = np.array([]) def wrapper(): + import pyDOE2 as pydoe + return pydoe.lhs(self.dim, self.num_pts, iterations=1) return _expdes_dist(wrapper, self.iterations, lb, ub, int_var) diff --git a/tests/test_experimental_designs.py b/tests/test_experimental_designs.py index c3c7429..156b2cf 100644 --- a/tests/test_experimental_designs.py +++ b/tests/test_experimental_designs.py @@ -1,3 +1,6 @@ +import subprocess +import sys + import numpy as np import pytest @@ -39,6 +42,31 @@ def test_slhd(): assert slhd.dim == 3 +def test_slhd_import_does_not_require_pydoe2(): + script = """ +import importlib.abc +import sys + +class BlockPyDOE2(importlib.abc.MetaPathFinder): + def find_spec(self, fullname, path=None, target=None): + if fullname == "pyDOE2": + raise ImportError("blocked pyDOE2 import") + return None + +sys.meta_path.insert(0, BlockPyDOE2()) +from pySOT.experimental_design import SymmetricLatinHypercube +SymmetricLatinHypercube(dim=2, num_pts=4) +""" + proc = subprocess.Popen( + [sys.executable, "-c", script], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + _, stderr = proc.communicate() + + assert proc.returncode == 0, stderr.decode("utf-8") + + def test_slhd_round(): num_pts = 10 dim = 3