Skip to content
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

STY: Use f-strings where possible #836

Merged
merged 1 commit into from
Dec 21, 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
16 changes: 6 additions & 10 deletions niworkflows/anat/ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,11 @@
me_7_2 = pe.Node(ImageMath(operation='ME', op2='5'), name='22_me_7_2')

# De-pad
depad_mask = pe.Node(
ImageMath(operation='PadImage', op2='-%d' % padding), name='23_depad_mask'
)
depad_segm = pe.Node(
ImageMath(operation='PadImage', op2='-%d' % padding), name='24_depad_segm'
)
depad_gm = pe.Node(ImageMath(operation='PadImage', op2='-%d' % padding), name='25_depad_gm')
depad_wm = pe.Node(ImageMath(operation='PadImage', op2='-%d' % padding), name='26_depad_wm')
depad_csf = pe.Node(ImageMath(operation='PadImage', op2='-%d' % padding), name='27_depad_csf')
depad_mask = pe.Node(ImageMath(operation='PadImage', op2=f'-{padding}'), name='23_depad_mask')
depad_segm = pe.Node(ImageMath(operation='PadImage', op2=f'-{padding}'), name='24_depad_segm')
depad_gm = pe.Node(ImageMath(operation='PadImage', op2=f'-{padding}'), name='25_depad_gm')
depad_wm = pe.Node(ImageMath(operation='PadImage', op2=f'-{padding}'), name='26_depad_wm')
depad_csf = pe.Node(ImageMath(operation='PadImage', op2=f'-{padding}'), name='27_depad_csf')

Check warning on line 723 in niworkflows/anat/ants.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/anat/ants.py#L719-L723

Added lines #L719 - L723 were not covered by tests

msk_conform = pe.Node(niu.Function(function=_conform_mask), name='msk_conform')
merge_tpms = pe.Node(niu.Merge(in_segmentation_model[0]), name='merge_tpms')
Expand Down Expand Up @@ -1052,7 +1048,7 @@
for label in labels:
newnii = nii.__class__(np.uint8(label_data == label), nii.affine, nii.header)
newnii.set_data_dtype('uint8')
out_file = fname_presuffix(in_segm, suffix='_class-%02d' % label, newpath=cwd)
out_file = fname_presuffix(in_segm, suffix=f'_class-{label:02d}', newpath=cwd)

Check warning on line 1051 in niworkflows/anat/ants.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/anat/ants.py#L1051

Added line #L1051 was not covered by tests
newnii.to_filename(out_file)
out_files.append(out_file)
return out_files
Expand Down
10 changes: 5 additions & 5 deletions niworkflows/interfaces/itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@

in_file, in_xform, ifargs, index, newpath = args
out_file = fname_presuffix(
in_file, suffix='_xform-%05d' % index, newpath=newpath, use_ext=True
in_file, suffix=f'_xform-{index:05d}', newpath=newpath, use_ext=True
)

copy_dtype = ifargs.pop('copy_dtype', False)
Expand Down Expand Up @@ -244,19 +244,19 @@

if nxforms != num_files:
raise RuntimeError(
'Number of transforms (%d) found in the ITK file does not match'
' the number of input image files (%d).' % (nxforms, num_files)
f'Number of transforms ({nxforms}) found in the ITK file does not'
f' match the number of input image files ({num_files}).'
)

# At this point splitting transforms will be necessary, generate a base name
out_base = fname_presuffix(
tf_file, suffix='_pos-%03d_xfm-{:05d}' % i, newpath=tmp_folder.name
tf_file, suffix=f'_pos-{i:03d}_xfm-{{:05d}}', newpath=tmp_folder.name
).format
# Split combined ITK transforms file
split_xfms = []
for xform_i in range(nxforms):
# Find start token to extract
startidx = tfdata.index('#Transform %d' % xform_i)
startidx = tfdata.index(f'#Transform {xform_i}')

Check warning on line 259 in niworkflows/interfaces/itk.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/itk.py#L259

Added line #L259 was not covered by tests
next_xform = base_xform + tfdata[startidx + 1 : startidx + 4] + ['']
xfm_file = out_base(xform_i)
with open(xfm_file, 'w') as out_xfm:
Expand Down
6 changes: 2 additions & 4 deletions niworkflows/interfaces/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
if interface_result.runtime.returncode != 0:
NIWORKFLOWS_LOG.warning('Retry #%d failed.', self.retry)
# Save outputs (if available)
term_out = _write_outputs(interface_result.runtime, '.nipype-%04d' % self.retry)
term_out = _write_outputs(interface_result.runtime, f'.nipype-{self.retry:04d}')

Check warning on line 225 in niworkflows/interfaces/norm.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/norm.py#L225

Added line #L225 was not covered by tests
if term_out:
NIWORKFLOWS_LOG.warning('Log of failed retry saved (%s).', ', '.join(term_out))
else:
Expand All @@ -235,9 +235,7 @@
self.retry += 1

# If all tries fail, raise an error.
raise RuntimeError(
'Robust spatial normalization failed after %d retries.' % (self.retry - 1)
)
raise RuntimeError(f'Robust spatial normalization failed after {self.retry - 1} retries.')

Check warning on line 238 in niworkflows/interfaces/norm.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/norm.py#L238

Added line #L238 was not covered by tests

def _get_ants_args(self):
args = {
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/interfaces/tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def test_signal_extraction_equivalence(tmp_path, nvols, nmasks, ext, factor):
se1 = nl.SignalExtraction(
in_file=img_fname,
label_files=masks_fname,
class_labels=['a%d' % i for i in range(nmasks)],
class_labels=[f'a{i}' for i in range(nmasks)],
out_file=nlsignals,
)
se2 = im.SignalExtraction(
in_file=img_fname,
label_files=masks_fname,
class_labels=['a%d' % i for i in range(nmasks)],
class_labels=[f'a{i}' for i in range(nmasks)],
out_file=imsignals,
)

Expand Down
2 changes: 1 addition & 1 deletion niworkflows/interfaces/tests/test_itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
args = (in_file, in_xform, ifargs, 0, str(tmpdir))
out_file, cmdline = _applytfms(args)

assert out_file == str(tmpdir / ('src_xform-%05d%s' % (0, ext)))
assert out_file == str(tmpdir / (f'src_xform-00000{ext}'))

Check warning on line 55 in niworkflows/interfaces/tests/test_itk.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/tests/test_itk.py#L55

Added line #L55 was not covered by tests

out_nii = nb.load(out_file)
assert np.allclose(nii.affine, out_nii.affine)
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/reports/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def generate_reports(

logger = logging.getLogger('cli')
error_list = ', '.join(
'%s (%d)' % (subid, err) for subid, err in zip(subject_list, report_errors) if err
f'{subid} ({err})' for subid, err in zip(subject_list, report_errors) if err
)
logger.error(
'Preprocessing did not finish successfully. Errors occurred while processing '
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/tests/test_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_ROIsPlot2(tmp_path):
for i in range(1, 5):
seg = np.zeros_like(newdata, dtype='uint8')
seg[(newdata > 0) & (newdata <= i)] = 1
out_file = str(tmp_path / ('segments%02d.nii.gz' % i))
out_file = str(tmp_path / f'segments{i:02d}.nii.gz')
nb.Nifti1Image(seg, im.affine, hdr).to_filename(out_file)
out_files.append(out_file)
roi_rpt = ROIsPlot(
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ def test_compression(tmp_path):
size = int(os.stat(uncompressed).st_size)
size_compress = int(os.stat(compressed).st_size)
assert size >= size_compress, (
'The uncompressed report is smaller (%d)'
'than the compressed report (%d)' % (size, size_compress)
f'The uncompressed report is smaller ({size})'
f'than the compressed report ({size_compress})'
)
Loading