Skip to content

Commit

Permalink
Increased code coverage with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshpinto committed Oct 1, 2023
1 parent 66946ad commit a9662a7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
22 changes: 0 additions & 22 deletions qudi_hira_analysis/helper_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import numpy as np
from scipy import sparse


def decibelm_to_watts(dbm_value: float) -> float:
Expand Down Expand Up @@ -44,23 +42,3 @@ def format_exponent_as_str(
def log_tick_formatter(val, pos=None):
""" Format ticks for log10 scale plots """
return rf"$10^{{{val:.0f}}}$"


def baseline_als(y: np.ndarray, lam: float = 1e6, p: float = 0.9,
niter: int = 10) -> np.ndarray:
"""
Asymmetric least squares baseline.
Source: Paul H. C. Eilers, Hans F.M. Boelens. Baseline Correction with Asymmetric
Least Squares Smoothing (2005).
"""
L = len(y) # noqa: N806
D = sparse.csc_matrix(np.diff(np.eye(L), 2)) # noqa: N806
w = np.ones(L)

z = None
for _ in range(niter):
W = sparse.spdiags(w, 0, L, L) # noqa: N806
Z = W + lam * D.dot(D.transpose()) # noqa: N806
z = sparse.linalg.spsolve(Z, w * y)
w = p * (y > z) + (1 - p) * (y < z)
return z
7 changes: 5 additions & 2 deletions tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pathlib import Path
from unittest import TestCase

import lmfit

from qudi_hira_analysis import DataHandler

logging.disable(logging.CRITICAL)
Expand Down Expand Up @@ -37,13 +39,14 @@ def test_pulsedodmr_lorentziandouble_fit(self):
odmr_list = self.dh.load_measurements(measurement_str="ODMR", pulsed=True)
odmr = odmr_list["20220315-2050-39"]

x_fit, y_fit, _ = self.dh.fit(
x_fit, y_fit, res = self.dh.fit(
x="Controlled variable(Hz)",
y="Signal",
data=odmr.data,
fit_function=self.dh.fit_function.lorentziandouble
)

odmr.fit_model = res
self.assertIsInstance(odmr.fit_model, lmfit.model.ModelResult)
self.assertAlmostEqual(x_fit.tolist()[0], 2850000000.0)
self.assertAlmostEqual(y_fit.tolist()[0], 1.1015461033795304)

Expand Down
27 changes: 27 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
from unittest import TestCase

from qudi_hira_analysis.helper_functions import (
decibelm_to_watts,
format_exponent_as_str,
log_tick_formatter,
)

logging.disable(logging.CRITICAL)


class TestFitting(TestCase):
def test_decibelm_to_watts(self):
self.assertEqual(0.01, decibelm_to_watts(10))

def test_format_exponent_as_str(self):
self.assertEqual(
"${ 1.0 } \cdot 10^{ -4 }$",
format_exponent_as_str(0.0001)
)

def test_log_tick_formatter(self):
self.assertEqual(
"$10^{1}$",
log_tick_formatter(1)
)
5 changes: 4 additions & 1 deletion tests/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ def test_bruker_mfm_load(self):
bruker_measurements = self.dh.load_measurements(measurement_str="",
extension=".001", qudi=False)

bruker_data = bruker_measurements[next(iter(bruker_measurements))].data
bruker = bruker_measurements[next(iter(bruker_measurements))]
bruker_data = bruker.data
scan_size = bruker.get_param_from_filename(unit="um")
mfm = bruker_data.get_channel("Phase", mfm=True)

self.assertEqual(scan_size, 15.0)
self.assertEqual(mfm.channel, "Phase")
self.assertEqual(mfm.type, "Bruker MFM")
bruker_data.file.close()
Expand Down

0 comments on commit a9662a7

Please sign in to comment.