Skip to content

Commit 3a5bbfb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b534c91 commit 3a5bbfb

6 files changed

Lines changed: 40 additions & 45 deletions

File tree

‎src/resample/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
__version__ = version("resample")
2020

2121
__all__ = [
22-
"jackknife",
22+
"__version__",
2323
"bootstrap",
24-
"permutation",
2524
"empirical",
26-
"__version__",
25+
"jackknife",
26+
"permutation",
2727
]

‎src/resample/_util.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
from typing import Optional, Tuple, Union
2-
31
import numpy as np
42
from numpy.typing import ArrayLike
53

64
__all__ = ["normalize_rng", "wilson_score_interval"]
75

86

97
def normalize_rng(
10-
random_state: Optional[Union[int, np.random.Generator]],
8+
random_state: int | np.random.Generator | None,
119
) -> np.random.Generator:
1210
"""Return normalized RNG object."""
1311
if random_state is None:
@@ -19,7 +17,7 @@ def normalize_rng(
1917

2018
def wilson_score_interval(
2119
n1: "ArrayLike", n: "ArrayLike", z: float
22-
) -> Tuple[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
20+
) -> tuple[np.ndarray, tuple[np.ndarray, np.ndarray]]:
2321
"""Return binomial fraction and Wilson score interval."""
2422
p = n1 / n
2523
norm = 1 / (1 + z**2 / n)

‎src/resample/bootstrap.py‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@
1313
"""
1414

1515
__all__ = [
16-
"resample",
1716
"bootstrap",
18-
"variance",
19-
"covariance",
2017
"confidence_interval",
18+
"covariance",
19+
"resample",
20+
"variance",
2121
]
2222

23+
from collections.abc import Callable, Collection, Generator
2324
from typing import (
2425
Any,
25-
Callable,
26-
Collection,
27-
Dict,
28-
Generator,
29-
List,
3026
Optional,
31-
Tuple,
32-
Union,
3327
)
3428

3529
import numpy as np
@@ -47,7 +41,7 @@ def resample(
4741
size: int = 100,
4842
method: str = "balanced",
4943
strata: Optional["ArrayLike"] = None,
50-
random_state: Optional[Union[np.random.Generator, int]] = None,
44+
random_state: np.random.Generator | int | None = None,
5145
) -> Generator[np.ndarray, None, None]:
5246
"""
5347
Return generator of bootstrap samples.
@@ -147,7 +141,7 @@ def resample(
147141
"""
148142
sample_np = np.atleast_1d(sample)
149143
n_sample = len(sample_np)
150-
args_np: List[np.ndarray] = []
144+
args_np: list[np.ndarray] = []
151145

152146
if args:
153147
if not isinstance(args[0], Collection):
@@ -158,7 +152,7 @@ def resample(
158152
"deprecated",
159153
FutureWarning,
160154
)
161-
kwargs: Dict[str, Any] = {
155+
kwargs: dict[str, Any] = {
162156
"size": size,
163157
"method": method,
164158
"strata": strata,
@@ -381,7 +375,7 @@ def confidence_interval(
381375
cl: float = 0.95,
382376
ci_method: str = "bca",
383377
**kwargs: Any,
384-
) -> Tuple[float, float]:
378+
) -> tuple[float, float]:
385379
"""
386380
Calculate bootstrap confidence intervals.
387381
@@ -491,7 +485,7 @@ def _resample_ordinary_1(
491485

492486

493487
def _resample_ordinary_n(
494-
samples: List[np.ndarray], size: int, rng: np.random.Generator
488+
samples: list[np.ndarray], size: int, rng: np.random.Generator
495489
) -> Generator[np.ndarray, None, None]:
496490
n = len(samples[0])
497491
indices = np.arange(n)
@@ -513,7 +507,7 @@ def _resample_balanced_1(
513507

514508

515509
def _resample_balanced_n(
516-
samples: List[np.ndarray], size: int, rng: np.random.Generator
510+
samples: list[np.ndarray], size: int, rng: np.random.Generator
517511
) -> Generator[np.ndarray, None, None]:
518512
n = len(samples[0])
519513
indices = rng.permutation(n * size)
@@ -533,7 +527,7 @@ def _resample_extended_1(
533527

534528

535529
def _resample_extended_n(
536-
samples: List[np.ndarray], size: int, rng: np.random.Generator
530+
samples: list[np.ndarray], size: int, rng: np.random.Generator
537531
) -> Generator[np.ndarray, None, None]:
538532
n = len(samples[0])
539533
for i in range(size):
@@ -543,7 +537,7 @@ def _resample_extended_n(
543537

544538
def _fit_parametric_family(
545539
dist: stats.rv_continuous, sample: np.ndarray
546-
) -> Tuple[float, ...]:
540+
) -> tuple[float, ...]:
547541
if dist == stats.multivariate_normal:
548542
# has no fit method...
549543
return np.mean(sample, axis=0), np.cov(sample.T, ddof=1)
@@ -579,14 +573,14 @@ def _resample_parametric(
579573

580574
def _confidence_interval_percentile(
581575
thetas: np.ndarray, alpha_half: float
582-
) -> Tuple[float, float]:
576+
) -> tuple[float, float]:
583577
quant = quantile_function_gen(thetas)
584578
return quant(alpha_half), quant(1 - alpha_half)
585579

586580

587581
def _confidence_interval_bca(
588582
theta: float, thetas: np.ndarray, j_thetas: np.ndarray, alpha_half: float
589-
) -> Tuple[float, float]:
583+
) -> tuple[float, float]:
590584
norm = stats.norm
591585

592586
# bias correction; implementation notes:

‎src/resample/empirical.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
like the empirical CDF. Implemented here are mostly tools used internally.
66
"""
77

8-
__all__ = ["cdf_gen", "quantile_function_gen", "influence"]
8+
__all__ = ["cdf_gen", "influence", "quantile_function_gen"]
99

10-
from typing import Callable, Union
10+
from collections.abc import Callable
11+
from typing import Union
1112

1213
import numpy as np
1314
from numpy.typing import ArrayLike
@@ -37,7 +38,7 @@ def cdf_gen(sample: "ArrayLike") -> Callable[[np.ndarray], np.ndarray]:
3738

3839
def quantile_function_gen(
3940
sample: "ArrayLike",
40-
) -> Callable[[Union[float, "ArrayLike"]], Union[float, np.ndarray]]:
41+
) -> Callable[[Union[float, "ArrayLike"]], float | np.ndarray]:
4142
"""
4243
Return the empirical quantile function for the given sample.
4344
@@ -57,7 +58,7 @@ class QuantileFn:
5758
def __init__(self, sample: "ArrayLike"):
5859
self._sorted = np.sort(sample, axis=0)
5960

60-
def __call__(self, p: Union[float, "ArrayLike"]) -> Union[float, np.ndarray]:
61+
def __call__(self, p: Union[float, "ArrayLike"]) -> float | np.ndarray:
6162
ndim = np.ndim(p) # must come before atleast_1d
6263
p = np.atleast_1d(p)
6364
result = np.empty(len(p))

‎src/resample/jackknife.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
"""
1818

1919
__all__ = [
20-
"resample",
21-
"jackknife",
2220
"bias",
2321
"bias_corrected",
24-
"variance",
2522
"cross_validation",
23+
"jackknife",
24+
"resample",
25+
"variance",
2626
]
2727

28-
from typing import Any, Callable, Collection, Generator, List
28+
from collections.abc import Callable, Collection, Generator
29+
from typing import Any
2930

3031
import numpy as np
3132
from numpy.typing import ArrayLike
@@ -136,7 +137,7 @@ def _resample_1(sample: np.ndarray, copy: bool) -> Generator[np.ndarray, None, N
136137
yield x.copy() if copy else x
137138

138139

139-
def _resample_n(samples: List[np.ndarray], copy: bool) -> Generator[Any, None, None]:
140+
def _resample_n(samples: list[np.ndarray], copy: bool) -> Generator[Any, None, None]:
140141
x = [a[1:].copy() for a in samples]
141142
yield (xi.copy() for xi in x)
142143
for i in range(len(samples[0]) - 1):
@@ -363,5 +364,5 @@ def cross_validation(
363364
deltas = []
364365
for i, (x_in, y_in) in enumerate(resample(x, y, copy=False)):
365366
yip = predict(x_in, y_in, x[i], *args)
366-
deltas.append((y[i] - yip))
367+
deltas.append(y[i] - yip)
367368
return np.var(deltas) # type:ignore

‎src/resample/permutation.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323

2424
__all__ = [
2525
"TestResult",
26-
"usp",
27-
"same_population",
2826
"anova",
2927
"kruskal",
3028
"pearsonr",
29+
"same_population",
3130
"spearmanr",
3231
"ttest",
32+
"usp",
3333
]
3434

3535
import sys
3636
import warnings
37+
from collections.abc import Callable
3738
from dataclasses import dataclass
38-
from typing import Any, Callable, Optional, Tuple, Union
39+
from typing import Any
3940

4041
import numpy as np
4142
from numpy.typing import ArrayLike, NDArray
@@ -89,7 +90,7 @@ def __len__(self) -> int:
8990
"""Return length of tuple."""
9091
return 3
9192

92-
def __getitem__(self, idx: int) -> Union[float, NDArray]:
93+
def __getitem__(self, idx: int) -> float | NDArray:
9394
"""Return fields by index."""
9495
if idx == 0:
9596
return self.statistic
@@ -105,7 +106,7 @@ def usp(
105106
*,
106107
size: int = 9999,
107108
method: str = "auto",
108-
random_state: Optional[Union[np.random.Generator, int]] = None,
109+
random_state: np.random.Generator | int | None = None,
109110
) -> TestResult:
110111
"""
111112
Test independence of two discrete data sets with the U-statistic.
@@ -198,9 +199,9 @@ def same_population(
198199
x: "ArrayLike",
199200
y: "ArrayLike",
200201
*args: "ArrayLike",
201-
transform: Optional[Callable[[NDArray], NDArray]] = None,
202+
transform: Callable[[NDArray], NDArray] | None = None,
202203
size: int = 9999,
203-
random_state: Optional[Union[np.random.Generator, int]] = None,
204+
random_state: np.random.Generator | int | None = None,
204205
) -> TestResult:
205206
"""
206207
Compute p-value for hypothesis that samples originate from same population.
@@ -511,7 +512,7 @@ def __call__(self, *args: NDArray) -> float:
511512
)
512513
return between_group_variability / within_group_variability
513514

514-
def _init(self, args: Tuple[NDArray, ...]) -> None:
515+
def _init(self, args: tuple[NDArray, ...]) -> None:
515516
n = sum(len(a) for a in args)
516517
k = len(args)
517518
self.km1 = k - 1

0 commit comments

Comments
 (0)