Skip to content

Commit dcab68b

Browse files
authored
Merge pull request #6 from tclose/gzip-and-echo-suffixes
Properly handle gzip extensions and multi-echo
2 parents 4ca4756 + 3ee3557 commit dcab68b

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,7 @@ dmypy.json
130130

131131
# Pycharm
132132
.idea
133+
134+
135+
# VSCode
136+
.vscode

conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import pytest
3+
4+
# For debugging in IDE's don't catch raised exceptions and let the IDE
5+
# break at it
6+
if os.getenv("_PYTEST_RAISE", "0") != "0":
7+
8+
@pytest.hookimpl(tryfirst=True)
9+
def pytest_exception_interact(call):
10+
raise call.excinfo.value
11+
12+
@pytest.hookimpl(tryfirst=True)
13+
def pytest_internalerror(excinfo):
14+
raise excinfo.value
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pydra.tasks.dcm2niix.utils import Dcm2Niix
2+
3+
4+
def test_dcm2niix():
5+
task = Dcm2Niix()
6+
task.inputs.in_dir = "test-data/test_dicoms"
7+
task.inputs.out_dir = "test-data"
8+
task.inputs.compress = "y"
9+
assert (
10+
task.cmdline == "dcm2niix -o test-data -f out_file -z y test-data/test_dicoms"
11+
)

pydra/tasks/dcm2niix/utils.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
import typing as ty
1+
import os.path
22
from pydra import ShellCommandTask
33
from pydra.engine.specs import ShellSpec, ShellOutSpec, File, Directory, SpecInfo
44

55

6+
def dcm2niix_out_file(out_dir, filename, echo, compress):
7+
# Append echo number of NIfTI echo to select is provided
8+
if echo:
9+
echo_suffix = f"_e{echo}"
10+
else:
11+
echo_suffix = ""
12+
13+
out_file = f"{out_dir}/{filename}{echo_suffix}.nii"
14+
15+
# If compressed, append the zip extension
16+
if compress in ("y", "o", "i"):
17+
out_file += ".gz"
18+
19+
return os.path.abspath(out_file)
20+
21+
22+
def dcm2niix_out_json(out_dir, filename, echo):
23+
# Append echo number of NIfTI echo to select is provided
24+
if echo:
25+
echo_suffix = f"_e{echo}"
26+
else:
27+
echo_suffix = ""
28+
29+
return os.path.abspath(f"{out_dir}/{filename}{echo_suffix}.json")
30+
31+
632
input_fields = [
733
(
834
"in_dir",
@@ -16,7 +42,7 @@
1642
),
1743
(
1844
"out_dir",
19-
str,
45+
Directory,
2046
{
2147
"argstr": "-o '{out_dir}'",
2248
"help_string": "output directory",
@@ -29,6 +55,18 @@
2955
"out_file",
3056
{"argstr": "-f '{filename}'", "help_string": "The output name for the file"},
3157
),
58+
(
59+
"echo",
60+
int,
61+
{
62+
"argstr": "",
63+
"help_string": (
64+
"The echo number to extract from the DICOM dataset. When multiple "
65+
"echoes are discovered in the dataset then dcm2niix will create "
66+
"separate files for each echo with the suffix '_e<echo-number>.nii'"
67+
),
68+
},
69+
),
3270
(
3371
"compress",
3472
str,
@@ -279,15 +317,17 @@
279317
File,
280318
{
281319
"help_string": "output NIfTI image",
282-
"output_file_template": "{out_dir}/{filename}.nii.gz",
320+
"callable": dcm2niix_out_file,
321+
"mandatory": True,
283322
},
284323
),
285324
(
286325
"out_json",
287326
File,
288327
{
289328
"help_string": "output BIDS side-car JSON",
290-
"output_file_template": "{out_dir}/{filename}.json",
329+
# "requires": [("bids", 'y')], FIXME: should be either 'y' or 'o'
330+
"callable": dcm2niix_out_json,
291331
},
292332
),
293333
(
@@ -319,10 +359,10 @@ class Dcm2Niix(ShellCommandTask):
319359
-------
320360
>>> task = Dcm2Niix()
321361
>>> task.inputs.in_dir = "test-data/test_dicoms"
322-
>>> task.inputs.out_dir = "test-data/output"
362+
>>> task.inputs.out_dir = "test-data"
323363
>>> task.inputs.compress = "y"
324364
>>> task.cmdline
325-
"dcm2niix -o 'test-data/output' -f 'out_file' -z y 'test-data/test_dicoms'"
365+
'dcm2niix -o test-data -f out_file -z y test-data/test_dicoms'
326366
"""
327367

328368
input_spec = Dcm2NiixInputSpec

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers =
2323
[options]
2424
python_requires = >=3.7
2525
install_requires =
26-
pydra >= 0.6.2
26+
pydra >= 0.19
2727
packages = find_namespace:
2828

2929
[options.packages.find]

0 commit comments

Comments
 (0)