Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed May 6, 2022
1 parent 999bb4e commit e48deb8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 32 deletions.
58 changes: 47 additions & 11 deletions icclim/ecad_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
metadata to it.
"""
import re
from typing import Callable, Optional, Tuple, Any
from typing import Any, Callable, Optional, Tuple
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -823,7 +823,11 @@ def _compute_percentile_doy(
callback: Callable = None,
) -> DataArray:
per = percentile_doy(
da, window, percentile, alpha=interpolation.alpha, beta=interpolation.beta,
da,
window,
percentile,
alpha=interpolation.alpha,
beta=interpolation.beta,
)
if callback is not None:
callback(50)
Expand Down Expand Up @@ -859,7 +863,10 @@ def _filter_in_wet_days(da: DataArray, dry_day_value: float):


def _compute_threshold_index(
da: DataArray, threshold: float, freq_config: dict, xclim_index_fun: Callable,
da: DataArray,
threshold: float,
freq_config: dict,
xclim_index_fun: Callable,
) -> DataArray:
result = xclim_index_fun(da, thresh=f"{threshold} °C", **freq_config)
return result
Expand All @@ -877,7 +884,11 @@ def _compute_spell_duration(
xclim_index_fun: Callable,
) -> Tuple[DataArray, Optional[DataArray]]:
per = _compute_percentile_doy(
cf_var.reference_da, per_thresh, per_window, per_interpolation, callback,
cf_var.reference_da,
per_thresh,
per_window,
per_interpolation,
callback,
)
run_bootstrap = _can_run_bootstrap(cf_var)
result = xclim_index_fun(
Expand Down Expand Up @@ -950,16 +961,26 @@ def compute_compound_index(
Otherwise, returns the index_result
"""
tas_per = _compute_percentile_doy(
tas.reference_da, tas_per_thresh, per_window, per_interpolation, callback,
tas.reference_da,
tas_per_thresh,
per_window,
per_interpolation,
callback,
)
tas_per = tas_per.squeeze(PERCENTILES_COORD, drop=True)
pr_in_base = _filter_in_wet_days(pr.reference_da, dry_day_value=np.NAN)
pr_out_of_base = _filter_in_wet_days(pr.study_da, dry_day_value=0)
pr_per = _compute_percentile_doy(
pr_in_base, pr_per_thresh, per_window, per_interpolation, callback,
pr_in_base,
pr_per_thresh,
per_window,
per_interpolation,
callback,
)
pr_per = pr_per.squeeze(PERCENTILES_COORD, drop=True)
result = xclim_index_fun(tas.study_da, tas_per, pr_out_of_base, pr_per, **freq_config)
result = xclim_index_fun(
tas.study_da, tas_per, pr_out_of_base, pr_per, **freq_config
)
if save_percentile:
# FIXME, not consistent with other percentile based indices
# We should probably return a Tuple (res, [tas_per, pr_per])
Expand All @@ -982,7 +1003,11 @@ def _compute_rxxptot(
base_wet_days, per_interpolation, pr_per_thresh
)
result = atmos.fraction_over_precip_thresh(
pr.study_da, per, thresh="1 mm/day", **freq_config, bootstrap=False,
pr.study_da,
per,
thresh="1 mm/day",
**freq_config,
bootstrap=False,
).squeeze(PERCENTILES_COORD, drop=True)
result = result * 100
result.attrs["units"] = "%"
Expand All @@ -1004,7 +1029,11 @@ def _compute_rxxp(
base_wet_days, per_interpolation, pr_per_thresh
)
result = atmos.days_over_precip_thresh(
pr.study_da, per, thresh="1 mm/day", **freq_config, bootstrap=False,
pr.study_da,
per,
thresh="1 mm/day",
**freq_config,
bootstrap=False,
)
result = result.squeeze(PERCENTILES_COORD, drop=True)
if is_percent:
Expand All @@ -1027,10 +1056,17 @@ def _compute_temperature_percentile_index(
) -> Tuple[DataArray, Optional[DataArray]]:
run_bootstrap = _can_run_bootstrap(cf_var)
per = _compute_percentile_doy(
cf_var.reference_da, tas_per_thresh, per_window, per_interpolation, callback,
cf_var.reference_da,
tas_per_thresh,
per_window,
per_interpolation,
callback,
).compute()
result = xclim_index_fun(
cf_var.study_da, per, **freq_config, bootstrap=run_bootstrap,
cf_var.study_da,
per,
**freq_config,
bootstrap=run_bootstrap,
).squeeze(PERCENTILES_COORD, drop=True)
if run_bootstrap:
result = _add_bootstrap_meta(result, per)
Expand Down
5 changes: 3 additions & 2 deletions icclim/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ def index(
index_name: str | None = None, # optional when computing user_indices
var_name: str | list[str] | None = None,
slice_mode: SliceMode = Frequency.YEAR,
time_range: list[datetime] = None, # TODO: use dateparser to accept strings
time_range: list[datetime] = None, # TODO: use dateparser to accept strings
out_file: str | None = None,
threshold: float | list[float] | None = None,
callback: Callable[[int], None] = log.callback,
callback_percentage_start_value: int = 0,
callback_percentage_total: int = 100,
base_period_time_range: list[datetime] | None = None, # TODO: use dateparser to accept strings
base_period_time_range: list[datetime]
| None = None, # TODO: use dateparser to accept strings
window_width: int = 5,
only_leap_years: bool = False,
ignore_Feb29th: bool = False,
Expand Down
12 changes: 6 additions & 6 deletions icclim/models/frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

from datetime import datetime, timedelta
from enum import Enum
from typing import Callable, List, Tuple, Union, Dict
from typing import Callable, Dict, List, Tuple, Union

import cftime

import dateparser
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -75,7 +74,7 @@ def add_time_bounds(da: DataArray) -> tuple[DataArray, DataArray]:
end_month + 1,
1,
calendar=first_time.calendar,
)- timedelta(days=1)
) - timedelta(days=1)
else:
end = cftime.datetime(
year_of_season_end,
Expand All @@ -86,7 +85,9 @@ def add_time_bounds(da: DataArray) -> tuple[DataArray, DataArray]:
else:
start = pd.to_datetime(f"{year}-{start_month}-{start_day}")
if end_day is None:
end = pd.to_datetime(f"{year_of_season_end}-{end_month + 1}") - timedelta(days=1)
end = pd.to_datetime(
f"{year_of_season_end}-{end_month + 1}"
) - timedelta(days=1)
else:
end = pd.to_datetime(f"{year_of_season_end}-{end_month}-{end_day}")
new_time_axis.append(start + (end - start) / 2)
Expand Down Expand Up @@ -249,7 +250,6 @@ class Frequency(Enum):
Do not use as is, use `slice_mode` with "month", "season" keywords instead.
"""


def __init__(self, freq: _Freq):
self._freq = freq

Expand Down Expand Up @@ -346,7 +346,7 @@ def _get_frequency_from_list(slice_mode_list: list) -> Frequency:
return custom_freq


def _build_frequency_filtered_by_month(months: List[int]):
def _build_frequency_filtered_by_month(months: list[int]):
return _Freq(
indexer=dict(month=months),
post_processing=_get_time_bounds_updater("MS"),
Expand Down
2 changes: 1 addition & 1 deletion icclim/models/user_index_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from enum import Enum
from typing import Any, Callable, Literal

from xclim.core.calendar import select_time
from xarray.core.dataarray import DataArray
from xclim.core.calendar import select_time

from icclim.icclim_exceptions import InvalidIcclimArgumentError
from icclim.models.frequency import Frequency
Expand Down
12 changes: 7 additions & 5 deletions icclim/tests/test_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ def test_winter(self):
assert freq.post_processing is not None

def test_lookup_season_between_dates(self):
freq = Frequency.lookup(["season",["07-19","08-14"]])
freq = Frequency.lookup(["season", ["07-19", "08-14"]])
assert freq == Frequency.CUSTOM
assert freq.pandas_freq == "AS-JUL"
assert freq.accepted_values == []
assert freq.post_processing is not None


class Test_seasons_resampler:
def test_simple(self):
# WHEN
Expand Down Expand Up @@ -104,16 +105,17 @@ def test_winter(self):
@pytest.mark.parametrize("use_cf", [True, False])
def test_between_dates(self, use_cf):
# WHEN
test_da = filter_months(stub_tas(use_cftime=use_cf), [11, 12, 1])\
.resample(time="AS-NOV")\
test_da = (
filter_months(stub_tas(use_cftime=use_cf), [11, 12, 1])
.resample(time="AS-NOV")
.mean()
)
da_res, time_bds_res = get_seasonal_time_updater(11, 1, 2, 30)(test_da)
# THEN
np.testing.assert_array_equal(1, da_res) # data must be unchanged
np.testing.assert_array_equal(1, da_res) # data must be unchanged
assert time_bds_res[0].data[0] == pd.to_datetime("2041-11-02")
assert time_bds_res[0].data[1] == pd.to_datetime("2042-01-30")



def filter_months(da, month_list: list[int]):
return da.sel(time=da.time.dt.month.isin(month_list))
10 changes: 7 additions & 3 deletions icclim/tests/test_generated_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_txx__season_slice_mode():
# THEN
# missing values for nov, dec of first period
np.testing.assert_array_equal(res.TXx.isel(time=0), np.NAN)
np.testing.assert_array_equal(res.TXx.isel(time=1), 30.)
np.testing.assert_array_equal(res.TXx.isel(time=1), 30.0)
np.testing.assert_array_equal(
res.time_bounds.isel(time=0),
[np.datetime64("2041-11-01"), np.datetime64("2042-02-28")],
Expand Down Expand Up @@ -137,7 +137,9 @@ def test_txx__months_slice_mode():
(CalcOperation.MAX_NUMBER_OF_CONSECUTIVE_EVENTS, 1, 1),
],
)
def test_custom_index__season_slice_mode(operator, expectation_year_1, expectation_year_2):
def test_custom_index__season_slice_mode(
operator, expectation_year_1, expectation_year_2
):
tas = stub_tas(2.0)
tas.loc[{"time": "2042-01-01"}] = 303.15
tas.loc[{"time": "2042-12-01"}] = 280.15
Expand All @@ -164,7 +166,9 @@ def test_custom_index__season_slice_mode(operator, expectation_year_1, expectati
(CalcOperation.RUN_SUM, 14, 14),
],
)
def test_custom_index_run_algos__season_slice_mode(operator, expectation_year_1, expectation_year_2):
def test_custom_index_run_algos__season_slice_mode(
operator, expectation_year_1, expectation_year_2
):
tas = stub_tas(2.0)
res = icclim.custom_index(
in_files=tas,
Expand Down
2 changes: 1 addition & 1 deletion icclim/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_indices_all_ignore_error(self):
in_files=ds,
out_file=self.OUTPUT_FILE,
ignore_error=True,
slice_mode="DJF"
slice_mode="DJF",
).compute()
for i in EcadIndex:
# No variable in input to compute snow indices
Expand Down
6 changes: 3 additions & 3 deletions icclim/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import numpy as np
import pandas as pd
import xarray
import xarray as xr

from icclim.models.frequency import Frequency
from icclim.models.index_config import CfVariable
from icclim.models.user_index_config import UserIndexConfig

import xarray as xr

VALUE_COUNT = 365 * 5 + 1 # 5 years of data (with 1 leap year)
VALUE_COUNT = 365 * 5 + 1 # 5 years of data (with 1 leap year)
COORDS = dict(
lat=[42],
lon=[42],
Expand All @@ -20,6 +19,7 @@

CF_TIME_RANGE = xr.cftime_range("2042-01-01", periods=VALUE_COUNT, freq="D")


def stub_user_index(cf_vars: List[CfVariable]):
return UserIndexConfig(
index_name="Yolo", calc_operation="noop", freq=Frequency.MONTH, cf_vars=cf_vars
Expand Down

0 comments on commit e48deb8

Please sign in to comment.