Skip to content

Commit

Permalink
lint: fix pydocstyle errors
Browse files Browse the repository at this point in the history
To reproduce the errors:

```
ruff check --select "D" black_it tests examples scripts
```

Most of the errors have been fixed using the '--fix' flag.
  • Loading branch information
marcofavorito authored and marcofavoritobi committed Sep 1, 2023
1 parent 7568789 commit 4c929b3
Show file tree
Hide file tree
Showing 42 changed files with 147 additions and 254 deletions.
21 changes: 7 additions & 14 deletions black_it/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def __init__(
random_state: Optional[int] = None,
n_jobs: Optional[int] = None,
):
"""
Initialize the Calibrator object.
"""Initialize the Calibrator object.
It must be initialized with details on the parameters to explore,
on the model to calibrate, on the samplers and on the loss function to use.
Expand Down Expand Up @@ -175,8 +174,7 @@ def _set_samplers_seeds(self) -> None:

@staticmethod
def _construct_samplers_id_table(samplers: List[BaseSampler]) -> Dict[str, int]:
"""
Construct the samplers-by-id table.
"""Construct the samplers-by-id table.
Given the list (built-in or user-defined) of samplers a calibration
session is going to use, return a map from the sampler human-readable
Expand Down Expand Up @@ -239,8 +237,7 @@ def update_samplers_id_table(self, samplers: Sequence[BaseSampler]) -> None:
def restore_from_checkpoint(
cls, checkpoint_path: str, model: Callable
) -> "Calibrator":
"""
Return an instantiated class from a database file and a model simulator.
"""Return an instantiated class from a database file and a model simulator.
Args:
checkpoint_path: the name of the database file to read from
Expand Down Expand Up @@ -312,8 +309,7 @@ def restore_from_checkpoint(
return calibrator

def simulate_model(self, params: NDArray) -> NDArray:
"""
Simulate the model.
"""Simulate the model.
This method calls the model simulator in parallel on a given set of parameter values, a number of repeated
evaluations are performed for each parameter to average out random fluctuations.
Expand Down Expand Up @@ -341,8 +337,7 @@ def simulate_model(self, params: NDArray) -> NDArray:
return simulated_data

def calibrate(self, n_batches: int) -> Tuple[NDArray, NDArray]:
"""
Run calibration for n batches.
"""Run calibration for n batches.
Args:
n_batches (int): number of 'batches' to be executed. Each batch runs over all methods
Expand Down Expand Up @@ -464,8 +459,7 @@ def calibrate(self, n_batches: int) -> Tuple[NDArray, NDArray]:
def check_convergence(
losses_samp: NDArray, n_sampled_params: int, convergence_precision: int
) -> bool:
"""
Check convergence of the calibration.
"""Check convergence of the calibration.
Args:
losses_samp: the sampled losses
Expand All @@ -482,8 +476,7 @@ def check_convergence(
return converged

def create_checkpoint(self, file_name: Union[str, os.PathLike]) -> None:
"""
Save the current state of the object.
"""Save the current state of the object.
Args:
file_name: the name of the folder where the data will be saved
Expand Down
9 changes: 3 additions & 6 deletions black_it/loss_functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def __init__(
coordinate_weights: Optional[NDArray] = None,
coordinate_filters: Optional[List[Optional[Callable]]] = None,
):
"""
Initialize the loss function.
"""Initialize the loss function.
Args:
coordinate_weights: the weights of the loss coordinates.
Expand All @@ -50,8 +49,7 @@ def __init__(
def compute_loss(
self, sim_data_ensemble: NDArray[np.float64], real_data: NDArray[np.float64]
) -> float:
"""
Compute the loss between simulated and real data.
"""Compute the loss between simulated and real data.
Args:
sim_data_ensemble: an ensemble of simulated data, of shape (ensemble_size, N, D)
Expand Down Expand Up @@ -140,8 +138,7 @@ def _check_coordinate_filters(self, num_coords: int) -> List[Optional[Callable]]
def compute_loss_1d(
self, sim_data_ensemble: NDArray[np.float64], real_data: NDArray[np.float64]
) -> float:
"""
Return the loss between a specific coordinate of two time series.
"""Return the loss between a specific coordinate of two time series.
Concrete classes have to override this method in order to implement new
loss functions.
Expand Down
24 changes: 8 additions & 16 deletions black_it/loss_functions/gsl_div.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
This module contains the GSL-div implementation.
"""This module contains the GSL-div implementation.
- Lamperti, F. (2018).
An information theoretic criterion for empirical validation of simulation models.
Expand Down Expand Up @@ -64,8 +63,7 @@


class GslDivLoss(BaseLoss):
"""
Class for the Gsl-div loss.
"""Class for the Gsl-div loss.
Example:
>>> expected_loss = 0.39737637181336855
Expand All @@ -84,8 +82,7 @@ def __init__(
coordinate_weights: Optional[NDArray] = None,
coordinate_filters: Optional[List[Optional[Callable]]] = None,
) -> None:
"""
Initialize the GSL-div loss object.
"""Initialize the GSL-div loss object.
Args:
nb_values: number of values the digitised series can take
Expand All @@ -101,8 +98,7 @@ def __init__(
def compute_loss_1d(
self, sim_data_ensemble: NDArray[np.float64], real_data: NDArray[np.float64]
) -> float:
"""
Return the GSL-div measure.
"""Return the GSL-div measure.
From (Lamperti, 2017):
Expand Down Expand Up @@ -213,8 +209,7 @@ def discretize(
start_index: Union[np.float64, float],
stop_index: Union[np.float64, float],
) -> NDArray[np.int64]:
"""
Discretize the TS in 'nb_values' finite states.
"""Discretize the TS in 'nb_values' finite states.
>>> GslDivLoss.discretize(
... [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
Expand Down Expand Up @@ -242,8 +237,7 @@ def discretize(

@staticmethod
def get_words(time_series: NDArray[np.float64], length: int) -> NDArray:
"""
Return an overlapping array of words (int32) of 'length' given a discretised vector.
"""Return an overlapping array of words (int32) of 'length' given a discretised vector.
>>> GslDivLoss.get_words(np.asarray([1, 2, 2, 2]), 2)
array([12, 22, 22])
Expand Down Expand Up @@ -273,8 +267,7 @@ def get_words(time_series: NDArray[np.float64], length: int) -> NDArray:

@staticmethod
def get_words_est_prob(time_series: NDArray[np.float64]) -> NDArray[np.float64]:
"""
Return an array of estimated probabilities given an array of words (int32).
"""Return an array of estimated probabilities given an array of words (int32).
Args:
time_series: any univariate array of words
Expand All @@ -288,8 +281,7 @@ def get_words_est_prob(time_series: NDArray[np.float64]) -> NDArray[np.float64]:

@staticmethod
def get_sh_entr(probs: NDArray[np.float64], log_base: float) -> float:
"""
Return the Shannon entropy given an array of probabilities.
"""Return the Shannon entropy given an array of probabilities.
Args:
probs: an array of probabilities describing the discrete probability distribution
Expand Down
6 changes: 2 additions & 4 deletions black_it/loss_functions/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def __init__(
coordinate_filters: Optional[List[Optional[Callable]]] = None,
h: Union[str, float] = "silverman",
):
"""
Initialize the loss function.
"""Initialize the loss function.
Args:
coordinate_weights: the weights of the loss coordinates.
Expand All @@ -71,8 +70,7 @@ def _get_bandwidth_scott(n: int, d: int) -> float:
def compute_loss(
self, sim_data_ensemble: NDArray[np.float64], real_data: NDArray[np.float64]
) -> float:
"""
Compute the loss between simulated and real data.
"""Compute the loss between simulated and real data.
Args:
sim_data_ensemble: an ensemble of simulated data, of shape (ensemble_size, N, D)
Expand Down
6 changes: 2 additions & 4 deletions black_it/loss_functions/minkowski.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def __init__(
coordinate_weights: Optional[NDArray] = None,
coordinate_filters: Optional[List[Optional[Callable]]] = None,
) -> None:
"""
Loss computed using a Minkowski distance.
"""Loss computed using a Minkowski distance.
The [Minkowski distance](https://en.wikipedia.org/wiki/Minkowski_distance)
is a generalization of both the Manhattan distance (p=1) and the Euclidean distance (p=2).
Expand All @@ -55,8 +54,7 @@ def __init__(
def compute_loss_1d(
self, sim_data_ensemble: NDArray[np.float64], real_data: NDArray[np.float64]
) -> float:
"""
Call scipy.spatial.distance.minkowski() on its arguments.
"""Call scipy.spatial.distance.minkowski() on its arguments.
Args:
sim_data_ensemble: the first operand
Expand Down
12 changes: 4 additions & 8 deletions black_it/loss_functions/msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Method-of-moments loss function.
"""Method-of-moments loss function.
This module contains the implementation of the loss function
based on the 'method of moments'.
Expand Down Expand Up @@ -59,8 +58,7 @@ def __init__(
coordinate_filters: Optional[List[Optional[Callable]]] = None,
standardise_moments: bool = False,
):
"""
Initialize the loss function based on the 'method of moments'.
"""Initialize the loss function based on the 'method of moments'.
Returns the MSM objective function, i.e. the square difference between
the moments of the two time series.
Expand Down Expand Up @@ -101,8 +99,7 @@ def _validate_covariance_and_calculator(
moment_calculator: MomentCalculator,
covariance_mat: Union[NDArray[np.float64], str],
) -> None:
"""
Validate the covariance matrix.
"""Validate the covariance matrix.
Args:
moment_calculator: the moment calculator
Expand Down Expand Up @@ -148,8 +145,7 @@ def _validate_covariance_and_calculator(
def compute_loss_1d(
self, sim_data_ensemble: NDArray[np.float64], real_data: NDArray[np.float64]
) -> float:
"""
Compute the loss based on the 'method of moments'.
"""Compute the loss based on the 'method of moments'.
Returns the MSM objective function, i.e. the square difference between the moments of the two time series.
Expand Down
3 changes: 1 addition & 2 deletions black_it/plot/plot_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ def plot_sampling_batch_nums(


def plot_sampling_interact(saving_folder: Union[str, os.PathLike]) -> None:
"""
Plot the parameter sampled colored according to the sampling method used to sample them.
"""Plot the parameter sampled colored according to the sampling method used to sample them.
The method allows to interactively choose the batch numbers included in the plot.
Expand Down
12 changes: 4 additions & 8 deletions black_it/samplers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@


class BaseSampler(BaseSeedable, ABC):
"""
BaseSampler interface.
"""BaseSampler interface.
This is the base class for all samplers.
"""
Expand All @@ -39,8 +38,7 @@ def __init__(
random_state: Optional[int] = None,
max_deduplication_passes: int = 5,
) -> None:
"""
Initialize the sampler.
"""Initialize the sampler.
Args:
batch_size: the number of points sampled every time the sampler is called
Expand All @@ -59,8 +57,7 @@ def sample_batch(
existing_points: NDArray[np.float64],
existing_losses: NDArray[np.float64],
) -> NDArray[np.float64]:
"""
Sample a number of new parameters fixed by the 'batch_size' attribute.
"""Sample a number of new parameters fixed by the 'batch_size' attribute.
Args:
batch_size: number of samples to collect
Expand All @@ -78,8 +75,7 @@ def sample(
existing_points: NDArray[np.float64],
existing_losses: NDArray[np.float64],
) -> NDArray[np.float64]:
"""
Sample from the search space.
"""Sample from the search space.
Args:
search_space: an object containing the details of the parameter search space
Expand Down
6 changes: 2 additions & 4 deletions black_it/samplers/best_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def __init__(
b: float = 1.0,
perturbation_range: int = 6,
):
"""
Initialize the sampler.
"""Initialize the sampler.
Args:
batch_size: the number of points sampled every time the sampler is called
Expand Down Expand Up @@ -84,8 +83,7 @@ def sample_batch(
existing_points: NDArray[np.float64],
existing_losses: NDArray[np.float64],
) -> NDArray[np.float64]:
"""
Sample from the search space using a genetic algorithm.
"""Sample from the search space using a genetic algorithm.
Args:
batch_size: the number of points to sample
Expand Down
13 changes: 5 additions & 8 deletions black_it/samplers/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Implementation the CORS sampler.
"""Implementation the CORS sampler.
Regis, Rommel G., and Christine A. Shoemaker. "Constrained global optimization of expensive black box functions
using radial basis functions." Journal of Global optimization 31.1 (2005): 153-171.
Regis, Rommel G., and Christine A. Shoemaker. "Constrained global optimization of expensive black box functions
using radial basis functions." Journal of Global optimization 31.1 (2005): 153-171.
"""
from math import factorial
Expand Down Expand Up @@ -63,8 +62,7 @@ def boxtocube(


def rbf(points: NDArray[np.float64], losses: NDArray[np.float64]) -> Callable:
"""
Build RBF-fit for given points (see Holmstrom, 2008 for details).
"""Build RBF-fit for given points (see Holmstrom, 2008 for details).
Implementation mostly taken from:
https://github.com/paulknysh/blackbox/blob/cd8baa0cc344f36b3fddf910ae8037f62009619c/black_box/blackbox.py#L148-L195
Expand Down Expand Up @@ -137,8 +135,7 @@ def __init__(
random_state: Optional[int] = None,
verbose: bool = False,
) -> None:
"""
Initialize the CORS sampler.
"""Initialize the CORS sampler.
Args:
batch_size: the batch size
Expand Down
Loading

0 comments on commit 4c929b3

Please sign in to comment.