From 01e724455d6a2502d96417ce282d92b295083fcf Mon Sep 17 00:00:00 2001 From: Aldo Date: Thu, 5 Sep 2024 11:56:28 +0200 Subject: [PATCH] lint: solve mypy linting errors --- black_it/calibrator.py | 8 +++++--- black_it/loss_functions/gsl_div.py | 2 +- black_it/loss_functions/likelihood.py | 12 ++++++++---- black_it/plot/plot_results.py | 4 +++- black_it/samplers/xgboost.py | 6 +++--- black_it/search_space.py | 2 +- examples/models/simple_models.py | 2 +- examples/models/sir/simlib.py | 4 ++-- tests/test_samplers/test_xgboost.py | 7 ++++--- 9 files changed, 28 insertions(+), 19 deletions(-) diff --git a/black_it/calibrator.py b/black_it/calibrator.py index 579a1648..419bd4a5 100644 --- a/black_it/calibrator.py +++ b/black_it/calibrator.py @@ -128,9 +128,11 @@ def __init__( # noqa: PLR0913 # initialize arrays self.params_samp = np.zeros((0, self.param_grid.dims)) self.losses_samp = np.zeros(0) - self.batch_num_samp = np.zeros(0, dtype=int) - self.method_samp = np.zeros(0, dtype=int) - self.series_samp = np.zeros((0, self.ensemble_size, self.N, self.D)) + self.batch_num_samp: NDArray[np.int64] = np.zeros(0, dtype=int) + self.method_samp: NDArray[np.int64] = np.zeros(0, dtype=int) + self.series_samp: NDArray[np.float64] = np.zeros( + (0, self.ensemble_size, self.N, self.D), + ) # initialize variables before calibration self.n_sampled_params = 0 diff --git a/black_it/loss_functions/gsl_div.py b/black_it/loss_functions/gsl_div.py index f70696bc..28af0cbc 100644 --- a/black_it/loss_functions/gsl_div.py +++ b/black_it/loss_functions/gsl_div.py @@ -270,7 +270,7 @@ def get_words(time_series: NDArray[np.float64], length: int) -> NDArray: "the chosen word length is too high", exception_class=ValueError, ) - tsw = np.zeros(shape=(tswlen,), dtype=np.int32) + tsw: NDArray[np.float64] = np.zeros(shape=(tswlen,), dtype=np.int32) for i in range(length): k = 10 ** (length - i - 1) diff --git a/black_it/loss_functions/likelihood.py b/black_it/loss_functions/likelihood.py index 39243941..bef9f3bf 100644 --- a/black_it/loss_functions/likelihood.py +++ b/black_it/loss_functions/likelihood.py @@ -18,7 +18,7 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING, Callable +from typing import TYPE_CHECKING, Callable, cast import numpy as np @@ -82,9 +82,13 @@ def compute_loss( Returns: The loss value. """ - r = sim_data_ensemble.shape[0] # number of repetitions - s = sim_data_ensemble.shape[1] # simulation length - d = sim_data_ensemble.shape[2] # number of dimensions + sim_data_ensemble_shape: tuple[int, int, int] = cast( + tuple[int, int, int], + sim_data_ensemble.shape, + ) + r = sim_data_ensemble_shape[0] # number of repetitions + s = sim_data_ensemble_shape[1] # simulation length + d = sim_data_ensemble_shape[2] # time series dimension if self.coordinate_weights is not None: warnings.warn( # noqa: B028 diff --git a/black_it/plot/plot_results.py b/black_it/plot/plot_results.py index b4576d25..ed42c167 100644 --- a/black_it/plot/plot_results.py +++ b/black_it/plot/plot_results.py @@ -33,6 +33,8 @@ import os from collections.abc import Collection + from numpy.typing import NDArray + def _get_samplers_id_table(saving_folder: str | os.PathLike) -> dict[str, int]: """Get the id table of the samplers from the checkpoint. @@ -299,7 +301,7 @@ def plot_sampling_interact(saving_folder: str | os.PathLike) -> None: data_frame = pd.read_csv(calibration_results_file) max_bn = int(max(data_frame["batch_num_samp"])) - all_bns = np.arange(max_bn + 1, dtype=int) + all_bns: NDArray[np.int64] = np.arange(max_bn + 1, dtype=int) indices_bns = np.array_split(all_bns, min(max_bn, 3)) dict_bns = {} diff --git a/black_it/samplers/xgboost.py b/black_it/samplers/xgboost.py index 275cea5b..da9b6d07 100644 --- a/black_it/samplers/xgboost.py +++ b/black_it/samplers/xgboost.py @@ -28,9 +28,9 @@ if TYPE_CHECKING: from numpy.typing import NDArray -MAX_FLOAT32 = np.finfo(np.float32).max -MIN_FLOAT32 = np.finfo(np.float32).min -EPS_FLOAT32 = np.finfo(np.float32).eps +MAX_FLOAT32: float = cast(float, np.finfo(np.float32).max) +MIN_FLOAT32: float = cast(float, np.finfo(np.float32).min) +EPS_FLOAT32: float = cast(float, np.finfo(np.float32).eps) class XGBoostSampler(MLSurrogateSampler): diff --git a/black_it/search_space.py b/black_it/search_space.py index 41a55e1c..2e03e1bd 100644 --- a/black_it/search_space.py +++ b/black_it/search_space.py @@ -72,7 +72,7 @@ def __init__( self._param_grid: list[NDArray[np.float64]] = [] self._space_size = 1 for i in range(self.dims): - new_col = np.arange( + new_col: NDArray[np.float64] = np.arange( parameters_bounds[0][i], parameters_bounds[1][i] + 0.0000001, parameters_precision[i], diff --git a/examples/models/simple_models.py b/examples/models/simple_models.py index 9ee8ef6e..010b30f1 100644 --- a/examples/models/simple_models.py +++ b/examples/models/simple_models.py @@ -31,7 +31,7 @@ from typing import TYPE_CHECKING import numpy as np -from scipy.stats import alpha, bernoulli +from scipy.stats import alpha, bernoulli # type: ignore[import-untyped] if TYPE_CHECKING: from collections.abc import Sequence diff --git a/examples/models/sir/simlib.py b/examples/models/sir/simlib.py index 656f6014..8dd1265e 100644 --- a/examples/models/sir/simlib.py +++ b/examples/models/sir/simlib.py @@ -62,7 +62,7 @@ def parse_simulator_output(stdout: str) -> list[dict[str, int]]: def _build_simulator_cmdline( docker_image_name: str, - sim_params: dict[str, str], + sim_params: dict[str, float], ) -> list[str]: """Convert a configuration object in a list of command line parameters. @@ -107,7 +107,7 @@ def _build_simulator_cmdline( def execute_simulator( path_to_simulator: str, - sim_params: dict[str, str], + sim_params: dict[str, float], ) -> list[dict[str, int]]: """Execute the simulator with the given parameters, and return a structured output. diff --git a/tests/test_samplers/test_xgboost.py b/tests/test_samplers/test_xgboost.py index 23a36369..590f5527 100644 --- a/tests/test_samplers/test_xgboost.py +++ b/tests/test_samplers/test_xgboost.py @@ -15,6 +15,7 @@ # along with this program. If not, see . """This module contains tests for the xgboost sampler.""" import sys +from typing import cast import numpy as np @@ -34,9 +35,9 @@ else: expected_params = np.array([[0.24, 0.26], [0.37, 0.21], [0.43, 0.14], [0.11, 0.04]]) -MAX_FLOAT32 = np.finfo(np.float32).max -MIN_FLOAT32 = np.finfo(np.float32).min -EPS_FLOAT32 = np.finfo(np.float32).eps +MAX_FLOAT32: float = cast(float, np.finfo(np.float32).max) +MIN_FLOAT32: float = cast(float, np.finfo(np.float32).min) +EPS_FLOAT32: float = cast(float, np.finfo(np.float32).eps) def test_xgboost_2d() -> None: