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

Add "hed-validator" to run validation on BIDS dataset #1025

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
76 changes: 76 additions & 0 deletions hed/scripts/hed_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import argparse
import json
import sys


def main():
# Create the argument parser
parser = argparse.ArgumentParser(description="Validate an HED BIDS dataset.")

# Positional argument for the dataset path
parser.add_argument("dataset_path", help="Path to the dataset directory")

# Optional argument for the format
parser.add_argument("-f", "--format", choices=["text", "json", "json_pp"], default="text",
help="Output format: 'text' (default) or 'json' ('json_pp' for pretty-printed json)")

# Optional argument for the output file
parser.add_argument("-o", "--output-file", help="File to save the output. If not provided, output is printed to the screen")

# Optional flag to check for warnings
parser.add_argument("--check-for-warnings", action="store_true",
help="Enable checking for warnings during validation")

# Parse the arguments
args = parser.parse_args()

issue_list = validate_dataset(args)

# Return 1 if there are issues, 0 otherwise
return int(bool(issue_list))


def validate_dataset(args):
# Delayed imports to speed up --help
from hed.errors import get_printable_issue_string
from hed.tools import BidsDataset
from hed import _version as vr

# Validate the dataset
bids = BidsDataset(args.dataset_path)
issue_list = bids.validate(check_for_warnings=args.check_for_warnings)
# Output based on format
if args.format in ("json", "json_pp"):
kw = {"indent": 4} if args.format == "json_pp" else {}
output = json.dumps(
{
"issues": issue_list,
"hedtools_version": str(vr.get_versions())
},
**kw)
elif args.format == "json":
output = json.dumps(issue_list)
elif args.format == "text":
# Print HEDTOOLS version
print(f"Using HEDTOOLS version: {str(vr.get_versions())}")

if issue_list:
output = get_printable_issue_string(issue_list, "HED validation errors: ", skip_filename=False)
# Print number of issues
print(f"Number of issues: {len(issue_list)}")
else:
output = "No HED validation errors"
else:
raise ValueError(args.format)
# Output to file or print to screen
if args.output_file:
with open(args.output_file, 'w') as fp:
fp.write(output)
else:
print(output)
return issue_list


if __name__ == "__main__":
sys.exit(main())

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies = [
run_remodel = "hed.tools.remodeling.cli.run_remodel:main"
run_remodel_backup = "hed.tools.remodeling.cli.run_remodel_backup:main"
run_remodel_restore = "hed.tools.remodeling.cli.run_remodel_restore:main"
hed-validator = "hed.scripts.hed_validator:main"
hed_validate_schemas = "hed.scripts.validate_schemas:main"
hed_update_schemas = "hed.scripts.convert_and_update_schema:main"
hed_add_ids = "hed.scripts.add_hed_ids:main"
Expand Down