python-quantities/python-quantities — Issue #28: Printing numpy array with Quantities fails
Author: ssalonen
Created: 2011-11-03 13:48:01+00:00
State: open

It seems that storing Quantities into numpy array has some problems with printing.

See the following example

In [98]: import numpy as np

In [99]: import quantities as pq

In [100]: tmp=np.empty(2, dtype=[('a', pq.Quantity), ('b', pq.Quantity)])

In [101]: tmp
Out[101]:
array([(None, None), (None, None)],
dtype=[('a', '|O4'), ('b', '|O4')])

In [102]: tmp[0][0] = 3.5*pq.meter

In [103]: tmp
Out[103]: ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (25, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (2, 0))

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

c:\code\ in ()

c:\code\ENV\lib\site-packages\IPython\Prompts.pyc in __call__(self, arg)
550
551 # and now call a possibly user-defined print mechanism

--> 552 manipulated_val = self.display(arg)
553
554 # user display hooks can change the variable to be stored in

c:\code\ENV\lib\site-packages\IPython\Prompts.pyc in _display(self, arg)
576 return IPython.generics.result_display(arg)
577 except TryNext:
--> 578 return self.shell.hooks.result_display(arg)
579
580 # Assign the default display method:

c:\code\ENV\lib\site-packages\IPython\hooks.pyc in __call__(self, *args, **kw)
139 #print "prio",prio,"cmd",cmd #dbg

140 try:
--> 141 ret = cmd(*args, **kw)
142 return ret
143 except ipapi.TryNext, exc:

c:\code\ENV\lib\site-packages\IPython\hooks.pyc in result_display(self, arg)
169
170 if self.rc.pprint:
--> 171 out = pformat(arg)
172 if '\n' in out:
173 # So that multi-line strings line up with the left column of

C:\Python27\Lib\pprint.pyc in pformat(self, object)
117 def pformat(self, object):
118 sio = _StringIO()
--> 119 self._format(object, sio, 0, 0, {}, 0)
120 return sio.getvalue()
121

C:\Python27\Lib\pprint.pyc in _format(self, object, stream, indent, allowance, context, level)
135 self._readable = False
136 return
--> 137 rep = self._repr(object, context, level - 1)
138 typ = _type(object)
139 sepLines = _len(rep) > (self._width - 1 - indent - allowance)

C:\Python27\Lib\pprint.pyc in _repr(self, object, context, level)
228 def _repr(self, object, context, level):
229 repr, readable, recursive = self.format(object, context.copy(),
--> 230 self._depth, level)
231 if not readable:
232 self._readable = False

C:\Python27\Lib\pprint.pyc in format(self, object, context, maxlevels, level)
240 and whether the object represents a recursive construct.
241 """
--> 242 return _safe_repr(object, context, maxlevels, level)
243
244

C:\Python27\Lib\pprint.pyc in _safe_repr(object, context, maxlevels, level)
325 return format % _commajoin(components), readable, recursive
326
--> 327 rep = repr(object)
328 return rep, (rep and not rep.startswith('<')), False
329

C:\Python27\lib\site-packages\numpy\core\numeric.pyc in array_repr(arr, max_line_width, precision, suppress_small)
1321 if arr.size > 0 or arr.shape==(0,):
1322 lst = array2string(arr, max_line_width, precision, suppress_small,
-> 1323 ', ', "array(")
1324 else: # show zero-length shape unless it is (0,)
1325 lst = "[], shape=%s" % (repr(arr.shape),)

C:\Python27\lib\site-packages\numpy\core\arrayprint.pyc in array2string(a, max_line_width, precision, suppress_small, separator, prefix, style)
307 else:
308 lst = _array2string(a, max_line_width, precision, suppress_small,
--> 309 separator, prefix)
310 return lst
311

C:\Python27\lib\site-packages\numpy\core\arrayprint.pyc in _array2string(a, max_line_width, precision, suppress_small, separator, prefix)
228 lst = _formatArray(a, format_function, len(a.shape), max_line_width,
229 next_line_prefix, separator,
--> 230 _summaryEdgeItems, summary_insert)[:-1]
231
232 return lst

C:\Python27\lib\site-packages\numpy\core\arrayprint.pyc in _formatArray(a, format_function, rank, max_line_len, next_line_prefix, separator, edge_items, summary_insert)
350
351 for i in xrange(trailing_items, 1, -1):
--> 352 word = format_function(a[-i]) + separator
353 s, line = _extendLine(s, line, word, max_line_len, next_line_prefix)
354

C:\Python27\lib\site-packages\numpy\core\numeric.pyc in array_str(a, max_line_width, precision, suppress_small)
1377
1378 """
-> 1379 return array2string(a, max_line_width, precision, suppress_small, ' ', "", str)
1380
1381 def set_string_function(f, repr=True):

C:\Python27\lib\site-packages\numpy\core\arrayprint.pyc in array2string(a, max_line_width, precision, suppress_small, separator, prefix, style)
300 except AttributeError:
301 if isinstance(x, tuple):
--> 302 x = _convert_arrays(x)
303 lst = style(x)
304 elif reduce(product, a.shape) == 0:

C:\Python27\lib\site-packages\numpy\core\arrayprint.pyc in _convert_arrays(obj)
237 for k in obj:
238 if isinstance(k, _nc.ndarray):
--> 239 k = k.tolist()
240 elif isinstance(k, tuple):
241 k = _convert_arrays(k)

c:\code\ENV\lib\site-packages\quantities\quantity.pyc in tolist(self)
405 work_list = self.magnitude.tolist()
406 #now go through and replace all numbers with the appropriate Quantity

--> 407 self._tolist(work_list)
408 return work_list
409

c:\code\ENV\lib\site-packages\quantities\quantity.pyc in _tolist(self, work_list)
409
410 def _tolist(self, work_list):
--> 411 for i in range(len(work_list)):
412 #if it's a list then iterate through that list

413 if isinstance(work_list[i], list):

TypeError: object of type 'float' has no len()