Skip to content

Commit

Permalink
STY: Apply ruff/pyupgrade preview rule UP031
Browse files Browse the repository at this point in the history
UP031 Use format specifiers instead of percent format
  • Loading branch information
DimitriPapadopoulos committed Dec 21, 2024
1 parent b7b4a69 commit 10a5369
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions niworkflows/anat/ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,14 @@ def init_atropos_wf(

# De-pad
depad_mask = pe.Node(
ImageMath(operation='PadImage', op2='-%d' % padding), name='23_depad_mask'
ImageMath(operation='PadImage', op2=f'-{padding}'), name='23_depad_mask'
)
depad_segm = pe.Node(
ImageMath(operation='PadImage', op2='-%d' % padding), name='24_depad_segm'
ImageMath(operation='PadImage', op2=f'-{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_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')

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 +1052,7 @@ def _select_labels(in_segm, labels):
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)
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 @@ def _applytfms(args):

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 @@ def _arrange_xfms(transforms, num_files, tmp_folder):

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}')
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
4 changes: 2 additions & 2 deletions niworkflows/interfaces/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _run_interface(self, runtime):
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}')
if term_out:
NIWORKFLOWS_LOG.warning('Log of failed retry saved (%s).', ', '.join(term_out))
else:
Expand All @@ -236,7 +236,7 @@ def _run_interface(self, runtime):

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

def _get_ants_args(self):
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 @@ def test_applytfms(tmpdir, ext, copy_dtype, in_dtype):
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}'))

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})'
)

0 comments on commit 10a5369

Please sign in to comment.