7. Regional frequency analyses¶
[1]:
import matplotlib.pyplot as plt
import xdatasets
from lmoments3.distr import KappaGen
from sklearn.cluster import HDBSCAN, OPTICS, AgglomerativeClustering
import xhydro as xh
import xhydro.frequency_analysis as xhfa
import xhydro.gis as xhgis
ERROR 1: PROJ: proj_create_from_database: Open of /home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/share/proj failed
Redefining 'percent' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining '%' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'year' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'yr' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'C' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'd' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'h' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'degrees_north' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'degrees_east' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining 'degrees' (<class 'pint.delegates.txt_defparser.plain.UnitDefinition'>)
Redefining '[speed]' (<class 'pint.delegates.txt_defparser.plain.DerivedDimensionDefinition'>)
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/__init__.py:21: UserWarning: The `exactextract` library is not present in the environment and will not be used.
This notebook will demonstrate how to use the xHydro library to perform regional frequency analyses on a dataset of streamflow data. Since the initial steps for regional frequency analysis are the same as those for local frequency analysis, users are encouraged to refer to the Local frequency analysis notebook for an overview.
For this example, we will use the same dataset of hydrometric gauges covering parts of southern Quebec, ensuring continuity with the previous analysis while expanding to a regional scale. However, as regional analyses rely on having access to multiple sources of data, we will extract streamflow for 15 stations.
[2]:
ds = (
xdatasets.Query(
**{
"datasets": {
"deh": {
"id": ["02*"],
"regulated": ["Natural"],
"variables": ["streamflow"],
}
},
"time": {"start": "1970-01-01", "minimum_duration": (30 * 365, "d")},
}
)
.data.squeeze()
.load()
)
# This dataset lacks some attributes, so let's add them.
ds = ds.rename({"streamflow": "q"})
ds["id"].attrs["cf_role"] = "timeseries_id"
ds["q"].attrs = {
"long_name": "Streamflow",
"units": "m3 s-1",
"standard_name": "water_volume_transport_in_river_channel",
"cell_methods": "time: mean",
}
# Clean some of the coordinates that are not needed for this example
ds = ds.drop_vars([c for c in ds.coords if c not in ["id", "time", "name"]])
timeargs = {
"annual": {},
}
ds_4fa = xh.indicators.get_yearly_op(
ds, op="max", timeargs=timeargs, missing="pct", missing_options={"tolerance": 0.15}
)
ds_4fa
[2]:
<xarray.Dataset> Size: 4kB
Dimensions: (id: 15, time: 56)
Coordinates:
* id (id) object 120B '020404' '020602' ... '024007' '024013'
name (id) object 120B 'York' 'Dartmouth' ... 'Bécancour'
* time (time) datetime64[ns] 448B 1970-01-01 ... 2025-01-01
Data variables:
q_max_annual (id, time) float32 3kB nan nan nan nan nan ... nan nan nan nan
Attributes:
cat:xrfreq: YS-JAN
cat:frequency: yr
cat:processing_level: indicators
cat:variable: ('q_max_annual',)
cat:id: 7.1. Explanatory variables¶
In regional frequency analyses, explanatory variables are used to help explain the spatial variation in hydrological extremes across different locations. These variables can include factors such as catchment area, elevation, precipitation, land use, and soil type, among others. By incorporating explanatory variables, we can account for the influence of geographic and environmental characteristics on extreme events, allowing for more accurate regional predictions.
7.1.1. a) Extraction of watershed characteristics using xhydro.gis¶
In this example, we’ll use catchment properties as our primary explanatory variables. However, other variables, such as climatological averages or land use data, can also be incorporated depending on the analysis requirements. For detailed steps on how to extract and work with these data, refer to the GIS operations notebook.
[3]:
gdf = xdatasets.Query(
**{
"datasets": {
"deh_polygons": {
"id": ["02*"],
"regulated": ["Natural"],
"variables": ["streamflow"],
}
},
"time": {"start": "1970-01-01", "minimum_duration": (30 * 365, "d")},
}
).data.reset_index()
dswp = xhgis.watershed_properties(
gdf[["Station", "geometry"]], unique_id="Station", output_format="xarray"
)
dswp
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/gis.py:201: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/gis.py:202: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.
[3]:
<xarray.Dataset> Size: 720B
Dimensions: (Station: 15)
Coordinates:
* Station (Station) object 120B '020404' '020602' ... '024007' '024013'
Data variables:
area (Station) float64 120B 6.712e+08 6.341e+08 ... 2.319e+08
perimeter (Station) float64 120B 2.509e+05 2.079e+05 ... 1.151e+05
gravelius (Station) float64 120B 2.732 2.329 2.51 ... 2.429 2.906 2.132
centroid_lon (Station) float64 120B -65.24 -64.91 -66.19 ... -71.6 -71.33
centroid_lat (Station) float64 120B 48.9 49.04 48.96 ... 46.1 46.19 46.087.1.2. b) Principal component analysis¶
After acquiring the explanatory variables, the next step is to process them using Principal Component Analysis (PCA) to reduce the dimensionality of the dataset. PCA helps simplify the dataset by transforming the original variables into a smaller set of uncorrelated components while retaining most of the variation in the data. This is accomplished using the function xhydro.frequency_analysis.regional.fit_pca.
[4]:
help(xhfa.regional.fit_pca)
Help on function fit_pca in module xhydro.frequency_analysis.regional:
fit_pca(ds: xr.Dataset, **kwargs) -> tuple
Perform Principal Component Analysis (PCA) on the input dataset.
This function scales the input data, applies PCA transformation, and returns
the transformed data along with the PCA object.
Parameters
----------
ds : xr.Dataset
Input dataset to perform PCA on.
\*\*kwargs : dict
Additional keyword arguments to pass to the PCA constructor.
Returns
-------
tuple: A tuple containing:
- data_pca (xr.DataArray): PCA-transformed data with 'Station' and 'components' as coordinates.
- obj_pca (sklearn.decomposition.PCA): Fitted PCA object.
Notes
-----
- The input data is scaled before PCA is applied.
- The number of components in the output depends on the n_components parameter passed to PCA.
[5]:
data_pca, pca = xhfa.regional.fit_pca(dswp, n_components=3)
data_pca
[5]:
<xarray.DataArray (Station: 15, components: 3)> Size: 360B
array([[-2.48506812e+03, 8.68222390e-01, 6.18492284e-01],
[-2.28706847e+03, -3.82682765e-01, -7.46540544e-01],
[-1.48206850e+03, 3.89329737e-01, -2.41928874e-01],
[-3.82066870e+02, -4.09517146e-01, 2.10528949e-01],
[-1.85066639e+02, 2.97231987e-01, 3.20226530e-03],
[ 1.12933711e+02, -2.73945787e+00, -2.80370822e-01],
[ 4.13933839e+02, 1.43184979e+00, 4.50497174e-02],
[ 5.11933876e+02, 9.70448285e-01, 1.23118865e+00],
[ 5.32933768e+02, -5.49290384e-01, -5.67606131e-01],
[ 5.38934065e+02, -3.95607723e-01, -8.15645783e-01],
[ 5.42933642e+02, -1.23796579e+00, 1.14791838e+00],
[ 8.11933845e+02, -6.70129061e-01, 8.80067651e-01],
[ 1.11393391e+03, 2.05320024e-01, -4.18140383e-01],
[ 1.11793429e+03, 4.39850876e+00, -5.36706342e-01],
[ 1.12393366e+03, -2.17626024e+00, -5.29509020e-01]])
Coordinates:
* Station (Station) object 120B '020404' '020602' ... '024007' '024013'
* components (components) int64 24B 0 1 2
Attributes:
long_name: Fitted Scaled Data
description: Fitted scaled data with StandardScaler and PCA from sk...
fitted_variables: ['area', 'perimeter', 'gravelius', 'centroid_lon', 'ce...The results show that the correlation between the components is close to 0, indicating that the first three components are sufficiently independent. This suggests that these components can be effectively used for the remainder of our analysis, as they capture most of the variation in the explanatory variables without significant overlap or multicollinearity.
[6]:
data_pca.to_dataframe(name="value").reset_index().pivot(
index="Station", columns="components"
).corr()
[6]:
| value | ||||
|---|---|---|---|---|
| components | 0 | 1 | 2 | |
| components | ||||
| value | 0 | 1.000000e+00 | 9.942103e-14 | 1.350617e-14 |
| 1 | 9.942103e-14 | 1.000000e+00 | -4.653426e-16 | |
| 2 | 1.350617e-14 | -4.653426e-16 | 1.000000e+00 | |
7.1.3. c) Clustering¶
The results from the PCA can be used to group the stations into clusters based on their similarities in the principal components. Clustering helps identify regions with similar characteristics, enabling more targeted and accurate regional frequency analyses. This step is accomplished using xhydro.frequency_analysis.regional.get_clusters, which supports clustering methods from sklearn.cluster. In this example, we will use AgglomerativeClustering to form 3 clusters based on the PCA
results.
[7]:
Help on function get_clusters in module xhydro.frequency_analysis.regional:
get_clusters(model: Callable, param: dict, sample: xr.Dataset | xr.DataArray) -> list
Get indices of groups from a fit using the specified model and parameters.
Parameters
----------
model : callable
Model class or instance with a fit method.
param : dict
Parameters for the model.
sample : xr.Dataset or xr.DataArray
Data sample to fit the model.
Returns
-------
list :
List of indices for each non-excluded group.
[8]:
groups = xhfa.regional.get_clusters(
AgglomerativeClustering, {"n_clusters": 3}, data_pca
)
groups
[8]:
[array(['023303', '023401', '023422', '023428', '023432', '023701',
'024003', '024007', '024013'], dtype=object),
array(['020404', '020602', '021407'], dtype=object),
array(['022507', '022704', '023002'], dtype=object)]
[9]:
ax = plt.subplot(1, 1, 1)
gdf[gdf["Station"].isin(groups[0])].plot(ax=ax, color="red")
gdf[gdf["Station"].isin(groups[1])].plot(ax=ax, color="green")
gdf[gdf["Station"].isin(groups[2])].plot(ax=ax, color="blue")
[9]:
<Axes: >
7.2. Regional Frequency Analysis¶
Hosking and Wallis introduced a method for regional frequency analysis based on L-moments, which are particularly useful for analyzing extreme events across different regions. Here’s a brief overview of the key concepts:
L-Moments: L-moments are statistical measures that are derived from linear combinations of order statistics. They offer a more robust alternative to traditional moments (like mean and variance), especially in the presence of outliers or when dealing with small sample sizes. L-moments are widely used in hydrological studies for estimating the characteristics of extreme values, as they provide more stable and reliable estimates.
Regional Frequency Analysis: This technique involves pooling data from multiple sites or regions to estimate the frequency distribution of extreme events, such as floods or droughts. The approach allows for the determination of common statistical parameters across different locations, enabling a more generalized and regional perspective on extreme event behavior. Hosking and Wallis’s method focuses on fitting a regional frequency distribution and assessing how well it captures the characteristics of the data.
Regional L-Moments: These are L-moments calculated from pooled data across multiple sites within a region. By applying regional L-moments, we can estimate the parameters of regional frequency distributions and better understand the occurrence of extreme events in different parts of the region. This method improves the accuracy and robustness of predictions for extreme events, particularly when data from multiple sites are available.
Let’s start by calculating the L-moments for each station using xhydro.frequency_analysis.regional.calc_moments.
[10]:
Help on function calc_moments in module xhydro.frequency_analysis.regional:
calc_moments(ds: xr.Dataset) -> xr.Dataset
Calculate L-moments for multiple stations.
Parameters
----------
ds : xr.Dataset
A vector of stations, where each element is an array-like object containing the data for which to calculate L-moments.
Returns
-------
xr.Dataset
L-moment dataset with a new lmom dimension.
Notes
-----
NaN values in each station are removed before calculating L-moments.
The function uses the `moment_l` function to calculate L-moments for each individual stations.
Equations are based on Hosking, J. R. M., & Wallis, J. R. (1997). Regional frequency analysis (p. 240).
[11]:
ds_moments = xhfa.regional.calc_moments(ds_4fa)
ds_moments
[11]:
<xarray.Dataset> Size: 1kB
Dimensions: (id: 15, lmom: 6)
Coordinates:
* id (id) object 120B '020404' '020602' ... '024007' '024013'
name (id) object 120B 'York' 'Dartmouth' ... 'Bécancour'
* lmom (lmom) <U4 96B 'l1' 'l2' 'l3' 'tau' 'tau3' 'tau4'
Data variables:
q_max_annual (id, lmom) float64 720B 145.1 31.03 5.494 ... 0.0697 0.1307
Attributes:
cat:xrfreq: YS-JAN
cat:frequency: yr
cat:processing_level: indicators
cat:variable: ('q_max_annual',)
cat:id: To perform regional frequency analysis, we need to reshape our datasets of annual maximums and L-moments based on the groupings identified by the clustering algorithm. Since there is no standardized naming convention for this new dimension, xHydro uses the name region_id for the cluster groupings. This reshaping process is handled by the function xhydro.frequency_analysis.regional.group_ds_by_regions, which organizes the data according to the cluster assignments and prepares it for
further analysis.
[12]:
Help on function group_ds_by_regions in module xhydro.frequency_analysis.regional:
group_ds_by_regions(ds: xr.Dataset, *, regions: list) -> xr.Dataset
Group a dataset by a list of regions.
Parameters
----------
ds : xr.Dataset
The input dataset to be grouped.
The associated dimension must have a 'cf_role: timeseries_id' attribute.
regions : list
A list of regions to be used for grouping the dataset.
Returns
-------
xr.Dataset
A new dataset with the grouped data.
[13]:
ds_groups = xhfa.regional.group_ds_by_regions(ds_4fa, regions=groups)
ds_moments_groups = xhfa.regional.group_ds_by_regions(ds_moments, regions=groups)
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for join will change from join='outer' to join='exact'. This change will result in the following ValueError: cannot be aligned with join='exact' because index/labels/sizes are not equal along these coordinates (dimensions): 'id' ('id',) The recommendation is to set join explicitly for this case.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for coords will change from coords='different' to coords='minimal'. This is likely to lead to different results when multiple datasets have matching variables with overlapping values. To opt in to new defaults and get rid of these warnings now use `set_options(use_new_combine_kwarg_defaults=True) or set coords explicitly.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for join will change from join='outer' to join='exact'. This change will result in the following ValueError: cannot be aligned with join='exact' because index/labels/sizes are not equal along these coordinates (dimensions): 'id' ('id',) The recommendation is to set join explicitly for this case.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for coords will change from coords='different' to coords='minimal'. This is likely to lead to different results when multiple datasets have matching variables with overlapping values. To opt in to new defaults and get rid of these warnings now use `set_options(use_new_combine_kwarg_defaults=True) or set coords explicitly.
We now need to compute two important scores for evaluating the regional frequency analysis: the H-Score and the Z-Score. The H-Score measures the homogeneity of data across different sites or regions relative to the regional model. It helps determine how well the data from multiple sites align with the regional distribution. The Z-Score quantifies the discrepancy between the observed and expected values, standardized by their variability. It helps determine how well the theoretical distribution (based on the regional model) fits the observed data.
H-Score (Homogeneity Score)
- H < 1: HomogeneousThis indicates that the data from different sites are quite similar and align well with the regional model, suggesting that the regional model is suitable for the entire region.
- 1 ≤ H < 2: Maybe HomogeneousThis range suggests some heterogeneity, meaning the data might still generally fit the regional model, but there are some variations that the model does not fully capture.
- H ≥ 2: HeterogeneousA score this high indicates significant differences between sites or regions, suggesting that the regional model might not be appropriate for all the data. The region may be too diverse or require model adjustments.
Z-Score (Goodness of Fit)
Low Z-Score: A low Z-Score suggests a good fit between the model and the observed data. Typically, an absolute Z-Score below 1.64 indicates that the model is appropriate and the fit is statistically acceptable.
High Z-Score: A high Z-Score suggests significant discrepancies between observed and expected values. An absolute Z-Score above 1.64 implies that the model might not fit the data well, and further adjustments or a different model might be needed.
The function xhydro.frequency_analysis.regional.calc_h_z is used to compute the H-Score and Z-Score for the regional frequency analysis.
WARNING
The kap argument in the xhydro.frequency_analysis.regional.calc_h_z function expects a Kappa3 distribution object generated using lmoments3.distr.KappaGen(). However, due to licensing issues, the lmoments3 library cannot currently be included in the xHydro requirements and must be installed independently.
[14]:
help(xhfa.regional.calc_h_z)
Help on function calc_h_z in module xhydro.frequency_analysis.regional:
calc_h_z(
ds_regions: xr.Dataset,
ds_moments_regions: xr.Dataset,
*,
kap: object,
seed: int | None = None
) -> xr.Dataset
Calculate heterogeneity measure H and Z-score for regional frequency analysis.
Parameters
----------
ds_regions : xr.Dataset
Dataset containing regions.
ds_moments_regions : xr.Dataset
Dataset containing L-moments for ds_regions.
kap : scipy.stats.kappa3
Kappa3 distribution object.
seed : int, optional
Random seed for reproducibility. Default is None.
Returns
-------
xr.Dataset
Dataset containing calculated H values and Z-scores for each region.
Notes
-----
This function applies the heterogeneity measure and Z-score calculations
for the regional frequency analysis. It uses L-moments and
the Kappa3 distribution in the process.
This function does not support lazy evaluation.
Equations are based on Hosking, J. R. M., & Wallis, J. R. (1997). Regional frequency analysis (p. 240).
[15]:
ds_hz = xhfa.regional.calc_h_z(ds_groups, ds_moments_groups, kap=KappaGen())
ds_hz
[15]:
<xarray.Dataset> Size: 96B
Dimensions: (region_id: 3, crit: 2)
Coordinates:
* region_id (region_id) int64 24B 0 1 2
* crit (crit) <U1 8B 'H' 'Z'
lmom <U4 16B 'l1'
Data variables:
q_max_annual (crit, region_id) float64 48B 0.7056 0.6233 ... 0.1515 0.3679We can filter the results to include only the data that has H and Z scores below given thresholds using the function xhydro.frequency_analysis.regional.mask_h_z. By default, the thresholds are set to 1 for H and 1.64 for Z. However, these thresholds can be customized to suit the specific requirements of the analysis.
[16]:
help(xhfa.regional.mask_h_z)
Help on function mask_h_z in module xhydro.frequency_analysis.regional:
mask_h_z(
ds: xr.Dataset,
*,
thresh_h: float | None = 1,
thresh_z: float | None = 1.64
) -> xr.DataArray
Create a boolean mask based on heterogeneity measure H and Z-score thresholds.
Parameters
----------
ds : xr.Dataset
Dataset containing H and Z values for each region.
thresh_h : float, optional
Threshold for the heterogeneity measure H. Default is 1.
thresh_z : float, optional
Threshold for the absolute Z-score. Default is 1.64.
Returns
-------
xr.DataArray
Boolean mask where True indicates regions that meet both threshold criteria.
[17]:
mask = xhfa.regional.mask_h_z(ds_hz)
ds_groups_h1 = ds_groups.where(mask).load()
ds_moments_groups_h1 = ds_moments_groups.where(mask).load()
The final step of the regional frequency analysis is similar to the local frequency analysis, but instead of using xhydro.frequency_analysis.local.parametric_quantiles, we use xhydro.frequency_analysis.regional.calculate_return_period to calculate the return periods. Additionally, to avoid performing the analysis on very small regions, the remove_small_regions argument can be used to exclude any regions with less than a specified number of stations. By default, this threshold is set
to 5.
[18]:
Help on function calculate_return_period in module xhydro.frequency_analysis.regional:
calculate_return_period(
ds_regions: xr.Dataset,
ds_moments_regions: xr.Dataset,
*,
return_period: np.array,
l1: xr.DataArray | None = None
) -> xr.DataArray
Calculate return periods from the regional frequency analysis.
Parameters
----------
ds_regions : xr.Dataset
Dataset containing region flow data.
ds_moments_regions : xr.Dataset
Dataset containing L-moments for region data.
return_period : array-like
Return periods to calculate.
l1 : xr.DataArray, optional
First L-moment (location) values. L-moment can be specified for ungauged catchments.
If None, values are taken from ds_moments_regions.
Returns
-------
xr.DataArray
Calculated return periods for each region and specified return period.
Notes
-----
This function calculates return periods using the Annual Flow Regime method.
If l1 is not provided, it uses the first L-moment from ds_moments_regions.
The function internally calls calculate_ic_from_AFR to compute the flood index.
Equations are based on Hosking, J. R. M., & Wallis, J. R. (1997). Regional frequency analysis (p. 240).
[19]:
Help on function remove_small_regions in module xhydro.frequency_analysis.regional:
remove_small_regions(ds: xr.Dataset, *, thresh: int = 5) -> xr.Dataset
Remove regions from the dataset that have fewer than the threshold number of stations.
Parameters
----------
ds : xr.Dataset
The input dataset containing regions and stations.
thresh : int, optional
The minimum number of stations required for a region to be kept. Default is 5.
Returns
-------
xr.Dataset
The dataset with small regions removed.
[20]:
q_t = xhfa.regional.calculate_return_period(
ds_groups_h1, ds_moments_groups_h1, return_period=[2, 20, 100]
)
q_t
[20]:
<xarray.Dataset> Size: 2kB
Dimensions: (region_id: 3, return_period: 3, id: 15)
Coordinates:
* region_id (region_id) int64 24B 0 1 2
* return_period (return_period) int64 24B 2 20 100
* id (id) object 120B '020404' '020602' ... '024007' '024013'
name (region_id, id) object 360B nan nan nan nan ... nan nan nan
lmom <U4 16B 'l1'
Data variables:
q_max_annual (region_id, return_period, id) float64 1kB nan nan ... nan
Attributes:
cat:xrfreq: YS-JAN
cat:frequency: yr
cat:processing_level: indicators
cat:variable: ('q_max_annual',)
cat:id: [21]:
q_t = xhfa.regional.remove_small_regions(q_t)
q_t
[21]:
<xarray.Dataset> Size: 648B
Dimensions: (region_id: 1, return_period: 3, id: 15)
Coordinates:
* region_id (region_id) int64 8B 0
* return_period (return_period) int64 24B 2 20 100
* id (id) object 120B '020404' '020602' ... '024007' '024013'
name (region_id, id) object 120B nan nan ... 'Bécancour'
lmom <U4 16B 'l1'
Data variables:
q_max_annual (region_id, return_period, id) float64 360B nan nan ... 131.7
Attributes:
cat:xrfreq: YS-JAN
cat:frequency: yr
cat:processing_level: indicators
cat:variable: ('q_max_annual',)
cat:id: Let’s plot results for one station, comparing local and regional analyses.
[22]:
q_reg = q_t.sel(id="023401").dropna(dim="region_id", how="all")
reg = q_reg.q_max_annual.squeeze()
[23]:
params_loc = xhfa.local.fit(ds_4fa.sel(id="023401"))
q_loc = xhfa.local.parametric_quantiles(params_loc, return_period=[2, 20, 100])
loc = q_loc.sel(scipy_dist="genextreme").q_max_annual
[24]:
fig = plt.figure(figsize=(15, 4))
plt.plot(reg.return_period.values, reg.values, "red", label="Regional analysis")
plt.plot(loc.return_period.values, loc.values, "black", label="Local analysis")
plt.xscale("log")
plt.grid(visible=True)
plt.xlabel("Return period (years)")
plt.ylabel("Streamflow (m$^3$/s)")
plt.legend()
[24]:
<matplotlib.legend.Legend at 0x7098ec256900>
7.3. Uncertainties¶
Uncertainties are an important aspect of frequency analysis and should be considered when interpreting results. These uncertainties often stem from data quality, the choice of distribution, and the estimation of parameters. While visualizations can provide insights into the model fit, it’s crucial to quantify and account for uncertainties, such as confidence intervals for parameter estimates, to ensure robust conclusions.
7.3.1. a) Bootstrapping the observations¶
One method for quantifying uncertainties is to bootstrap the observations. In this example, we will perform bootstrapping a small number of times to illustrate the process, though in practice, a higher number of iterations (e.g., 5000) is recommended to obtain more reliable estimates. Bootstrapping resamples the observed data with replacement to generate multiple datasets, which can then be used to assess the variability in the model’s parameters and results.
This can be accomplished by calling xhydro.frequency_analysis.uncertainties.bootstrap_obs. One key difference with local analyses is that for regional analyses, no fitting step is involved, making this function significantly faster. Instead, the results are obtained by first using xhydro.frequency_analysis.uncertainties.calc_moments_iter to compute L-moments for all bootstrap samples. Then, xhydro.frequency_analysis.uncertainties.calculate_quantiles_over_boostraped_groups is used to
calculate the return periods based on those L-moments.
[25]:
help(xhfa.uncertainties.calculate_quantiles_over_boostraped_groups)
Help on function calculate_quantiles_over_boostraped_groups in module xhydro.frequency_analysis.uncertainties:
calculate_quantiles_over_boostraped_groups(
bv: str,
groups: xr.DataArray | xr.Dataset,
moments_iter: xr.DataArray | xr.Dataset,
return_period: np.array,
small_regions_threshold: int | None = 5,
l1: xr.DataArray | None = None
) -> xr.DataArray
Calculate quantiles for each bootstrap sample and group.
Parameters
----------
bv : str
The basin identifier or 'all' to proceed on all basins (needed for ungauged).
The associated dimension must have a 'cf_role: timeseries_id' attribute.
groups : xr.DataArray or xr.Dataset
The grouped data.
moments_iter : xr.DataArray or xr.Dataset
The L-moments for each bootstrap sample.
return_period : array-like
The return periods to calculate quantiles for.
small_regions_threshold : int, optional
The threshold for removing small regions. Default is 5.
l1 : xr.DataArray, optional
First L-moment (location) values. L-moment can be specified for ungauged catchments.
If `None`, values are taken from ds_moments_iter.
Returns
-------
xr.DataArray or xr.Dataset
Quantiles for each bootstrap sample and group. Returns a Dataset if input groups
and moments_iter are Datasets, otherwise returns a DataArray.
[26]:
help(xhfa.uncertainties.calculate_quantiles_over_boostraped_groups)
Help on function calculate_quantiles_over_boostraped_groups in module xhydro.frequency_analysis.uncertainties:
calculate_quantiles_over_boostraped_groups(
bv: str,
groups: xr.DataArray | xr.Dataset,
moments_iter: xr.DataArray | xr.Dataset,
return_period: np.array,
small_regions_threshold: int | None = 5,
l1: xr.DataArray | None = None
) -> xr.DataArray
Calculate quantiles for each bootstrap sample and group.
Parameters
----------
bv : str
The basin identifier or 'all' to proceed on all basins (needed for ungauged).
The associated dimension must have a 'cf_role: timeseries_id' attribute.
groups : xr.DataArray or xr.Dataset
The grouped data.
moments_iter : xr.DataArray or xr.Dataset
The L-moments for each bootstrap sample.
return_period : array-like
The return periods to calculate quantiles for.
small_regions_threshold : int, optional
The threshold for removing small regions. Default is 5.
l1 : xr.DataArray, optional
First L-moment (location) values. L-moment can be specified for ungauged catchments.
If `None`, values are taken from ds_moments_iter.
Returns
-------
xr.DataArray or xr.Dataset
Quantiles for each bootstrap sample and group. Returns a Dataset if input groups
and moments_iter are Datasets, otherwise returns a DataArray.
[27]:
ds_reg_boot = xhfa.uncertainties.bootstrap_obs(ds_4fa, n_samples=35)
ds_moments_iter = xhfa.uncertainties.calc_moments_iter(ds_reg_boot).load()
ds_moments_iter
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:222: RuntimeWarning: invalid value encountered in scalar divide
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:223: RuntimeWarning: divide by zero encountered in scalar divide
[27]:
<xarray.Dataset> Size: 26kB
Dimensions: (samples: 35, id: 15, lmom: 6)
Coordinates:
* samples (samples) int64 280B 0 1 2 3 4 5 6 7 ... 28 29 30 31 32 33 34
* id (id) object 120B '020404' '020602' ... '024007' '024013'
name (id) object 120B 'York' 'Dartmouth' ... 'Bécancour'
* lmom (lmom) <U4 96B 'l1' 'l2' 'l3' 'tau' 'tau3' 'tau4'
Data variables:
q_max_annual (samples, id, lmom) float64 25kB 130.4 22.71 ... 0.07208 0.101
Attributes:
cat:xrfreq: YS-JAN
cat:frequency: yr
cat:processing_level: indicators
cat:variable: ('q_max_annual',)
cat:id: [28]:
q_reg_boot = xhfa.uncertainties.calculate_quantiles_over_boostraped_groups(
"023401",
ds_groups_h1,
ds_moments_iter,
return_period=[2, 20, 100],
)
q_reg_boot
[28]:
<xarray.Dataset> Size: 2kB
Dimensions: (return_period: 3, id: 1, samples: 35)
Coordinates:
* return_period (return_period) int64 24B 2 20 100
* id (id) <U6 24B '023401'
* samples (samples) object 280B MultiIndex
* region_id (samples) int64 280B 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0
* obs_samples (samples) int64 280B 0 1 2 3 4 5 6 7 ... 28 29 30 31 32 33 34
lmom <U4 16B 'l1'
name object 8B 'Beaurivage'
variable <U12 48B 'q_max_annual'
Data variables:
q_max_annual (id, return_period, samples) float64 840B 187.9 ... 355.07.3.2. b) Using multiple regions¶
Another approach to estimating uncertainty is by considering multiple regions for a single catchment of interest. This can be done by applying different clustering methods or by performing a jackknife procedure on the station list. While this process can be computationally intensive, we present a simplified example here to illustrate the potential of this method. The main objective is to demonstrate how considering multiple regions can help in evaluating uncertainty in the regional frequency analysis.
First, we will use xhydro.frequency_analysis.uncertainties.generate_combinations on the results from our PCA to generate lists of stations. This function creates combinations that exclude a certain number of stations each time, allowing us to evaluate how the exclusion of different stations impacts the groupings.
[29]:
Help on function generate_combinations in module xhydro.frequency_analysis.uncertainties:
generate_combinations(da: xr.DataArray, *, n: int) -> list
Generate combinations of indices omitting up to N indices.
Parameters
----------
da : xarray.DataArray
Input DataArray.
n : int
Number of indices to omit in each combination.
Returns
-------
list
List of all combinations.
[30]:
combinations_list = xhfa.uncertainties.generate_combinations(data_pca, n=2)
print(
f"This generated {len(combinations_list)} lists of stations, varying from {len(combinations_list[0])} to {len(combinations_list[-1])} stations."
)
This generated 121 lists of stations, varying from 15 to 13 stations.
Next, we will combine those lists with three clustering methods and, for each method, we’ll try to change some of the parameters.
[31]:
clust_options = {
AgglomerativeClustering: {"arg_name": "n_clusters", "range": range(2, 12)},
HDBSCAN: {"arg_name": "min_cluster_size", "range": range(6, 7)},
OPTICS: {"arg_name": "min_samples", "range": range(4, 5)},
}
[32]:
groups = []
for model in [AgglomerativeClustering, HDBSCAN, OPTICS]:
for p in clust_options[model]["range"]:
d_param = {}
d_param[clust_options[model]["arg_name"]] = p
for combination in combinations_list:
# Extract data for the current combination
data_com = data_pca.sel(Station=list(combination))
# Get groups from the fit and add to the list
groups = groups + xhfa.regional.get_clusters(model, d_param, data_com)
unique_groups = [list(x) for x in {tuple(x) for x in groups}]
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/sklearn/cluster/_hdbscan/hdbscan.py:722: FutureWarning: The default value of `copy` will change from False to True in 1.10. Explicitly set a value for `copy` to silence this warning.
The next steps follow a similar approach to the previous ones, but now with multiple regions.
[33]:
ds_groups_multiregion = xhfa.regional.group_ds_by_regions(ds_4fa, regions=unique_groups)
ds_moments_groups_multiregion = xhfa.regional.group_ds_by_regions(
ds_moments, regions=unique_groups
)
ds_h_z_multiregion = xhfa.regional.calc_h_z(
ds_groups_multiregion, ds_moments_groups_multiregion, kap=KappaGen()
)
mask_multiregion = xhfa.regional.mask_h_z(ds_h_z_multiregion)
ds_groups_h1_multiregion = ds_groups_multiregion.where(mask_multiregion).load()
ds_moments_groups_h1_multiregion = ds_moments_groups_multiregion.where(
mask_multiregion
).load()
q_t_multiregion = xhfa.regional.calculate_return_period(
ds_groups_h1_multiregion,
ds_moments_groups_h1_multiregion,
return_period=[2, 20, 100],
)
q_t_multiregion = xhfa.regional.remove_small_regions(q_t_multiregion)
q = q_t_multiregion.sel(id="023401").dropna(dim="region_id", how="all")
q
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for join will change from join='outer' to join='exact'. This change will result in the following ValueError: cannot be aligned with join='exact' because index/labels/sizes are not equal along these coordinates (dimensions): 'id' ('id',) The recommendation is to set join explicitly for this case.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for coords will change from coords='different' to coords='minimal'. This is likely to lead to different results when multiple datasets have matching variables with overlapping values. To opt in to new defaults and get rid of these warnings now use `set_options(use_new_combine_kwarg_defaults=True) or set coords explicitly.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for join will change from join='outer' to join='exact'. This change will result in the following ValueError: cannot be aligned with join='exact' because index/labels/sizes are not equal along these coordinates (dimensions): 'id' ('id',) The recommendation is to set join explicitly for this case.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/regional.py:672: FutureWarning: In a future version of xarray the default value for coords will change from coords='different' to coords='minimal'. This is likely to lead to different results when multiple datasets have matching variables with overlapping values. To opt in to new defaults and get rid of these warnings now use `set_options(use_new_combine_kwarg_defaults=True) or set coords explicitly.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/lmoments3/distr.py:579: RuntimeWarning: invalid value encountered in power
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py:2630: UserWarning: Kappa distribution fit blablabla (quelle serait la cause d'un ValueError?), returning all NaNs. Error: L-Moments invalid.
[33]:
<xarray.Dataset> Size: 7kB
Dimensions: (region_id: 170, return_period: 3)
Coordinates:
* region_id (region_id) int64 1kB 0 1 3 6 7 9 ... 309 313 317 318 321 323
name (region_id) object 1kB 'Beaurivage' ... 'Beaurivage'
* return_period (return_period) int64 24B 2 20 100
lmom <U4 16B 'l1'
id <U6 24B '023401'
Data variables:
q_max_annual (region_id, return_period) float64 4kB 189.1 319.2 ... 396.6
Attributes:
cat:xrfreq: YS-JAN
cat:frequency: yr
cat:processing_level: indicators
cat:variable: ('q_max_annual',)
cat:id: 7.3.3. c) Combining the bootstrap with multiple regions¶
It is possible to combine both methods—bootstrapping and multiple regions—by integrating the following:
Bootstrapped L-moments from
xhydro.frequency_analysis.uncertainties.calc_moments_iter.Masked results from
xhydro.frequency_analysis.regional.group_ds_by_regionsthat have been filtered based on the H and Z-Scores.
When combining these methods, xhydro.frequency_analysis.uncertainties.calculate_quantiles_over_boostraped_groups will check in how many distinct region_id values the station is present and will stack the data accordingly with the bootstrap samples. For instance, if we have 35 bootstraps and roughly 220 regions, this will generate a total of around 7,700 samples for further analysis.
[34]:
q_reg_multiregion_boot = xhfa.uncertainties.calculate_quantiles_over_boostraped_groups(
"023401",
ds_groups_h1_multiregion,
ds_moments_iter,
return_period=[2, 20, 100],
)
q_reg_multiregion_boot
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/uncertainties.py:223: FutureWarning: In a future version of xarray the default value for join will change from join='outer' to join='exact'. This change will result in the following ValueError: cannot be aligned with join='exact' because index/labels/sizes are not equal along these coordinates (dimensions): 'id' ('id',) The recommendation is to set join explicitly for this case.
/home/docs/checkouts/readthedocs.org/user_builds/xhydro/conda/stable/lib/python3.14/site-packages/xhydro/frequency_analysis/uncertainties.py:223: FutureWarning: In a future version of xarray the default value for coords will change from coords='different' to coords='minimal'. This is likely to lead to different results when multiple datasets have matching variables with overlapping values. To opt in to new defaults and get rid of these warnings now use `set_options(use_new_combine_kwarg_defaults=True) or set coords explicitly.
[34]:
<xarray.Dataset> Size: 333kB
Dimensions: (return_period: 3, id: 1, samples: 5950)
Coordinates:
* return_period (return_period) int64 24B 2 20 100
* id (id) <U6 24B '023401'
* samples (samples) object 48kB MultiIndex
name (samples) object 48kB 'Beaurivage' ... 'Beaurivage'
* region_id (samples) int64 48kB 0 0 0 0 0 0 ... 323 323 323 323 323 323
* obs_samples (samples) int64 48kB 0 1 2 3 4 5 6 7 ... 28 29 30 31 32 33 34
lmom <U4 16B 'l1'
variable <U12 48B 'q_max_annual'
Data variables:
q_max_annual (id, return_period, samples) float64 143kB 186.6 ... 372.17.3.4. d) Comparison¶
Let’s show the difference between approaches.
[35]:
fig, ax = plt.subplots(1, 3)
fig.set_figheight(4)
fig.set_figwidth(15)
# Subset the data
q_reg_boots = q_reg_boot.q_max_annual.sel(id="023401")
q_t_multiregions = q_t_multiregion.q_max_annual.sel(id="023401")
q_t_multiregion_boots = q_reg_multiregion_boot.q_max_annual.sel(id="023401")
def _make_plot(data, dim, label):
# Original fit
ax.plot(
loc.return_period.values,
loc,
"black",
label="Local frequency analysis",
)
ax.plot(
reg.return_period.values,
reg,
"red",
label="Original regional frequency analysis",
)
ax.plot(
data.return_period.values,
data.quantile(0.5, dim),
"green",
label=label,
)
data_05 = data.quantile(0.05, dim)
data_95 = data.quantile(0.95, dim)
ax.fill_between(
data.return_period.values, data_05, data_95, alpha=0.2, color="green"
)
plt.xscale("log")
plt.grid(visible=True)
plt.xlabel("Return period (years)")
plt.ylabel("Streamflow (m$^3$/s)")
plt.ylim([150, 500])
ax.legend()
ax = plt.subplot(1, 3, 1)
_make_plot(q_reg_boots, "samples", "Bootstrapped observations")
ax = plt.subplot(1, 3, 2)
_make_plot(q_t_multiregions, "region_id", "Multiple regions")
ax = plt.subplot(1, 3, 3)
_make_plot(q_t_multiregion_boots, "samples", "Combined")