Skip to content

STY: Enforce ruff/Perflint rules (PERF) #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions nibabel/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
7 changes: 3 additions & 4 deletions nibabel/nicom/tests/test_dicomwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions nibabel/streamlines/tests/test_streamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion nibabel/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions nibabel/tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, value in fm.items():
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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions nibabel/tests/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions nibabel/tests/test_filehandles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ in range(N))
finally:
del img, imgs
shutil.rmtree(tmpdir)
4 changes: 2 additions & 2 deletions nibabel/tests/test_files_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions nibabel/tests/test_spm99analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ select = [
"F",
"FURB",
"I",
"PERF",
"PIE",
"PLE",
"PYI",
Expand All @@ -144,6 +145,7 @@ ignore = [
"C401",
"C408",
"C416",
"PERF203",
"PIE790",
"PYI024",
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
Expand Down
Loading