Skip to content

Commit

Permalink
Merge branch 'naming_conventions' of https://github.com/Ouranosinc/xsdba
Browse files Browse the repository at this point in the history
 into naming_conventions
  • Loading branch information
coxipi committed Feb 17, 2025
2 parents bd3133c + 043dc4b commit 1574cb9
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 57 deletions.
63 changes: 42 additions & 21 deletions src/xsdba/_adjustment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=no-value-for-parameter
"""# noqa: SS01
"""
# noqa: SS01
Adjustment Algorithms
=====================
Expand Down Expand Up @@ -49,7 +50,8 @@ def dqm_train(
adapt_freq_thresh: str | None = None,
jitter_under_thresh_value: str | None = None,
) -> xr.Dataset:
"""Train step on one group.
"""
Train step on one group.
Parameters
----------
Expand Down Expand Up @@ -111,7 +113,8 @@ def eqm_train(
adapt_freq_thresh: str | None = None,
jitter_under_thresh_value: str | None = None,
) -> xr.Dataset:
"""EQM: Train step on one group.
"""
EQM: Train step on one group.
Parameters
----------
Expand Down Expand Up @@ -154,7 +157,8 @@ def eqm_train(


def _npdft_train(ref, hist, rots, quantiles, method, extrap, n_escore, standardize):
r"""Npdf transform to correct a source `hist` into target `ref`.
r"""
Npdf transform to correct a source `hist` into target `ref`.
Perform a rotation, bias correct `hist` into `ref` with QuantileDeltaMapping, and rotate back.
Do this iteratively over all rotations `rots` and conserve adjustment factors `af_q` in each iteration.
Expand Down Expand Up @@ -212,7 +216,8 @@ def mbcn_train(
extrapolation: str,
n_escore: int,
) -> xr.Dataset:
"""Npdf transform training.
"""
Npdf transform training.
Adjusting factors obtained for each rotation in the npdf transform and conserved to be applied in
the adjusting step in :py:func:`mcbn_adjust`.
Expand Down Expand Up @@ -299,7 +304,8 @@ def mbcn_train(


def _npdft_adjust(sim, af_q, rots, quantiles, method, extrap):
"""Npdf transform adjusting.
"""
Npdf transform adjusting.
Adjusting factors `af_q` obtained in the training step are applied on the simulated data `sim` at each iterated
rotation, see :py:func:`_npdft_train`.
Expand Down Expand Up @@ -351,7 +357,8 @@ def mbcn_adjust(
adj_kws: dict,
period_dim: str | None,
) -> xr.DataArray:
"""Perform the adjustment portion MBCn multivariate bias correction technique.
"""
Perform the adjustment portion MBCn multivariate bias correction technique.
The function :py:func:`mbcn_train` pre-computes the adjustment factors for each rotation
in the npdf portion of the MBCn algorithm. The rest of adjustment is performed here
Expand Down Expand Up @@ -470,7 +477,8 @@ def mbcn_adjust(
def qm_adjust(
ds: xr.Dataset, *, group: Grouper, interp: str, extrapolation: str, kind: str
) -> xr.Dataset:
"""QM (DQM and EQM): Adjust step on one block.
"""
QM (DQM and EQM): Adjust step on one block.
Parameters
----------
Expand Down Expand Up @@ -517,7 +525,8 @@ def dqm_adjust(
extrapolation: str,
detrend: int | PolyDetrend,
) -> xr.Dataset:
"""DQM adjustment on one block.
"""
DQM adjustment on one block.
Parameters
----------
Expand Down Expand Up @@ -576,7 +585,8 @@ def dqm_adjust(

@map_blocks(reduces=[Grouper.PROP, "quantiles"], scen=[], sim_q=[])
def qdm_adjust(ds: xr.Dataset, *, group, interp, extrapolation, kind) -> xr.Dataset:
"""QDM: Adjust process on one block.
"""
QDM: Adjust process on one block.
Parameters
----------
Expand Down Expand Up @@ -605,7 +615,8 @@ def qdm_adjust(ds: xr.Dataset, *, group, interp, extrapolation, kind) -> xr.Data
hist_thresh=[Grouper.PROP],
)
def loci_train(ds: xr.Dataset, *, group, thresh) -> xr.Dataset:
"""LOCI: Train on one block.
"""
LOCI: Train on one block.
Parameters
----------
Expand All @@ -631,7 +642,8 @@ def loci_train(ds: xr.Dataset, *, group, thresh) -> xr.Dataset:

@map_blocks(reduces=[Grouper.PROP], scen=[])
def loci_adjust(ds: xr.Dataset, *, group, thresh, interp) -> xr.Dataset:
"""LOCI: Adjust on one block.
"""
LOCI: Adjust on one block.
Parameters
----------
Expand All @@ -652,7 +664,8 @@ def loci_adjust(ds: xr.Dataset, *, group, thresh, interp) -> xr.Dataset:

@map_groups(af=[Grouper.PROP])
def scaling_train(ds: xr.Dataset, *, dim, kind) -> xr.Dataset:
"""Scaling: Train on one group.
"""
Scaling: Train on one group.
Parameters
----------
Expand All @@ -670,7 +683,8 @@ def scaling_train(ds: xr.Dataset, *, dim, kind) -> xr.Dataset:

@map_blocks(reduces=[Grouper.PROP], scen=[])
def scaling_adjust(ds: xr.Dataset, *, group, interp, kind) -> xr.Dataset:
"""Scaling: Adjust on one block.
"""
Scaling: Adjust on one block.
Parameters
----------
Expand All @@ -686,7 +700,8 @@ def scaling_adjust(ds: xr.Dataset, *, group, interp, kind) -> xr.Dataset:


def npdf_transform(ds: xr.Dataset, **kwargs) -> xr.Dataset:
r"""N-pdf transform : Iterative univariate adjustment in random rotated spaces.
r"""
N-pdf transform : Iterative univariate adjustment in random rotated spaces.
Parameters
----------
Expand Down Expand Up @@ -836,7 +851,8 @@ def extremes_train(
dist,
quantiles: np.ndarray,
) -> xr.Dataset:
"""Train extremes for a given variable series.
"""
Train extremes for a given variable series.
Parameters
----------
Expand Down Expand Up @@ -901,7 +917,8 @@ def extremes_adjust(
extrapolation: str,
cluster_thresh: float,
) -> xr.Dataset:
"""Adjust extremes to reflect many distribution factors.
"""
Adjust extremes to reflect many distribution factors.
Parameters
----------
Expand Down Expand Up @@ -964,7 +981,8 @@ def _otc_adjust(
jitter_inside_bins: bool = True,
normalization: str | None = "max_distance",
):
"""Optimal Transport Correction of the bias of X with respect to Y.
"""
Optimal Transport Correction of the bias of X with respect to Y.
Parameters
----------
Expand Down Expand Up @@ -1063,7 +1081,8 @@ def otc_adjust(
adapt_freq_thresh: dict | None = None,
normalization: str | None = "max_distance",
):
"""Optimal Transport Correction of the bias of `hist` with respect to `ref`.
"""
Optimal Transport Correction of the bias of `hist` with respect to `ref`.
Parameters
----------
Expand Down Expand Up @@ -1162,7 +1181,8 @@ def _dotc_adjust(
kind: dict | None = None,
normalization: str | None = "max_distance",
):
"""Dynamical Optimal Transport Correction of the bias of X with respect to Y.
"""
Dynamical Optimal Transport Correction of the bias of X with respect to Y.
Parameters
----------
Expand Down Expand Up @@ -1292,7 +1312,8 @@ def dotc_adjust(
adapt_freq_thresh: dict | None = None,
normalization: str | None = "max_distance",
):
"""Dynamical Optimal Transport Correction of the bias of X with respect to Y.
"""
Dynamical Optimal Transport Correction of the bias of X with respect to Y.
Parameters
----------
Expand Down
9 changes: 6 additions & 3 deletions src/xsdba/_processing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""# noqa: SS01
"""
# noqa: SS01
Compute Functions Submodule
===========================
Expand Down Expand Up @@ -107,7 +108,8 @@ def _normalize(
dim: Sequence[str],
kind: str = ADDITIVE,
) -> xr.Dataset:
"""Normalize an array by removing its mean.
"""
Normalize an array by removing its mean.
Parameters
----------
Expand Down Expand Up @@ -143,7 +145,8 @@ def _normalize(

@map_groups(reordered=[Grouper.DIM], main_only=False)
def _reordering(ds: xr.Dataset, *, dim: str) -> xr.Dataset:
"""Group-wise reordering.
"""
Group-wise reordering.
Parameters
----------
Expand Down
24 changes: 16 additions & 8 deletions src/xsdba/detrending.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""# noqa: SS01
"""
# noqa: SS01
Detrending Objects Utilities
============================
"""
Expand All @@ -14,7 +15,8 @@


class BaseDetrend(ParametrizableWithDataset):
"""Base class for detrending objects.
"""
Base class for detrending objects.
Defines three methods:
Expand All @@ -34,7 +36,8 @@ class BaseDetrend(ParametrizableWithDataset):

@parse_group
def __init__(self, *, group: Grouper | str = "time", kind: str = "+", **kwargs):
"""Initialize Detrending object.
"""
Initialize Detrending object.
Parameters
----------
Expand All @@ -52,7 +55,8 @@ def fitted(self):
return hasattr(self, "ds")

def fit(self, da: xr.DataArray):
"""Extract the trend of a DataArray along a specific dimension.
"""
Extract the trend of a DataArray along a specific dimension.
Returns a new object that can be used for detrending and retrending.
Fitted objects are unique to the fitted coordinate used.
Expand All @@ -64,7 +68,8 @@ def fit(self, da: xr.DataArray):
return new

def _get_trend(self, da: xr.DataArray):
"""Compute the trend along the self.group.dim as found on da.
"""
Compute the trend along the self.group.dim as found on da.
If da is a DataArray (and has a `dtype` attribute), the trend is cast to have the same dtype.
Expand Down Expand Up @@ -148,7 +153,8 @@ def _meandetrend_get_trend(da, *, dim, kind):


class PolyDetrend(BaseDetrend):
"""Detrend time series using a polynomial regression.
"""
Detrend time series using a polynomial regression.
Attributes
----------
Expand Down Expand Up @@ -193,7 +199,8 @@ def _polydetrend_get_trend(da, *, dim, degree, preserve_mean, kind):


class LoessDetrend(BaseDetrend):
"""Detrend time series using a LOESS regression.
"""
Detrend time series using a LOESS regression.
The fit is a piecewise linear regression. For each point, the contribution of all
neighbors is weighted by a bell-shaped curve (gaussian) with parameters sigma (std).
Expand Down Expand Up @@ -278,7 +285,8 @@ def _loessdetrend_get_trend(


class RollingMeanDetrend(BaseDetrend):
"""Detrend time series using a rolling mean.
"""
Detrend time series using a rolling mean.
Attributes
----------
Expand Down
9 changes: 6 additions & 3 deletions src/xsdba/loess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""# noqa: SS01
"""
# noqa: SS01
LOESS Smoothing Submodule
=========================
"""
Expand Down Expand Up @@ -57,7 +58,8 @@ def _loess_nb(
dx=0,
skipna=True,
): # pragma: no cover
"""1D Locally weighted regression: fits a nonparametric regression curve to a scatter plot.
"""
1D Locally weighted regression: fits a nonparametric regression curve to a scatter plot.
The arrays x and y contain an equal number of elements; each pair (x[i], y[i]) defines
a data point in the scatter plot. The function returns the estimated (smooth) values of y.
Expand Down Expand Up @@ -185,7 +187,8 @@ def loess_smoothing(
equal_spacing: bool | None = None,
skipna: bool = True,
):
r"""Locally weighted regression in 1D: fits a nonparametric regression curve to a scatter plot.
r"""
Locally weighted regression in 1D: fits a nonparametric regression curve to a scatter plot.
Returns a smoothed curve along given dimension. The regression is computed for each point using a subset of
neighbouring points as given from evaluating the weighting function locally.
Expand Down
Loading

0 comments on commit 1574cb9

Please sign in to comment.