Skip to content

Commit 8f77bf1

Browse files
committed
STY: pyupgrade --py37-plus
Mostly motivated by excessive use of arguments to super(). Also caught a number of `np.array(X).astype(Y)` to convert to `np.array(X, Y)`. [git-blame-ignore-rev]
1 parent 2a322ff commit 8f77bf1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+218
-257
lines changed

nibabel/analyze.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def __init__(self, binaryblock=None, endianness=None, check=True):
248248
>>> hdr4.endianness == swapped_code
249249
True
250250
"""
251-
super(AnalyzeHeader, self).__init__(binaryblock, endianness, check)
251+
super().__init__(binaryblock, endianness, check)
252252

253253
@classmethod
254254
def guessed_endian(klass, hdr):
@@ -336,7 +336,7 @@ def guessed_endian(klass, hdr):
336336
@classmethod
337337
def default_structarr(klass, endianness=None):
338338
"""Return header data for empty header with given endianness"""
339-
hdr_data = super(AnalyzeHeader, klass).default_structarr(endianness)
339+
hdr_data = super().default_structarr(endianness)
340340
hdr_data['sizeof_hdr'] = klass.sizeof_hdr
341341
hdr_data['dim'] = 1
342342
hdr_data['dim'][0] = 0
@@ -904,7 +904,7 @@ class AnalyzeImage(SpatialImage):
904904
ImageArrayProxy = ArrayProxy
905905

906906
def __init__(self, dataobj, affine, header=None, extra=None, file_map=None, dtype=None):
907-
super(AnalyzeImage, self).__init__(dataobj, affine, header, extra, file_map)
907+
super().__init__(dataobj, affine, header, extra, file_map)
908908
# Reset consumable values
909909
self._header.set_data_offset(0)
910910
self._header.set_slope_inter(None, None)

nibabel/arraywriters.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def scaling_needed(self):
313313
data are within range of the output type, return False
314314
* Otherwise return True
315315
"""
316-
if not super(SlopeArrayWriter, self).scaling_needed():
316+
if not super().scaling_needed():
317317
return False
318318
mn, mx = self.finite_range() # this is cached
319319
# No finite data - no scaling needed
@@ -428,7 +428,7 @@ def _range_scale(self, in_min, in_max):
428428
# not lose precision because min/max are of fp type.
429429
out_min, out_max = np.array((out_min, out_max), dtype=big_float)
430430
else: # (u)int
431-
out_min, out_max = [int_to_float(v, big_float) for v in (out_min, out_max)]
431+
out_min, out_max = (int_to_float(v, big_float) for v in (out_min, out_max))
432432
if self._out_dtype.kind == 'u':
433433
if in_min < 0 and in_max > 0:
434434
raise WriterError(
@@ -507,13 +507,11 @@ def __init__(self, array, out_dtype=None, calc_scale=True, scaler_dtype=np.float
507507
>>> (aw.slope, aw.inter) == (1.0, 128)
508508
True
509509
"""
510-
super(SlopeInterArrayWriter, self).__init__(
511-
array, out_dtype, calc_scale, scaler_dtype, **kwargs
512-
)
510+
super().__init__(array, out_dtype, calc_scale, scaler_dtype, **kwargs)
513511

514512
def reset(self):
515513
"""Set object to values before any scaling calculation"""
516-
super(SlopeInterArrayWriter, self).reset()
514+
super().reset()
517515
self.inter = 0.0
518516

519517
def _get_inter(self):
@@ -549,14 +547,14 @@ def to_fileobj(self, fileobj, order='F'):
549547

550548
def _iu2iu(self):
551549
# (u)int to (u)int
552-
mn, mx = [as_int(v) for v in self.finite_range()]
550+
mn, mx = (as_int(v) for v in self.finite_range())
553551
# range may be greater than the largest integer for this type.
554552
# as_int needed to work round numpy 1.4.1 int casting bug
555553
out_dtype = self._out_dtype
556554
# Options in this method are scaling using intercept only. These will
557555
# have to pass through ``self.scaler_dtype`` (because the intercept is
558556
# in this type).
559-
o_min, o_max = [as_int(v) for v in shared_range(self.scaler_dtype, out_dtype)]
557+
o_min, o_max = (as_int(v) for v in shared_range(self.scaler_dtype, out_dtype))
560558
type_range = o_max - o_min
561559
mn2mx = mx - mn
562560
if mn2mx <= type_range: # might offset be enough?
@@ -579,7 +577,7 @@ def _iu2iu(self):
579577
self.inter = inter
580578
return
581579
# Try slope options (sign flip) and then range scaling
582-
super(SlopeInterArrayWriter, self)._iu2iu()
580+
super()._iu2iu()
583581

584582
def _range_scale(self, in_min, in_max):
585583
"""Calculate scaling, intercept based on data range and output type"""
@@ -604,7 +602,7 @@ def _range_scale(self, in_min, in_max):
604602
in_min, in_max = as_int(in_min), as_int(in_max)
605603
in_range = int_to_float(in_max - in_min, big_float)
606604
# Cast to float for later processing.
607-
in_min, in_max = [int_to_float(v, big_float) for v in (in_min, in_max)]
605+
in_min, in_max = (int_to_float(v, big_float) for v in (in_min, in_max))
608606
if out_dtype.kind == 'f':
609607
# Type range, these are also floats
610608
info = type_info(out_dtype)

nibabel/brikhead.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def parse_AFNI_header(fobj):
195195
"""
196196
# edge case for being fed a filename instead of a file object
197197
if isinstance(fobj, str):
198-
with open(fobj, 'rt') as src:
198+
with open(fobj) as src:
199199
return parse_AFNI_header(src)
200200
# unpack variables in HEAD file
201201
head = fobj.read().split('\n\n')
@@ -239,9 +239,7 @@ def __init__(self, file_like, header, *, mmap=True, keep_file_open=None):
239239
effect. The default value (``None``) will result in the value of
240240
``nibabel.arrayproxy.KEEP_FILE_OPEN_DEFAULT`` being used.
241241
"""
242-
super(AFNIArrayProxy, self).__init__(
243-
file_like, header, mmap=mmap, keep_file_open=keep_file_open
244-
)
242+
super().__init__(file_like, header, mmap=mmap, keep_file_open=keep_file_open)
245243
self._scaling = header.get_data_scaling()
246244

247245
@property
@@ -293,9 +291,7 @@ def __init__(self, info):
293291
"""
294292
self.info = info
295293
dt = _get_datatype(self.info)
296-
super(AFNIHeader, self).__init__(
297-
data_dtype=dt, shape=self._calc_data_shape(), zooms=self._calc_zooms()
298-
)
294+
super().__init__(data_dtype=dt, shape=self._calc_data_shape(), zooms=self._calc_zooms())
299295

300296
@classmethod
301297
def from_header(klass, header=None):
@@ -553,7 +549,7 @@ def filespec_to_file_map(klass, filespec):
553549
If `filespec` is not recognizable as being a filename for this
554550
image type.
555551
"""
556-
file_map = super(AFNIImage, klass).filespec_to_file_map(filespec)
552+
file_map = super().filespec_to_file_map(filespec)
557553
# check for AFNI-specific BRIK/HEAD compression idiosyncrasies
558554
for key, fholder in file_map.items():
559555
fname = fholder.filename

nibabel/cifti2/cifti2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ def __init__(
14511451
"""
14521452
if not isinstance(header, Cifti2Header) and header:
14531453
header = Cifti2Header.from_axes(header)
1454-
super(Cifti2Image, self).__init__(dataobj, header=header, extra=extra, file_map=file_map)
1454+
super().__init__(dataobj, header=header, extra=extra, file_map=file_map)
14551455
self._nifti_header = LimitedNifti2Header.from_header(nifti_header)
14561456

14571457
# if NIfTI header not specified, get data type from input array

nibabel/cifti2/parse_cifti2.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _valid_intent_code(klass, intent_code):
9191

9292
@classmethod
9393
def may_contain_header(klass, binaryblock):
94-
if not super(_Cifti2AsNiftiHeader, klass).may_contain_header(binaryblock):
94+
if not super().may_contain_header(binaryblock):
9595
return False
9696
hdr = klass(binaryblock=binaryblock[: klass.sizeof_hdr])
9797
return klass._valid_intent_code(hdr.get_intent('code')[0])
@@ -135,9 +135,7 @@ class Cifti2Parser(xml.XmlParser):
135135
"""Class to parse an XML string into a CIFTI-2 header object"""
136136

137137
def __init__(self, encoding=None, buffer_size=3500000, verbose=0):
138-
super(Cifti2Parser, self).__init__(
139-
encoding=encoding, buffer_size=buffer_size, verbose=verbose
140-
)
138+
super().__init__(encoding=encoding, buffer_size=buffer_size, verbose=verbose)
141139
self.fsm_state = []
142140
self.struct_state = []
143141

nibabel/cmdline/diff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def get_data_diff(files, max_abs=0, max_rel=0, dtype=np.float64):
248248
# Since we operated on sub-selected values already, we need
249249
# to plug them back in
250250
candidates[
251-
tuple((indexes[sub_thr] for indexes in np.where(candidates)))
251+
tuple(indexes[sub_thr] for indexes in np.where(candidates))
252252
] = False
253253
max_rel_diff = np.max(rel_diff)
254254
else:

nibabel/cmdline/roi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def lossless_slice(img, slicers):
2222
def parse_slice(crop, allow_step=True):
2323
if crop is None:
2424
return slice(None)
25-
start, stop, *extra = [int(val) if val else None for val in crop.split(':')]
25+
start, stop, *extra = (int(val) if val else None for val in crop.split(':'))
2626
if len(extra) > 1:
2727
raise ValueError(f'Cannot parse specification: {crop}')
2828
if not allow_step and extra and extra[0] not in (1, None):

0 commit comments

Comments
 (0)