Skip to content

Commit c7aa789

Browse files
committed
slight tidy
1 parent d232915 commit c7aa789

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

spm12/regseg.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ def move_files(fin, opth):
2525
return fdst
2626

2727

28-
def glob_re(pttrn, pth):
28+
def glob_match(pttrn, pth):
2929
"""
3030
glob with regular expressions
3131
"""
32-
m = re.compile(pttrn)
33-
return [os.path.join(pth, f) for f in os.listdir(pth) if m.match(f)]
32+
return (os.path.join(pth, f) for f in os.listdir(pth) if re.match(pttrn, f))
3433

3534

3635
def fwhm2sig(fwhm, voxsize=2.0):
@@ -45,13 +44,11 @@ def smoothim(fim, fwhm=4, fout=""):
4544
imsmo = ndi.filters.gaussian_filter(
4645
imd["im"], fwhm2sig(fwhm, voxsize=imd["voxsize"]), mode="mirror"
4746
)
48-
4947
if not fout:
5048
f = nii.file_parts(fim)
5149
fout = os.path.join(
5250
f[0], "{}_smo{}{}".format(f[1], str(fwhm).replace(".", "-"), f[2])
5351
)
54-
5552
nii.array2nii(
5653
imsmo,
5754
imd["affine"],
@@ -63,7 +60,6 @@ def smoothim(fim, fwhm=4, fout=""):
6360
),
6461
flip=imd["flip"],
6562
)
66-
6763
return {"im": imsmo, "fim": fout, "fwhm": fwhm, "affine": imd["affine"]}
6864

6965

@@ -99,7 +95,6 @@ def coreg_spm(
9995
image according to the rigid body transformation.
10096
"""
10197
out = {} # output dictionary
102-
10398
sep = sep or [4, 2]
10499
tol = tol or [
105100
0.0200,
@@ -127,7 +122,7 @@ def coreg_spm(
127122
log.debug("output path:%s", opth)
128123
create_dir(opth)
129124

130-
# > decompress ref image as necessary
125+
# decompress ref image as necessary
131126
if hasext(imref, "gz"):
132127
imrefu = nii.nii_ugzip(imref, outpath=opth)
133128
else:
@@ -137,8 +132,7 @@ def coreg_spm(
137132

138133
if fwhm_ref > 0:
139134
smodct = smoothim(imrefu, fwhm_ref)
140-
141-
# > delete the previous version (non-smoothed)
135+
# delete the previous version (non-smoothed)
142136
os.remove(imrefu)
143137
imrefu = smodct["fim"]
144138

@@ -148,7 +142,7 @@ def coreg_spm(
148142
)
149143
)
150144

151-
# > floating
145+
# floating
152146
if hasext(imflo, "gz"):
153147
imflou = nii.nii_ugzip(imflo, outpath=opth)
154148
else:
@@ -158,11 +152,11 @@ def coreg_spm(
158152

159153
if fwhm_flo > 0:
160154
smodct = smoothim(imflou, fwhm_flo)
161-
# > delete the previous version (non-smoothed)
155+
# delete the previous version (non-smoothed)
162156
if not modify_nii:
163157
os.remove(imflou)
164158
else:
165-
# > save the uncompressed and unsmoothed version
159+
# save the uncompressed and unsmoothed version
166160
imflou_ = imflou
167161

168162
imflou = smodct["fim"]
@@ -189,7 +183,7 @@ def coreg_spm(
189183
nargout=2,
190184
)
191185

192-
# > modify the affine of the floating image (as usually done in SPM)
186+
# modify the affine of the floating image (as usually done in SPM)
193187
if modify_nii:
194188
eng.amypad_coreg_modify_affine(imflou_, Mm)
195189
out["freg"] = imflou_
@@ -222,13 +216,13 @@ def coreg_spm(
222216
"affine-flo-" + nii.file_parts(imflo)[1] + fcomment + ".npy",
223217
)
224218
else:
225-
# > add '.npy' extension if not in the affine output file name
219+
# add '.npy' extension if not in the affine output file name
226220
if not fname_aff.endswith(".npy"):
227221
fname_aff += ".npy"
228222

229223
faff = os.path.join(opth, "affine-spm", fname_aff)
230224

231-
# > safe the affine transformation
225+
# save the affine transformation
232226
if save_arr:
233227
np.save(faff, M)
234228
if save_txt:
@@ -281,15 +275,15 @@ def resample_spm(
281275
log.debug("output path:%s", opth)
282276
create_dir(opth)
283277

284-
# > decompress if necessary
278+
# decompress if necessary
285279
if hasext(imref, "gz"):
286280
imrefu = nii.nii_ugzip(imref, outpath=opth)
287281
else:
288282
fnm = nii.file_parts(imref)[1] + "_copy.nii"
289283
imrefu = os.path.join(opth, fnm)
290284
shutil.copyfile(imref, imrefu)
291285

292-
# > floating
286+
# floating
293287
if hasext(imflo, "gz"):
294288
imflou = nii.nii_ugzip(imflo, outpath=opth)
295289
else:
@@ -333,7 +327,7 @@ def resample_spm(
333327
if del_out_uncmpr:
334328
os.remove(fim)
335329

336-
# > the compressed output naming
330+
# the compressed output naming
337331
if fimout:
338332
fout = os.path.join(opth, fimout)
339333
elif pickname == "ref":
@@ -407,24 +401,21 @@ def seg_spm(
407401
out["invdef"] = move_files(invdef, outpath)
408402
out["fordef"] = move_files(fordef, outpath)
409403
# go through tissue types and move them to the output folder
410-
cs = glob_re(r"c\d*", os.path.dirname(param))
411-
for c in cs:
404+
for c in glob_match(r"c\d*", os.path.dirname(param)):
412405
nm = os.path.basename(c)[:2]
413406
out[nm] = move_files(c, outpath)
414407
else:
415408
out["param"] = param
416409
out["invdef"] = invdef
417410
out["fordef"] = fordef
418411

419-
cs = glob_re(r"c\d*", os.path.dirname(param))
420-
for c in cs:
412+
for c in glob_match(r"c\d*", os.path.dirname(param)):
421413
nm = os.path.basename(c)[:2]
422414
out[nm] = c
423415
return out
424416

425417

426418
def normw_spm(f_def, files4norm, matlab_eng_name="", outpath=None):
427-
428419
"""
429420
Write normalisation output to NIfTI files using SPM12.
430421
Args:

0 commit comments

Comments
 (0)