|
1 | | -from fileformats.core import converter |
| 1 | +import typing as ty |
| 2 | +from fileformats.core import converter, FileSet |
2 | 3 | from fileformats.medimage.base import MedicalImage |
3 | | - |
4 | 4 | from fileformats.medimage_mrtrix3 import ( |
5 | 5 | ImageFormat as MrtrixImage, |
6 | 6 | ImageHeader as MrtrixImageHeader, |
7 | 7 | ImageFormatGz as MrtrixImageGz, |
8 | 8 | ) |
9 | 9 |
|
10 | | -try: |
11 | | - from pydra.tasks.mrtrix3.v3_0 import MrConvert |
12 | | -except ImportError: |
13 | | - from pydra.tasks.mrtrix3.latest import mrconvert as MrConvert |
14 | | - |
15 | | - in_out_file_kwargs = {"in_file": "input", "out_file": "output"} |
16 | | -else: |
17 | | - in_out_file_kwargs = {} |
| 10 | +from pydra.tasks.mrtrix3.v3_0 import MrConvert |
18 | 11 |
|
19 | 12 |
|
20 | | -@converter( |
21 | | - source_format=MedicalImage, |
22 | | - target_format=MrtrixImageGz, |
23 | | - out_ext=MrtrixImageGz.ext, |
24 | | - **in_out_file_kwargs, |
25 | | -) |
26 | | -@converter( |
27 | | - source_format=MedicalImage, |
28 | | - target_format=MrtrixImageHeader, |
29 | | - out_ext=MrtrixImageHeader.ext, |
30 | | - **in_out_file_kwargs, |
31 | | -) |
32 | | -@converter( |
33 | | - source_format=MedicalImage, |
34 | | - target_format=MrtrixImage, |
35 | | - out_ext=MrtrixImage.ext, |
36 | | - **in_out_file_kwargs, |
37 | | -) |
38 | | -def mrconvert(name, out_ext: str, **kwargs): |
39 | | - """Initiate an MRConvert task with the output file extension set |
| 13 | +def out_file_template(fileformat: ty.Type[FileSet]) -> str: |
| 14 | + """Return the output file name for a given file format |
40 | 15 |
|
41 | 16 | Parameters |
42 | 17 | ---------- |
43 | | - name : str |
44 | | - name of the converter task |
45 | | - out_ext : str |
46 | | - extension of the output file, used by MRConvert to determine the desired format |
| 18 | + fileformat : type |
| 19 | + the file format class |
47 | 20 |
|
48 | 21 | Returns |
49 | 22 | ------- |
50 | | - pydra.ShellCommandTask |
51 | | - the converter task |
| 23 | + str |
| 24 | + the output file name |
52 | 25 | """ |
53 | | - return MrConvert(name=name, out_file="out" + out_ext, **kwargs) |
| 26 | + return "out" + fileformat.ext |
54 | 27 |
|
55 | 28 |
|
56 | | -# @converter( |
57 | | -# source_format=MedicalImage, |
58 | | -# target_format=MrtrixImageHeader, |
59 | | -# out_ext=MrtrixImageHeader.ext, |
60 | | -# **in_out_file_kwargs, |
61 | | -# ) |
62 | | -# def mrconvert2(name, out_ext: str, **kwargs): |
63 | | -# """Initiate an MRConvert task with the output file extension set |
| 29 | +# Register MrConvert as a converter for MrTrix formats |
64 | 30 |
|
65 | | -# Parameters |
66 | | -# ---------- |
67 | | -# name : str |
68 | | -# name of the converter task |
69 | | -# out_ext : str |
70 | | -# extension of the output file, used by MRConvert to determine the desired format |
| 31 | +converter( |
| 32 | + source_format=MedicalImage, |
| 33 | + target_format=MrtrixImageGz, |
| 34 | + out_file=out_file_template(MrtrixImageGz), |
| 35 | +)(MrConvert) |
71 | 36 |
|
72 | | -# Returns |
73 | | -# ------- |
74 | | -# pydra.ShellCommandTask |
75 | | -# the converter task |
76 | | -# """ |
77 | | -# return MrConvert(name=name, out_file="out" + out_ext, **kwargs) |
| 37 | +converter( |
| 38 | + source_format=MedicalImage, |
| 39 | + target_format=MrtrixImageHeader, |
| 40 | + out_file=out_file_template(MrtrixImageHeader), |
| 41 | +)(MrConvert) |
| 42 | + |
| 43 | +converter( |
| 44 | + source_format=MedicalImage, |
| 45 | + target_format=MrtrixImage, |
| 46 | + out_file=out_file_template(MrtrixImage), |
| 47 | +)(MrConvert) |
0 commit comments