Skip to content

Commit 9f17357

Browse files
authored
Merge pull request #3648 from DimitriPapadopoulos/FURB
STY: Apply ruff/refurb rules
2 parents d52a62d + cfcbc6d commit 9f17357

File tree

14 files changed

+65
-82
lines changed

14 files changed

+65
-82
lines changed

nipype/algorithms/misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def remove_identical_paths(in_files):
531531
commonprefix = op.commonprefix(in_files)
532532
lastslash = commonprefix.rfind("/")
533533
commonpath = commonprefix[0 : (lastslash + 1)]
534-
for fileidx, in_file in enumerate(in_files):
534+
for in_file in in_files:
535535
path, name, ext = split_filename(in_file)
536536
in_file = op.join(path, name)
537537
name = in_file.replace(commonpath, "")

nipype/interfaces/ants/registration.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1127,14 +1127,10 @@ def _format_metric_argument(**kwargs):
11271127
return retval
11281128

11291129
def _format_transform(self, index):
1130-
retval = []
1131-
retval.append("%s[ " % self.inputs.transforms[index])
11321130
parameters = ", ".join(
11331131
[str(element) for element in self.inputs.transform_parameters[index]]
11341132
)
1135-
retval.append("%s" % parameters)
1136-
retval.append(" ]")
1137-
return "".join(retval)
1133+
return f"{self.inputs.transforms[index]}[ {parameters} ]"
11381134

11391135
def _format_registration(self):
11401136
retval = []

nipype/interfaces/cmtk/nx.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def average_networks(in_files, ntwk_res_file, group_id):
124124
ntwk = remove_all_edges(ntwk_res_file)
125125
counting_ntwk = ntwk.copy()
126126
# Sums all the relevant variables
127-
for index, subject in enumerate(in_files):
127+
for subject in in_files:
128128
tmp = _read_pickle(subject)
129129
iflogger.info("File %s has %i edges", subject, tmp.number_of_edges())
130130
edges = list(tmp.edges())

nipype/interfaces/cmtk/tests/test_nbs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def creating_graphs(tmpdir):
1717
graphlist = []
1818
graphnames = ["name" + str(i) for i in range(6)]
19-
for idx, name in enumerate(graphnames):
19+
for idx in range(len(graphnames)):
2020
graph = np.random.rand(10, 10)
2121
G = nx.from_numpy_array(graph)
2222
out_file = tmpdir.strpath + graphnames[idx] + ".pck"

nipype/interfaces/dipy/reconstruction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _run_interface(self, runtime):
124124
else:
125125
nodiff = np.where(~gtab.b0s_mask)
126126
nodiffidx = nodiff[0].tolist()
127-
n = 20 if len(nodiffidx) >= 20 else len(nodiffidx)
127+
n = min(20, len(nodiffidx))
128128
idxs = np.random.choice(nodiffidx, size=n, replace=False)
129129
noise_data = dsample.take(idxs, axis=-1)[noise_msk == 1, ...]
130130

nipype/interfaces/elastix/registration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _list_outputs(self):
8282
config = {}
8383

8484
with open(params) as f:
85-
for line in f.readlines():
85+
for line in f:
8686
line = line.strip()
8787
if not line.startswith("//") and line:
8888
m = regex.search(line)

nipype/interfaces/fsl/model.py

+29-33
Original file line numberDiff line numberDiff line change
@@ -822,34 +822,32 @@ class FILMGLS(FSLCommand):
822822
def _get_pe_files(self, cwd):
823823
files = None
824824
if isdefined(self.inputs.design_file):
825-
fp = open(self.inputs.design_file)
826-
for line in fp.readlines():
827-
if line.startswith("/NumWaves"):
828-
numpes = int(line.split()[-1])
829-
files = []
830-
for i in range(numpes):
831-
files.append(self._gen_fname("pe%d.nii" % (i + 1), cwd=cwd))
832-
break
833-
fp.close()
825+
with open(self.inputs.design_file) as fp:
826+
for line in fp:
827+
if line.startswith("/NumWaves"):
828+
numpes = int(line.split()[-1])
829+
files = [
830+
self._gen_fname(f"pe{i + 1}.nii", cwd=cwd)
831+
for i in range(numpes)
832+
]
833+
break
834834
return files
835835

836836
def _get_numcons(self):
837837
numtcons = 0
838838
numfcons = 0
839839
if isdefined(self.inputs.tcon_file):
840-
fp = open(self.inputs.tcon_file)
841-
for line in fp.readlines():
842-
if line.startswith("/NumContrasts"):
843-
numtcons = int(line.split()[-1])
844-
break
845-
fp.close()
840+
with open(self.inputs.tcon_file) as fp:
841+
for line in fp:
842+
if line.startswith("/NumContrasts"):
843+
numtcons = int(line.split()[-1])
844+
break
846845
if isdefined(self.inputs.fcon_file):
847-
fp = open(self.inputs.fcon_file)
848-
for line in fp.readlines():
849-
if line.startswith("/NumContrasts"):
850-
numfcons = int(line.split()[-1])
851-
break
852-
fp.close()
846+
with open(self.inputs.fcon_file) as fp:
847+
for line in fp:
848+
if line.startswith("/NumContrasts"):
849+
numfcons = int(line.split()[-1])
850+
break
853851
return numtcons, numfcons
854852

855853
def _list_outputs(self):
@@ -1297,19 +1295,17 @@ def _get_numcons(self):
12971295
numtcons = 0
12981296
numfcons = 0
12991297
if isdefined(self.inputs.tcon_file):
1300-
fp = open(self.inputs.tcon_file)
1301-
for line in fp.readlines():
1302-
if line.startswith("/NumContrasts"):
1303-
numtcons = int(line.split()[-1])
1304-
break
1305-
fp.close()
1298+
with open(self.inputs.tcon_file) as fp:
1299+
for line in fp:
1300+
if line.startswith("/NumContrasts"):
1301+
numtcons = int(line.split()[-1])
1302+
break
13061303
if isdefined(self.inputs.fcon_file):
1307-
fp = open(self.inputs.fcon_file)
1308-
for line in fp.readlines():
1309-
if line.startswith("/NumContrasts"):
1310-
numfcons = int(line.split()[-1])
1311-
break
1312-
fp.close()
1304+
with open(self.inputs.fcon_file) as fp:
1305+
for line in fp:
1306+
if line.startswith("/NumContrasts"):
1307+
numfcons = int(line.split()[-1])
1308+
break
13131309
return numtcons, numfcons
13141310

13151311
def _list_outputs(self):

nipype/interfaces/io.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ def _list_outputs(self):
975975
if self.inputs.sort_filelist:
976976
filelist = human_order_sorted(filelist)
977977
outputs[key] = simplify_list(filelist)
978-
for argnum, arglist in enumerate(args):
978+
for arglist in args:
979979
maxlen = 1
980980
for arg in arglist:
981981
if isinstance(arg, (str, bytes)) and hasattr(self.inputs, arg):
@@ -1250,7 +1250,7 @@ def _list_outputs(self):
12501250
if self.inputs.sort_filelist:
12511251
filelist = human_order_sorted(filelist)
12521252
outputs[key] = simplify_list(filelist)
1253-
for argnum, arglist in enumerate(args):
1253+
for arglist in args:
12541254
maxlen = 1
12551255
for arg in arglist:
12561256
if isinstance(arg, (str, bytes)) and hasattr(self.inputs, arg):
@@ -1995,7 +1995,7 @@ def _list_outputs(self):
19951995
if file_object.exists()
19961996
]
19971997
)
1998-
for argnum, arglist in enumerate(args):
1998+
for arglist in args:
19991999
maxlen = 1
20002000
for arg in arglist:
20012001
if isinstance(arg, (str, bytes)) and hasattr(self.inputs, arg):
@@ -2609,7 +2609,7 @@ def _list_outputs(self):
26092609
if not args:
26102610
outputs[key] = self._get_files_over_ssh(template)
26112611

2612-
for argnum, arglist in enumerate(args):
2612+
for arglist in args:
26132613
maxlen = 1
26142614
for arg in arglist:
26152615
if isinstance(arg, (str, bytes)) and hasattr(self.inputs, arg):

nipype/interfaces/nipy/preprocess.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _run_interface(self, runtime):
203203
# nipy does not encode euler angles. return in original form of
204204
# translation followed by rotation vector see:
205205
# http://en.wikipedia.org/wiki/Rodrigues'_rotation_formula
206-
for i, mo in enumerate(motion):
206+
for mo in motion:
207207
params = [
208208
"%.10f" % item for item in np.hstack((mo.translation, mo.rotation))
209209
]

nipype/pipeline/engine/workflows.py

+21-24
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,11 @@ def write_graph(
483483
def write_hierarchical_dotfile(
484484
self, dotfilename=None, colored=False, simple_form=True
485485
):
486-
dotlist = ["digraph %s{" % self.name]
487-
dotlist.append(
488-
self._get_dot(prefix=" ", colored=colored, simple_form=simple_form)
489-
)
490-
dotlist.append("}")
491-
dotstr = "\n".join(dotlist)
486+
dotlist = self._get_dot(prefix=" ", colored=colored, simple_form=simple_form)
487+
dotstr = f"digraph {self.name}{{\n{dotlist}\n}}"
492488
if dotfilename:
493-
fp = open(dotfilename, "w")
494-
fp.writelines(dotstr)
495-
fp.close()
489+
with open(dotfilename, "w") as fp:
490+
fp.writelines(dotstr)
496491
else:
497492
logger.info(dotstr)
498493

@@ -532,7 +527,7 @@ def export(
532527
lines.append(wfdef)
533528
if include_config:
534529
lines.append(f"{self.name}.config = {self.config}")
535-
for idx, node in enumerate(nodes):
530+
for node in nodes:
536531
nodename = node.fullname.replace(".", "_")
537532
# write nodes
538533
nodelines = format_node(
@@ -1068,23 +1063,25 @@ def _get_dot(
10681063
nodename = fullname.replace(".", "_")
10691064
dotlist.append("subgraph cluster_%s {" % nodename)
10701065
if colored:
1071-
dotlist.append(
1072-
prefix + prefix + 'edge [color="%s"];' % (colorset[level + 1])
1073-
)
1074-
dotlist.append(prefix + prefix + "style=filled;")
1075-
dotlist.append(
1076-
prefix + prefix + 'fillcolor="%s";' % (colorset[level + 2])
1066+
dotlist.extend(
1067+
(
1068+
f'{prefix * 2}edge [color="{colorset[level + 1]}"];',
1069+
f"{prefix * 2}style=filled;",
1070+
f'{prefix * 2}fillcolor="{colorset[level + 2]}";',
1071+
)
10771072
)
1078-
dotlist.append(
1079-
node._get_dot(
1080-
prefix=prefix + prefix,
1081-
hierarchy=hierarchy + [self.name],
1082-
colored=colored,
1083-
simple_form=simple_form,
1084-
level=level + 3,
1073+
dotlist.extend(
1074+
(
1075+
node._get_dot(
1076+
prefix=prefix + prefix,
1077+
hierarchy=hierarchy + [self.name],
1078+
colored=colored,
1079+
simple_form=simple_form,
1080+
level=level + 3,
1081+
),
1082+
"}",
10851083
)
10861084
)
1087-
dotlist.append("}")
10881085
else:
10891086
for subnode in self._graph.successors(node):
10901087
if node._hierarchy != subnode._hierarchy:

nipype/pipeline/plugins/somaflow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, plugin_args=None):
2424
def _submit_graph(self, pyfiles, dependencies, nodes):
2525
jobs = []
2626
soma_deps = []
27-
for idx, fname in enumerate(pyfiles):
27+
for fname in pyfiles:
2828
name = os.path.splitext(os.path.split(fname)[1])[0]
2929
jobs.append(Job(command=[sys.executable, fname], name=name))
3030
for key, values in list(dependencies.items()):

nipype/sphinxext/apidoc/docstring.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class InterfaceDocstring(NipypeDocstring):
6262
_name_rgx = re.compile(
6363
r"^\s*(:(?P<role>\w+):`(?P<name>[a-zA-Z0-9_.-]+)`|"
6464
r" (?P<name2>[a-zA-Z0-9_.-]+))\s*",
65-
re.X,
65+
re.VERBOSE,
6666
)
6767

6868
def __init__(

nipype/sphinxext/plot_workflow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def contains_doctest(text):
484484
return False
485485
except SyntaxError:
486486
pass
487-
r = re.compile(r"^\s*>>>", re.M)
487+
r = re.compile(r"^\s*>>>", re.MULTILINE)
488488
m = r.search(text)
489489
return bool(m)
490490

nipype/utils/docparse.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,7 @@ def insert_doc(doc, new_items):
166166
# Add rest of documents
167167
tmpdoc.extend(doclist[2:])
168168
# Insert newlines
169-
newdoc = []
170-
for line in tmpdoc:
171-
newdoc.append(line)
172-
newdoc.append("\n")
173-
# We add one too many newlines, remove it.
174-
newdoc.pop(-1)
175-
return "".join(newdoc)
169+
return "\n".join(tmpdoc)
176170

177171

178172
def build_doc(doc, opts):

0 commit comments

Comments
 (0)