Skip to content

Commit c496592

Browse files
committed
Remove type hints incompatible with Numpy 2.1
1 parent d566579 commit c496592

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

modelskill/metrics.py

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
import sys
7272
import warnings
73-
from typing import Callable, Iterable, List, Optional, Set, Tuple, Union
73+
from typing import Any, Callable, Iterable, List, Optional, Set, Tuple, Union
7474

7575
import numpy as np
7676
import pandas as pd
@@ -79,7 +79,7 @@
7979
from .settings import options
8080

8181

82-
def bias(obs, model) -> float:
82+
def bias(obs, model) -> Any:
8383
r"""Bias (mean error)
8484
8585
$$
@@ -93,7 +93,7 @@ def bias(obs, model) -> float:
9393
return np.mean(model - obs)
9494

9595

96-
def max_error(obs, model) -> float:
96+
def max_error(obs, model) -> Any:
9797
r"""Max (absolute) error
9898
9999
$$
@@ -109,15 +109,15 @@ def max_error(obs, model) -> float:
109109

110110
def mae(
111111
obs: np.ndarray, model: np.ndarray, weights: Optional[np.ndarray] = None
112-
) -> float:
112+
) -> Any:
113113
"""alias for mean_absolute_error"""
114114
assert obs.size == model.size
115115
return mean_absolute_error(obs, model, weights)
116116

117117

118118
def mean_absolute_error(
119119
obs: np.ndarray, model: np.ndarray, weights: Optional[np.ndarray] = None
120-
) -> float:
120+
) -> Any:
121121
r"""Mean Absolute Error (MAE)
122122
123123
$$
@@ -133,12 +133,12 @@ def mean_absolute_error(
133133
return error
134134

135135

136-
def mape(obs: np.ndarray, model: np.ndarray) -> float:
136+
def mape(obs: np.ndarray, model: np.ndarray) -> Any:
137137
"""alias for mean_absolute_percentage_error"""
138138
return mean_absolute_percentage_error(obs, model)
139139

140140

141-
def mean_absolute_percentage_error(obs: np.ndarray, model: np.ndarray) -> float:
141+
def mean_absolute_percentage_error(obs: np.ndarray, model: np.ndarray) -> Any:
142142
r"""Mean Absolute Percentage Error (MAPE)
143143
144144
$$
@@ -161,7 +161,7 @@ def mean_absolute_percentage_error(obs: np.ndarray, model: np.ndarray) -> float:
161161

162162
def urmse(
163163
obs: np.ndarray, model: np.ndarray, weights: Optional[np.ndarray] = None
164-
) -> float:
164+
) -> Any:
165165
r"""Unbiased Root Mean Squared Error (uRMSE)
166166
167167
$$
@@ -190,7 +190,7 @@ def rmse(
190190
model: np.ndarray,
191191
weights: Optional[np.ndarray] = None,
192192
unbiased: bool = False,
193-
) -> float:
193+
) -> Any:
194194
"""alias for root_mean_squared_error"""
195195
return root_mean_squared_error(obs, model, weights, unbiased)
196196

@@ -200,7 +200,7 @@ def root_mean_squared_error(
200200
model: np.ndarray,
201201
weights: Optional[np.ndarray] = None,
202202
unbiased: bool = False,
203-
) -> float:
203+
) -> Any:
204204
r"""Root Mean Squared Error (RMSE)
205205
206206
$$
@@ -234,12 +234,12 @@ def root_mean_squared_error(
234234
return error
235235

236236

237-
def nse(obs: np.ndarray, model: np.ndarray) -> float:
237+
def nse(obs: np.ndarray, model: np.ndarray) -> Any:
238238
"""alias for nash_sutcliffe_efficiency"""
239239
return nash_sutcliffe_efficiency(obs, model)
240240

241241

242-
def nash_sutcliffe_efficiency(obs: np.ndarray, model: np.ndarray) -> float:
242+
def nash_sutcliffe_efficiency(obs: np.ndarray, model: np.ndarray) -> Any:
243243
r"""Nash-Sutcliffe Efficiency (NSE)
244244
245245
$$
@@ -261,12 +261,12 @@ def nash_sutcliffe_efficiency(obs: np.ndarray, model: np.ndarray) -> float:
261261

262262
if len(obs) == 0:
263263
return np.nan
264-
error = 1 - (np.sum((obs - model) ** 2) / np.sum((obs - np.mean(obs)) ** 2))
264+
error = 1 - (np.sum((obs - model) ** 2) / np.sum((obs - np.mean(obs)) ** 2)) # type: ignore
265265

266266
return error
267267

268268

269-
def kling_gupta_efficiency(obs: np.ndarray, model: np.ndarray) -> float:
269+
def kling_gupta_efficiency(obs: np.ndarray, model: np.ndarray) -> Any:
270270
r"""
271271
Kling-Gupta Efficiency (KGE)
272272
@@ -306,12 +306,12 @@ def kling_gupta_efficiency(obs: np.ndarray, model: np.ndarray) -> float:
306306
return res
307307

308308

309-
def kge(obs: np.ndarray, model: np.ndarray) -> float:
309+
def kge(obs: np.ndarray, model: np.ndarray) -> Any:
310310
"""alias for kling_gupta_efficiency"""
311311
return kling_gupta_efficiency(obs, model)
312312

313313

314-
def r2(obs: np.ndarray, model: np.ndarray) -> float:
314+
def r2(obs: np.ndarray, model: np.ndarray) -> Any:
315315
r"""Coefficient of determination (R2)
316316
317317
Pronounced 'R-squared'; the proportion of the variation in the dependent variable that is predictable from the independent variable(s), i.e. the proportion of explained variance.
@@ -339,18 +339,18 @@ def r2(obs: np.ndarray, model: np.ndarray) -> float:
339339
return np.nan
340340

341341
residual = model - obs
342-
SSr = np.sum(residual**2)
343-
SSt = np.sum((obs - obs.mean()) ** 2)
342+
SSr: Any = np.sum(residual**2)
343+
SSt: Any = np.sum((obs - obs.mean()) ** 2)
344344

345345
return 1 - SSr / SSt
346346

347347

348-
def mef(obs: np.ndarray, model: np.ndarray) -> float:
348+
def mef(obs: np.ndarray, model: np.ndarray) -> Any:
349349
"""alias for model_efficiency_factor"""
350350
return model_efficiency_factor(obs, model)
351351

352352

353-
def model_efficiency_factor(obs: np.ndarray, model: np.ndarray) -> float:
353+
def model_efficiency_factor(obs: np.ndarray, model: np.ndarray) -> Any:
354354
r"""Model Efficiency Factor (MEF)
355355
356356
Scale independent RMSE, standardized by Stdev of observations
@@ -373,12 +373,12 @@ def model_efficiency_factor(obs: np.ndarray, model: np.ndarray) -> float:
373373
return rmse(obs, model) / obs.std()
374374

375375

376-
def cc(obs: np.ndarray, model: np.ndarray, weights=None) -> float:
376+
def cc(obs: np.ndarray, model: np.ndarray, weights=None) -> Any:
377377
"""alias for corrcoef"""
378378
return corrcoef(obs, model, weights)
379379

380380

381-
def corrcoef(obs, model, weights=None) -> float:
381+
def corrcoef(obs, model, weights=None) -> Any:
382382
r"""Pearson’s Correlation coefficient (CC)
383383
384384
$$
@@ -405,12 +405,12 @@ def corrcoef(obs, model, weights=None) -> float:
405405
return C[0, 1] / np.sqrt(C[0, 0] * C[1, 1])
406406

407407

408-
def rho(obs: np.ndarray, model: np.ndarray) -> float:
408+
def rho(obs: np.ndarray, model: np.ndarray) -> Any:
409409
"""alias for spearmanr"""
410410
return spearmanr(obs, model)
411411

412412

413-
def spearmanr(obs: np.ndarray, model: np.ndarray) -> float:
413+
def spearmanr(obs: np.ndarray, model: np.ndarray) -> Any:
414414
r"""Spearman rank correlation coefficient
415415
416416
The rank correlation coefficient is similar to the Pearson correlation coefficient but
@@ -442,12 +442,12 @@ def spearmanr(obs: np.ndarray, model: np.ndarray) -> float:
442442
return scipy.stats.spearmanr(obs, model)[0]
443443

444444

445-
def si(obs: np.ndarray, model: np.ndarray) -> float:
445+
def si(obs: np.ndarray, model: np.ndarray) -> Any:
446446
"""alias for scatter_index"""
447447
return scatter_index(obs, model)
448448

449449

450-
def scatter_index(obs: np.ndarray, model: np.ndarray) -> float:
450+
def scatter_index(obs: np.ndarray, model: np.ndarray) -> Any:
451451
r"""Scatter index (SI)
452452
453453
Which is the same as the unbiased-RMSE normalized by the absolute mean of the observations.
@@ -468,7 +468,7 @@ def scatter_index(obs: np.ndarray, model: np.ndarray) -> float:
468468
return np.sqrt(np.mean(residual**2)) / np.mean(np.abs(obs))
469469

470470

471-
def scatter_index2(obs: np.ndarray, model: np.ndarray) -> float:
471+
def scatter_index2(obs: np.ndarray, model: np.ndarray) -> Any:
472472
r"""Alternative formulation of the scatter index (SI)
473473
474474
$$
@@ -483,17 +483,17 @@ def scatter_index2(obs: np.ndarray, model: np.ndarray) -> float:
483483
return np.nan
484484

485485
return np.sqrt(
486-
np.sum(((model - model.mean()) - (obs - obs.mean())) ** 2) / np.sum(obs**2)
486+
np.sum(((model - model.mean()) - (obs - obs.mean())) ** 2) / np.sum(obs**2) # type: ignore
487487
)
488488

489489

490-
def ev(obs: np.ndarray, model: np.ndarray) -> float:
490+
def ev(obs: np.ndarray, model: np.ndarray) -> Any:
491491
"""alias for explained_variance"""
492492
assert obs.size == model.size
493493
return explained_variance(obs, model)
494494

495495

496-
def explained_variance(obs: np.ndarray, model: np.ndarray) -> float:
496+
def explained_variance(obs: np.ndarray, model: np.ndarray) -> Any:
497497
r"""EV: Explained variance
498498
499499
EV is the explained variance and measures the proportion
@@ -520,10 +520,10 @@ def explained_variance(obs: np.ndarray, model: np.ndarray) -> float:
520520
if len(obs) == 0:
521521
return np.nan
522522

523-
nominator = np.sum((obs - obs.mean()) ** 2) - np.sum(
523+
nominator: Any = np.sum((obs - obs.mean()) ** 2) - np.sum( # type: ignore
524524
((obs - obs.mean()) - (model - model.mean())) ** 2
525525
)
526-
denominator = np.sum((obs - obs.mean()) ** 2)
526+
denominator: Any = np.sum((obs - obs.mean()) ** 2)
527527

528528
return nominator / denominator
529529

@@ -534,7 +534,7 @@ def pr(
534534
inter_event_level: float = 0.7,
535535
AAP: Union[int, float] = 2,
536536
inter_event_time: str = "36h",
537-
) -> float:
537+
) -> Any:
538538
"""alias for peak_ratio"""
539539
assert obs.size == model.size
540540
return peak_ratio(obs, model, inter_event_level, AAP, inter_event_time)
@@ -546,7 +546,7 @@ def peak_ratio(
546546
inter_event_level: float = 0.7,
547547
AAP: Union[int, float] = 2,
548548
inter_event_time: str = "36h",
549-
) -> float:
549+
) -> Any:
550550
r"""Peak Ratio
551551
552552
PR is the mean of the largest-N individual ratios of identified peaks in the
@@ -633,7 +633,7 @@ def peak_ratio(
633633
return res
634634

635635

636-
def willmott(obs: np.ndarray, model: np.ndarray) -> float:
636+
def willmott(obs: np.ndarray, model: np.ndarray) -> Any:
637637
r"""Willmott's Index of Agreement
638638
639639
A scaled representation of the predictive accuracy of the model against observations. A value of 1 indicates a perfect match, and 0 indicates no agreement at all.
@@ -662,13 +662,15 @@ def willmott(obs: np.ndarray, model: np.ndarray) -> float:
662662
return np.nan
663663

664664
residual = model - obs
665-
nominator = np.sum(residual**2)
666-
denominator = np.sum((np.abs(model - obs.mean()) + np.abs(obs - obs.mean())) ** 2)
665+
nominator: Any = np.sum(residual**2)
666+
denominator: Any = np.sum(
667+
(np.abs(model - obs.mean()) + np.abs(obs - obs.mean())) ** 2
668+
)
667669

668670
return 1 - nominator / denominator
669671

670672

671-
def hit_ratio(obs: np.ndarray, model: np.ndarray, a=0.1) -> float:
673+
def hit_ratio(obs: np.ndarray, model: np.ndarray, a=0.1) -> Any:
672674
r"""Fraction within obs ± acceptable deviation
673675
674676
$$
@@ -693,7 +695,7 @@ def hit_ratio(obs: np.ndarray, model: np.ndarray, a=0.1) -> float:
693695
return np.mean(np.abs(obs - model) < a)
694696

695697

696-
def lin_slope(obs: np.ndarray, model: np.ndarray, reg_method="ols") -> float:
698+
def lin_slope(obs: np.ndarray, model: np.ndarray, reg_method="ols") -> Any:
697699
r"""Slope of the regression line.
698700
699701
$$
@@ -736,11 +738,11 @@ def _linear_regression(
736738
return slope, intercept
737739

738740

739-
def _std_obs(obs: np.ndarray, model: np.ndarray) -> float:
741+
def _std_obs(obs: np.ndarray, model: np.ndarray) -> Any:
740742
return obs.std()
741743

742744

743-
def _std_mod(obs: np.ndarray, model: np.ndarray) -> float:
745+
def _std_mod(obs: np.ndarray, model: np.ndarray) -> Any:
744746
return model.std()
745747

746748

@@ -894,7 +896,7 @@ def _c_residual(obs: np.ndarray, model: np.ndarray) -> np.ndarray:
894896
return resi
895897

896898

897-
def c_bias(obs: np.ndarray, model: np.ndarray) -> float:
899+
def c_bias(obs: np.ndarray, model: np.ndarray) -> Any:
898900
"""Circular bias (mean error)
899901
900902
Parameters
@@ -924,7 +926,7 @@ def c_bias(obs: np.ndarray, model: np.ndarray) -> float:
924926
return circmean(resi, low=-180.0, high=180.0)
925927

926928

927-
def c_max_error(obs: np.ndarray, model: np.ndarray) -> float:
929+
def c_max_error(obs: np.ndarray, model: np.ndarray) -> Any:
928930
"""Circular max error
929931
930932
Parameters
@@ -962,7 +964,7 @@ def c_mean_absolute_error(
962964
obs: np.ndarray,
963965
model: np.ndarray,
964966
weights: Optional[np.ndarray] = None,
965-
) -> float:
967+
) -> Any:
966968
"""Circular mean absolute error
967969
968970
Parameters
@@ -990,7 +992,7 @@ def c_mae(
990992
obs: np.ndarray,
991993
model: np.ndarray,
992994
weights: Optional[np.ndarray] = None,
993-
) -> float:
995+
) -> Any:
994996
"""alias for circular mean absolute error"""
995997
return c_mean_absolute_error(obs, model, weights)
996998

@@ -999,7 +1001,7 @@ def c_root_mean_squared_error(
9991001
obs: np.ndarray,
10001002
model: np.ndarray,
10011003
weights: Optional[np.ndarray] = None,
1002-
) -> float:
1004+
) -> Any:
10031005
"""Circular root mean squared error
10041006
10051007
Parameters
@@ -1026,7 +1028,7 @@ def c_rmse(
10261028
obs: np.ndarray,
10271029
model: np.ndarray,
10281030
weights: Optional[np.ndarray] = None,
1029-
) -> float:
1031+
) -> Any:
10301032
"""alias for circular root mean squared error"""
10311033
return c_root_mean_squared_error(obs, model, weights)
10321034

@@ -1035,7 +1037,7 @@ def c_unbiased_root_mean_squared_error(
10351037
obs: np.ndarray,
10361038
model: np.ndarray,
10371039
weights: Optional[np.ndarray] = None,
1038-
) -> float:
1040+
) -> Any:
10391041
"""Circular unbiased root mean squared error
10401042
10411043
Parameters
@@ -1065,7 +1067,7 @@ def c_urmse(
10651067
obs: np.ndarray,
10661068
model: np.ndarray,
10671069
weights: Optional[np.ndarray] = None,
1068-
) -> float:
1070+
) -> Any:
10691071
"""alias for circular unbiased root mean squared error"""
10701072
return c_unbiased_root_mean_squared_error(obs, model, weights)
10711073

0 commit comments

Comments
 (0)