Skip to content

Commit

Permalink
add basic synthetic calibration test
Browse files Browse the repository at this point in the history
  • Loading branch information
jveitchmichaelis committed May 16, 2021
1 parent 545ba1e commit 53da7f0
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/test_synthetic_calibration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np
import pytest

from rascal.synthetic import SyntheticSpectrum
from rascal.calibrator import Calibrator

def test_default():

# Create a test spectrum with a simple linear relationship
# between pixels/wavelengths. The intercept is set to
# 100 nm and the dispersion is set to 2.
intercept = 100
dispersion = 2.0
s = SyntheticSpectrum(coefficients = [dispersion, intercept])

# We add a bunch of wavelegnths between 200-1200 nm
waves = np.linspace(200,1200, num=20)
peaks = s.get_pixels(waves)
assert len(peaks) > 0

# Set up the calibrator with the pixel values of our
# wavelengths
c = Calibrator(peaks=peaks)

# Arbitrarily we'll set the number of pixels to 768 (i.e.
# a max range of around 1500 nm
c.set_calibrator_properties(num_pix=768)

# Setup the Hough transform parameters
c.set_hough_properties(range_tolerance=100.,
min_wavelength=100.,
max_wavelength=1500.)

# Add our fake lines as the atlas
c.add_user_atlas(elements=["Test"]*len(waves), wavelengths=waves)
assert len(c.atlas) > 0

# And let's try and fit...
best_p, rms, residual, peak_utilisation = c.fit(max_tries=200)

best_p, x_fit, y_fit, residual, peak_utilisation = c.match_peaks(
best_p, refine=False, robust_refit=True)

fit_diff = c.polyval(x_fit, best_p) - y_fit
rms = np.sqrt(np.sum(fit_diff**2 / len(x_fit)))

assert peak_utilisation > 0.9
assert rms < 1e-6

0 comments on commit 53da7f0

Please sign in to comment.