Skip to content

ENH: AFNI (3d)LocalBistat interface #2590

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

Merged
merged 3 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions nipype/interfaces/afni/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .utils import (
ABoverlap, AFNItoNIFTI, Autobox, Axialize, BrickStat, Bucket, Calc, Cat,
CatMatvec, CenterMass, ConvertDset, Copy, Dot, Edge3, Eval, FWHMx,
MaskTool, Merge, Notes, NwarpApply, NwarpAdjust, NwarpCat, OneDToolPy,
Refit, Resample, TCat, TCatSubBrick, TStat, To3D, Unifize, Undump, ZCutUp,
GCOR, Zcat, Zeropad)
LocalBistat, MaskTool, Merge, Notes, NwarpApply, NwarpAdjust, NwarpCat,
OneDToolPy, Refit, Resample, TCat, TCatSubBrick, TStat, To3D, Unifize,
Undump, ZCutUp, GCOR, Zcat, Zeropad)
from .model import (Deconvolve, Remlfit, Synthesize)
64 changes: 64 additions & 0 deletions nipype/interfaces/afni/tests/test_auto_LocalBistat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
from __future__ import unicode_literals
from ..utils import LocalBistat


def test_LocalBistat_inputs():
input_map = dict(
args=dict(argstr='%s', ),
automask=dict(),
environ=dict(
nohash=True,
usedefault=True,
),
ignore_exception=dict(
deprecated='1.0.0',
nohash=True,
usedefault=True,
),
in_files=dict(
argstr='%s',
mandatory=True,
position=-1,
),
mask_file=dict(
argstr='-weight %s',
xor=['automask'],
),
neighborhood=dict(
argstr='-nbhd %s',
mandatory=True,
),
num_threads=dict(
nohash=True,
usedefault=True,
),
out_file=dict(
argstr='-prefix %s',
keep_extension=True,
name_source='in_files',
name_template='%s_bistat',
position=0,
),
outputtype=dict(),
stat=dict(
argstr='-stat %s',
mandatory=True,
),
terminal_output=dict(
deprecated='1.0.0',
nohash=True,
),
)
inputs = LocalBistat.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_LocalBistat_outputs():
output_map = dict(out_file=dict(), )
outputs = LocalBistat.output_spec()

for key, metadata in list(output_map.items()):
for metakey, value in list(metadata.items()):
assert getattr(outputs.traits()[key], metakey) == value
118 changes: 118 additions & 0 deletions nipype/interfaces/afni/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,124 @@ def _list_outputs(self):
return outputs


class LocalBistatInputSpec(AFNICommandInputSpec):
in_files = InputMultiPath(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be traits.List, not InputMultiPath.

And out of curiosity, is it more likely that people will want to pass in a list of two files or want separate names for each file (e.g. in_file1, in_file2)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be separate names are more suitable, you are right

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to split this into two traits, or leave it as one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I forgot this, I am doing it

File(exists=True),
minlen=2,
maxlen=2,
mandatory=True,
argstr='%s',
position=-1,
desc='Filenames of the 2 images to compute statistics between')
neighborhood = traits.Either(
traits.Tuple(traits.Enum('SPHERE', 'RHDD', 'TOHD'), traits.Float()),
traits.Tuple(traits.Enum('RECT'), traits.Tuple(traits.Float(),
traits.Float(),
traits.Float())),
mandatory=True,
desc='The region around each voxel that will be extracted for '
'the statistics calculation. Possible regions are: '
'\'SPHERE\', \'RHDD\' (rhombic dodecahedron), \'TOHD\' '
'(truncated octahedron) with a given radius in mm or '
'\'RECT\' (rectangular block) with dimensions to specify in mm.',
argstr='-nbhd %s')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

argstr='-nbhd %s(%s)'

Related to a comment below.

_stat_names = ['pearson', 'spearman', 'quadrant', 'mutinfo', 'normuti',
'jointent', 'hellinger', 'crU', 'crM', 'crA', 'L2slope',
'L1slope', 'num', 'ALL']
stat = traits.Either(
traits.Enum(*_stat_names), traits.List(traits.Enum(_stat_names)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not work?

stat = traits.MultiPath(traits.Enum(_stat_names), ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand: traits does not seem to have a `MultiPath

AttributeError: 'module' object has no attribute 'MultiPath'`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. I meant InputMultiPath, not traits.MultiPath. I don't know if I copy-pasted or actually wrote that out.

mandatory=True,
desc='statistics to compute. Possible names are :'
' * pearson = Pearson correlation coefficient'
' * spearman = Spearman correlation coefficient'
' * quadrant = Quadrant correlation coefficient'
' * mutinfo = Mutual Information'
' * normuti = Normalized Mutual Information'
' * jointent = Joint entropy'
' * hellinger= Hellinger metric'
' * crU = Correlation ratio (Unsymmetric)'
' * crM = Correlation ratio (symmetrized by Multiplication)'
' * crA = Correlation ratio (symmetrized by Addition)'
' * L2slope = slope of least-squares (L2) linear regression of '
' the data from dataset1 vs. the dataset2 '
' (i.e., d2 = a + b*d1 ==> this is \'b\')'
' * L1slope = slope of least-absolute-sum (L1) linear '
' regression of the data from dataset1 vs. '
' the dataset2'
' * num = number of the values in the region: '
' with the use of -mask or -automask, '
' the size of the region around any given '
' voxel will vary; this option lets you '
' map that size.'
' * ALL = all of the above, in that order'
'More than one option can be used.',
argstr='-stat %s')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try '-stat %s...'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it works, thanks !

mask_file = traits.File(
exists=True,
desc='mask image file name. Voxels NOT in the mask will not be used '
'in the neighborhood of any voxel. Also, a voxel NOT in the mask '
'will have its statistic(s) computed as zero (0).',
argstr='-mask %s')
automask = traits.Bool(
desc='Compute the mask as in program 3dAutomask.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an argstr here?

mask_file = traits.File(
exists=True,
desc='File name of an image to use as a weight. Only applies to '
'\'pearson\' statistics.',
argstr='-weight %s',
xor=['automask'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have two mask_file traits.

out_file = traits.File(
desc='Output dataset.',
argstr='-prefix %s',
name_source='in_files',
name_template='%s_bistat',
keep_extension=True,
position=0)


class LocalBistat(AFNICommand):
"""3dLocalBistat - computes statistics between 2 datasets, at each voxel,
based on a local neighborhood of that voxel.

For complete details, see the `3dLocalBistat Documentation.
<https://afni.nimh.nih.gov/pub../pub/dist/doc/program_help/3dLocalBistat.html>`_

Examples
========

>>> from nipype.interfaces import afni
>>> bistat = afni.LocalBistat()
>>> bistat.inputs.in_files = ['functional.nii', 'structural.nii']
>>> bistat.inputs.neighborhood = ('SPHERE', 1.2)
>>> bistat.inputs.stat = 'pearson'
>>> bistat.inputs.outputtype = 'NIFTI'
>>> bistat.cmdline
"3dLocalBistat -prefix functional_bistat.nii -nbhd 'SPHERE(1.2)' -stat pearson functional.nii structural.nii"
>>> res = automask.run() # doctest: +SKIP

"""

_cmd = '3dLocalBistat'
input_spec = LocalBistatInputSpec
output_spec = AFNICommandOutputSpec

def _format_arg(self, name, spec, value):
if name == 'neighborhood':
region_name, region_size = value
if region_name == 'RECT':
return spec.argstr % (
"'{0}({1})'".format(region_name, ','.join(region_size)))
else:
return spec.argstr % (
"'{0}({1})'".format(region_name, region_size))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the argstr='-nbhd %s(%s)' as suggested above, you could make this whole block:

if name == 'neighborhood' and value[0] == 'RECT':
    value = ('RECT', '%s,%s,%s' % value[1])

(Letting value fall through into the super call.)

if name == 'stat':
if isinstance(value, (str, bytes)):
return spec.argstr % value
else:
return ' '.join([spec.argstr % v for v in value])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure you can drop this if name == 'stat' block and get the same behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure you can drop this if name == 'stat' block and get the same behavior.

this doesn't seem to work: it doesn't repeat the option '-stat' before each value

bistat.inputs.stat = ['pearson', 'ALL']

gives -stat pearson ALL instead of -stat pearson -stat ALL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@salma1601 - if you change the argstr above to '-stat %s...' it will do what you are looking for.

return super(LocalBistat, self)._format_arg(name, spec, value)


class MaskToolInputSpec(AFNICommandInputSpec):
in_file = File(
desc='input file or files to 3dmask_tool',
Expand Down