Skip to content

Commit

Permalink
Ensure that colour.utilities.validate_method output is cached.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Apr 28, 2023
1 parent 3b3ce28 commit 8b3457c
Show file tree
Hide file tree
Showing 62 changed files with 155 additions and 142 deletions.
2 changes: 1 addition & 1 deletion colour/adaptation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def chromatic_adaptation(
array([ 0.2152436..., 0.1253522..., 0.0388406...])
"""

method = validate_method(method, CHROMATIC_ADAPTATION_METHODS)
method = validate_method(method, tuple(CHROMATIC_ADAPTATION_METHODS))

function = CHROMATIC_ADAPTATION_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/adaptation/cmccat2000.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def chromatic_adaptation_CMCCAT2000(

direction = validate_method(
direction,
["Forward", "Inverse"],
("Forward", "Inverse"),
'"{0}" direction is invalid, it must be one of {1}!',
)

Expand Down
2 changes: 1 addition & 1 deletion colour/adaptation/vonkries.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def matrix_chromatic_adaptation_VonKries(

transform = validate_method(
transform,
CHROMATIC_ADAPTATION_TRANSFORMS,
tuple(CHROMATIC_ADAPTATION_TRANSFORMS),
'"{0}" chromatic adaptation transform is invalid, '
"it must be one of {1}!",
)
Expand Down
2 changes: 1 addition & 1 deletion colour/adaptation/zhai2018.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def chromatic_adaptation_Zhai2018(
Y_wd = XYZ_wd[..., 1][..., None]
Y_wo = XYZ_wo[..., 1][..., None]

transform = validate_method(transform, ["CAT02", "CAT16"])
transform = validate_method(transform, ("CAT02", "CAT16"))
M = CHROMATIC_ADAPTATION_TRANSFORMS[transform]

RGB_b = vector_dot(M, XYZ_b)
Expand Down
30 changes: 16 additions & 14 deletions colour/algebra/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@
"""


def get_sdiv_mode() -> Literal[
"Numpy",
"Ignore",
"Warning",
"Raise",
"Ignore Zero Conversion",
"Warning Zero Conversion",
"Ignore Limit Conversion",
"Warning Limit Conversion",
]:
def get_sdiv_mode() -> (
Literal[
"Numpy",
"Ignore",
"Warning",
"Raise",
"Ignore Zero Conversion",
"Warning Zero Conversion",
"Ignore Limit Conversion",
"Warning Limit Conversion",
]
):
"""
Return *Colour* safe division mode.
Expand Down Expand Up @@ -156,7 +158,7 @@ def set_sdiv_mode(
],
validate_method(
mode,
[
(
"Numpy",
"Ignore",
"Warning",
Expand All @@ -165,7 +167,7 @@ def set_sdiv_mode(
"Warning Zero Conversion",
"Ignore Limit Conversion",
"Warning Limit Conversion",
],
),
),
)

Expand Down Expand Up @@ -305,7 +307,7 @@ def sdiv(a: ArrayLike, b: ArrayLike) -> NDArrayFloat:

mode = validate_method(
_SDIV_MODE,
[
(
"Numpy",
"Ignore",
"Warning",
Expand All @@ -314,7 +316,7 @@ def sdiv(a: ArrayLike, b: ArrayLike) -> NDArrayFloat:
"Warning Zero Conversion",
"Ignore Limit Conversion",
"Warning Limit Conversion",
],
),
)

if mode == "numpy":
Expand Down
2 changes: 1 addition & 1 deletion colour/algebra/extrapolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def method(self, value: Literal["Linear", "Constant"] | str):
f'"method" property: "{value}" type is not "str"!',
)

value = validate_method(value, ["Linear", "Constant"])
value = validate_method(value, ("Linear", "Constant"))

self._method = value

Expand Down
2 changes: 1 addition & 1 deletion colour/algebra/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,6 @@ def table_interpolation(
[ 1.1178206..., 0.1762039..., 0.2209534...]])
"""

method = validate_method(method, TABLE_INTERPOLATION_METHODS)
method = validate_method(method, tuple(TABLE_INTERPOLATION_METHODS))

return TABLE_INTERPOLATION_METHODS[method](V_xyz, table)
2 changes: 1 addition & 1 deletion colour/appearance/hke.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def HelmholtzKohlrausch_effect_object_Nayatani1997(
u, v = tsplit(uv)
u_c, v_c = tsplit(uv_c)

method = validate_method(method, HKE_NAYATANI1997_METHODS)
method = validate_method(method, tuple(HKE_NAYATANI1997_METHODS))

K_Br = coefficient_K_Br_Nayatani1997(L_a)
q = coefficient_q_Nayatani1997(np.arctan2(v - v_c, u - u_c))
Expand Down
6 changes: 4 additions & 2 deletions colour/biochemistry/michaelis_menten.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def reaction_rate_MichaelisMenten(
1.0360547...
"""

method = validate_method(method, REACTION_RATE_MICHAELISMENTEN_METHODS)
method = validate_method(
method, tuple(REACTION_RATE_MICHAELISMENTEN_METHODS)
)

function = REACTION_RATE_MICHAELISMENTEN_METHODS[method]

Expand Down Expand Up @@ -392,7 +394,7 @@ def substrate_concentration_MichaelisMenten(
"""

method = validate_method(
method, SUBSTRATE_CONCENTRATION_MICHAELISMENTEN_METHODS
method, tuple(SUBSTRATE_CONCENTRATION_MICHAELISMENTEN_METHODS)
)

function = SUBSTRATE_CONCENTRATION_MICHAELISMENTEN_METHODS[method]
Expand Down
10 changes: 6 additions & 4 deletions colour/characterisation/correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def polynomial_expansion(
array([ 0.1722481..., 0.0917066..., 0.0641693..., 0.0010136..., 1...])
"""

method = validate_method(method, POLYNOMIAL_EXPANSION_METHODS)
method = validate_method(method, tuple(POLYNOMIAL_EXPANSION_METHODS))

function = POLYNOMIAL_EXPANSION_METHODS[method]

Expand Down Expand Up @@ -941,7 +941,7 @@ def matrix_colour_correction(
[-0.0631495..., 0.0921247..., 0.9713415...]])
"""

method = validate_method(method, MATRIX_COLOUR_CORRECTION_METHODS)
method = validate_method(method, tuple(MATRIX_COLOUR_CORRECTION_METHODS))

function = MATRIX_COLOUR_CORRECTION_METHODS[method]

Expand Down Expand Up @@ -1190,7 +1190,9 @@ def apply_matrix_colour_correction(
array([ 0.1793456..., 0.1003392..., 0.0617218...])
"""

method = validate_method(method, APPLY_MATRIX_COLOUR_CORRECTION_METHODS)
method = validate_method(
method, tuple(APPLY_MATRIX_COLOUR_CORRECTION_METHODS)
)

function = APPLY_MATRIX_COLOUR_CORRECTION_METHODS[method]

Expand Down Expand Up @@ -1469,7 +1471,7 @@ def colour_correction(
array([ 0.1334872..., 0.0843921..., 0.0599014...])
"""

method = validate_method(method, COLOUR_CORRECTION_METHODS)
method = validate_method(method, tuple(COLOUR_CORRECTION_METHODS))

function = COLOUR_CORRECTION_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/colorimetry/correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,6 @@ def bandpass_correction(
{'method': 'Constant', 'left': None, 'right': None})
"""

method = validate_method(method, BANDPASS_CORRECTION_METHODS)
method = validate_method(method, tuple(BANDPASS_CORRECTION_METHODS))

return BANDPASS_CORRECTION_METHODS[method](sd)
6 changes: 3 additions & 3 deletions colour/colorimetry/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def sd_gaussian(
0.3678794...
"""

method = validate_method(method, SD_GAUSSIAN_METHODS)
method = validate_method(method, tuple(SD_GAUSSIAN_METHODS))

return SD_GAUSSIAN_METHODS[method](
mu_peak_wavelength, sigma_fwhm, shape, **kwargs
Expand Down Expand Up @@ -673,7 +673,7 @@ def sd_single_led(
1...
"""

method = validate_method(method, SD_SINGLE_LED_METHODS)
method = validate_method(method, tuple(SD_SINGLE_LED_METHODS))

return SD_SINGLE_LED_METHODS[method](
peak_wavelength, fwhm, shape, **kwargs
Expand Down Expand Up @@ -851,7 +851,7 @@ def sd_multi_leds(
0.1295132...
"""

method = validate_method(method, SD_MULTI_LEDS_METHODS)
method = validate_method(method, tuple(SD_MULTI_LEDS_METHODS))

return SD_MULTI_LEDS_METHODS[method](
peak_wavelengths, fwhm, peak_power_ratios, shape, **kwargs
Expand Down
4 changes: 2 additions & 2 deletions colour/colorimetry/lefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def mesopic_weighting_function(

source = validate_method(
source,
["Blue Heavy", "Red Heavy"],
("Blue Heavy", "Red Heavy"),
'"{0}" light source colour temperature is invalid, '
"it must be one of {1}!",
)
method = validate_method(method, ["MOVE", "LRC"])
method = validate_method(method, ("MOVE", "LRC"))

mesopic_x_luminance_values = sorted(DATA_MESOPIC_X.keys())
index = mesopic_x_luminance_values.index(
Expand Down
6 changes: 3 additions & 3 deletions colour/colorimetry/lightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def lightness_Fairchild2011(
"""

Y = to_domain_1(Y)
method = validate_method(method, ["hdr-CIELAB", "hdr-IPT"])
method = validate_method(method, ("hdr-CIELAB", "hdr-IPT"))

maximum_perception = 247 if method == "hdr-cielab" else 246

Expand Down Expand Up @@ -498,7 +498,7 @@ def lightness_Abebe2017(

Y = as_float_array(Y)
Y_n = as_float_array(Y_n)
method = validate_method(method, ["Michaelis-Menten", "Stevens"])
method = validate_method(method, ("Michaelis-Menten", "Stevens"))

Y_Y_n = Y / Y_n
if method == "stevens":
Expand Down Expand Up @@ -630,7 +630,7 @@ def lightness(
"""

Y = as_float_array(Y)
method = validate_method(method, LIGHTNESS_METHODS)
method = validate_method(method, tuple(LIGHTNESS_METHODS))

function = LIGHTNESS_METHODS[method]

Expand Down
6 changes: 3 additions & 3 deletions colour/colorimetry/luminance.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def luminance_Fairchild2011(
"""

L_hdr = to_domain_100(L_hdr)
method = validate_method(method, ["hdr-CIELAB", "hdr-IPT"])
method = validate_method(method, ("hdr-CIELAB", "hdr-IPT"))

maximum_perception = 247 if method == "hdr-cielab" else 246

Expand Down Expand Up @@ -508,7 +508,7 @@ def luminance_Abebe2017(

L = as_float_array(L)
Y_n = as_float_array(Y_n)
method = validate_method(method, ["Michaelis-Menten", "Stevens"])
method = validate_method(method, ("Michaelis-Menten", "Stevens"))

if method == "stevens":
Y = np.where(
Expand Down Expand Up @@ -644,7 +644,7 @@ def luminance(
"""

LV = as_float_array(LV)
method = validate_method(method, LUMINANCE_METHODS)
method = validate_method(method, tuple(LUMINANCE_METHODS))

function = LUMINANCE_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/colorimetry/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2803,7 +2803,7 @@ def reshape_sd(
"""

method = validate_method(
method, valid_methods=["Align", "Extrapolate", "Interpolate", "Trim"]
method, valid_methods=("Align", "Extrapolate", "Interpolate", "Trim")
)

# Handling dict-like keyword arguments.
Expand Down
6 changes: 3 additions & 3 deletions colour/colorimetry/tristimulus_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def lagrange_coefficients_ASTME2022(

interval_type = validate_method(
interval_type,
["Boundary", "Inner"],
("Boundary", "Inner"),
'"{0}" interval type is invalid, it must be one of {1}!',
)

Expand Down Expand Up @@ -1243,7 +1243,7 @@ def sd_to_XYZ(
cmfs, illuminant, illuminant_default="E"
)

method = validate_method(method, SD_TO_XYZ_METHODS)
method = validate_method(method, tuple(SD_TO_XYZ_METHODS))

global _CACHE_SD_TO_XYZ # noqa: PLW0602

Expand Down Expand Up @@ -2019,7 +2019,7 @@ class on the last / tail axis as follows:
[ 24.6452277..., 26.0809382..., 27.7106399...]])
"""

method = validate_method(method, MSDS_TO_XYZ_METHODS)
method = validate_method(method, tuple(MSDS_TO_XYZ_METHODS))

function = MSDS_TO_XYZ_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/colorimetry/whiteness.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def whiteness(
XYZ = as_float_array(XYZ)
XYZ_0 = as_float_array(XYZ_0)

method = validate_method(method, WHITENESS_METHODS)
method = validate_method(method, tuple(WHITENESS_METHODS))

kwargs.update({"XYZ": XYZ, "XYZ_0": XYZ_0})

Expand Down
2 changes: 1 addition & 1 deletion colour/colorimetry/yellowness.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def yellowness(
10.2999999...
"""

method = validate_method(method, YELLOWNESS_METHODS)
method = validate_method(method, tuple(YELLOWNESS_METHODS))

function = YELLOWNESS_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/continuous/multi_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ def fill_nan(
[ 9. 100. 110. 120.]]
"""

method = validate_method(method, ["Interpolation", "Constant"])
method = validate_method(method, ("Interpolation", "Constant"))

for signal in self._signals.values():
signal.fill_nan(method, default)
Expand Down
2 changes: 1 addition & 1 deletion colour/continuous/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ def fill_nan(
[ 9. 100.]]
"""

method = validate_method(method, ["Interpolation", "Constant"])
method = validate_method(method, ("Interpolation", "Constant"))

self._fill_domain_nan(method, default)
self._fill_range_nan(method, default)
Expand Down
2 changes: 1 addition & 1 deletion colour/contrast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def contrast_sensitivity_function(
360.8691122...
"""

method = validate_method(method, CONTRAST_SENSITIVITY_METHODS)
method = validate_method(method, tuple(CONTRAST_SENSITIVITY_METHODS))

function = CONTRAST_SENSITIVITY_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/difference/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def delta_E(
0.0001034...
"""

method = validate_method(method, DELTA_E_METHODS)
method = validate_method(method, tuple(DELTA_E_METHODS))

function = DELTA_E_METHODS[method]

Expand Down
2 changes: 1 addition & 1 deletion colour/difference/huang2015.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def power_function_Huang2015(

coefficients = validate_method(
coefficients,
COEFFICIENTS_HUANG2015,
tuple(COEFFICIENTS_HUANG2015),
'"{0}" coefficients are invalid, they must be one of {1}!',
)

Expand Down
2 changes: 1 addition & 1 deletion colour/difference/stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def index_stress(
0.1211709...
"""

method = validate_method(method, INDEX_STRESS_METHODS)
method = validate_method(method, tuple(INDEX_STRESS_METHODS))

function = INDEX_STRESS_METHODS[method]

Expand Down
Loading

0 comments on commit 8b3457c

Please sign in to comment.