Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into enh/circleci-neur…
Browse files Browse the repository at this point in the history
…odocker
  • Loading branch information
jakubk committed Nov 13, 2017
2 parents 399fe16 + ca91455 commit 126831e
Show file tree
Hide file tree
Showing 127 changed files with 994 additions and 987 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Upcoming release (0.14.0)
================

* FIX: Testing maintainance and improvements (https://github.com/nipy/nipype/pull/2252)
* ENH: Add elapsed_time and final metric_value to ants.Registration (https://github.com/nipy/nipype/pull/1985)
* ENH: Improve terminal_output feature (https://github.com/nipy/nipype/pull/2209)
* ENH: Simple interface to FSL std2imgcoords (https://github.com/nipy/nipype/pull/2209, prev #1398)
Expand Down
12 changes: 12 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
import numpy, os

@pytest.fixture(autouse=True)
def add_np(doctest_namespace):
doctest_namespace['np'] = numpy
doctest_namespace['os'] = os


filepath = os.path.dirname(os.path.realpath(__file__))
datadir = os.path.realpath(os.path.join(filepath, 'nipype/testing/data'))
doctest_namespace["datadir"] = datadir
13 changes: 7 additions & 6 deletions nipype/algorithms/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ..utils import NUMPY_MMAP
from ..utils.misc import normalize_mc_params

IFLOG = logging.getLogger('interface')
IFLOGGER = logging.getLogger('interface')


class ComputeDVARSInputSpec(BaseInterfaceInputSpec):
Expand Down Expand Up @@ -286,7 +286,7 @@ def _run_interface(self, runtime):
tr = self.inputs.series_tr

if self.inputs.normalize and tr is None:
IFLOG.warn('FD plot cannot be normalized if TR is not set')
IFLOGGER.warn('FD plot cannot be normalized if TR is not set')

self._results['out_figure'] = op.abspath(self.inputs.out_figure)
fig = plot_confound(fd_res, self.inputs.figsize, 'FD', units='mm',
Expand Down Expand Up @@ -601,8 +601,8 @@ def _process_masks(self, mask_images, timeseries=None):
# save mask
mask_file = os.path.abspath('mask_{:03d}.nii.gz'.format(i))
out_image.to_filename(mask_file)
IFLOG.debug('tCompcor computed and saved mask of shape {} to '
'mask_file {}'.format(mask.shape, mask_file))
IFLOGGER.debug('tCompcor computed and saved mask of shape %s to '
'mask_file %s', str(mask.shape), mask_file)
self._mask_files.append(mask_file)
out_images.append(out_image)
return out_images
Expand Down Expand Up @@ -919,7 +919,8 @@ def regress_poly(degree, data, remove_mean=True, axis=-1):
:param int axis: numpy array axes along which regression is performed
"""
IFLOG.debug('Performing polynomial regression on data of shape ' + str(data.shape))
IFLOGGER.debug('Performing polynomial regression on data of shape %s',
str(data.shape))

datashape = data.shape
timepoints = datashape[axis]
Expand Down Expand Up @@ -1147,7 +1148,7 @@ def _full_rank(X, cmax=1e15):
c = smax / smin
if c < cmax:
return X, c
IFLOG.warn('Matrix is singular at working precision, regularizing...')
IFLOGGER.warn('Matrix is singular at working precision, regularizing...')
lda = (smax - cmax * smin) / (cmax - 1)
s = s + lda
X = np.dot(U, np.dot(np.diag(s), V))
Expand Down
42 changes: 18 additions & 24 deletions nipype/algorithms/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,26 +362,23 @@ def _run_interface(self, runtime):
if isinstance(in_dict[key][0], np.ndarray):
saved_variables.append(key)
else:
iflogger.info('One of the keys in the input file, {k}, is not a Numpy array'.format(k=key))
iflogger.info('One of the keys in the input file, %s, is '
'not a Numpy array', key)

if len(saved_variables) > 1:
iflogger.info(
'{N} variables found:'.format(N=len(saved_variables)))
iflogger.info('%i variables found:', len(saved_variables))
iflogger.info(saved_variables)
for variable in saved_variables:
iflogger.info(
'...Converting {var} - type {ty} - to\
CSV'.format(var=variable, ty=type(in_dict[variable]))
)
matlab2csv(
in_dict[variable], variable, self.inputs.reshape_matrix)
iflogger.info('...Converting %s - type %s - to CSV',
variable, type(in_dict[variable]))
matlab2csv(in_dict[variable], variable, self.inputs.reshape_matrix)
elif len(saved_variables) == 1:
_, name, _ = split_filename(self.inputs.in_file)
variable = saved_variables[0]
iflogger.info('Single variable found {var}, type {ty}:'.format(
var=variable, ty=type(in_dict[variable])))
iflogger.info('...Converting {var} to CSV from {f}'.format(
var=variable, f=self.inputs.in_file))
iflogger.info('Single variable found %s, type %s:', variable,
type(in_dict[variable]))
iflogger.info('...Converting %s to CSV from %s', variable,
self.inputs.in_file)
matlab2csv(in_dict[variable], name, self.inputs.reshape_matrix)
else:
iflogger.error('No values in the MATLAB file?!')
Expand All @@ -396,8 +393,8 @@ def _list_outputs(self):
if isinstance(in_dict[key][0], np.ndarray):
saved_variables.append(key)
else:
iflogger.error('One of the keys in the input file, {k}, is\
not a Numpy array'.format(k=key))
iflogger.error('One of the keys in the input file, %s, is '
'not a Numpy array', key)

if len(saved_variables) > 1:
outputs['csv_files'] = replaceext(saved_variables, '.csv')
Expand Down Expand Up @@ -555,28 +552,25 @@ def _run_interface(self, runtime):
iflogger.info('Column headings have been provided:')
headings = self.inputs.column_headings
else:
iflogger.info(
'Column headings not provided! Pulled from input filenames:')
iflogger.info('Column headings not provided! Pulled from input filenames:')
headings = remove_identical_paths(self.inputs.in_files)

if isdefined(self.inputs.extra_field):
if isdefined(self.inputs.extra_column_heading):
extraheading = self.inputs.extra_column_heading
iflogger.info('Extra column heading provided: {col}'.format(
col=extraheading))
iflogger.info('Extra column heading provided: %s', extraheading)
else:
extraheading = 'type'
iflogger.info(
'Extra column heading was not defined. Using "type"')
iflogger.info('Extra column heading was not defined. Using "type"')
headings.append(extraheading)
extraheadingBool = True

if len(self.inputs.in_files) == 1:
iflogger.warn('Only one file input!')

if isdefined(self.inputs.row_headings):
iflogger.info('Row headings have been provided. Adding "labels"\
column header.')
iflogger.info('Row headings have been provided. Adding "labels"'
'column header.')
prefix = '"{p}","'.format(p=self.inputs.row_heading_title)
csv_headings = prefix + '","'.join(itertools.chain(
headings)) + '"\n'
Expand Down Expand Up @@ -1310,7 +1304,7 @@ def merge_rois(in_files, in_idxs, in_ref,
# to avoid memory errors
if op.splitext(in_ref)[1] == '.gz':
try:
iflogger.info('uncompress %i' % in_ref)
iflogger.info('uncompress %i', in_ref)
sp.check_call(['gunzip', in_ref], stdout=sp.PIPE, shell=True)
in_ref = op.splitext(in_ref)[0]
except:
Expand Down
14 changes: 7 additions & 7 deletions nipype/algorithms/modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ def _generate_standard_design(self, infolist, functional_runs=None,
for f in filename_to_list(sessinfo[i]['scans']):
shape = load(f, mmap=NUMPY_MMAP).shape
if len(shape) == 3 or shape[3] == 1:
iflogger.warning(('You are using 3D instead of 4D '
'files. Are you sure this was '
'intended?'))
iflogger.warning('You are using 3D instead of 4D '
'files. Are you sure this was '
'intended?')
numscans += 1
else:
numscans += shape[3]
Expand Down Expand Up @@ -686,7 +686,7 @@ def _gen_regress(self, i_onsets, i_durations, i_amplitudes, nscans):

if dt < 1:
raise Exception('Time multiple less than 1 ms')
iflogger.info('Setting dt = %d ms\n' % dt)
iflogger.info('Setting dt = %d ms\n', dt)
npts = int(np.ceil(total_time / dt))
times = np.arange(0, total_time, dt) * 1e-3
timeline = np.zeros((npts))
Expand All @@ -705,9 +705,9 @@ def _gen_regress(self, i_onsets, i_durations, i_amplitudes, nscans):
if isdefined(self.inputs.model_hrf) and self.inputs.model_hrf:
response = np.convolve(boxcar, hrf)
reg_scale = 1.0 / response.max()
iflogger.info('response sum: %.4f max: %.4f' % (response.sum(),
response.max()))
iflogger.info('reg_scale: %.4f' % reg_scale)
iflogger.info('response sum: %.4f max: %.4f', response.sum(),
response.max())
iflogger.info('reg_scale: %.4f', reg_scale)

for i, t in enumerate(onsets):
idx = int(np.round(t / dt))
Expand Down
3 changes: 1 addition & 2 deletions nipype/algorithms/tests/test_compcor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class TestCompCor():
@pytest.fixture(autouse=True)
def setup_class(self, tmpdir):
# setup
self.temp_dir = str(tmpdir)
os.chdir(self.temp_dir)
tmpdir.chdir()
noise = np.fromfunction(self.fake_noise_fun, self.fake_data.shape)
self.realigned_file = utils.save_toy_nii(self.fake_data + noise,
self.filenames['functionalnii'])
Expand Down
7 changes: 4 additions & 3 deletions nipype/algorithms/tests/test_confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def test_fd(tmpdir):
tempdir = str(tmpdir)
tempdir = tmpdir.strpath
ground_truth = np.loadtxt(example_data('fsl_motion_outliers_fd.txt'))
fdisplacement = FramewiseDisplacement(in_file=example_data('fsl_mcflirt_movpar.txt'),
out_file=tempdir + '/fd.txt',
Expand All @@ -43,7 +43,7 @@ def test_dvars(tmpdir):
in_mask=example_data('ds003_sub-01_mc_brainmask.nii.gz'),
save_all=True,
intensity_normalization=0)
os.chdir(str(tmpdir))
tmpdir.chdir()
res = dvars.run()

dv1 = np.loadtxt(res.outputs.out_all, skiprows=1)
Expand All @@ -66,7 +66,8 @@ def test_dvars(tmpdir):

assert (np.abs(dv1[:, 2] - ground_truth[:, 2]).sum() / len(dv1)) < 0.05

def test_outliers(tmpdir):

def test_outliers():
np.random.seed(0)
in_data = np.random.randn(100)
in_data[0] += 10
Expand Down
23 changes: 11 additions & 12 deletions nipype/algorithms/tests/test_errormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

def test_errormap(tmpdir):

tempdir = str(tmpdir)
# Single-Spectual
# Make two fake 2*2*2 voxel volumes
volume1 = np.array([[[2.0, 8.0], [1.0, 2.0]], [[1.0, 9.0], [0.0, 3.0]]]) # John von Neumann's birthday
Expand All @@ -22,15 +21,15 @@ def test_errormap(tmpdir):
img2 = nb.Nifti1Image(volume2, np.eye(4))
maskimg = nb.Nifti1Image(mask, np.eye(4))

nb.save(img1, os.path.join(tempdir, 'von.nii.gz'))
nb.save(img2, os.path.join(tempdir, 'alan.nii.gz'))
nb.save(maskimg, os.path.join(tempdir, 'mask.nii.gz'))
nb.save(img1, tmpdir.join('von.nii.gz').strpath)
nb.save(img2, tmpdir.join('alan.nii.gz').strpath)
nb.save(maskimg, tmpdir.join('mask.nii.gz').strpath)

# Default metric
errmap = ErrorMap()
errmap.inputs.in_tst = os.path.join(tempdir, 'von.nii.gz')
errmap.inputs.in_ref = os.path.join(tempdir, 'alan.nii.gz')
errmap.out_map = os.path.join(tempdir, 'out_map.nii.gz')
errmap.inputs.in_tst = tmpdir.join('von.nii.gz').strpath
errmap.inputs.in_ref = tmpdir.join('alan.nii.gz').strpath
errmap.out_map = tmpdir.join('out_map.nii.gz').strpath
result = errmap.run()
assert result.outputs.distance == 1.125

Expand All @@ -45,7 +44,7 @@ def test_errormap(tmpdir):
assert result.outputs.distance == 0.875

# Masked
errmap.inputs.mask = os.path.join(tempdir, 'mask.nii.gz')
errmap.inputs.mask = tmpdir.join('mask.nii.gz').strpath
result = errmap.run()
assert result.outputs.distance == 1.0

Expand All @@ -62,11 +61,11 @@ def test_errormap(tmpdir):
msvolume2[:, :, :, 1] = volume1
msimg2 = nb.Nifti1Image(msvolume2, np.eye(4))

nb.save(msimg1, os.path.join(tempdir, 'von-ray.nii.gz'))
nb.save(msimg2, os.path.join(tempdir, 'alan-ray.nii.gz'))
nb.save(msimg1, tmpdir.join('von-ray.nii.gz').strpath)
nb.save(msimg2, tmpdir.join('alan-ray.nii.gz').strpath)

errmap.inputs.in_tst = os.path.join(tempdir, 'von-ray.nii.gz')
errmap.inputs.in_ref = os.path.join(tempdir, 'alan-ray.nii.gz')
errmap.inputs.in_tst = tmpdir.join('von-ray.nii.gz').strpath
errmap.inputs.in_ref = tmpdir.join('alan-ray.nii.gz').strpath
errmap.inputs.metric = 'sqeuclidean'
result = errmap.run()
assert result.outputs.distance == 5.5
Expand Down
15 changes: 7 additions & 8 deletions nipype/algorithms/tests/test_mesh_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@

@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
def test_ident_distances(tmpdir):
tempdir = str(tmpdir)
os.chdir(tempdir)
tmpdir.chdir()

in_surf = example_data('surf01.vtk')
dist_ident = m.ComputeMeshWarp()
dist_ident.inputs.surface1 = in_surf
dist_ident.inputs.surface2 = in_surf
dist_ident.inputs.out_file = os.path.join(tempdir, 'distance.npy')
dist_ident.inputs.out_file = tmpdir.join('distance.npy')
res = dist_ident.run()
assert res.outputs.distance == 0.0

Expand All @@ -33,11 +32,11 @@ def test_ident_distances(tmpdir):

@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
def test_trans_distances(tmpdir):
tempdir = str(tmpdir)
tempdir = tmpdir.strpath
from ...interfaces.vtkbase import tvtk

in_surf = example_data('surf01.vtk')
warped_surf = os.path.join(tempdir, 'warped.vtk')
warped_surf = tmpdir.join('warped.vtk')

inc = np.array([0.7, 0.3, -0.2])

Expand All @@ -53,7 +52,7 @@ def test_trans_distances(tmpdir):
dist = m.ComputeMeshWarp()
dist.inputs.surface1 = in_surf
dist.inputs.surface2 = warped_surf
dist.inputs.out_file = os.path.join(tempdir, 'distance.npy')
dist.inputs.out_file = tmpdir.join('distance.npy')
res = dist.run()
assert np.allclose(res.outputs.distance, np.linalg.norm(inc), 4)
dist.inputs.weighting = 'area'
Expand All @@ -63,14 +62,14 @@ def test_trans_distances(tmpdir):

@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
def test_warppoints(tmpdir):
os.chdir(str(tmpdir))
tmpdir.chdir()

# TODO: include regression tests for when tvtk is installed


@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
def test_meshwarpmaths(tmpdir):
os.chdir(str(tmpdir))
tmpdir.chdir()

# TODO: include regression tests for when tvtk is installed

Expand Down
17 changes: 7 additions & 10 deletions nipype/algorithms/tests/test_modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@


def test_modelgen1(tmpdir):
tempdir = str(tmpdir)
filename1 = os.path.join(tempdir, 'test1.nii')
filename2 = os.path.join(tempdir, 'test2.nii')
filename1 = tmpdir.join('test1.nii').strpath
filename2 = tmpdir.join('test2.nii').strpath
Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1)
Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2)
s = SpecifyModel()
Expand Down Expand Up @@ -56,9 +55,8 @@ def test_modelgen1(tmpdir):


def test_modelgen_spm_concat(tmpdir):
tempdir = str(tmpdir)
filename1 = os.path.join(tempdir, 'test1.nii')
filename2 = os.path.join(tempdir, 'test2.nii')
filename1 = tmpdir.join('test1.nii').strpath
filename2 = tmpdir.join('test2.nii').strpath
Nifti1Image(np.random.rand(10, 10, 10, 30), np.eye(4)).to_filename(filename1)
Nifti1Image(np.random.rand(10, 10, 10, 30), np.eye(4)).to_filename(filename2)

Expand Down Expand Up @@ -97,7 +95,7 @@ def test_modelgen_spm_concat(tmpdir):
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0]))

# Test case for variable number of events in separate runs, sometimes unique.
filename3 = os.path.join(tempdir, 'test3.nii')
filename3 = tmpdir.join('test3.nii').strpath
Nifti1Image(np.random.rand(10, 10, 10, 30), np.eye(4)).to_filename(filename3)
s.inputs.functional_runs = [filename1, filename2, filename3]
info = [Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2]], durations=[[1, 1], [1]]),
Expand All @@ -122,9 +120,8 @@ def test_modelgen_spm_concat(tmpdir):


def test_modelgen_sparse(tmpdir):
tempdir = str(tmpdir)
filename1 = os.path.join(tempdir, 'test1.nii')
filename2 = os.path.join(tempdir, 'test2.nii')
filename1 = tmpdir.join('test1.nii').strpath
filename2 = tmpdir.join('test2.nii').strpath
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename1)
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename2)
s = SpecifySparseModel()
Expand Down
Loading

0 comments on commit 126831e

Please sign in to comment.