Skip to content

Commit

Permalink
add test + description.json for passing validation
Browse files Browse the repository at this point in the history
- also includes quality fixes"
  • Loading branch information
kaitj committed Feb 27, 2023
1 parent 80bf788 commit d0641d4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion snakebids/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def create_parser(include_snakemake=False):
"--skip-bids-validation",
"--skip_bids-validation",
action="store_true",
help=("Skip bids validation of input dataset")
help=("Skip bids validation of input dataset"),
)

standard_group.add_argument(
Expand Down
16 changes: 7 additions & 9 deletions snakebids/core/input_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def generate_inputs(

# Attempt to validate with node bids-validator, if needed
validated = _validate_input_dir if not skip_bids_validation else None

# Generates a BIDSLayout
# If not skipping validation, set validate indicator to opposite of output
# from _validate_input_dir, otherwise do not validate
Expand Down Expand Up @@ -351,7 +351,7 @@ def _gen_bids_layout(
derivatives subdirectory of the input dataset.
validate : bool
A boolean that indicates whether validation should be performed on
A boolean that indicates whether validation should be performed on
input dataset
pybids_database_dir : str
Expand Down Expand Up @@ -394,9 +394,9 @@ def _validate_input_dir(
bids_dir: Union[Path, str],
) -> bool:
"""Perform validation of dataset. Initial attempt at validation performed
with node-version of bids-validator. If not found, will fall back to
with node-version of bids-validator. If not found, will fall back to
validation integrated into pybids.
Parameters
----------
bids_dir : str
Expand All @@ -408,16 +408,14 @@ def _validate_input_dir(
Indication of whether validation was successfully performed using
the node-version of bids-validator
"""
try:
validator_config_dict = {
"ignoredFiles": ['/participants.tsv']
}
try:
validator_config_dict = {"ignoredFiles": ["/participants.tsv"]}

with tempfile.NamedTemporaryFile(mode="w+", suffix=".json") as temp:
temp.write(json.dumps(validator_config_dict))
temp.flush()

subprocess.check_call(['bids-validator', str(bids_dir), '-c', temp.name])
subprocess.check_call(["bids-validator", str(bids_dir), "-c", temp.name])

return True
except FileNotFoundError:
Expand Down
4 changes: 4 additions & 0 deletions snakebids/tests/data/dataset_description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Name": "Snakebids - test dataset",
"BIDSVersion": "1.8.0"
}
2 changes: 1 addition & 1 deletion snakebids/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_runs_in_correct_mode(
"pybids_db_reset": True,
"snakefile": Path("Snakefile"),
"output_dir": outputdir.resolve(),
"skip_bids_validation": False
"skip_bids_validation": False,
}
)
if root == "app" and tail == "":
Expand Down
42 changes: 21 additions & 21 deletions snakebids/tests/test_generate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ def test_ambiguous_paths_with_extra_entities_leads_to_error(

with pytest.raises(ConfigError):
generate_inputs(
root,
pybids_inputs,
skip_bids_validation=True,
use_bids_inputs=True
root, pybids_inputs, skip_bids_validation=True, use_bids_inputs=True
)

@settings(
Expand Down Expand Up @@ -129,10 +126,7 @@ def test_ambiguous_paths_with_missing_entity_leads_to_error(

with pytest.raises(ConfigError):
generate_inputs(
root,
pybids_inputs,
skip_bids_validation=True,
use_bids_inputs=True
root, pybids_inputs, skip_bids_validation=True, use_bids_inputs=True
)

@settings(
Expand Down Expand Up @@ -169,10 +163,7 @@ def test_entity_excluded_when_filter_false(
)

data = generate_inputs(
root,
pybids_inputs,
skip_bids_validation=True,
use_bids_inputs=True
root, pybids_inputs, skip_bids_validation=True, use_bids_inputs=True
)
assert data == expected

Expand Down Expand Up @@ -252,10 +243,7 @@ def test_entity_excluded_when_filter_true(self, tmpdir: Path, dataset: BidsDatas
)

data = generate_inputs(
root,
pybids_inputs,
skip_bids_validation=True,
use_bids_inputs=True
root, pybids_inputs, skip_bids_validation=True, use_bids_inputs=True
)
assert data == expected

Expand Down Expand Up @@ -1007,24 +995,36 @@ def start(self, tmpdir):

# Copy test data
shutil.copytree(self.bids_dir, f"{self.tmp_dir}/data")
assert filecmp.dircmp("snakebids/tests/data/bids_t1w", f"{self.tmp_dir}/data")

# Copy dataset description
shutil.copy(
"snakebids/tests/data/dataset_description.json", f"{self.tmp_dir}/data"
)

def test_check_validator(self):
"""Test validator defaults to pybids"""
assert _validate_input_dir(self.bids_dir) == False

"""Test validator defaults to pybids (i.e. False)"""
assert _validate_input_dir(self.bids_dir) == False

# Test for validation failure
def test_pybids_validation_fail(self):
with pytest.raises(BIDSValidationError):
_gen_bids_layout(
bids_dir=f"{self.tmp_dir}/data",
bids_dir=self.bids_dir,
derivatives=False,
pybids_database_dir=None,
pybids_reset_database=False,
validate=True,
)

def test_pybids_validation_pass(self):
assert _gen_bids_layout(
bids_dir=f"{self.tmp_dir}/data",
derivatives=False,
pybids_database_dir=None,
pybids_reset_database=False,
validate=True,
)


class TestDB:
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit d0641d4

Please sign in to comment.