Skip to content

Commit bce1ee3

Browse files
authored
Black formatter (#278)
1 parent bc73ba4 commit bce1ee3

45 files changed

Lines changed: 1117 additions & 806 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/source/installation.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ EZyRB requires:
1111
- scipy
1212
- matplotlib
1313
- scikit-learn
14-
- torch (for neural network-based methods)
1514

1615
Install via pip
1716
---------------

ezyrb/__init__.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
"""EZyRB package"""
22

33
__all__ = [
4-
'Database', 'Snapshot', 'Reduction', 'POD', 'Approximation', 'RBF',
5-
'Linear', 'GPR', 'ANN', 'KNeighborsRegressor',
6-
'RadiusNeighborsRegressor', 'AE', 'ReducedOrderModel', 'PODAE',
7-
'RegularGrid', 'MultiReducedOrderModel', 'SklearnApproximation',
8-
'SklearnReduction'
4+
"Database",
5+
"Snapshot",
6+
"Reduction",
7+
"POD",
8+
"Approximation",
9+
"RBF",
10+
"Linear",
11+
"GPR",
12+
"ANN",
13+
"KNeighborsRegressor",
14+
"RadiusNeighborsRegressor",
15+
"AE",
16+
"ReducedOrderModel",
17+
"PODAE",
18+
"RegularGrid",
19+
"MultiReducedOrderModel",
20+
"SklearnApproximation",
21+
"SklearnReduction",
922
]
1023

1124
from .database import Database

ezyrb/approximation/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
"""EZyRB package"""
22

33
__all__ = [
4-
'Approximation', 'RBF', 'Linear', 'GPR',
5-
'ANN', 'KNeighborsRegressor', 'RadiusNeighborsRegressor',
6-
'SklearnApproximation'
4+
"Approximation",
5+
"RBF",
6+
"Linear",
7+
"GPR",
8+
"ANN",
9+
"KNeighborsRegressor",
10+
"RadiusNeighborsRegressor",
11+
"SklearnApproximation",
712
]
813

914
from .approximation import Approximation

ezyrb/approximation/ann.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ class ANN(Approximation):
4646
>>> print(y_pred)
4747
>>> print(len(ann.loss_trend))
4848
>>> print(ann.loss_trend[-1])
49+
50+
.. note::
51+
This module provides a wrapper around sklearn's MLPRegressor for
52+
multidimensional function approximation using feed-forward neural
53+
networks. It is not intended for deep learning tasks, but rather for
54+
approximating functions based on given data points. For more advanced
55+
deep learning applications, consider using dedicated libraries such as
56+
:ref:`PINA`.
4957
"""
58+
5059
def __init__(
5160
self,
5261
layers,

ezyrb/approximation/approximation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Approximation(ABC):
1010
All the classes that implement the input-output mapping should be inherited
1111
from this class.
1212
"""
13+
1314
@abstractmethod
1415
def fit(self, points, values):
1516
"""Abstract `fit`"""

ezyrb/approximation/gpr.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Module wrapper exploiting `GPy` for Gaussian Process Regression
33
"""
4+
45
import logging
56
import numpy as np
67
from scipy.optimize import minimize
@@ -40,19 +41,24 @@ class GPR(Approximation):
4041
>>> print(np.allclose(y, y_pred))
4142
4243
"""
44+
4345
def __init__(self, kern=None, normalizer=True, optimization_restart=20):
4446
"""
4547
Initialize a Gaussian Process Regressor.
46-
48+
4749
:param kern: Kernel object from sklearn. Default is None.
4850
:param bool normalizer: Whether to normalize values. Default is True.
4951
:param int optimization_restart: Number of restarts for optimization.
5052
Default is 20.
5153
"""
5254

53-
logger.debug("Initializing GPR with kernel=%s, normalizer=%s, "
54-
"optimization_restart=%d",
55-
kern, normalizer, optimization_restart)
55+
logger.debug(
56+
"Initializing GPR with kernel=%s, normalizer=%s, "
57+
"optimization_restart=%d",
58+
kern,
59+
normalizer,
60+
optimization_restart,
61+
)
5662
self.X_sample = None
5763
self.Y_sample = None
5864
self.kern = kern
@@ -67,8 +73,11 @@ def fit(self, points, values):
6773
:param array_like points: the coordinates of the points.
6874
:param array_like values: the values in the points.
6975
"""
70-
logger.debug("Fitting GPR with points shape: %s, values shape: %s",
71-
np.array(points).shape, np.array(values).shape)
76+
logger.debug(
77+
"Fitting GPR with points shape: %s, values shape: %s",
78+
np.array(points).shape,
79+
np.array(values).shape,
80+
)
7281
self.X_sample = np.array(points)
7382
self.Y_sample = np.array(values)
7483
if self.X_sample.ndim == 1:
@@ -80,8 +89,10 @@ def fit(self, points, values):
8089

8190
logger.debug("Creating GaussianProcessRegressor")
8291
self.model = GaussianProcessRegressor(
83-
kernel=self.kern, n_restarts_optimizer=self.optimization_restart,
84-
normalize_y=self.normalizer)
92+
kernel=self.kern,
93+
n_restarts_optimizer=self.optimization_restart,
94+
normalize_y=self.normalizer,
95+
)
8596
self.model.fit(self.X_sample, self.Y_sample)
8697
logger.info("GPR fitted successfully")
8798

@@ -118,14 +129,14 @@ def optimal_mu(self, bounds, optimization_restart=10):
118129
def min_obj(X):
119130
return -1 * np.linalg.norm(self.predict(X.reshape(1, -1), True)[1])
120131

121-
initial_starts = np.random.uniform(bounds[:, 0],
122-
bounds[:, 1],
123-
size=(optimization_restart, dim))
132+
initial_starts = np.random.uniform(
133+
bounds[:, 0], bounds[:, 1], size=(optimization_restart, dim)
134+
)
124135

125136
# Find the best optimum by starting from n_restart different random
126137
# points.
127138
for x0 in initial_starts:
128-
res = minimize(min_obj, x0, bounds=bounds, method='L-BFGS-B')
139+
res = minimize(min_obj, x0, bounds=bounds, method="L-BFGS-B")
129140

130141
if res.fun < min_val:
131142
min_val = res.fun

ezyrb/approximation/kneighbors_regressor.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class KNeighborsRegressor(NeighborsRegressor):
1414
1515
:param kwargs: arguments passed to the internal instance of
1616
KNeighborsRegressor.
17-
17+
1818
:Example:
19-
19+
2020
>>> import numpy as np
2121
>>> from ezyrb import KNeighborsRegressor
2222
>>> x = np.random.uniform(-1, 1, size=(20, 2))
@@ -26,12 +26,14 @@ class KNeighborsRegressor(NeighborsRegressor):
2626
>>> new_x = np.array([[0.5, 0.5]])
2727
>>> y_pred = knn.predict(new_x)
2828
"""
29+
2930
def __init__(self, **kwargs):
3031
"""
3132
Initialize a K-Neighbors Regressor.
32-
33+
3334
:param kwargs: Arguments passed to sklearn's KNeighborsRegressor.
3435
"""
35-
logger.debug("Initializing KNeighborsRegressor with kwargs: %s",
36-
kwargs)
36+
logger.debug(
37+
"Initializing KNeighborsRegressor with kwargs: %s", kwargs
38+
)
3739
self.regressor = Regressor(**kwargs)

ezyrb/approximation/linear.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ class Linear(Approximation):
2020
of the convex hull of the input points. If not provided, then the
2121
default is numpy.nan.
2222
"""
23+
2324
def __init__(self, fill_value=np.nan):
2425
"""
2526
Initialize a Linear interpolator.
26-
27+
2728
:param float fill_value: Value for points outside the convex hull.
2829
Default is numpy.nan.
2930
"""
@@ -38,33 +39,41 @@ def fit(self, points, values):
3839
:param array_like points: the coordinates of the points.
3940
:param array_like values: the values in the points.
4041
"""
41-
logger.debug("Fitting Linear with points shape: %s, "
42-
"values shape: %s",
43-
np.array(points).shape, np.array(values).shape)
42+
logger.debug(
43+
"Fitting Linear with points shape: %s, " "values shape: %s",
44+
np.array(points).shape,
45+
np.array(values).shape,
46+
)
4447
# the first dimension is the list of parameters, the second one is
4548
# the dimensionality of each tuple of parameters (we look for
4649
# parameters of dimensionality one)
4750
as_np_array = np.array(points)
4851
if not np.issubdtype(as_np_array.dtype, np.number):
4952
logger.error("Invalid points format/dimension")
50-
raise ValueError('Invalid format or dimension for the argument'
51-
'`points`.')
53+
raise ValueError(
54+
"Invalid format or dimension for the argument" "`points`."
55+
)
5256

5357
if as_np_array.shape[-1] == 1:
5458
as_np_array = np.squeeze(as_np_array, axis=-1)
5559
logger.debug("Squeezed points array")
5660

57-
if as_np_array.ndim == 1 or (as_np_array.ndim == 2
58-
and as_np_array.shape[1] == 1):
61+
if as_np_array.ndim == 1 or (
62+
as_np_array.ndim == 2 and as_np_array.shape[1] == 1
63+
):
5964
logger.debug("Using 1D interpolation")
60-
self.interpolator = interp1d(as_np_array, values, axis=0,
61-
bounds_error=False,
62-
fill_value=self.fill_value)
65+
self.interpolator = interp1d(
66+
as_np_array,
67+
values,
68+
axis=0,
69+
bounds_error=False,
70+
fill_value=self.fill_value,
71+
)
6372
else:
6473
logger.debug("Using ND interpolation")
65-
self.interpolator = LinearNDInterp(points,
66-
values,
67-
fill_value=self.fill_value)
74+
self.interpolator = LinearNDInterp(
75+
points, values, fill_value=self.fill_value
76+
)
6877
logger.info("Linear fitted successfully")
6978

7079
def predict(self, new_point):

ezyrb/approximation/neighbors_regressor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
class NeighborsRegressor(Approximation):
88
"""
99
A generic superclass for wrappers of *NeighborsRegressor from sklearn.
10-
10+
1111
This class provides a common interface for neighbor-based regression methods.
1212
1313
:param kwargs: Arguments passed to the internal instance of
1414
*NeighborsRegressor.
15-
15+
1616
:Example:
17-
17+
1818
>>> import numpy as np
1919
>>> from ezyrb import KNeighborsRegressor
2020
>>> x = np.random.uniform(-1, 1, size=(20, 2))
@@ -23,6 +23,7 @@ class NeighborsRegressor(Approximation):
2323
>>> knn.fit(x, y)
2424
>>> y_pred = knn.predict(x[:5])
2525
"""
26+
2627
def fit(self, points, values):
2728
"""
2829
Construct the interpolator given `points` and `values`.

ezyrb/approximation/radius_neighbors_regressor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class RadiusNeighborsRegressor(NeighborsRegressor):
1111
1212
:param kwargs: arguments passed to the internal instance of
1313
RadiusNeighborsRegressor.
14-
14+
1515
:Example:
16-
16+
1717
>>> import numpy as np
1818
>>> from ezyrb import RadiusNeighborsRegressor
1919
>>> x = np.random.uniform(-1, 1, size=(20, 2))
@@ -23,10 +23,11 @@ class RadiusNeighborsRegressor(NeighborsRegressor):
2323
>>> new_x = np.array([[0.0, 0.0]])
2424
>>> y_pred = rnn.predict(new_x)
2525
"""
26+
2627
def __init__(self, **kwargs):
2728
"""
2829
Initialize a Radius Neighbors Regressor.
29-
30+
3031
:param kwargs: Arguments passed to sklearn's RadiusNeighborsRegressor.
3132
"""
3233
self.regressor = Regressor(**kwargs)

0 commit comments

Comments
 (0)