Skip to content

Commit

Permalink
Change argparse default + improve suffix handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SamGuay committed Apr 22, 2022
1 parent f7f4583 commit 882e055
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
18 changes: 9 additions & 9 deletions dcm2bids/dcm2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
16 changes: 7 additions & 9 deletions dcm2bids/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 882e055

Please sign in to comment.