-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
3,436 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
name: spikegadgets_to_nwb | ||
channels: | ||
- defaults | ||
- conda-forge | ||
- franklab | ||
- defaults | ||
- conda-forge | ||
- franklab | ||
dependencies: | ||
- numpy | ||
- scipy | ||
- pandas | ||
- pynwb | ||
- ipython | ||
- black | ||
- pytest | ||
- pytest-cov | ||
- pip | ||
- pyyaml | ||
- pip: | ||
- ndx-franklab-novela | ||
- numpy | ||
- scipy | ||
- pandas | ||
- pynwb | ||
- ipython | ||
- black | ||
- pytest | ||
- pytest-cov | ||
- pip | ||
- pyyaml | ||
- jsonschema | ||
- pip: | ||
- ndx-franklab-novela |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.