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 bids-validator option #206

Merged
merged 6 commits into from
Jun 9, 2023
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
6 changes: 6 additions & 0 deletions dcm2bids/cli/dcm2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def _build_arg_parser():
default=DEFAULT.cliOutputDir,
help="Output BIDS directory. [%(default)s]")

p.add_argument("--bids_validate",
action='store_true',
help="If set, once your conversion is done it"
" will check if your output folder is BIDS valid. [%(default)s]\n"
f"bids-validator needs to be installed check: {DEFAULT.link_bids_validator}")

p.add_argument("--forceDcm2niix",
action="store_true",
help="Overwrite previous temporary dcm2niix "
Expand Down
76 changes: 16 additions & 60 deletions dcm2bids/dcm2bids_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
participant,
config,
output_dir=DEFAULT.outputDir,
bids_validate=DEFAULT.bids_validate,
session=DEFAULT.session,
clobber=DEFAULT.clobber,
forceDcm2niix=DEFAULT.forceDcm2niix,
Expand All @@ -56,6 +57,7 @@ def __init__(
self.config = load_json(valid_path(config, type="file"))
self.participant = Participant(participant, session)
self.clobber = clobber
self.bids_validate = bids_validate
self.forceDcm2niix = forceDcm2niix
self.logLevel = log_level

Expand All @@ -71,6 +73,8 @@ def __init__(
self.logger.info("session: %s", self.participant.session)
self.logger.info("config: %s", os.path.realpath(config))
self.logger.info("BIDS directory: %s", os.path.realpath(output_dir))
self.logger.info("Validate BIDS: %s", self.bids_validate)


@property
def dicomDirs(self):
Expand Down Expand Up @@ -134,6 +138,17 @@ def run(self):
acq.setDstFile()
intendedForList = self.move(acq, intendedForList)

if self.bids_validate:
try:
self.logger.info(f"Validate if { self.output_dir} is BIDS valid.")
self.logger.info("Use bids-validator version: ")
run_shell_command(['bids-validator', '-v'])
run_shell_command(['bids-validator', self.bidsDir])
except:
self.logger.info("The bids-validator does not seem to work properly. "
"The bids-validator may not been installed on your computer. "
f"Please check: https://github.com/bids-standard/bids-validator#quickstart.")

def move(self, acquisition, intendedForList):
"""Move an acquisition to BIDS format"""
for srcFile in glob(acquisition.srcRoot + ".*"):
Expand Down Expand Up @@ -186,63 +201,4 @@ def move(self, acquisition, intendedForList):
if intendedFile not in intendedForList[acquisition.indexSidecar]:
intendedForList[acquisition.indexSidecar].append(intendedFile)

return intendedForList


def _build_arg_parser():
p = argparse.ArgumentParser(description=__doc__, epilog=DEFAULT.EPILOG,
formatter_class=argparse.RawTextHelpFormatter)

p.add_argument("-d", "--dicom_dir",
type=Path, required=True, nargs="+",
help="DICOM directory(ies).")

p.add_argument("-p", "--participant",
required=True,
help="Participant ID.")

p.add_argument("-s", "--session",
required=False,
default="",
help="Session ID.")

p.add_argument("-c", "--config",
type=Path,
required=True,
help="JSON configuration file (see example/config.json).")

p.add_argument("-o", "--output_dir",
required=False,
type=Path,
default=Path.cwd(),
help="Output BIDS directory. (Default: %(default)s)")

p.add_argument("--forceDcm2niix",
action="store_true",
help="Overwrite previous temporary dcm2niix "
"output if it exists.")

p.add_argument("--clobber",
action="store_true",
help="Overwrite output if it exists.")

p.add_argument("-l", "--log_level",
required=False,
default=DEFAULT.cliLogLevel,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Set logging level. [%(default)s]")

return p


def main():
"""Let's go"""
parser = _build_arg_parser()
args = parser.parse_args()

app = Dcm2BidsGen(**vars(args))
return app.run()


if __name__ == "__main__":
sys.exit(main())
return intendedForList
3 changes: 3 additions & 0 deletions dcm2bids/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class DEFAULT(object):

doc = "Documentation at https://unfmontreal.github.io/Dcm2Bids/"

link_bids_validator = "https://github.com/bids-standard/bids-validator#quickstart"

# cli dcm2bids
cliSession = ""
cliOutputDir = os.getcwd()
Expand All @@ -21,6 +23,7 @@ class DEFAULT(object):
# dcm2bids.py
outputDir = Path.cwd()
session = "" # also Participant object
bids_validate = False
clobber = False
forceDcm2niix = False
defaceTpl = None
Expand Down
4 changes: 3 additions & 1 deletion docs/get-started/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ usage: dcm2bids [-h] -d DICOM_DIR [DICOM_DIR ...] -p PARTICIPANT [-s SESSION] -c
[-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-a]

Reorganising NIfTI files from dcm2niix into the Brain Imaging Data Structure
dcm2bids 2.1.7
dcm2bids 3.0.0

options:
-h, --help show this help message and exit
Expand All @@ -292,6 +292,8 @@ options:
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
Output BIDS directory, Default: current directory
(/home/sam/dcm2bids-proj)
--bids_validate If set, once your conversion is done it will check if your output folder is BIDS valid. [False]
bids-validator needs to be installed check: https://github.com/bids-standard/bids-validator#quickstart
--forceDcm2niix Overwrite previous temporary dcm2niix output if it exists
--clobber Overwrite output if it exists
-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Expand Down
8 changes: 6 additions & 2 deletions docs/tutorial/first-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ You can test it with any command but a safe way is to use the `--help` command.
[-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-a]

Reorganising NIfTI files from dcm2niix into the Brain Imaging Data Structure
dcm2bids 2.1.7
dcm2bids 3.0.0

options:
-h, --help show this help message and exit
Expand All @@ -98,6 +98,8 @@ You can test it with any command but a safe way is to use the `--help` command.
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
Output BIDS directory, Default: current directory
(/home/sam)
--bids_validate If set, once your conversion is done it will check if your output folder is BIDS valid. [False]
bids-validator needs to be installed check: https://github.com/bids-standard/bids-validator#quickstart
--forceDcm2niix Overwrite previous temporary dcm2niix output if it exists
--clobber Overwrite output if it exists
-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Expand Down Expand Up @@ -951,7 +953,7 @@ command.
[-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-a]

Reorganising NIfTI files from dcm2niix into the Brain Imaging Data Structure
dcm2bids 2.1.7
dcm2bids 3.0.0

options:
-h, --help show this help message and exit
Expand All @@ -965,6 +967,8 @@ command.
JSON configuration file (see example/config.json)
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
Output BIDS directory, Default: current directory (/home/sam/dcm2bids-tutorial/bids_project)
--bids_validate If set, once your conversion is done it will check if your output folder is BIDS valid. [False]
bids-validator needs to be installed check: https://github.com/bids-standard/bids-validator#quickstart
--forceDcm2niix Overwrite previous temporary dcm2niix output if it exists
--clobber Overwrite output if it exists
-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Expand Down
20 changes: 10 additions & 10 deletions requirements-doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,51 @@ filelock==3.6.0
future==0.18.3
ghp-import==2.0.2
gitdb==4.0.9
GitPython==3.1.29
GitPython==3.1.30
hug==2.6.1
idna==3.3
importlib-metadata==4.11.3
ipython==8.10.0
jedi==0.18.1
Jinja2==3.1.1
livereload==2.6.3
Mako==1.2.2
Mako==1.2.3
Markdown==3.3.6
MarkupSafe==2.1.1
matplotlib-inline==0.1.3
mergedeep==1.3.4
mike==1.1.2
mkdocs==1.2.4
mkdocs==1.3.0
mkdocs-git-authors-plugin==0.6.4
mkdocs-git-revision-date-localized-plugin==1.0.1
mkdocs-git-revision-date-plugin==0.3.2
mkdocs-material==7.3.0
mkdocs-material-extensions==1.0.3
packaging==21.3
packaging==23.1
parso==0.8.3
pdocs==1.1.1
pdocs==1.2.0
pexpect==4.8.0
pickleshare==0.7.5
platformdirs==2.5.1
pluggy==1.0.0
portray==1.7.0
portray==1.8.0
prompt-toolkit==3.0.30
ptyprocess==0.7.0
pure-eval==0.2.2
py==1.11.0
Pygments==2.11.2
pymdown-extensions==7.1
Pygments==2.12.0
pymdown-extensions==10.0
pyparsing==3.0.8
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
pyyaml_env_tag==0.1
requests==2.27.1
requests==2.31.0
six==1.16.0
smmap==5.0.0
stack-data==0.2.0
toml==0.10.2
tornado==6.1
tornado==6.3.2
tox==3.25.0
traitlets==5.1.1
urllib3==1.26.9
Expand Down