Skip to content

Commit

Permalink
Merge pull request #6 from tclose/gzip-and-echo-suffixes
Browse files Browse the repository at this point in the history
Properly handle gzip extensions and multi-echo
  • Loading branch information
tclose authored Jul 27, 2022
2 parents 4ca4756 + 3ee3557 commit dcab68b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ dmypy.json

# Pycharm
.idea


# VSCode
.vscode
14 changes: 14 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
import pytest

# For debugging in IDE's don't catch raised exceptions and let the IDE
# break at it
if os.getenv("_PYTEST_RAISE", "0") != "0":

@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
raise call.excinfo.value

@pytest.hookimpl(tryfirst=True)
def pytest_internalerror(excinfo):
raise excinfo.value
11 changes: 11 additions & 0 deletions pydra/tasks/dcm2niix/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydra.tasks.dcm2niix.utils import Dcm2Niix


def test_dcm2niix():
task = Dcm2Niix()
task.inputs.in_dir = "test-data/test_dicoms"
task.inputs.out_dir = "test-data"
task.inputs.compress = "y"
assert (
task.cmdline == "dcm2niix -o test-data -f out_file -z y test-data/test_dicoms"
)
52 changes: 46 additions & 6 deletions pydra/tasks/dcm2niix/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import typing as ty
import os.path
from pydra import ShellCommandTask
from pydra.engine.specs import ShellSpec, ShellOutSpec, File, Directory, SpecInfo


def dcm2niix_out_file(out_dir, filename, echo, compress):
# Append echo number of NIfTI echo to select is provided
if echo:
echo_suffix = f"_e{echo}"
else:
echo_suffix = ""

out_file = f"{out_dir}/{filename}{echo_suffix}.nii"

# If compressed, append the zip extension
if compress in ("y", "o", "i"):
out_file += ".gz"

return os.path.abspath(out_file)


def dcm2niix_out_json(out_dir, filename, echo):
# Append echo number of NIfTI echo to select is provided
if echo:
echo_suffix = f"_e{echo}"
else:
echo_suffix = ""

return os.path.abspath(f"{out_dir}/{filename}{echo_suffix}.json")


input_fields = [
(
"in_dir",
Expand All @@ -16,7 +42,7 @@
),
(
"out_dir",
str,
Directory,
{
"argstr": "-o '{out_dir}'",
"help_string": "output directory",
Expand All @@ -29,6 +55,18 @@
"out_file",
{"argstr": "-f '{filename}'", "help_string": "The output name for the file"},
),
(
"echo",
int,
{
"argstr": "",
"help_string": (
"The echo number to extract from the DICOM dataset. When multiple "
"echoes are discovered in the dataset then dcm2niix will create "
"separate files for each echo with the suffix '_e<echo-number>.nii'"
),
},
),
(
"compress",
str,
Expand Down Expand Up @@ -279,15 +317,17 @@
File,
{
"help_string": "output NIfTI image",
"output_file_template": "{out_dir}/{filename}.nii.gz",
"callable": dcm2niix_out_file,
"mandatory": True,
},
),
(
"out_json",
File,
{
"help_string": "output BIDS side-car JSON",
"output_file_template": "{out_dir}/{filename}.json",
# "requires": [("bids", 'y')], FIXME: should be either 'y' or 'o'
"callable": dcm2niix_out_json,
},
),
(
Expand Down Expand Up @@ -319,10 +359,10 @@ class Dcm2Niix(ShellCommandTask):
-------
>>> task = Dcm2Niix()
>>> task.inputs.in_dir = "test-data/test_dicoms"
>>> task.inputs.out_dir = "test-data/output"
>>> task.inputs.out_dir = "test-data"
>>> task.inputs.compress = "y"
>>> task.cmdline
"dcm2niix -o 'test-data/output' -f 'out_file' -z y 'test-data/test_dicoms'"
'dcm2niix -o test-data -f out_file -z y test-data/test_dicoms'
"""

input_spec = Dcm2NiixInputSpec
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers =
[options]
python_requires = >=3.7
install_requires =
pydra >= 0.6.2
pydra >= 0.19
packages = find_namespace:

[options.packages.find]
Expand Down

0 comments on commit dcab68b

Please sign in to comment.