{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: fit a baseline\n", "\n", "## Load the libraries and create a signal\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# load libraries\n", "%matplotlib inline\n", "import numpy as np\n", "import rampy\n", "\n", "# the following code is for the rendering of plotly graphs in Rampy docs, you don't need it\n", "# Set renderer for Jupyter notebooks\n", "import plotly.io as pio\n", "pio.renderers.default = 'notebook'\n", "\n", "# create signal\n", "nb_points =500\n", "x = np.linspace(50, 500, nb_points)\n", "\n", "# gaussian peaks\n", "p1 = 20.0 * np.exp(-np.log(2) * ((x-150.0)/15.0)**2)\n", "p2 = 100.0 * np.exp(-np.log(2) * ((x-250.0)/5.0)**2)\n", "p3 = 50.0 * np.exp(-np.log(2) * ((x-450.0)/1.0)**2)\n", "p4 = 20.0 * np.exp(-np.log(2) * ((x-350.0)/30.0)**2)\n", "p5 = 30.0 * np.exp(-np.log(2) * ((x-460.0)/5.0)**2)\n", "\n", "# background: a large sin distortion + linear \n", "bkg = 10*np.sin(x/50) + 0.1*x\n", "\n", "#noise\n", "noise = 2.0 * np.random.normal(size=nb_points)\n", "\n", "#observation\n", "y = p1 + p2 + p3 + p4 + p5 + noise +bkg\n", "\n", "# we use the rampy API (plotly behind it!) to plot the spectrum\n", "fig = rampy.plot_spectrum(x, y, title = \"our spectrum\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now using different baselines to retrieve the true background\n", "\n", "We also use the plot_spectrum function to show the different baselines." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_2305391/347025575.py:8: DeprecationWarning:\n", "\n", "The 'roi' parameter now has a default value of np.array([[0, 100]]). Please specify 'roi' explicitly in future calls to avoid this warning.\n", "\n", "/tmp/ipykernel_2305391/347025575.py:9: DeprecationWarning:\n", "\n", "The 'roi' parameter now has a default value of np.array([[0, 100]]). Please specify 'roi' explicitly in future calls to avoid this warning.\n", "\n", "/tmp/ipykernel_2305391/347025575.py:10: DeprecationWarning:\n", "\n", "The 'roi' parameter now has a default value of np.array([[0, 100]]). Please specify 'roi' explicitly in future calls to avoid this warning.\n", "\n" ] }, { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# need to define some fitting regions for the spline\n", "roi = [[0,100],[200,220],[280, 290],[420,430],[480,500]]\n", "\n", "# calculating the baselines\n", "ycalc_poly, base_poly = rampy.baseline(x, y, roi, method='poly', polynomial_order=4)\n", "ycalc_gcvspl, base_gcvspl = rampy.baseline(x,y,roi, method='gcvspline')\n", "ycalc_uni, base_uni = rampy.baseline(x, y, roi, method='unispline', s=1.2)\n", "ycalc_als, base_als = rampy.baseline(x, y, method='als', lam=10**5, p=0.05)\n", "ycalc_arpls, base_arpsl = rampy.baseline(x, y, method='arPLS', lam=10**6, ratio=0.001)\n", "ycalc_drpls, base_drpsl = rampy.baseline(x, y, method='drPLS', lam=10**6)\n", "ycalc_rubberband, base_rubberband = rampy.baseline(x, y, roi, method='rubberband')\n", "ycalc_whittaker, base_whittaker = rampy.baseline(x, y, roi, method='whittaker', lam=10**5)\n", "\n", "# doing the figure\n", "fig = rampy.plot_spectrum(x, y, baselines=[base_poly, base_gcvspl, base_uni, base_als, base_arpsl, base_drpsl, base_rubberband, base_whittaker],\n", " baseline_labels=['poly', 'gcvspline', 'unispline', 'als', 'arPLS', 'drPLS', 'rubberband', 'whittaker'],\n", " title = \"Various baseline comparison\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Automatic selection of smoothing coefficient for GCV spline\n", "\n", "For the GCV spline, we can forget about the s parameter and it will be determined using the GCV method, or we can set it to a desired value. Here is a comparison:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# calculating the baselines\n", "ycalc_gcvspl, base_gcvspl = rampy.baseline(x,y,roi,'gcvspline')\n", "ycalc_gcvspl_2, base_gcvspl_2 = rampy.baseline(x,y,roi,'gcvspline', s=1.0)\n", "\n", "# figure\n", "fig = rampy.plot_spectrum(x, y, \n", " baselines=[base_gcvspl, base_gcvspl_2],\n", " baseline_labels=['s calculated by GCV', 'manual s'],\n", " title = \"Comparing manual and GCV smoothing splines for baselines\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Machine learning baseline!\n", "\n", "You can use a Gaussian process to fit the baseline." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# calculating the baselines\n", "ycalc_GP, base_GP = rampy.baseline(x,y,roi,'GP')\n", "\n", "# figure\n", "fig = rampy.plot_spectrum(x, y, \n", " baselines=[base_gcvspl, base_GP],\n", " baseline_labels=['GCV spline', 'Gaussian Process'],\n", " title = \"Comparing GCV smoothing spline and Gaussian Process baselines\", xaxis_title=\"X\", yaxis_title=\"Y\")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "gpvisc", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 1 }