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
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 16, 2024
1 parent 9778739 commit a7e5bde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 16 additions & 12 deletions xclim/indices/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,12 @@ def spell_mask(
# Checks
multivariate = False
if var_reducer is not None:
if 'variable' in data.dims and 'variable' in thresh.dims:
if "variable" in data.dims and "variable" in thresh.dims:
multivariate = True
else:
raise ValueError("'var_reducer' was given but the data does not have 'variable' dimension.")
raise ValueError(
"'var_reducer' was given but the data does not have 'variable' dimension."
)
if weights is not None:
if win_reducer != "mean":
raise ValueError(
Expand Down Expand Up @@ -492,7 +494,7 @@ def spell_length_statistics(
freq : str
Resampling frequency.
resample_before_spells : bool
Determines if the resampling should take place before or after finding the
Determines if the resampling should take place before or after finding the
spells. If it takes place before, spells cannot cross the period boundary.
\*\*indexer
Indexing parameters to compute the indicator on a temporal subset of the data.
Expand Down Expand Up @@ -535,7 +537,7 @@ def spell_length_statistics(
"""
thresh = convert_units_to(threshold, data, context="infer")

def _spell(da, frq = None):
def _spell(da, frq=None):
is_in_spell = spell_mask(da, window, win_reducer, op, thresh).astype(np.float32)
is_in_spell = select_time(is_in_spell, **indexer)

Expand All @@ -548,7 +550,7 @@ def _spell(da, frq = None):
)

if resample_before_spells:
out = resample_map(data, 'time', freq, _spell)
out = resample_map(data, "time", freq, _spell)
else:
out = _spell(data, freq)

Expand Down Expand Up @@ -599,7 +601,7 @@ def bivariate_spell_length_statistics(
freq : str
Resampling frequency.
resample_before_spells : bool
Determines if the resampling should take place before or after finding the
Determines if the resampling should take place before or after finding the
spells. If it takes place before, spells cannot cross the period boundary.
\*\*indexer
Indexing parameters to compute the indicator on a temporal subset of the data.
Expand All @@ -614,14 +616,16 @@ def bivariate_spell_length_statistics(
thresh1 = convert_units_to(threshold1, data1, context="infer")
thresh2 = convert_units_to(threshold2, data2, context="infer")

data = xr.concat([data2, data2], 'variable')
data = xr.concat([data2, data2], "variable")
if isinstance(thresh1, xr.DataArray):
thresh = xr.concat([thresh1, thresh2], 'variable')
thresh = xr.concat([thresh1, thresh2], "variable")
else:
thresh = xr.DataArray([thresh1, thresh2], dims=('variable',))
thresh = xr.DataArray([thresh1, thresh2], dims=("variable",))

def _spell(da, frq = None):
is_in_spell = spell_mask(da, window, win_reducer, op, thresh, var_reducer='all').astype(np.float32)
def _spell(da, frq=None):
is_in_spell = spell_mask(
da, window, win_reducer, op, thresh, var_reducer="all"
).astype(np.float32)
is_in_spell = select_time(is_in_spell, **indexer)

return rl.rle_statistics(
Expand All @@ -633,7 +637,7 @@ def _spell(da, frq = None):
)

if resample_before_spells:
out = resample_map(data, 'time', freq, _spell, reduced_dims=['variable'])
out = resample_map(data, "time", freq, _spell, reduced_dims=["variable"])
else:
out = _spell(data, freq)

Expand Down
4 changes: 2 additions & 2 deletions xclim/indices/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def resample_map(
map_blocks: bool | Literal["from_context"] = "from_context",
resample_kwargs: dict | None = None,
map_kwargs: dict | None = None,
reduced_dims: Sequence[str] | None = None
reduced_dims: Sequence[str] | None = None,
) -> xr.DataArray | xr.Dataset:
r"""
Wraps xarray's resample(...).map() with a :py:func:`xarray.map_blocks`, ensuring the chunking is appropriate using flox.
Expand Down Expand Up @@ -608,7 +608,7 @@ def _resample_map(obj_chnk, dm, frq, rs_kws, fun, mp_kws):

# Template. We are hoping that this takes a negligeable time as it is never loaded.
template = obj_rechunked.resample(**{dim: freq}, **resample_kwargs).first()
if reduced_dims: # Removed reduced dims
if reduced_dims: # Removed reduced dims
template = template.isel({d: 0 for d in reduced_dims}, drop=True)

# New chunks along time : infer the number of elements resulting from the resampling of each chunk
Expand Down

0 comments on commit a7e5bde

Please sign in to comment.