Skip to content

ENH: json-related entities in MRConvert's inputs specification #3401

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions doc/devel/matlab_example1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from nipype.interfaces.matlab import MatlabCommand
from nipype.interfaces.base import TraitedSpec, \
BaseInterface, BaseInterfaceInputSpec, File
from nipype.interfaces.base import (
TraitedSpec,
BaseInterface,
BaseInterfaceInputSpec,
File,
)
import os
from string import Template


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


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

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

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

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


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


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

input_spec = HelloWorldInputSpec
output_spec = HelloWorldOutputSpec

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

def run(self, **inputs):
Expand Down
12 changes: 9 additions & 3 deletions nipype/interfaces/mrtrix/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions nipype/interfaces/mrtrix3/tests/test_auto_MRConvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
),
Expand Down
96 changes: 82 additions & 14 deletions nipype/interfaces/mrtrix3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,7 +138,11 @@ 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")

Expand Down Expand Up @@ -173,7 +181,11 @@ 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")
Expand Down Expand Up @@ -246,10 +258,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,
Expand Down Expand Up @@ -353,7 +373,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(
Expand Down Expand Up @@ -438,10 +459,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,
Expand Down Expand Up @@ -498,7 +527,11 @@ 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")
bzero = traits.Bool(argstr="-bzero", desc="extract b=0 volumes")
Expand Down Expand Up @@ -549,7 +582,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",
Expand All @@ -566,7 +603,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,
Expand All @@ -580,10 +620,26 @@ 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",
)
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):
Expand All @@ -610,12 +666,18 @@ class MRConvert(MRTrix3Base):
def _list_outputs(self):
outputs = self.output_spec().get()
outputs["out_file"] = op.abspath(self.inputs.out_file)
if self.inputs.json_export:
outputs["json_export"] = op.abspath(self.inputs.json_export)
return outputs


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")
operation = traits.Enum(
Expand All @@ -637,7 +699,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",
)


Expand Down Expand Up @@ -677,7 +741,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),
Expand Down
15 changes: 7 additions & 8 deletions tools/checkspecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
----------
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading