Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditions to match ref.time & hist.time, and also ref.time.size & sim.time.size #1995

Merged
merged 32 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
561f812
check_matching_time method: ref & hist must match
coxipi Nov 13, 2024
85f4f9f
add pull number
coxipi Nov 13, 2024
7dc378e
Merge branch 'main' into check_matching_times
coxipi Nov 18, 2024
553716b
Merge branch 'main' into check_matching_times
coxipi Nov 19, 2024
60cd830
add new class attr
coxipi Nov 25, 2024
bb69de3
Merge branch 'main' of https://github.com/Ouranosinc/xclim into check…
coxipi Nov 25, 2024
d42044f
Adjust can modify sim time if needed
coxipi Nov 25, 2024
90053c5
add tests
coxipi Nov 25, 2024
c037542
add skip if OT not installed
coxipi Nov 26, 2024
7ed51be
clearer attrs, more checks
coxipi Nov 27, 2024
ac12ab5
update CHANGELOG
coxipi Nov 27, 2024
4c873fe
typo
coxipi Nov 27, 2024
1a11cb6
more checks in many classes
coxipi Nov 27, 2024
e1aca56
LOCI check calendars
coxipi Nov 27, 2024
57ccf19
forgot to refactor in tests
coxipi Nov 27, 2024
e4a1a40
more refactoring to pass on and add new test
coxipi Nov 27, 2024
60a920b
second test has a more general signature
coxipi Nov 27, 2024
e9ea021
fix conda (from :pull:1988)
coxipi Nov 27, 2024
c8caced
remove defaults, remove conda.anaconda.com override from lint
Zeitsperre Nov 28, 2024
7846418
allow connections to conda.anaconda.org
Zeitsperre Nov 26, 2024
c7241a7
use load_dataset with xr.tutorial
coxipi Nov 28, 2024
f55c64b
Merge branch 'check_matching_times' of https://github.com/Ouranosinc/…
coxipi Nov 28, 2024
68cbb8d
temp deactivate time_size checks for OTC/dOTC
coxipi Nov 28, 2024
6331679
remove test_different_times for now
coxipi Nov 28, 2024
d385c2e
change nan handling in dOTC
coxipi Nov 28, 2024
d17489c
Merge branch 'check_matching_times' of https://github.com/Ouranosinc/…
coxipi Nov 28, 2024
5278a0e
remove test print
coxipi Nov 28, 2024
6584953
Fix nan handling in dOTC (#2005)
coxipi Nov 28, 2024
ddba143
add fix nan handling description
coxipi Nov 28, 2024
880f98c
refactor var name
coxipi Nov 28, 2024
066d3f3
add training with different times possibility
coxipi Dec 9, 2024
58f0ada
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Bug fixes
Internal changes
^^^^^^^^^^^^^^^^
* Changed french translations with word "pluvieux" to "avec précipitations". (:issue:`1960`, :pull:`1994`).
* Using different time for `ref` and `hist` is now explicitly forbidden in many bias adjustment methods (e.g. `EmpiricalQuantileMapping`). (:issue:`1903`, :pull:`1995`)
* `streamflow` entry replaced with `q` in ``variables.yml``. (:issue:`1912`, :pull:`1996`)
* In order to address 403 (forbidden) request errors when retrieving data from GitHub via ReadTheDocs, the ``nimbus`` class has been modified to use an overloaded `fetch` method that appends a User-Agent header to the request. (:pull:`2001`).
* Addressed a very rare race condition that can happen if `pytest` is tearing down the test environment when running across multiple workers. (:pull:`1863`).
Expand Down
31 changes: 27 additions & 4 deletions xclim/sdba/adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class BaseAdjustment(ParametrizableWithDataset):
"""

_allow_diff_calendars = True
_allow_diff_training_times = True
_attribute = "_xclim_adjustment"

def __init__(self, *args, _trained=False, **kwargs):
Expand Down Expand Up @@ -208,6 +209,12 @@ def __convert_units_to(_input_da, _internal_dim, _internal_target):
convert_units_to(inda, target, context="infer") for inda in inputs
), target

@classmethod
def _check_matching_time(cls, ref, hist):
"""Raise an error ref and hist times don't match."""
if all(ref.time == hist.time) is False:
raise ValueError("`ref` and `hist` should have the same time arrays.")

@classmethod
def _train(cls, ref, hist, **kwargs):
raise NotImplementedError()
Expand Down Expand Up @@ -259,6 +266,9 @@ def train(cls, ref: DataArray, hist: DataArray, **kwargs) -> TrainAdjust:
else:
train_units = ""

if cls._allow_diff_training_times is False:
cls._check_matching_time(ref, hist)

ds, params = cls._train(ref, hist, **kwargs)
obj = cls(
_trained=True,
Expand Down Expand Up @@ -340,6 +350,8 @@ class Adjust(BaseAdjustment):
and returning the scen dataset/array.
"""

_replace_sim_time = True

@classmethod
def adjust(
cls,
Expand Down Expand Up @@ -370,6 +382,13 @@ def adjust(
sim = hist.copy()
sim.attrs["_is_hist"] = True

# This below implies that ref.time and sim.time have the same size
# Since `ref,hist, sim` are in the same `map_groups` call, they must have
# the same time
if cls.__replace_sim_time:
sim_time = sim.time
sim["time"] = ref["time"]

coxipi marked this conversation as resolved.
Show resolved Hide resolved
kwargs = parse_group(cls._adjust, kwargs)
skip_checks = kwargs.pop("skip_input_checks", False)

Expand All @@ -385,6 +404,8 @@ def adjust(
out = out.rename("scen").to_dataset()

scen = out.scen
if cls.__replace_sim_time:
scen["time"] = sim_time

params = ", ".join([f"{k}={repr(v)}" for k, v in kwargs.items()])
infostr = f"{cls.__name__}.adjust(ref, hist, sim, {params})"
Expand Down Expand Up @@ -442,7 +463,7 @@ class EmpiricalQuantileMapping(TrainAdjust):
:cite:cts:`sdba-deque_frequency_2007`
"""

_allow_diff_calendars = False
_allow_diff_training_times = False

@classmethod
def _train(
Expand Down Expand Up @@ -542,7 +563,7 @@ class DetrendedQuantileMapping(TrainAdjust):

"""

_allow_diff_calendars = False
_allow_diff_training_times = False

@classmethod
def _train(
Expand Down Expand Up @@ -869,7 +890,7 @@ class LOCI(TrainAdjust):
:cite:cts:`sdba-schmidli_downscaling_2006`
"""

_allow_diff_calendars = False
_allow_diff_training_times = False

@classmethod
def _train(
Expand Down Expand Up @@ -921,7 +942,7 @@ class Scaling(TrainAdjust):
The interpolation method to use then interpolating the adjustment factors. Defaults to "nearest".
"""

_allow_diff_calendars = False
_allow_diff_training_times = False

@classmethod
def _train(
Expand Down Expand Up @@ -1738,6 +1759,8 @@ class MBCn(TrainAdjust):
Only "time" and "time.dayofyear" (with a suitable window) are implemented as possible values for `group`.
"""

_allow_diff_training_times = False

@classmethod
def _train(
cls,
Expand Down
Loading