Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: support python 3.12 (but drop 3.8) #82

Merged
merged 8 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: [3.8]
python-version: [3.9]

timeout-minutes: 30

Expand All @@ -37,7 +37,7 @@ jobs:
- name: Install dependencies
run: |
pip install tox

# Install markdown-spellcheck
sudo npm install -g markdown-spellcheck
- name: Generate Documentation
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: [3.8]
python-version: [3.9]

timeout-minutes: 30

Expand All @@ -36,4 +36,4 @@ jobs:
run: tox -e check-copyright
- name: Misc checks
run: |
tox -e bandit,safety
muxator marked this conversation as resolved.
Show resolved Hide resolved
tox -e bandit
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.8, 3.9, "3.10", "3.11"]
python-version: [3.9, "3.10", "3.11", "3.12"]

timeout-minutes: 30

Expand Down
4 changes: 2 additions & 2 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
# Whether to enable preview mode. When preview mode is enabled, Ruff will use unstable rules and fixes.
preview = true

# Assume Python 3.8
target-version = "py38"
# Assume Python 3.9
target-version = "py39"

[per-file-ignores]
"examples/docker-sir.py" = ["INP001", "T201"]
Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ We have various commands which are helpful during development.
```
make lint-all
make static
make safety
make bandit
```

Expand Down
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ clean-test: ## remove test and coverage artifacts
rm -fr coverage.xml

.PHONY: lint-all
lint-all: black check-copyright ruff static bandit safety vulture darglint ## run all linters
lint-all: black check-copyright ruff static bandit vulture darglint ## run all linters

.PHONY: lint-all-files
lint-all-files: black-files ruff-files static-files bandit-files vulture-files darglint-files ## run all linters for specific files (specified with files="file1 file2 somedir ...")
Expand Down Expand Up @@ -130,10 +130,6 @@ bandit-files: ## run bandit for specific files (specified with files="file1 file
$(call check_defined, files)
bandit $(files)

.PHONY: safety
safety: ## run safety
safety check

.PHONY: vulture
vulture: ## run vulture
vulture black_it scripts/whitelists/package_whitelist.py
Expand Down
11 changes: 7 additions & 4 deletions black_it/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import time
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Sequence, cast
from typing import TYPE_CHECKING, Callable, cast

import numpy as np
from joblib import Parallel, delayed # type: ignore[import]
Expand All @@ -39,6 +39,7 @@

if TYPE_CHECKING:
import os
from collections.abc import Sequence

from numpy.typing import NDArray

Expand Down Expand Up @@ -127,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
5 changes: 4 additions & 1 deletion black_it/loss_functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Callable, Optional, Sequence
from typing import TYPE_CHECKING, Callable, Optional

import numpy as np
from numpy.typing import NDArray

from black_it.utils.base import _assert

if TYPE_CHECKING:
from collections.abc import Sequence

TimeSeriesFilter = Optional[Callable[[NDArray[np.float64]], NDArray[np.float64]]]
"""A filter that receives a time series and returns its filtered version. Used by the BaseLoss constructor."""

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
7 changes: 5 additions & 2 deletions black_it/plot/plot_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pickle # nosec B403
from pathlib import Path
from typing import TYPE_CHECKING, Collection
from typing import TYPE_CHECKING

import matplotlib.pyplot as plt # type: ignore[import]
import numpy as np
Expand All @@ -31,6 +31,9 @@

if TYPE_CHECKING:
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]:
Expand Down Expand Up @@ -298,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
4 changes: 3 additions & 1 deletion black_it/samplers/halton.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
from __future__ import annotations

import itertools
from typing import TYPE_CHECKING, Iterator
from typing import TYPE_CHECKING

import numpy as np

from black_it.samplers.base import BaseSampler
from black_it.utils.base import check_arg, digitize_data

if TYPE_CHECKING:
from collections.abc import Iterator

from numpy.typing import NDArray

from black_it.search_space import SearchSpace
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
4 changes: 3 additions & 1 deletion black_it/schedulers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import contextlib
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Generator, Sequence
from typing import TYPE_CHECKING

from black_it.utils.seedable import BaseSeedable

if TYPE_CHECKING:
from collections.abc import Generator, Sequence

import numpy as np
from numpy.typing import NDArray

Expand Down
5 changes: 3 additions & 2 deletions black_it/schedulers/rl/rl_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from __future__ import annotations

import threading
from typing import TYPE_CHECKING, List, Sequence, cast
from typing import TYPE_CHECKING, cast

import numpy as np

Expand All @@ -27,6 +27,7 @@
from black_it.schedulers.base import BaseScheduler

if TYPE_CHECKING:
from collections.abc import Sequence
from queue import Queue

from numpy._typing import NDArray
Expand Down Expand Up @@ -103,7 +104,7 @@ def _add_or_get_bootstrap_sampler(
return samplers, sampler_types[HaltonSampler]

new_sampler = HaltonSampler(batch_size=1)
return tuple(list(samplers) + cast(List[BaseSampler], [new_sampler])), len(
return tuple(list(samplers) + cast(list[BaseSampler], [new_sampler])), len(
samplers,
)

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
4 changes: 3 additions & 1 deletion black_it/utils/json_pandas_checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import json
import pickle # nosec B403
from pathlib import Path
from typing import TYPE_CHECKING, Mapping
from typing import TYPE_CHECKING

import numpy as np
import pandas as pd # type: ignore[import]
Expand All @@ -29,6 +29,8 @@
from black_it.utils.base import NumpyArrayEncoder, PathLike

if TYPE_CHECKING:
from collections.abc import Mapping

from numpy.typing import NDArray

from black_it.loss_functions.base import BaseLoss
Expand Down
4 changes: 3 additions & 1 deletion black_it/utils/sqlite3_checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import pickle # nosec B403
import sqlite3
from pathlib import Path
from typing import TYPE_CHECKING, Mapping, Sequence
from typing import TYPE_CHECKING

import numpy as np
from numpy.typing import NDArray

if TYPE_CHECKING:
from collections.abc import Mapping, Sequence

from black_it.loss_functions.base import BaseLoss
from black_it.samplers.base import BaseSampler
from black_it.utils.base import PathLike
Expand Down
4 changes: 3 additions & 1 deletion examples/models/economics/brock_hommes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
"""Implementation of the model in (Brock and Hommes, 1998)."""
from __future__ import annotations

from typing import TYPE_CHECKING, Sequence
from typing import TYPE_CHECKING

import numpy as np
from scipy.special import softmax

if TYPE_CHECKING:
from collections.abc import Sequence

from numpy.typing import NDArray


Expand Down
4 changes: 3 additions & 1 deletion examples/models/simple_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
"""
from __future__ import annotations

from typing import TYPE_CHECKING, Sequence
from typing import TYPE_CHECKING

import numpy as np
from scipy.stats import alpha, bernoulli

if TYPE_CHECKING:
from collections.abc import Sequence

from numpy.typing import NDArray


Expand Down
Loading
Loading