Skip to content

Commit

Permalink
Merge pull request #262 from UNFmontreal/enh_input_archive
Browse files Browse the repository at this point in the history
[ENH] Add possibility to input dicom tar or zip archives
arnaudbore authored Sep 12, 2023
2 parents bc77eae + 942ae78 commit 9496b7a
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions dcm2bids/cli/dcm2bids.py
Original file line number Diff line number Diff line change
@@ -26,7 +26,8 @@ def _build_arg_parser():

p.add_argument("-d", "--dicom_dir",
required=True, nargs="+",
help="DICOM directory(ies).")
help="DICOM directory(ies) or archive(s) (" +
DEFAULT.arch_extensions + ").")

p.add_argument("-p", "--participant",
required=True,
@@ -62,7 +63,7 @@ def _build_arg_parser():
p.add_argument("--force_dcm2bids",
action="store_true",
help="Overwrite previous temporary dcm2bids "
"output if it exists.")
"output if it exists.")

p.add_argument("--skip_dcm2niix",
action="store_true",
3 changes: 2 additions & 1 deletion dcm2bids/cli/dcm2bids_helper.py
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ def _build_arg_parser():

p.add_argument("-d", "--dicom_dir",
required=True, nargs="+",
help="DICOM files directory.")
help="DICOM directory(ies) or archive(s) (" +
DEFAULT.arch_extensions + ").")

p.add_argument("-o", "--output_dir",
required=False,
33 changes: 33 additions & 0 deletions dcm2bids/dcm2niix_gen.py
Original file line number Diff line number Diff line change
@@ -6,8 +6,11 @@
import os
import shlex
import shutil
import tarfile
import zipfile
from glob import glob

from dcm2bids.utils.io import valid_path
from dcm2bids.utils.utils import DEFAULT, run_shell_command


@@ -42,6 +45,7 @@ def __init__(
self.skip_dcm2niix = skip_dcm2niix
self.options = options
self.helper = helper
self.rm_tmp_dir = False

@property
def output_dir(self):
@@ -101,17 +105,46 @@ def execute(self):
"""
if not self.skip_dcm2niix:
for dicomDir in self.dicom_dirs:
if os.path.isfile(dicomDir):
tmp_dcm_name = os.path.join(self.output_dir.parent,
self.output_dir.name + '_tmp')
self.rm_tmp_dir = valid_path(tmp_dcm_name, type="folder")

if tarfile.is_tarfile(dicomDir):
self.logger.info(f"Extracting archive {dicomDir} to temporary "
f"dicom directory {self.rm_tmp_dir}.")
with tarfile.open(dicomDir) as archive:
archive.extractall(self.rm_tmp_dir)

elif zipfile.is_zipfile(dicomDir):
self.logger.info(f"Extracting archive {dicomDir} to temporary "
f"dicom directory {self.rm_tmp_dir}.")
with zipfile.ZipFile(dicomDir, 'r') as zip_ref:
zip_ref.extractall(self.rm_tmp_dir)

else:
self.logger.error(f"\n{dicomDir} is not a supported file" +
" extension." +
DEFAULT.arch_extensions + " are supported.")
dicomDir = self.rm_tmp_dir

cmd = ['dcm2niix', *shlex.split(self.options),
'-o', self.output_dir, dicomDir]

output = run_shell_command(cmd)

try:
output = output.decode()
except Exception:
pass

if self.rm_tmp_dir:
shutil.rmtree(self.rm_tmp_dir)
self.logger.info("Temporary dicom directory removed.")

self.logger.debug(f"\n{output}")
self.logger.info("Check log file for dcm2niix output\n")

else:
for dicomDir in self.dicom_dirs:
shutil.copytree(dicomDir, self.output_dir, dirs_exist_ok=True)
3 changes: 3 additions & 0 deletions dcm2bids/utils/utils.py
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@ class DEFAULT(object):
cli_session = ""
cli_log_level = "INFO"

# Archives
arch_extensions = "tar, tar.bz2, tar.gz or zip"

# dcm2bids.py
output_dir = Path.cwd()
session = "" # also Participant object

0 comments on commit 9496b7a

Please sign in to comment.