Skip to content

Commit 6aeebd5

Browse files
authored
Merge pull request #835 from padix-key/print-options
Abbreviate printed `AtomArray` and `AtomArrayStack`
2 parents 9ca7e28 + bc103c8 commit 6aeebd5

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

src/biotite/structure/atoms.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
"repeat",
2020
"from_template",
2121
"coord",
22+
"set_print_limits",
2223
]
2324

2425
import abc
2526
import numbers
27+
import textwrap
2628
from collections.abc import Sequence
2729
import numpy as np
2830
from biotite.copyable import Copyable
@@ -42,6 +44,9 @@ class _AtomArrayBase(Copyable, metaclass=abc.ABCMeta):
4244
The amount of atoms in the structure.
4345
"""
4446

47+
_max_models_printed = 10
48+
_max_atoms_printed = 1000
49+
4550
def __init__(self, length):
4651
"""
4752
Create the annotation arrays
@@ -685,12 +690,11 @@ def __init__(self, length):
685690
def __repr__(self):
686691
"""Represent AtomArray as a string for debugging."""
687692
atoms = ""
688-
for i in range(0, self.array_length()):
689-
if len(atoms) == 0:
690-
atoms = "\n\t" + self.get_atom(i).__repr__()
691-
else:
692-
atoms = atoms + ",\n\t" + self.get_atom(i).__repr__()
693-
return f"array([{atoms}\n])"
693+
for i in range(0, min(self.array_length(), _AtomArrayBase._max_atoms_printed)):
694+
atoms = textwrap.indent(self.get_atom(i).__repr__(), "\t") + ",\n"
695+
if self.array_length() > _AtomArrayBase._max_atoms_printed:
696+
atoms = atoms + "\t...,\n"
697+
return f"array([\n{atoms}])"
694698

695699
@property
696700
def shape(self):
@@ -833,7 +837,12 @@ def __str__(self):
833837
834838
Each line contains the attributes of one atom.
835839
"""
836-
return "\n".join([str(atom) for atom in self])
840+
string = "\n".join(
841+
[str(atom) for atom in self[: _AtomArrayBase._max_atoms_printed]]
842+
)
843+
if self.array_length() > _AtomArrayBase._max_atoms_printed:
844+
string += "\n\t..."
845+
return string
837846

838847
def __copy_create__(self):
839848
return AtomArray(self.array_length())
@@ -940,12 +949,11 @@ def __init__(self, depth, length):
940949
def __repr__(self):
941950
"""Represent AtomArrayStack as a string for debugging."""
942951
arrays = ""
943-
for i in range(0, self.stack_depth()):
944-
if len(arrays) == 0:
945-
arrays = "\n\t" + self.get_array(i).__repr__()
946-
else:
947-
arrays = arrays + ",\n\t" + self.get_array(i).__repr__()
948-
return f"stack([{arrays}\n])"
952+
for i in range(0, min(self.stack_depth(), _AtomArrayBase._max_models_printed)):
953+
arrays = textwrap.indent(self.get_array(i).__repr__(), "\t") + ",\n"
954+
if self.stack_depth() > _AtomArrayBase._max_models_printed:
955+
arrays = arrays + "\t...,\n"
956+
return f"stack([\n{arrays}])"
949957

950958
def get_array(self, index):
951959
"""
@@ -1150,6 +1158,9 @@ def __str__(self):
11501158
"""
11511159
string = ""
11521160
for i, array in enumerate(self):
1161+
if i >= _AtomArrayBase._max_models_printed:
1162+
string += "..." + "\n" + "\n"
1163+
break
11531164
string += "Model " + str(i + 1) + "\n"
11541165
string += str(array) + "\n" + "\n"
11551166
return string
@@ -1560,3 +1571,21 @@ def coord(item):
15601571
return item.astype(np.float32, copy=False)
15611572
else:
15621573
return np.array(item, dtype=np.float32)
1574+
1575+
1576+
def set_print_limits(max_models=10, max_atoms=1000):
1577+
"""
1578+
Set the maximum number of models and atoms to print in the ``str()`` and ``repr()``
1579+
representations.
1580+
1581+
The remaining models/atoms are abbreviated by ellipses.
1582+
1583+
Parameters
1584+
----------
1585+
max_models : int
1586+
The maximum number of models to print.
1587+
max_atoms : int
1588+
The maximum number of atoms to print.
1589+
"""
1590+
_AtomArrayBase._max_models_printed = max_models
1591+
_AtomArrayBase._max_atoms_printed = max_atoms

tests/structure/test_atoms.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,20 @@ def test_pickle(atom, array, stack):
225225

226226
test_stack = pickle.loads(pickle.dumps(stack))
227227
assert test_stack == stack
228+
229+
230+
def test_set_print_limits(array, stack):
231+
"""
232+
Check the output of :func:`set_print_limits()`
233+
by setting the maximum number of models and atoms to print very low.
234+
"""
235+
atom_string = str(array[0])
236+
atom_repr = repr(array[0])
237+
struc.set_print_limits(max_models=1, max_atoms=1)
238+
assert str(array) == f"{atom_string}\n\t..."
239+
assert str(stack) == f"Model 1\n{atom_string}\n\t...\n\n...\n\n"
240+
assert repr(array) == f"array([\n\t{atom_repr},\n\t...,\n])"
241+
assert (
242+
repr(stack)
243+
== f"stack([\n\tarray([\n\t\t{atom_repr},\n\t\t...,\n\t]),\n\t...,\n])"
244+
)

0 commit comments

Comments
 (0)