Skip to content

[ENH] Mrtrix3 enhancements #3426

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

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ec35b55
added kwarg json_import to mrconvert interface
GalKepler Oct 30, 2021
412f7af
added kwarg json_export to mrconvert interface
GalKepler Oct 30, 2021
b5246a8
refractoring
GalKepler Oct 30, 2021
5979cb8
refractoring w/ black...
GalKepler Oct 30, 2021
aa64ccd
Created an interface for mrtrix3's mrcat function
GalKepler Oct 31, 2021
1b74350
forgot to run make before commit...
GalKepler Oct 31, 2021
a156154
refractored black formatting
GalKepler Oct 31, 2021
87f999c
Merge branch 'mrcat'
GalKepler Oct 31, 2021
480c1f9
fixed a small issue with output listing
GalKepler Oct 31, 2021
97c3a89
Merge branch 'mrconvert'
GalKepler Oct 31, 2021
dbed52b
just added some tensor-derived metrics for the interface of mrtrix3's…
GalKepler Oct 31, 2021
e263d36
a small addition for the mrconvert interface to include bvec/bval exp…
GalKepler Nov 7, 2021
9311658
a small addition for the mrconvert interface to include bvec/bval exp…
GalKepler Nov 7, 2021
8462a45
updated some small changes
GalKepler Nov 26, 2021
c42ac75
added interface for mrtrix3's transformconvert and mrtransform
GalKepler Nov 30, 2021
7a77fdd
added interface for mrtrix3's transformconvert and mrtransform
GalKepler Nov 30, 2021
a79c5d4
Merge branch 'master' of https://github.com/nipy/nipype
GalKepler Dec 21, 2021
3334c58
Fixed dwipreproc default value in tests.
ZviBaratz Dec 22, 2021
a6472dc
Fixed TransformFSLConvert import path in example docs.
ZviBaratz Dec 22, 2021
1413f30
Tried changing file names to mif files in other doctests.
ZviBaratz Dec 22, 2021
9414af8
Reverted DWIPreprocInputSpec.out_grad_mrtrix default value change.
ZviBaratz Dec 22, 2021
ed6b685
Reverted test value change.
ZviBaratz Dec 22, 2021
054e0b1
Removed TransformFSLConvert example to avoid doctest failure due to m…
ZviBaratz Dec 22, 2021
947bef2
Tried manually generating the missing automatic test modules.
ZviBaratz Dec 23, 2021
3782d28
Re-ran formatting.
ZviBaratz Dec 23, 2021
fd470a5
Reinstalled dev env requirements and triggered a commit to try and fi…
ZviBaratz Dec 23, 2021
8375356
Merge branch 'master' into mrtrix
GalKepler Apr 4, 2022
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
21 changes: 13 additions & 8 deletions doc/devel/matlab_example1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from nipype.interfaces.matlab import MatlabCommand
from nipype.interfaces.base import TraitedSpec, \
BaseInterface, BaseInterfaceInputSpec, File
from nipype.interfaces.base import (
TraitedSpec,
BaseInterface,
BaseInterfaceInputSpec,
File,
)
import os
from string import Template


class ConmapTxt2MatInputSpec(BaseInterfaceInputSpec):
in_file = File(exists=True, mandatory=True)
out_file = File('cmatrix.mat', usedefault=True)
out_file = File("cmatrix.mat", usedefault=True)


class ConmapTxt2MatOutputSpec(TraitedSpec):
Expand All @@ -19,14 +23,15 @@ class ConmapTxt2Mat(BaseInterface):
output_spec = ConmapTxt2MatOutputSpec

def _run_interface(self, runtime):
d = dict(in_file=self.inputs.in_file,
out_file=self.inputs.out_file)
d = dict(in_file=self.inputs.in_file, out_file=self.inputs.out_file)
# This is your MATLAB code template
script = Template("""in_file = '$in_file';
script = Template(
"""in_file = '$in_file';
out_file = '$out_file';
ConmapTxt2Mat(in_file, out_file);
exit;
""").substitute(d)
"""
).substitute(d)

# mfile = True will create an .m file with your script and executed.
# Alternatively
Expand All @@ -43,5 +48,5 @@ def _run_interface(self, runtime):

def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
outputs["out_file"] = os.path.abspath(self.inputs.out_file)
return outputs
8 changes: 5 additions & 3 deletions doc/devel/matlab_example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


class HelloWorldInputSpec(MatlabInputSpec):
name = traits.Str(mandatory=True,
desc='Name of person to say hello to')
name = traits.Str(mandatory=True, desc="Name of person to say hello to")


class HelloWorldOutputSpec(TraitedSpec):
Expand All @@ -29,6 +28,7 @@ class HelloWorld(MatlabCommand):
>>> out = hello.run()
>>> print out.outputs.matlab_output
"""

input_spec = HelloWorldInputSpec
output_spec = HelloWorldOutputSpec

Expand All @@ -37,7 +37,9 @@ def _my_script(self):
script = """
disp('Hello %s Python')
two = 1 + 1
""" % (self.inputs.name)
""" % (
self.inputs.name
)
return script

def run(self, **inputs):
Expand Down
7 changes: 5 additions & 2 deletions nipype/interfaces/mrtrix/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def track_gen(track_points):
"Expecting %s points, found only %s" % (stream_count, n_streams)
)
iflogger.error(
"Expecting %s points, found only %s", stream_count, n_streams
"Expecting %s points, found only %s",
stream_count,
n_streams,
)
break
pts = np.ndarray(shape=(n_pts, pt_cols), dtype=f4dt, buffer=pts_str)
Expand Down Expand Up @@ -225,7 +227,8 @@ def _run_interface(self, runtime):
self.inputs.registration_image_file
):
iflogger.info(
"Applying transformation from matrix file %s", self.inputs.matrix_file
"Applying transformation from matrix file %s",
self.inputs.matrix_file,
)
xfm = np.genfromtxt(self.inputs.matrix_file)
iflogger.info(xfm)
Expand Down
Binary file removed nipype/interfaces/mrtrix/defhdr.mat
Binary file not shown.
15 changes: 12 additions & 3 deletions nipype/interfaces/mrtrix/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,11 @@ class GenerateWhiteMatterMaskInputSpec(CommandLineInputSpec):
desc="Diffusion-weighted images",
)
binary_mask = File(
exists=True, argstr="%s", mandatory=True, position=-2, desc="Binary brain mask"
exists=True,
argstr="%s",
mandatory=True,
position=-2,
desc="Binary brain mask",
)
out_WMProb_filename = File(
genfile=True,
Expand Down Expand Up @@ -669,7 +673,9 @@ class ErodeInputSpec(CommandLineInputSpec):
argstr="-npass %s", desc="the number of passes (default: 1)"
)
dilate = traits.Bool(
argstr="-dilate", position=1, desc="Perform dilation rather than erosion"
argstr="-dilate",
position=1,
desc="Perform dilation rather than erosion",
)
quiet = traits.Bool(
argstr="-quiet",
Expand Down Expand Up @@ -729,7 +735,10 @@ class ThresholdInputSpec(CommandLineInputSpec):
desc="The input image to be thresholded",
)
out_filename = File(
genfile=True, argstr="%s", position=-1, desc="The output binary image mask."
genfile=True,
argstr="%s",
position=-1,
desc="The output binary image mask.",
)
absolute_threshold_value = traits.Float(
argstr="-abs %s", desc="Specify threshold value as absolute intensity."
Expand Down
41 changes: 22 additions & 19 deletions nipype/interfaces/mrtrix3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
# -*- coding: utf-8 -*-
"""MRTrix3 provides software tools to perform various types of diffusion MRI analyses."""
from .connectivity import BuildConnectome, LabelConfig, LabelConvert
from .preprocess import (
ACTPrepareFSL,
DWIBiasCorrect,
DWIDenoise,
DWIPreproc,
MRDeGibbs,
ReplaceFSwithFIRST,
ResponseSD,
)
from .reconst import ConstrainedSphericalDeconvolution, EstimateFOD, FitTensor
from .tracking import Tractography
from .utils import (
Mesh2PVE,
Generate5tt,
TCK2VTK,
BrainMask,
TensorMetrics,
ComputeTDI,
TCK2VTK,
MRMath,
DWIExtract,
Generate5tt,
Mesh2PVE,
MRCat,
MRConvert,
MRMath,
MRResize,
DWIExtract,
SHConv,
MRTransform,
SH2Amp,
SHConv,
TensorMetrics,
TransformFSLConvert,
)
from .preprocess import (
ResponseSD,
ACTPrepareFSL,
ReplaceFSwithFIRST,
DWIPreproc,
DWIDenoise,
MRDeGibbs,
DWIBiasCorrect,
)
from .tracking import Tractography
from .reconst import FitTensor, EstimateFOD, ConstrainedSphericalDeconvolution
from .connectivity import LabelConfig, LabelConvert, BuildConnectome
19 changes: 15 additions & 4 deletions nipype/interfaces/mrtrix3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
# -*- coding: utf-8 -*-

from ... import logging, LooseVersion
from ... import LooseVersion, logging
from ...utils.filemanip import which
from ..base import (
CommandLineInputSpec,
CommandLine,
traits,
CommandLineInputSpec,
File,
isdefined,
PackageInfo,
isdefined,
traits,
)

iflogger = logging.getLogger("nipype.interface")
Expand Down Expand Up @@ -79,6 +79,15 @@ class MRTrix3BaseInputSpec(CommandLineInputSpec):
exists=True, argstr="-fslgrad %s %s", desc="bvecs file in FSL format"
)
in_bval = File(exists=True, desc="bvals file in FSL format")
out_bvec = File(
exists=False,
argstr="-export_grad_fsl %s %s",
desc="export bvec file in FSL format",
)
out_bval = File(
exists=False,
desc="export bval file in FSL format",
)


class MRTrix3Base(CommandLine):
Expand All @@ -96,6 +105,8 @@ def _format_arg(self, name, trait_spec, value):

if name == "in_bvec":
return trait_spec.argstr % (value, self.inputs.in_bval)
if name == "out_bvec":
return trait_spec.argstr % (value, self.inputs.out_bval)

return super(MRTrix3Base, self)._format_arg(name, trait_spec, value)

Expand Down
44 changes: 36 additions & 8 deletions nipype/interfaces/mrtrix3/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

class DWIDenoiseInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", position=-2, mandatory=True, desc="input DWI image"
exists=True,
argstr="%s",
position=-2,
mandatory=True,
desc="input DWI image",
)
mask = File(exists=True, argstr="-mask %s", position=1, desc="mask image")
extent = traits.Tuple(
Expand Down Expand Up @@ -88,7 +92,11 @@ class DWIDenoise(MRTrix3Base):

class MRDeGibbsInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", position=-2, mandatory=True, desc="input DWI image"
exists=True,
argstr="%s",
position=-2,
mandatory=True,
desc="input DWI image",
)
axes = traits.ListInt(
default_value=[0, 1],
Expand Down Expand Up @@ -177,7 +185,11 @@ class MRDeGibbs(MRTrix3Base):

class DWIBiasCorrectInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", position=-2, mandatory=True, desc="input DWI image"
exists=True,
argstr="%s",
position=-2,
mandatory=True,
desc="input DWI image",
)
in_mask = File(argstr="-mask %s", desc="input mask image for bias field estimation")
use_ants = traits.Bool(
Expand Down Expand Up @@ -245,7 +257,11 @@ def _format_arg(self, name, trait_spec, value):

class DWIPreprocInputSpec(MRTrix3BaseInputSpec):
in_file = File(
exists=True, argstr="%s", position=0, mandatory=True, desc="input DWI image"
exists=True,
argstr="%s",
position=0,
mandatory=True,
desc="input DWI image",
)
out_file = File(
"preproc.mif",
Expand Down Expand Up @@ -292,7 +308,8 @@ class DWIPreprocInputSpec(MRTrix3BaseInputSpec):
desc="Manually provide additional command-line options to the topup command",
)
export_grad_mrtrix = traits.Bool(
argstr="-export_grad_mrtrix", desc="export new gradient files in mrtrix format"
argstr="-export_grad_mrtrix",
desc="export new gradient files in mrtrix format",
)
export_grad_fsl = traits.Bool(
argstr="-export_grad_fsl", desc="export gradient files in FSL format"
Expand Down Expand Up @@ -387,7 +404,11 @@ class ResponseSDInputSpec(MRTrix3BaseInputSpec):
desc="response estimation algorithm (multi-tissue)",
)
in_file = File(
exists=True, argstr="%s", position=-5, mandatory=True, desc="input DWI image"
exists=True,
argstr="%s",
position=-5,
mandatory=True,
desc="input DWI image",
)
mtt_file = File(argstr="%s", position=-4, desc="input 5tt image")
wm_file = File(
Expand Down Expand Up @@ -511,10 +532,17 @@ class ReplaceFSwithFIRSTInputSpec(CommandLineInputSpec):
desc="input anatomical image",
)
in_t1w = File(
exists=True, argstr="%s", mandatory=True, position=-3, desc="input T1 image"
exists=True,
argstr="%s",
mandatory=True,
position=-3,
desc="input T1 image",
)
in_config = File(
exists=True, argstr="%s", position=-2, desc="connectome configuration file"
exists=True,
argstr="%s",
position=-2,
desc="connectome configuration file",
)

out_file = File(
Expand Down
73 changes: 73 additions & 0 deletions nipype/interfaces/mrtrix3/tests/test_auto_MRCat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
from ..utils import MRCat


def test_MRCat_inputs():
input_map = dict(
args=dict(
argstr="%s",
),
axis=dict(
argstr="-axis %s",
),
bval_scale=dict(
argstr="-bvalue_scaling %s",
),
datatype=dict(
argstr="-datatype %s",
),
environ=dict(
nohash=True,
usedefault=True,
),
grad_file=dict(
argstr="-grad %s",
extensions=None,
xor=["grad_fsl"],
),
grad_fsl=dict(
argstr="-fslgrad %s %s",
xor=["grad_file"],
),
in_bval=dict(
extensions=None,
),
in_bvec=dict(
argstr="-fslgrad %s %s",
extensions=None,
),
in_files=dict(
argstr="%s",
mandatory=True,
position=-2,
),
nthreads=dict(
argstr="-nthreads %d",
nohash=True,
),
out_file=dict(
argstr="%s",
extensions=None,
mandatory=True,
position=-1,
usedefault=True,
),
)
inputs = MRCat.input_spec()

for key, metadata in list(input_map.items()):
for metakey, value in list(metadata.items()):
assert getattr(inputs.traits()[key], metakey) == value


def test_MRCat_outputs():
output_map = dict(
out_file=dict(
extensions=None,
),
)
outputs = MRCat.output_spec()

for key, metadata in list(output_map.items()):
for metakey, value in list(metadata.items()):
assert getattr(outputs.traits()[key], metakey) == value
Loading