Skip to content

Commit 7a721b8

Browse files
djareckamgxd
authored andcommitted
pytest cleaning (#2252)
* changing str(tmpdir) to tmpdir.strpath in tests * using tmpdir.join where its possible (nibabel doesnt like py.path.local) * using tmpdir.join().strpath in tests with nibabel or other funcions that dont take path.local * moving doctest ALLOW_UNICODE to pytest.ini, so doesnt have to be included in the docstrings * fixin one test * Revert "moving doctest ALLOW_UNICODE to pytest.ini, so doesnt have to be included in the docstrings" This reverts commit 952fdc9. My script removed to many things from doctest, will update it. * moving doctest ALLOW_UNICODE and NORMALIZE_WHITESPACE to pytest.ini, so doesnt have to be included in the docstrings * moving to temporary directories, so tests can be run in readonly; adding conftest.py with datadir variable and some basic libraries (so you dont have to import them in doctests) * removing almost all reminders of tempfile package, one test doesn work(TODO) * more cleaning: mostly moving to tmpdir.chdir and tmpdir.check * small fix * small fix * changing 4 more tests to avoid errors on read-only * changing crashfile dir in one test to avoid read-only problems * fixing the DataSink test so it doesnt go to recursive loop (problem related to tmpdir that is the same in fixtures and test functions, DataSInk was trying copy dir to subdir etc.) * adding testsetup directives to remove some parts from documentation
1 parent 557aad3 commit 7a721b8

File tree

101 files changed

+687
-701
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+687
-701
lines changed

conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
import numpy, os
3+
4+
@pytest.fixture(autouse=True)
5+
def add_np(doctest_namespace):
6+
doctest_namespace['np'] = numpy
7+
doctest_namespace['os'] = os
8+
9+
10+
filepath = os.path.dirname(os.path.realpath(__file__))
11+
datadir = os.path.realpath(os.path.join(filepath, 'nipype/testing/data'))
12+
doctest_namespace["datadir"] = datadir

nipype/algorithms/tests/test_compcor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class TestCompCor():
2121
@pytest.fixture(autouse=True)
2222
def setup_class(self, tmpdir):
2323
# setup
24-
self.temp_dir = str(tmpdir)
25-
os.chdir(self.temp_dir)
24+
tmpdir.chdir()
2625
noise = np.fromfunction(self.fake_noise_fun, self.fake_data.shape)
2726
self.realigned_file = utils.save_toy_nii(self.fake_data + noise,
2827
self.filenames['functionalnii'])

nipype/algorithms/tests/test_confounds.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
def test_fd(tmpdir):
23-
tempdir = str(tmpdir)
23+
tempdir = tmpdir.strpath
2424
ground_truth = np.loadtxt(example_data('fsl_motion_outliers_fd.txt'))
2525
fdisplacement = FramewiseDisplacement(in_file=example_data('fsl_mcflirt_movpar.txt'),
2626
out_file=tempdir + '/fd.txt',
@@ -43,7 +43,7 @@ def test_dvars(tmpdir):
4343
in_mask=example_data('ds003_sub-01_mc_brainmask.nii.gz'),
4444
save_all=True,
4545
intensity_normalization=0)
46-
os.chdir(str(tmpdir))
46+
tmpdir.chdir()
4747
res = dvars.run()
4848

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

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

69-
def test_outliers(tmpdir):
69+
70+
def test_outliers():
7071
np.random.seed(0)
7172
in_data = np.random.randn(100)
7273
in_data[0] += 10

nipype/algorithms/tests/test_errormap.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
def test_errormap(tmpdir):
1313

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

25-
nb.save(img1, os.path.join(tempdir, 'von.nii.gz'))
26-
nb.save(img2, os.path.join(tempdir, 'alan.nii.gz'))
27-
nb.save(maskimg, os.path.join(tempdir, 'mask.nii.gz'))
24+
nb.save(img1, tmpdir.join('von.nii.gz').strpath)
25+
nb.save(img2, tmpdir.join('alan.nii.gz').strpath)
26+
nb.save(maskimg, tmpdir.join('mask.nii.gz').strpath)
2827

2928
# Default metric
3029
errmap = ErrorMap()
31-
errmap.inputs.in_tst = os.path.join(tempdir, 'von.nii.gz')
32-
errmap.inputs.in_ref = os.path.join(tempdir, 'alan.nii.gz')
33-
errmap.out_map = os.path.join(tempdir, 'out_map.nii.gz')
30+
errmap.inputs.in_tst = tmpdir.join('von.nii.gz').strpath
31+
errmap.inputs.in_ref = tmpdir.join('alan.nii.gz').strpath
32+
errmap.out_map = tmpdir.join('out_map.nii.gz').strpath
3433
result = errmap.run()
3534
assert result.outputs.distance == 1.125
3635

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

4746
# Masked
48-
errmap.inputs.mask = os.path.join(tempdir, 'mask.nii.gz')
47+
errmap.inputs.mask = tmpdir.join('mask.nii.gz').strpath
4948
result = errmap.run()
5049
assert result.outputs.distance == 1.0
5150

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

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

68-
errmap.inputs.in_tst = os.path.join(tempdir, 'von-ray.nii.gz')
69-
errmap.inputs.in_ref = os.path.join(tempdir, 'alan-ray.nii.gz')
67+
errmap.inputs.in_tst = tmpdir.join('von-ray.nii.gz').strpath
68+
errmap.inputs.in_ref = tmpdir.join('alan-ray.nii.gz').strpath
7069
errmap.inputs.metric = 'sqeuclidean'
7170
result = errmap.run()
7271
assert result.outputs.distance == 5.5

nipype/algorithms/tests/test_mesh_ops.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515

1616
@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
1717
def test_ident_distances(tmpdir):
18-
tempdir = str(tmpdir)
19-
os.chdir(tempdir)
18+
tmpdir.chdir()
2019

2120
in_surf = example_data('surf01.vtk')
2221
dist_ident = m.ComputeMeshWarp()
2322
dist_ident.inputs.surface1 = in_surf
2423
dist_ident.inputs.surface2 = in_surf
25-
dist_ident.inputs.out_file = os.path.join(tempdir, 'distance.npy')
24+
dist_ident.inputs.out_file = tmpdir.join('distance.npy')
2625
res = dist_ident.run()
2726
assert res.outputs.distance == 0.0
2827

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

3433
@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
3534
def test_trans_distances(tmpdir):
36-
tempdir = str(tmpdir)
35+
tempdir = tmpdir.strpath
3736
from ...interfaces.vtkbase import tvtk
3837

3938
in_surf = example_data('surf01.vtk')
40-
warped_surf = os.path.join(tempdir, 'warped.vtk')
39+
warped_surf = tmpdir.join('warped.vtk')
4140

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

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

6463
@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
6564
def test_warppoints(tmpdir):
66-
os.chdir(str(tmpdir))
65+
tmpdir.chdir()
6766

6867
# TODO: include regression tests for when tvtk is installed
6968

7069

7170
@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")
7271
def test_meshwarpmaths(tmpdir):
73-
os.chdir(str(tmpdir))
72+
tmpdir.chdir()
7473

7574
# TODO: include regression tests for when tvtk is installed
7675

nipype/algorithms/tests/test_modelgen.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818

1919
def test_modelgen1(tmpdir):
20-
tempdir = str(tmpdir)
21-
filename1 = os.path.join(tempdir, 'test1.nii')
22-
filename2 = os.path.join(tempdir, 'test2.nii')
20+
filename1 = tmpdir.join('test1.nii').strpath
21+
filename2 = tmpdir.join('test2.nii').strpath
2322
Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1)
2423
Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2)
2524
s = SpecifyModel()
@@ -56,9 +55,8 @@ def test_modelgen1(tmpdir):
5655

5756

5857
def test_modelgen_spm_concat(tmpdir):
59-
tempdir = str(tmpdir)
60-
filename1 = os.path.join(tempdir, 'test1.nii')
61-
filename2 = os.path.join(tempdir, 'test2.nii')
58+
filename1 = tmpdir.join('test1.nii').strpath
59+
filename2 = tmpdir.join('test2.nii').strpath
6260
Nifti1Image(np.random.rand(10, 10, 10, 30), np.eye(4)).to_filename(filename1)
6361
Nifti1Image(np.random.rand(10, 10, 10, 30), np.eye(4)).to_filename(filename2)
6462

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

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

123121

124122
def test_modelgen_sparse(tmpdir):
125-
tempdir = str(tmpdir)
126-
filename1 = os.path.join(tempdir, 'test1.nii')
127-
filename2 = os.path.join(tempdir, 'test2.nii')
123+
filename1 = tmpdir.join('test1.nii').strpath
124+
filename2 = tmpdir.join('test2.nii').strpath
128125
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename1)
129126
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename2)
130127
s = SpecifySparseModel()

nipype/algorithms/tests/test_moments.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# -*- coding: utf-8 -*-
22
import numpy as np
3-
import tempfile
43
from nipype.algorithms.misc import calc_moments
54

65

7-
def test_skew():
6+
def test_skew(tmpdir):
87
data = """14.62418305 5.916396751 -1.658088086 4.71113546 1.598428608 5.612553811 -5.004056368 -4.057513911
98
11.16365251 17.32688599 -3.099920667 2.630189741 2.389709914 0.379332731 -0.2899694205 -4.363591482
109
2.059205599 23.90705054 0.7180462297 -1.976963652 7.487682025 -5.583986129 1.094800525 -2.319858134
@@ -126,13 +125,12 @@ def test_skew():
126125
-0.5057854071 -2.415896554 -9.663571931 -5.714041661 -6.037933426 8.673756933 10.03557773 8.629816199
127126
3.622185659 0.4716627142 -10.92515308 -3.705286841 -2.776089545 2.271920902 9.251504922 5.744980887
128127
"""
129-
with tempfile.NamedTemporaryFile(mode='w', delete=True) as f:
130-
f.write(data)
131-
f.flush()
132-
skewness = calc_moments(f.name, 3)
133-
assert np.allclose(skewness, np.array(
134-
[-0.23418937314622, 0.2946365564954823, -0.05781002053540932,
135-
-0.3512508282578762, -
136-
0.07035664150233077, -
137-
0.01935867699166935,
138-
0.00483863369427428, 0.21879460029850167]))
128+
f = tmpdir.join("filetest")
129+
f.write(data)
130+
skewness = calc_moments(f.strpath, 3)
131+
assert np.allclose(skewness, np.array(
132+
[-0.23418937314622, 0.2946365564954823, -0.05781002053540932,
133+
-0.3512508282578762, -
134+
0.07035664150233077, -
135+
0.01935867699166935,
136+
0.00483863369427428, 0.21879460029850167]))

nipype/algorithms/tests/test_normalize_tpms.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919

2020
def test_normalize_tpms(tmpdir):
21-
tempdir = str(tmpdir)
2221

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

3130
for i in range(3):
3231
mapname = example_data('tpm_%02d.nii.gz' % i)
33-
filename = os.path.join(tempdir, 'modtpm_%02d.nii.gz' % i)
34-
out_files.append(os.path.join(tempdir, 'normtpm_%02d.nii.gz' % i))
32+
filename = tmpdir.join('modtpm_%02d.nii.gz' % i).strpath
33+
out_files.append(tmpdir.join('normtpm_%02d.nii.gz' % i).strpath)
3534

3635
im = nb.load(mapname, mmap=NUMPY_MMAP)
3736
data = im.get_data()

nipype/algorithms/tests/test_overlap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def check_close(val1, val2):
2020
in1 = example_data('segmentation0.nii.gz')
2121
in2 = example_data('segmentation1.nii.gz')
2222

23-
os.chdir(str(tmpdir))
23+
tmpdir.chdir()
2424
overlap = Overlap()
2525
overlap.inputs.volume1 = in1
2626
overlap.inputs.volume2 = in1

nipype/algorithms/tests/test_splitmerge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ def test_split_and_merge(tmpdir):
1414
from nipype.algorithms.misc import split_rois, merge_rois
1515

1616
in_mask = example_data('tpms_msk.nii.gz')
17-
dwfile = op.join(str(tmpdir), 'dwi.nii.gz')
17+
dwfile = tmpdir.join('dwi.nii.gz').strpath
1818
mskdata = nb.load(in_mask, mmap=NUMPY_MMAP).get_data()
1919
aff = nb.load(in_mask, mmap=NUMPY_MMAP).affine
2020

2121
dwshape = (mskdata.shape[0], mskdata.shape[1], mskdata.shape[2], 6)
2222
dwdata = np.random.normal(size=dwshape)
23-
os.chdir(str(tmpdir))
23+
tmpdir.chdir()
2424
nb.Nifti1Image(dwdata.astype(np.float32),
2525
aff, None).to_filename(dwfile)
2626

nipype/algorithms/tests/test_tsnr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class TestTSNR():
3030
@pytest.fixture(autouse=True)
3131
def setup_class(self, tmpdir):
3232
# setup temp folder
33-
self.temp_dir = str(tmpdir)
34-
os.chdir(self.temp_dir)
33+
tmpdir.chdir()
3534

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

nipype/caching/tests/test_memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_caching(tmpdir):
2525
try:
2626
# Prevent rerun to check that evaluation is computed only once
2727
config.set('execution', 'stop_on_first_rerun', 'true')
28-
mem = Memory(str(tmpdir))
28+
mem = Memory(tmpdir.strpath)
2929
first_nb_run = nb_runs
3030
results = mem.cache(SideEffectInterface)(input1=2, input2=1)
3131
assert nb_runs == first_nb_run + 1

nipype/interfaces/afni/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class Deconvolve(AFNICommand):
260260
>>> deconvolve.inputs.stim_label = [(1, 'Houses')]
261261
>>> deconvolve.inputs.gltsym = ['SYM: +Houses']
262262
>>> deconvolve.inputs.glt_label = [(1, 'Houses')]
263-
>>> deconvolve.cmdline # doctest: +ALLOW_UNICODE
263+
>>> deconvolve.cmdline
264264
"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"
265265
>>> res = deconvolve.run() # doctest: +SKIP
266266
"""
@@ -574,7 +574,7 @@ class Remlfit(AFNICommand):
574574
>>> remlfit.inputs.out_file = 'output.nii'
575575
>>> remlfit.inputs.matrix = 'output.1D'
576576
>>> remlfit.inputs.gltsym = [('SYM: +Lab1 -Lab2', 'TestSYM'), ('timeseries.txt', 'TestFile')]
577-
>>> remlfit.cmdline # doctest: +ALLOW_UNICODE
577+
>>> remlfit.cmdline
578578
'3dREMLfit -gltsym "SYM: +Lab1 -Lab2" TestSYM -gltsym "timeseries.txt" TestFile -input "functional.nii functional2.nii" -matrix output.1D -Rbuck output.nii'
579579
>>> res = remlfit.run() # doctest: +SKIP
580580
"""
@@ -660,7 +660,7 @@ class Synthesize(AFNICommand):
660660
>>> synthesize.inputs.cbucket = 'functional.nii'
661661
>>> synthesize.inputs.matrix = 'output.1D'
662662
>>> synthesize.inputs.select = ['baseline']
663-
>>> synthesize.cmdline # doctest: +ALLOW_UNICODE
663+
>>> synthesize.cmdline
664664
'3dSynthesize -cbucket functional.nii -matrix output.1D -select baseline'
665665
>>> syn = synthesize.run() # doctest: +SKIP
666666
"""

0 commit comments

Comments
 (0)