Skip to content

Commit 4c9a129

Browse files
MNT: Apply refurb suggestion
[FURB113]: Use `x.extend(...)` instead of repeatedly calling `x.append()`
1 parent 0690ea3 commit 4c9a129

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

nibabel/cifti2/tests/test_cifti2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,7 @@ def test_matrixindicesmap():
326326
parcel = ci.Cifti2Parcel()
327327

328328
assert mim.volume is None
329-
mim.append(volume)
330-
mim.append(parcel)
329+
mim.extend((volume, parcel))
331330

332331

333332
assert mim.volume == volume

nibabel/cifti2/tests/test_new_cifti2.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ def test_dtseries():
232232
series_map = create_series_map((0, ))
233233
geometry_map = create_geometry_map((1, ))
234234
matrix = ci.Cifti2Matrix()
235-
matrix.append(series_map)
236-
matrix.append(geometry_map)
235+
matrix.extend((series_map, geometry_map))
237236
hdr = ci.Cifti2Header(matrix)
238237
data = np.random.randn(13, 10)
239238
img = ci.Cifti2Image(data, hdr)
@@ -254,8 +253,7 @@ def test_dscalar():
254253
scalar_map = create_scalar_map((0, ))
255254
geometry_map = create_geometry_map((1, ))
256255
matrix = ci.Cifti2Matrix()
257-
matrix.append(scalar_map)
258-
matrix.append(geometry_map)
256+
matrix.extend((scalar_map, geometry_map))
259257
hdr = ci.Cifti2Header(matrix)
260258
data = np.random.randn(2, 10)
261259
img = ci.Cifti2Image(data, hdr)
@@ -276,8 +274,7 @@ def test_dlabel():
276274
label_map = create_label_map((0, ))
277275
geometry_map = create_geometry_map((1, ))
278276
matrix = ci.Cifti2Matrix()
279-
matrix.append(label_map)
280-
matrix.append(geometry_map)
277+
matrix.extend((label_map, geometry_map))
281278
hdr = ci.Cifti2Header(matrix)
282279
data = np.random.randn(2, 10)
283280
img = ci.Cifti2Image(data, hdr)
@@ -318,8 +315,7 @@ def test_ptseries():
318315
series_map = create_series_map((0, ))
319316
parcel_map = create_parcel_map((1, ))
320317
matrix = ci.Cifti2Matrix()
321-
matrix.append(series_map)
322-
matrix.append(parcel_map)
318+
matrix.extend((series_map, parcel_map))
323319
hdr = ci.Cifti2Header(matrix)
324320
data = np.random.randn(13, 4)
325321
img = ci.Cifti2Image(data, hdr)
@@ -340,8 +336,7 @@ def test_pscalar():
340336
scalar_map = create_scalar_map((0, ))
341337
parcel_map = create_parcel_map((1, ))
342338
matrix = ci.Cifti2Matrix()
343-
matrix.append(scalar_map)
344-
matrix.append(parcel_map)
339+
matrix.extend((scalar_map, parcel_map))
345340
hdr = ci.Cifti2Header(matrix)
346341
data = np.random.randn(2, 4)
347342
img = ci.Cifti2Image(data, hdr)
@@ -362,8 +357,7 @@ def test_pdconn():
362357
geometry_map = create_geometry_map((0, ))
363358
parcel_map = create_parcel_map((1, ))
364359
matrix = ci.Cifti2Matrix()
365-
matrix.append(geometry_map)
366-
matrix.append(parcel_map)
360+
matrix.extend((geometry_map, parcel_map))
367361
hdr = ci.Cifti2Header(matrix)
368362
data = np.random.randn(10, 4)
369363
img = ci.Cifti2Image(data, hdr)
@@ -384,8 +378,7 @@ def test_dpconn():
384378
parcel_map = create_parcel_map((0, ))
385379
geometry_map = create_geometry_map((1, ))
386380
matrix = ci.Cifti2Matrix()
387-
matrix.append(parcel_map)
388-
matrix.append(geometry_map)
381+
matrix.extend((parcel_map, geometry_map))
389382
hdr = ci.Cifti2Header(matrix)
390383
data = np.random.randn(4, 10)
391384
img = ci.Cifti2Image(data, hdr)
@@ -406,8 +399,7 @@ def test_plabel():
406399
label_map = create_label_map((0, ))
407400
parcel_map = create_parcel_map((1, ))
408401
matrix = ci.Cifti2Matrix()
409-
matrix.append(label_map)
410-
matrix.append(parcel_map)
402+
matrix.extend((label_map, parcel_map))
411403
hdr = ci.Cifti2Header(matrix)
412404
data = np.random.randn(2, 4)
413405
img = ci.Cifti2Image(data, hdr)
@@ -448,8 +440,7 @@ def test_pconnseries():
448440
series_map = create_series_map((2, ))
449441

450442
matrix = ci.Cifti2Matrix()
451-
matrix.append(parcel_map)
452-
matrix.append(series_map)
443+
matrix.extend((parcel_map, series_map))
453444
hdr = ci.Cifti2Header(matrix)
454445
data = np.random.randn(4, 4, 13)
455446
img = ci.Cifti2Image(data, hdr)
@@ -473,8 +464,7 @@ def test_pconnscalar():
473464
scalar_map = create_scalar_map((2, ))
474465

475466
matrix = ci.Cifti2Matrix()
476-
matrix.append(parcel_map)
477-
matrix.append(scalar_map)
467+
matrix.extend((parcel_map, scalar_map))
478468
hdr = ci.Cifti2Header(matrix)
479469
data = np.random.randn(4, 4, 2)
480470
img = ci.Cifti2Image(data, hdr)
@@ -499,8 +489,7 @@ def test_wrong_shape():
499489
brain_model_map = create_geometry_map((1, ))
500490

501491
matrix = ci.Cifti2Matrix()
502-
matrix.append(scalar_map)
503-
matrix.append(brain_model_map)
492+
matrix.extend((scalar_map, brain_model_map))
504493
hdr = ci.Cifti2Header(matrix)
505494

506495
# correct shape is (2, 10)

nibabel/cmdline/dicomfs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ def readdir(self, path, fh):
117117
return -errno.ENOENT
118118
logger.debug(f'matched {matched_path}')
119119
fnames = [k.encode('ascii', 'replace') for k in matched_path.keys()]
120-
fnames.append('.')
121-
fnames.append('..')
120+
fnames.extend(('.', '..'))
122121
return [fuse.Direntry(f) for f in fnames]
123122

124123
def getattr(self, path):

nibabel/streamlines/tck.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,13 @@ def _write_header(fileobj, header):
249249
Field.VOXEL_TO_RASMM, # Streamlines are always in RAS+ mm.
250250
"count", "datatype", "file"] # Fields being replaced.
251251

252-
lines = []
253-
lines.append(f"count: {header[Field.NB_STREAMLINES]:010}")
254-
lines.append("datatype: Float32LE") # Always Float32LE.
255-
lines.extend([f"{k}: {v}"
256-
for k, v in header.items()
257-
if k not in exclude and not k.startswith("_")])
252+
lines = [
253+
f"count: {header[Field.NB_STREAMLINES]:010}",
254+
"datatype: Float32LE", # Always Float32LE.
255+
]
256+
lines.extend(f"{k}: {v}"
257+
for k, v in header.items()
258+
if k not in exclude and not k.startswith("_"))
258259
out = "\n".join(lines)
259260

260261
# Check the header is well formatted.

nibabel/tests/data/gen_standard.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ def _gen_straight_streamline(start, end, steps=3):
3535
return np.array(coords).T
3636

3737
# Generate a 3D 'X' template fitting inside the voxel centered at (0,0,0).
38-
X = []
39-
X.append(_gen_straight_streamline((-0.5, -0.5, -0.5), (0.5, 0.5, 0.5)))
40-
X.append(_gen_straight_streamline((-0.5, 0.5, -0.5), (0.5, -0.5, 0.5)))
41-
X.append(_gen_straight_streamline((-0.5, 0.5, 0.5), (0.5, -0.5, -0.5)))
42-
X.append(_gen_straight_streamline((-0.5, -0.5, 0.5), (0.5, 0.5, -0.5)))
38+
X = [
39+
_gen_straight_streamline((-0.5, -0.5, -0.5), (0.5, 0.5, 0.5)),
40+
_gen_straight_streamline((-0.5, 0.5, -0.5), (0.5, -0.5, 0.5)),
41+
_gen_straight_streamline((-0.5, 0.5, 0.5), (0.5, -0.5, -0.5)),
42+
_gen_straight_streamline((-0.5, -0.5, 0.5), (0.5, 0.5, -0.5)),
43+
]
4344

4445
# Get the coordinates of voxels 'on' in the mask.
4546
coords = np.array(zip(*np.where(mask)))
4647

47-
streamlines = []
48-
for c in coords:
49-
for line in X:
50-
streamlines.append((line + c) * voxel_size)
48+
streamlines = [(line + c) * voxel_size for line in X for c in coords]
5149

5250
return streamlines
5351

0 commit comments

Comments
 (0)