.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/optimizers/plot_custom_bc_1_whittaker.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_optimizers_plot_custom_bc_1_whittaker.py: Customized Baseline Correction ------------------------------ This example looks at the ingenious baseline correction method created by Liland et al., :meth:`~.Baseline.custom_bc`. The :meth:`.custom_bc` method works exceedingly well for morphological and smoothing baselines, since those methods typically depend directly on the number of data points, and for Whittaker-smoothing-based methods, since the `lam` value is :ref:`heavily dependant on the number of data points `. This example will examine the use of the optimizer method :meth:`~.Baseline.custom_bc` paired with the Whittaker-smoothing-based method :meth:`~.Baseline.arpls` .. GENERATED FROM PYTHON SOURCE LINES 20-43 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from pybaselines import Baseline from pybaselines.utils import gaussian x = np.linspace(20, 1000, 1000) signal = ( + gaussian(x, 6, 240, 5) + gaussian(x, 8, 350, 11) + gaussian(x, 15, 400, 18) + gaussian(x, 6, 550, 6) + gaussian(x, 13, 700, 8) + gaussian(x, 9, 800, 9) + gaussian(x, 9, 880, 7) ) baseline = 5 + 6 * np.exp(-(x - 40) / 30) + gaussian(x, 5, 1000, 300) noise = np.random.default_rng(0).normal(0, 0.1, len(x)) y = signal + baseline + noise baseline_fitter = Baseline(x_data=x) .. GENERATED FROM PYTHON SOURCE LINES 45-55 For certain types of data, there can often be a sharp change in the baseline within a small region, such as in Raman spectroscopy near a Raman shift of 0 or in XRD at low two-theta. This presents a significant challenge to baseline algorithms that fit a single "global" baseline such as Whittaker-smoothing-based methods. The majority of the data can be fit using a "stiff" baseline, but the anomolous region requires a more flexible baseline. Plotting each of these two cases separately, it is apparent each fits its target region well, but combining the two into a single baseline is difficult. .. GENERATED FROM PYTHON SOURCE LINES 55-67 .. code-block:: Python lam_flexible = 1e2 lam_stiff = 5e5 flexible_baseline = baseline_fitter.arpls(y, lam=lam_flexible)[0] stiff_baseline = baseline_fitter.arpls(y, lam=lam_stiff)[0] plt.figure() plt.plot(x, y) plt.plot(x, flexible_baseline, label='Flexible baseline') plt.plot(x, stiff_baseline, label='Stiff baseline') plt.legend() .. image-sg:: /generated/examples/optimizers/images/sphx_glr_plot_custom_bc_1_whittaker_001.png :alt: plot custom bc 1 whittaker :srcset: /generated/examples/optimizers/images/sphx_glr_plot_custom_bc_1_whittaker_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-76 The beauty of Liland's customized baseline correction method is that it allows fitting baselines that are stiff in some regions and flexible in others by simply truncating the data to increase the stiffness. The input ``lam`` value within `method_kwargs` should correspond to the most flexible region, and the truncation should begin close to where the stiff and flexible baselines overlap, which is at approximately x=160 from the above figure. A small `lam` value of 1e1 is used to then smooth the calculated baseline using Whittaker smoothing so that the two regions connect without any significant discontinuity. .. GENERATED FROM PYTHON SOURCE LINES 76-92 .. code-block:: Python crossover_index = np.argmin(abs(x - 160)) fit_baseline, params = baseline_fitter.custom_bc( y, 'arpls', regions=([crossover_index, None],), sampling=15, method_kwargs={'lam': lam_flexible}, lam=1e1 ) plt.figure() plt.plot(x, y) plt.plot(x, fit_baseline, label='Fit baseline') plt.plot(x, baseline, '--', label='True baseline') plt.legend() .. image-sg:: /generated/examples/optimizers/images/sphx_glr_plot_custom_bc_1_whittaker_002.png :alt: plot custom bc 1 whittaker :srcset: /generated/examples/optimizers/images/sphx_glr_plot_custom_bc_1_whittaker_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-96 Looking at the results, this method is able to accurately recreate the true data even though the two baselines have significantly different requirements for stiffness. .. GENERATED FROM PYTHON SOURCE LINES 96-104 .. code-block:: Python plt.figure() plt.plot(x, y - baseline, label='True data') plt.plot(x, y - fit_baseline, label='Baseline corrected') plt.legend() plt.show() .. image-sg:: /generated/examples/optimizers/images/sphx_glr_plot_custom_bc_1_whittaker_003.png :alt: plot custom bc 1 whittaker :srcset: /generated/examples/optimizers/images/sphx_glr_plot_custom_bc_1_whittaker_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.193 seconds) .. _sphx_glr_download_generated_examples_optimizers_plot_custom_bc_1_whittaker.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_custom_bc_1_whittaker.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_custom_bc_1_whittaker.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_custom_bc_1_whittaker.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_