[Numpy-discussion] Using numpy's flatten_dtype with structured dtypes that have titles

Emma Willemsma emma.willemsma at morgansolar.com
Thu Aug 19 17:07:03 EDT 2010


Hi,

I originally posted this question on stackoverflow, and the user who
responded to me suggested that I bring this issue to the attention of the
numpy developers.

I am working with structured arrays to store experimental data. I'm using
titles to store information about my fields, in this case the units of
measure. When I call numpy.lib.io.flatten_dtype() on my dtype, I get:

ValueError: too many values to unpack
File "c:\Python25\Lib\site-packages\numpy\lib\_iotools.py", line 78,
in flatten_dtype

  (typ, _) = ndtype.fields[field]

I wouldn't really care, except that numpy.genfromtxt() calls
numpy.lib.io.flatten_dtype(), and I need to be able to import my data from
text files.

I'm wondering if I've done something wrong. Is flatten_dtype() not meant to
support titles? Is there a work-around for genfromtxt()?

Here's a snippet of my code:
import numpy
fname = "C:\\Somefile.txt"
dtype = numpy.dtype([(("Amps","Current"),"f8"),(("Volts","Voltage"),"f8")])
myarray = numpy.genfromtxt(fname,dtype)

The user who responded suggested the following monkey-patch:

import numpy
import numpy.lib.io
def flatten_dtype(ndtype):

    """
    Unpack a structured data-type.

    """
    names = ndtype.names

    if names is None:
        return [ndtype]

    else:
        types = []
        for field in names:

            typ_fields = ndtype.fields[field]

            flat_dt = flatten_dtype(typ_fields[0])

            types.extend(flat_dt)
        return types

numpy.lib.io.flatten_dtype=flatten_dtype

Is this something that could be changed in numpy?

Thanks!
Emma
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100819/b4af515f/attachment.html>


More information about the NumPy-Discussion mailing list