Skip to content

Commit

Permalink
test: fix linting and some tests [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
AldoGl committed Sep 2, 2024
1 parent 554a754 commit a3f0f8b
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
run: tox -e check-copyright
- name: Misc checks
run: |
tox -e bandit,safety
tox -e bandit
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ jobs:
# https://stackoverflow.com/questions/73029883/could-not-find-hdf5-installation-for-pytables-on-m1-mac
echo "HDF5_DIR=/opt/homebrew/opt/hdf5" >> $GITHUB_ENV
echo "BLOSC_DIR=/opt/homebrew/opt/c-blosc" >> $GITHUB_ENV
# - if: ${{ matrix.python-version == '3.12' }}
# name: Install setuptools
# run: pip install setuptools
- if: ${{ (matrix.os == 'ubuntu-latest') || (matrix.os == 'macos-latest') }}
name: Unit tests and coverage (ubuntu-latest, macos-latest)
run: |
Expand Down
8 changes: 5 additions & 3 deletions black_it/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion black_it/loss_functions/gsl_div.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions black_it/loss_functions/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion black_it/plot/plot_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = {}
Expand Down
6 changes: 3 additions & 3 deletions black_it/samplers/xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion black_it/search_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion examples/models/simple_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/models/sir/simlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Binary file added tests/fixtures/data/test_sir_w_breaks_python.npy
Binary file not shown.
1 change: 0 additions & 1 deletion tests/test_examples/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ class TestMainExample(BaseMainExampleTestClass):
"METHOD: BestBatchSampler",
*[f"BATCH NUMBER: {i}" for i in range(1, nb_batches + 1)],
TRUE_PARAMETERS_STR,
BEST_PARAMETERS_STR,
)
7 changes: 4 additions & 3 deletions tests/test_samplers/test_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This module contains tests for the xgboost sampler."""
import sys
from typing import cast

import numpy as np

Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ commands =
# test environment for notebooks
[testenv:py{39,310,311,312}-nb]
deps =
pytest==7.4.2
mesa==2.1.1
nbmake==1.4.3
pytest==7.4.2 # current latest: 8.3.2
mesa==2.1.1 # current latest: 2.3.3
nbmake==1.5.4
commands = pytest examples/tests_on_toy_model.ipynb --nbmake --nbmake-timeout=300

[testenv:mypy]
Expand Down

0 comments on commit a3f0f8b

Please sign in to comment.