From beb0da5470f3dc49b43d3d43ee042e3060d69e11 Mon Sep 17 00:00:00 2001 From: Aidan Griffiths Date: Thu, 19 Oct 2023 09:56:45 +1100 Subject: [PATCH] refactor: remove deprecated typing alias' for builtins and collections.abc --- .../categorical/multicategorical_impl.py | 19 +++++++------------ src/scores/continuous/murphy_impl.py | 3 ++- src/scores/probability/crps_impl.py | 4 ++-- src/scores/probability/functions.py | 4 ++-- src/scores/typing.py | 4 ++-- src/scores/utils.py | 3 ++- 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/scores/categorical/multicategorical_impl.py b/src/scores/categorical/multicategorical_impl.py index 20ec98cb7..c93eb6a7d 100644 --- a/src/scores/categorical/multicategorical_impl.py +++ b/src/scores/categorical/multicategorical_impl.py @@ -1,13 +1,14 @@ """ This module contains methods which may be used for scoring multicategorical forecasts """ -from typing import Optional, Sequence, Union +from collections.abc import Sequence +from typing import Optional, Union import numpy as np import xarray as xr from scores.functions import apply_weights -from scores.utils import check_dims, dims_complement, gather_dimensions +from scores.utils import check_dims, gather_dimensions def firm( @@ -19,7 +20,7 @@ def firm( discount_distance: Optional[float] = 0, reduce_dims: Optional[Sequence[str]] = None, preserve_dims: Optional[Sequence[str]] = None, - weights: Optional[xr.DataArray]=None + weights: Optional[xr.DataArray] = None, ) -> xr.Dataset: """ Calculates the FIxed Risk Multicategorical (FIRM) score including the @@ -105,14 +106,10 @@ def firm( ) total_score = [] for categorical_threshold, weight in zip(categorical_thresholds, threshold_weights): - score = weight * _single_category_score( - fcst, obs, risk_parameter, categorical_threshold, discount_distance - ) + score = weight * _single_category_score(fcst, obs, risk_parameter, categorical_threshold, discount_distance) total_score.append(score) summed_score = sum(total_score) - reduce_dims = gather_dimensions( - fcst.dims, obs.dims, reduce_dims, preserve_dims - ) + reduce_dims = gather_dimensions(fcst.dims, obs.dims, reduce_dims, preserve_dims) summed_score = apply_weights(summed_score, weights) score = summed_score.mean(dim=reduce_dims) @@ -133,9 +130,7 @@ def _check_firm_inputs( raise ValueError("`categorical_thresholds` must have at least one threshold") if not len(categorical_thresholds) == len(threshold_weights): - raise ValueError( - "The length of `categorical_thresholds` and `weights` must be equal" - ) + raise ValueError("The length of `categorical_thresholds` and `weights` must be equal") if risk_parameter <= 0 or risk_parameter >= 1: raise ValueError("0 < `risk_parameter` < 1 must be satisfied") diff --git a/src/scores/continuous/murphy_impl.py b/src/scores/continuous/murphy_impl.py index 7bee53cd6..db0761d49 100644 --- a/src/scores/continuous/murphy_impl.py +++ b/src/scores/continuous/murphy_impl.py @@ -1,7 +1,8 @@ """ Murphy score """ -from typing import Literal, Optional, Sequence, Union +from collections.abc import Sequence +from typing import Literal, Optional, Union import numpy as np import xarray as xr diff --git a/src/scores/probability/crps_impl.py b/src/scores/probability/crps_impl.py index d330ff258..cdfcec9c1 100644 --- a/src/scores/probability/crps_impl.py +++ b/src/scores/probability/crps_impl.py @@ -2,8 +2,8 @@ This module supports the implementation of the CRPS scoring function, drawing from additional functions. The primary method, `crps_cdf` is imported into the probability module to be part of the probability API """ - -from typing import Iterable, Literal, Optional +from collections.abc import Iterable +from typing import Literal, Optional import numpy as np import pandas as pd diff --git a/src/scores/probability/functions.py b/src/scores/probability/functions.py index 70f2abf8d..4d9eabd62 100644 --- a/src/scores/probability/functions.py +++ b/src/scores/probability/functions.py @@ -2,8 +2,8 @@ This module contains a variety of functions which modify data in various ways to process data structures to support probablistic verification. """ - -from typing import Iterable, Literal, Optional +from collections.abc import Iterable +from typing import Literal, Optional import numpy as np import pandas as pd diff --git a/src/scores/typing.py b/src/scores/typing.py index f7f4cbbf9..f8a2a39d7 100644 --- a/src/scores/typing.py +++ b/src/scores/typing.py @@ -2,8 +2,8 @@ This module contains various compound or union types which can be used across the codebase to ensure a consistent approach to typing is handled. """ - -from typing import Hashable, Iterable, Optional, Union +from collections.abc import Hashable, Iterable +from typing import Optional, Union import pandas as pd import xarray as xr diff --git a/src/scores/utils.py b/src/scores/utils.py index a887cad0e..29572d25d 100644 --- a/src/scores/utils.py +++ b/src/scores/utils.py @@ -2,7 +2,8 @@ Contains frequently-used functions of a general nature within scores """ import warnings -from typing import Hashable, Iterable, List, Optional +from collections.abc import Hashable, Iterable +from typing import Optional import xarray as xr