From 8b5214e5d99e7b08d9284942799ab9ce90d997d3 Mon Sep 17 00:00:00 2001 From: "arnaud.bore" Date: Thu, 8 Jun 2023 11:51:27 -0400 Subject: [PATCH 1/6] add bids-validator option --- dcm2bids/cli/dcm2bids.py | 6 ++++ dcm2bids/dcm2bids_gen.py | 78 ++++++++++------------------------------ dcm2bids/utils/utils.py | 3 ++ 3 files changed, 27 insertions(+), 60 deletions(-) diff --git a/dcm2bids/cli/dcm2bids.py b/dcm2bids/cli/dcm2bids.py index 3b3a007f..f0ee9a5a 100644 --- a/dcm2bids/cli/dcm2bids.py +++ b/dcm2bids/cli/dcm2bids.py @@ -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 " diff --git a/dcm2bids/dcm2bids_gen.py b/dcm2bids/dcm2bids_gen.py index d49db765..daaeea3a 100644 --- a/dcm2bids/dcm2bids_gen.py +++ b/dcm2bids/dcm2bids_gen.py @@ -13,6 +13,8 @@ from datetime import datetime from glob import glob +from bids_validator import BIDSValidator + from dcm2bids.dcm2niix_gen import Dcm2niixGen from dcm2bids.utils.logger import setup_logging from dcm2bids.sidecar import Sidecar, SidecarPairing @@ -43,6 +45,7 @@ def __init__( participant, config, output_dir=DEFAULT.outputDir, + bids_validate=DEFAULT.bids_validate, session=DEFAULT.session, clobber=DEFAULT.clobber, forceDcm2niix=DEFAULT.forceDcm2niix, @@ -56,6 +59,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 @@ -71,6 +75,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): @@ -134,6 +140,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("bids-validator does not seem to work properly" + "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 + ".*"): @@ -186,63 +203,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 \ No newline at end of file diff --git a/dcm2bids/utils/utils.py b/dcm2bids/utils/utils.py index f2548c98..2f1d03e4 100644 --- a/dcm2bids/utils/utils.py +++ b/dcm2bids/utils/utils.py @@ -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() @@ -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 From 70ca1123a8e513798c961ed06d7f54304f6fb458 Mon Sep 17 00:00:00 2001 From: "arnaud.bore" Date: Thu, 8 Jun 2023 12:58:16 -0400 Subject: [PATCH 2/6] fix requirements --- requirements-doc.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/requirements-doc.txt b/requirements-doc.txt index ba3eff87..2aa78f42 100644 --- a/requirements-doc.txt +++ b/requirements-doc.txt @@ -13,7 +13,7 @@ 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 @@ -21,43 +21,43 @@ 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 +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 From 3ea86b9713fec1cc36651a6b355054cf253b7264 Mon Sep 17 00:00:00 2001 From: "arnaud.bore" Date: Thu, 8 Jun 2023 13:00:34 -0400 Subject: [PATCH 3/6] fix import --- dcm2bids/dcm2bids_gen.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/dcm2bids/dcm2bids_gen.py b/dcm2bids/dcm2bids_gen.py index daaeea3a..9ac7fb40 100644 --- a/dcm2bids/dcm2bids_gen.py +++ b/dcm2bids/dcm2bids_gen.py @@ -13,8 +13,6 @@ from datetime import datetime from glob import glob -from bids_validator import BIDSValidator - from dcm2bids.dcm2niix_gen import Dcm2niixGen from dcm2bids.utils.logger import setup_logging from dcm2bids.sidecar import Sidecar, SidecarPairing From fd21ac2048ba38eff4e5733b4124349e925cb158 Mon Sep 17 00:00:00 2001 From: "arnaud.bore" Date: Thu, 8 Jun 2023 13:03:13 -0400 Subject: [PATCH 4/6] update Pygments --- requirements-doc.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-doc.txt b/requirements-doc.txt index 2aa78f42..4cf42a10 100644 --- a/requirements-doc.txt +++ b/requirements-doc.txt @@ -45,7 +45,7 @@ prompt-toolkit==3.0.30 ptyprocess==0.7.0 pure-eval==0.2.2 py==1.11.0 -Pygments==2.11.2 +Pygments==2.12.0 pymdown-extensions==10.0 pyparsing==3.0.8 python-dateutil==2.8.2 From 88381152906e8cf379973a5baa84b0d1f668101a Mon Sep 17 00:00:00 2001 From: "arnaud.bore" Date: Thu, 8 Jun 2023 13:20:21 -0400 Subject: [PATCH 5/6] update documentation --- docs/get-started/install.md | 4 +++- docs/tutorial/first-steps.md | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/get-started/install.md b/docs/get-started/install.md index 0043f170..e7162c0c 100644 --- a/docs/get-started/install.md +++ b/docs/get-started/install.md @@ -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 @@ -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} diff --git a/docs/tutorial/first-steps.md b/docs/tutorial/first-steps.md index a913258c..31295505 100644 --- a/docs/tutorial/first-steps.md +++ b/docs/tutorial/first-steps.md @@ -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 @@ -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} @@ -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 @@ -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} From 76381e4d6952877306aa98390bbf336336094427 Mon Sep 17 00:00:00 2001 From: arnaudbore Date: Fri, 9 Jun 2023 15:49:55 -0400 Subject: [PATCH 6/6] Update dcm2bids/dcm2bids_gen.py Co-authored-by: Samuel Guay --- dcm2bids/dcm2bids_gen.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dcm2bids/dcm2bids_gen.py b/dcm2bids/dcm2bids_gen.py index 9ac7fb40..94b97951 100644 --- a/dcm2bids/dcm2bids_gen.py +++ b/dcm2bids/dcm2bids_gen.py @@ -145,9 +145,9 @@ def run(self): run_shell_command(['bids-validator', '-v']) run_shell_command(['bids-validator', self.bidsDir]) except: - self.logger.info("bids-validator does not seem to work properly" - "bids-validator may not been installed on your computer" - f"Please check: https://github.com/bids-standard/bids-validator#quickstart") + 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"""