diff --git a/tests/end_to_end/test_generate_and_fit.py b/tests/end_to_end/test_generate_and_fit.py index 1980aecc..8e96363c 100644 --- a/tests/end_to_end/test_generate_and_fit.py +++ b/tests/end_to_end/test_generate_and_fit.py @@ -1,5 +1,7 @@ import numpy as np +import pandas as pd +import ramanchada2 as rc2 from ramanchada2 import spectrum @@ -20,3 +22,29 @@ def test_generate_and_fit(): assert len(true_pos) == len(calc_pos), 'wrong number of peaks found' assert np.max(np.abs(true_pos - fit_pos - shift)) < 3, 'fit locations far from truth' assert np.max(np.abs(true_pos - calc_pos - shift)) < 5, 'find_peaks locations far from truth' + + +def test_gaussian_fit_parameters(): + x = np.linspace(-100, 3500, 2500) + params = [ + dict(center=400, sigma=6, amplitude=500), + dict(center=430, sigma=3, amplitude=300), + dict(center=600, sigma=10, amplitude=500), + dict(center=625, sigma=5, amplitude=300), + dict(center=700, sigma=10, amplitude=500), + dict(center=780, sigma=5, amplitude=300), + ] + spe = rc2.spectrum.from_theoretical_lines(shapes=['gaussian']*len(params), + params=params, x=x) + spe = spe.add_gaussian_noise_drift(sigma=.5, coef=.2) + spe = spe.subtract_moving_minimum(100) + params_df = pd.DataFrame.from_records(params) + + cand = spe.find_peak_multipeak(prominence=spe.y_noise_MAD()*10, width=3, wlen=100) + fitres = spe.fit_peak_multimodel(profile='Gaussian', candidates=cand, vary_baseline=True) + df = fitres.to_dataframe_peaks() + + assert np.all(np.isclose(df['amplitude'], params_df['amplitude'], atol=df['amplitude_stderr']*5)) + assert np.all(np.isclose(df['center'], params_df['center'], atol=df['center_stderr']*5)) + assert np.all(np.isclose(df['sigma'], params_df['sigma'], atol=df['sigma_stderr']*5)) + assert np.all(np.isclose(df['sigma']*np.sqrt(8*np.log(2)), df['fwhm']))