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

Fixed test_write_report() log path issue #954

Merged
merged 3 commits into from
Apr 11, 2022
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
20 changes: 18 additions & 2 deletions dandi/bids_validator_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def validate_all(

def write_report(
validation_result,
report_path="bids-validator-report_{}.log",
report_path="/var/tmp/bids-validator-report_{}.log",
datetime_format="%Y%m%d-%H%M%S",
):
"""Write a human-readable report based on the validation result.
Expand All @@ -458,6 +458,12 @@ def write_report(
"""

report_path = report_path.format(datetime.datetime.now().strftime(datetime_format))
report_path = os.path.abspath(os.path.expanduser(report_path))
try:
os.makedirs(os.path.dirname(report_path))
except OSError:
pass

total_file_count = len(validation_result["path_listing"])
validated_files_count = total_file_count - len(validation_result["path_tracking"])
with open(report_path, "w") as f:
Expand Down Expand Up @@ -601,6 +607,7 @@ def validate_bids(
schema_version=None,
force_select=False,
debug=False,
report_path=False,
):
"""
Validate paths according to BIDS schema.
Expand All @@ -623,6 +630,10 @@ def validate_bids(
If None, the `dataset_description.json` fie will be queried for the dataset schema version.
force_select : bool, optional
Whether to fall back to newest version of schema if no version is given or found.
report_path : bool or str, optional
If `True` a log will be written using the standard output path of `.write_report()`.
If string, the string will be used as the output path.
If the variable evaluates as False, no log will be written.

Examples
--------
Expand All @@ -644,6 +655,11 @@ def validate_bids(
regex_schema,
debug=debug,
)
write_report(validation_result)

if report_path:
if isinstance(report_path, str):
write_report(validation_result, report_path=report_path)
else:
write_report(validation_result)
TheChymera marked this conversation as resolved.
Show resolved Hide resolved

return validation_result
16 changes: 15 additions & 1 deletion dandi/cli/cmd_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,29 @@
@devel_option(
"--schema", help="Validate against new BIDS schema version", metavar="VERSION"
)
@click.option("--report", help="Specify path to write a report under.")
@click.option(
"--report-flag",
"-r",
is_flag=True,
help="Whether to write a report under a"
"unique path in the current directory. Only usable if `--report` is not already used.",
)
@click.argument("paths", nargs=-1, type=click.Path(exists=True, dir_okay=True))
@devel_debug_option()
@map_to_click_exceptions
def validate_bids(paths, schema=None, devel_debug=False):
def validate_bids(
paths, schema=None, devel_debug=False, report=False, report_flag=False
):
"""Validate BIDS paths."""
from ..validate import validate_bids as validate_bids_

if report_flag and not report:
report = report_flag

validate_bids_(
*paths,
report=report,
schema_version=schema,
devel_debug=devel_debug,
)
Expand Down
7 changes: 0 additions & 7 deletions dandi/tests/data/output_bids_validator_xs_write.log

This file was deleted.

18 changes: 14 additions & 4 deletions dandi/tests/test_bids_validator_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_load_all():
assert "mandatory" in list(entry.keys())


def test_write_report():
def test_write_report(tmp_path):
from dandi.bids_validator_xs import write_report

validation_result = {}
Expand Down Expand Up @@ -273,8 +273,8 @@ def test_write_report():
]

report_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"data/output_bids_validator_xs_write.log",
tmp_path,
"output_bids_validator_xs_write.log",
TheChymera marked this conversation as resolved.
Show resolved Hide resolved
)
expected_report_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
Expand All @@ -288,7 +288,7 @@ def test_write_report():
assert report_text == expected_report_text


def test_bids_datasets(bids_examples):
def test_bids_datasets(bids_examples, tmp_path):
from dandi.bids_validator_xs import validate_bids

whitelist = [
Expand Down Expand Up @@ -327,6 +327,16 @@ def test_bids_datasets(bids_examples):
for f in files:
selected_path = os.path.join(root, f)
selected_paths.append(selected_path)
# Does terminal debug output work?
result = validate_bids(selected_paths, schema_version=schema_path, debug=True)
# Does default log path specification work?
result = validate_bids(selected_paths, schema_version=schema_path, report_path=True)
# Does custom log path specification work?
result = validate_bids(
selected_paths,
schema_version=schema_path,
debug=True,
report_path=os.path.join(tmp_path, "test_bids.log"),
)
# Have all files been validated?
assert len(result["path_tracking"]) == 0
11 changes: 7 additions & 4 deletions dandi/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def validate_bids(
*paths: str,
schema_version: Optional[str] = None,
devel_debug: bool = False,
report: Optional[str] = None,
) -> Any:
"""Validate BIDS paths.
Expand All @@ -20,18 +21,20 @@ def validate_bids(
BIDS schema version to use, this setting will override the version specified in the dataset.
devel_debug : bool, optional
Whether to trigger debugging in the BIDS validator.
report_path : bool or str, optional
If `True` a log will be written using the standard output path of `.write_report()`.
If string, the string will be used as the output path.
If the variable evaluates as False, no log will be written.
Notes
-----
Can be used from bash, as:
DANDI_DEVEL=1 dandi validate-bids --schema="1.7.0+012+dandi001" /data/paths
DANDI_DEVEL=1 dandi validate-bids --schema="1.7.0+012+dandi001" --report="my.log" /my/path
"""
from .bids_validator_xs import validate_bids as validate_bids_

return validate_bids_(
paths,
schema_version=schema_version,
debug=devel_debug,
paths, schema_version=schema_version, debug=devel_debug, report_path=report
)


Expand Down