Skip to content

pytest cleaning #2252

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 16 commits into from
Nov 7, 2017
Merged
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
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
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
22 changes: 10 additions & 12 deletions nipype/algorithms/tests/test_moments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
import numpy as np
import tempfile
from nipype.algorithms.misc import calc_moments


def test_skew():
def test_skew(tmpdir):
data = """14.62418305 5.916396751 -1.658088086 4.71113546 1.598428608 5.612553811 -5.004056368 -4.057513911
11.16365251 17.32688599 -3.099920667 2.630189741 2.389709914 0.379332731 -0.2899694205 -4.363591482
2.059205599 23.90705054 0.7180462297 -1.976963652 7.487682025 -5.583986129 1.094800525 -2.319858134
Expand Down Expand Up @@ -126,13 +125,12 @@ def test_skew():
-0.5057854071 -2.415896554 -9.663571931 -5.714041661 -6.037933426 8.673756933 10.03557773 8.629816199
3.622185659 0.4716627142 -10.92515308 -3.705286841 -2.776089545 2.271920902 9.251504922 5.744980887
"""
with tempfile.NamedTemporaryFile(mode='w', delete=True) as f:
f.write(data)
f.flush()
skewness = calc_moments(f.name, 3)
assert np.allclose(skewness, np.array(
[-0.23418937314622, 0.2946365564954823, -0.05781002053540932,
-0.3512508282578762, -
0.07035664150233077, -
0.01935867699166935,
0.00483863369427428, 0.21879460029850167]))
f = tmpdir.join("filetest")
f.write(data)
skewness = calc_moments(f.strpath, 3)
assert np.allclose(skewness, np.array(
[-0.23418937314622, 0.2946365564954823, -0.05781002053540932,
-0.3512508282578762, -
0.07035664150233077, -
0.01935867699166935,
0.00483863369427428, 0.21879460029850167]))
5 changes: 2 additions & 3 deletions nipype/algorithms/tests/test_normalize_tpms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


def test_normalize_tpms(tmpdir):
tempdir = str(tmpdir)

in_mask = example_data('tpms_msk.nii.gz')
mskdata = nb.load(in_mask, mmap=NUMPY_MMAP).get_data()
Expand All @@ -30,8 +29,8 @@ def test_normalize_tpms(tmpdir):

for i in range(3):
mapname = example_data('tpm_%02d.nii.gz' % i)
filename = os.path.join(tempdir, 'modtpm_%02d.nii.gz' % i)
out_files.append(os.path.join(tempdir, 'normtpm_%02d.nii.gz' % i))
filename = tmpdir.join('modtpm_%02d.nii.gz' % i).strpath
out_files.append(tmpdir.join('normtpm_%02d.nii.gz' % i).strpath)

im = nb.load(mapname, mmap=NUMPY_MMAP)
data = im.get_data()
Expand Down
2 changes: 1 addition & 1 deletion nipype/algorithms/tests/test_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def check_close(val1, val2):
in1 = example_data('segmentation0.nii.gz')
in2 = example_data('segmentation1.nii.gz')

os.chdir(str(tmpdir))
tmpdir.chdir()
overlap = Overlap()
overlap.inputs.volume1 = in1
overlap.inputs.volume2 = in1
Expand Down
4 changes: 2 additions & 2 deletions nipype/algorithms/tests/test_splitmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def test_split_and_merge(tmpdir):
from nipype.algorithms.misc import split_rois, merge_rois

in_mask = example_data('tpms_msk.nii.gz')
dwfile = op.join(str(tmpdir), 'dwi.nii.gz')
dwfile = tmpdir.join('dwi.nii.gz').strpath
mskdata = nb.load(in_mask, mmap=NUMPY_MMAP).get_data()
aff = nb.load(in_mask, mmap=NUMPY_MMAP).affine

dwshape = (mskdata.shape[0], mskdata.shape[1], mskdata.shape[2], 6)
dwdata = np.random.normal(size=dwshape)
os.chdir(str(tmpdir))
tmpdir.chdir()
nb.Nifti1Image(dwdata.astype(np.float32),
aff, None).to_filename(dwfile)

Expand Down
3 changes: 1 addition & 2 deletions nipype/algorithms/tests/test_tsnr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class TestTSNR():
@pytest.fixture(autouse=True)
def setup_class(self, tmpdir):
# setup temp folder
self.temp_dir = str(tmpdir)
os.chdir(self.temp_dir)
tmpdir.chdir()

utils.save_toy_nii(self.fake_data, self.in_filenames['in_file'])

Expand Down
2 changes: 1 addition & 1 deletion nipype/caching/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_caching(tmpdir):
try:
# Prevent rerun to check that evaluation is computed only once
config.set('execution', 'stop_on_first_rerun', 'true')
mem = Memory(str(tmpdir))
mem = Memory(tmpdir.strpath)
first_nb_run = nb_runs
results = mem.cache(SideEffectInterface)(input1=2, input2=1)
assert nb_runs == first_nb_run + 1
Expand Down
6 changes: 3 additions & 3 deletions nipype/interfaces/afni/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Deconvolve(AFNICommand):
>>> deconvolve.inputs.stim_label = [(1, 'Houses')]
>>> deconvolve.inputs.gltsym = ['SYM: +Houses']
>>> deconvolve.inputs.glt_label = [(1, 'Houses')]
>>> deconvolve.cmdline # doctest: +ALLOW_UNICODE
>>> deconvolve.cmdline
"3dDeconvolve -input functional.nii functional2.nii -bucket output.nii -x1D output.1D -num_stimts 1 -stim_times 1 timeseries.txt 'SPMG1(4)' -stim_label 1 Houses -num_glt 1 -gltsym 'SYM: +Houses' -glt_label 1 Houses"
>>> res = deconvolve.run() # doctest: +SKIP
"""
Expand Down Expand Up @@ -574,7 +574,7 @@ class Remlfit(AFNICommand):
>>> remlfit.inputs.out_file = 'output.nii'
>>> remlfit.inputs.matrix = 'output.1D'
>>> remlfit.inputs.gltsym = [('SYM: +Lab1 -Lab2', 'TestSYM'), ('timeseries.txt', 'TestFile')]
>>> remlfit.cmdline # doctest: +ALLOW_UNICODE
>>> remlfit.cmdline
'3dREMLfit -gltsym "SYM: +Lab1 -Lab2" TestSYM -gltsym "timeseries.txt" TestFile -input "functional.nii functional2.nii" -matrix output.1D -Rbuck output.nii'
>>> res = remlfit.run() # doctest: +SKIP
"""
Expand Down Expand Up @@ -660,7 +660,7 @@ class Synthesize(AFNICommand):
>>> synthesize.inputs.cbucket = 'functional.nii'
>>> synthesize.inputs.matrix = 'output.1D'
>>> synthesize.inputs.select = ['baseline']
>>> synthesize.cmdline # doctest: +ALLOW_UNICODE
>>> synthesize.cmdline
'3dSynthesize -cbucket functional.nii -matrix output.1D -select baseline'
>>> syn = synthesize.run() # doctest: +SKIP
"""
Expand Down
Loading