Skip to content

Commit

Permalink
yaml validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldello committed Sep 21, 2023
1 parent becb7ad commit abd2d02
Show file tree
Hide file tree
Showing 8 changed files with 3,306 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,7 +14,8 @@ dependencies:
- pytest-cov
- pip
- pyyaml
- jsonschema
- pip:
- ndx-franklab-novela
- neo
- hdmf=3.8.1
- hdmf=3.8.1
6 changes: 6 additions & 0 deletions src/spikegadgets_to_nwb/convert_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from copy import deepcopy
from datetime import datetime
from xml.etree import ElementTree
from spikegadgets_to_nwb import metadata_validation


import numpy as np
import pandas as pd
Expand Down Expand Up @@ -40,8 +42,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))

Check warning on line 50 in src/spikegadgets_to_nwb/convert_yaml.py

View check run for this annotation

Codecov / codecov/patch

src/spikegadgets_to_nwb/convert_yaml.py#L50

Added line #L50 was not covered by tests
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 abd2d02

Please sign in to comment.