Skip to content

Commit c09f880

Browse files
committed
Add missing docstrings, fix outdated ones
1 parent 9d130cd commit c09f880

20 files changed

+485
-48
lines changed

ezyrb/approximation/ann.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ class ANN(Approximation):
5050
def __init__(self, layers, function, stop_training, loss=None,
5151
optimizer=torch.optim.Adam, lr=0.001, l2_regularization=0,
5252
frequency_print=10, last_identity=True):
53+
"""
54+
Initialize an Artificial Neural Network.
55+
56+
:param list layers: Ordered list with the number of neurons of each hidden layer.
57+
:param function: Activation function(s) for each layer.
58+
:param stop_training: Stopping criteria for training (iterations and/or tolerance).
59+
:param loss: Loss function to use. Default is MSELoss.
60+
:param optimizer: Optimizer class to use. Default is Adam.
61+
:param float lr: Learning rate. Default is 0.001.
62+
:param float l2_regularization: L2 regularization coefficient. Default is 0.
63+
:param int frequency_print: Frequency of printing during training. Default is 10.
64+
:param bool last_identity: Whether the last activation is identity. Default is True.
65+
"""
5366
if loss is None:
5467
loss = torch.nn.MSELoss()
5568

@@ -121,15 +134,13 @@ def _list_to_sequential(layers, functions):
121134

122135
def _build_model(self, points, values):
123136
"""
124-
Build the torch model.
125-
Considering the number of neurons per layer (self.layers), a
126-
feed-forward NN is defined:
127-
- activation function from layer i>=0 to layer i+1:
128-
self.function[i]; activation function at the output layer:
129-
Identity (by default).
130-
:param numpy.ndarray points: the coordinates of the given (training)
131-
points.
132-
:param numpy.ndarray values: the (training) values in the points.
137+
Build the torch neural network model.
138+
139+
Constructs a feed-forward neural network with the specified layers
140+
and activation functions.
141+
142+
:param numpy.ndarray points: The coordinates of the training points.
143+
:param numpy.ndarray values: The training values at the points.
133144
"""
134145
layers = self.layers.copy()
135146
layers.insert(0, points.shape[1])

ezyrb/approximation/gpr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class GPR(Approximation):
3838
3939
"""
4040
def __init__(self, kern=None, normalizer=True, optimization_restart=20):
41+
"""
42+
Initialize a Gaussian Process Regressor.
43+
44+
:param kern: Kernel object from sklearn. Default is None.
45+
:param bool normalizer: Whether to normalize values. Default is True.
46+
:param int optimization_restart: Number of restarts for optimization.
47+
Default is 20.
48+
"""
4149

4250
self.X_sample = None
4351
self.Y_sample = None

ezyrb/approximation/kneighbors_regressor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@
77

88
class KNeighborsRegressor(NeighborsRegressor):
99
"""
10-
K-Neighbors Regressor.
10+
K-Neighbors Regressor for multidimensional approximation.
1111
1212
:param kwargs: arguments passed to the internal instance of
1313
KNeighborsRegressor.
14+
15+
:Example:
16+
17+
>>> import numpy as np
18+
>>> from ezyrb import KNeighborsRegressor
19+
>>> x = np.random.uniform(-1, 1, size=(20, 2))
20+
>>> y = np.array([np.sin(x[:, 0]), np.cos(x[:, 1])]).T
21+
>>> knn = KNeighborsRegressor(n_neighbors=3)
22+
>>> knn.fit(x, y)
23+
>>> new_x = np.array([[0.5, 0.5]])
24+
>>> y_pred = knn.predict(new_x)
1425
"""
1526
def __init__(self, **kwargs):
27+
"""
28+
Initialize a K-Neighbors Regressor.
29+
30+
:param kwargs: Arguments passed to sklearn's KNeighborsRegressor.
31+
"""
1632
self.regressor = Regressor(**kwargs)

ezyrb/approximation/linear.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class Linear(Approximation):
1818
default is numpy.nan.
1919
"""
2020
def __init__(self, fill_value=np.nan):
21+
"""
22+
Initialize a Linear interpolator.
23+
24+
:param float fill_value: Value for points outside the convex hull.
25+
Default is numpy.nan.
26+
"""
2127
self.fill_value = fill_value
2228
self.interpolator = None
2329

ezyrb/approximation/neighbors_regressor.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
class NeighborsRegressor(Approximation):
88
"""
99
A generic superclass for wrappers of *NeighborsRegressor from sklearn.
10+
11+
This class provides a common interface for neighbor-based regression methods.
1012
11-
:param kwargs: arguments passed to the internal instance of
13+
:param kwargs: Arguments passed to the internal instance of
1214
*NeighborsRegressor.
15+
16+
:Example:
17+
18+
>>> import numpy as np
19+
>>> from ezyrb import KNeighborsRegressor
20+
>>> x = np.random.uniform(-1, 1, size=(20, 2))
21+
>>> y = np.sin(x[:, 0]) + np.cos(x[:, 1])
22+
>>> knn = KNeighborsRegressor(n_neighbors=5)
23+
>>> knn.fit(x, y)
24+
>>> y_pred = knn.predict(x[:5])
1325
"""
1426
def fit(self, points, values):
1527
"""

ezyrb/approximation/radius_neighbors_regressor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@
77

88
class RadiusNeighborsRegressor(NeighborsRegressor):
99
"""
10-
Radius Neighbors Regressor.
10+
Radius Neighbors Regressor for multidimensional approximation.
1111
1212
:param kwargs: arguments passed to the internal instance of
1313
RadiusNeighborsRegressor.
14+
15+
:Example:
16+
17+
>>> import numpy as np
18+
>>> from ezyrb import RadiusNeighborsRegressor
19+
>>> x = np.random.uniform(-1, 1, size=(20, 2))
20+
>>> y = np.sin(x[:, 0]) * np.cos(x[:, 1])
21+
>>> rnn = RadiusNeighborsRegressor(radius=0.5)
22+
>>> rnn.fit(x, y)
23+
>>> new_x = np.array([[0.0, 0.0]])
24+
>>> y_pred = rnn.predict(new_x)
1425
"""
1526
def __init__(self, **kwargs):
27+
"""
28+
Initialize a Radius Neighbors Regressor.
29+
30+
:param kwargs: Arguments passed to sklearn's RadiusNeighborsRegressor.
31+
"""
1632
self.regressor = Regressor(**kwargs)

ezyrb/database.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Database():
99
"""
10-
Database class
10+
Database class for storing parameter-snapshot pairs.
1111
1212
:param array_like parameters: the input parameters
1313
:param array_like snapshots: the input snapshots
@@ -16,6 +16,20 @@ class Database():
1616
:param Scale scaler_snapshots: the scaler for the snapshots. Default is
1717
None meaning no scaling.
1818
:param array_like space: the input spatial data
19+
20+
:Example:
21+
22+
>>> import numpy as np
23+
>>> from ezyrb import Database, Parameter, Snapshot
24+
>>> params = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
25+
>>> snapshots = np.random.rand(3, 100)
26+
>>> db = Database(params, snapshots)
27+
>>> print(len(db))
28+
3
29+
>>> print(db.parameters_matrix.shape)
30+
(3, 2)
31+
>>> print(db.snapshots_matrix.shape)
32+
(3, 100)
1933
"""
2034
def __init__(self, parameters=None, snapshots=None, space=None):
2135
self._pairs = []

ezyrb/parameter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,33 @@
22
import numpy as np
33

44
class Parameter:
5+
"""
6+
Class for representing a parameter in the reduced order model.
7+
8+
This class encapsulates parameter values and provides validation
9+
to ensure parameters are 1-dimensional arrays.
10+
11+
:param array_like values: The parameter values as a 1D array.
12+
13+
:Example:
14+
15+
>>> import numpy as np
16+
>>> from ezyrb import Parameter
17+
>>> param = Parameter([1.0, 2.5, 3.0])
18+
>>> print(param.values)
19+
[1. 2.5 3. ]
20+
>>> param2 = Parameter(np.array([4.5, 5.5]))
21+
>>> print(param2.values)
22+
[4.5 5.5]
23+
"""
524

625
def __init__(self, values):
26+
"""
27+
Initialize a Parameter object.
28+
29+
:param array_like values: The parameter values. Can be a Parameter
30+
instance or an array-like object that can be converted to a 1D numpy array.
31+
"""
732
if isinstance(values, Parameter):
833
self.values = values.values
934
else:
@@ -16,6 +41,12 @@ def values(self):
1641

1742
@values.setter
1843
def values(self, new_values):
44+
"""
45+
Set the parameter values with validation.
46+
47+
:param array_like new_values: The new parameter values.
48+
:raises ValueError: If the new values are not a 1D array.
49+
"""
1950
if np.asarray(new_values).ndim != 1:
2051
raise ValueError('only 1D array are usable as parameter.')
2152

ezyrb/plugin/aggregation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class Aggregation(Plugin):
4242
"""
4343

4444
def __init__(self, fit_function=None, predict_function=Linear()):
45+
"""
46+
Initialize the Aggregation plugin.
47+
48+
:param fit_function: Regression model to fit weights in validation set.
49+
If None, uses standard space-dependent methods. Default is None.
50+
:param predict_function: Regression model to predict weights in test set.
51+
Default is Linear().
52+
"""
4553
super().__init__()
4654
self.fit_function = fit_function
4755
self.predict_function = predict_function

ezyrb/plugin/automatic_shift.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ class AutomaticShiftSnapshots(Plugin):
5050
"""
5151
def __init__(self, shift_network, interp_network, interpolator,
5252
parameter_index=0, reference_index=0, barycenter_loss=0):
53+
"""
54+
Initialize the AutomaticShiftSnapshots plugin.
55+
56+
:param shift_network: Neural network for learning the shift function.
57+
:param interp_network: Neural network for interpolation.
58+
:param Approximation interpolator: Interpolator for shifted snapshots evaluation.
59+
:param int parameter_index: Index of parameter component. Default is 0.
60+
:param int reference_index: Index of reference snapshot. Default is 0.
61+
:param float barycenter_loss: Weight for barycenter loss term. Default is 0.
62+
"""
5363
super().__init__()
5464

5565
self.interpolator = interpolator
@@ -61,6 +71,7 @@ def __init__(self, shift_network, interp_network, interpolator,
6171

6272
def _train_interp_network(self):
6373
"""
74+
Train the interpolation network on the reference snapshot.
6475
"""
6576
self.interp_network.fit(
6677
self.reference_snapshot.space.reshape(-1, 1),
@@ -69,6 +80,9 @@ def _train_interp_network(self):
6980

7081
def _train_shift_network(self, db):
7182
"""
83+
Train the shift network using the database snapshots.
84+
85+
:param Database db: The database containing snapshots.
7286
"""
7387
ref_center = torch.tensor(np.average(
7488
self.reference_snapshot.space * self.reference_snapshot.values))

0 commit comments

Comments
 (0)