Skip to content
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

MAINT: Various minor improvements to complement previous PR #2964

Merged
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
108 changes: 36 additions & 72 deletions nipype/interfaces/afni/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
"""Provide interface to AFNI commands."""
from __future__ import (print_function, division, unicode_literals,
absolute_import)
from builtins import object, str
from future.utils import raise_from

import os
from sys import platform
from distutils import spawn

from ... import logging, LooseVersion
from ...utils.filemanip import split_filename, fname_presuffix

from ...utils.filemanip import split_filename
from ..base import (CommandLine, traits, CommandLineInputSpec, isdefined, File,
TraitedSpec, PackageInfo)
from ...external.due import BibTeX
Expand All @@ -23,14 +21,15 @@


class Info(PackageInfo):
"""Handle afni output type and version information.
"""
"""Handle afni output type and version information."""

__outputtype = 'AFNI'
ftypes = {'NIFTI': '.nii', 'AFNI': '', 'NIFTI_GZ': '.nii.gz'}
version_cmd = 'afni --version'

@staticmethod
def parse_version(raw_info):
"""Check and parse AFNI's version."""
version_stamp = raw_info.split('\n')[0].split('Version ')[1]
if version_stamp.startswith('AFNI'):
version_stamp = version_stamp.split('AFNI_')[1]
Expand All @@ -46,7 +45,8 @@ def parse_version(raw_info):

@classmethod
def output_type_to_ext(cls, outputtype):
"""Get the file extension for the given output type.
"""
Get the file extension for the given output type.

Parameters
----------
Expand All @@ -57,8 +57,8 @@ def output_type_to_ext(cls, outputtype):
-------
extension : str
The file extension for the output type.
"""

"""
try:
return cls.ftypes[outputtype]
except KeyError as e:
Expand All @@ -67,24 +67,28 @@ def output_type_to_ext(cls, outputtype):

@classmethod
def outputtype(cls):
"""AFNI has no environment variables,
Output filetypes get set in command line calls
Nipype uses AFNI as default
"""
Set default output filetype.

AFNI has no environment variables, Output filetypes get set in command line calls
Nipype uses ``AFNI`` as default


Returns
-------
None

"""
# warn(('AFNI has no environment variable that sets filetype '
# 'Nipype uses NIFTI_GZ as default'))
return 'AFNI'

@staticmethod
def standard_image(img_name):
'''Grab an image from the standard location.
"""
Grab an image from the standard location.

Could be made more fancy to allow for more relocatability'''
Could be made more fancy to allow for more relocatability

"""
clout = CommandLine(
'which afni',
ignore_exception=True,
Expand All @@ -101,6 +105,7 @@ def standard_image(img_name):
class AFNICommandBase(CommandLine):
"""
A base class to fix a linking problem in OSX and afni.

See http://afni.nimh.nih.gov/afni/community/board/read.php?1,145346,145347#msg-145347
"""

Expand All @@ -127,7 +132,8 @@ class AFNICommandOutputSpec(TraitedSpec):


class AFNICommand(AFNICommandBase):
"""Shared options for several AFNI commands """
"""Shared options for several AFNI commands."""

input_spec = AFNICommandInputSpec
_outputtype = None

Expand Down Expand Up @@ -162,6 +168,7 @@ class AFNICommand(AFNICommandBase):

@property
def num_threads(self):
"""Get number of threads."""
return self.inputs.num_threads

@num_threads.setter
Expand All @@ -170,20 +177,21 @@ def num_threads(self, value):

@classmethod
def set_default_output_type(cls, outputtype):
"""Set the default output type for AFNI classes.
"""
Set the default output type for AFNI classes.

This method is used to set the default output type for all afni
subclasses. However, setting this will not update the output
type for any existing instances. For these, assign the
<instance>.inputs.outputtype.
"""

if outputtype in Info.ftypes:
cls._outputtype = outputtype
else:
raise AttributeError('Invalid AFNI outputtype: %s' % outputtype)

def __init__(self, **inputs):
"""Instantiate an AFNI command tool wrapper."""
super(AFNICommand, self).__init__(**inputs)
self.inputs.on_trait_change(self._output_update, 'outputtype')

Expand All @@ -199,13 +207,16 @@ def __init__(self, **inputs):
self._output_update()

def _nthreads_update(self):
"""Update environment with new number of threads"""
"""Update environment with new number of threads."""
self.inputs.environ['OMP_NUM_THREADS'] = '%d' % self.inputs.num_threads

def _output_update(self):
""" i think? updates class private attribute based on instance input
in fsl also updates ENVIRON variable....not valid in afni
as it uses no environment variables
"""
Update the internal property with the provided input.

i think? updates class private attribute based on instance input
in fsl also updates ENVIRON variable....not valid in afni
as it uses no environment variables
"""
self._outputtype = self.inputs.outputtype

Expand All @@ -226,59 +237,9 @@ def _list_outputs(self):
outputs[name] = outputs[name] + "+orig.BRIK"
return outputs

def _gen_fname(self,
basename,
cwd=None,
suffix=None,
change_ext=True,
ext=None):
"""Generate a filename based on the given parameters.

The filename will take the form: cwd/basename<suffix><ext>.
If change_ext is True, it will use the extentions specified in
<instance>intputs.output_type.

Parameters
----------
basename : str
Filename to base the new filename on.
cwd : str
Path to prefix to the new filename. (default is os.getcwd())
suffix : str
Suffix to add to the `basename`. (defaults is '' )
change_ext : bool
Flag to change the filename extension to the FSL output type.
(default True)

Returns
-------
fname : str
New filename based on given parameters.

"""

if basename == '':
msg = 'Unable to generate filename for command %s. ' % self.cmd
msg += 'basename is not set!'
raise ValueError(msg)
if cwd is None:
cwd = os.getcwd()
if ext is None:
ext = Info.output_type_to_ext(self.inputs.outputtype)
if change_ext:
if suffix:
suffix = ''.join((suffix, ext))
else:
suffix = ext
if suffix is None:
suffix = ''
fname = fname_presuffix(
basename, suffix=suffix, use_ext=False, newpath=cwd)
return fname


def no_afni():
""" Checks if AFNI is available """
"""Check whether AFNI is not available."""
if Info.version() is None:
return True
return False
Expand All @@ -292,8 +253,11 @@ class AFNIPythonCommandInputSpec(CommandLineInputSpec):


class AFNIPythonCommand(AFNICommand):
"""A subtype of AFNI command line for Python scripts."""

@property
def cmd(self):
"""Revise the command path."""
orig_cmd = super(AFNIPythonCommand, self).cmd
found = spawn.find_executable(orig_cmd)
return found if found is not None else orig_cmd
Expand Down
54 changes: 53 additions & 1 deletion nipype/interfaces/afni/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ...external.due import BibTeX

from .base import (AFNICommandBase, AFNICommand, AFNICommandInputSpec,
AFNICommandOutputSpec)
AFNICommandOutputSpec, Info)


class DeconvolveInputSpec(AFNICommandInputSpec):
Expand Down Expand Up @@ -307,6 +307,58 @@ def _list_outputs(self):
return outputs


def _gen_fname(self,
basename,
cwd=None,
suffix=None,
change_ext=True,
ext=None):
"""Generate a filename based on the given parameters.

The filename will take the form: cwd/basename<suffix><ext>.
If change_ext is True, it will use the extentions specified in
<instance>intputs.output_type.

Parameters
----------
basename : str
Filename to base the new filename on.
cwd : str
Path to prefix to the new filename. (default is os.getcwd())
suffix : str
Suffix to add to the `basename`. (defaults is '' )
change_ext : bool
Flag to change the filename extension to the FSL output type.
(default True)

Returns
-------
fname : str
New filename based on given parameters.

"""
from nipype.utils.filemanip import fname_presuffix

if basename == '':
msg = 'Unable to generate filename for command %s. ' % self.cmd
msg += 'basename is not set!'
raise ValueError(msg)
if cwd is None:
cwd = os.getcwd()
if ext is None:
ext = Info.output_type_to_ext(self.inputs.outputtype)
if change_ext:
if suffix:
suffix = ''.join((suffix, ext))
else:
suffix = ext
if suffix is None:
suffix = ''
fname = fname_presuffix(
basename, suffix=suffix, use_ext=False, newpath=cwd)
return fname


class RemlfitInputSpec(AFNICommandInputSpec):
# mandatory files
in_files = InputMultiPath(
Expand Down
Loading