Skip to content

Commit

Permalink
Merge pull request #40 from RIMS-Code/error_catching_json_calibration…
Browse files Browse the repository at this point in the history
…_read

Add a more reasonable error on json cal loading error
  • Loading branch information
trappitsch authored Aug 10, 2022
2 parents 8b89463 + 7cf0910 commit 0aa2e79
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 2 deletions.
8 changes: 7 additions & 1 deletion rimseval/interfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def load_cal_file(crd: CRDFileProcessor, fname: Path = None) -> None:
`.json` suffix.
:raises OSError: Calibration file does not exist.
:raises OSError: JSON file cannot be decoded. JSON error message is returned too.
"""
if fname is None:
fname = crd.fname.with_suffix(".json")
Expand All @@ -78,7 +79,12 @@ def load_cal_file(crd: CRDFileProcessor, fname: Path = None) -> None:
raise OSError(f"The requested calibration file {fname} does not exist.")

with fname.open("r", encoding="utf-8") as fin:
json_object = json.load(fin)
try:
json_object = json.load(fin)
except json.decoder.JSONDecodeError as orig_err:
raise OSError(
f"Cannot open the calibration file {fname.name}. JSON decode error."
) from orig_err

def entry_loader(key: str, json_obj: Any) -> Any:
"""Return the value of a json_object dictionary if existent, otherwise None."""
Expand Down
10 changes: 10 additions & 0 deletions tests/func/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ def crd_int_delta(crd_file) -> CRDFileProcessor:
)
crd.integrals_pkg = np.array([crd.integrals, crd.integrals])
return crd


@pytest.fixture
def data_files_path(request) -> Path:
"""Provides the path to the `data_files` folder.
:return: Path to the folder
"""
curr = Path(request.fspath).parents[0]
return Path(curr).joinpath("data_files").absolute()
147 changes: 147 additions & 0 deletions tests/func/data_files/faulty_cal_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"mcal": [
[
5.302762235136544,
89.9046977
],
[
5.552698550914553,
90.9056396
],
[
5.8006809603321,
91.9050347
],
[
6.293683219315044,
93.9063108
],
[
6.781419663024983,
95.9082714
]
],
"integral_names": [
"Zr-90",
"Zr-91",
"Zr-92",
"Zr-94",
"Zr-96",
],
"integrals": [
[
89.7446977,
90.0646977
],
[
90.7456396,
91.0656396
],
[
91.7450347,
92.0650347
],
[
93.7463108,
94.0663108
],
[
95.74827140000001,
96.0682714
]
],
"background_names": [
"Zr-96",
"Zr-96",
"Zr-94",
"Zr-94",
"Zr-92",
"Zr-92",
"Zr-91",
"Zr-91",
"Zr-90",
"Zr-90"
],
"backgrounds": [
[
95.55629192191135,
95.7582714
],
[
96.05827140000001,
96.20540570285735
],
[
93.51206792520081,
93.7563108
],
[
94.0563108,
94.23868782924484
],
[
91.49690872465203,
91.7550347
],
[
92.05503470000001,
92.20415209792155
],
[
90.57652351286292,
90.7556396
],
[
91.0556396,
91.22563729380892
],
[
89.59800870875029,
89.7546977
],
[
90.0546977,
90.23743422430904
]
],
"applied_filters": {
"spectrum_part": [
true,
[
[
0,
125000
]
]
],
"max_ions_per_shot": [
false,
15
],
"max_ions_per_time": [
false,
1,
0.01
],
"max_ions_per_tof_window": [
false,
1,
[
2.07,
2.2
]
],
"packages": [
false,
1000
],
"max_ions_per_pkg": [
false,
1000
],
"dead_time_corr": [
true,
15
]
}
}
14 changes: 13 additions & 1 deletion tests/func/test_interfacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np

from rimseval import interfacer
from rimseval.processor import CRDFileProcessor


def test_read_lion_eval_calfile(mocker, crd_proc_mock):
Expand Down Expand Up @@ -39,3 +38,16 @@ def test_read_lion_eval_calfile(mocker, crd_proc_mock):
np.testing.assert_almost_equal(integrals_rec[1], integrals_exp[1])
assert backgrounds_rec[0] == backgrounds_exp[0]
np.testing.assert_almost_equal(backgrounds_rec[1], backgrounds_exp[1])


def test_read_lion_eval_calfile_read_error(data_files_path, crd_proc_mock):
"""Raise IOError if the calibration file cannot be read successfully."""
cal_file = data_files_path.joinpath("faulty_cal_file.json")

err_exp = f"Cannot open the calibration file {cal_file.name}. JSON decode error."

with pytest.raises(IOError) as err:
interfacer.load_cal_file(crd_proc_mock, cal_file)

msg = err.value.args[0]
assert msg == err_exp

0 comments on commit 0aa2e79

Please sign in to comment.