Skip to content

Commit e5dfc9e

Browse files
committed
BF: when setting attrs to numpy arrays > 2x2 don't log the full value
It turns out that repr() on numpy arrays is slow if even slightly bigger (probably as numpy tries to find a readable way to format it). To prvent slow logging we now just log that a change occurred without logging the (slow) new value. See discussion here: https://discourse.psychopy.org/t/elementarray-xys-update-takes-16-ms/6725
1 parent b4d1ab9 commit e5dfc9e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

psychopy/tools/attributetools.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,15 @@ def logAttrib(obj, log, attrib, value=None):
154154
if value is None:
155155
value = getattr(obj, attrib)
156156

157-
# Log on next flip
158-
message = "%s: %s = %s" % (obj.name, attrib, value.__repr__())
157+
# for numpy arrays bigger than 2x2 repr is slow (up to 1ms) so just
158+
# say it was an array
159+
if isinstance(value, numpy.ndarray) \
160+
and (value.ndim > 2 or len(value) > 2):
161+
valStr = repr(type(value))
162+
else:
163+
valStr = value.__repr__()
164+
message = "%s: %s = %s" % (obj.name, attrib, valStr)
165+
159166
try:
160167
obj.win.logOnFlip(message, level=logging.EXP, obj=obj)
161168
except AttributeError:

0 commit comments

Comments
 (0)