{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced tools\n", "\n", "The previous notebook covered the most common utilities of `xsdba` for conventional cases. Here, we explore more advanced usage of `xsdba` tools.\n", "\n", "## Optimization with dask\n", "\n", "Adjustment processes can be very heavy when we need to compute them over large regions and long timeseries. Using small groupings (like `time.dayofyear`) adds precision and robustness, but also decouples the load and computing complexity. Fortunately, unlike the heroic pioneers of scientific computing who managed to write parallelized FORTRAN, we now have [dask](https://dask.org/). With only a few parameters, we can magically distribute the computing load to multiple workers and threads.\n", "\n", "A good first read on the use of dask within xarray are the latter's [Optimization tips](https://xarray.pydata.org/en/stable/user-guide/dask.html#optimization-tips).\n", "\n", "Some `xsdba`-specific tips:\n", "\n", "* Most adjustment method will need to perform operation on the whole `time` coordinate, so it is best to optimize chunking along the other dimensions. This is often different from how public data is shared, where more universal 3D chunks are used.\n", "\n", " Chunking of outputs can be controlled in xarray's [to_netcdf](https://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_netcdf.html?highlight=to_netcdf#xarray.Dataset.to_netcdf). We also suggest using [Zarr](https://zarr.readthedocs.io/en/stable/) files. According to [its creators](https://ui.adsabs.harvard.edu/abs/2018AGUFMIN33A..06A/abstract), `zarr` stores should give better performances, especially because of their better ability for parallel I/O. See [Dataset.to_zarr](https://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_zarr.html?highlight=to_zarr#xarray.Dataset.to_zarr) and this useful [rechunking package](https://rechunker.readthedocs.io).\n", "\n", "\n", "* One of the main bottleneck for adjustments with small groups is that dask needs to build and optimize an enormous task graph. This issue is alleviated with the use of `map_blocks` in the adjustment methods. However, not all adjustment methods use this optimized syntax.\n", "\n", " In order to help dask, one can split the processing in parts. For splitting training and adjustment, see [the section below](#Initializing-an-Adjustment-object-from-a-training-dataset).\n", "\n", "\n", "* Another massive bottleneck of parallelization of `xarray` is the thread-locking behaviour of some methods. It is quite difficult to isolate and avoid these locking instances, so one of the best workarounds is to use `dask` configurations with many _processes_ and few _threads_. The former do not share memory and thus are not impacted when a lock is activated from a thread in another worker. However, this adds many memory transfer operations and, by experience, reduces dask's ability to parallelize some pipelines. Such a dask Client is usually created with a large `n_workers` and a small `threads_per_worker`.\n", "\n", "\n", "* Sometimes, datasets have auxiliary coordinates (for example : lat / lon in a rotated pole dataset). Xarray handles these variables as data variables and will **not** load them if dask is used. However, in some operations, `xsdba` or `xarray` will trigger access to those variables, triggering computations each time, since they are `dask`-based. To avoid this behaviour, one can load the coordinates, or simply remove them from the inputs.\n", "\n", "\n", "## LOESS smoothing and detrending\n", "\n", "As described in Cleveland (1979), locally weighted linear regressions are multiple regression methods using a nearest-neighbour approach. Instead of using all data points to compute a linear or polynomial regression, LOESS algorithms compute a local regression for each point in the dataset, using only the k-nearest neighbours as selected by a weighting function. This weighting function must fulfill some strict requirements, see the doc of `xsdba.loess.loess_smoothing` for more details.\n", "\n", "In `xsdba`'s implementation, the user can choose between local _constancy_ ($d=0$, local estimates are weighted averages) and local _linearity_ ($d=1$, local estimates are taken from linear regressions). Two weighting functions are currently implemented : \"tricube\" ($w(x) = (1 - x^3)^3$) and \"gaussian\" ($w(x) = e^{-x^2 / 2\\sigma^2}$). Finally, the number of Cleveland's _robustifying iterations_ is controllable through `niter`. After computing an estimate of $y(x)$, the weights are modulated by a function of the distance between the estimate and the points and the procedure is started over. These iterations are made to weaken the effect of outliers on the estimate.\n", "\n", "The next example shows the application of the LOESS to daily temperature data. The black line and dot are the estimated $y$, outputs of the `xsdba.loess.loess_smoothing` function, using local linear regression (passing $d = 1$), a window spanning 20% ($f = 0.2$) of the domain, the \"tricube\" weighting function and only one iteration. The red curve illustrates the weighting function on January 1st 2014, where the red circles are the nearest-neighbours used in the estimation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "from __future__ import annotations\n", "\n", "from tempfile import TemporaryDirectory\n", "\n", "import matplotlib.pyplot as plt\n", "import nc_time_axis\n", "import numpy as np\n", "import xarray as xr\n", "\n", "from xsdba import loess\n", "\n", "%matplotlib inline\n", "plt.style.use(\"seaborn-v0_8\")\n", "plt.rcParams[\"figure.figsize\"] = (11, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Daily temperature data from xarray's tutorials\n", "ds = xr.tutorial.open_dataset(\"air_temperature\").resample(time=\"D\").mean()\n", "tas = ds.isel(lat=0, lon=0).air\n", "\n", "# Compute the smoothed series\n", "f = 0.2\n", "ys = loess.loess_smoothing(tas, d=1, weights=\"tricube\", f=f, niter=1)\n", "\n", "# Plot data points and smoothed series\n", "fig, ax = plt.subplots()\n", "ax.plot(tas.time, tas, \"o\", fillstyle=\"none\")\n", "ax.plot(tas.time, ys, \"k\")\n", "ax.set_xlabel(\"Time\")\n", "ax.set_ylabel(\"Temperature [K]\")\n", "\n", "## The code below calls internal functions to demonstrate how the weights are computed.\n", "\n", "# LOESS algorithms as implemented here use scaled coordinates.\n", "x = tas.time\n", "x = (x - x[0]) / (x[-1] - x[0])\n", "xi = x[366]\n", "ti = tas.time[366]\n", "\n", "# Weighting function take the distance with all neighbors scaled by the r parameter as input\n", "r = int(f * tas.time.size)\n", "h = np.sort(np.abs(x - xi))[r]\n", "weights = loess._tricube_weighting(np.abs(x - xi).values / h)\n", "\n", "# Plot nearest neighbors and weighing function\n", "wax = ax.twinx()\n", "wax.plot(tas.time, weights, color=\"indianred\")\n", "ax.plot(tas.time, tas.where(tas * weights > 0), \"o\", color=\"lightcoral\", alpha=0.5)\n", "\n", "ax.plot(ti, ys[366], \"ko\")\n", "wax.set_ylabel(\"Weights\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "LOESS smoothing can suffer from heavy boundary effects. On the previous graph, we can associate the strange bend on the left end of the line to them. The next example shows a stronger case. Usually, $\\frac{f}{2}N$ points on each side should be discarded. On the other hand, LOESS has the advantage of always staying within the bounds of the data.\n", "\n", "\n", "### LOESS Detrending\n", "\n", "In climate science, it can be used in the detrending process. `xsdba` provides `xsdba.detrending.LoessDetrend` in order to compute trend with the LOESS smoothing and remove them from timeseries.\n", "\n", "First we create some toy data with a sinusoidal annual cycle, random noise and a linear temperature increase." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time = xr.cftime_range(\"1990-01-01\", \"2049-12-31\", calendar=\"noleap\")\n", "tas = xr.DataArray(\n", " (\n", " 10 * np.sin(time.dayofyear * 2 * np.pi / 365)\n", " + 5 * (np.random.random_sample(time.size) - 0.5) # Annual variability\n", " + np.linspace(0, 1.5, num=time.size) # Random noise\n", " ), # 1.5 degC increase in 60 years\n", " dims=(\"time\",),\n", " coords={\"time\": time},\n", " attrs={\"units\": \"degC\"},\n", " name=\"temperature\",\n", ")\n", "tas.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we compute the trend on the data. Here, we compute on the whole timeseries (`group='time'`) with the parameters suggested above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xsdba.detrending import LoessDetrend\n", "\n", "# Create the detrending object\n", "det = LoessDetrend(group=\"time\", d=0, niter=2, f=0.2)\n", "# Fitting returns a new object and computes the trend.\n", "fit = det.fit(tas)\n", "# Get the detrended series\n", "tas_det = fit.detrend(tas)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "fit.ds.trend.plot(ax=ax, label=\"Computed trend\")\n", "ax.plot(time, np.linspace(0, 1.5, num=time.size), label=\"Expected tred\")\n", "ax.plot([time[0], time[int(0.1 * time.size)]], [0.4, 0.4], linewidth=6, color=\"gray\")\n", "ax.plot([time[-int(0.1 * time.size)], time[-1]], [1.1, 1.1], linewidth=6, color=\"gray\")\n", "ax.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As said earlier, this example shows how the Loess has strong boundary effects. It is recommended to remove the $\\frac{f}{2}\\cdot N$ outermost points on each side, as shown by the gray bars in the graph above.\n", "\n", "## Initializing an Adjustment object from a training dataset\n", "\n", "For large scale uses, when the training step deserves its own computation and write to disk, or simply when there are multiples `sim` to be adjusted with the same training, it is helpful to be able to instantiate the Adjustment objects from the training dataset itself. This trick relies on a global attribute \"adj_params\" set on the training dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import xarray as xr\n", "\n", "# Create toy data for the example, here fake temperature timeseries\n", "t = xr.cftime_range(\"2000-01-01\", \"2030-12-31\", freq=\"D\", calendar=\"noleap\")\n", "ref = xr.DataArray(\n", " (\n", " -20 * np.cos(2 * np.pi * t.dayofyear / 365)\n", " + 2 * np.random.random_sample((t.size,))\n", " + 273.15\n", " + 0.1 * (t - t[0]).days / 365\n", " ), # \"warming\" of 1K per decade,\n", " dims=(\"time\",),\n", " coords={\"time\": t},\n", " attrs={\"units\": \"K\"},\n", ")\n", "sim = xr.DataArray(\n", " (\n", " -18 * np.cos(2 * np.pi * t.dayofyear / 365)\n", " + 2 * np.random.random_sample((t.size,))\n", " + 273.15\n", " + 0.11 * (t - t[0]).days / 365\n", " ), # \"warming\" of 1.1K per decade\n", " dims=(\"time\",),\n", " coords={\"time\": t},\n", " attrs={\"units\": \"K\"},\n", ")\n", "\n", "ref = ref.sel(time=slice(None, \"2015-01-01\"))\n", "hist = sim.sel(time=slice(None, \"2015-01-01\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xsdba.adjustment import QuantileDeltaMapping\n", "\n", "QDM = QuantileDeltaMapping.train(\n", " ref, hist, nquantiles=15, kind=\"+\", group=\"time.dayofyear\"\n", ")\n", "QDM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The trained `QDM` exposes the training data in the `ds` attribute, Here, we will write it to disk, read it back and initialize a new object from it. Notice the `adj_params` in the dataset, that has the same value as the repr string printed just above. Also, notice the `_xsdba_adjustment` attribute that contains a JSON string, so we can rebuild the adjustment object later." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "QDM.ds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The engine keyword is only needed if netCDF4 is not available\n", "with TemporaryDirectory() as tmpdir:\n", " QDM.ds.to_netcdf(f\"{tmpdir}/QDM_training.nc\", engine=\"h5netcdf\")\n", " ds = xr.open_dataset(f\"{tmpdir}/QDM_training.nc\", engine=\"h5netcdf\").load()\n", " QDM2 = QuantileDeltaMapping.from_dataset(ds)\n", " ds.close()\n", "QDM2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the case above, creating a full object from the dataset doesn't make the most sense since we are in the same python session, with the \"old\" object still available. This method effective when we reload the training data in a different python session, say on another computer. **However, take note that there is no retro-compatibility insurance.** If the `QuantileDeltaMapping` class was to change in a new `xsdba` version, one would not be able to create the new object from a dataset saved with the old one.\n", "\n", "For the case where we stay in the same Python session, it is still useful to trigger the dask computations. For small datasets, that could mean a simple `QDM.ds.load()`, but sometimes even the training data is too large to be full loaded in memory. In that case, we could also do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The engine keyword is only needed if netCDF4 is not available\n", "with TemporaryDirectory(delete=False) as tmpdir:\n", " QDM.ds.to_netcdf(f\"{tmpdir}/QDM_training2.nc\", engine=\"h5netcdf\")\n", " ds = xr.open_dataset(f\"{tmpdir}/QDM_training2.nc\", engine=\"h5netcdf\")\n", " ds.attrs[\"title\"] = \"This is the dataset, but read from disk.\"\n", " QDM.set_dataset(ds)\n", "QDM.ds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "QDM2.adjust(sim)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Retrieving extra output diagnostics\n", "\n", "\n", "\n", "To fully understand what is happening during the bias-adjustment process, `xsdba` can output _diagnostic_ variables, giving more visibility to what the adjustment is doing behind the scene. This behaviour, a `verbose` option, is controlled by the `extra_output` option, set with `xsdba.set_options`. When `True`, `train` calls are instructed to include additional variables to the training datasets. In addition, the `adjust` calls will always output a dataset, with `scen` and, depending on the algorithm, other diagnostics variables. See the documentation of each `Adjustment` objects to see what extra variables are available.\n", "\n", "For the moment, this feature is still under construction and only a few `Adjustment` actually provide these extra outputs. Please open issues on the GitHub repo if you have needs or ideas of interesting diagnostic variables.\n", "\n", "For example, `QDM.adjust` adds `sim_q`, which gives the quantile of each element of `sim` within its group." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xsdba import set_options\n", "\n", "with set_options(extra_output=True):\n", " QDM = QuantileDeltaMapping.train(\n", " ref, hist, nquantiles=15, kind=\"+\", group=\"time.dayofyear\"\n", " )\n", " out = QDM.adjust(sim)\n", "\n", "out.sim_q" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Moving window for adjustments\n", "\n", "Some Adjustment methods require that the adjusted data (`sim`) be of the same length (same number of points) than the training data (`ref` and `hist`). These requirements often ensure conservation of statistical properties and a better representation of the climate change signal over the long adjusted timeseries.\n", "\n", "In opposition to a conventional \"rolling window\", here it is the _years_ that are the base units of the window, not the elements themselves. `xsdba` implements `xsdba.stack_periods` and `xsdba.unstack_periods` to manipulate data in that goal. The \"stack\" function cuts the data in overlapping windows of a certain length and stacks them along a new `\"period\"` dimension, alike to xarray's `da.rolling(time=win).construct('period')`, but with yearly steps. The stride (or step) between each window can also be controlled. This argument is an indicator of how many years overlap between each window. With a value of `1`, a window will have `window - 1` years overlapping with the previous one. The default (`None`) is to have `stride = window` will result in no overlap at all. The default units in which `window` and `stride` are given is a year (\"YS\"), but can be changed with argument `freq`.\n", "\n", "By chunking the result along this `'period'` dimension, it is expected to be more computationally efficient (when using `dask`) than looping over the windows with a for-loop (or a `GroupyBy`)\n", "\n", "Note that this results in two restrictions:\n", "\n", "1. The constructed array has the same \"time\" axis for all windows. This is a problem if the actual _year_ is of importance for the adjustment, but this is not the case for any of `xsdba`'s current adjustment methods.\n", "2. The input timeseries must be in a calendar with uniform year lengths. For daily data, this means only the \"360_day\", \"noleap\" and \"all_leap\" calendars are supported.\n", "\n", "The \"unstack\" function does the opposite: it concatenates the windows together to recreate the original timeseries. It only works for the no-overlap case where `stride = window` and for the non-ambiguous one where `stride` divides `window` into an odd number (N) of parts. In that latter situation, the middle parts of each period are kept when reconstructing the timeseries, in addition to the first (last) parts of the first (last) period needed to get a full timeseries.\n", "\n", "Quantile Delta Mapping requires that the adjustment period should be of a length similar to the training one. As our `ref` and `hist` cover 15 years but `sim` covers 31 years, we will transform `sim` by stacking windows of 15 years. With a stride of five (5) years, this means the first window goes from 2000 to 2014 (inclusive). Then 2005-2019, 2010-2024 and 2015-2029. The last year will be dropped as it can't be included in any complete window.\n", "\n", "