Skip to content

Commit

Permalink
yaml validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldello committed Aug 2, 2023
1 parent b2f2eee commit 23752c3
Show file tree
Hide file tree
Showing 8 changed files with 3,305 additions and 1 deletion.
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- pytest-cov
- pip
- pyyaml
- jsonschema
- pip:
- ndx-franklab-novela
- neo
- neo
5 changes: 5 additions & 0 deletions src/spikegadgets_to_nwb/convert_yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yaml
from xml.etree import ElementTree
from spikegadgets_to_nwb import metadata_validation

from pynwb import NWBFile, TimeSeries
from pynwb.file import Subject, ProcessingModule
Expand Down Expand Up @@ -37,8 +38,12 @@ def load_metadata(
tuple[dict, list[dict]]
the yaml generator metadata and list of probe metadatas
"""
metadata = None
with open(metadata_path, "r") as stream:
metadata = yaml.safe_load(stream)
is_metadata_valid, metadata_errors = metadata_validation.validate(metadata)
if not is_metadata_valid:
raise ValueError("".join(metadata_errors))
probe_metadata = []
for path in probe_metadata_paths:
with open(path, "r") as stream:
Expand Down
74 changes: 74 additions & 0 deletions src/spikegadgets_to_nwb/metadata_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import jsonschema
import os
import yaml
import copy
import datetime


def _get_nwb_json_schema_path() -> str:
"""Get the NWB JSON Schema file path
Returns
-------
str
NWB Schema file Path
"""
current_path = os.path.dirname(os.path.abspath(__file__))
json_schema_file = "/./nwb_schema.json"
return f"{current_path}{os.path.normpath(json_schema_file)}"


def _get_json_schema() -> str:
"""Get JSON Schema
Returns
-------
str
JSON Schema content
"""
json_schema = None
json_schema_path = _get_nwb_json_schema_path()
with open(json_schema_path, "r") as stream:
json_schema = yaml.safe_load(stream)
return json_schema


def validate(metadata: dict) -> tuple:
"""Validates metadata
Parameters
----------
metadata : dict
metadata documenting the particulars of a session
Returns
-------
tuple
information of the validity of the metadata data and any errors
"""
assert metadata is not None # metadata cannot be null
assert isinstance(metadata, dict) # cannot proceed if metadata is not a dictionary

# date_of_birth is set to a datetime by the YAML-to-dict converter.
# This code converts date_of_birth to string
metadata_content = copy.deepcopy(metadata) or {}
if (
metadata_content["subject"]
and metadata_content["subject"]["date_of_birth"]
and type(metadata_content["subject"]["date_of_birth"]) is datetime.datetime
):
metadata_content["subject"]["date_of_birth"] = (
metadata_content["subject"]["date_of_birth"].utcnow().isoformat()
)

schema = _get_json_schema()
validator = jsonschema.Draft202012Validator(schema)
metadata_validation_errors = validator.iter_errors(metadata_content)
errors = []

for metadata_validation_error in metadata_validation_errors:
errors.append(metadata_validation_error.message)

is_valid = len(errors) == 0

return is_valid, errors
Loading

0 comments on commit 23752c3

Please sign in to comment.