Skip to content

Commit

Permalink
Merge pull request #1406 from DimitriPapadopoulos/ruff_0.9.1
Browse files Browse the repository at this point in the history
chore: Updates after pre-commit autoupdate
  • Loading branch information
effigies authored Jan 16, 2025
2 parents 5aca3f2 + e22675f commit 9d66d8a
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 33 deletions.
3 changes: 0 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ repos:
exclude: = ["doc", "tools"]
- id: ruff-format
exclude: = ["doc", "tools"]
- id: ruff
args: [ --select, ISC001, --fix ]
exclude: = ["doc", "tools"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
hooks:
Expand Down
2 changes: 1 addition & 1 deletion nibabel/benchmarks/bench_arrayproxy_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def fmt_sliceobj(sliceobj):
return f'[{", ".join(slcstr)}]'

with InTemporaryDirectory():
print(f'Generating test data... ({int(round(np.prod(SHAPE) * 4 / 1048576.0))} MB)')
print(f'Generating test data... ({round(np.prod(SHAPE) * 4 / 1048576.0)} MB)')

data = np.array(np.random.random(SHAPE), dtype=np.float32)

Expand Down
6 changes: 3 additions & 3 deletions nibabel/cmdline/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ def display_diff(files, diff):
item_str = str(item)
# Value might start/end with some invisible spacing characters so we
# would "condition" it on both ends a bit
item_str = re.sub('^[ \t]+', '<', item_str)
item_str = re.sub('[ \t]+$', '>', item_str)
item_str = re.sub(r'^[ \t]+', '<', item_str)
item_str = re.sub(r'[ \t]+$', '>', item_str)
# and also replace some other invisible symbols with a question
# mark
item_str = re.sub('[\x00]', '?', item_str)
item_str = re.sub(r'[\x00]', '?', item_str)
output += value_width.format(item_str)

output += '\n'
Expand Down
2 changes: 1 addition & 1 deletion nibabel/cmdline/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def table2string(table, out=None):
table[i] += [''] * (nelements_max - len(table_))

# eat whole entry while computing width for @w (for wide)
markup_strip = re.compile('^@([lrc]|w.*)')
markup_strip = re.compile(r'^@([lrc]|w.*)')
col_width = [max(len(markup_strip.sub('', x)) for x in column) for column in zip(*table)]
trans = str.maketrans('lrcw', '<>^^')
lines = []
Expand Down
6 changes: 3 additions & 3 deletions nibabel/nicom/dicomwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ def vendor(self):
# Look at manufacturer tag first
mfgr = self.get('Manufacturer')
if mfgr:
if re.search('Siemens', mfgr, re.IGNORECASE):
if re.search(r'Siemens', mfgr, re.IGNORECASE):
return Vendor.SIEMENS
if re.search('Philips', mfgr, re.IGNORECASE):
if re.search(r'Philips', mfgr, re.IGNORECASE):
return Vendor.PHILIPS
if re.search('GE Medical', mfgr, re.IGNORECASE):
if re.search(r'GE Medical', mfgr, re.IGNORECASE):
return Vendor.GE
# Next look at UID prefixes
for uid_src in ('StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID'):
Expand Down
4 changes: 2 additions & 2 deletions nibabel/nicom/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_find_private_section_real():
# On real data first
assert fps(DATA, 0x29, 'SIEMENS CSA HEADER') == 0x1000
assert fps(DATA, 0x29, b'SIEMENS CSA HEADER') == 0x1000
assert fps(DATA, 0x29, re.compile('SIEMENS CSA HEADER')) == 0x1000
assert fps(DATA, 0x29, re.compile(r'SIEMENS CSA HEADER')) == 0x1000
assert fps(DATA, 0x29, 'NOT A HEADER') is None
assert fps(DATA, 0x29, 'SIEMENS MEDCOM HEADER2') == 0x1100
assert fps(DATA_PHILIPS, 0x29, 'SIEMENS CSA HEADER') == None
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_find_private_section_fake():
ds.add_new((0x11, 0x15), 'LO', b'far section')
assert fps(ds, 0x11, 'far section') == 0x1500
# More than one match - find the first.
assert fps(ds, 0x11, re.compile('(another|third) section')) == 0x1100
assert fps(ds, 0x11, re.compile(r'(another|third) section')) == 0x1100
# The signalling element number must be <= 0xFF
ds = pydicom.dataset.Dataset({})
ds.add_new((0x11, 0xFF), 'LO', b'some section')
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def test_str(self):
hdr = self.header_class()
s1 = str(hdr)
# check the datacode recoding
rexp = re.compile('^datatype +: float32', re.MULTILINE)
rexp = re.compile(r'^datatype +: float32', re.MULTILINE)
assert rexp.search(s1) is not None

def test_from_header(self):
Expand Down
10 changes: 5 additions & 5 deletions nibabel/tests/test_fileslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,16 +489,16 @@ def test_optimize_read_slicers():
(slice(None),),
)
# Check gap threshold with 3D
_depends0 = partial(threshold_heuristic, skip_thresh=10 * 4 - 1)
_depends1 = partial(threshold_heuristic, skip_thresh=10 * 4)
depends0 = partial(threshold_heuristic, skip_thresh=10 * 4 - 1)
depends1 = partial(threshold_heuristic, skip_thresh=10 * 4)
assert optimize_read_slicers(
(slice(9), slice(None), slice(None)), (10, 6, 2), 4, _depends0
(slice(9), slice(None), slice(None)), (10, 6, 2), 4, depends0
) == ((slice(None), slice(None), slice(None)), (slice(0, 9, 1), slice(None), slice(None)))
assert optimize_read_slicers(
(slice(None), slice(5), slice(None)), (10, 6, 2), 4, _depends0
(slice(None), slice(5), slice(None)), (10, 6, 2), 4, depends0
) == ((slice(None), slice(0, 5, 1), slice(None)), (slice(None), slice(None), slice(None)))
assert optimize_read_slicers(
(slice(None), slice(5), slice(None)), (10, 6, 2), 4, _depends1
(slice(None), slice(5), slice(None)), (10, 6, 2), 4, depends1
) == ((slice(None), slice(None), slice(None)), (slice(None), slice(0, 5, 1), slice(None)))
# Check longs as integer slices
sn = slice(None)
Expand Down
4 changes: 2 additions & 2 deletions nibabel/tests/test_loadsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_load_bad_compressed_extension(tmp_path, extension):
pytest.skip()
file_path = tmp_path / f'img.nii{extension}'
file_path.write_bytes(b'bad')
with pytest.raises(ImageFileError, match='.*is not a .* file'):
with pytest.raises(ImageFileError, match=r'.*is not a .* file'):
load(file_path)


Expand All @@ -99,7 +99,7 @@ def test_load_good_extension_with_bad_data(tmp_path, extension):
file_path = tmp_path / f'img.nii{extension}'
with Opener(file_path, 'wb') as fobj:
fobj.write(b'bad')
with pytest.raises(ImageFileError, match='Cannot work out file type of .*'):
with pytest.raises(ImageFileError, match=r'Cannot work out file type of .*'):
load(file_path)


Expand Down
18 changes: 9 additions & 9 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,11 @@ def test_slice_times(self):
hdr.set_slice_duration(0.1)
# We need a function to print out the Nones and floating point
# values in a predictable way, for the tests below.
_stringer = lambda val: f'{val:2.1f}' if val is not None else None
_print_me = lambda s: list(map(_stringer, s))
stringer = lambda val: f'{val:2.1f}' if val is not None else None
print_me = lambda s: list(map(stringer, s))
# The following examples are from the nifti1.h documentation.
hdr['slice_code'] = slice_order_codes['sequential increasing']
assert _print_me(hdr.get_slice_times()) == [
assert print_me(hdr.get_slice_times()) == [
'0.0',
'0.1',
'0.2',
Expand All @@ -553,17 +553,17 @@ def test_slice_times(self):
]
hdr['slice_start'] = 1
hdr['slice_end'] = 5
assert _print_me(hdr.get_slice_times()) == [None, '0.0', '0.1', '0.2', '0.3', '0.4', None]
assert print_me(hdr.get_slice_times()) == [None, '0.0', '0.1', '0.2', '0.3', '0.4', None]
hdr['slice_code'] = slice_order_codes['sequential decreasing']
assert _print_me(hdr.get_slice_times()) == [None, '0.4', '0.3', '0.2', '0.1', '0.0', None]
assert print_me(hdr.get_slice_times()) == [None, '0.4', '0.3', '0.2', '0.1', '0.0', None]
hdr['slice_code'] = slice_order_codes['alternating increasing']
assert _print_me(hdr.get_slice_times()) == [None, '0.0', '0.3', '0.1', '0.4', '0.2', None]
assert print_me(hdr.get_slice_times()) == [None, '0.0', '0.3', '0.1', '0.4', '0.2', None]
hdr['slice_code'] = slice_order_codes['alternating decreasing']
assert _print_me(hdr.get_slice_times()) == [None, '0.2', '0.4', '0.1', '0.3', '0.0', None]
assert print_me(hdr.get_slice_times()) == [None, '0.2', '0.4', '0.1', '0.3', '0.0', None]
hdr['slice_code'] = slice_order_codes['alternating increasing 2']
assert _print_me(hdr.get_slice_times()) == [None, '0.2', '0.0', '0.3', '0.1', '0.4', None]
assert print_me(hdr.get_slice_times()) == [None, '0.2', '0.0', '0.3', '0.1', '0.4', None]
hdr['slice_code'] = slice_order_codes['alternating decreasing 2']
assert _print_me(hdr.get_slice_times()) == [None, '0.4', '0.1', '0.3', '0.0', '0.2', None]
assert print_me(hdr.get_slice_times()) == [None, '0.4', '0.1', '0.3', '0.0', '0.2', None]
# test set
hdr = self.header_class()
hdr.set_dim_info(slice=2)
Expand Down
6 changes: 3 additions & 3 deletions nibabel/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ def set_volume_idx(self, v):

def _set_volume_index(self, v, update_slices=True):
"""Set the plot data using a volume index"""
v = self._data_idx[3] if v is None else int(round(v))
v = self._data_idx[3] if v is None else round(v)
if v == self._data_idx[3]:
return
max_ = np.prod(self._volume_dims)
self._data_idx[3] = max(min(int(round(v)), max_ - 1), 0)
self._data_idx[3] = max(min(round(v), max_ - 1), 0)
idx = (slice(None), slice(None), slice(None))
if self._data.ndim > 3:
idx = idx + tuple(np.unravel_index(self._data_idx[3], self._volume_dims))
Expand All @@ -401,7 +401,7 @@ def _set_position(self, x, y, z, notify=True):
idxs = np.dot(self._inv_affine, self._position)[:3]
idxs_new_order = idxs[self._order]
for ii, (size, idx) in enumerate(zip(self._sizes, idxs_new_order)):
self._data_idx[ii] = max(min(int(round(idx)), size - 1), 0)
self._data_idx[ii] = max(min(round(idx), size - 1), 0)
for ii in range(3):
# sagittal: get to S/A
# coronal: get to S/L
Expand Down

0 comments on commit 9d66d8a

Please sign in to comment.