Skip to content

Commit ea10616

Browse files
domnaMarJMue
andauthored
Fix for rii encoding problems under windows + windows pytest (for 3.13 only) (#228)
* Use chardet in RII scripts * Add windows pytest * Fix installation of uv * Us uv install action * Add dispersion with encoding errors * Cleanup diff --------- Co-authored-by: MarJMue <[email protected]>
1 parent 36dd713 commit ea10616

File tree

8 files changed

+73
-49
lines changed

8 files changed

+73
-49
lines changed

.github/workflows/pytest.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@ on:
1010
branches: [master]
1111

1212
jobs:
13+
test_python_win:
14+
runs-on: windows-latest
15+
16+
steps:
17+
- uses: actions/checkout@v5
18+
with:
19+
submodules: recursive
20+
- name: Set up Python 3.13
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: "3.13"
24+
25+
- name: Install the latest version of uv
26+
uses: astral-sh/setup-uv@v6
27+
28+
- name: Install module
29+
run: |
30+
uv pip install --system ".[fitting,dev]"
31+
uv pip install --system torch --index-url https://download.pytorch.org/whl/cpu
32+
- name: Test with pytest
33+
run: |
34+
pytest --nbmake
35+
1336
test_python:
1437
runs-on: ubuntu-latest
1538
strategy:

src/elli/database/refractive_index_info.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
IndexDispersionSum,
2929
)
3030
from ..materials import IsotropicMaterial
31+
from ..utils import detect_encoding
3132

3233
WavelengthFilterType = Union[
3334
None, float, int, List[Union[float, int]], Tuple[Union[float, int]]
@@ -287,18 +288,7 @@ def get_dispersion(
287288
Returns:
288289
Dispersion: A dispersion object containing the tabulated data.
289290
"""
290-
291-
index = self.catalog.loc[
292-
(self.catalog["book"] == book) & (self.catalog["page"] == page)
293-
].index
294-
295-
if len(index) != 1:
296-
raise ValueError("No entry found.")
297-
298-
yml_file = yaml.load(
299-
self.rii_path.joinpath(self.catalog.loc[index[0]]["path"]).read_text(),
300-
yaml.SafeLoader,
301-
)
291+
yml_file = self._open_file(book, page)
302292

303293
dispersion_list = []
304294
contains_index_dispersion = False
@@ -441,18 +431,7 @@ def get_reference(self, book: str, page: str) -> str:
441431
Returns:
442432
str: Reference information.
443433
"""
444-
445-
index = self.catalog.loc[
446-
(self.catalog["book"] == book) & (self.catalog["page"] == page)
447-
].index
448-
449-
if len(index) != 1:
450-
raise ValueError("No entry found.")
451-
452-
yml_file = yaml.load(
453-
self.rii_path.joinpath(self.catalog.loc[index[0]]["path"]).read_text(),
454-
yaml.SafeLoader,
455-
)
434+
yml_file = self._open_file(book, page)
456435

457436
return yml_file["REFERENCES"]
458437

@@ -466,6 +445,20 @@ def get_comment(self, book: str, page: str) -> str:
466445
Returns:
467446
str: Dispersion information.
468447
"""
448+
yml_file = self._open_file(book, page)
449+
450+
return yml_file["COMMENTS"]
451+
452+
def _open_file(self, book: str, page: str) -> dict:
453+
"""Opens the selected dispersion file.
454+
455+
Args:
456+
book (str): Name of the Material, named 'Book' on the website and the database. E.g. 'Au'
457+
page (str): Name of the Source, named 'Page' on the website and the database. E.g. 'Johnson'
458+
459+
Returns:
460+
dict: Content of the YAML file.
461+
"""
469462

470463
index = self.catalog.loc[
471464
(self.catalog["book"] == book) & (self.catalog["page"] == page)
@@ -474,9 +467,15 @@ def get_comment(self, book: str, page: str) -> str:
474467
if len(index) != 1:
475468
raise ValueError("No entry found.")
476469

477-
yml_file = yaml.load(
478-
self.rii_path.joinpath(self.catalog.loc[index[0]]["path"]).read_text(),
479-
yaml.SafeLoader,
470+
encoding = detect_encoding(
471+
self.rii_path.joinpath(self.catalog.loc[index[0]]["path"])
480472
)
481473

482-
return yml_file["COMMENTS"]
474+
with open(
475+
self.rii_path.joinpath(self.catalog.loc[index[0]]["path"]),
476+
"r",
477+
encoding=encoding,
478+
) as f:
479+
yml_file = yaml.load(f, yaml.SafeLoader)
480+
481+
return yml_file

src/elli/importer/__init__.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +0,0 @@
1-
import chardet
2-
3-
4-
def detect_encoding(fname: str) -> str:
5-
r"""Detects the encoding of file fname.
6-
Args:
7-
fname (str): Filename
8-
Returns:
9-
str: Encoding identifier string.
10-
"""
11-
with open(fname, "rb") as f:
12-
raw_data = f.read()
13-
result = chardet.detect(raw_data)
14-
return result["encoding"]

src/elli/importer/accurion.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import numpy as np
66
import pandas as pd
77

8-
from ..utils import convert_delta_range
9-
from . import detect_encoding
8+
from ..utils import convert_delta_range, detect_encoding
109

1110

1211
def read_accurion_psi_delta(fname: str) -> pd.DataFrame:

src/elli/importer/spectraray.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import pandas as pd
99
from packaging.version import Version, parse
1010

11-
from ..utils import calc_rho, convert_delta_range
12-
from . import detect_encoding
11+
from ..utils import calc_rho, convert_delta_range, detect_encoding
1312

1413

1514
def read_spectraray_psi_delta(

src/elli/importer/woollam.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from pint import DimensionalityError, UndefinedUnitError
1212

1313
from ..units import ureg
14-
from ..utils import calc_rho
15-
from . import detect_encoding
14+
from ..utils import calc_rho, detect_encoding
1615

1716
logger = logging.getLogger(__name__)
1817

src/elli/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Encoding: utf-8
22
from dataclasses import dataclass
33

4+
import chardet
45
import numpy as np
56
import numpy.typing as npt
67
import pandas as pd
@@ -361,3 +362,16 @@ def rotation_v_theta(v: npt.ArrayLike, theta: float) -> npt.NDArray:
361362
+ m_w * np.sin(np.deg2rad(theta))
362363
+ np.linalg.matrix_power(m_w, 2) * (1 - np.cos(np.deg2rad(theta)))
363364
)
365+
366+
367+
def detect_encoding(fname: str) -> str:
368+
r"""Detects the encoding of file fname.
369+
Args:
370+
fname (str): Filename
371+
Returns:
372+
str: Encoding identifier string.
373+
"""
374+
with open(fname, "rb") as f:
375+
raw_data = f.read()
376+
result = chardet.detect(raw_data)
377+
return result["encoding"]

tests/test_refractive_index_info.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Tests the importing from the refractiveindex.info database."""
22

3-
import elli
43
import numpy as np
54
import pytest
65

6+
import elli
7+
78

89
class TestRefractiveIndexInfo:
910
"""Test if the refractive index.info parser works."""
@@ -14,6 +15,10 @@ def test_get_mat(self):
1415
mat = self.RII.get_mat("Au", "Johnson")
1516
assert isinstance(mat, elli.IsotropicMaterial)
1617

18+
def test_encoding(self):
19+
mat = self.RII.get_mat("Ta2O5", "Franta")
20+
assert isinstance(mat, elli.IsotropicMaterial)
21+
1722
def test_dispersion_error(self):
1823
with pytest.raises(ValueError):
1924
self.RII.get_dispersion("foo", "bar")

0 commit comments

Comments
 (0)