diff --git a/.pep8speaks.yml b/.pep8speaks.yml index e0f5abee..4848af40 100644 --- a/.pep8speaks.yml +++ b/.pep8speaks.yml @@ -1,11 +1,7 @@ scanner: diff_only: True # If False, the entire file touched by the Pull Request is scanned for errors. If True, only the diff is scanned. - linter: pycodestyle # Other option is flake8 + linter: flake8 # Other option is flake8 -pycodestyle: # Same as scanner.linter value. Other option is flake8 - max-line-length: 88 # Follow black's line length - ignore: # Errors and warnings to ignore - - E402 # module level import not at top of file no_blank_comment: False # If True, no comment is made on PR without any errors. descending_issues_order: False # If True, PEP 8 issues in message will be displayed in descending order of line numbers in the file diff --git a/nitransforms/base.py b/nitransforms/base.py index 6c1eba17..296b4da6 100644 --- a/nitransforms/base.py +++ b/nitransforms/base.py @@ -87,8 +87,10 @@ def _to_hdf5(self, group): def __eq__(self, other): try: - return np.allclose( - self.affine, other.affine, rtol=EQUALITY_TOL) and self.shape == other.shape + return ( + np.allclose(self.affine, other.affine, rtol=EQUALITY_TOL) + and self.shape == other.shape + ) except AttributeError: pass return False diff --git a/nitransforms/conftest.py b/nitransforms/conftest.py index c93daea9..0ce0e295 100644 --- a/nitransforms/conftest.py +++ b/nitransforms/conftest.py @@ -58,8 +58,12 @@ def get_data(): newaff[0, 0] *= -1.0 newaff[1, 1] *= -1.0 newaff[:2, 3] = imgaff.dot(np.hstack((np.array(img.shape[:3]) - 1, 1.0)))[:2] - _data['LPS'] = nb.Nifti1Image(np.flip(np.flip(img.get_fdata(), 0), 1), newaff, img.header) - A = nb.volumeutils.shape_zoom_affine(img.shape, img.header.get_zooms(), x_flip=False) + _data['LPS'] = nb.Nifti1Image( + np.flip(np.flip(img.get_fdata(), 0), 1), newaff, img.header + ) + A = nb.volumeutils.shape_zoom_affine( + img.shape, img.header.get_zooms(), x_flip=False + ) R = nb.affines.from_matvec(nb.eulerangles.euler2mat(x=0.09, y=0.001, z=0.001)) newaff = R.dot(A) oblique_img = nb.Nifti1Image(img.get_fdata(), newaff, img.header) diff --git a/nitransforms/linear.py b/nitransforms/linear.py index 9e0d379c..262b6ca4 100644 --- a/nitransforms/linear.py +++ b/nitransforms/linear.py @@ -58,7 +58,9 @@ def __init__(self, matrix=None, reference=None): self._matrix = np.array(matrix) assert self._matrix.ndim == 3, 'affine matrix should be 3D' - assert self._matrix.shape[-2] == self._matrix.shape[-1], 'affine matrix is not square' + assert ( + self._matrix.shape[-2] == self._matrix.shape[-1] + ), 'affine matrix is not square' if reference: if isinstance(reference, str): @@ -196,7 +198,9 @@ def map_voxel(self, index, nindex=0, moving=None): if index.shape[0] == self._matrix[nindex].shape[0] - 1: index = np.append(index, [1]) - matrix = reference.affine.dot(self._matrix[nindex].dot(np.linalg.inv(moving.affine))) + matrix = reference.affine.dot( + self._matrix[nindex].dot(np.linalg.inv(moving.affine)) + ) return tuple(matrix.dot(index)[:-1]) def _to_hdf5(self, x5_root): @@ -235,7 +239,10 @@ def to_filename(self, filename, fmt='X5', moving=None): T = self.matrix.copy() pre = LPS post = LPS - if obliquity(self.reference.affine).min() * 180 / pi > OBLIQUITY_THRESHOLD_DEG: + if ( + obliquity(self.reference.affine).min() * 180 / pi + > OBLIQUITY_THRESHOLD_DEG + ): print('Reference affine axes are oblique.') M = self.reference.affine A = shape_zoom_affine(self.reference.shape, @@ -245,7 +252,11 @@ def to_filename(self, filename, fmt='X5', moving=None): if not moving: moving = self.reference - if moving and obliquity(moving.affine).min() * 180 / pi > OBLIQUITY_THRESHOLD_DEG: + if ( + moving + and obliquity(moving.affine).min() * 180 / pi + > OBLIQUITY_THRESHOLD_DEG + ): print('Moving affine axes are oblique.') M2 = moving.affine A2 = shape_zoom_affine(moving.shape, diff --git a/nitransforms/nonlinear.py b/nitransforms/nonlinear.py index 350c820f..45a1c0f4 100644 --- a/nitransforms/nonlinear.py +++ b/nitransforms/nonlinear.py @@ -219,7 +219,7 @@ def _interp_transform(self, coords): self._coeffs[ijk] if not np.any(offbounds) else [0.0] * self.ndim) - coords[:3] += weights.dot(np.array(coeffs, dtype=float)) + # coords[:3] += weights.dot(np.array(coeffs, dtype=float)) return self.reference.inverse.dot(np.hstack((coords, 1)))[:3] def map_voxel(self, index, moving=None): @@ -237,7 +237,9 @@ def resample(self, moving, order=3, mode='constant', cval=0.0, prefilter=True, >>> coeffs = np.zeros((6, 6, 6, 3)) >>> coeffs[2, 2, 2, ...] = [10.0, -20.0, 0] >>> aff = ref.affine - >>> aff[:3, :3] = aff[:3, :3].dot(np.eye(3) * np.array(ref.header.get_zooms()[:3]) / 6.0) + >>> aff[:3, :3] = aff[:3, :3].dot(np.eye(3) * np.array( + ... ref.header.get_zooms()[:3]) / 6.0 + ... ) >>> coeffsimg = nb.Nifti1Image(coeffs, ref.affine, ref.header) >>> xfm = BSplineFieldTransform(ref, coeffsimg) # doctest: +SKIP >>> new = xfm.resample(ref) # doctest: +SKIP diff --git a/nitransforms/patched.py b/nitransforms/patched.py index 7c3b5db6..41708b41 100644 --- a/nitransforms/patched.py +++ b/nitransforms/patched.py @@ -1,6 +1,7 @@ import numpy as np from nibabel.affines import voxel_sizes + def obliquity(affine): r""" Estimate the *obliquity* an affine's axes represent. @@ -24,6 +25,7 @@ def obliquity(affine): best_cosines = np.abs((affine[:-1, :-1] / vs).max(axis=1)) return np.arccos(best_cosines) + def shape_zoom_affine(shape, zooms, x_flip=True, y_flip=False): ''' Get affine implied by given shape and zooms We get the translations from the center of the image (implied by @@ -85,4 +87,4 @@ def shape_zoom_affine(shape, zooms, x_flip=True, y_flip=False): aff = np.eye(4) aff[:3, :3] = np.diag(zooms) aff[:3, -1] = -origin * zooms - return aff \ No newline at end of file + return aff diff --git a/nitransforms/tests/test_affines.py b/nitransforms/tests/test_affines.py index f76293f7..de9d77f3 100644 --- a/nitransforms/tests/test_affines.py +++ b/nitransforms/tests/test_affines.py @@ -3,6 +3,7 @@ from nibabel.eulerangles import euler2mat from ..patched import obliquity + def test_obliquity(): """Check the calculation of inclination of an affine axes.""" from math import pi diff --git a/nitransforms/tests/test_transform.py b/nitransforms/tests/test_transform.py index 97733d07..23c8db7b 100644 --- a/nitransforms/tests/test_transform.py +++ b/nitransforms/tests/test_transform.py @@ -107,7 +107,13 @@ def test_linear_save(data_path, get_data, image_orientation, sw_tool): 'RAS', 'LAS', 'LPS', # 'oblique', ]) @pytest.mark.parametrize('sw_tool', ['itk', 'fsl', 'afni']) -def test_apply_linear_transform(tmpdir, data_path, get_data, image_orientation, sw_tool): +def test_apply_linear_transform( + tmpdir, + data_path, + get_data, + image_orientation, + sw_tool +): """Check implementation of exporting affines to formats.""" tmpdir.chdir() diff --git a/setup.cfg b/setup.cfg index 3fca8bbf..1619f08a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,3 +24,6 @@ tests = all = %(test)s +[flake8] +max-line-length = 100 +ignore = D100,D101,D102,D103,D104,D105,D200,D201,D202,D204,D205,D208,D209,D210,D300,D301,D400,D401,D403,E24,E121,E123,E126,E226,E266,E402,E704,E731,F821,I100,I101,I201,N802,N803,N804,N806,W503,W504,W605