diff --git a/pyoptsparse/pyCONMIN/pyCONMIN.py b/pyoptsparse/pyCONMIN/pyCONMIN.py index 9261a0a6..97b57930 100644 --- a/pyoptsparse/pyCONMIN/pyCONMIN.py +++ b/pyoptsparse/pyCONMIN/pyCONMIN.py @@ -18,7 +18,7 @@ # import the compiled module THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -conmin = try_import_compiled_module_from_path("conmin", THIS_DIR) +conmin = try_import_compiled_module_from_path("conmin", THIS_DIR, raise_warning=True) class CONMIN(Optimizer): diff --git a/pyoptsparse/pyNSGA2/pyNSGA2.py b/pyoptsparse/pyNSGA2/pyNSGA2.py index 6b0cc302..c6bd2e5c 100644 --- a/pyoptsparse/pyNSGA2/pyNSGA2.py +++ b/pyoptsparse/pyNSGA2/pyNSGA2.py @@ -17,7 +17,7 @@ # import the compiled module THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -nsga2 = try_import_compiled_module_from_path("nsga2", THIS_DIR) +nsga2 = try_import_compiled_module_from_path("nsga2", THIS_DIR, raise_warning=True) class NSGA2(Optimizer): diff --git a/pyoptsparse/pyOpt_constraint.py b/pyoptsparse/pyOpt_constraint.py index 80cfe85c..e85a850d 100644 --- a/pyoptsparse/pyOpt_constraint.py +++ b/pyoptsparse/pyOpt_constraint.py @@ -8,8 +8,8 @@ # Local modules from .pyOpt_error import Error, pyOptSparseWarning +from .pyOpt_types import Dict1DType from .pyOpt_utils import INFINITY, _broadcast_to_array, convertToCOO -from .types import Dict1DType class Constraint: diff --git a/pyoptsparse/pyOpt_gradient.py b/pyoptsparse/pyOpt_gradient.py index ec6e58a9..27083c64 100644 --- a/pyoptsparse/pyOpt_gradient.py +++ b/pyoptsparse/pyOpt_gradient.py @@ -8,7 +8,7 @@ # Local modules from .pyOpt_MPI import MPI from .pyOpt_optimization import Optimization -from .types import Dict1DType, Dict2DType +from .pyOpt_types import Dict1DType, Dict2DType class Gradient: diff --git a/pyoptsparse/pyOpt_optimization.py b/pyoptsparse/pyOpt_optimization.py index ac41174d..a15ffa93 100644 --- a/pyoptsparse/pyOpt_optimization.py +++ b/pyoptsparse/pyOpt_optimization.py @@ -15,6 +15,7 @@ from .pyOpt_constraint import Constraint from .pyOpt_error import Error from .pyOpt_objective import Objective +from .pyOpt_types import Dict1DType, Dict2DType, NumpyType from .pyOpt_utils import ( ICOL, IDATA, @@ -28,7 +29,6 @@ scaleRows, ) from .pyOpt_variable import Variable -from .types import Dict1DType, Dict2DType, NumpyType class Optimization: diff --git a/pyoptsparse/types.py b/pyoptsparse/pyOpt_types.py similarity index 100% rename from pyoptsparse/types.py rename to pyoptsparse/pyOpt_types.py diff --git a/pyoptsparse/pyOpt_utils.py b/pyoptsparse/pyOpt_utils.py index 35eb7400..7edba239 100644 --- a/pyoptsparse/pyOpt_utils.py +++ b/pyoptsparse/pyOpt_utils.py @@ -8,6 +8,7 @@ mat = {'csr':[rowp, colind, data], 'shape':[nrow, ncols]} # A csr matrix mat = {'csc':[colp, rowind, data], 'shape':[nrow, ncols]} # A csc matrix """ + # Standard Python modules import importlib import os @@ -24,7 +25,7 @@ # Local modules from .pyOpt_error import Error -from .types import ArrayType +from .pyOpt_types import ArrayType # Define index mnemonics IROW = 0 @@ -576,7 +577,9 @@ def _broadcast_to_array(name: str, value: ArrayType, n_values: int, allow_none: return value -def try_import_compiled_module_from_path(module_name: str, path: Optional[str] = None) -> Union[types.ModuleType, str]: +def try_import_compiled_module_from_path( + module_name: str, path: Optional[str] = None, raise_warning: bool = False +) -> Union[types.ModuleType, str]: """ Attempt to import a module from a given path. @@ -586,6 +589,8 @@ def try_import_compiled_module_from_path(module_name: str, path: Optional[str] = The name of the module path : Optional[str] The path to import from. If None, the default ``sys.path`` is used. + raise_warning : bool + If true, raise an import warning. By default false. Returns ------- @@ -600,7 +605,7 @@ def try_import_compiled_module_from_path(module_name: str, path: Optional[str] = try: module = importlib.import_module(module_name) except ImportError as e: - if path is not None: + if raise_warning: warnings.warn( f"{module_name} module could not be imported from {path}.", stacklevel=2, diff --git a/pyoptsparse/pySLSQP/pySLSQP.py b/pyoptsparse/pySLSQP/pySLSQP.py index d7e6b367..bb2431b4 100644 --- a/pyoptsparse/pySLSQP/pySLSQP.py +++ b/pyoptsparse/pySLSQP/pySLSQP.py @@ -17,7 +17,7 @@ # import the compiled module THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -slsqp = try_import_compiled_module_from_path("slsqp", THIS_DIR) +slsqp = try_import_compiled_module_from_path("slsqp", THIS_DIR, raise_warning=True) class SLSQP(Optimizer): diff --git a/pyoptsparse/pySNOPT/pySNOPT.py b/pyoptsparse/pySNOPT/pySNOPT.py index 63185e33..c576a978 100644 --- a/pyoptsparse/pySNOPT/pySNOPT.py +++ b/pyoptsparse/pySNOPT/pySNOPT.py @@ -385,6 +385,7 @@ def __call__( self.setOption("Total real workspace", lenrw) cw = np.empty((lencw, 8), dtype="|S1") + cw[:] = " " iw = np.zeros(leniw, np.intc) rw = np.zeros(lenrw, float) snopt.sninit(iPrint, iSumm, cw, iw, rw) @@ -444,11 +445,6 @@ def __call__( start = np.array(self.getOption("Start")) ObjAdd = np.array(0.0, float) ProbNm = np.array(self.optProb.name, "c") - cdummy = -1111111 # this is a magic variable defined in SNOPT for undefined strings - cw[51, :] = cdummy # we set these to cdummy so that a placeholder is used in printout - cw[52, :] = cdummy - cw[53, :] = cdummy - cw[54, :] = cdummy xs = np.concatenate((xs, np.zeros(ncon, float))) bl = np.concatenate((blx, blc)) bu = np.concatenate((bux, buc)) @@ -701,11 +697,11 @@ def _set_snopt_options(self, iPrint: int, iSumm: int, cw: ndarray, iw: ndarray, if name == "Problem Type": snopt.snset(value, iPrint, iSumm, inform, cw, iw, rw) elif name == "Print file": - snopt.snset(name + " " + f"{iPrint}", iPrint, iSumm, inform, cw, iw, rw) + snopt.snset(f"{name} {iPrint}", iPrint, iSumm, inform, cw, iw, rw) elif name == "Summary file": - snopt.snset(name + " " + f"{iSumm}", iPrint, iSumm, inform, cw, iw, rw) + snopt.snset(f"{name} {iSumm}", iPrint, iSumm, inform, cw, iw, rw) else: - snopt.snset(name + " " + value, iPrint, iSumm, inform, cw, iw, rw) + snopt.snset(f"{name} {value}", iPrint, iSumm, inform, cw, iw, rw) elif isinstance(value, float): snopt.snsetr(name, value, iPrint, iSumm, inform, cw, iw, rw) elif isinstance(value, int): diff --git a/tests/test_other.py b/tests/test_other.py index 41435b8d..f0c59b2c 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -18,7 +18,7 @@ def test_nonexistent_path(self): if "snopt" in key: sys.modules.pop(key) with self.assertWarns(UserWarning): - module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path") + module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path", raise_warning=True) self.assertTrue(isinstance(module, str)) def test_sys_path_unchanged(self):