From 64ecae41e3a27087aa05b7ea27a23551d10a1f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Dupuis?= Date: Thu, 20 Jun 2024 16:53:21 -0400 Subject: [PATCH] dim arg in measures_improvement --- AUTHORS.rst | 1 + CHANGELOG.rst | 3 ++- src/xscen/diagnostics.py | 26 ++++++++++++++------------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index c481db41..63c5609a 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -23,3 +23,4 @@ Contributors * Yannick Rousseau * Marco Braun `@vindelico `_ * Sarah-Claude Bourdeau-Goulet `@sarahclaude `_ +* Éric Dupuis `@coxipi `_ diff --git a/CHANGELOG.rst b/CHANGELOG.rst index bf392919..e76555b8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,7 @@ Changelog v0.9.2 (Unreleased) ------------------- -Contributors to this version: Juliette Lavoie (:user:`juliettelavoie`). +Contributors to this version: Juliette Lavoie (:user:`juliettelavoie`), Éric Dupuis (:user:`coxipi`) @@ -12,6 +12,7 @@ Internal changes ^^^^^^^^^^^^^^^^ * Include domain in `weight_location` in ``regrid_dataset``. (:pull:`414`). * Added pins to xarray, xclim, h5py, and netcdf4. (:pull:`414`). +* `xs.diagnostics.measures_improvement` now accepts `dim`, which specifies `dimension(s)` on which the proportion of improved pixels are computed. Bug fixes ^^^^^^^^^ diff --git a/src/xscen/diagnostics.py b/src/xscen/diagnostics.py index c916cf53..4396107d 100644 --- a/src/xscen/diagnostics.py +++ b/src/xscen/diagnostics.py @@ -525,7 +525,9 @@ def measures_heatmap( def measures_improvement( - meas_datasets: Union[list[xr.Dataset], dict], to_level: str = "diag-improved" + meas_datasets: Union[list[xr.Dataset], dict], + dim: str | Sequence[str] | None = None, + to_level: str = "diag-improved", ) -> xr.Dataset: """ Calculate the fraction of improved grid points for each property between two datasets of measures. @@ -536,6 +538,8 @@ def measures_improvement( List of 2 datasets: Initial dataset of measures and final (improved) dataset of measures. Both datasets must have the same variables. It is also possible to pass a dictionary where the values are the datasets and the key are not used. + dim : str or sequence of str, optional + Dimension(s) on which to compute the percentage of improved grid points. to_level: str processing_level to assign to the output dataset @@ -554,24 +558,22 @@ def measures_improvement( ) ds1 = meas_datasets[0] ds2 = meas_datasets[1] + if dim is None: + dims = ds1.dims + dims = [dim] if isinstance(dim, str) else dim + percent_better = [] for var in ds2.data_vars: if "xclim.sdba.measures.RATIO" in ds1[var].attrs["history"]: diff_bias = abs(ds1[var] - 1) - abs(ds2[var] - 1) else: diff_bias = abs(ds1[var]) - abs(ds2[var]) - diff_bias = diff_bias.values.ravel() - diff_bias = diff_bias[~np.isnan(diff_bias)] - - total = ds2[var].values.ravel() - total = total[~np.isnan(total)] + total_improved = (diff_bias >= 0).sum(dim=dims) + total_notnull = diff_bias.notnull().sum(dim=dims) + percent_better_var = total_improved / total_notnull + percent_better.append(percent_better_var.expand_dims({"properties": [var]})) - improved = diff_bias >= 0 - percent_better.append(np.sum(improved) / len(total)) - - ds_better = xr.DataArray( - percent_better, coords={"properties": list(ds2.data_vars)}, dims="properties" - ) + ds_better = xr.concat(percent_better, dim="properties") ds_better = ds_better.to_dataset(name="improved_grid_points")