Skip to content

Commit 713ef7b

Browse files
committed
Improved repr of ArraySequence
1 parent 1d01c0a commit 713ef7b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

nibabel/streamlines/array_sequence.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,17 @@ def __len__(self):
263263
return len(self._offsets)
264264

265265
def __repr__(self):
266-
return repr(list(self))
266+
if len(self) > np.get_printoptions()['threshold']:
267+
# Show only the first and last edgeitems.
268+
edgeitems = np.get_printoptions()['edgeitems']
269+
data = str(list(self[:edgeitems]))[:-1]
270+
data += ", ..., "
271+
data += str(list(self[-edgeitems:]))[1:]
272+
else:
273+
data = str(list(self))
274+
275+
return "{name}({data})".format(name=self.__class__.__name__,
276+
data=data)
267277

268278
def save(self, filename):
269279
""" Saves this :class:`ArraySequence` object to a .npz file. """

nibabel/streamlines/tests/test_array_sequence.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ def test_arraysequence_repr(self):
249249
# Test that calling repr on a ArraySequence object is not falling.
250250
repr(SEQ_DATA['seq'])
251251

252+
# Test calling repr when the number of arrays is bigger dans Numpy's
253+
# print option threshold.
254+
nb_arrays = 50
255+
seq = ArraySequence(generate_data(nb_arrays, common_shape=(1,),
256+
rng=SEQ_DATA['rng']))
257+
258+
bkp_threshold = np.get_printoptions()['threshold']
259+
np.set_printoptions(threshold=nb_arrays*2)
260+
txt1 = repr(seq)
261+
np.set_printoptions(threshold=nb_arrays//2)
262+
txt2 = repr(seq)
263+
assert_true(len(txt2) < len(txt1))
264+
np.set_printoptions(threshold=bkp_threshold)
265+
252266
def test_save_and_load_arraysequence(self):
253267
# Test saving and loading an empty ArraySequence.
254268
with tempfile.TemporaryFile(mode="w+b", suffix=".npz") as f:

0 commit comments

Comments
 (0)