Skip to content

Commit 12044e4

Browse files
jorenhampre-commit-ci[bot]KelSolaar
authored
PR: Add scipy-stubs as development dependency. (colour-science#1342)
* add `scipy-stubs` as dev dependency * fix typing errors related to `scipy.optimize.minimize` * narrow `ArrayLike` types to float arrays for use with `scipy-stubs` * ensure that `scipy.optimize.fmin` is optimizing a scalar function * fix LSP issues in the interpolation and extrapolation `Protocol` definitions * remove redundant `typing.cast` * unleash `ruff` * overload `coefficient_K_Br_Nayatani1997` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix typing issues when using `numpy==2.2.4` * fix typing errors caused by resolved issues in older `scipy-stubs` versions * prefer `str | os.PathLike[str]` over `_typeshed.StrPath` Co-authored-by: Thomas Mansencal <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Thomas Mansencal <[email protected]>
1 parent 42e9e0e commit 12044e4

File tree

27 files changed

+133
-85
lines changed

27 files changed

+133
-85
lines changed

colour/adaptation/cie1994.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from colour.algebra import sdiv, sdiv_mode, spow, vecmul
2525

2626
if typing.TYPE_CHECKING:
27-
from colour.hints import ArrayLike, NDArrayFloat
27+
from colour.hints import ArrayLike, DTypeFloat, NDArray, NDArrayFloat
2828

2929
from colour.utilities import (
3030
as_float_array,
@@ -290,7 +290,13 @@ def effective_adapting_responses(
290290
return ((Y_o[..., None] * E_o[..., None]) / (100 * np.pi)) * xez
291291

292292

293-
def beta_1(x: ArrayLike) -> NDArrayFloat:
293+
@typing.overload
294+
def beta_1(x: float | DTypeFloat) -> DTypeFloat: ...
295+
@typing.overload
296+
def beta_1(x: NDArray) -> NDArrayFloat: ...
297+
@typing.overload
298+
def beta_1(x: ArrayLike) -> DTypeFloat | NDArrayFloat: ...
299+
def beta_1(x: ArrayLike) -> DTypeFloat | NDArrayFloat:
294300
"""
295301
Compute the exponent :math:`\\beta_1` for the middle and long-wavelength
296302
sensitive cones.
@@ -313,10 +319,16 @@ def beta_1(x: ArrayLike) -> NDArrayFloat:
313319

314320
x_p = spow(x, 0.4495)
315321

316-
return (6.469 + 6.362 * x_p) / (6.469 + x_p)
322+
return (x_p * 6.362 + 6.469) / (x_p + 6.469)
317323

318324

319-
def beta_2(x: ArrayLike) -> NDArrayFloat:
325+
@typing.overload
326+
def beta_2(x: float | DTypeFloat) -> DTypeFloat: ...
327+
@typing.overload
328+
def beta_2(x: NDArray) -> NDArrayFloat: ...
329+
@typing.overload
330+
def beta_2(x: ArrayLike) -> DTypeFloat | NDArrayFloat: ...
331+
def beta_2(x: ArrayLike) -> DTypeFloat | NDArrayFloat:
320332
"""
321333
Compute the exponent :math:`\\beta_2` for the short-wavelength sensitive
322334
cones.
@@ -339,7 +351,7 @@ def beta_2(x: ArrayLike) -> NDArrayFloat:
339351

340352
x_p = spow(x, 0.5128)
341353

342-
return 0.7844 * (8.414 + 8.091 * x_p) / (8.414 + x_p)
354+
return (x_p * 8.091 + 8.414) * 0.7844 / (x_p + 8.414)
343355

344356

345357
def exponential_factors(RGB_o: ArrayLike) -> NDArrayFloat:

colour/algebra/common.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
Any,
1919
ArrayLike,
2020
Callable,
21+
DTypeFloat,
22+
NDArray,
2123
NDArrayFloat,
2224
Self,
2325
Tuple,
@@ -437,7 +439,15 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
437439
return wrapper
438440

439441

440-
def spow(a: ArrayLike, p: ArrayLike) -> NDArrayFloat:
442+
@typing.overload
443+
def spow(a: float | DTypeFloat, p: float | DTypeFloat) -> DTypeFloat: ...
444+
@typing.overload
445+
def spow(a: NDArray, p: ArrayLike) -> NDArrayFloat: ...
446+
@typing.overload
447+
def spow(a: ArrayLike, p: NDArray) -> NDArrayFloat: ...
448+
@typing.overload
449+
def spow(a: ArrayLike, p: ArrayLike) -> DTypeFloat | NDArrayFloat: ...
450+
def spow(a: ArrayLike, p: ArrayLike) -> DTypeFloat | NDArrayFloat:
441451
"""
442452
Raise given array :math:`a` to the power :math:`p` as follows:
443453
:math:`sign(a) * |a|^p`.

colour/algebra/interpolation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ class PchipInterpolator(scipy.interpolate.PchipInterpolator):
12921292
"""
12931293

12941294
def __init__(self, x: ArrayLike, y: ArrayLike, *args: Any, **kwargs: Any) -> None:
1295-
super().__init__(x, y, *args, **kwargs)
1295+
super().__init__(as_float_array(x), as_float_array(y), *args, **kwargs)
12961296

12971297
self._y: NDArrayFloat = as_float_array(y)
12981298

colour/algebra/tests/test_interpolation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ def test_y(self) -> None:
14381438

14391439
interpolator.y = np.linspace(0, 1, 10)
14401440

1441-
assert interpolator(5) == 5
1441+
assert interpolator(np.array(5)) == 5
14421442

14431443

14441444
class TestNullInterpolator:

colour/appearance/ciecam02.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,10 @@ def temporary_magnitude_quantity_inverse(
14381438
"""
14391439

14401440
C = as_float_array(C)
1441-
J = np.maximum(J, EPSILON)
1441+
J_prime = np.maximum(J, EPSILON)
14421442
n = as_float_array(n)
14431443

1444-
return spow(C / (np.sqrt(J / 100) * spow(1.64 - 0.29**n, 0.73)), 1 / 0.9)
1444+
return spow(C / (np.sqrt(J_prime / 100) * spow(1.64 - 0.29**n, 0.73)), 1 / 0.9)
14451445

14461446

14471447
def chroma_correlate(

colour/appearance/hke.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from colour.algebra import spow
3333

3434
if typing.TYPE_CHECKING:
35-
from colour.hints import ArrayLike, Literal, NDArrayFloat
35+
from colour.hints import ArrayLike, DTypeFloat, Literal, NDArray, NDArrayFloat
3636

3737
from colour.utilities import CanonicalMapping, as_float_array, tsplit, validate_method
3838

@@ -238,9 +238,13 @@ def coefficient_q_Nayatani1997(
238238
)
239239

240240

241-
def coefficient_K_Br_Nayatani1997(
242-
L_a: ArrayLike,
243-
) -> NDArrayFloat:
241+
@typing.overload
242+
def coefficient_K_Br_Nayatani1997(L_a: float | DTypeFloat) -> DTypeFloat: ...
243+
@typing.overload
244+
def coefficient_K_Br_Nayatani1997(L_a: NDArray) -> NDArrayFloat: ...
245+
@typing.overload
246+
def coefficient_K_Br_Nayatani1997(L_a: ArrayLike) -> DTypeFloat | NDArrayFloat: ...
247+
def coefficient_K_Br_Nayatani1997(L_a: ArrayLike) -> DTypeFloat | NDArrayFloat:
244248
"""
245249
Return the :math:`K_{Br}` coefficient for *Nayatani (1997)* *HKE*
246250
computations.
@@ -276,4 +280,4 @@ def coefficient_K_Br_Nayatani1997(
276280

277281
L_a_4495 = spow(L_a, 0.4495)
278282

279-
return 0.2717 * (6.469 + 6.362 * L_a_4495) / (6.469 + L_a_4495)
283+
return (L_a_4495 * 6.362 + 6.469) * 0.2717 / (L_a_4495 + 6.469)

colour/appearance/rlab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ def XYZ_to_RLAB(
273273
M = np.matmul(np.matmul(MATRIX_R, row_as_diagonal(LMS_a_L)), MATRIX_XYZ_TO_HPE)
274274
XYZ_ref = vecmul(M, XYZ)
275275

276+
Y_ref: NDArrayFloat
276277
X_ref, Y_ref, Z_ref = tsplit(XYZ_ref)
277278

278279
# Computing the correlate of *Lightness* :math:`L^R`.

colour/characterisation/aces_it.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
Any,
8888
ArrayLike,
8989
Callable,
90+
DTypeFloat,
9091
Literal,
9192
LiteralChromaticAdaptationTransform,
9293
Mapping,
@@ -768,7 +769,7 @@ def optimisation_factory_rawtoaces_v1() -> (
768769

769770
def objective_function(
770771
M: NDArrayFloat, RGB: NDArrayFloat, Lab: NDArrayFloat
771-
) -> NDArrayFloat:
772+
) -> DTypeFloat:
772773
"""Objective function according to *RAW to ACES* v1."""
773774

774775
M = finaliser_function(M)
@@ -830,9 +831,7 @@ def optimisation_factory_Jzazbz() -> Tuple[NDArrayFloat, Callable, Callable, Cal
830831

831832
x_0 = as_float_array([1, 0, 0, 1, 0, 0])
832833

833-
def objective_function(
834-
M: ArrayLike, RGB: ArrayLike, Jab: ArrayLike
835-
) -> NDArrayFloat:
834+
def objective_function(M: ArrayLike, RGB: ArrayLike, Jab: ArrayLike) -> DTypeFloat:
836835
""":math:`J_za_zb_z` colourspace based objective function."""
837836

838837
M = finaliser_function(M)
@@ -901,9 +900,7 @@ def optimisation_factory_Oklab_15() -> (
901900

902901
x_0 = as_float_array([1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1])
903902

904-
def objective_function(
905-
M: ArrayLike, RGB: ArrayLike, Jab: ArrayLike
906-
) -> NDArrayFloat:
903+
def objective_function(M: ArrayLike, RGB: ArrayLike, Jab: ArrayLike) -> DTypeFloat:
907904
"""*Oklab* colourspace based objective function."""
908905

909906
M = finaliser_function(M)
@@ -1124,7 +1121,8 @@ def matrix_idt(
11241121
XYZ_to_optimization_colour_model,
11251122
finaliser_function,
11261123
) = optimisation_factory()
1127-
optimisation_settings = {
1124+
1125+
optimisation_settings: dict[str, Any] = {
11281126
"method": "BFGS",
11291127
"jac": "2-point",
11301128
}

colour/examples/contrast/examples_contrast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def maximise_spatial_frequency(L: ArrayLike) -> NDArrayFloat:
7575
E=E, # noqa: B023
7676
**settings_BT2246,
7777
)
78-
),
79-
0,
78+
).item(),
79+
[0],
8080
disp=False,
8181
)[0]
8282
)

colour/hints/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,18 @@ def x(self) -> NDArray: # noqa: D102
149149
...
150150

151151
@x.setter
152-
def x(self, value: ArrayLike) -> None: ...
152+
def x(self, value: ArrayLike, /) -> None: ...
153153

154154
@property
155155
def y(self) -> NDArray: # noqa: D102
156156
...
157157

158158
@y.setter
159-
def y(self, value: ArrayLike) -> None: ...
159+
def y(self, value: ArrayLike, /) -> None: ...
160160

161161
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # pragma: no cover
162162

163-
def __call__(self, x: ArrayLike) -> NDArray: # noqa: D102
163+
def __call__(self, x: NDArrayFloat) -> NDArray: # noqa: D102
164164
... # pragma: no cover
165165

166166

@@ -170,11 +170,11 @@ def interpolator(self) -> ProtocolInterpolator: # noqa: D102
170170
...
171171

172172
@interpolator.setter
173-
def interpolator(self, value: ProtocolInterpolator) -> None: ...
173+
def interpolator(self, value: ProtocolInterpolator, /) -> None: ...
174174

175175
def __init__(self, *args: Any, **kwargs: Any) -> None: ... # pragma: no cover
176176

177-
def __call__(self, x: ArrayLike) -> NDArray: # noqa: D102
177+
def __call__(self, x: NDArrayFloat) -> NDArray: # noqa: D102
178178
... # pragma: no cover
179179

180180

0 commit comments

Comments
 (0)