From c0206d9f442abd1baf451140f3657c4f4c263051 Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Wed, 10 Aug 2022 14:19:53 +0200 Subject: [PATCH 1/3] Add a more reasonable error on json cal loading error These error messages are caught in the GUI and returned to the user. --- rimseval/interfacer.py | 10 +- tests/func/conftest.py | 10 ++ .../func/data_files/incomplete_cal_file.json | 147 ++++++++++++++++++ tests/func/test_interfacer.py | 14 +- 4 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 tests/func/data_files/incomplete_cal_file.json diff --git a/rimseval/interfacer.py b/rimseval/interfacer.py index 75bfbbe..69225e8 100644 --- a/rimseval/interfacer.py +++ b/rimseval/interfacer.py @@ -69,7 +69,8 @@ def load_cal_file(crd: CRDFileProcessor, fname: Path = None) -> None: :param fname: Filename and path. If `None`, try file with same name as CRD file but `.json` suffix. - :raises OSError: Calibration file does not exist. + :raises IOError: Calibration file does not exist. + :raises IOError: JSON file cannot be decoded. JSON error message is returned too. """ if fname is None: fname = crd.fname.with_suffix(".json") @@ -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.""" diff --git a/tests/func/conftest.py b/tests/func/conftest.py index 227927a..fe21278 100644 --- a/tests/func/conftest.py +++ b/tests/func/conftest.py @@ -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() diff --git a/tests/func/data_files/incomplete_cal_file.json b/tests/func/data_files/incomplete_cal_file.json new file mode 100644 index 0000000..e606f37 --- /dev/null +++ b/tests/func/data_files/incomplete_cal_file.json @@ -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 + ] + } +} diff --git a/tests/func/test_interfacer.py b/tests/func/test_interfacer.py index 627b8d7..16b89d3 100644 --- a/tests/func/test_interfacer.py +++ b/tests/func/test_interfacer.py @@ -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): @@ -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("incomplete_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 From 9e99e9fbc61af35910a5e77e2362a3d96044c8f5 Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Wed, 10 Aug 2022 14:22:31 +0200 Subject: [PATCH 2/3] Rename the faulty calibration file --- .../{incomplete_cal_file.json => faulty_cal_file.json} | 0 tests/func/test_interfacer.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/func/data_files/{incomplete_cal_file.json => faulty_cal_file.json} (100%) diff --git a/tests/func/data_files/incomplete_cal_file.json b/tests/func/data_files/faulty_cal_file.json similarity index 100% rename from tests/func/data_files/incomplete_cal_file.json rename to tests/func/data_files/faulty_cal_file.json diff --git a/tests/func/test_interfacer.py b/tests/func/test_interfacer.py index 16b89d3..03ca76b 100644 --- a/tests/func/test_interfacer.py +++ b/tests/func/test_interfacer.py @@ -42,7 +42,7 @@ def test_read_lion_eval_calfile(mocker, crd_proc_mock): 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("incomplete_cal_file.json") + cal_file = data_files_path.joinpath("faulty_cal_file.json") err_exp = f"Cannot open the calibration file {cal_file.name}. JSON decode error." From 56fd174a08e6448455d4703e4510504ad07d1e35 Mon Sep 17 00:00:00 2001 From: Reto Trappitsch Date: Wed, 10 Aug 2022 14:44:07 +0200 Subject: [PATCH 3/3] Fix docstring --- rimseval/interfacer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rimseval/interfacer.py b/rimseval/interfacer.py index 69225e8..c6245ee 100644 --- a/rimseval/interfacer.py +++ b/rimseval/interfacer.py @@ -69,8 +69,8 @@ def load_cal_file(crd: CRDFileProcessor, fname: Path = None) -> None: :param fname: Filename and path. If `None`, try file with same name as CRD file but `.json` suffix. - :raises IOError: Calibration file does not exist. - :raises IOError: JSON file cannot be decoded. JSON error message is returned too. + :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")