From aeb7a8d2a627afc450618ae844101e1f8dfb98ce Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 15:56:07 +0200 Subject: [PATCH 1/5] STY: Apply ruff/Perflint rule PERF102 PERF102 When using only the keys of a dict use the `keys()` method PERF102 When using only the values of a dict use the `values()` method --- nibabel/streamlines/tests/test_streamlines.py | 8 ++++---- nibabel/testing/helpers.py | 2 +- nibabel/tests/test_analyze.py | 8 ++++---- nibabel/tests/test_files_interface.py | 4 ++-- nibabel/tests/test_nifti1.py | 2 +- nibabel/tests/test_spm99analyze.py | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/nibabel/streamlines/tests/test_streamlines.py b/nibabel/streamlines/tests/test_streamlines.py index 857e64fec..740b4c261 100644 --- a/nibabel/streamlines/tests/test_streamlines.py +++ b/nibabel/streamlines/tests/test_streamlines.py @@ -207,7 +207,7 @@ def test_save_tractogram_file(self): def test_save_empty_file(self): tractogram = Tractogram(affine_to_rasmm=np.eye(4)) - for ext, cls in FORMATS.items(): + for ext in FORMATS: with InTemporaryDirectory(): filename = 'streamlines' + ext nib.streamlines.save(tractogram, filename) @@ -216,7 +216,7 @@ def test_save_empty_file(self): def test_save_simple_file(self): tractogram = Tractogram(DATA['streamlines'], affine_to_rasmm=np.eye(4)) - for ext, cls in FORMATS.items(): + for ext in FORMATS: with InTemporaryDirectory(): filename = 'streamlines' + ext nib.streamlines.save(tractogram, filename) @@ -262,7 +262,7 @@ def test_save_complex_file(self): def test_save_sliced_tractogram(self): tractogram = Tractogram(DATA['streamlines'], affine_to_rasmm=np.eye(4)) original_tractogram = tractogram.copy() - for ext, cls in FORMATS.items(): + for ext in FORMATS: with InTemporaryDirectory(): filename = 'streamlines' + ext nib.streamlines.save(tractogram[::2], filename) @@ -283,7 +283,7 @@ def test_save_from_generator(self): tractogram = Tractogram(DATA['streamlines'], affine_to_rasmm=np.eye(4)) # Just to create a generator - for ext, _ in FORMATS.items(): + for ext in FORMATS: filtered = (s for s in tractogram.streamlines if True) lazy_tractogram = LazyTractogram(lambda: filtered, affine_to_rasmm=np.eye(4)) diff --git a/nibabel/testing/helpers.py b/nibabel/testing/helpers.py index ae859d657..ad4bf258c 100644 --- a/nibabel/testing/helpers.py +++ b/nibabel/testing/helpers.py @@ -14,7 +14,7 @@ def bytesio_filemap(klass): """Return bytes io filemap for this image class `klass`""" file_map = klass.make_file_map() - for name, fileholder in file_map.items(): + for fileholder in file_map.values(): fileholder.fileobj = BytesIO() fileholder.pos = 0 return file_map diff --git a/nibabel/tests/test_analyze.py b/nibabel/tests/test_analyze.py index cb7b8d686..d3c6211bf 100644 --- a/nibabel/tests/test_analyze.py +++ b/nibabel/tests/test_analyze.py @@ -728,7 +728,7 @@ def test_data_hdr_cache(self): IC = self.image_class # save an image to a file map fm = IC.make_file_map() - for key, value in fm.items(): + for key in fm: fm[key].fileobj = BytesIO() shape = (2, 3, 4) data = np.arange(24, dtype=np.int8).reshape(shape) @@ -831,7 +831,7 @@ def test_header_updating(self): hdr = img.header hdr.set_zooms((4, 5, 6)) # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() hdr_back = img.from_file_map(img.file_map).header @@ -842,7 +842,7 @@ def test_header_updating(self): assert_array_equal(hdr.get_zooms(), (2, 3, 4)) # Modify affine in-place? Update on save. img.affine[0, 0] = 9 - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() hdr_back = img.from_file_map(img.file_map).header @@ -864,7 +864,7 @@ def test_pickle(self): assert_array_equal(img.get_fdata(), img2.get_fdata()) assert img.header == img2.header # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() img_prox = img.from_file_map(img.file_map) diff --git a/nibabel/tests/test_files_interface.py b/nibabel/tests/test_files_interface.py index 07e394eca..b3562b608 100644 --- a/nibabel/tests/test_files_interface.py +++ b/nibabel/tests/test_files_interface.py @@ -28,7 +28,7 @@ def test_files_spatialimages(): ] for klass in klasses: file_map = klass.make_file_map() - for key, value in file_map.items(): + for value in file_map.values(): assert value.filename is None assert value.fileobj is None assert value.pos == 0 @@ -41,7 +41,7 @@ def test_files_spatialimages(): img = klass(arr.astype(np.float32), aff) else: img = klass(arr, aff) - for key, value in img.file_map.items(): + for value in img.file_map.values(): assert value.filename is None assert value.fileobj is None assert value.pos == 0 diff --git a/nibabel/tests/test_nifti1.py b/nibabel/tests/test_nifti1.py index ec4b8674e..52e38fded 100644 --- a/nibabel/tests/test_nifti1.py +++ b/nibabel/tests/test_nifti1.py @@ -820,7 +820,7 @@ def _qform_rt(self, img): hdr['qform_code'] = 3 hdr['sform_code'] = 4 # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() return img.from_file_map(img.file_map) diff --git a/nibabel/tests/test_spm99analyze.py b/nibabel/tests/test_spm99analyze.py index ada92d3b0..26098d8ed 100644 --- a/nibabel/tests/test_spm99analyze.py +++ b/nibabel/tests/test_spm99analyze.py @@ -423,7 +423,7 @@ def test_mat_read(self): aff = np.diag([2, 3, 4, 1]) # no LR flip in affine img = img_klass(arr, aff) fm = img.file_map - for key, value in fm.items(): + for value in fm.values(): value.fileobj = BytesIO() # Test round trip img.to_file_map() @@ -475,7 +475,7 @@ def test_none_affine(self): img = img_klass(np.zeros((2, 3, 4)), None) aff = img.header.get_best_affine() # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() img_back = img.from_file_map(img.file_map) From d6b6c3b1590d9644217923ab7cb1708eb8c694da Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:01:28 +0200 Subject: [PATCH 2/5] STY: Apply ruff/Perflint rule PERF401 PERF401 Use a list comprehension to create a transformed list --- nibabel/data.py | 3 +-- nibabel/nicom/tests/test_dicomwrappers.py | 7 +++---- nibabel/tests/test_euler.py | 6 +----- nibabel/tests/test_filehandles.py | 3 +-- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/nibabel/data.py b/nibabel/data.py index c49580d09..8ea056d8e 100644 --- a/nibabel/data.py +++ b/nibabel/data.py @@ -87,8 +87,7 @@ def list_files(self, relative=True): for base, dirs, files in os.walk(self.base_path): if relative: base = base[len(self.base_path) + 1 :] - for filename in files: - out_list.append(pjoin(base, filename)) + out_list.extend(pjoin(base, filename) for filename in files) return out_list diff --git a/nibabel/nicom/tests/test_dicomwrappers.py b/nibabel/nicom/tests/test_dicomwrappers.py index 55c27df50..db3f66751 100755 --- a/nibabel/nicom/tests/test_dicomwrappers.py +++ b/nibabel/nicom/tests/test_dicomwrappers.py @@ -429,10 +429,9 @@ def fake_shape_dependents( class PrintBase: def __repr__(self): - attr_strs = [] - for attr in dir(self): - if attr[0].isupper(): - attr_strs.append(f'{attr}={getattr(self, attr)}') + attr_strs = [ + f'{attr}={getattr(self, attr)}' for attr in dir(self) if attr[0].isupper() + ] return f"{self.__class__.__name__}({', '.join(attr_strs)})" class DimIdxSeqElem(pydicom.Dataset): diff --git a/nibabel/tests/test_euler.py b/nibabel/tests/test_euler.py index b0c965c39..3cc07e8f5 100644 --- a/nibabel/tests/test_euler.py +++ b/nibabel/tests/test_euler.py @@ -21,12 +21,8 @@ FLOAT_EPS = np.finfo(np.float64).eps # Example rotations """ -eg_rots = [] params = np.arange(-pi * 2, pi * 2.5, pi / 2) -for x in params: - for y in params: - for z in params: - eg_rots.append((x, y, z)) +eg_rots = [(x, y, z) for x in params for y in params for z in params] def x_only(x): diff --git a/nibabel/tests/test_filehandles.py b/nibabel/tests/test_filehandles.py index 506a62375..93eb284df 100644 --- a/nibabel/tests/test_filehandles.py +++ b/nibabel/tests/test_filehandles.py @@ -33,8 +33,7 @@ def test_multiload(): tmpdir = mkdtemp() fname = pjoin(tmpdir, 'test.img') save(img, fname) - for i in range(N): - imgs.append(load(fname)) + imgs.extend(load(fname) for i in range(N)) finally: del img, imgs shutil.rmtree(tmpdir) From 0a5af04eb6e68946f0310dcedb3be36d79233655 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:49:52 +0200 Subject: [PATCH 3/5] STY: Enforce ruff/Perflint rules (PERF) --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index f45532e81..0706e0876 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -122,6 +122,7 @@ select = [ "F", "FURB", "I", + "PERF", "PIE", "PLE", "PYI", @@ -144,6 +145,7 @@ ignore = [ "C401", "C408", "C416", + "PERF203", "PIE790", "PYI024", # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules From 326addc5d98968a50f9cec8f58b8110557e448c0 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:03:49 +0200 Subject: [PATCH 4/5] STY: Consistency Co-authored-by: Chris Markiewicz --- nibabel/tests/test_analyze.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nibabel/tests/test_analyze.py b/nibabel/tests/test_analyze.py index d3c6211bf..befc920f1 100644 --- a/nibabel/tests/test_analyze.py +++ b/nibabel/tests/test_analyze.py @@ -728,8 +728,8 @@ def test_data_hdr_cache(self): IC = self.image_class # save an image to a file map fm = IC.make_file_map() - for key in fm: - fm[key].fileobj = BytesIO() + for value in fm.values(): + value.fileobj = BytesIO() shape = (2, 3, 4) data = np.arange(24, dtype=np.int8).reshape(shape) affine = np.eye(4) From 74c853f5d9afa19f97ccf529b83763b852ae5e55 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:04:46 +0200 Subject: [PATCH 5/5] STY: Prefix unused loop control variable with an underscore Co-authored-by: Chris Markiewicz --- nibabel/tests/test_filehandles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nibabel/tests/test_filehandles.py b/nibabel/tests/test_filehandles.py index 93eb284df..c985d3544 100644 --- a/nibabel/tests/test_filehandles.py +++ b/nibabel/tests/test_filehandles.py @@ -33,7 +33,7 @@ def test_multiload(): tmpdir = mkdtemp() fname = pjoin(tmpdir, 'test.img') save(img, fname) - imgs.extend(load(fname) for i in range(N)) + imgs.extend(load(fname) for _ in range(N)) finally: del img, imgs shutil.rmtree(tmpdir)