Skip to content

xclim.DetrendedQuantileMapping with train_test_split="unfair" failing #833

@aaronspring

Description

@aaronspring

Describe the bug
xclim.DetrendedQuantileMapping with train_test_split="unfair" failing, but works for QuantileDeltaMapping, EmpiricalQuantileMapping and Scaling.

Origin/discovered in #832

Code Sample

climpred/tests/test_bias_removal.py: https://github.com/pangeo-data/climpred/pull/832/files#r1349961062 skipping in @pytest.mark.parametrize("how", XCLIM_BIAS_CORRECTION_METHODS) on test_remove_bias_unfair_artificial_skill_over_fair_xclim

>           he_unfair = he.remove_bias(
                how=how,
                alignment=alignment,
                group=group,
                train_test_split="unfair",
                **metric_kwargs,
            )

climpred/tests/test_bias_removal.py:312:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
climpred/classes.py:2835: in remove_bias
    self = func(
climpred/bias_removal.py:731: in xclim_sdba
    bias_removed_hind = hindcast.verify(
climpred/classes.py:2376: in verify
    res = self._apply_climpred_function(
climpred/classes.py:1844: in _apply_climpred_function
    return func(hind.drop_vars(drop_init), verif.drop_vars(drop_obs), **kwargs)
climpred/classes.py:2291: in _verify
    metric_over_leads = [
climpred/classes.py:2292: in <listcomp>
    _apply_metric_at_given_lead(
climpred/prediction.py:100: in _apply_metric_at_given_lead
    result = metric.function(lforecast, lverif, dim=dim, **metric_kwargs)
climpred/bias_removal.py:714: in bc_func
    c[v] = adjustment(reference[v], model[v], data_to_be_corrected[v])
climpred/bias_removal.py:707: in adjustment
    data_to_be_corrected = dqm.adjust(data_to_be_corrected, **adjust_kwargs)
../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xclim/sdba/adjustment.py:215: in adjust
    out = self._adjust(sim, *args, **kwargs)
../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xclim/sdba/adjustment.py:488: in _adjust
    scen = dqm_adjust(
<boltons.funcutils.FunctionBuilder-255>:2: in _map_blocks
    ???
../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xclim/sdba/base.py:477: in _parse_group
    return func(*args, **kwargs)
../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xclim/sdba/base.py:685: in _map_blocks
    out = ds.map_blocks(
../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xarray/core/dataset.py:8646: in map_blocks
    return map_blocks(func, self, args, kwargs, template)
../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xarray/core/parallel.py:318: in map_blocks
    return func(obj, *args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

dsblock = <xarray.Dataset>
Dimensions:    (quantiles: 20, group: 1, time: 119, member: 10)
Coordinates:
  * quantiles  (quantile...lim.sdba.adjustment.DetrendedQuanti...
    adj_params:          DetrendedQuantileMapping(group=Grouper(add_dims=['me...
kwargs = {'detrend': 1, 'extrapolation': 'constant', 'group': Grouper(add_dims=['member'], name='time'), 'interp': 'nearest', ...}

    def _call_and_transpose_on_exit(dsblock, **kwargs):
        """Call the decorated func and transpose to ensure the same dim order as on the templace."""
        try:
            _decode_cf_coords(dsblock)
            out = func(dsblock, **kwargs).transpose(*all_dims)
        except Exception as err:
>           raise ValueError(
                f"{func.__name__} failed on block with coords : {dsblock.coords}."
            ) from err
E           ValueError: dqm_adjust failed on block with coords : Coordinates:
E             * quantiles  (quantiles) float64 0.025 0.075 0.125 0.175 ... 0.875 0.925 0.975
E             * group      (group) int64 1
E             * time       (time) object 2000-06-01 00:00:00 ... 2010-04-01 00:00:00
E             * member     (member) float32 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0.

../../mambaforge/envs/climpred-dev/lib/python3.10/site-packages/xclim/sdba/base.py:668: ValueError

Expected behavior
pass

Output of climpred.show_versions()

commit: None python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:41:52) [Clang 15.0.7 ] python-bits: 64 OS: Darwin OS-release: 22.6.0 machine: arm64 processor: arm byteorder: little LC_ALL: None LANG: None LOCALE: None.UTF-8

climpred: 2.3.0.post13
xarray: 2023.9.0
pandas: 1.5.3
numpy: 1.24.4
scipy: 1.11.3
cftime: 1.6.2
netcdf4: None
nc_time_axis: 1.4.1
matplotlib: 3.8.0
cf_xarray: 0.8.4
xclim: 0.42.0
dask: 2023.9.3
distributed: 2023.9.3
setuptools: 68.2.2
pip: 23.2.1
conda: None
IPython: 8.16.1
sphinx: 5.3.0

worked before unpinning xclim and xarray: https://github.com/pangeo-data/climpred/tree/1424e89e9bdf3eb1ae47d581be2953ede0b98996, see #831 and #832

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions