Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test parameters from gaussian fit #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']))
Loading