diff --git a/orsopy/fileio/base.py b/orsopy/fileio/base.py index 3875186..d71f69c 100644 --- a/orsopy/fileio/base.py +++ b/orsopy/fileio/base.py @@ -954,6 +954,33 @@ def _read_header_data(file: Union[TextIO, str], validate: bool = False) -> Tuple return dct_list, data, version +def _validate_columns(cols: List): + """ + Checks that the first four columns have the correct properties, i.e. names, units, etc. + + :param cols: list of column information entries. + """ + if len(cols) < 2: + raise ValueError( + "The ORSO specification requires at least 'Qz' and 'R' columns to be present" + ) + + names = ["Qz", "R", "sR", "sQz"] + for name, col in zip(names, cols): + col_name = col.get("name", None) + if col_name is not None and col_name != name: + raise ValueError( + "The first four columns should be named 'Qz', 'R', 'sR', 'sQz')" + ) + if cols[0]["unit"] not in ["1/angstrom", "1/nm"]: + raise ValueError( + "The Qz column should have units of '1/angstrom' or '1/nm'" + ) + # sQz unit has to be the same as Qz + if len(cols) > 3 and cols[3].get("unit", None) is not None and cols[0]["unit"] != cols[3]["unit"]: + raise ValueError("The sQz column should have the same units as the Qz column") + + def _validate_header_data(dct_list: List[dict]): """ Checks whether a json dictionary corresponds to a valid ORSO header. @@ -971,6 +998,9 @@ def _validate_header_data(dct_list: List[dict]): """ import jsonschema + for dct in dct_list: + _validate_columns(dct.get("columns", [])) + vi = sys.version_info if vi.minor < 7: warnings.warn( diff --git a/orsopy/fileio/tests/test_schema.py b/orsopy/fileio/tests/test_schema.py index 5096b24..629a116 100644 --- a/orsopy/fileio/tests/test_schema.py +++ b/orsopy/fileio/tests/test_schema.py @@ -1,6 +1,5 @@ import json import unittest -import sys from pathlib import Path @@ -42,27 +41,16 @@ def test_empty_ort(self): _validate_header_data([test.to_dict()]) def test_wrong_schema(self): - vi = sys.version_info - - with self.assertRaises(jsonschema.exceptions.ValidationError): + with self.assertRaises(ValueError): _validate_header_data([{}]) test = fileio.Orso.empty() test.columns[0].unit = "test_fail_unit" - if vi.minor < 7: - with self.assertWarns(fileio.base.ORSOSchemaWarning): - _validate_header_data([test.to_dict()]) - else: - with self.assertRaises(jsonschema.exceptions.ValidationError): - _validate_header_data([test.to_dict()]) + with self.assertRaises(ValueError): + _validate_header_data([test.to_dict()]) test.columns[0].unit = "1/nm" _validate_header_data([test.to_dict()]) test.columns[1].name = "NotRightColumn" - - if vi.minor < 7: - with self.assertWarns(fileio.base.ORSOSchemaWarning): - _validate_header_data([test.to_dict()]) - else: - with self.assertRaises(jsonschema.exceptions.ValidationError): - _validate_header_data([test.to_dict()]) + with self.assertRaises(ValueError): + _validate_header_data([test.to_dict()])