Skip to content

Commit

Permalink
Black reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tennlee committed Aug 25, 2023
1 parent ed1481d commit b23cc99
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 149 deletions.
69 changes: 35 additions & 34 deletions src/scores/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,42 @@ def mse(fcst, obs, reduce_dims=None, preserve_dims=None, weights=None):
return _mse


def rmse(fcst, obs, reduce_dims = None, preserve_dims = None, weights=None):
def rmse(fcst, obs, reduce_dims=None, preserve_dims=None, weights=None):
"""Calculate the Root Mean Squared Error from xarray or pandas objects.
A detailed explanation is on [Wikipedia](https://en.wikipedia.org/wiki/Root-mean-square_deviation)
Dimensional reduction is not supported for pandas and the user should
convert their data to xarray to formulate the call to the metric.
At most one of `reduce_dims` and `preserve_dims` may be specified.
Specifying both will result in an exception.
Args:
fcst Union[xr.Dataset, xr.DataArray, pd.Dataframe, pd.Series]: Forecast
or predicted variables in xarray or pandas.
obs (Union[xr.Dataset, xr.DataArray, pd.Dataframe, pd.Series]): Observed
variables in xarray or pandas.
reduce_dims (Union[str, Iterable[str]): Optionally specify which dimensions to reduce when
calculating RMSE. All other dimensions will be preserved.
preserve_dims (Union[str, Iterable[str]): Optionally specify which dimensions to preserve
when calculating RMSE. All other dimensions will be reduced.
As a special case, 'all' will allow all dimensions to be
preserved. In this case, the result will be in the same
shape/dimensionality as the forecast, and the errors will be
the absolute error at each point (i.e. single-value comparison
against observed), and the forecast and observed dimensions
must match precisely.
weights: Not yet implemented. Allow weighted averaging (e.g. by
area, by latitude, by population, custom)
Returns:
Union[xr.Dataset, xr.DataArray, pd.Dataframe, pd.Series]: An object containing
a single floating point number representing the root mean squared
error for the supplied data. All dimensions will be reduced.
Otherwise: Returns an object representing the root mean squared error,
reduced along the relevant dimensions and weighted appropriately.
A detailed explanation is on [Wikipedia](https://en.wikipedia.org/wiki/Root-mean-square_deviation)
Dimensional reduction is not supported for pandas and the user should
convert their data to xarray to formulate the call to the metric.
At most one of `reduce_dims` and `preserve_dims` may be specified.
Specifying both will result in an exception.
Args:
fcst Union[xr.Dataset, xr.DataArray, pd.Dataframe, pd.Series]: Forecast
or predicted variables in xarray or pandas.
obs (Union[xr.Dataset, xr.DataArray, pd.Dataframe, pd.Series]): Observed
variables in xarray or pandas.
reduce_dims (Union[str, Iterable[str]): Optionally specify which dimensions to reduce when
calculating RMSE. All other dimensions will be preserved.
preserve_dims (Union[str, Iterable[str]): Optionally specify which dimensions to preserve
when calculating RMSE. All other dimensions will be reduced.
As a special case, 'all' will allow all dimensions to be
preserved. In this case, the result will be in the same
shape/dimensionality as the forecast, and the errors will be
the absolute error at each point (i.e. single-value comparison
against observed), and the forecast and observed dimensions
must match precisely.
weights: Not yet implemented. Allow weighted averaging (e.g. by
area, by latitude, by population, custom)
Returns:
Union[xr.Dataset, xr.DataArray, pd.Dataframe, pd.Series]: An object containing
a single floating point number representing the root mean squared
error for the supplied data. All dimensions will be reduced.
Otherwise: Returns an object representing the root mean squared error,
reduced along the relevant dimensions and weighted appropriately.
"""
_mse = mse(fcst=fcst, obs=obs, reduce_dims=reduce_dims, preserve_dims=preserve_dims, weights=weights)
Expand All @@ -100,6 +100,7 @@ def rmse(fcst, obs, reduce_dims = None, preserve_dims = None, weights=None):

return _rmse


def mae(fcst, obs, reduce_dims=None, preserve_dims=None, weights=None):
"""Calculates the mean absolute error from forecast and observed data.
Expand Down
23 changes: 12 additions & 11 deletions src/scores/functions.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import numpy as np


def apply_weights(values, weights=None):
'''
"""
Returns:
A new array with the elements of values multiplied by the specified weights.
Args:
- weights: The weightings to be used at every location in the values array. If weights contains additional
dimensions, these will be taken to mean that multiple weightings are wanted simultaneoulsy, and these
- weights: The weightings to be used at every location in the values array. If weights contains additional
dimensions, these will be taken to mean that multiple weightings are wanted simultaneoulsy, and these
dimensions will be added to the new array.
- values: The unweighted values to be used as the basis for weighting calculation
Note - this weighting function is different to the .weighted method contained in xarray in that xarray's
method does not allow NaNs to be present in the weights or data.
'''
Note - this weighting function is different to the .weighted method contained in xarray in that xarray's
method does not allow NaNs to be present in the weights or data.
"""

if weights is not None:
result = values * weights
return result
Expand All @@ -24,11 +25,11 @@ def apply_weights(values, weights=None):


def create_latitude_weights(latitudes):
'''
"""
A common way of weighting errors is to make them proportional to the amount of area
which is contained in a particular region. This is approximated by the cosine
of the latitude on an LLXY grid. Nuances not accounted for include the variation in
latitude across the region, or the irregularity of the surface of the earth.
'''
"""
weights = np.cos(np.deg2rad(latitudes))
return weights
return weights
2 changes: 1 addition & 1 deletion src/scores/probability/crps_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def crps_cdf(
include_components=include_components,
)

weighted = scores.functions.apply_weights(result, weights)
weighted = scores.functions.apply_weights(result, weights)

dims_to_collapse = scores.utils.dims_complement(result, dims=dims)
result = weighted.mean(dim=dims_to_collapse)
Expand Down
19 changes: 8 additions & 11 deletions src/scores/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
import xarray as xr


WARN_ALL_DATA_CONFLICT_MSG = '''
WARN_ALL_DATA_CONFLICT_MSG = """
You are requesting to reduce or preserve every dimension by specifying the string 'all'.
In this case, 'all' is also a named dimension in your data, leading to an ambiguity.
In order to reduce or preserve the named data dimension, specify ['all'] as a list item
rather than relying on string interpretation. The program will continue to interpret the
string as an instruction to reduce or preserve every dimension.
'''
"""

ERROR_SPECIFIED_NONPRESENT_PRESERVE_DIMENSION = '''
ERROR_SPECIFIED_NONPRESENT_PRESERVE_DIMENSION = """
You are requesting to preserve a dimension which does not appear in your data (fcst or obs).
It is ambiguous how to proceed therefore an exception has been raised instead.
'''
"""

ERROR_SPECIFIED_NONPRESENT_REDUCE_DIMENSION = '''
ERROR_SPECIFIED_NONPRESENT_REDUCE_DIMENSION = """
You are requesting to reduce a dimension which does not appear in your data (fcst or obs).
It is ambiguous how to proceed therefore an exception has been raised instead.
'''
"""

ERROR_OVERSPECIFIED_PRESERVE_REDUCE = '''
ERROR_OVERSPECIFIED_PRESERVE_REDUCE = """
You have specified both preserve_dims and reduce_dims. This method doesn't know how
to properly interpret that, therefore an exception has been raised.
'''
"""


class DimensionError(Exception):
Expand Down Expand Up @@ -76,7 +76,6 @@ def gather_dimensions(fcst_dims, obs_dims, weights_dims=None, reduce_dims=None,

# Handle preserve_dims case
if preserve_dims is not None:

if preserve_dims == "all":
return set([])

Expand Down Expand Up @@ -206,5 +205,3 @@ def check_dims(xr_data, expected_dims, mode=None):
f"Dimensions {list(xr_data[data_var].dims)} of data variable "
f"'{data_var}' are not {mode} to the dimensions {sorted(dims_set)}"
)


Loading

0 comments on commit b23cc99

Please sign in to comment.