Skip to content

Commit 95086d1

Browse files
committed
STY: pyflakes and some pep8 in algorithms
1 parent ae6e7fe commit 95086d1

File tree

11 files changed

+328
-300
lines changed

11 files changed

+328
-300
lines changed

nipype/algorithms/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def _get_brodmann_area(self):
7575
labels = [self.inputs.labels]
7676
else:
7777
labels = self.inputs.labels
78-
for label in labels:
79-
newdata[origdata == label] = 1
78+
for lab in labels:
79+
newdata[origdata == lab] = 1
8080
if self.inputs.hemi == 'right':
8181
newdata[floor(float(origdata.shape[0]) / 2):, :, :] = 0
8282
elif self.inputs.hemi == 'left':

nipype/algorithms/modelgen.py

Lines changed: 100 additions & 87 deletions
Large diffs are not rendered by default.

nipype/algorithms/rapidart.py

Lines changed: 148 additions & 142 deletions
Large diffs are not rendered by default.

nipype/algorithms/tests/test_modelgen.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,30 @@
77
from nibabel import Nifti1Image
88
import numpy as np
99

10-
from nipype.testing import (assert_equal, assert_false, assert_true,
10+
from nipype.testing import (assert_equal,
1111
assert_raises, assert_almost_equal)
1212
from nipype.interfaces.base import Bunch, TraitError
1313
from nipype.algorithms.modelgen import (SpecifyModel, SpecifySparseModel,
1414
SpecifySPMModel)
1515

16+
1617
def test_modelgen1():
1718
tempdir = mkdtemp()
18-
filename1 = os.path.join(tempdir,'test1.nii')
19-
filename2 = os.path.join(tempdir,'test2.nii')
20-
Nifti1Image(np.random.rand(10,10,10,200), np.eye(4)).to_filename(filename1)
21-
Nifti1Image(np.random.rand(10,10,10,200), np.eye(4)).to_filename(filename2)
19+
filename1 = os.path.join(tempdir, 'test1.nii')
20+
filename2 = os.path.join(tempdir, 'test2.nii')
21+
Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1)
22+
Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2)
2223
s = SpecifyModel()
2324
s.inputs.input_units = 'scans'
24-
set_output_units = lambda : setattr(s.inputs, 'output_units', 'scans')
25+
set_output_units = lambda: setattr(s.inputs, 'output_units', 'scans')
2526
yield assert_raises, TraitError, set_output_units
2627
s.inputs.functional_runs = [filename1, filename2]
2728
s.inputs.time_repetition = 6
2829
s.inputs.high_pass_filter_cutoff = 128.
2930
info = [Bunch(conditions=['cond1'], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None,
30-
pmod=None, regressors = None, regressor_names = None, tmod=None),
31+
pmod=None, regressors=None, regressor_names=None, tmod=None),
3132
Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None,
32-
pmod=None, regressors = None, regressor_names = None, tmod=None)]
33+
pmod=None, regressors=None, regressor_names=None, tmod=None)]
3334
s.inputs.subject_info = info
3435
res = s.run()
3536
yield assert_equal, len(res.outputs.session_info), 2
@@ -38,12 +39,13 @@ def test_modelgen1():
3839
yield assert_almost_equal, np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([12, 300, 600, 1080])
3940
rmtree(tempdir)
4041

42+
4143
def test_modelgen_spm_concat():
4244
tempdir = mkdtemp()
43-
filename1 = os.path.join(tempdir,'test1.nii')
44-
filename2 = os.path.join(tempdir,'test2.nii')
45-
Nifti1Image(np.random.rand(10,10,10,50), np.eye(4)).to_filename(filename1)
46-
Nifti1Image(np.random.rand(10,10,10,50), np.eye(4)).to_filename(filename2)
45+
filename1 = os.path.join(tempdir, 'test1.nii')
46+
filename2 = os.path.join(tempdir, 'test2.nii')
47+
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename1)
48+
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename2)
4749
s = SpecifySPMModel()
4850
s.inputs.input_units = 'secs'
4951
s.inputs.output_units = 'scans'
@@ -54,9 +56,9 @@ def test_modelgen_spm_concat():
5456
s.inputs.time_repetition = 6
5557
s.inputs.high_pass_filter_cutoff = 128.
5658
info = [Bunch(conditions=['cond1'], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None,
57-
pmod=None, regressors = None, regressor_names = None, tmod=None),
59+
pmod=None, regressors=None, regressor_names=None, tmod=None),
5860
Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None,
59-
pmod=None, regressors = None, regressor_names = None, tmod=None)]
61+
pmod=None, regressors=None, regressor_names=None, tmod=None)]
6062
s.inputs.subject_info = info
6163
res = s.run()
6264
yield assert_equal, len(res.outputs.session_info), 1
@@ -65,12 +67,13 @@ def test_modelgen_spm_concat():
6567
yield assert_almost_equal, np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([2.0, 50.0, 100.0, 180.0, 330.0, 340.0, 400.0, 450.0])
6668
rmtree(tempdir)
6769

70+
6871
def test_modelgen_sparse():
6972
tempdir = mkdtemp()
70-
filename1 = os.path.join(tempdir,'test1.nii')
71-
filename2 = os.path.join(tempdir,'test2.nii')
72-
Nifti1Image(np.random.rand(10,10,10,50), np.eye(4)).to_filename(filename1)
73-
Nifti1Image(np.random.rand(10,10,10,50), np.eye(4)).to_filename(filename2)
73+
filename1 = os.path.join(tempdir, 'test1.nii')
74+
filename2 = os.path.join(tempdir, 'test2.nii')
75+
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename1)
76+
Nifti1Image(np.random.rand(10, 10, 10, 50), np.eye(4)).to_filename(filename2)
7477
s = SpecifySparseModel()
7578
s.inputs.input_units = 'secs'
7679
s.inputs.functional_runs = [filename1, filename2]

nipype/algorithms/tests/test_rapidart.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,106 @@
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
from nipype.testing import (assert_equal, assert_false, assert_true,
4-
assert_raises, assert_almost_equal)
4+
assert_almost_equal)
55
import nipype.algorithms.rapidart as ra
66
from nipype.interfaces.base import Bunch
7-
from tempfile import mkdtemp
8-
import os
9-
from shutil import rmtree
107
import numpy as np
118

9+
1210
def test_artifactdetect():
13-
input_map = dict(intersect_mask = dict(),
14-
mask_file = dict(),
15-
mask_threshold = dict(),
16-
mask_type = dict(),
17-
norm_threshold = dict(),
18-
parameter_source = dict(mandatory=True,),
19-
realigned_files = dict(mandatory=True,),
20-
realignment_parameters = dict(),
21-
rotation_threshold = dict(),
22-
translation_threshold = dict(),
23-
use_differences = dict(usedefault=True,),
24-
use_norm = dict(usedefault=True,),
25-
zintensity_threshold = dict(),
11+
input_map = dict(intersect_mask=dict(),
12+
mask_file=dict(),
13+
mask_threshold=dict(),
14+
mask_type=dict(),
15+
norm_threshold=dict(),
16+
parameter_source=dict(mandatory=True,),
17+
realigned_files=dict(mandatory=True,),
18+
realignment_parameters=dict(),
19+
rotation_threshold=dict(),
20+
translation_threshold=dict(),
21+
use_differences=dict(usedefault=True,),
22+
use_norm=dict(usedefault=True,),
23+
zintensity_threshold=dict(),
2624
)
2725
instance = ra.ArtifactDetect()
2826
for key, metadata in input_map.items():
2927
for metakey, value in metadata.items():
3028
yield assert_equal, getattr(instance.inputs.traits()[key], metakey), value
3129

30+
3231
def test_stimuluscorrelation():
33-
input_map = dict(concatenated_design = dict(mandatory=True,),
34-
intensity_values = dict(mandatory=True,),
35-
realignment_parameters = dict(mandatory=True,),
36-
spm_mat_file = dict(mandatory=True,),
32+
input_map = dict(concatenated_design=dict(mandatory=True,),
33+
intensity_values=dict(mandatory=True,),
34+
realignment_parameters=dict(mandatory=True,),
35+
spm_mat_file=dict(mandatory=True,),
3736
)
3837
instance = ra.StimulusCorrelation()
3938
for key, metadata in input_map.items():
4039
for metakey, value in metadata.items():
4140
yield assert_equal, getattr(instance.inputs.traits()[key], metakey), value
4241

42+
4343
def test_ad_init():
44-
ad = ra.ArtifactDetect(use_differences=[True,False])
44+
ad = ra.ArtifactDetect(use_differences=[True, False])
4545
yield assert_true, ad.inputs.use_differences[0]
4646
yield assert_false, ad.inputs.use_differences[1]
4747

48+
4849
def test_ad_output_filenames():
4950
ad = ra.ArtifactDetect()
5051
outputdir = '/tmp'
5152
f = 'motion.nii'
52-
outlierfile,intensityfile,statsfile,normfile,plotfile = ad._get_output_filenames(f,outputdir)
53+
outlierfile, intensityfile, statsfile, normfile, plotfile = ad._get_output_filenames(f, outputdir)
5354
yield assert_equal, outlierfile, '/tmp/art.motion_outliers.txt'
5455
yield assert_equal, intensityfile, '/tmp/global_intensity.motion.txt'
5556
yield assert_equal, statsfile, '/tmp/stats.motion.txt'
5657
yield assert_equal, normfile, '/tmp/norm.motion.txt'
5758
yield assert_equal, plotfile, '/tmp/plot.motion.png'
5859

60+
5961
def test_ad_get_affine_matrix():
6062
ad = ra.ArtifactDetect()
6163
matrix = ad._get_affine_matrix(np.array([0]))
6264
yield assert_equal, matrix, np.eye(4)
6365
# test translation
64-
params = [1,2,3]
66+
params = [1, 2, 3]
6567
matrix = ad._get_affine_matrix(params)
6668
out = np.eye(4)
67-
out[0:3,3] = params
69+
out[0:3, 3] = params
6870
yield assert_equal, matrix, out
6971
# test rotation
70-
params = np.array([0,0,0,np.pi/2,np.pi/2,np.pi/2])
72+
params = np.array([0, 0, 0, np.pi / 2, np.pi / 2, np.pi / 2])
7173
matrix = ad._get_affine_matrix(params)
72-
out = np.array([0,0,1,0,0,-1,0,0,1,0,0,0,0,0,0,1]).reshape((4,4))
74+
out = np.array([0, 0, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1]).reshape((4, 4))
7375
yield assert_almost_equal, matrix, out
7476
# test scaling
75-
params = np.array([0,0,0,0,0,0,1,2,3])
77+
params = np.array([0, 0, 0, 0, 0, 0, 1, 2, 3])
7678
matrix = ad._get_affine_matrix(params)
77-
out = np.array([1,0,0,0,0,2,0,0,0,0,3,0,0,0,0,1]).reshape((4,4))
79+
out = np.array([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1]).reshape((4, 4))
7880
yield assert_equal, matrix, out
7981
# test shear
80-
params = np.array([0,0,0,0,0,0,1,1,1,1,2,3])
82+
params = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3])
8183
matrix = ad._get_affine_matrix(params)
82-
out = np.array([1,1,2,0,0,1,3,0,0,0,1,0,0,0,0,1]).reshape((4,4))
84+
out = np.array([1, 1, 2, 0, 0, 1, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1]).reshape((4, 4))
8385
yield assert_equal, matrix, out
8486

87+
8588
def test_ad_get_norm():
8689
ad = ra.ArtifactDetect()
87-
params = np.array([0,0,0,0,0,0,0,0,0,np.pi/4,np.pi/4,np.pi/4,0,0,0,-np.pi/4,-np.pi/4,-np.pi/4]).reshape((3,6))
88-
norm = ad._calc_norm(params,False)
89-
yield assert_almost_equal, norm, np.array([18.86436316, 37.74610158, 31.29780829])
90-
norm = ad._calc_norm(params,True)
91-
yield assert_almost_equal, norm, np.array([ 0. , 143.72192614, 173.92527131])
90+
params = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, np.pi / 4, np.pi / 4,
91+
np.pi / 4, 0, 0, 0, -np.pi / 4,
92+
-np.pi / 4, -np.pi / 4]).reshape((3, 6))
93+
norm = ad._calc_norm(params, False)
94+
yield assert_almost_equal, norm, np.array([18.86436316, 37.74610158, 31.29780829])
95+
norm = ad._calc_norm(params, True)
96+
yield assert_almost_equal, norm, np.array([0., 143.72192614, 173.92527131])
97+
9298

9399
def test_sc_init():
94100
sc = ra.StimulusCorrelation(concatenated_design=True)
95101
yield assert_true, sc.inputs.concatenated_design
96102

103+
97104
def test_sc_populate_inputs():
98105
sc = ra.StimulusCorrelation()
99106
inputs = Bunch(realignment_parameters=None,
@@ -102,10 +109,10 @@ def test_sc_populate_inputs():
102109
concatenated_design=None)
103110
yield assert_equal, sc.inputs.__dict__.keys(), inputs.__dict__.keys()
104111

112+
105113
def test_sc_output_filenames():
106114
sc = ra.StimulusCorrelation()
107115
outputdir = '/tmp'
108116
f = 'motion.nii'
109-
corrfile = sc._get_output_filenames(f,outputdir)
117+
corrfile = sc._get_output_filenames(f, outputdir)
110118
yield assert_equal, corrfile, '/tmp/qa.motion_stimcorr.txt'
111-

nipype/interfaces/freesurfer/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ def _gen_outfilename(self):
772772
_, name, ext = split_filename(self.inputs.parcstats_file)
773773
elif isdefined(self.inputs.label_file):
774774
_, name, ext = split_filename(self.inputs.label_file)
775-
elif isdefied(self.inputs.scalarcurv_file):
775+
elif isdefined(self.inputs.scalarcurv_file):
776776
_, name, ext = split_filename(self.inputs.scalarcurv_file)
777777
elif isdefined(self.inputs.functional_file):
778778
_, name, ext = split_filename(self.inputs.functional_file)

nipype/interfaces/nipy/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def _run_interface(self, runtime):
253253
self._z_maps = []
254254
for contrast_def in self.inputs.contrasts:
255255
name = contrast_def[0]
256-
type = contrast_def[1]
256+
_ = contrast_def[1]
257257
contrast = np.zeros(len(reg_names))
258258

259259
for i, reg_name in enumerate(reg_names):

nipype/interfaces/nipy/preprocess.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ class ComputeMaskInputSpec(BaseInterfaceInputSpec):
2525
M = traits.Float(desc="upper fraction of the histogram to be discarded")
2626
cc = traits.Bool(desc="if True, only the largest connect component is kept")
2727

28+
2829
class ComputeMaskOutputSpec(TraitedSpec):
2930
brain_mask = File(exists=True)
3031

32+
3133
class ComputeMask(BaseInterface):
3234
input_spec = ComputeMaskInputSpec
3335
output_spec = ComputeMaskOutputSpec
3436

3537
def _run_interface(self, runtime):
3638

3739
args = {}
38-
for key in [k for k,_ in self.inputs.items() if k not in BaseInterfaceInputSpec().trait_names()]:
40+
for key in [k for k, _ in self.inputs.items() if k not in BaseInterfaceInputSpec().trait_names()]:
3941
value = getattr(self.inputs, key)
4042
if isdefined(value):
4143
if key in ['mean_volume', 'reference_volume']:
@@ -54,5 +56,3 @@ def _list_outputs(self):
5456
outputs = self._outputs().get()
5557
outputs["brain_mask"] = self._brain_mask_path
5658
return outputs
57-
58-

nipype/interfaces/nitime/analysis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
from nipype.utils.misc import package_check
1717
package_check('matplotlib')
1818

19-
from nipype.interfaces.base import (TraitedSpec, File, InputMultiPath,
20-
OutputMultiPath, Undefined, traits,
19+
from nipype.interfaces.base import (TraitedSpec, File, Undefined, traits,
2120
BaseInterface, isdefined,
22-
BaseInterfaceInputSpec)
21+
BaseInterfaceInputSpec)
2322

2423
from nipype.utils.filemanip import fname_presuffix
2524

nipype/interfaces/spm/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from nipype.interfaces.spm.base import (SPMCommand, SPMCommandInputSpec,
2929
scans_for_fnames)
3030
from nipype.utils.filemanip import (filename_to_list, list_to_filename,
31-
loadflat, split_filename)
31+
split_filename)
3232

3333
logger = logging.getLogger('spmlogger')
3434

0 commit comments

Comments
 (0)