From e09e36a701dcbdb30d127e1cc6739b6214e4bf90 Mon Sep 17 00:00:00 2001
From: Dimitri Papadopoulos
<3234522+DimitriPapadopoulos@users.noreply.github.com>
Date: Tue, 21 Nov 2023 20:10:20 +0100
Subject: [PATCH] STY: Apply pyupgrade suggestions
pyupgrade --py310-plus
Also manually change a few `.format()` calls to f-strings.
---
fmriprep/cli/parser.py | 4 +---
fmriprep/data/__init__.py | 2 +-
fmriprep/interfaces/reports.py | 17 +++++++--------
fmriprep/interfaces/workbench.py | 4 ++--
fmriprep/utils/asynctools.py | 3 ++-
fmriprep/utils/bids.py | 2 +-
fmriprep/workflows/base.py | 36 +++++++++++++-------------------
7 files changed, 29 insertions(+), 39 deletions(-)
diff --git a/fmriprep/cli/parser.py b/fmriprep/cli/parser.py
index 69ac637e4..2e9d15e9e 100644
--- a/fmriprep/cli/parser.py
+++ b/fmriprep/cli/parser.py
@@ -109,9 +109,7 @@ def _slice_time_ref(value, parser):
is_release = not any((currentv.is_devrelease, currentv.is_prerelease, currentv.is_postrelease))
parser = ArgumentParser(
- description="fMRIPrep: fMRI PREProcessing workflows v{}".format(
- config.environment.version
- ),
+ description=f"fMRIPrep: fMRI PREProcessing workflows v{config.environment.version}",
formatter_class=ArgumentDefaultsHelpFormatter,
**kwargs,
)
diff --git a/fmriprep/data/__init__.py b/fmriprep/data/__init__.py
index 98ab4cfb9..9b913687c 100644
--- a/fmriprep/data/__init__.py
+++ b/fmriprep/data/__init__.py
@@ -110,7 +110,7 @@ class Loader:
.. automethod:: cached
"""
- def __init__(self, anchor: Union[str, ModuleType]):
+ def __init__(self, anchor: str | ModuleType):
self._anchor = anchor
self.files = files(anchor)
self.exit_stack = ExitStack()
diff --git a/fmriprep/interfaces/reports.py b/fmriprep/interfaces/reports.py
index 445a05aa5..c46d9efa8 100644
--- a/fmriprep/interfaces/reports.py
+++ b/fmriprep/interfaces/reports.py
@@ -169,10 +169,9 @@ def _generate_segment(self):
if counts:
header = '\t\t
'
+ s = '' if n_runs == 1 else 's'
lines = [
- '\t\t\tTask: {task_id} ({n_runs:d} run{s})'.format(
- task_id=task_id, n_runs=n_runs, s='' if n_runs == 1 else 's'
- )
+ f'\t\t\tTask: {task_id} ({n_runs:d} run{s})'
for task_id, n_runs in sorted(counts.items())
]
tasks = '\n'.join([header] + lines + [footer])
@@ -255,20 +254,20 @@ def _generate_segment(self):
pedir = get_world_pedir(self.inputs.orientation, self.inputs.pe_direction)
- dummy_scan_tmp = "{n_dum}"
+ dummy_scan_tmp = f"{n_dum}"
if self.inputs.dummy_scans == self.inputs.algo_dummy_scans:
dummy_scan_msg = ' '.join(
- [dummy_scan_tmp, "(Confirmed: {n_alg} automatically detected)"]
- ).format(n_dum=self.inputs.dummy_scans, n_alg=self.inputs.algo_dummy_scans)
+ [dummy_scan_tmp, f"(Confirmed: {self.inputs.algo_dummy_scans} automatically detected)"]
+ )
# the number of dummy scans was specified by the user and
# it is not equal to the number detected by the algorithm
elif self.inputs.dummy_scans is not None:
dummy_scan_msg = ' '.join(
- [dummy_scan_tmp, "(Warning: {n_alg} automatically detected)"]
- ).format(n_dum=self.inputs.dummy_scans, n_alg=self.inputs.algo_dummy_scans)
+ [dummy_scan_tmp, f"(Warning: {self.inputs.algo_dummy_scans} automatically detected)"]
+ )
# the number of dummy scans was not specified by the user
else:
- dummy_scan_msg = dummy_scan_tmp.format(n_dum=self.inputs.algo_dummy_scans)
+ dummy_scan_msg = dummy_scan_tmp
multiecho = "Single-echo EPI sequence."
n_echos = len(self.inputs.echo_idx)
diff --git a/fmriprep/interfaces/workbench.py b/fmriprep/interfaces/workbench.py
index f13fb5b67..2a6cd1228 100644
--- a/fmriprep/interfaces/workbench.py
+++ b/fmriprep/interfaces/workbench.py
@@ -282,7 +282,7 @@ def _format_arg(self, opt, spec, val):
if opt in ("current_area", "new_area"):
if not self.inputs.area_surfs and not self.inputs.area_metrics:
raise ValueError(
- "{} was set but neither area_surfs or" " area_metrics were set".format(opt)
+ f"{opt} was set but neither area_surfs or area_metrics were set"
)
if opt == "method":
if (
@@ -290,7 +290,7 @@ def _format_arg(self, opt, spec, val):
and not self.inputs.area_surfs
and not self.inputs.area_metrics
):
- raise ValueError("Exactly one of area_surfs or area_metrics" " must be specified")
+ raise ValueError("Exactly one of area_surfs or area_metrics must be specified")
if opt == "valid_roi_out" and val:
# generate a filename and add it to argstr
roi_out = self._gen_filename(self.inputs.in_file, suffix="_roi")
diff --git a/fmriprep/utils/asynctools.py b/fmriprep/utils/asynctools.py
index c61016849..1718f625c 100644
--- a/fmriprep/utils/asynctools.py
+++ b/fmriprep/utils/asynctools.py
@@ -1,5 +1,6 @@
import asyncio
-from typing import Callable, TypeVar
+from typing import TypeVar
+from collections.abc import Callable
R = TypeVar('R')
diff --git a/fmriprep/utils/bids.py b/fmriprep/utils/bids.py
index dbf52f300..f74f6994e 100644
--- a/fmriprep/utils/bids.py
+++ b/fmriprep/utils/bids.py
@@ -289,7 +289,7 @@ def check_pipeline_version(pipeline_name, cvers, data_desc):
# Very old style
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."
def extract_entities(file_list):
diff --git a/fmriprep/workflows/base.py b/fmriprep/workflows/base.py
index 3beb5e949..6fa863116 100644
--- a/fmriprep/workflows/base.py
+++ b/fmriprep/workflows/base.py
@@ -168,20 +168,18 @@ def init_single_subject_wf(subject_id: str):
from fmriprep.workflows.bold.base import init_bold_wf
workflow = Workflow(name=f'sub_{subject_id}_wf')
- workflow.__desc__ = """
+ workflow.__desc__ = f"""
Results included in this manuscript come from preprocessing
-performed using *fMRIPrep* {fmriprep_ver}
+performed using *fMRIPrep* {config.environment.version}
(@fmriprep1; @fmriprep2; RRID:SCR_016216),
-which is based on *Nipype* {nipype_ver}
+which is based on *Nipype* {config.environment.nipype_version}
(@nipype1; @nipype2; RRID:SCR_002502).
-""".format(
- fmriprep_ver=config.environment.version, nipype_ver=config.environment.nipype_version
- )
- workflow.__postdesc__ = """
+"""
+ workflow.__postdesc__ = f"""
Many internal operations of *fMRIPrep* use
-*Nilearn* {nilearn_ver} [@nilearn, RRID:SCR_001362],
+*Nilearn* {NILEARN_VERSION} [@nilearn, RRID:SCR_001362],
mostly within the functional processing workflow.
For more details of the pipeline, see [the section corresponding
to workflows in *fMRIPrep*'s documentation]\
@@ -199,9 +197,7 @@ def init_single_subject_wf(subject_id: str):
### References
-""".format(
- nilearn_ver=NILEARN_VERSION
- )
+"""
subject_data = collect_data(
config.execution.layout,
@@ -219,12 +215,10 @@ def init_single_subject_wf(subject_id: str):
anat_only = config.workflow.anat_only
# Make sure we always go through these two checks
if not anat_only and not subject_data['bold']:
- task_id = config.execution.task_id
+ task_id = config.execution.task_id or ''
raise RuntimeError(
- "No BOLD images found for participant {} and task {}. "
- "All workflows require BOLD images.".format(
- subject_id, task_id if task_id else ''
- )
+ f"No BOLD images found for participant {subject_id} and "
+ f"task {task_id}. All workflows require BOLD images."
)
bold_runs = [
@@ -613,14 +607,12 @@ def init_single_subject_wf(subject_id: str):
# Append the functional section to the existing anatomical excerpt
# That way we do not need to stream down the number of bold datasets
- func_pre_desc = """
+ func_pre_desc = f"""
Functional data preprocessing
-: For each of the {num_bold} BOLD runs found per subject (across all
-tasks and sessions), the following preprocessing was performed.
-""".format(
- num_bold=len(bold_runs)
- )
+: For each of the {len(bold_runs)} BOLD runs found per subject (across
+all tasks and sessions), the following preprocessing was performed.
+"""
for bold_series in bold_runs:
bold_file = bold_series[0]