Skip to content

Commit

Permalink
add unit test for cubids bids-version
Browse files Browse the repository at this point in the history
  • Loading branch information
tientong98 committed Dec 12, 2024
1 parent 75bf86e commit 1736b51
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
16 changes: 8 additions & 8 deletions cubids/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _parse_validate():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -119,7 +119,7 @@ def _parse_bids_version():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -188,7 +188,7 @@ def _parse_group():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -255,7 +255,7 @@ def _parse_apply():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -351,7 +351,7 @@ def _parse_datalad_save():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -393,7 +393,7 @@ def _parse_undo():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -617,7 +617,7 @@ def _parse_remove_metadata_fields():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down Expand Up @@ -663,7 +663,7 @@ def _parse_print_metadata_fields():
type=PathExists,
action="store",
help=(
"the root of a BIDS dataset. It should contain "
"The root of a BIDS dataset. It should contain "
"sub-X directories and dataset_description.json"
),
)
Expand Down
5 changes: 3 additions & 2 deletions cubids/cubids.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,9 +1346,10 @@ def get_all_metadata_fields(self):
metadata = json.loads(content)
found_fields.update(metadata.keys())
except json.JSONDecodeError as e:
print(f"Error decoding JSON in {json_file}: {e}")
warnings.warn(f"Error decoding JSON in {json_file}: {e}")
except Exception as e:
print(f"Unexpected error with file {json_file}: {e}")
warnings.warn(f"Unexpected error with file {json_file}: {e}")

return sorted(found_fields)

def remove_metadata_fields(self, fields_to_remove):
Expand Down
52 changes: 51 additions & 1 deletion cubids/tests/test_bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
import pandas as pd
import pytest
from packaging.version import Version

from cubids.cubids import CuBIDS
from cubids.metadata_merge import merge_json_into_json, merge_without_overwrite
Expand All @@ -22,7 +23,15 @@
file_hash,
get_data,
)
from cubids.validator import build_validator_call, parse_validator_output, run_validator
from cubids.validator import (
build_validator_call,
parse_validator_output,
run_validator,
get_bids_validator_version,
extract_summary_info,
update_dataset_description,
bids_validator_version,
)

COMPLETE_KEY_GROUPS = [
"acquisition-HASC55AP_datatype-dwi_suffix-dwi",
Expand Down Expand Up @@ -1028,6 +1037,47 @@ def test_validator(tmp_path):
assert isinstance(parsed, pd.DataFrame)


def bids_validator_version(tmp_path):
"""Test validator.bids_validator_version."""
# Get the BIDS validator version
validator_version = get_bids_validator_version()
# Extract schemaVersion
summary_info = extract_summary_info(decoded)


def test_bids_version(tmp_path):
"""Test workflows.bids_version."""
data_root = get_data(tmp_path)
bids_dir = Path(data_root) / "complete"

# Ensure the test directory exists
assert bids_dir.exists()

# test the validator in valid dataset
call = build_validator_call(bids_dir)
ret = run_validator(call)

assert ret.returncode == 0

decoded = ret.stdout.decode("UTF-8")

# Get the BIDS validator version
validator_version = Version(get_bids_validator_version()["ValidatorVersion"])
# Extract schemaVersion
schema_version = Version(extract_summary_info(decoded)["SchemaVersion"])

# Set baseline versions to compare against
min_validator_version = Version("2.0.0")
min_schema_version = Version("0.11.3")

assert (
validator_version >= min_validator_version
), f"Validator version {validator_version} is less than minimum {min_validator_version}"
assert (
schema_version >= min_schema_version
), f"Schema version {schema_version} is less than minimum {min_schema_version}"


def test_docker():
"""Verify that docker is installed and the user has permission to run docker images.
Expand Down
3 changes: 2 additions & 1 deletion cubids/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import re
import subprocess
import warnings

import pandas as pd

Expand Down Expand Up @@ -249,7 +250,7 @@ def update_dataset_description(path, new_info):
)
print("Changes saved with DataLad.")
except subprocess.CalledProcessError as e:
print(f"Error running DataLad save: {e}")
warnings.warn(f"Error running DataLad save: {e}")


def bids_validator_version(output, path, write=False):
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ We can accomplish this using the following command:
$ conda install deno
The new schema ``bids-validator`` doesn't need to be installed
The new schema-based ``bids-validator`` doesn't need to be installed
and will be implemented automatically when `cubids validate` is called


Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ cubids-copy-exemplars = "cubids.cli:_enter_copy_exemplars"
cubids-undo = "cubids.cli:_enter_undo"
bids-sidecar-merge = "cubids.cli:_enter_bids_sidecar_merge"
cubids-validate = "cubids.cli:_enter_validate"
cubids-bids-version = "cubids.cli:_enter_bids_version"
cubids-datalad-save = "cubids.cli:_enter_datalad_save"
cubids-print-metadata-fields = "cubids.cli:_enter_print_metadata_fields"
cubids-remove-metadata-fields = "cubids.cli:_enter_remove_metadata_fields"
Expand Down

0 comments on commit 1736b51

Please sign in to comment.