Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: added separate column validation #127

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions orsopy/fileio/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down
22 changes: 5 additions & 17 deletions orsopy/fileio/tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import unittest
import sys

from pathlib import Path

Expand Down Expand Up @@ -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()])