Skip to content

Commit 48f1722

Browse files
committed
explicitly importing numpy.testing where assert_equal or assert_almost_equal from numpy is used; small cleaning of imports that are not used
1 parent 2741349 commit 48f1722

File tree

11 files changed

+47
-54
lines changed

11 files changed

+47
-54
lines changed

nipype/algorithms/tests/test_mesh_ops.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import os
77

88
import pytest
9-
from nipype.testing import assert_almost_equal, example_data
9+
import nipype.testing as npt
10+
from nipype.testing import example_data
1011
import numpy as np
1112
from nipype.algorithms import mesh as m
1213
from ...interfaces import vtkbase as VTKInfo
@@ -56,10 +57,10 @@ def test_trans_distances(tmpdir):
5657
dist.inputs.surface2 = warped_surf
5758
dist.inputs.out_file = os.path.join(tempdir, 'distance.npy')
5859
res = dist.run()
59-
assert_almost_equal(res.outputs.distance, np.linalg.norm(inc), 4)
60+
npt.assert_almost_equal(res.outputs.distance, np.linalg.norm(inc), 4)
6061
dist.inputs.weighting = 'area'
6162
res = dist.run()
62-
assert_almost_equal(res.outputs.distance, np.linalg.norm(inc), 4)
63+
npt.assert_almost_equal(res.outputs.distance, np.linalg.norm(inc), 4)
6364

6465

6566
@pytest.mark.skipif(VTKInfo.no_tvtk(), reason="tvtk is not installed")

nipype/algorithms/tests/test_modelgen.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111

1212
import pytest
13-
from nipype.testing import assert_almost_equal
13+
import numpy.testing as npt
1414
from nipype.interfaces.base import Bunch, TraitError
1515
from nipype.algorithms.modelgen import (SpecifyModel, SpecifySparseModel,
1616
SpecifySPMModel)
@@ -38,21 +38,21 @@ def test_modelgen1(tmpdir):
3838
assert len(res.outputs.session_info) == 2
3939
assert len(res.outputs.session_info[0]['regress']) == 0
4040
assert len(res.outputs.session_info[0]['cond']) == 1
41-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([12, 300, 600, 1080]))
41+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([12, 300, 600, 1080]))
4242
info = [Bunch(conditions=['cond1'], onsets=[[2]], durations=[[1]]),
4343
Bunch(conditions=['cond1'], onsets=[[3]], durations=[[1]])]
4444
s.inputs.subject_info = deepcopy(info)
4545
res = s.run()
46-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([6.]))
47-
assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][0]['duration']), np.array([6.]))
46+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([6.]))
47+
npt.assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][0]['duration']), np.array([6.]))
4848
info = [Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2]], durations=[[1, 1], [1]]),
4949
Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2, 4]], durations=[[1, 1], [1, 1]])]
5050
s.inputs.subject_info = deepcopy(info)
5151
s.inputs.input_units = 'scans'
5252
res = s.run()
53-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([6., 6.]))
54-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([6., ]))
55-
assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][1]['duration']), np.array([6., 6.]))
53+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([6., 6.]))
54+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([6., ]))
55+
npt.assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][1]['duration']), np.array([6., 6.]))
5656

5757

5858
def test_modelgen_spm_concat(tmpdir):
@@ -79,22 +79,22 @@ def test_modelgen_spm_concat(tmpdir):
7979
assert len(res.outputs.session_info[0]['regress']) == 1
8080
assert np.sum(res.outputs.session_info[0]['regress'][0]['val']) == 30
8181
assert len(res.outputs.session_info[0]['cond']) == 1
82-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0, 210.0, 220.0, 280.0, 330.0]))
83-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([1., 1., 1., 1., 1., 1., 1., 1.]))
82+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0, 210.0, 220.0, 280.0, 330.0]))
83+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([1., 1., 1., 1., 1., 1., 1., 1.]))
8484

8585
# Test case of scans as output units instead of seconds
8686
setattr(s.inputs, 'output_units', 'scans')
8787
assert s.inputs.output_units == 'scans'
8888
s.inputs.subject_info = deepcopy(info)
8989
res = s.run()
90-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0, 210.0, 220.0, 280.0, 330.0]) / 6)
90+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0, 210.0, 220.0, 280.0, 330.0]) / 6)
9191

9292
# Test case for no concatenation with seconds as output units
9393
s.inputs.concatenate_runs = False
9494
s.inputs.subject_info = deepcopy(info)
9595
s.inputs.output_units = 'secs'
9696
res = s.run()
97-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0]))
97+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 170.0]))
9898

9999
# Test case for variable number of events in separate runs, sometimes unique.
100100
filename3 = os.path.join(tempdir, 'test3.nii')
@@ -105,10 +105,10 @@ def test_modelgen_spm_concat(tmpdir):
105105
Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2]], durations=[[1, 1], [1]])]
106106
s.inputs.subject_info = deepcopy(info)
107107
res = s.run()
108-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([1., 1.]))
109-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([1., ]))
110-
assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][1]['duration']), np.array([1., 1.]))
111-
assert_almost_equal(np.array(res.outputs.session_info[2]['cond'][1]['duration']), np.array([1., ]))
108+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([1., 1.]))
109+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([1., ]))
110+
npt.assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][1]['duration']), np.array([1., 1.]))
111+
npt.assert_almost_equal(np.array(res.outputs.session_info[2]['cond'][1]['duration']), np.array([1., ]))
112112

113113
# Test case for variable number of events in concatenated runs, sometimes unique.
114114
s.inputs.concatenate_runs = True
@@ -117,8 +117,8 @@ def test_modelgen_spm_concat(tmpdir):
117117
Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2]], durations=[[1, 1], [1]])]
118118
s.inputs.subject_info = deepcopy(info)
119119
res = s.run()
120-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([1., 1., 1., 1., 1., 1.]))
121-
assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([1., 1., 1., 1.]))
120+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([1., 1., 1., 1., 1., 1.]))
121+
npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([1., 1., 1., 1.]))
122122

123123

124124
def test_modelgen_sparse(tmpdir):
@@ -148,12 +148,12 @@ def test_modelgen_sparse(tmpdir):
148148

149149
s.inputs.model_hrf = True
150150
res = s.run()
151-
assert_almost_equal(res.outputs.session_info[0]['regress'][0]['val'][0], 0.016675298129743384)
151+
npt.assert_almost_equal(res.outputs.session_info[0]['regress'][0]['val'][0], 0.016675298129743384)
152152
assert len(res.outputs.session_info[0]['regress']) == 1
153153
s.inputs.use_temporal_deriv = True
154154
res = s.run()
155155

156156
assert len(res.outputs.session_info[0]['regress']) == 2
157-
assert_almost_equal(res.outputs.session_info[0]['regress'][0]['val'][0], 0.016675298129743384)
158-
assert_almost_equal(res.outputs.session_info[1]['regress'][1]['val'][5], 0.007671459162258378)
157+
npt.assert_almost_equal(res.outputs.session_info[0]['regress'][0]['val'][0], 0.016675298129743384)
158+
npt.assert_almost_equal(res.outputs.session_info[1]['regress'][1]['val'][5], 0.007671459162258378)
159159

nipype/algorithms/tests/test_rapidart.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import numpy as np
77

8-
from ...testing import assert_equal, assert_almost_equal
8+
import numpy.testing as npt
99
from .. import rapidart as ra
1010
from ...interfaces.base import Bunch
1111

@@ -33,38 +33,38 @@ def test_ad_output_filenames():
3333

3434
def test_ad_get_affine_matrix():
3535
matrix = ra._get_affine_matrix(np.array([0]), 'SPM')
36-
assert_equal(matrix, np.eye(4))
36+
npt.assert_equal(matrix, np.eye(4))
3737
# test translation
3838
params = [1, 2, 3]
3939
matrix = ra._get_affine_matrix(params, 'SPM')
4040
out = np.eye(4)
4141
out[0:3, 3] = params
42-
assert_equal(matrix, out)
42+
npt.assert_equal(matrix, out)
4343
# test rotation
4444
params = np.array([0, 0, 0, np.pi / 2, np.pi / 2, np.pi / 2])
4545
matrix = ra._get_affine_matrix(params, 'SPM')
4646
out = np.array([0, 0, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1]).reshape((4, 4))
47-
assert_almost_equal(matrix, out)
47+
npt.assert_almost_equal(matrix, out)
4848
# test scaling
4949
params = np.array([0, 0, 0, 0, 0, 0, 1, 2, 3])
5050
matrix = ra._get_affine_matrix(params, 'SPM')
5151
out = np.array([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1]).reshape((4, 4))
52-
assert_equal(matrix, out)
52+
npt.assert_equal(matrix, out)
5353
# test shear
5454
params = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3])
5555
matrix = ra._get_affine_matrix(params, 'SPM')
5656
out = np.array([1, 1, 2, 0, 0, 1, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1]).reshape((4, 4))
57-
assert_equal(matrix, out)
57+
npt.assert_equal(matrix, out)
5858

5959

6060
def test_ad_get_norm():
6161
params = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, np.pi / 4, np.pi / 4,
6262
np.pi / 4, 0, 0, 0, -np.pi / 4,
6363
-np.pi / 4, -np.pi / 4]).reshape((3, 6))
6464
norm, _ = ra._calc_norm(params, False, 'SPM')
65-
assert_almost_equal(norm, np.array([18.86436316, 37.74610158, 31.29780829]))
65+
npt.assert_almost_equal(norm, np.array([18.86436316, 37.74610158, 31.29780829]))
6666
norm, _ = ra._calc_norm(params, True, 'SPM')
67-
assert_almost_equal(norm, np.array([0., 143.72192614, 173.92527131]))
67+
npt.assert_almost_equal(norm, np.array([0., 143.72192614, 173.92527131]))
6868

6969

7070
def test_sc_init():

nipype/algorithms/tests/test_tsnr.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33

4-
from ...testing import (assert_equal, assert_almost_equal, utils)
4+
from ...testing import utils
55
from ..confounds import TSNR
66
from .. import misc
77

88
import pytest
9+
import numpy.testing as npt
910
import mock
1011
import nibabel as nb
1112
import numpy as np
@@ -94,27 +95,27 @@ def test_warning(self, mock_warn):
9495
assert True in [args[0].count('confounds') > 0 for _, args, _ in mock_warn.mock_calls]
9596

9697
def assert_expected_outputs_poly(self, tsnrresult, expected_ranges):
97-
assert_equal(os.path.basename(tsnrresult.outputs.detrended_file),
98-
self.out_filenames['detrended_file'])
98+
assert os.path.basename(tsnrresult.outputs.detrended_file) == \
99+
self.out_filenames['detrended_file']
99100
self.assert_expected_outputs(tsnrresult, expected_ranges)
100101

101102
def assert_expected_outputs(self, tsnrresult, expected_ranges):
102103
self.assert_default_outputs(tsnrresult.outputs)
103104
self.assert_unchanged(expected_ranges)
104105

105106
def assert_default_outputs(self, outputs):
106-
assert_equal(os.path.basename(outputs.mean_file),
107-
self.out_filenames['mean_file'])
108-
assert_equal(os.path.basename(outputs.stddev_file),
109-
self.out_filenames['stddev_file'])
110-
assert_equal(os.path.basename(outputs.tsnr_file),
111-
self.out_filenames['tsnr_file'])
107+
assert os.path.basename(outputs.mean_file) == \
108+
self.out_filenames['mean_file']
109+
assert os.path.basename(outputs.stddev_file) == \
110+
self.out_filenames['stddev_file']
111+
assert os.path.basename(outputs.tsnr_file) == \
112+
self.out_filenames['tsnr_file']
112113

113114
def assert_unchanged(self, expected_ranges):
114115
for key, (min_, max_) in expected_ranges.items():
115116
data = np.asarray(nb.load(self.out_filenames[key])._data)
116-
assert_almost_equal(np.amin(data), min_, decimal=1)
117-
assert_almost_equal(np.amax(data), max_, decimal=1)
117+
npt.assert_almost_equal(np.amin(data), min_, decimal=1)
118+
npt.assert_almost_equal(np.amax(data), max_, decimal=1)
118119

119120

120121
fake_data = np.array([[[[2, 4, 3, 9, 1],

nipype/interfaces/nitime/tests/test_nitime.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import numpy as np
88

9-
import pytest, pdb
10-
from nipype.testing import (assert_equal, assert_raises, skipif)
9+
import pytest
1110
from nipype.testing import example_data
1211
import nipype.interfaces.nitime as nitime
1312

nipype/interfaces/spm/tests/test_preprocess.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import numpy as np
99

1010
import pytest
11-
from nipype.testing import (assert_equal, assert_false, assert_true,
12-
assert_raises, skipif)
1311
import nibabel as nb
1412
import nipype.interfaces.spm as spm
1513
from nipype.interfaces.spm import no_spm

nipype/interfaces/tests/test_base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import nipype.interfaces.base as nib
1717
from nipype.utils.filemanip import split_filename
1818
from nipype.interfaces.base import Undefined, config
19-
from traits.testing.nose_tools import skip
2019
import traits.api as traits
2120

2221
@pytest.mark.parametrize("args", [
@@ -97,7 +96,6 @@ class spec(nib.TraitedSpec):
9796
infields = spec(foo=1)
9897
hashval = ([('foo', 1), ('goo', '0.0000000000')], 'e89433b8c9141aa0fda2f8f4d662c047')
9998
assert infields.get_hashval() == hashval
100-
# yield assert_equal, infields.hashval[1], hashval[1]
10199
assert infields.__repr__() == '\nfoo = 1\ngoo = 0.0\n'
102100

103101

nipype/interfaces/tests/test_io.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import pytest
1616
import nipype
17-
from nipype.testing import assert_equal, assert_true, assert_false, skipif
1817
import nipype.interfaces.io as nio
1918
from nipype.interfaces.base import Undefined
2019

nipype/interfaces/tests/test_nilearn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import numpy as np
88

99
from ...testing import utils
10-
from numpy.testing import assert_almost_equal
1110

1211
from .. import nilearn as iface
1312
from ...pipeline import engine as pe
1413

1514
import pytest
15+
import numpy.testing as npt
1616

1717
no_nilearn = True
1818
try:
@@ -149,7 +149,7 @@ def assert_expected_output(self, labels, wanted):
149149
for i, time in enumerate(got):
150150
assert len(labels) == len(time)
151151
for j, segment in enumerate(time):
152-
assert_almost_equal(segment, wanted[i][j], decimal=1)
152+
npt.assert_almost_equal(segment, wanted[i][j], decimal=1)
153153

154154

155155
def teardown_class(self):

nipype/pipeline/engine/tests/test_engine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ def _submit_job(self, node, updatehash=False):
532532
logger.info('Exception: %s' % str(e))
533533
error_raised = True
534534
assert error_raised
535-
# yield assert_true, 'Submit called' in e
536535
# rerun to ensure we have outputs
537536
w1.run(plugin='Linear')
538537
# set local check

nipype/testing/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
template = funcfile
2727
transfm = funcfile
2828

29-
from nose.tools import *
30-
from numpy.testing import *
3129

3230
from . import decorators as dec
3331
from .utils import skip_if_no_package, package_check, TempFATFS

0 commit comments

Comments
 (0)