From ec35b55573bad4427772bdcd1c68466a48de1f94 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sat, 30 Oct 2021 18:50:36 +0300 Subject: [PATCH 01/22] added kwarg json_import to mrconvert interface --- nipype/interfaces/mrtrix3/utils.py | 124 +++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 25 deletions(-) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 77c3047f20..b1414274b3 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -66,7 +66,11 @@ def _list_outputs(self): class Mesh2PVEInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-3, desc="input mesh" + exists=True, + argstr="%s", + mandatory=True, + position=-3, + desc="input mesh", ) reference = File( exists=True, @@ -134,9 +138,15 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): desc="tissue segmentation algorithm", ) in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input image", + ) + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") class Generate5ttOutputSpec(TraitedSpec): @@ -173,13 +183,21 @@ def _list_outputs(self): class TensorMetricsInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-1, desc="input DTI image" + exists=True, + argstr="%s", + mandatory=True, + position=-1, + desc="input DTI image", ) out_fa = File(argstr="-fa %s", desc="output FA file") out_adc = File(argstr="-adc %s", desc="output ADC file") - out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") - out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") + out_evec = File( + argstr="-vector %s", desc="output selected eigenvector(s) file" + ) + out_eval = File( + argstr="-value %s", desc="output selected eigenvalue(s) file" + ) component = traits.List( [1], usedefault=True, @@ -194,7 +212,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" " brain mask image" + "only perform computation within the specified binary" + " brain mask image" ), ) modulate = traits.Enum( @@ -246,10 +265,18 @@ def _list_outputs(self): class ComputeTDIInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input tractography" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input tractography", ) out_file = File( - "tdi.mif", argstr="%s", usedefault=True, position=-1, desc="output TDI file" + "tdi.mif", + argstr="%s", + usedefault=True, + position=-1, + desc="output TDI file", ) reference = File( exists=True, @@ -274,7 +301,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " + "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -353,7 +381,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): "(these lengths are then taken into account during TWI calculation)", ) ends_only = traits.Bool( - argstr="-ends_only", desc="only map the streamline" " endpoints to the image" + argstr="-ends_only", + desc="only map the streamline" " endpoints to the image", ) tck_weights = File( @@ -363,7 +392,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -438,10 +468,18 @@ def _list_outputs(self): class TCK2VTKInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input tractography" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input tractography", ) out_file = File( - "tracks.vtk", argstr="%s", usedefault=True, position=-1, desc="output VTK file" + "tracks.vtk", + argstr="%s", + usedefault=True, + position=-1, + desc="output VTK file", ) reference = File( exists=True, @@ -460,7 +498,8 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -498,9 +537,15 @@ def _list_outputs(self): class DWIExtractInputSpec(MRTrix3BaseInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input image", + ) + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -549,7 +594,11 @@ def _list_outputs(self): class MRConvertInputSpec(MRTrix3BaseInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input image", ) out_file = File( "dwi.mif", @@ -566,7 +615,10 @@ class MRConvertInputSpec(MRTrix3BaseInputSpec): desc="extract data at the specified coordinates", ) vox = traits.List( - traits.Float, sep=",", argstr="-vox %s", desc="change the voxel dimensions" + traits.Float, + sep=",", + argstr="-vox %s", + desc="change the voxel dimensions", ) axes = traits.List( traits.Int, @@ -580,6 +632,12 @@ class MRConvertInputSpec(MRTrix3BaseInputSpec): argstr="-scaling %s", desc="specify the data scaling parameter", ) + json_import = File( + exists=True, + argstr="-json_import %s", + mandatory=False, + desc="import data from a JSON file into header key-value pairs", + ) class MRConvertOutputSpec(TraitedSpec): @@ -615,9 +673,15 @@ def _list_outputs(self): class MRMathInputSpec(MRTrix3BaseInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-3, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-3, + desc="input image", + ) + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") operation = traits.Enum( "mean", "median", @@ -637,7 +701,9 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): desc="operation to computer along a specified axis", ) axis = traits.Int( - 0, argstr="-axis %d", desc="specfied axis to perform the operation along" + 0, + argstr="-axis %d", + desc="specfied axis to perform the operation along", ) @@ -677,7 +743,11 @@ def _list_outputs(self): class MRResizeInputSpec(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", ) image_size = traits.Tuple( (traits.Int, traits.Int, traits.Int), @@ -794,7 +864,9 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SHConv(CommandLine): @@ -858,7 +930,9 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SH2Amp(CommandLine): From 412f7af292f6ae0f011fc9159eb3e4bc0ece69d3 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sat, 30 Oct 2021 18:53:12 +0300 Subject: [PATCH 02/22] added kwarg json_export to mrconvert interface --- nipype/interfaces/mrtrix3/utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index b1414274b3..941e2ba70b 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -638,10 +638,20 @@ class MRConvertInputSpec(MRTrix3BaseInputSpec): mandatory=False, desc="import data from a JSON file into header key-value pairs", ) + json_export = File( + exists=False, + argstr="-json_export %s", + mandatory=False, + desc="export data from an image header key-value pairs into a JSON file", + ) class MRConvertOutputSpec(TraitedSpec): out_file = File(exists=True, desc="output image") + json_export = File( + exists=True, + desc="exported data from an image header key-value pairs in a JSON file", + ) class MRConvert(MRTrix3Base): From b5246a854366638aa0ef16f50ca5d3b9ace86448 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sat, 30 Oct 2021 19:20:38 +0300 Subject: [PATCH 03/22] refractoring --- doc/devel/matlab_example1.py | 21 ++++++++++++------- doc/devel/matlab_example2.py | 8 ++++--- .../mrtrix3/tests/test_auto_MRConvert.py | 13 ++++++++++++ nipype/interfaces/mrtrix3/utils.py | 1 + tools/checkspecs.py | 15 +++++++------ tools/gitwash_dumper.py | 6 ++---- tools/run_examples.py | 12 ++++++++--- tools/toollib.py | 4 +++- 8 files changed, 53 insertions(+), 27 deletions(-) diff --git a/doc/devel/matlab_example1.py b/doc/devel/matlab_example1.py index aaf6c4cb3a..9f15389669 100644 --- a/doc/devel/matlab_example1.py +++ b/doc/devel/matlab_example1.py @@ -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): @@ -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 @@ -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 diff --git a/doc/devel/matlab_example2.py b/doc/devel/matlab_example2.py index 8d683ea45f..7d75e307b1 100644 --- a/doc/devel/matlab_example2.py +++ b/doc/devel/matlab_example2.py @@ -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): @@ -29,6 +28,7 @@ class HelloWorld(MatlabCommand): >>> out = hello.run() >>> print out.outputs.matlab_output """ + input_spec = HelloWorldInputSpec output_spec = HelloWorldOutputSpec @@ -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): diff --git a/nipype/interfaces/mrtrix3/tests/test_auto_MRConvert.py b/nipype/interfaces/mrtrix3/tests/test_auto_MRConvert.py index 578ffb9b1a..6c1f874273 100644 --- a/nipype/interfaces/mrtrix3/tests/test_auto_MRConvert.py +++ b/nipype/interfaces/mrtrix3/tests/test_auto_MRConvert.py @@ -44,6 +44,16 @@ def test_MRConvert_inputs(): mandatory=True, position=-2, ), + json_export=dict( + argstr="-json_export %s", + extensions=None, + mandatory=False, + ), + json_import=dict( + argstr="-json_import %s", + extensions=None, + mandatory=False, + ), nthreads=dict( argstr="-nthreads %d", nohash=True, @@ -73,6 +83,9 @@ def test_MRConvert_inputs(): def test_MRConvert_outputs(): output_map = dict( + json_export=dict( + extensions=None, + ), out_file=dict( extensions=None, ), diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 941e2ba70b..75dc0e8e8c 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -678,6 +678,7 @@ class MRConvert(MRTrix3Base): def _list_outputs(self): outputs = self.output_spec().get() outputs["out_file"] = op.abspath(self.inputs.out_file) + outputs["json_export"] = op.abspath(self.inputs.json_export) return outputs diff --git a/tools/checkspecs.py b/tools/checkspecs.py index 032fd122cc..7aaac0d107 100644 --- a/tools/checkspecs.py +++ b/tools/checkspecs.py @@ -13,8 +13,7 @@ # Functions and classes class InterfaceChecker(object): - """Class for checking all interface specifications - """ + """Class for checking all interface specifications""" def __init__( self, @@ -23,7 +22,7 @@ def __init__( module_skip_patterns=None, class_skip_patterns=None, ): - r""" Initialize package for parsing + r"""Initialize package for parsing Parameters ---------- @@ -113,14 +112,14 @@ def _uri2path(self, uri): return path def _path2uri(self, dirpath): - """ Convert directory path to uri """ + """Convert directory path to uri""" relpath = dirpath.replace(self.root_path, self.package_name) if relpath.startswith(os.path.sep): relpath = relpath[1:] return relpath.replace(os.path.sep, ".") def _parse_module(self, uri): - """ Parse module defined in *uri* """ + """Parse module defined in *uri*""" filename = self._uri2path(uri) if filename is None: # nothing that we could handle here. @@ -131,7 +130,7 @@ def _parse_module(self, uri): return functions, classes def _parse_lines(self, linesource, module): - """ Parse lines of text for functions and classes """ + """Parse lines of text for functions and classes""" functions = [] classes = [] for line in linesource: @@ -387,7 +386,7 @@ def test_specs(self, uri): return bad_specs def _survives_exclude(self, matchstr, match_type): - """ Returns True if *matchstr* does not match patterns + """Returns True if *matchstr* does not match patterns ``self.package_name`` removed from front of string if present @@ -429,7 +428,7 @@ def _survives_exclude(self, matchstr, match_type): return True def discover_modules(self): - """ Return module sequence discovered from ``self.package_name`` + """Return module sequence discovered from ``self.package_name`` Parameters diff --git a/tools/gitwash_dumper.py b/tools/gitwash_dumper.py index 36efd7f83e..2c08547bac 100755 --- a/tools/gitwash_dumper.py +++ b/tools/gitwash_dumper.py @@ -50,9 +50,7 @@ def cp_files(in_path, globs, out_path): def filename_search_replace(sr_pairs, filename, backup=False): - """ Search and replace for expressions in files - - """ + """Search and replace for expressions in files""" in_txt = open(filename, "rt").read(-1) out_txt = in_txt[:] for in_exp, out_exp in sr_pairs: @@ -94,7 +92,7 @@ def make_link_targets( url=None, ml_url=None, ): - """ Check and make link targets + """Check and make link targets If url is None or ml_url is None, check if there are links present for these in `known_link_fname`. If not, raise error. The check is: diff --git a/tools/run_examples.py b/tools/run_examples.py index 20382ef74d..bd77f1a0a4 100644 --- a/tools/run_examples.py +++ b/tools/run_examples.py @@ -4,9 +4,15 @@ if __name__ == "__main__": - print(dedent("""Nipype examples have been moved to niflow-nipype1-examples. + print( + dedent( + """Nipype examples have been moved to niflow-nipype1-examples. -Install with: pip install niflow-nipype1-examples""")) +Install with: pip install niflow-nipype1-examples""" + ) + ) if sys.argv[1:]: - print("Run this command with: niflow-nipype1-examples " + " ".join(sys.argv[1:])) + print( + "Run this command with: niflow-nipype1-examples " + " ".join(sys.argv[1:]) + ) sys.exit(1) diff --git a/tools/toollib.py b/tools/toollib.py index 77d864f142..e740787575 100644 --- a/tools/toollib.py +++ b/tools/toollib.py @@ -31,7 +31,9 @@ def sh(cmd): def compile_tree(): """Compile all Python files below current directory.""" vstr = ".".join(map(str, sys.version_info[:2])) - stat = os.system("%s %s/lib/python%s/compileall.py ." % (sys.executable, sys.prefix, vstr)) + stat = os.system( + "%s %s/lib/python%s/compileall.py ." % (sys.executable, sys.prefix, vstr) + ) if stat: msg = "*** ERROR: Some Python files in tree do NOT compile! ***\n" msg += "See messages above for the actual file that produced it.\n" From 5979cb869882aa51279b4ec5e6ee9c9ac47c3daf Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sat, 30 Oct 2021 19:27:41 +0300 Subject: [PATCH 04/22] refractoring w/ black... --- nipype/interfaces/mrtrix/convert.py | 12 ++++++--- nipype/interfaces/mrtrix3/utils.py | 40 ++++++++--------------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/nipype/interfaces/mrtrix/convert.py b/nipype/interfaces/mrtrix/convert.py index 41b593d9a6..b20389c4a1 100644 --- a/nipype/interfaces/mrtrix/convert.py +++ b/nipype/interfaces/mrtrix/convert.py @@ -121,7 +121,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) @@ -193,7 +195,10 @@ class MRTrix2TrackVis(DipyBaseInterface): output_spec = MRTrix2TrackVisOutputSpec def _run_interface(self, runtime): - from dipy.tracking.utils import move_streamlines, affine_from_fsl_mat_file + from dipy.tracking.utils import ( + move_streamlines, + affine_from_fsl_mat_file, + ) dx, dy, dz = get_data_dims(self.inputs.image_file) vx, vy, vz = get_vox_dims(self.inputs.image_file) @@ -215,7 +220,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) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 75dc0e8e8c..c5059242b6 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -144,9 +144,7 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") class Generate5ttOutputSpec(TraitedSpec): @@ -192,12 +190,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): out_fa = File(argstr="-fa %s", desc="output FA file") out_adc = File(argstr="-adc %s", desc="output ADC file") - out_evec = File( - argstr="-vector %s", desc="output selected eigenvector(s) file" - ) - out_eval = File( - argstr="-value %s", desc="output selected eigenvalue(s) file" - ) + out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") + out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") component = traits.List( [1], usedefault=True, @@ -212,8 +206,7 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" - " brain mask image" + "only perform computation within the specified binary" " brain mask image" ), ) modulate = traits.Enum( @@ -301,8 +294,7 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " - "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -392,8 +384,7 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) @@ -498,8 +489,7 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) @@ -543,9 +533,7 @@ class DWIExtractInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -690,9 +678,7 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): position=-3, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") operation = traits.Enum( "mean", "median", @@ -875,9 +861,7 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File( - exists=True, desc="the output convoluted spherical harmonics file" - ) + out_file = File(exists=True, desc="the output convoluted spherical harmonics file") class SHConv(CommandLine): @@ -941,9 +925,7 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File( - exists=True, desc="the output convoluted spherical harmonics file" - ) + out_file = File(exists=True, desc="the output convoluted spherical harmonics file") class SH2Amp(CommandLine): From aa64ccdfbcbf40932c0c53ca601aa2a488d21862 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sun, 31 Oct 2021 10:02:02 +0200 Subject: [PATCH 05/22] Created an interface for mrtrix3's mrcat function --- nipype/interfaces/mrtrix3/__init__.py | 1 + nipype/interfaces/mrtrix3/utils.py | 213 +++++++++++++++++++++++--- 2 files changed, 189 insertions(+), 25 deletions(-) diff --git a/nipype/interfaces/mrtrix3/__init__.py b/nipype/interfaces/mrtrix3/__init__.py index 9ea9850b1b..126192030f 100644 --- a/nipype/interfaces/mrtrix3/__init__.py +++ b/nipype/interfaces/mrtrix3/__init__.py @@ -15,6 +15,7 @@ DWIExtract, SHConv, SH2Amp, + MRCat, ) from .preprocess import ( ResponseSD, diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 77c3047f20..dd22aff941 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -64,9 +64,108 @@ def _list_outputs(self): return outputs +class MRCatInputSpec(MRTrix3BaseInputSpec): + in_files = traits.List( + File(exists=True), + argstr="%s", + position=-2, + mandatory=True, + desc="files to concatenate", + ) + + out_file = File( + "concatenated.mif", + argstr="%s", + mandatory=True, + position=-1, + usedefault=True, + desc="output concatenated image", + ) + + axis = traits.Int( + argstr="-axis %s", + desc="""specify axis along which concatenation should be performed. By default, + the program will use the last non-singleton, non-spatial axis of any of + the input images - in other words axis 3 or whichever axis (greater than + 3) of the input images has size greater than one""", + ) + + datatype = traits.Enum( + "float32", + "float32le", + "float32be", + "float64", + "float64le", + "float64be", + "int64", + "uint64", + "int64le", + "uint64le", + "int64be", + "uint64be", + "int32", + "uint32", + "int32le", + "uint32le", + "int32be", + "uint32be", + "int16", + "uint16", + "int16le", + "uint16le", + "int16be", + "uint16be", + "cfloat32", + "cfloat32le", + "cfloat32be", + "cfloat64", + "cfloat64le", + "cfloat64be", + "int8", + "uint8", + "bit", + argstr="-datatype %s", + desc="specify output image data type", + ) + + +class MRCatOutputSpec(TraitedSpec): + out_file = File(exists=True, desc="the output concatenated image") + + +class MRCat(CommandLine): + """ + Concatenate several images into one + + + Example + ------- + + >>> import nipype.interfaces.mrtrix3 as mrt + >>> mrcat = mrt.MRCat() + >>> mrcat.inputs.in_files = ['image1.mif','image2.mif'] + >>> mrcat.cmdline # doctest: +ELLIPSIS + 'mrcat image1.mif image2.mif concatenated.mif' + >>> mrcat.run() # doctest: +SKIP + """ + + _cmd = "mrcat" + input_spec = MRCatInputSpec + output_spec = MRCatOutputSpec + + def _list_outputs(self): + outputs = self.output_spec().get() + outputs["out_file"] = op.abspath(self.inputs.out_file) + return outputs + + class Mesh2PVEInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-3, desc="input mesh" + exists=True, + argstr="%s", + mandatory=True, + position=-3, + desc="input mesh", ) reference = File( exists=True, @@ -134,9 +233,15 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): desc="tissue segmentation algorithm", ) in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input image", + ) + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") class Generate5ttOutputSpec(TraitedSpec): @@ -173,13 +278,21 @@ def _list_outputs(self): class TensorMetricsInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-1, desc="input DTI image" + exists=True, + argstr="%s", + mandatory=True, + position=-1, + desc="input DTI image", ) out_fa = File(argstr="-fa %s", desc="output FA file") out_adc = File(argstr="-adc %s", desc="output ADC file") - out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") - out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") + out_evec = File( + argstr="-vector %s", desc="output selected eigenvector(s) file" + ) + out_eval = File( + argstr="-value %s", desc="output selected eigenvalue(s) file" + ) component = traits.List( [1], usedefault=True, @@ -194,7 +307,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" " brain mask image" + "only perform computation within the specified binary" + " brain mask image" ), ) modulate = traits.Enum( @@ -246,10 +360,18 @@ def _list_outputs(self): class ComputeTDIInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input tractography" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input tractography", ) out_file = File( - "tdi.mif", argstr="%s", usedefault=True, position=-1, desc="output TDI file" + "tdi.mif", + argstr="%s", + usedefault=True, + position=-1, + desc="output TDI file", ) reference = File( exists=True, @@ -274,7 +396,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " + "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -353,7 +476,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): "(these lengths are then taken into account during TWI calculation)", ) ends_only = traits.Bool( - argstr="-ends_only", desc="only map the streamline" " endpoints to the image" + argstr="-ends_only", + desc="only map the streamline" " endpoints to the image", ) tck_weights = File( @@ -363,7 +487,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -438,10 +563,18 @@ def _list_outputs(self): class TCK2VTKInputSpec(CommandLineInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input tractography" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input tractography", ) out_file = File( - "tracks.vtk", argstr="%s", usedefault=True, position=-1, desc="output VTK file" + "tracks.vtk", + argstr="%s", + usedefault=True, + position=-1, + desc="output VTK file", ) reference = File( exists=True, @@ -460,7 +593,8 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -498,9 +632,15 @@ def _list_outputs(self): class DWIExtractInputSpec(MRTrix3BaseInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input image", + ) + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -549,7 +689,11 @@ def _list_outputs(self): class MRConvertInputSpec(MRTrix3BaseInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-2, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-2, + desc="input image", ) out_file = File( "dwi.mif", @@ -566,7 +710,10 @@ class MRConvertInputSpec(MRTrix3BaseInputSpec): desc="extract data at the specified coordinates", ) vox = traits.List( - traits.Float, sep=",", argstr="-vox %s", desc="change the voxel dimensions" + traits.Float, + sep=",", + argstr="-vox %s", + desc="change the voxel dimensions", ) axes = traits.List( traits.Int, @@ -615,9 +762,15 @@ def _list_outputs(self): class MRMathInputSpec(MRTrix3BaseInputSpec): in_file = File( - exists=True, argstr="%s", mandatory=True, position=-3, desc="input image" + exists=True, + argstr="%s", + mandatory=True, + position=-3, + desc="input image", + ) + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") operation = traits.Enum( "mean", "median", @@ -637,7 +790,9 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): desc="operation to computer along a specified axis", ) axis = traits.Int( - 0, argstr="-axis %d", desc="specfied axis to perform the operation along" + 0, + argstr="-axis %d", + desc="specfied axis to perform the operation along", ) @@ -677,7 +832,11 @@ def _list_outputs(self): class MRResizeInputSpec(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", ) image_size = traits.Tuple( (traits.Int, traits.Int, traits.Int), @@ -794,7 +953,9 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SHConv(CommandLine): @@ -858,7 +1019,9 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SH2Amp(CommandLine): From 1b74350803c68c3469c001c5d0c0cc427ebca147 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sun, 31 Oct 2021 10:17:12 +0200 Subject: [PATCH 06/22] forgot to run make before commit... --- .../mrtrix3/tests/test_auto_MRCat.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 nipype/interfaces/mrtrix3/tests/test_auto_MRCat.py diff --git a/nipype/interfaces/mrtrix3/tests/test_auto_MRCat.py b/nipype/interfaces/mrtrix3/tests/test_auto_MRCat.py new file mode 100644 index 0000000000..5659da4507 --- /dev/null +++ b/nipype/interfaces/mrtrix3/tests/test_auto_MRCat.py @@ -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 From a156154f39e3ae9a2bb12dce4588f98200f6077c Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sun, 31 Oct 2021 10:23:41 +0200 Subject: [PATCH 07/22] refractored black formatting --- doc/devel/matlab_example1.py | 21 ++++++++++------ doc/devel/matlab_example2.py | 8 +++--- nipype/interfaces/mrtrix3/utils.py | 40 ++++++++---------------------- tools/checkspecs.py | 15 ++++++----- tools/gitwash_dumper.py | 6 ++--- tools/run_examples.py | 12 ++++++--- tools/toollib.py | 4 ++- 7 files changed, 50 insertions(+), 56 deletions(-) diff --git a/doc/devel/matlab_example1.py b/doc/devel/matlab_example1.py index aaf6c4cb3a..9f15389669 100644 --- a/doc/devel/matlab_example1.py +++ b/doc/devel/matlab_example1.py @@ -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): @@ -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 @@ -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 diff --git a/doc/devel/matlab_example2.py b/doc/devel/matlab_example2.py index 8d683ea45f..7d75e307b1 100644 --- a/doc/devel/matlab_example2.py +++ b/doc/devel/matlab_example2.py @@ -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): @@ -29,6 +28,7 @@ class HelloWorld(MatlabCommand): >>> out = hello.run() >>> print out.outputs.matlab_output """ + input_spec = HelloWorldInputSpec output_spec = HelloWorldOutputSpec @@ -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): diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index dd22aff941..fa624fa79c 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -239,9 +239,7 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") class Generate5ttOutputSpec(TraitedSpec): @@ -287,12 +285,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): out_fa = File(argstr="-fa %s", desc="output FA file") out_adc = File(argstr="-adc %s", desc="output ADC file") - out_evec = File( - argstr="-vector %s", desc="output selected eigenvector(s) file" - ) - out_eval = File( - argstr="-value %s", desc="output selected eigenvalue(s) file" - ) + out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") + out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") component = traits.List( [1], usedefault=True, @@ -307,8 +301,7 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" - " brain mask image" + "only perform computation within the specified binary" " brain mask image" ), ) modulate = traits.Enum( @@ -396,8 +389,7 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " - "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -487,8 +479,7 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) @@ -593,8 +584,7 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) @@ -638,9 +628,7 @@ class DWIExtractInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -768,9 +756,7 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): position=-3, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") operation = traits.Enum( "mean", "median", @@ -953,9 +939,7 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File( - exists=True, desc="the output convoluted spherical harmonics file" - ) + out_file = File(exists=True, desc="the output convoluted spherical harmonics file") class SHConv(CommandLine): @@ -1019,9 +1003,7 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File( - exists=True, desc="the output convoluted spherical harmonics file" - ) + out_file = File(exists=True, desc="the output convoluted spherical harmonics file") class SH2Amp(CommandLine): diff --git a/tools/checkspecs.py b/tools/checkspecs.py index 032fd122cc..7aaac0d107 100644 --- a/tools/checkspecs.py +++ b/tools/checkspecs.py @@ -13,8 +13,7 @@ # Functions and classes class InterfaceChecker(object): - """Class for checking all interface specifications - """ + """Class for checking all interface specifications""" def __init__( self, @@ -23,7 +22,7 @@ def __init__( module_skip_patterns=None, class_skip_patterns=None, ): - r""" Initialize package for parsing + r"""Initialize package for parsing Parameters ---------- @@ -113,14 +112,14 @@ def _uri2path(self, uri): return path def _path2uri(self, dirpath): - """ Convert directory path to uri """ + """Convert directory path to uri""" relpath = dirpath.replace(self.root_path, self.package_name) if relpath.startswith(os.path.sep): relpath = relpath[1:] return relpath.replace(os.path.sep, ".") def _parse_module(self, uri): - """ Parse module defined in *uri* """ + """Parse module defined in *uri*""" filename = self._uri2path(uri) if filename is None: # nothing that we could handle here. @@ -131,7 +130,7 @@ def _parse_module(self, uri): return functions, classes def _parse_lines(self, linesource, module): - """ Parse lines of text for functions and classes """ + """Parse lines of text for functions and classes""" functions = [] classes = [] for line in linesource: @@ -387,7 +386,7 @@ def test_specs(self, uri): return bad_specs def _survives_exclude(self, matchstr, match_type): - """ Returns True if *matchstr* does not match patterns + """Returns True if *matchstr* does not match patterns ``self.package_name`` removed from front of string if present @@ -429,7 +428,7 @@ def _survives_exclude(self, matchstr, match_type): return True def discover_modules(self): - """ Return module sequence discovered from ``self.package_name`` + """Return module sequence discovered from ``self.package_name`` Parameters diff --git a/tools/gitwash_dumper.py b/tools/gitwash_dumper.py index 36efd7f83e..2c08547bac 100755 --- a/tools/gitwash_dumper.py +++ b/tools/gitwash_dumper.py @@ -50,9 +50,7 @@ def cp_files(in_path, globs, out_path): def filename_search_replace(sr_pairs, filename, backup=False): - """ Search and replace for expressions in files - - """ + """Search and replace for expressions in files""" in_txt = open(filename, "rt").read(-1) out_txt = in_txt[:] for in_exp, out_exp in sr_pairs: @@ -94,7 +92,7 @@ def make_link_targets( url=None, ml_url=None, ): - """ Check and make link targets + """Check and make link targets If url is None or ml_url is None, check if there are links present for these in `known_link_fname`. If not, raise error. The check is: diff --git a/tools/run_examples.py b/tools/run_examples.py index 20382ef74d..bd77f1a0a4 100644 --- a/tools/run_examples.py +++ b/tools/run_examples.py @@ -4,9 +4,15 @@ if __name__ == "__main__": - print(dedent("""Nipype examples have been moved to niflow-nipype1-examples. + print( + dedent( + """Nipype examples have been moved to niflow-nipype1-examples. -Install with: pip install niflow-nipype1-examples""")) +Install with: pip install niflow-nipype1-examples""" + ) + ) if sys.argv[1:]: - print("Run this command with: niflow-nipype1-examples " + " ".join(sys.argv[1:])) + print( + "Run this command with: niflow-nipype1-examples " + " ".join(sys.argv[1:]) + ) sys.exit(1) diff --git a/tools/toollib.py b/tools/toollib.py index 77d864f142..e740787575 100644 --- a/tools/toollib.py +++ b/tools/toollib.py @@ -31,7 +31,9 @@ def sh(cmd): def compile_tree(): """Compile all Python files below current directory.""" vstr = ".".join(map(str, sys.version_info[:2])) - stat = os.system("%s %s/lib/python%s/compileall.py ." % (sys.executable, sys.prefix, vstr)) + stat = os.system( + "%s %s/lib/python%s/compileall.py ." % (sys.executable, sys.prefix, vstr) + ) if stat: msg = "*** ERROR: Some Python files in tree do NOT compile! ***\n" msg += "See messages above for the actual file that produced it.\n" From 480c1f9a85e4d72a4934825186d24c047eed9643 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sun, 31 Oct 2021 15:23:44 +0200 Subject: [PATCH 08/22] fixed a small issue with output listing --- nipype/interfaces/mrtrix3/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index c5059242b6..e2fa3bab19 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -666,7 +666,8 @@ class MRConvert(MRTrix3Base): def _list_outputs(self): outputs = self.output_spec().get() outputs["out_file"] = op.abspath(self.inputs.out_file) - outputs["json_export"] = op.abspath(self.inputs.json_export) + if self.inputs.json_export: + outputs["json_export"] = op.abspath(self.inputs.json_export) return outputs From dbed52bc811c8e55588de5b6ec4285bd3e9535c4 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sun, 31 Oct 2021 22:14:57 +0200 Subject: [PATCH 09/22] just added some tensor-derived metrics for the interface of mrtrix3's tensor2metric --- nipype/interfaces/mrtrix3/preprocess.py | 46 ++++++++++++++++++----- nipype/interfaces/mrtrix3/utils.py | 50 +++++++++++++++++++------ 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/nipype/interfaces/mrtrix3/preprocess.py b/nipype/interfaces/mrtrix3/preprocess.py index ef67365f0b..feeff4e527 100644 --- a/nipype/interfaces/mrtrix3/preprocess.py +++ b/nipype/interfaces/mrtrix3/preprocess.py @@ -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( @@ -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], @@ -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( @@ -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", @@ -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" @@ -300,7 +317,7 @@ class DWIPreprocInputSpec(MRTrix3BaseInputSpec): out_grad_mrtrix = File( "grad.b", argstr="%s", - usedefault=True, + usedefault=False, requires=["export_grad_mrtrix"], desc="name of new gradient file", ) @@ -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( @@ -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( diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 212e8186b1..c855d1514e 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -239,7 +239,9 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" + ) class Generate5ttOutputSpec(TraitedSpec): @@ -285,8 +287,17 @@ class TensorMetricsInputSpec(CommandLineInputSpec): out_fa = File(argstr="-fa %s", desc="output FA file") out_adc = File(argstr="-adc %s", desc="output ADC file") - out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") - out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") + out_ad = File(argstr="-ad %s", desc="output AD file") + out_rd = File(argstr="-rd %s", desc="output RD file") + out_cl = File(argstr="-cl %s", desc="output CL file") + out_cp = File(argstr="-cp %s", desc="output CP file") + out_cs = File(argstr="-cs %s", desc="output CS file") + out_evec = File( + argstr="-vector %s", desc="output selected eigenvector(s) file" + ) + out_eval = File( + argstr="-value %s", desc="output selected eigenvalue(s) file" + ) component = traits.List( [1], usedefault=True, @@ -301,7 +312,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" " brain mask image" + "only perform computation within the specified binary" + " brain mask image" ), ) modulate = traits.Enum( @@ -316,6 +328,11 @@ class TensorMetricsInputSpec(CommandLineInputSpec): class TensorMetricsOutputSpec(TraitedSpec): out_fa = File(desc="output FA file") out_adc = File(desc="output ADC file") + out_ad = File(desc="output AD file") + out_rd = File(desc="output RD file") + out_cl = File(desc="output CL file") + out_cp = File(desc="output CP file") + out_cs = File(desc="output CS file") out_evec = File(desc="output selected eigenvector(s) file") out_eval = File(desc="output selected eigenvalue(s) file") @@ -389,7 +406,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " + "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -479,7 +497,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -584,7 +603,8 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -628,7 +648,9 @@ class DWIExtractInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" + ) bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -774,7 +796,9 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): position=-3, desc="input image", ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" + ) operation = traits.Enum( "mean", "median", @@ -957,7 +981,9 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SHConv(CommandLine): @@ -1021,7 +1047,9 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SH2Amp(CommandLine): From e263d362a28963febaebbabd8603cfa4fe7f327f Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Sun, 7 Nov 2021 23:02:38 +0200 Subject: [PATCH 10/22] a small addition for the mrconvert interface to include bvec/bval exports. WIP --- nipype/interfaces/mrtrix/preprocess.py | 2 +- nipype/interfaces/mrtrix3/base.py | 21 ++++++++++-- nipype/interfaces/mrtrix3/utils.py | 47 ++++++++++++++++++++------ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/nipype/interfaces/mrtrix/preprocess.py b/nipype/interfaces/mrtrix/preprocess.py index c79f9016e9..31ee8307e9 100644 --- a/nipype/interfaces/mrtrix/preprocess.py +++ b/nipype/interfaces/mrtrix/preprocess.py @@ -97,7 +97,7 @@ class MRConvertInputSpec(CommandLineInputSpec): offset_bias = traits.Float( argstr="-scale %d", position=3, - units="mm", + units="mm",json_import desc="Apply offset to the intensity values.", ) replace_NaN_with_zero = traits.Bool( diff --git a/nipype/interfaces/mrtrix3/base.py b/nipype/interfaces/mrtrix3/base.py index 7684e06cd7..6008c6b5d9 100644 --- a/nipype/interfaces/mrtrix3/base.py +++ b/nipype/interfaces/mrtrix3/base.py @@ -46,7 +46,8 @@ def looseversion(cls): class MRTrix3BaseInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) # DW gradient table import options @@ -79,6 +80,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): @@ -96,6 +106,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) @@ -104,7 +116,9 @@ def _parse_inputs(self, skip=None): skip = [] try: - if isdefined(self.inputs.grad_file) or isdefined(self.inputs.grad_fsl): + if isdefined(self.inputs.grad_file) or isdefined( + self.inputs.grad_fsl + ): skip += ["in_bvec", "in_bval"] is_bvec = isdefined(self.inputs.in_bvec) @@ -112,7 +126,8 @@ def _parse_inputs(self, skip=None): if is_bvec or is_bval: if not is_bvec or not is_bval: raise RuntimeError( - "If using bvecs and bvals inputs, both" "should be defined" + "If using bvecs and bvals inputs, both" + "should be defined" ) skip += ["in_bval"] except AttributeError: diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index e2fa3bab19..a947492a5a 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -144,7 +144,9 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" + ) class Generate5ttOutputSpec(TraitedSpec): @@ -190,8 +192,12 @@ class TensorMetricsInputSpec(CommandLineInputSpec): out_fa = File(argstr="-fa %s", desc="output FA file") out_adc = File(argstr="-adc %s", desc="output ADC file") - out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") - out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") + out_evec = File( + argstr="-vector %s", desc="output selected eigenvector(s) file" + ) + out_eval = File( + argstr="-value %s", desc="output selected eigenvalue(s) file" + ) component = traits.List( [1], usedefault=True, @@ -206,7 +212,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" " brain mask image" + "only perform computation within the specified binary" + " brain mask image" ), ) modulate = traits.Enum( @@ -294,7 +301,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " + "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -384,7 +392,8 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -489,7 +498,8 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" " of available cpus will be used", + desc="number of threads. if zero, the number" + " of available cpus will be used", nohash=True, ) @@ -533,7 +543,9 @@ class DWIExtractInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" + ) bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -596,6 +608,7 @@ class MRConvertInputSpec(MRTrix3BaseInputSpec): usedefault=True, desc="output image", ) + coord = traits.List( traits.Int, sep=" ", @@ -640,6 +653,8 @@ class MRConvertOutputSpec(TraitedSpec): exists=True, desc="exported data from an image header key-value pairs in a JSON file", ) + out_bvec = File(exists=True, desc="export bvec file in FSL format") + out_bval = File(exists=True, desc="export bvec file in FSL format") class MRConvert(MRTrix3Base): @@ -668,6 +683,10 @@ def _list_outputs(self): outputs["out_file"] = op.abspath(self.inputs.out_file) if self.inputs.json_export: outputs["json_export"] = op.abspath(self.inputs.json_export) + if self.inputs.out_bvec: + outputs["out_bvec"] = op.abspath(self.inputs.out_bvec) + if self.inputs.out_bval: + outputs["out_bval"] = op.abspath(self.inputs.out_bval) return outputs @@ -679,7 +698,9 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): position=-3, desc="input image", ) - out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") + out_file = File( + argstr="%s", mandatory=True, position=-1, desc="output image" + ) operation = traits.Enum( "mean", "median", @@ -862,7 +883,9 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SHConv(CommandLine): @@ -926,7 +949,9 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File(exists=True, desc="the output convoluted spherical harmonics file") + out_file = File( + exists=True, desc="the output convoluted spherical harmonics file" + ) class SH2Amp(CommandLine): From 8462a45185cb36fefde3137c457c28287243d7f9 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Fri, 26 Nov 2021 11:49:08 +0200 Subject: [PATCH 11/22] updated some small changes --- nipype/interfaces/mrtrix/defhdr.mat | Bin 533 -> 0 bytes nipype/interfaces/mrtrix/preprocess.py | 73 +++++++++++++++++++------ nipype/interfaces/mrtrix3/base.py | 8 +-- 3 files changed, 59 insertions(+), 22 deletions(-) delete mode 100644 nipype/interfaces/mrtrix/defhdr.mat diff --git a/nipype/interfaces/mrtrix/defhdr.mat b/nipype/interfaces/mrtrix/defhdr.mat deleted file mode 100644 index e27ddf576f24fa7d876bb603c8ed276945dfe30d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 533 zcmeZu4DoSvQZUssQ1EpO(M`+DN!3vZ$Vn_o%P-2cQgHY2i*PhE(NS#k5il@%`tma{F!VAqFjUNWJ8iGuVFQ7N zTwyJipsS6Y%Nf*;h4mOPCT(4FEK7H5SxWn*vPTCi816C1HBOv3k-ISZ`py6j;T7t0 z*X%w${dcGEa$k;{)(T5P*DUCnvgvj1t`M&y%k352@0D|lub#5|j+U*$#wA%XAEz1_ z23tPzJDVzF7{)v0n8exG_@0*1sU9z%NA*0uojEUZvgd4V$;5m1tAhfh&+~3yH0gww z`dgP}rGF)6dr7+*O`Ml>_rRTFNeYdDLC=-K*|J($dzvg)CVpY*dUpBNi8W?M>)r0` zJ@G@t@ze<(AePZ&NV{uZ9#9*qu)q1=gcAl^U+8Hp;jMBD`)%~`UqSq~KT#gXZ<~kj z{mgw{p1p?qVL8*h#;*o%%pOT3_wI0=Kigv0v5TtT{~fLQ=E%SJr_IFw%C~o#PG0n1 zZ{_^xN2R9Qi`Z_|-plwaBmeT(AM1PDr|#GNSlwPb{qWz*jITG9-|dKg!hN3cjV=Fq wMzI@TW`EtZbq~+m&+7N5>wd3Z_v3s`-NN2aiRWvDr!)NIa{t8sS|Yn10FWN#ZU6uP diff --git a/nipype/interfaces/mrtrix/preprocess.py b/nipype/interfaces/mrtrix/preprocess.py index 31ee8307e9..14d549c530 100644 --- a/nipype/interfaces/mrtrix/preprocess.py +++ b/nipype/interfaces/mrtrix/preprocess.py @@ -24,7 +24,9 @@ class MRConvertInputSpec(CommandLineInputSpec): position=-2, desc="voxel-order data filename", ) - out_filename = File(genfile=True, argstr="%s", position=-1, desc="Output filename") + out_filename = File( + genfile=True, argstr="%s", position=-1, desc="Output filename" + ) extract_at_axis = traits.Enum( 1, 2, @@ -97,7 +99,7 @@ class MRConvertInputSpec(CommandLineInputSpec): offset_bias = traits.Float( argstr="-scale %d", position=3, - units="mm",json_import + units="mm", desc="Apply offset to the intensity values.", ) replace_NaN_with_zero = traits.Bool( @@ -227,11 +229,15 @@ class DWI2TensorInputSpec(CommandLineInputSpec): position=1, desc=("Do not display information messages or progress " "status."), ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class DWI2TensorOutputSpec(TraitedSpec): - tensor = File(exists=True, desc="path/name of output diffusion tensor image") + tensor = File( + exists=True, desc="path/name of output diffusion tensor image" + ) class DWI2Tensor(CommandLine): @@ -271,7 +277,9 @@ class Tensor2VectorInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class Tensor2VectorOutputSpec(TraitedSpec): @@ -337,7 +345,9 @@ class Tensor2FractionalAnisotropyInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class Tensor2FractionalAnisotropyOutputSpec(TraitedSpec): @@ -403,7 +413,9 @@ class Tensor2ApparentDiffusionInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class Tensor2ApparentDiffusionOutputSpec(TraitedSpec): @@ -466,7 +478,9 @@ class MRMultiplyInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class MRMultiplyOutputSpec(TraitedSpec): @@ -523,7 +537,9 @@ class MRTrixViewerInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class MRTrixViewerOutputSpec(TraitedSpec): @@ -595,7 +611,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, @@ -669,14 +689,18 @@ 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", position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class ErodeOutputSpec(TraitedSpec): @@ -729,7 +753,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." @@ -738,7 +765,9 @@ class ThresholdInputSpec(CommandLineInputSpec): argstr="-percent %s", desc="Specify threshold value as a percentage of the peak intensity in the input image.", ) - invert = traits.Bool(argstr="-invert", position=1, desc="Invert output binary mask") + invert = traits.Bool( + argstr="-invert", position=1, desc="Invert output binary mask" + ) replace_zeros_with_NaN = traits.Bool( argstr="-nan", position=1, desc="Replace all zero values with NaN" ) @@ -747,7 +776,9 @@ class ThresholdInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class ThresholdOutputSpec(TraitedSpec): @@ -812,7 +843,9 @@ class MedianFilter3DInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class MedianFilter3DOutputSpec(TraitedSpec): @@ -864,7 +897,9 @@ class MRTransformInputSpec(CommandLineInputSpec): position=-2, desc="Input images to be transformed", ) - out_filename = File(genfile=True, argstr="%s", position=-1, desc="Output image") + out_filename = File( + genfile=True, argstr="%s", position=-1, desc="Output image" + ) invert = traits.Bool( argstr="-inverse", position=1, @@ -914,7 +949,9 @@ class MRTransformInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) class MRTransformOutputSpec(TraitedSpec): diff --git a/nipype/interfaces/mrtrix3/base.py b/nipype/interfaces/mrtrix3/base.py index 6008c6b5d9..3ab76e881d 100644 --- a/nipype/interfaces/mrtrix3/base.py +++ b/nipype/interfaces/mrtrix3/base.py @@ -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") From c42ac756a126add4b139e99b75b2ac7ff763aaff Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Tue, 30 Nov 2021 11:57:46 +0200 Subject: [PATCH 12/22] added interface for mrtrix3's transformconvert and mrtransform --- nipype/interfaces/mrtrix3/__init__.py | 42 +++--- nipype/interfaces/mrtrix3/utils.py | 191 +++++++++++++++++++++++++- 2 files changed, 209 insertions(+), 24 deletions(-) diff --git a/nipype/interfaces/mrtrix3/__init__.py b/nipype/interfaces/mrtrix3/__init__.py index 126192030f..0ff8daa510 100644 --- a/nipype/interfaces/mrtrix3/__init__.py +++ b/nipype/interfaces/mrtrix3/__init__.py @@ -2,30 +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, - MRCat, -) -from .preprocess import ( - ResponseSD, - ACTPrepareFSL, - ReplaceFSwithFIRST, - DWIPreproc, - DWIDenoise, - MRDeGibbs, - DWIBiasCorrect, + SHConv, + TensorMetrics, + TransformFSLConvert, ) -from .tracking import Tractography -from .reconst import FitTensor, EstimateFOD, ConstrainedSphericalDeconvolution -from .connectivity import LabelConfig, LabelConvert, BuildConnectome diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 754a18a13d..4f71252485 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -4,16 +4,17 @@ import os.path as op +from ...utils.filemanip import split_filename from ..base import ( - CommandLineInputSpec, CommandLine, - traits, - TraitedSpec, + CommandLineInputSpec, File, InputMultiPath, + TraitedSpec, isdefined, + traits, ) -from .base import MRTrix3BaseInputSpec, MRTrix3Base +from .base import MRTrix3Base, MRTrix3BaseInputSpec class BrainMaskInputSpec(MRTrix3BaseInputSpec): @@ -795,6 +796,188 @@ def _list_outputs(self): return outputs +class TransformFSLConvertInputSpec(MRTrix3BaseInputSpec): + in_file = File( + exists=True, + argstr="%s", + mandatory=True, + position=1, + desc="FLIRT input image", + ) + reference = File( + exists=True, + argstr="%s", + mandatory=True, + position=2, + desc="FLIRT reference image", + ) + in_transform = File( + exists=True, + argstr="%s", + mandatory=True, + position=0, + desc="FLIRT output transformation matrix", + ) + out_transform = File( + "transform_mrtrix.txt", + argstr="%s", + mandatory=True, + position=-1, + usedefault=True, + desc="output transformed affine in mrtrix3's format", + ) + flirt_import = traits.Bool( + True, + argstr="flirt_import", + mandatory=True, + usedefault=True, + position=-2, + desc="import transform from FSL's FLIRT.", + ) + + +class TransformFSLConvertOutputSpec(TraitedSpec): + out_transform = File( + exists=True, desc="output transformed affine in mrtrix3's format" + ) + + +class TransformFSLConvert(MRTrix3Base): + """ + Perform conversion between FSL's transformation matrix format to mrtrix3's. + + Example + ------- + + >>> import nipype.interfaces.mrtrix3 as mrt + >>> transform = mrt.TransformConvert() + >>> transform.inputs.in_file = 'flirt_in.nii.gz' + >>> transform.inputs.reference = 'flirt_ref.nii.gz' + >>> transform.inputs.in_transform = 'transform_flirt.mat' + >>> transform.inputs.out_transform = 'transform_mrtrix.txt' + >>> transform.cmdline # doctest: +ELLIPSIS + 'transformconvert transform_flirt.mat flirt_in.nii flirt_ref.nii flirt_import transform_mrtrix.txt' + >>> transform.run() # doctest: +SKIP + """ + + _cmd = "transformconvert" + input_spec = TransformFSLConvertInputSpec + output_spec = TransformFSLConvertOutputSpec + + def _list_outputs(self): + outputs = self.output_spec().get() + outputs["out_transform"] = op.abspath(self.inputs.out_transform) + return outputs + + +class MRTransformInputSpec(MRTrix3BaseInputSpec): + in_files = InputMultiPath( + File(exists=True), + argstr="%s", + mandatory=True, + position=-2, + desc="Input images to be transformed", + ) + out_file = File( + genfile=True, + argstr="%s", + position=-1, + desc="Output image", + ) + invert = traits.Bool( + argstr="-inverse", + position=1, + desc="Invert the specified transform before using it", + ) + linear_transform = File( + exists=True, + argstr="-linear %s", + position=1, + desc=( + "Specify a linear transform to apply, in the form of a 3x4 or 4x4 ascii file. " + "Note the standard reverse convention is used, " + "where the transform maps points in the template image to the moving image. " + "Note that the reverse convention is still assumed even if no -template image is supplied." + ), + ) + replace_transform = traits.Bool( + argstr="-replace", + position=1, + desc="replace the current transform by that specified, rather than applying it to the current transform", + ) + transformation_file = File( + exists=True, + argstr="-transform %s", + position=1, + desc="The transform to apply, in the form of a 4x4 ascii file.", + ) + template_image = File( + exists=True, + argstr="-template %s", + position=1, + desc="Reslice the input image to match the specified template image.", + ) + reference_image = File( + exists=True, + argstr="-reference %s", + position=1, + desc="in case the transform supplied maps from the input image onto a reference image, use this option to specify the reference. Note that this implicitly sets the -replace option.", + ) + flip_x = traits.Bool( + argstr="-flipx", + position=1, + desc="assume the transform is supplied assuming a coordinate system with the x-axis reversed relative to the MRtrix convention (i.e. x increases from right to left). This is required to handle transform matrices produced by FSL's FLIRT command. This is only used in conjunction with the -reference option.", + ) + quiet = traits.Bool( + argstr="-quiet", + position=1, + desc="Do not display information messages or progress status.", + ) + debug = traits.Bool( + argstr="-debug", position=1, desc="Display debugging messages." + ) + + +class MRTransformOutputSpec(TraitedSpec): + out_file = File(exists=True, desc="the output image of the transformation") + + +class MRTransform(MRTrix3Base): + """ + Apply spatial transformations or reslice images + + Example + ------- + + >>> MRxform = MRTransform() + >>> MRxform.inputs.in_files = 'anat_coreg.mif' + >>> MRxform.run() # doctest: +SKIP + """ + + _cmd = "mrtransform" + input_spec = MRTransformInputSpec + output_spec = MRTransformOutputSpec + + def _list_outputs(self): + outputs = self.output_spec().get() + outputs["out_file"] = self.inputs.out_file + if not isdefined(outputs["out_file"]): + outputs["out_file"] = op.abspath(self._gen_outfilename()) + else: + outputs["out_file"] = op.abspath(outputs["out_file"]) + return outputs + + def _gen_filename(self, name): + if name == "out_file": + return self._gen_outfilename() + else: + return None + + def _gen_outfilename(self): + _, name, _ = split_filename(self.inputs.in_files[0]) + return name + "_MRTransform.mif" + + class MRMathInputSpec(MRTrix3BaseInputSpec): in_file = File( exists=True, From 7a77fdd8a858fdc2f53a24e361085ff8280ad549 Mon Sep 17 00:00:00 2001 From: GalBenZvi Date: Tue, 30 Nov 2021 12:13:48 +0200 Subject: [PATCH 13/22] added interface for mrtrix3's transformconvert and mrtransform --- nipype/interfaces/mrtrix3/utils.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 4f71252485..c8a6123dbc 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -5,15 +5,8 @@ import os.path as op from ...utils.filemanip import split_filename -from ..base import ( - CommandLine, - CommandLineInputSpec, - File, - InputMultiPath, - TraitedSpec, - isdefined, - traits, -) +from ..base import (CommandLine, CommandLineInputSpec, File, InputMultiPath, + TraitedSpec, isdefined, traits) from .base import MRTrix3Base, MRTrix3BaseInputSpec From 3334c58f7a95b9bcd126e03036d253fd4055ed72 Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Wed, 22 Dec 2021 20:25:14 +0200 Subject: [PATCH 14/22] Fixed dwipreproc default value in tests. --- nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py b/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py index 7f226fe3cd..b289b9c8f3 100644 --- a/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py +++ b/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py @@ -115,7 +115,7 @@ def test_DWIPreproc_outputs(): out_grad_mrtrix=dict( argstr="%s", extensions=None, - usedefault=True, + usedefault=False, ), ) outputs = DWIPreproc.output_spec() From a6472dc981654356dee7bdc5a0f1cf0576b81aaf Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Wed, 22 Dec 2021 20:31:51 +0200 Subject: [PATCH 15/22] Fixed TransformFSLConvert import path in example docs. --- nipype/interfaces/mrtrix3/utils.py | 57 ++++++++++++------------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index c8a6123dbc..8d6da8f930 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -5,8 +5,15 @@ import os.path as op from ...utils.filemanip import split_filename -from ..base import (CommandLine, CommandLineInputSpec, File, InputMultiPath, - TraitedSpec, isdefined, traits) +from ..base import ( + CommandLine, + CommandLineInputSpec, + File, + InputMultiPath, + TraitedSpec, + isdefined, + traits, +) from .base import MRTrix3Base, MRTrix3BaseInputSpec @@ -233,9 +240,7 @@ class Generate5ttInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") class Generate5ttOutputSpec(TraitedSpec): @@ -286,12 +291,8 @@ class TensorMetricsInputSpec(CommandLineInputSpec): out_cl = File(argstr="-cl %s", desc="output CL file") out_cp = File(argstr="-cp %s", desc="output CP file") out_cs = File(argstr="-cs %s", desc="output CS file") - out_evec = File( - argstr="-vector %s", desc="output selected eigenvector(s) file" - ) - out_eval = File( - argstr="-value %s", desc="output selected eigenvalue(s) file" - ) + out_evec = File(argstr="-vector %s", desc="output selected eigenvector(s) file") + out_eval = File(argstr="-value %s", desc="output selected eigenvalue(s) file") component = traits.List( [1], usedefault=True, @@ -306,8 +307,7 @@ class TensorMetricsInputSpec(CommandLineInputSpec): exists=True, argstr="-mask %s", desc=( - "only perform computation within the specified binary" - " brain mask image" + "only perform computation within the specified binary" " brain mask image" ), ) modulate = traits.Enum( @@ -400,8 +400,7 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) max_tod = traits.Int( argstr="-tod %d", - desc="generate a Track Orientation " - "Distribution (TOD) in each voxel.", + desc="generate a Track Orientation " "Distribution (TOD) in each voxel.", ) contrast = traits.Enum( @@ -491,8 +490,7 @@ class ComputeTDIInputSpec(CommandLineInputSpec): ) nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) @@ -597,8 +595,7 @@ class TCK2VTKInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) @@ -642,9 +639,7 @@ class DWIExtractInputSpec(MRTrix3BaseInputSpec): position=-2, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes") nobzero = traits.Bool(argstr="-no_bzero", desc="extract non b=0 volumes") singleshell = traits.Bool( @@ -843,7 +838,7 @@ class TransformFSLConvert(MRTrix3Base): ------- >>> import nipype.interfaces.mrtrix3 as mrt - >>> transform = mrt.TransformConvert() + >>> transform = mrt.TransformFSLConvert() >>> transform.inputs.in_file = 'flirt_in.nii.gz' >>> transform.inputs.reference = 'flirt_ref.nii.gz' >>> transform.inputs.in_transform = 'transform_flirt.mat' @@ -926,9 +921,7 @@ class MRTransformInputSpec(MRTrix3BaseInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class MRTransformOutputSpec(TraitedSpec): @@ -979,9 +972,7 @@ class MRMathInputSpec(MRTrix3BaseInputSpec): position=-3, desc="input image", ) - out_file = File( - argstr="%s", mandatory=True, position=-1, desc="output image" - ) + out_file = File(argstr="%s", mandatory=True, position=-1, desc="output image") operation = traits.Enum( "mean", "median", @@ -1164,9 +1155,7 @@ class SHConvInputSpec(CommandLineInputSpec): class SHConvOutputSpec(TraitedSpec): - out_file = File( - exists=True, desc="the output convoluted spherical harmonics file" - ) + out_file = File(exists=True, desc="the output convoluted spherical harmonics file") class SHConv(CommandLine): @@ -1230,9 +1219,7 @@ class SH2AmpInputSpec(CommandLineInputSpec): class SH2AmpOutputSpec(TraitedSpec): - out_file = File( - exists=True, desc="the output convoluted spherical harmonics file" - ) + out_file = File(exists=True, desc="the output convoluted spherical harmonics file") class SH2Amp(CommandLine): From 1413f303979ed275d74d732b3cb2bca329df954e Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Wed, 22 Dec 2021 20:53:13 +0200 Subject: [PATCH 16/22] Tried changing file names to mif files in other doctests. --- nipype/interfaces/mrtrix3/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 8d6da8f930..9a02d5045e 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -144,9 +144,9 @@ class MRCat(CommandLine): >>> import nipype.interfaces.mrtrix3 as mrt >>> mrcat = mrt.MRCat() - >>> mrcat.inputs.in_files = ['image1.mif','image2.mif'] + >>> mrcat.inputs.in_files = ['dwi.mif','mask.mif'] >>> mrcat.cmdline # doctest: +ELLIPSIS - 'mrcat image1.mif image2.mif concatenated.mif' + 'mrcat dwi.mif mask.mif concatenated.mif' >>> mrcat.run() # doctest: +SKIP """ From 9414af876a375e9145039df1db59c0fd4f1eacd3 Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Wed, 22 Dec 2021 20:58:18 +0200 Subject: [PATCH 17/22] Reverted DWIPreprocInputSpec.out_grad_mrtrix default value change. --- nipype/interfaces/mrtrix3/preprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/interfaces/mrtrix3/preprocess.py b/nipype/interfaces/mrtrix3/preprocess.py index feeff4e527..1fcb2ce36a 100644 --- a/nipype/interfaces/mrtrix3/preprocess.py +++ b/nipype/interfaces/mrtrix3/preprocess.py @@ -317,7 +317,7 @@ class DWIPreprocInputSpec(MRTrix3BaseInputSpec): out_grad_mrtrix = File( "grad.b", argstr="%s", - usedefault=False, + usedefault=True, requires=["export_grad_mrtrix"], desc="name of new gradient file", ) From ed6b685cfc5672e4d7734fadc27445bcc2b01d7c Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Wed, 22 Dec 2021 22:33:09 +0200 Subject: [PATCH 18/22] Reverted test value change. --- nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py b/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py index b289b9c8f3..7f226fe3cd 100644 --- a/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py +++ b/nipype/interfaces/mrtrix3/tests/test_auto_DWIPreproc.py @@ -115,7 +115,7 @@ def test_DWIPreproc_outputs(): out_grad_mrtrix=dict( argstr="%s", extensions=None, - usedefault=False, + usedefault=True, ), ) outputs = DWIPreproc.output_spec() From 054e0b1d70bf911ca6a8b442cd9c68f1bdd92a30 Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Wed, 22 Dec 2021 22:37:27 +0200 Subject: [PATCH 19/22] Removed TransformFSLConvert example to avoid doctest failure due to missing required file paths. --- nipype/interfaces/mrtrix3/utils.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/nipype/interfaces/mrtrix3/utils.py b/nipype/interfaces/mrtrix3/utils.py index 9a02d5045e..193ebb10fe 100644 --- a/nipype/interfaces/mrtrix3/utils.py +++ b/nipype/interfaces/mrtrix3/utils.py @@ -833,19 +833,6 @@ class TransformFSLConvertOutputSpec(TraitedSpec): class TransformFSLConvert(MRTrix3Base): """ Perform conversion between FSL's transformation matrix format to mrtrix3's. - - Example - ------- - - >>> import nipype.interfaces.mrtrix3 as mrt - >>> transform = mrt.TransformFSLConvert() - >>> transform.inputs.in_file = 'flirt_in.nii.gz' - >>> transform.inputs.reference = 'flirt_ref.nii.gz' - >>> transform.inputs.in_transform = 'transform_flirt.mat' - >>> transform.inputs.out_transform = 'transform_mrtrix.txt' - >>> transform.cmdline # doctest: +ELLIPSIS - 'transformconvert transform_flirt.mat flirt_in.nii flirt_ref.nii flirt_import transform_mrtrix.txt' - >>> transform.run() # doctest: +SKIP """ _cmd = "transformconvert" From 947bef211a194b06385bedf6a4faa38ac4a481a7 Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Thu, 23 Dec 2021 09:23:51 +0200 Subject: [PATCH 20/22] Tried manually generating the missing automatic test modules. --- .../mrtrix3/tests/test_auto_MRTransform.py | 113 ++++++++++++++++++ .../tests/test_auto_TransformFSLConvert.py | 93 ++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 nipype/interfaces/mrtrix3/tests/test_auto_MRTransform.py create mode 100644 nipype/interfaces/mrtrix3/tests/test_auto_TransformFSLConvert.py diff --git a/nipype/interfaces/mrtrix3/tests/test_auto_MRTransform.py b/nipype/interfaces/mrtrix3/tests/test_auto_MRTransform.py new file mode 100644 index 0000000000..b50ee2c67f --- /dev/null +++ b/nipype/interfaces/mrtrix3/tests/test_auto_MRTransform.py @@ -0,0 +1,113 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from ..utils import MRTransform + + +def test_MRTransform_inputs(): + input_map = dict( + args=dict( + argstr="%s", + ), + bval_scale=dict( + argstr="-bvalue_scaling %s", + ), + debug=dict( + argstr="-debug", + position=1, + ), + environ=dict( + nohash=True, + usedefault=True, + ), + flip_x=dict( + argstr="-flipx", + position=1, + ), + 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, + ), + invert=dict( + argstr="-inverse", + position=1, + ), + linear_transform=dict( + argstr="-linear %s", + extensions=None, + position=1, + ), + nthreads=dict( + argstr="-nthreads %d", + nohash=True, + ), + out_bval=dict( + extensions=None, + ), + out_bvec=dict( + argstr="-export_grad_fsl %s %s", + extensions=None, + ), + out_file=dict( + argstr="%s", + extensions=None, + genfile=True, + position=-1, + ), + quiet=dict( + argstr="-quiet", + position=1, + ), + reference_image=dict( + argstr="-reference %s", + extensions=None, + position=1, + ), + replace_transform=dict( + argstr="-replace", + position=1, + ), + template_image=dict( + argstr="-template %s", + extensions=None, + position=1, + ), + transformation_file=dict( + argstr="-transform %s", + extensions=None, + position=1, + ), + ) + inputs = MRTransform.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_MRTransform_outputs(): + output_map = dict( + out_file=dict( + extensions=None, + ), + ) + outputs = MRTransform.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value diff --git a/nipype/interfaces/mrtrix3/tests/test_auto_TransformFSLConvert.py b/nipype/interfaces/mrtrix3/tests/test_auto_TransformFSLConvert.py new file mode 100644 index 0000000000..48a528a75e --- /dev/null +++ b/nipype/interfaces/mrtrix3/tests/test_auto_TransformFSLConvert.py @@ -0,0 +1,93 @@ +# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT +from ..utils import TransformFSLConvert + + +def test_TransformFSLConvert_inputs(): + input_map = dict( + args=dict( + argstr="%s", + ), + bval_scale=dict( + argstr="-bvalue_scaling %s", + ), + environ=dict( + nohash=True, + usedefault=True, + ), + flirt_import=dict( + argstr="flirt_import", + mandatory=True, + position=-2, + 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_file=dict( + argstr="%s", + extensions=None, + mandatory=True, + position=1, + ), + in_transform=dict( + argstr="%s", + extensions=None, + mandatory=True, + position=0, + ), + nthreads=dict( + argstr="-nthreads %d", + nohash=True, + ), + out_bval=dict( + extensions=None, + ), + out_bvec=dict( + argstr="-export_grad_fsl %s %s", + extensions=None, + ), + out_transform=dict( + argstr="%s", + extensions=None, + mandatory=True, + position=-1, + usedefault=True, + ), + reference=dict( + argstr="%s", + extensions=None, + mandatory=True, + position=2, + ), + ) + inputs = TransformFSLConvert.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_TransformFSLConvert_outputs(): + output_map = dict( + out_transform=dict( + extensions=None, + ), + ) + outputs = TransformFSLConvert.output_spec() + + for key, metadata in list(output_map.items()): + for metakey, value in list(metadata.items()): + assert getattr(outputs.traits()[key], metakey) == value From 3782d28e846c34bb97749c3724933b3a76f7ad06 Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Thu, 23 Dec 2021 09:32:15 +0200 Subject: [PATCH 21/22] Re-ran formatting. --- nipype/interfaces/mrtrix/preprocess.py | 56 +++++++------------------- nipype/interfaces/mrtrix3/base.py | 10 ++--- 2 files changed, 17 insertions(+), 49 deletions(-) diff --git a/nipype/interfaces/mrtrix/preprocess.py b/nipype/interfaces/mrtrix/preprocess.py index 14d549c530..64456460f8 100644 --- a/nipype/interfaces/mrtrix/preprocess.py +++ b/nipype/interfaces/mrtrix/preprocess.py @@ -24,9 +24,7 @@ class MRConvertInputSpec(CommandLineInputSpec): position=-2, desc="voxel-order data filename", ) - out_filename = File( - genfile=True, argstr="%s", position=-1, desc="Output filename" - ) + out_filename = File(genfile=True, argstr="%s", position=-1, desc="Output filename") extract_at_axis = traits.Enum( 1, 2, @@ -229,15 +227,11 @@ class DWI2TensorInputSpec(CommandLineInputSpec): position=1, desc=("Do not display information messages or progress " "status."), ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class DWI2TensorOutputSpec(TraitedSpec): - tensor = File( - exists=True, desc="path/name of output diffusion tensor image" - ) + tensor = File(exists=True, desc="path/name of output diffusion tensor image") class DWI2Tensor(CommandLine): @@ -277,9 +271,7 @@ class Tensor2VectorInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class Tensor2VectorOutputSpec(TraitedSpec): @@ -345,9 +337,7 @@ class Tensor2FractionalAnisotropyInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class Tensor2FractionalAnisotropyOutputSpec(TraitedSpec): @@ -413,9 +403,7 @@ class Tensor2ApparentDiffusionInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class Tensor2ApparentDiffusionOutputSpec(TraitedSpec): @@ -478,9 +466,7 @@ class MRMultiplyInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class MRMultiplyOutputSpec(TraitedSpec): @@ -537,9 +523,7 @@ class MRTrixViewerInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class MRTrixViewerOutputSpec(TraitedSpec): @@ -698,9 +682,7 @@ class ErodeInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class ErodeOutputSpec(TraitedSpec): @@ -765,9 +747,7 @@ class ThresholdInputSpec(CommandLineInputSpec): argstr="-percent %s", desc="Specify threshold value as a percentage of the peak intensity in the input image.", ) - invert = traits.Bool( - argstr="-invert", position=1, desc="Invert output binary mask" - ) + invert = traits.Bool(argstr="-invert", position=1, desc="Invert output binary mask") replace_zeros_with_NaN = traits.Bool( argstr="-nan", position=1, desc="Replace all zero values with NaN" ) @@ -776,9 +756,7 @@ class ThresholdInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class ThresholdOutputSpec(TraitedSpec): @@ -843,9 +821,7 @@ class MedianFilter3DInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class MedianFilter3DOutputSpec(TraitedSpec): @@ -897,9 +873,7 @@ class MRTransformInputSpec(CommandLineInputSpec): position=-2, desc="Input images to be transformed", ) - out_filename = File( - genfile=True, argstr="%s", position=-1, desc="Output image" - ) + out_filename = File(genfile=True, argstr="%s", position=-1, desc="Output image") invert = traits.Bool( argstr="-inverse", position=1, @@ -949,9 +923,7 @@ class MRTransformInputSpec(CommandLineInputSpec): position=1, desc="Do not display information messages or progress status.", ) - debug = traits.Bool( - argstr="-debug", position=1, desc="Display debugging messages." - ) + debug = traits.Bool(argstr="-debug", position=1, desc="Display debugging messages.") class MRTransformOutputSpec(TraitedSpec): diff --git a/nipype/interfaces/mrtrix3/base.py b/nipype/interfaces/mrtrix3/base.py index 3ab76e881d..1a4af955fb 100644 --- a/nipype/interfaces/mrtrix3/base.py +++ b/nipype/interfaces/mrtrix3/base.py @@ -46,8 +46,7 @@ def looseversion(cls): class MRTrix3BaseInputSpec(CommandLineInputSpec): nthreads = traits.Int( argstr="-nthreads %d", - desc="number of threads. if zero, the number" - " of available cpus will be used", + desc="number of threads. if zero, the number" " of available cpus will be used", nohash=True, ) # DW gradient table import options @@ -116,9 +115,7 @@ def _parse_inputs(self, skip=None): skip = [] try: - if isdefined(self.inputs.grad_file) or isdefined( - self.inputs.grad_fsl - ): + if isdefined(self.inputs.grad_file) or isdefined(self.inputs.grad_fsl): skip += ["in_bvec", "in_bval"] is_bvec = isdefined(self.inputs.in_bvec) @@ -126,8 +123,7 @@ def _parse_inputs(self, skip=None): if is_bvec or is_bval: if not is_bvec or not is_bval: raise RuntimeError( - "If using bvecs and bvals inputs, both" - "should be defined" + "If using bvecs and bvals inputs, both" "should be defined" ) skip += ["in_bval"] except AttributeError: From fd470a5d00012926dd382bc82df4c4fa88c7250b Mon Sep 17 00:00:00 2001 From: Zvi Baratz Date: Thu, 23 Dec 2021 10:27:20 +0200 Subject: [PATCH 22/22] Reinstalled dev env requirements and triggered a commit to try and fix formatting changes. --- tools/checkspecs.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/checkspecs.py b/tools/checkspecs.py index 7aaac0d107..f57502aa42 100644 --- a/tools/checkspecs.py +++ b/tools/checkspecs.py @@ -112,14 +112,14 @@ def _uri2path(self, uri): return path def _path2uri(self, dirpath): - """Convert directory path to uri""" + """ Convert directory path to uri """ relpath = dirpath.replace(self.root_path, self.package_name) if relpath.startswith(os.path.sep): relpath = relpath[1:] return relpath.replace(os.path.sep, ".") def _parse_module(self, uri): - """Parse module defined in *uri*""" + """ Parse module defined in *uri* """ filename = self._uri2path(uri) if filename is None: # nothing that we could handle here. @@ -130,7 +130,7 @@ def _parse_module(self, uri): return functions, classes def _parse_lines(self, linesource, module): - """Parse lines of text for functions and classes""" + """ Parse lines of text for functions and classes """ functions = [] classes = [] for line in linesource: