Skip to content

Commit

Permalink
STY: Use f-strings where possible
Browse files Browse the repository at this point in the history
As suggested by `pyupgrade --py38-plus`:
https://github.com/asottile/pyupgrade#f-strings
  • Loading branch information
DimitriPapadopoulos committed Nov 21, 2023
1 parent be4ba94 commit c37102b
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 43 deletions.
6 changes: 2 additions & 4 deletions niworkflows/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,12 @@ def _run_interface(self, runtime):

if self._require_t1w and not bids_dict['t1w']:
raise FileNotFoundError(
"No T1w images found for subject sub-{}".format(self.inputs.subject_id)
f"No T1w images found for subject sub-{self.inputs.subject_id}"
)

if self._require_funcs and not bids_dict["bold"]:
raise FileNotFoundError(
"No functional images found for subject sub-{}".format(
self.inputs.subject_id
)
f"No functional images found for subject sub-{self.inputs.subject_id}"
)

for imtype in ["bold", "t2w", "flair", "fmap", "sbref", "roi", "pet"]:
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/interfaces/cifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,6 @@ def _create_cifti_image(
img.set_data_dtype(bold_img.get_data_dtype())
img.nifti_header.set_intent("NIFTI_INTENT_CONNECTIVITY_DENSE_SERIES")

out_file = "{}.dtseries.nii".format(split_filename(bold_file)[1])
out_file = f"{split_filename(bold_file)[1]}.dtseries.nii"
ci.save(img, out_file)
return Path.cwd() / out_file
6 changes: 3 additions & 3 deletions niworkflows/interfaces/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def spike_regressors(
spikes = np.zeros((max(indices) + 1, len(mask)))
for i, m in enumerate(sorted(mask)):
spikes[m, i] = 1
header = ["{:s}{:02d}".format(header_prefix, vol) for vol in range(len(mask))]
header = [f"{header_prefix}{vol:02d}" for vol in range(len(mask))]
spikes = pd.DataFrame(data=spikes, columns=header)
if concatenate:
return pd.concat((data, spikes), axis=1)
Expand Down Expand Up @@ -359,7 +359,7 @@ def temporal_derivatives(order, variables, data):
variables_deriv[0] = variables
order = set(order) - set([0])
for o in order:
variables_deriv[o] = ["{}_derivative{}".format(v, o) for v in variables]
variables_deriv[o] = [f"{v}_derivative{o}" for v in variables]
data_deriv[o] = np.tile(np.nan, data[variables].shape)
data_deriv[o][o:, :] = np.diff(data[variables], n=o, axis=0)
variables_deriv = reduce((lambda x, y: x + y), variables_deriv.values())
Expand Down Expand Up @@ -402,7 +402,7 @@ def exponential_terms(order, variables, data):
variables_exp[1] = variables
order = set(order) - set([1])
for o in order:
variables_exp[o] = ["{}_power{}".format(v, o) for v in variables]
variables_exp[o] = [f"{v}_power{o}" for v in variables]
data_exp[o] = data[variables] ** o
variables_exp = reduce((lambda x, y: x + y), variables_exp.values())
data_exp = pd.DataFrame(
Expand Down
6 changes: 3 additions & 3 deletions niworkflows/interfaces/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
_copyxform(
self.inputs.reference_image,
os.path.abspath(self._gen_filename("output_image")),
message="%s (niworkflows v%s)" % (self.__class__.__name__, __version__),
message=f"{self.__class__.__name__} (niworkflows v{__version__})",
)
return runtime

Expand Down Expand Up @@ -110,7 +110,7 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
_copyxform(
self.inputs.fixed_image[0],
os.path.abspath(out_file),
message="%s (niworkflows v%s)" % (self.__class__.__name__, __version__),
message=f"{self.__class__.__name__} (niworkflows v{__version__})",
)

# Inverse transform
Expand All @@ -119,7 +119,7 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
_copyxform(
self.inputs.moving_image[0],
os.path.abspath(out_file),
message="%s (niworkflows v%s)" % (self.__class__.__name__, __version__),
message=f"{self.__class__.__name__} (niworkflows v{__version__})",
)

return runtime
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/interfaces/freesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def medial_wall_to_nan(in_file, subjects_dir, den=None, newpath=None):
if target_subject.startswith("fsaverage"):
cortex = nb.freesurfer.read_label(
os.path.join(
subjects_dir, target_subject, "label", "{}.cortex.label".format(fn[:2])
subjects_dir, target_subject, "label", f"{fn[:2]}.cortex.label"
)
)
medial = np.delete(np.arange(len(func.darrays[0].data)), cortex)
Expand All @@ -578,7 +578,7 @@ def mri_info(fname, argument):
import subprocess as sp
import numpy as np

cmd_info = "mri_info --%s %s" % (argument, fname)
cmd_info = f"mri_info --{argument} {fname}"
proc = sp.Popen(cmd_info, stdout=sp.PIPE, shell=True)
data = bytearray(proc.stdout.read())
mstring = np.fromstring(data.decode("utf-8"), sep="\n")
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/interfaces/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def _run_interface(self, runtime):
Analyses of this dataset MAY BE INVALID.
</p>
"""
snippet = '<h3 class="elem-title">%s</h3>\n%s\n' % (warning_txt, description)
snippet = f'<h3 class="elem-title">{warning_txt}</h3>\n{description}\n'
# Store new file and report
img.to_filename(out_fname)
with open(out_report, "w") as fobj:
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/interfaces/nibabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def _run_interface(self, runtime):
self.inputs.moving_image,
fov_mask=self.inputs.fov_mask,
force_xform_code=self.inputs.xform_code,
message="%s (niworkflows v%s)" % (self.__class__.__name__, __version__),
message=f"{self.__class__.__name__} (niworkflows v{__version__})",
newpath=runtime.cwd,
)
return runtime
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/interfaces/tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def test_ReadSidecarJSON_connection(testdata_dir, field):
"derivatives, subjects_dir",
[
(os.getenv("FREESURFER_HOME"), "subjects"),
("/tmp", "%s/%s" % (os.getenv("FREESURFER_HOME"), "subjects")),
("/tmp", "{}/{}".format((os.getenv("FREESURFER_HOME"), "subjects"))),
],
)
def test_fsdir_noaction(derivatives, subjects_dir):
Expand Down
6 changes: 3 additions & 3 deletions niworkflows/interfaces/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ def _check_len(self, name, new):
if name in self._fields:
if isinstance(new, str) or len(new) < 1:
raise ValueError(
'Trying to set an invalid value (%s) for input "%s"' % (new, name)
f'Trying to set an invalid value ({new}) for input "{name}"'
)

if len(new) != len(self.inputs.keys):
raise ValueError(
'Length of value (%s) for input field "%s" does not match '
"the length of the indexing list." % (new, name)
f'Length of value ({new}) for input field "{name}" '
"does not match the length of the indexing list."
)

def _run_interface(self, runtime):
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/reports/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def _load_config(self, config):
self.out_dir = self.out_dir / self.packagename

if self.subject_id is not None:
self.root = self.root / "sub-{}".format(self.subject_id)
self.root = self.root / f"sub-{self.subject_id}"

if "template_path" in settings:
self.template_path = config.parent / settings["template_path"]
Expand Down Expand Up @@ -371,7 +371,7 @@ def index(self, config):

# Populate errors section
error_dir = (
self.out_dir / "sub-{}".format(self.subject_id) / "log" / self.run_uuid
self.out_dir / f"sub-{self.subject_id}" / "log" / self.run_uuid
)
if error_dir.is_dir():
from ..utils.misc import read_crashfile
Expand Down
2 changes: 1 addition & 1 deletion niworkflows/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,4 @@ def check_pipeline_version(cvers, data_desc):
desc = json.loads(data_desc.read_text())
dvers = desc.get("PipelineDescription", {}).get("Version", "0+unknown")
if Version(cvers).public != Version(dvers).public:
return "Previous output generated by version {} found.".format(dvers)
return f"Previous output generated by version {dvers} found."
17 changes: 7 additions & 10 deletions niworkflows/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,17 @@ def get_template_specs(
tpl_target_path = tf.get(in_template, **template_spec)
if not tpl_target_path:
raise RuntimeError(
"""\
Could not find template "{0}" with specs={1}. Please revise your template \
argument.""".format(
in_template, template_spec
)
f"""\
Could not find template "{in_template}" with specs={template_spec}. \
Please revise your template argument."""
)

if isinstance(tpl_target_path, list):
tpl_target_path = ", ".join([str(p) for p in tpl_target_path])
raise RuntimeError(
"""\
The available template modifiers ({0}) did not select a unique template \
(got "{1}"). Please revise your template argument.""".format(
template_spec, ", ".join([str(p) for p in tpl_target_path])
)
f"""\
The available template modifiers ({template_spec}) did not select a unique \
template (got "{tpl_target_path}"). Please revise your template argument."""
)

return str(tpl_target_path), common_spec
Expand Down
3 changes: 2 additions & 1 deletion niworkflows/utils/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def fullname(self):
"""
if "cohort" not in self.spec:
return self.space
return "%s:cohort-%s" % (self.space, self.spec["cohort"])
cohort = self.spec["cohort"]
return f"{self.space}:cohort-{self.spec['cohort']}"

@property
def legacyname(self):
Expand Down
4 changes: 2 additions & 2 deletions niworkflows/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_copy_gzip(tmpdir):
check_call(["gzip", "-N", str(filepath)])
assert not filepath.exists()

gzpath1 = "%s/%s" % (tmpdir, "name1.txt.gz")
gzpath2 = "%s/%s" % (tmpdir, "name2.txt.gz")
gzpath1 = f"{tmpdir}/name1.txt.gz"
gzpath2 = f"{tmpdir}/name2.txt.gz"
_copy_any(gzpath1, gzpath2)
assert Path(gzpath2).exists()
check_call(["gunzip", "-N", "-f", gzpath2])
Expand Down
10 changes: 5 additions & 5 deletions niworkflows/viz/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ def compcor_variance_plot(
if len(metadata_files) == 1:
metadata_sources = ["CompCor"]
else:
metadata_sources = ["Decomposition {:d}".format(i) for i in range(len(metadata_files))]
metadata_sources = [f"Decomposition {i:d}" for i in range(len(metadata_files))]
for file, source in zip(metadata_files, metadata_sources):
metadata[source] = pd.read_csv(str(file), sep=r"\s+")
metadata[source]["source"] = source
Expand Down Expand Up @@ -794,10 +794,10 @@ def compcor_variance_plot(
for m, (source, mask) in enumerate(decompositions):
components = metadata[(metadata["mask"] == mask) & (metadata["source"] == source)]
if len([m for s, m in decompositions if s == source]) > 1:
title_mask = " ({} mask)".format(mask)
title_mask = f" ({mask} mask)"
else:
title_mask = ""
fig_title = "{}{}".format(source, title_mask)
fig_title = f"{source}{title_mask}"

ax[m].plot(
np.arange(components.shape[0] + 1),
Expand All @@ -818,7 +818,7 @@ def compcor_variance_plot(
+ 1
)
ax[m].axhline(y=100 * thr, color="lightgrey", linewidth=0.25)
ax[m].axvline(x=varexp[thr], color="C{}".format(i), linewidth=2, linestyle=":")
ax[m].axvline(x=varexp[thr], color=f"C{i}", linewidth=2, linestyle=":")
ax[m].text(
0,
100 * thr,
Expand Down Expand Up @@ -949,7 +949,7 @@ def confounds_correlation_plot(
)

ax1.set_xlabel("Confound time series")
ax1.set_ylabel("Magnitude of correlation with {}".format(reference))
ax1.set_ylabel(f"Magnitude of correlation with {reference}")
ax1.tick_params(axis="x", which="both", width=0)
ax1.tick_params(axis="y", which="both", width=5, length=5)

Expand Down
6 changes: 3 additions & 3 deletions niworkflows/viz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def plot_segs(
image_nii, segs=seg_niis, compress=compress, **plot_params
)
# Find and replace the figure_1 id.
svg = svg.replace("figure_1", "segmentation-%s-%s" % (d, uuid4()), 1)
svg = svg.replace("figure_1", f"segmentation-{d}-{uuid4()}", 1)
out_files.append(fromstring(svg))

return out_files
Expand Down Expand Up @@ -389,7 +389,7 @@ def plot_registration(
display.close()

# Find and replace the figure_1 id.
svg = svg.replace("figure_1", "%s-%s-%s" % (div_id, mode, uuid4()), 1)
svg = svg.replace("figure_1", f"{div_id}-{mode}-{uuid4()}", 1)
out_files.append(fromstring(svg))

return out_files
Expand Down Expand Up @@ -631,7 +631,7 @@ def plot_melodic_components(
if noise_components.size == n_components:
ncomps = "ALL"
ax.annotate(
"WARNING: {} components were classified as noise".format(ncomps),
f"WARNING: {ncomps} components were classified as noise",
xy=(0.0, 0.5),
xycoords="axes fraction",
xytext=(0.01, 0.5),
Expand Down

0 comments on commit c37102b

Please sign in to comment.