Skip to content

Added ApplyXFM interface #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Below is a list of all planned interfaces, with completed interfaces checked. Th
### Preprocess

- [x] ApplyWarp (`applywarp`)
- [ ] ApplyXFM (`flirt`)
- [X] ApplyXFM (`flirt`)
- [x] BET (`bet`)
- [x] FAST (`fast`)
- [x] FIRST (`first`)
Expand Down
75 changes: 75 additions & 0 deletions pydra/tasks/fsl/preprocess/applyxfm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from pydra.engine import specs
from .flirt import FLIRT

input_fields = [
(
"in_file",
specs.File,
{
"help_string": "input file",
"argstr": "-in {in_file}",
"mandatory": True,
"position": 0,
},
),
(
"reference",
specs.File,
{
"help_string": "reference file",
"argstr": "-ref {reference}",
"mandatory": True,
"position": 1,
},
),
(
"apply_xfm",
bool,
True,
{
"help_string": "apply transformation supplied by in_matrix_file or uses_qform to use the affine matrix stored in the reference header",
"argstr": "-applyxfm",
},
),
(
"in_matrix_file",
str,
{
"help_string": "input 4x4 affine matrix",
"argstr": "-init {in_matrix_file}",
"mandatory": True,
},
),
(
"out_file",
str,
{
"help_string": "registered output file",
"argstr": "-out {out_file}",
"position": 2,
"output_file_template": "{in_file}_flirt",
},
),
]

ApplyXFM_input_spec = specs.SpecInfo(name="Input", fields=input_fields, bases=(specs.ShellSpec,))


class ApplyXFM(FLIRT):
"""
Example
-------
>>> task = ApplyXFM()
>>> task.inputs.in_file = 'test.nii.gz'
>>> task.inputs.in_matrix_file = 'transform.mat'
>>> task.inputs.reference = 'dest.nii.gz'
>>> task.cmdline # doctest: +ELLIPSIS
'flirt -in test.nii.gz -ref dest.nii.gz -out .../test_flirt.nii.gz -applyxfm -init transform.mat'

# using a custom outfile
>>> task.inputs.out_file = 'custom_outfile.nii.gz'
>>> task.cmdline
'flirt -in test.nii.gz -ref dest.nii.gz -out custom_outfile.nii.gz -applyxfm -init transform.mat'
"""

input_spec = ApplyXFM_input_spec
38 changes: 38 additions & 0 deletions pydra/tasks/fsl/preprocess/tests/test_run_applyxfm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os, pytest
from pathlib import Path
from ..applyxfm import ApplyXFM


@pytest.mark.xfail("FSLDIR" not in os.environ, reason="no FSL found", raises=FileNotFoundError)
@pytest.mark.parametrize("inputs, outputs", [(None, ["out_file"])])
def test_ApplyXFM(test_data, inputs, outputs):
in_file = Path(test_data) / "test.nii.gz"
reference = Path(test_data) / "test.nii.gz"
in_matrix_file = Path(test_data) / "transform.mat"
if inputs is None:
inputs = {}
for key, val in inputs.items():
try:
inputs[key] = eval(val)
except:
pass
task = ApplyXFM(in_file=in_file, reference=reference, in_matrix_file=in_matrix_file, **inputs)
assert set(task.generated_output_names) == set(["return_code", "stdout", "stderr"] + outputs)
res = task()
print("RESULT: ", res)
for out_nm in outputs:
assert getattr(res.output, out_nm).exists()


@pytest.mark.parametrize("inputs, error", [(None, "AttributeError")])
def test_ApplyXFM_exception(test_data, inputs, error):
if inputs is None:
inputs = {}
for key, val in inputs.items():
try:
inputs[key] = eval(val)
except:
pass
task = ApplyXFM(**inputs)
with pytest.raises(eval(error)):
task.generated_output_names
Binary file added pydra/tasks/fsl/tests/data/magnitude.nii
Binary file not shown.
Binary file added pydra/tasks/fsl/tests/data/test_warpcoef.nii.gz
Binary file not shown.
4 changes: 4 additions & 0 deletions pydra/tasks/fsl/tests/data/transform.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 0.0001371992403 0 -0.01404274377
0 1 0.0003819660051 -0.04808253666
0 0 1 0
0 0 0 1