Skip to content

Commit

Permalink
Update to 2024 versions of black and scikit-learn (#504)
Browse files Browse the repository at this point in the history
* switch from black 23 to black 24

* fix mfkpls
  • Loading branch information
Paul-Saves committed Jan 29, 2024
1 parent c9bb576 commit be831f5
Show file tree
Hide file tree
Showing 48 changed files with 82 additions and 55 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
This package is distributed under New BSD license.
"""

from setuptools import setup, Extension
import sys
import numpy as np
Expand Down
7 changes: 5 additions & 2 deletions smt/applications/mfkpls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

from sklearn.cross_decomposition import PLSRegression as pls
from sklearn.metrics.pairwise import manhattan_distances
from sklearn.metrics.pairwise import check_pairwise_arrays

from smt.applications import MFK
from smt.utils.kriging import componentwise_distance_PLS
Expand Down Expand Up @@ -42,7 +42,10 @@ def _differences(self, X, Y):
Overrides differences function for MFK
Compute the manhattan_distances
"""
return manhattan_distances(X, Y, sum_over_features=False)
X, Y = check_pairwise_arrays(X, Y)
D = X[:, np.newaxis, :] - Y[np.newaxis, :, :]
D = np.abs(D, D)
return D.reshape((-1, X.shape[1]))

def _componentwise_distance(self, dx, opt=0):
d = componentwise_distance_PLS(
Expand Down
6 changes: 3 additions & 3 deletions smt/applications/mixed_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ def __init__(
)
and self._surrogate.options["categorical_kernel"] is None
):
self._surrogate.options[
"categorical_kernel"
] = MixIntKernelType.HOMO_HSPHERE
self._surrogate.options["categorical_kernel"] = (
MixIntKernelType.HOMO_HSPHERE
)
warnings.warn(
"Using MixedIntegerSurrogateModel integer model with Continuous Relaxation is not supported. Switched to homoscedastic hypersphere kernel instead."
)
Expand Down
1 change: 1 addition & 0 deletions smt/applications/moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Mixture of Experts
"""

# TODO : support for best number of clusters
# TODO : implement verbosity 'print_global'
# TODO : documentation
Expand Down
1 change: 1 addition & 0 deletions smt/examples/multi_modal/run_genn_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
This package is distributed under New BSD license.
"""

from smt.surrogate_models.genn import GENN, load_smt_data

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions smt/problems/branin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Branin function.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
16 changes: 4 additions & 12 deletions smt/problems/cantilever_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design: comparison and application. In ASME Turbo Expo 2016: Turbomachinery Technical Conference and Exposition. American Society of Mechanical Engineers. June, 2016.
Cheng, G. H., Younis, A., Hajikolaei, K. H., and Wang, G. G. Trust Region Based Mode Pursuing Sampling Method for Global Optimization of High Dimensional Design Problems. Journal of Mechanical Design, 137(2). 2015.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down Expand Up @@ -75,16 +76,10 @@ def _evaluate(self, x, kx):
b = x[:, 3 * kelem + 0]
h = x[:, 3 * kelem + 1]
y[:, 0] += (
-12.0
/ b**2
/ h**3
* np.sum(x[:, 2 + 3 * kelem :: 3], axis=1) ** 3
-12.0 / b**2 / h**3 * np.sum(x[:, 2 + 3 * kelem :: 3], axis=1) ** 3
)
y[:, 0] -= (
-12.0
/ b**2
/ h**3
* np.sum(x[:, 5 + 3 * kelem :: 3], axis=1) ** 3
-12.0 / b**2 / h**3 * np.sum(x[:, 5 + 3 * kelem :: 3], axis=1) ** 3
)

elif kx % 3 == 1:
Expand All @@ -101,10 +96,7 @@ def _evaluate(self, x, kx):
b = x[:, 3 * ielem + 0]
h = x[:, 3 * ielem + 1]
y[:, 0] += (
36.0
/ b
/ h**3
* np.sum(x[:, 2 + 3 * ielem :: 3], axis=1) ** 2
36.0 / b / h**3 * np.sum(x[:, 2 + 3 * ielem :: 3], axis=1) ** 2
)
if kelem > ielem:
y[:, 0] -= (
Expand Down
1 change: 1 addition & 0 deletions smt/problems/hierarchical_goldstein.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Cantilever beam problem from:
P. Saves, Y. Diouane, N. Bartoli, T. Lefebvre, and J. Morlier. A mixed-categorical correlation kernel for gaussian process, 2022
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/lp_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Norm function.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/mixed_cantilever_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Cantilever beam problem from:
P. Saves, Y. Diouane, N. Bartoli, T. Lefebvre, and J. Morlier. A mixed-categorical correlation kernel for gaussian process, 2022
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/ndim_cantilever_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
N-dimensional cantilever beam problem.
"""

import numpy as np

from smt.utils.options_dictionary import OptionsDictionary
Expand Down
1 change: 1 addition & 0 deletions smt/problems/ndim_robot_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
N-dimensional robot arm problem.
"""

import numpy as np

from smt.utils.options_dictionary import OptionsDictionary
Expand Down
1 change: 1 addition & 0 deletions smt/problems/ndim_rosenbrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
N-dimensional Rosenbrock problem.
"""

import numpy as np

from smt.utils.options_dictionary import OptionsDictionary
Expand Down
1 change: 1 addition & 0 deletions smt/problems/ndim_step_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
N-dimensional step function problem.
"""

import numpy as np

from smt.utils.options_dictionary import OptionsDictionary
Expand Down
1 change: 1 addition & 0 deletions smt/problems/neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
C. Audet, E. Hall e-Hannan, and S. Le Digabel. A general mathematical framework for constrained mixed-variable blackbox optimization problems with meta and categorical variables. Operations Research Forum,499
4:137, 2023.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Base class for benchmarking/test problems.
"""

from typing import Optional
import numpy as np

Expand Down
1 change: 1 addition & 0 deletions smt/problems/reduced_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Reduced problem class - selects a subset of input variables.
"""

import numpy as np

from smt.utils.options_dictionary import OptionsDictionary
Expand Down
1 change: 1 addition & 0 deletions smt/problems/robot_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design: comparison and application. In ASME Turbo Expo 2016: Turbomachinery Technical Conference and Exposition. American Society of Mechanical Engineers. June, 2016.
An, J., and Owen, A. Quasi-Regression. Journal of complexity, 17(4), pp. 588-607, 2001.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/rosenbrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Multi-dimensional Rosenbrock function.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Sphere function.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/tensor_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Tensor-product of cos, exp, or tanh.
"""

import numpy as np

from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/torsion_vibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design: comparison and application. In ASME Turbo Expo 2016: Turbomachinery Technical Conference and Exposition. American Society of Mechanical Engineers. June, 2016.
Wang, L., Beeson, D., Wiggs, G., and Rayasam, M. A Comparison of Metamodeling Methods Using Practical Industry Requirements. In Proceedings of the 47th AIAA/ASME/ASCE/AHS/ASC structures, structural dynamics, and materials conference, Newport, RI, pp. AIAA 2006-1811.
"""

import numpy as np
from scipy.misc import derivative

Expand Down
1 change: 1 addition & 0 deletions smt/problems/water_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design: comparison and application. In ASME Turbo Expo 2016: Turbomachinery Technical Conference and Exposition. American Society of Mechanical Engineers. June, 2016.
Morris, M. D., Mitchell, T. J., and Ylvisaker, D. Bayesian Design and Analysis of Computer Experiments: Use of Derivatives in Surface Prediction. Technometrics, 35(3), pp. 243-255. 1993.
"""

import numpy as np
from scipy.misc import derivative

Expand Down
1 change: 1 addition & 0 deletions smt/problems/water_flow_lfidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Water flow problem from:
Xiong, S., Qian, P. Z., & Wu, C. J. (2013). Sequential design and analysis of high-accuracy and low-accuracy computer codes. Technometrics, 55(1), 37-46.
"""

import numpy as np
from scipy.misc import derivative

Expand Down
1 change: 1 addition & 0 deletions smt/problems/welded_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design: comparison and application. In ASME Turbo Expo 2016: Turbomachinery Technical Conference and Exposition. American Society of Mechanical Engineers. June, 2016.
Deb, K. An Efficient Constraint Handling Method for Genetic Algorithms. Computer methods in applied mechanics and engineering, 186(2), pp. 311-338. 2000.
"""

import numpy as np
from scipy.misc import derivative
from smt.problems.problem import Problem
Expand Down
1 change: 1 addition & 0 deletions smt/problems/wing_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Forrester, A., Sobester, A., and Keane, A., 2008,
Engineering Design Via Surrogate Modelling: A Practical Guide, John Wiley & Sons, United Kingdom.
"""

import numpy as np
from scipy.misc import derivative

Expand Down
1 change: 1 addition & 0 deletions smt/sampling_methods/full_factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Full-factorial sampling.
"""

import numpy as np

from smt.sampling_methods.sampling_method import ScaledSamplingMethod
Expand Down
9 changes: 3 additions & 6 deletions smt/sampling_methods/lhs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
LHS sampling; uses the pyDOE3 package.
"""

from pyDOE3 import lhs
from scipy.spatial.distance import pdist, cdist
import numpy as np
Expand Down Expand Up @@ -290,12 +291,8 @@ def _PhiP_exchange(self, X, k, PhiP_, p, fixed_index):

dist1 = cdist([X[i1, :]], X_)
dist2 = cdist([X[i2, :]], X_)
d1 = np.sqrt(
dist1**2 + (X[i2, k] - X_[:, k]) ** 2 - (X[i1, k] - X_[:, k]) ** 2
)
d2 = np.sqrt(
dist2**2 - (X[i2, k] - X_[:, k]) ** 2 + (X[i1, k] - X_[:, k]) ** 2
)
d1 = np.sqrt(dist1**2 + (X[i2, k] - X_[:, k]) ** 2 - (X[i1, k] - X_[:, k]) ** 2)
d2 = np.sqrt(dist2**2 - (X[i2, k] - X_[:, k]) ** 2 + (X[i1, k] - X_[:, k]) ** 2)

res = (
PhiP_**p + (d1 ** (-p) - dist1 ** (-p) + d2 ** (-p) - dist2 ** (-p)).sum()
Expand Down
1 change: 1 addition & 0 deletions smt/sampling_methods/pydoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
pyDOE3 sampling methods
"""

from pyDOE3 import doe_box_behnken
from pyDOE3 import doe_gsd
from pyDOE3 import doe_factorial
Expand Down
1 change: 1 addition & 0 deletions smt/sampling_methods/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Random sampling.
"""

import numpy as np

from smt.sampling_methods.sampling_method import ScaledSamplingMethod
Expand Down
1 change: 1 addition & 0 deletions smt/sampling_methods/sampling_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Base class for sampling algorithms.
"""

from abc import ABCMeta, abstractmethod
import numpy as np
import warnings
Expand Down
2 changes: 1 addition & 1 deletion smt/surrogate_models/idw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
This package is distributed under New BSD license.
"""

import numpy as np
from scipy.sparse import csc_matrix
from smt.surrogate_models.surrogate_model import SurrogateModel
Expand All @@ -13,7 +14,6 @@


class IDW(SurrogateModel):

"""
Inverse distance weighting interpolant
This model uses the inverse distance between the unknown and training
Expand Down
1 change: 1 addition & 0 deletions smt/surrogate_models/krg_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Some functions are copied from gaussian_process submodule (Scikit-learn 0.14)
This package is distributed under New BSD license.
"""

import numpy as np
from enum import Enum
from scipy import linalg, optimize
Expand Down
2 changes: 1 addition & 1 deletion smt/surrogate_models/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
TO DO:
- define outputs['sol'] = self.sol
"""

import numpy as np

from sklearn import linear_model
Expand All @@ -15,7 +16,6 @@


class LS(SurrogateModel):

"""
Least square model.
This model uses the linear_model.LinearRegression class from scikit-learn.
Expand Down
2 changes: 1 addition & 1 deletion smt/surrogate_models/qp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
TO DO:
- define outputs['sol'] = self.sol
"""

import numpy as np
import scipy
from smt.surrogate_models.surrogate_model import SurrogateModel
Expand All @@ -15,7 +16,6 @@


class QP(SurrogateModel):

"""
Square polynomial approach
"""
Expand Down
2 changes: 1 addition & 1 deletion smt/surrogate_models/rbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This package is distributed under New BSD license.
"""

import numpy as np
from scipy.sparse import csc_matrix
from smt.surrogate_models.surrogate_model import SurrogateModel
Expand All @@ -14,7 +15,6 @@


class RBF(SurrogateModel):

"""
Radial basis function interpolant with global polynomial trend.
"""
Expand Down
1 change: 1 addition & 0 deletions smt/surrogate_models/rmtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This package is distributed under New BSD license.
"""

import numpy as np
import scipy.sparse
from numbers import Integral
Expand Down
1 change: 1 addition & 0 deletions smt/surrogate_models/rmtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This package is distributed under New BSD license.
"""

import numpy as np
import scipy.sparse
from numbers import Integral
Expand Down
Loading

0 comments on commit be831f5

Please sign in to comment.