Skip to content

Commit ec55fda

Browse files
committed
fix: Issues identified by ruff
1 parent df011fa commit ec55fda

File tree

11 files changed

+34
-45
lines changed

11 files changed

+34
-45
lines changed

.circleci/version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
import sdcflows
44

5-
print(sdcflows.__version__, end='', file=open('/tmp/.docker-version.txt', 'w'))
5+
with open('/tmp/.docker-version.txt', 'w') as f:
6+
f.write(sdcflows.__version__)

docs/tools/apigen.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ def _parse_module(self, uri):
185185
# nothing that we could handle here.
186186
return ([], [])
187187

188-
f = open(filename)
189-
functions, classes = self._parse_lines(f)
190-
f.close()
188+
with open(filename) as f:
189+
functions, classes = self._parse_lines(f)
191190
return functions, classes
192191

193192
def _parse_module_with_import(self, uri):
@@ -346,15 +345,7 @@ def _survives_exclude(self, matchstr, match_type):
346345
L = len(self.package_name)
347346
if matchstr[:L] == self.package_name:
348347
matchstr = matchstr[L:]
349-
for pat in patterns:
350-
try:
351-
pat.search
352-
except AttributeError:
353-
pat = re.compile(pat)
354-
if pat.search(matchstr):
355-
return False
356-
357-
return True
348+
return not any(re.search(pat, matchstr) for pat in patterns)
358349

359350
def discover_modules(self):
360351
r"""Return module sequence discovered from ``self.package_name``
@@ -434,11 +425,8 @@ def write_modules_api(self, modules, outdir):
434425
document_body.append(body)
435426

436427
out_module = ulm + self.rst_extension
437-
outfile = os.path.join(outdir, out_module)
438-
fileobj = open(outfile, 'w')
439-
440-
fileobj.writelines(document_head + document_body)
441-
fileobj.close()
428+
with open(os.path.join(outdir, out_module), 'w') as fileobj:
429+
fileobj.writelines(document_head + document_body)
442430
written_modules.append(out_module)
443431

444432
self.written_modules = written_modules
@@ -493,14 +481,13 @@ def write_index(self, outdir, froot='gen', relative_to=None):
493481
relpath = (outdir + os.path.sep).replace(relative_to + os.path.sep, '')
494482
else:
495483
relpath = outdir
496-
idx = open(path, 'w')
497-
w = idx.write
498-
w('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n\n')
499-
500-
title = 'API Reference'
501-
w(title + '\n')
502-
w('=' * len(title) + '\n\n')
503-
w('.. toctree::\n\n')
504-
for f in self.written_modules:
505-
w(f' {os.path.join(relpath, f)}\n')
506-
idx.close()
484+
with open(path, 'w') as idx:
485+
w = idx.write
486+
w('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n\n')
487+
488+
title = 'API Reference'
489+
w(title + '\n')
490+
w('=' * len(title) + '\n\n')
491+
w('.. toctree::\n\n')
492+
for f in self.written_modules:
493+
w(f' {os.path.join(relpath, f)}\n')

docs/tools/buildmodref.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def writeapi(package, outdir, source_version, other_defines=True):
4747
]
4848
docwriter.write_api_docs(outdir)
4949
docwriter.write_index(outdir, 'index', relative_to=outdir)
50-
print('%d files written' % len(docwriter.written_modules))
50+
print(f'{len(docwriter.written_modules)} files written')
5151

5252

5353
if __name__ == '__main__':

sdcflows/fieldmaps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def __attrs_post_init__(self):
376376
raise ValueError(
377377
'A fieldmap or phase-difference estimation type was found, '
378378
f'but an anatomical reference ({magnitude} file) is missing.'
379-
)
379+
) from None
380380

381381
# Check presence and try to find (if necessary) the second magnitude file
382382
if self.method == EstimatorType.PHASEDIFF and 'magnitude2' not in suffix_set:
@@ -390,7 +390,7 @@ def __attrs_post_init__(self):
390390
raise ValueError(
391391
'A phase-difference estimation (phase1/2) type was found, '
392392
'but an anatomical reference (magnitude2 file) is missing.'
393-
)
393+
) from None
394394

395395
# Fieldmap option 2: PEPOLAR (and fieldmap-less or ANAT)
396396
# IMPORTANT NOTE: fieldmap-less approaches can be considered PEPOLAR with RO = 0.0s

sdcflows/tests/test_fieldmaps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_FieldmapEstimation(dsA_dir, inputfiles, method, nsources):
125125

126126
# Exercise workflow creation
127127
wf = fe.get_workflow()
128-
wf == fe.get_workflow()
128+
assert wf == fe.get_workflow()
129129

130130

131131
@pytest.mark.parametrize(

sdcflows/utils/bimap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ def __setitem__(self, key, value):
135135
try:
136136
hash(value)
137137
except TypeError as exc:
138-
raise TypeError(f"value '{value}' of {exc}")
138+
raise TypeError(f"value '{value}' of {exc}") from None
139139
try:
140140
hash(key)
141141
except TypeError as exc:
142-
raise TypeError(f"key '{key}' of {exc}")
142+
raise TypeError(f"key '{key}' of {exc}") from None
143143

144144
if self.__contains__(key):
145145
raise KeyError(f"'{key}' is already {'a value' * (key in self._inverse)} in mapping")

sdcflows/utils/phasemanip.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ def delta_te(in_values):
135135
te2 = float(in_values.get('EchoTimeDifference'))
136136
return abs(te2)
137137
except TypeError:
138-
raise ValueError('Phase/phase-difference fieldmaps: no echo-times information.')
138+
raise ValueError(
139+
'Phase/phase-difference fieldmaps: no echo-times information.'
140+
) from None
139141
except ValueError:
140-
raise ValueError(f'Could not interpret metadata <EchoTimeDifference={te2}>.')
142+
raise ValueError(f'Could not interpret metadata <EchoTimeDifference={te2}>.') from None
141143
try:
142144
te2 = float(te2 or 'unknown')
143145
te1 = float(te1 or 'unknown')
144146
except ValueError:
145-
raise ValueError(f'Could not interpret metadata <EchoTime(1,2)={(te1, te2)}>.')
147+
raise ValueError(f'Could not interpret metadata <EchoTime(1,2)={(te1, te2)}>.') from None
146148

147149
return abs(te2 - te1)

sdcflows/utils/wrangler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def find_anatomical_estimators(
606606
meta.update({'TotalReadoutTime': get_trt(meta, candidate.path)})
607607
epi_targets.append(fm.FieldmapFile(candidate, metadata=meta))
608608

609-
def sort_key(fmap):
609+
def sort_key(fmap, suffixes=suffixes):
610610
# Return sbref before DWI/BOLD and shortest echo first
611611
return suffixes.index(fmap.suffix), fmap.metadata.get('EchoTime', 1)
612612

sdcflows/workflows/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ def init_fmap_preproc_wf(
115115
in_file=f.path,
116116
use_estimate=use_metadata_estimates,
117117
)
118-
except ValueError:
118+
except ValueError as e:
119119
msg = f'Missing readout timing information for <{f.path}>.'
120-
raise RuntimeError(msg)
120+
raise RuntimeError(msg) from e
121121

122122
est_wf = estimator.get_workflow(
123123
use_metadata_estimates=use_metadata_estimates,

sdcflows/workflows/fit/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def init_sdcflows_wf():
4949
logger=config.loggers.cli,
5050
)
5151

52-
for subject, sub_estimators in estimators_record.items():
52+
for sub_estimators in estimators_record.values():
5353
for estim in sub_estimators:
5454
estim_wf = estim.get_workflow(
5555
omp_nthreads=config.nipype.omp_nthreads,

0 commit comments

Comments
 (0)