|
1 |
| -import typing as ty |
| 1 | +import os.path |
2 | 2 | from pydra import ShellCommandTask
|
3 | 3 | from pydra.engine.specs import ShellSpec, ShellOutSpec, File, Directory, SpecInfo
|
4 | 4 |
|
5 | 5 |
|
| 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 | + |
6 | 32 | input_fields = [
|
7 | 33 | (
|
8 | 34 | "in_dir",
|
|
16 | 42 | ),
|
17 | 43 | (
|
18 | 44 | "out_dir",
|
19 |
| - str, |
| 45 | + Directory, |
20 | 46 | {
|
21 | 47 | "argstr": "-o '{out_dir}'",
|
22 | 48 | "help_string": "output directory",
|
|
29 | 55 | "out_file",
|
30 | 56 | {"argstr": "-f '{filename}'", "help_string": "The output name for the file"},
|
31 | 57 | ),
|
| 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 | + ), |
32 | 70 | (
|
33 | 71 | "compress",
|
34 | 72 | str,
|
|
279 | 317 | File,
|
280 | 318 | {
|
281 | 319 | "help_string": "output NIfTI image",
|
282 |
| - "output_file_template": "{out_dir}/{filename}.nii.gz", |
| 320 | + "callable": dcm2niix_out_file, |
| 321 | + "mandatory": True, |
283 | 322 | },
|
284 | 323 | ),
|
285 | 324 | (
|
286 | 325 | "out_json",
|
287 | 326 | File,
|
288 | 327 | {
|
289 | 328 | "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, |
291 | 331 | },
|
292 | 332 | ),
|
293 | 333 | (
|
@@ -319,10 +359,10 @@ class Dcm2Niix(ShellCommandTask):
|
319 | 359 | -------
|
320 | 360 | >>> task = Dcm2Niix()
|
321 | 361 | >>> task.inputs.in_dir = "test-data/test_dicoms"
|
322 |
| - >>> task.inputs.out_dir = "test-data/output" |
| 362 | + >>> task.inputs.out_dir = "test-data" |
323 | 363 | >>> task.inputs.compress = "y"
|
324 | 364 | >>> 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' |
326 | 366 | """
|
327 | 367 |
|
328 | 368 | input_spec = Dcm2NiixInputSpec
|
|
0 commit comments