Skip to content

Commit 21dc4bd

Browse files
domnaMarJMue
andauthored
Fix for wollam data without dpolE (#214)
* Fix for wollam data without dpolE * Use union typing to support older py versions * Fix `as_index()` and `as_dielectric()` * Add test to check if values stay the same after dispersion conversion * Remove `as_dielectric()` * Fix tests * Set rep and single params * Remove unecessary import * Update changelog --------- Co-authored-by: MarJMue <[email protected]>
1 parent 38443ec commit 21dc4bd

File tree

6 files changed

+621
-24
lines changed

6 files changed

+621
-24
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Version 0.22.4
4+
5+
### Bugfixes
6+
7+
- Fix for an error when copying of an IndexDispersion created from a Dispersion
8+
- Fix for reading of woollam data if no dpolE is present
9+
310
## Version 0.22.3
411

512
### Bugfixes

src/elli/dispersions/base_dispersion.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""Abstract base class and utility classes for pyElli dispersion"""
33

44
from abc import ABC, abstractmethod
5-
from copy import deepcopy
65
from typing import List, Optional, Union
76

87
import numpy as np
@@ -239,15 +238,25 @@ def as_index(self):
239238
Please ensure that you know what you are doing as building dielectric
240239
and index based dispersions is normally mathematically wrong.
241240
"""
242-
index_class = deepcopy(self)
243-
# pylint: disable=attribute-defined-outside-init
244-
index_class.refractive_index = lambda lbda: sqrt(
245-
index_class.dielectric_function(lbda)
241+
242+
AnonymousIndexDispersion = type(
243+
"AnonymousIndexDispersion",
244+
(IndexDispersion,),
245+
{
246+
"rep_params_template": self.rep_params_template,
247+
"single_params_template": self.single_params_template,
248+
"dielectric_function": self.dielectric_function,
249+
"refractive_index": lambda self, lbda: sqrt(
250+
self.dielectric_function(lbda)
251+
),
252+
},
246253
)
247-
index_class.__class__ = IndexDispersion # pylint: disable=invalid-name
248-
index_class.dielectric_function = self.dielectric_function
249254

250-
return index_class
255+
idx_dispersion = AnonymousIndexDispersion()
256+
idx_dispersion.rep_params = self.rep_params
257+
idx_dispersion.single_params = self.single_params
258+
259+
return idx_dispersion
251260

252261

253262
class IndexDispersion(BaseDispersion):
@@ -301,18 +310,6 @@ def __add__(
301310
def dielectric_function(self, lbda: npt.ArrayLike) -> npt.NDArray:
302311
return self.refractive_index(lbda) ** 2
303312

304-
def as_dielectric(self):
305-
"""
306-
Returns this class as Dispersion.
307-
This method may be used to add dielectric and index based dispersions.
308-
Please ensure that you know what you are doing as building dielectric
309-
and index based dispersions is normally mathematically wrong.
310-
"""
311-
diel_disp = deepcopy(self)
312-
diel_disp.__class__ = Dispersion # pylint: disable=invalid-name
313-
diel_disp.dielectric_function = self.dielectric_function
314-
return diel_disp
315-
316313

317314
class DispersionFactory:
318315
"""A factory class for dispersion objects"""

src/elli/importer/woollam.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import logging
77
import re
8-
from typing import TextIO
8+
from typing import TextIO, Union
99

1010
import pandas as pd
1111
from pint import DimensionalityError, UndefinedUnitError
@@ -20,7 +20,7 @@
2020
is_float_regex = re.compile(r"[+-]?(\d+([.]\d*)?([eE][+-]?\d+)?|[.]\d+([eE][+-]?\d+)?)")
2121

2222

23-
def is_float(line: str) -> bool:
23+
def is_float(line: Union[float, int, str]) -> bool:
2424
"""Checks whether the given line is a float
2525
2626
Args:
@@ -29,7 +29,11 @@ def is_float(line: str) -> bool:
2929
Returns:
3030
bool: True if it is a float, False otherwise
3131
"""
32-
return bool(is_float_regex.search(line))
32+
if isinstance(line, (float, int)):
33+
return True
34+
if isinstance(line, str):
35+
return bool(is_float_regex.search(line))
36+
return False
3337

3438

3539
def _is_wvase_tabular(line: str) -> bool:
@@ -126,7 +130,7 @@ def _read_wvase_dataframe(file_object: TextIO) -> pd.DataFrame:
126130
header=None,
127131
names=["Wavelength", "Angle of Incidence", "Ψ", "Δ", "Ψ_err", "Δ_err"],
128132
)
129-
print(dframe)
133+
130134
dframe = (
131135
dframe[dframe.apply(lambda x: is_float(x.iloc[0]), axis=1)]
132136
.set_index(["Wavelength", "Angle of Incidence"])

tests/test_dispersions.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for dispersion models"""
22

33
import os
4+
from copy import deepcopy
45
from shutil import copytree, rmtree
56

67
import numpy as np
@@ -11,6 +12,7 @@
1112

1213
import elli
1314
from elli.dispersions.base_dispersion import InvalidParameters
15+
from elli.dispersions.sellmeier import Sellmeier
1416

1517

1618
@fixture
@@ -233,3 +235,38 @@ def test_correct_reconstruction_with_pseudo_dielectric():
233235
).get_dielectric_df(check_lbda),
234236
gaussian.get_dielectric_df(check_lbda),
235237
)
238+
239+
240+
def test_deepcopy_of_index_dispersion():
241+
sell = Sellmeier()
242+
sell.add(1, 1)
243+
244+
deepcopy(sell.as_index())
245+
246+
247+
def test_index_dispersion_conversion():
248+
sell = Sellmeier()
249+
sell.add(1, 1)
250+
251+
lbda = np.linspace(300, 900, 100)
252+
253+
assert_array_equal(
254+
sell.as_index().get_dielectric(lbda),
255+
sell.get_dielectric(lbda),
256+
)
257+
258+
assert_array_equal(
259+
sell.as_index().add(2, 2).get_dielectric(lbda), sell.get_dielectric(lbda)
260+
)
261+
262+
263+
def test_dispersion_conversion_to_other_type():
264+
lbda = np.linspace(300, 900, 100)
265+
RII = elli.db.RII()
266+
267+
disp = RII.get_dispersion("AgCl", "Tilton")
268+
269+
assert_array_equal(
270+
disp.as_index().get_dielectric(lbda),
271+
disp.get_dielectric(lbda),
272+
)

tests/test_wollam.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
import pytest
44
from fixtures import datadir # pylint: disable=unused-import
5+
56
import elli
67

78

89
# pylint: disable=redefined-outer-name
910
def test_reading_of_psi_delta_woollam(datadir):
1011
"""Psi/delta Spectraray file is read w/o errors"""
1112
data_wvase = elli.read_woollam_psi_delta(datadir / "wvase_example.dat")
13+
data_wvase_wo_dpolE = elli.read_woollam_psi_delta(
14+
datadir / "wvase_example_wo_dpolE.dat"
15+
)
1216
data_cease = elli.read_woollam_psi_delta(datadir / "complete_ease_example.dat")
1317

1418
assert data_wvase.shape == (542, 2)
19+
assert data_wvase_wo_dpolE.shape == (542, 2)
1520
assert data_cease.shape == (3263, 2)
1621

1722

0 commit comments

Comments
 (0)