Skip to content

Commit f83773a

Browse files
committed
added validator check
1 parent 3744c17 commit f83773a

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

petdeface/petdeface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
from mideface import ApplyMideface
3232
from mideface import Mideface
3333
from pet import WeightedAverage
34+
from utils import run_validator
3435
except ModuleNotFoundError:
3536
from .mideface import ApplyMideface
3637
from .mideface import Mideface
3738
from .pet import WeightedAverage
39+
from .utils import run_validator
3840

3941

4042
# collect version from pyproject.toml
@@ -207,6 +209,10 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
207209
else:
208210
args = args
209211

212+
# first check to see if the dataset is bids valid
213+
if not args.skip_bids_validator:
214+
run_validator(args.bids_dir)
215+
210216
if not check_valid_fs_license() and not locate_freesurfer_license().exists():
211217
raise Exception("You need a valid FreeSurfer license to proceed!")
212218

petdeface/utils.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import subprocess
2+
from pathlib import Path
3+
import json
4+
import sys
5+
6+
7+
class InvalidBIDSDataset(Exception):
8+
def __init__(self, message, errors):
9+
super().__init__(message)
10+
self.errors = errors
11+
print(f"{message}\n{errors}")
12+
13+
14+
def deno_validator_installed():
15+
get_help = subprocess.run(
16+
"bids-validator-deno --help", shell=True, capture_output=True
17+
)
18+
if get_help.returncode == 0:
19+
return True
20+
else:
21+
return False
22+
23+
24+
def run_validator(bids_path):
25+
bids_path = Path(bids_path)
26+
if bids_path.exists():
27+
pass
28+
else:
29+
raise FileNotFoundError(bids_path)
30+
if deno_validator_installed():
31+
command = f"bids-validator-deno {bids_path.resolve()} --ignoreWarnings --json --no-color"
32+
run_validator = subprocess.run(command, shell=True, capture_output=True)
33+
json_output = json.loads(run_validator.stdout.decode("utf-8"))
34+
# since we've ignored warnings any issue in issue is an error
35+
issues = json_output.get("issues").get("issues")
36+
formatted_errors = ""
37+
for issue in issues:
38+
formatted_errors += "\n" + json.dumps(issue, indent=4)
39+
if formatted_errors != "":
40+
raise InvalidBIDSDataset(
41+
message=f"Dataset at {bids_path} is invalid, see:",
42+
errors=formatted_errors,
43+
)
44+
45+
else:
46+
raise Exception(
47+
f"bids-validator-deno not found"
48+
+ "\nskip validation with --skip_bids_validator"
49+
+ "\nor install with pip install bids-validator-deno"
50+
)

poetry.lock

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "petdeface"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
description = "A nipype PET and MR defacing pipeline for BIDS datasets utilizing FreeSurfer's MiDeFace."
55
authors = ["Martin Nørgaard <[email protected]>", "Anthony Galassi <[email protected]>", "Murat Bilgel <[email protected]>"]
66
license = "MIT"
@@ -20,6 +20,7 @@ setuptools = "^68.1.2"
2020
petutils = "^0.0.1"
2121
niworkflows = "^1.11.0"
2222
niftifixer = {git = "https://github.com/openneuropet/nifti_fixer.git"}
23+
bids-validator-deno = "^2.0.5"
2324

2425

2526
[tool.poetry.group.dev.dependencies]

0 commit comments

Comments
 (0)