Skip to content

Commit 6b15a97

Browse files
committed
A bit more f-strings, but that's it
1 parent 2271c97 commit 6b15a97

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

nibabel/streamlines/tck.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ def _read_header(fileobj):
330330
hdr['datatype'] = "Float32LE"
331331

332332
if not hdr['datatype'].startswith('Float32'):
333-
msg = ("TCK only supports float32 dtype but 'datatype: {}' was"
334-
" specified in the header.").format(hdr['datatype'])
333+
msg = (f"TCK only supports float32 dtype but 'datatype: "
334+
f"{hdr['datatype']}' was specified in the header.")
335335
raise HeaderError(msg)
336336

337337
if 'file' not in hdr:
@@ -341,9 +341,9 @@ def _read_header(fileobj):
341341
hdr['file'] = f'. {offset_data}'
342342

343343
if hdr['file'].split()[0] != '.':
344-
msg = ("TCK only supports single-file - in other words the"
345-
" filename part must be specified as '.' but '{}' was"
346-
" specified.").format(hdr['file'].split()[0])
344+
msg = (f"TCK only supports single-file - in other words the"
345+
f" filename part must be specified as '.' but "
346+
f"'{hdr['file'].split()[0]}' was specified.")
347347
raise HeaderError("Missing 'file' attribute in TCK header.")
348348

349349
# Set endianness and _dtype attributes in the header.
@@ -454,6 +454,6 @@ def __str__(self):
454454
info = ""
455455
info += f"\nMAGIC NUMBER: {hdr[Field.MAGIC_NUMBER]}"
456456
info += "\n"
457-
info += "\n".join(["{}: {}".format(k, v)
457+
info += "\n".join([f"{k}: {v}"
458458
for k, v in hdr.items() if not k.startswith('_')])
459459
return info

nibabel/streamlines/tractogram.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def __setitem__(self, key, value):
109109

110110
# We make sure there is the right amount of values
111111
if self.n_rows > 0 and len(value) != self.n_rows:
112-
msg = ("The number of values ({0}) should match n_elements "
113-
"({1}).").format(len(value), self.n_rows)
112+
msg = (f"The number of values ({len(value)}) should match "
113+
f"n_elements ({self.n_rows}).")
114114
raise ValueError(msg)
115115

116116
self.store[key] = value
@@ -140,10 +140,9 @@ def extend(self, other):
140140
"""
141141
if (len(self) > 0 and len(other) > 0 and
142142
sorted(self.keys()) != sorted(other.keys())):
143-
msg = ("Entry mismatched between the two PerArrayDict objects."
144-
" This PerArrayDict contains '{0}' whereas the other "
145-
" contains '{1}'.").format(sorted(self.keys()),
146-
sorted(other.keys()))
143+
msg = (f"Entry mismatched between the two PerArrayDict objects. "
144+
f"This PerArrayDict contains '{sorted(self.keys())}' "
145+
f"whereas the other contains '{sorted(other.keys())}'.")
147146
raise ValueError(msg)
148147

149148
self.n_rows += other.n_rows
@@ -365,8 +364,8 @@ def affine_to_rasmm(self, value):
365364
if value is not None:
366365
value = np.array(value)
367366
if value.shape != (4, 4):
368-
msg = ("Affine matrix has a shape of (4, 4) but a ndarray with"
369-
"shape {} was provided instead.").format(value.shape)
367+
msg = (f"Affine matrix has a shape of (4, 4) but a ndarray with"
368+
f"shape {value.shape} was provided instead.")
370369
raise ValueError(msg)
371370

372371
self._affine_to_rasmm = value

nibabel/streamlines/trk.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,14 @@ def encode_value_in_name(value, name, max_name_len=20):
153153
`value`, padded with ``\x00`` bytes.
154154
"""
155155
if len(name) > max_name_len:
156-
msg = ("Data information named '{0}' is too long"
157-
" (max {1} characters.)").format(name, max_name_len)
156+
msg = (f"Data information named '{name}' is too long"
157+
f" (max {max_name_len} characters.)")
158158
raise ValueError(msg)
159159
encoded_name = name if value <= 1 else name + '\x00' + str(value)
160160
if len(encoded_name) > max_name_len:
161-
msg = ("Data information named '{0}' is too long (need to be less"
162-
" than {1} characters when storing more than one value"
163-
" for a given data information."
164-
).format(name, max_name_len - (len(str(value)) + 1))
161+
msg = (f"Data information named '{name}' is too long (need to be less"
162+
f" than {max_name_len - (len(str(value)) + 1)} characters "
163+
f"when storing more than one value for a given data information.")
165164
raise ValueError(msg)
166165
# Fill to the end with zeros
167166
return encoded_name.ljust(max_name_len, '\x00').encode('latin1')
@@ -196,8 +195,8 @@ def decode_value_from_name(encoded_name):
196195
value = int(splits[1]) # Decode value.
197196
elif len(splits) > 2:
198197
# The remaining bytes are not \x00, raising.
199-
msg = ("Wrong scalar_name or property_name: '{0}'."
200-
" Unused characters should be \\x00.").format(encoded_name)
198+
msg = (f"Wrong scalar_name or property_name: '{encoded_name}'."
199+
f" Unused characters should be \\x00.")
201200
raise HeaderError(msg)
202201

203202
return name, value
@@ -473,9 +472,9 @@ def save(self, fileobj):
473472
# Update field 'property_name' using 'data_per_streamline'.
474473
data_for_streamline = first_item.data_for_streamline
475474
if len(data_for_streamline) > MAX_NB_NAMED_PROPERTIES_PER_STREAMLINE:
476-
msg = ("Can only store {0} named data_per_streamline (also"
477-
" known as 'properties' in the TRK format)."
478-
).format(MAX_NB_NAMED_SCALARS_PER_POINT)
475+
msg = (f"Can only store {MAX_NB_NAMED_SCALARS_PER_POINT} named "
476+
f"data_per_streamline (also known as 'properties' in the "
477+
f"TRK format).")
479478
raise ValueError(msg)
480479

481480
data_for_streamline_keys = sorted(data_for_streamline.keys())
@@ -491,9 +490,9 @@ def save(self, fileobj):
491490
# Update field 'scalar_name' using 'tractogram.data_per_point'.
492491
data_for_points = first_item.data_for_points
493492
if len(data_for_points) > MAX_NB_NAMED_SCALARS_PER_POINT:
494-
msg = ("Can only store {0} named data_per_point (also known"
495-
" as 'scalars' in the TRK format)."
496-
).format(MAX_NB_NAMED_SCALARS_PER_POINT)
493+
msg = (f"Can only store {MAX_NB_NAMED_SCALARS_PER_POINT} "
494+
f"named data_per_point (also known as 'scalars' in "
495+
f"the TRK format).")
497496
raise ValueError(msg)
498497

499498
data_for_points_keys = sorted(data_for_points.keys())
@@ -588,9 +587,9 @@ def _read_header(fileobj):
588587
# Swap byte order
589588
header_rec = header_rec.newbyteorder()
590589
if header_rec['hdr_size'] != TrkFile.HEADER_SIZE:
591-
msg = "Invalid hdr_size: {0} instead of {1}"
592-
raise HeaderError(msg.format(header_rec['hdr_size'],
593-
TrkFile.HEADER_SIZE))
590+
msg = (f"Invalid hdr_size: {header_rec['hdr_size']} "
591+
f"instead of {TrkFile.HEADER_SIZE}")
592+
raise HeaderError(msg)
594593

595594
if header_rec['version'] == 1:
596595
# There is no 4x4 matrix for voxel to RAS transformation.
@@ -616,9 +615,9 @@ def _read_header(fileobj):
616615
# able to determine the axis directions.
617616
axcodes = aff2axcodes(header[Field.VOXEL_TO_RASMM])
618617
if None in axcodes:
619-
msg = ("The 'vox_to_ras' affine is invalid! Could not"
620-
" determine the axis directions from it.\n{0}"
621-
).format(header[Field.VOXEL_TO_RASMM])
618+
msg = (f"The 'vox_to_ras' affine is invalid! Could not"
619+
f" determine the axis directions from it.\n"
620+
f"{header[Field.VOXEL_TO_RASMM]}")
622621
raise HeaderError(msg)
623622

624623
# By default, the voxel order is LPS.

nibabel/tests/test_analyze.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ def test_log_checks(self):
141141
fhdr, message, raiser = self.log_chk(hdr, 30)
142142

143143
assert fhdr['sizeof_hdr'] == self.sizeof_hdr
144-
assert message == 'sizeof_hdr should be {0}; set sizeof_hdr to {0}'.format(self.sizeof_hdr)
144+
assert (message == f'sizeof_hdr should be {self.sizeof_hdr}; '
145+
f'set sizeof_hdr to {self.sizeof_hdr}')
145146
pytest.raises(*raiser)
146147
# RGB datatype does not raise error
147148
hdr = HC()

0 commit comments

Comments
 (0)