From 882e055612b07e359f553796bbe90b29495d9e37 Mon Sep 17 00:00:00 2001 From: Samuel Guay Date: Fri, 22 Apr 2022 14:10:13 -0400 Subject: [PATCH] Change argparse default + improve suffix handling --- dcm2bids/dcm2bids.py | 18 +++++++++--------- dcm2bids/utils.py | 16 +++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/dcm2bids/dcm2bids.py b/dcm2bids/dcm2bids.py index b2a174bc..ea305c2b 100644 --- a/dcm2bids/dcm2bids.py +++ b/dcm2bids/dcm2bids.py @@ -134,8 +134,8 @@ def move(self, acquisition, intendedForList): """Move an acquisition to BIDS format""" for srcFile in glob(acquisition.srcRoot + ".*"): - _, ext = splitext_(srcFile) - dstFile = os.path.join(self.bidsDir, acquisition.dstRoot + ext) + ext = Path(srcFile).suffixes + dstFile = (self.bidsDir / acquisition.dstRoot).with_suffix("".join(ext)) dstFile.parent.mkdir(parents = True, exist_ok = True) @@ -153,9 +153,9 @@ def move(self, acquisition, intendedForList): # it's an anat nifti file and the user using a deface script if ( self.config.get("defaceTpl") - and acquisition.dataType == "anat" + and acquisition.dataType == "func" and ".nii" in ext - ): + ): try: os.remove(dstFile) except FileNotFoundError: @@ -166,9 +166,9 @@ def move(self, acquisition, intendedForList): cmd = [w.replace('dstFile', dstFile) for w in defaceTpl] run_shell_command(cmd) - intendedForList[acquisition.indexSidecar].append(acquisition.dstIntendedFor + ext) + intendedForList[acquisition.indexSidecar].append(acquisition.dstIntendedFor + "".join(ext)) - elif ext == ".json": + elif ".json" in ext: data = acquisition.dstSidecarData(self.config["descriptions"], intendedForList) save_json(dstFile, data) @@ -199,7 +199,7 @@ def _build_arg_parser(): p.add_argument("-s", "--session", required=False, - default=DEFAULT.cliSession, + default="", help="Session ID.") p.add_argument("-c", "--config", @@ -210,8 +210,8 @@ def _build_arg_parser(): p.add_argument("-o", "--output_dir", required=False, type=Path, - default=DEFAULT.cliOutputDir, - help="Output BIDS directory, [%(default)s]") + default=Path.cwd(), + help="Output BIDS directory. (Default: %(default)s)") p.add_argument("--forceDcm2niix", action="store_true", diff --git a/dcm2bids/utils.py b/dcm2bids/utils.py index 4ee02fa1..d487157f 100644 --- a/dcm2bids/utils.py +++ b/dcm2bids/utils.py @@ -17,14 +17,12 @@ class DEFAULT(object): """ Default values of the package""" # cli dcm2bids - cliSession = "" - cliOutputDir = os.getcwd() cliLogLevel = "INFO" EPILOG="Documentation at https://github.com/unfmontreal/Dcm2Bids" # dcm2bids.py - outputDir = cliOutputDir - session = cliSession # also Participant object + outputDir = Path.cwd() + session = "" # also Participant object clobber = False forceDcm2niix = False defaceTpl = None @@ -67,7 +65,7 @@ def load_json(filename): def save_json(filename, data): - with open(filename, "w") as f: + with filename.open("w") as f: json.dump(data, f, indent=4) @@ -133,14 +131,14 @@ def valid_path(in_path, type="folder"): if isinstance(in_path, str): in_path = Path(in_path) - if type=='folder': + if type == 'folder': if in_path.is_dir() or in_path.parent.is_dir(): - return str(in_path) + return in_path else: raise NotADirectoryError(in_path) - elif type=="file": + elif type == "file": if in_path.is_file(): - return str(in_path) + return in_path else: raise FileNotFoundError(in_path)