Skip to content

Commit

Permalink
Test parameters from gaussian fit
Browse files Browse the repository at this point in the history
  • Loading branch information
georgievgeorgi committed Nov 18, 2024
1 parent d7a6dd5 commit 3939a8d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/end_to_end/test_generate_and_fit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import pandas as pd

import ramanchada2 as rc2
from ramanchada2 import spectrum


Expand All @@ -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']))

0 comments on commit 3939a8d

Please sign in to comment.