Skip to content

Commit

Permalink
Use ruff as formatter and linter (SMTorg#515)
Browse files Browse the repository at this point in the history
* Use __all__ in __init__.py

* Remove py2 import

* Ignore check F821: undefined name

* Ruff --fix in sampling_methods

* Rust --fix in surrogate_models

* Remove unused import

* Ruff --fix in utils

* Remove unused import

* Remove unused variable

* Ignore variable name

* Ruff --fix

* Ignore ruff cache directories

* Fix bare except (E722)

* Remove OLD_SKLEARN

* Remove E712

* Remove E741

* Fix import for MFK examples in doc

* Remove F841

* Remove E731

* Remove E721

* Remove E711

* Remove F811

* Remove some F401

* Use ruff in CI

* Fix indentation

* Uppercase COMPILED_AVAILABLE

* Remove some F401

* Remove F401

* Remove E402

* Adjust dev docs

* Fix ruff link
  • Loading branch information
relf authored Feb 21, 2024
1 parent 2785147 commit 006694e
Show file tree
Hide file tree
Showing 80 changed files with 588 additions and 747 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/tests_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ jobs:
pip list
pip install -e .
- name: Check format with black
- name: Check with ruff
run: |
black --version
black --diff smt
black --check smt
ruff --version
ruff check .
- name: Test with pytest and coverage
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ smt/src/rmts/rmtsclib.cpp
/venv
/pip-wheel-metadata
**/.ipynb_checkpoints
**/.ruff_cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Tests](https://github.com/SMTOrg/smt/workflows/Tests/badge.svg)](https://github.com/SMTorg/smt/actions?query=workflow%3ATests)
[![Coverage Status](https://coveralls.io/repos/github/SMTorg/smt/badge.svg?branch=master)](https://coveralls.io/github/SMTorg/smt?branch=master)
[![Documentation Status](https://readthedocs.org/projects/smt/badge/?version=latest)](https://smt.readthedocs.io/en/latest/?badge=latest)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

The surrogate modeling toolbox (SMT) is a Python package that contains a collection of surrogate modeling methods, sampling techniques, and benchmarking functions. This package provides a library of surrogate models that is simple to use and facilitates the implementation of additional methods.

Expand Down
3 changes: 2 additions & 1 deletion doc/_src_docs/dev_docs.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion doc/_src_docs/dev_docs.rstx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Contributing to SMT consists of the following steps:
- Clone *your* SMT repo and install in development mode: go in your local smt folder and run ``pip install -e .``
- Write the class following the developer API given in the section below, and add it to the right folder, e.g., in ``smt/surrogate_models/``.
- Add the import statement in the corresponding ``__init__.py`` file, e.g., ``smt/surrogate_models/__init__.py``.
- Add tests to the top-level ``tests`` directory following the existing examples and run tests (see `Testing`_ section below)
- Add tests to the top-level ``tests`` directory following the existing examples and run tests (see `Testing`_ section below).
- Format and lint your code with `ruff <https://github.com/astral-sh/ruff>`_.
- Add a documentation page in the appropriate directory, e.g., ``doc/_src_docs/surrogate_models/rbf.rstx``, using the existing docs as a reference (see `Building the documentation`_ section below).
- Add an entry in the table of contents so that readers can find the documentation page, e.g., in ``doc/_src_docs/surrogate_model.rstx``.
- Test and commit the changes, push to the forked version of SMT and issue a pull request for review and comments from the other developers of SMT and the larger community
Expand Down
4 changes: 2 additions & 2 deletions doc/embed_directives/directive_embed_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def run(self, file_dir, file_name, embed_num_indent, args):
globals(),
)

exec("obj = {}({})".format(class_name, params), globals())
exec("obj = {}({})".format(class_name, params), globals()) # noqa: F821

options = getattr(obj, attribute_name)
options = getattr(obj, attribute_name) # noqa: F821

outputs = []
for option_name, option_data in options._declared_entries.items():
Expand Down
2 changes: 1 addition & 1 deletion doc/preprocess_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def process_options(root, file_name, iline, line):
exec("from smt.{} import {}".format(type_, class_name), globals())
exec("sm_class = {}".format(class_name), globals())

sm = sm_class()
sm = sm_class() # noqa: F821
options = sm.options

outputs = []
Expand Down
8 changes: 3 additions & 5 deletions doc/preprocess_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
This package is distributed under New BSD license.
"""

import os, sys
import os
import sys
import inspect
import importlib
import contextlib

try:
from StringIO import StringIO
except:
from io import StringIO
from io import StringIO

import matplotlib

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[build-system]
requires = ["setuptools", "wheel", "numpy", "Cython"]
requires = ["setuptools", "wheel", "numpy", "Cython"]

[tool.ruff.lint]
ignore = []
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ numba # JIT compiler
matplotlib # used in examples and tests
pytest # tests runner
pytest-xdist # allows running parallel testing with pytest -n <num_workers>
black # check code format
ruff # format and lint code
ConfigSpace~=0.6.1
12 changes: 12 additions & 0 deletions smt/applications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@
from .mfkpls import MFKPLS
from .mfkplsk import MFKPLSK
from .ego import EGO, Evaluator

__all__ = [
"VFM",
"MOE",
"MOESurrogateModel",
"MFK",
"NestedLHS",
"MFKPLS",
"MFKPLSK",
"EGO",
"Evaluator",
]
8 changes: 4 additions & 4 deletions smt/applications/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
try:
from smt.surrogate_models import IDW, RBF, RMTC, RMTB

compiled_available = True
except:
compiled_available = False
COMPILED_AVAILABLE = True
except ImportError:
COMPILED_AVAILABLE = False


class SurrogateBasedApplication:
if compiled_available:
if COMPILED_AVAILABLE:
_surrogate_type = {
"KRG": KRG,
"LS": LS,
Expand Down
10 changes: 4 additions & 6 deletions smt/applications/ego.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
from smt.utils.design_space import (
BaseDesignSpace,
DesignSpace,
FloatVariable,
CategoricalVariable,
)
from smt.sampling_methods import LHS

Expand Down Expand Up @@ -342,10 +340,10 @@ def _find_best_point(self, x_data=None, y_data=None, enable_tunneling=False):
cons = []
for j in range(len(bounds)):
lower, upper = bounds[j]
l = {"type": "ineq", "fun": lambda x, lb=lower, i=j: x[i] - lb}
u = {"type": "ineq", "fun": lambda x, ub=upper, i=j: ub - x[i]}
cons.append(l)
cons.append(u)
lo = {"type": "ineq", "fun": lambda x, lb=lower, i=j: x[i] - lb}
up = {"type": "ineq", "fun": lambda x, ub=upper, i=j: ub - x[i]}
cons.append(lo)
cons.append(up)
bounds = None
options = {"maxiter": 300}
else:
Expand Down
25 changes: 11 additions & 14 deletions smt/applications/mfk.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __call__(self, nb_samples_hifi):

if len(nt) != self.nlevel:
raise ValueError("nt must be a list of nlevel elements")
if np.allclose(np.sort(nt)[::-1], nt) == False:
if not np.allclose(np.sort(nt)[::-1], nt):
raise ValueError("nt must be a list of decreasing integers")

doe = []
Expand Down Expand Up @@ -131,13 +131,13 @@ def __call__(self, nb_samples_hifi):
for j in range(doe[i].shape[0]):
dj = np.sort(d[j, :])
k = dj[0]
l = (np.where(d[j, :] == k))[0][0]
ll = (np.where(d[j, :] == k))[0][0]
m = 0
while l in ind:
while ll in ind:
m = m + 1
k = dj[m]
l = (np.where(d[j, :] == k))[0][0]
ind.append(l)
ll = (np.where(d[j, :] == k))[0][0]
ind.append(ll)

doe[i - 1] = np.delete(doe[i - 1], ind, axis=0)
doe[i - 1] = np.vstack((doe[i - 1], doe[i]))
Expand Down Expand Up @@ -190,13 +190,13 @@ def _check_list_structure(self, X, y):
y : same as X
"""

if type(X) is not list:
if not isinstance(X, list):
nlevel = 1
X = [X]
else:
nlevel = len(X)

if type(y) is not list:
if not isinstance(y, list):
y = [y]

if len(X) != len(y):
Expand Down Expand Up @@ -759,9 +759,7 @@ def predict_variances_all_levels(self, X, is_acting=None):
sigma2 = self.optimal_par[0]["sigma2"] / self.y_std**2
MSE[:, 0] = sigma2 * (
# 1 + self.optimal_noise_all[0] - (r_t ** 2).sum(axis=0) + (u_ ** 2).sum(axis=0)
1
- (r_t**2).sum(axis=0)
+ (u_**2).sum(axis=0)
1 - (r_t**2).sum(axis=0) + (u_**2).sum(axis=0)
)

# Calculate recursively kriging variance at level i
Expand Down Expand Up @@ -845,17 +843,16 @@ def predict_variances_all_levels(self, X, is_acting=None):
Q_ = (np.dot((yt - np.dot(Ft, beta)).T, yt - np.dot(Ft, beta)))[0, 0]
MSE[:, i] = (
# sigma2_rho * MSE[:, i - 1]
+Q_ / (2 * (self.nt_all[i] - p - q))
+Q_
/ (2 * (self.nt_all[i] - p - q))
# * (1 + self.optimal_noise_all[i] - (r_t ** 2).sum(axis=0))
* (1 - (r_t**2).sum(axis=0))
+ sigma2 * (u_**2).sum(axis=0)
)
else:
MSE[:, i] = sigma2 * (
# 1 + self.optimal_noise_all[i] - (r_t ** 2).sum(axis=0) + (u_ ** 2).sum(axis=0)
1
- (r_t**2).sum(axis=0)
+ (u_**2).sum(axis=0)
1 - (r_t**2).sum(axis=0) + (u_**2).sum(axis=0)
) # + sigma2_rho * MSE[:, i - 1]
if self.options["propagate_uncertainty"]:
MSE[:, i] = MSE[:, i] + sigma2_rho * MSE[:, i - 1]
Expand Down
17 changes: 3 additions & 14 deletions smt/applications/moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@
import numpy as np
import warnings

OLD_SKLEARN = False
try: # scikit-learn < 0.20.0
from sklearn.mixture import GMM as GaussianMixture

OLD_SKLEARN = True
except:
from sklearn.mixture import GaussianMixture
from sklearn.mixture import GaussianMixture
from scipy.stats import multivariate_normal

from smt.utils.options_dictionary import OptionsDictionary
from smt.applications.application import SurrogateBasedApplication
from smt.utils.misc import compute_rms_error
from smt.surrogate_models.surrogate_model import SurrogateModel

warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand Down Expand Up @@ -246,7 +238,7 @@ def train(self):

self.ndim = nx = x.shape[1]
xt = self._training_values[:, 0:nx]
yt = self._training_values[:, nx : nx + 1]
_yt = self._training_values[:, nx : nx + 1]
ct = self._training_values[:, nx + 1 :]

# Clustering
Expand Down Expand Up @@ -647,10 +639,7 @@ def _create_clusters_distributions(self, heaviside_factor=1.0):
distribs = []
dim = self.ndim
means = self.cluster.means_
if OLD_SKLEARN:
cov = heaviside_factor * self.cluster.covars_
else:
cov = heaviside_factor * self.cluster.covariances_
cov = heaviside_factor * self.cluster.covariances_
for k in range(self.n_clusters):
meansk = means[k][0:dim]
covk = cov[k][0:dim, 0:dim]
Expand Down
37 changes: 15 additions & 22 deletions smt/applications/tests/test_ego.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@

import warnings

warnings.filterwarnings("ignore")

import os
import unittest
import numpy as np
from sys import argv, platform

try:
import matplotlib
from sys import argv

matplotlib.use("Agg")
NO_MATPLOTLIB = False
except:
NO_MATPLOTLIB = True

from smt.applications import EGO
from smt.applications.ego import Evaluator
Expand All @@ -45,6 +36,16 @@
)
import smt.utils.design_space as ds

warnings.filterwarnings("ignore")

try:
import matplotlib

matplotlib.use("Agg")
NO_MATPLOTLIB = False
except ImportError:
NO_MATPLOTLIB = True


# This implementation only works with Python > 3.3
class ParallelEvaluator(Evaluator):
Expand Down Expand Up @@ -354,8 +355,6 @@ def test_branin_2D_mixed_tunnel(self):

@staticmethod
def function_test_mixed_integer(X):
import numpy as np

# float
x1 = X[:, 0]
# XType.ENUM 1
Expand Down Expand Up @@ -917,7 +916,6 @@ def f_obj(X):
point to evaluate
"""
PI = 3.14159265358979323846
fail = False
x = X[:, 0]
# categorial variable
c = X[:, 1]
Expand Down Expand Up @@ -969,7 +967,6 @@ def f_obj(X):
else:
print("type error")
print(X)
fail = True
return y

# To define the variables x^{quant} and x^{cat}
Expand Down Expand Up @@ -1166,10 +1163,7 @@ def function_test_1d(x):
def run_ego_mixed_integer_example():
import numpy as np
from smt.applications import EGO
from smt.applications.mixed_integer import (
MixedIntegerContext,
MixedIntegerSamplingMethod,
)
from smt.applications.mixed_integer import MixedIntegerContext
from smt.surrogate_models import MixIntKernelType
from smt.utils.design_space import (
DesignSpace,
Expand All @@ -1179,7 +1173,6 @@ def run_ego_mixed_integer_example():
)
import matplotlib.pyplot as plt
from smt.surrogate_models import KRG
from smt.sampling_methods import LHS

# Regarding the interface, the function to be optimized should handle
# categorical values as index values in the enumeration type specification.
Expand Down Expand Up @@ -1252,12 +1245,12 @@ def function_test_mixed_integer(X):
for k in range(n_iter):
mini[k] = np.log(np.abs(np.min(y_data[0 : k + n_doe - 1]) - min_ref))
x_plot = np.linspace(1, n_iter + 0.5, n_iter)
u = max(np.floor(max(mini)) + 1, -100)
l = max(np.floor(min(mini)) - 0.2, -10)
up = max(np.floor(max(mini)) + 1, -100)
lo = max(np.floor(min(mini)) - 0.2, -10)
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot(x_plot, mini, color="r")
axes.set_ylim([l, u])
axes.set_ylim([lo, up])
plt.title("minimum convergence plot", loc="center")
plt.xlabel("number of iterations")
plt.ylabel("log of the difference w.r.t the best")
Expand Down
Loading

0 comments on commit 006694e

Please sign in to comment.