Skip to content

Commit

Permalink
Adapt tests and fitting decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
MarJMue committed Oct 22, 2024
1 parent d73da02 commit b241480
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/elli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from .dispersions import *
from .dispersions.base_dispersion import *
from .experiment import Experiment
from .importer.accurion import read_accurion_psi_delta
from .importer.accurion import read_accurion
from .importer.nexus import *
from .importer.spectraray import *
from .importer.woollam import read_woollam_psi_delta, read_woollam_rho, scale_to_nm
from .importer.woollam import read_woollam, scale_to_nm
from .materials import *
from .result import Result, ResultList
from .solver2x2 import Solver2x2
Expand Down
38 changes: 22 additions & 16 deletions src/elli/fitting/decorator_psi_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ def set_rho(self, update_exp: bool = False, update_names: bool = False) -> None:
self.fig.data[3].y = data.rho.imag

if update_exp:
exp_rho = calc_rho(self.exp_data)
self.fig.data[0].y = np.real(exp_rho).values
self.fig.data[1].y = np.imag(exp_rho).values
self.fig.data[0].y = np.real(self.exp_data.rho)
self.fig.data[1].y = np.imag(self.exp_data.rho)

if update_names:
self.fig.data[0].name = "ρr"
Expand All @@ -102,8 +101,8 @@ def set_residual(
exp_rho = calc_rho(self.exp_data)
self.fig.data[0].y = self.exp_data.psi - data.psi
self.fig.data[1].y = self.exp_data.delta - data.delta
self.fig.data[2].y = np.real(exp_rho).values - data.rho.real
self.fig.data[3].y = np.imag(exp_rho).values - data.rho.imag
self.fig.data[2].y = np.real(self.exp_data.rho) - data.rho.real
self.fig.data[3].y = np.imag(self.exp_data.rho) - data.rho.imag

if update_names:
self.fig.data[0].name = "Psi Res."
Expand Down Expand Up @@ -132,7 +131,7 @@ def set_pseudo_diel(
self.fig.data[3].y = peps.loc[:, "ϵ2"]

if update_exp:
exp_peps = calc_pseudo_diel(calc_rho(self.exp_data), self.angle)
exp_peps = calc_pseudo_diel(self.exp_data, self.angle)
self.fig.data[0].y = exp_peps.loc[:, "ϵ1"]
self.fig.data[1].y = exp_peps.loc[:, "ϵ2"]

Expand Down Expand Up @@ -270,11 +269,14 @@ def fit(self, method="leastsq"):
Returns:
Result: The fitting result
"""
rho = calc_rho(self.exp_data)
res = minimize(
self.fit_function,
self.params,
args=(rho.Wavelength, rho.values.real, rho.values.imag),
args=(
self.exp_data.Wavelength,
np.real(self.exp_data.rho),
np.imag(self.exp_data.rho),
),
method=method,
)

Expand All @@ -288,7 +290,9 @@ def plot(self) -> go.Figure:
return go.FigureWidget(
pd.concat(
[
self.exp_data.to_dataframe(),
self.exp_data[["psi", "delta"]]
.reset_coords(drop=True)
.to_dataframe(),
pd.DataFrame(
{"Ψ_fit": fit_result.psi, "Δ_fit": fit_result.delta},
index=self.exp_data.Wavelength,
Expand All @@ -299,18 +303,17 @@ def plot(self) -> go.Figure:

def plot_rho(self) -> go.Figure:
"""Plot the fit results as Rho"""
rho = calc_rho(self.exp_data)
fit_result = self.model(rho.Wavelength, self.fitted_params)
fit_result = self.model(self.exp_data.Wavelength, self.fitted_params)

return go.FigureWidget(
pd.DataFrame(
{
"ρr": rho.values.real,
"ρi": rho.values.imag,
"ρr": np.real(self.exp_data.rho),
"ρi": np.imag(self.exp_data.rho),
"ρcr": fit_result.rho.real,
"ρci": fit_result.rho.imag,
},
index=rho.Wavelength,
index=self.exp_data.Wavelength,
).plot(backend="plotly")
)

Expand Down Expand Up @@ -349,7 +352,10 @@ def get_model_data(
fit_result = self.model(self.exp_data.Wavelength, params)
desc = "model"

exp_data = {"psi-delta": self.exp_data, "rho": calc_rho(self.exp_data)}
exp_data = {
"psi-delta": self.exp_data[["psi", "delta"]],
"rho": self.exp_data.rho,
}

sim_data = {
"psi-delta": pd.DataFrame(
Expand Down Expand Up @@ -404,7 +410,7 @@ def __init__(
self.fig = go.FigureWidget(
pd.concat(
[
exp_data.to_dataframe(),
exp_data[["psi", "delta"]].reset_coords(drop=True).to_dataframe(),
pd.DataFrame(
{
"Ψ_calc": model(exp_data.Wavelength, params).psi,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_TiO2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def datadir(tmpdir, request):
@fixture
def meas_data(datadir):
"""Fixture for getting the reference measurement data from the file."""
return elli.read_spectraray_rho(datadir.join("TiO2_400cycles.txt")).sel(
return elli.read_spectraray(datadir.join("TiO2_400cycles.txt")).sel(
Angle_of_Incidence=70.06, Wavelength=slice(400, 800)
)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_accurion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@


# pylint: disable=redefined-outer-name
def test_reading_of_psi_delta_woollam(datadir):
def test_reading_accurion(datadir):
"""Psi/delta Accurion file is read w/o errors"""
data = elli.read_accurion_psi_delta(
datadir / "Si3N4_on_4inBF33_W02_20240903-150451.ds.dat"
)
data = elli.read_accurion(datadir / "Si3N4_on_4inBF33_W02_20240903-150451.ds.dat")

assert len(data.Wavelength) == 57
np.testing.assert_array_equal(data.Angle_of_Incidence, [40, 50])
16 changes: 3 additions & 13 deletions tests/test_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from __future__ import unicode_literals

import pytest

from fixtures import datadir # pylint: disable=unused-import

import elli


Expand All @@ -13,16 +13,6 @@
["ellips.test.nxs", "ellips_nx_opt.test.nxs"],
)
# pylint: disable=redefined-outer-name
def test_reading_of_psi_delta_nxs(datadir, filename):
def test_reading_nxs(datadir, filename):
"""Psi/delta NeXus file is read w/o errors"""
elli.read_nexus_psi_delta(datadir / filename)


@pytest.mark.parametrize(
"filename",
["ellips.test.nxs", "ellips_nx_opt.test.nxs"],
)
# pylint: disable=redefined-outer-name
def test_reading_and_conv_to_rho(datadir, filename):
"""Rho values are read from Psi / Delta file"""
elli.read_nexus_rho(datadir / filename)
elli.read_nexus(datadir / filename)
Loading

0 comments on commit b241480

Please sign in to comment.