tags:

views:

83

answers:

1

Hi,

I usually don't post questions on these forums, but I've searched all over the place and I haven't found anything about this issue.

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)
+1  A: 

Here is a possible workaround:

Since your custom dtype causes a problem, supply a flattened dtype instead:

In [77]: arr=np.genfromtxt('a',dtype='f8,f8')

In [78]: arr
Out[78]: 
array([(1.0, 2.0), (3.0, 4.0)], 
      dtype=[('f0', '<f8'), ('f1', '<f8')])

Then use astype to convert to your desired dtype:

In [79]: arr=np.genfromtxt('a',dtype='f8,f8').astype(dtype)

In [80]: arr
Out[80]: 
array([(1.0, 2.0), (3.0, 4.0)], 
      dtype=[(('Amps', 'Current'), '<f8'), (('Volts', 'Voltage'), '<f8')])

Edit: Another alternative is to monkey-patch numpy.lib.io.flatten_dtype:

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
unutbu
Thanks for the workaround. That's working for me, but it's a bit tough to generalize. I do a great deal of data processing, and I'm working on a project that will automate much of what I do. I was hoping to use these structured arrays as my data container for the whole project. Unfortunately, the more I look into it, the more these titles seem like an afterthought, tacked on to numpy without full support. I'm thinking of monkey patching the offending numpy.lib.io.flatten_dtype(). I know that isn't generally good practice, but in this case it seems like the easiest route. Any thoughts?
Emma
I've added some code showing how flatten_dtype could be monkey-patched. I think using a monkey-patch would be okay as a quick fix, but of course it would be much more satisfying to get this bug fixed in numpy so no one will be plagued with this problem in the future.To that end, you may want to file a ticket at http://projects.scipy.org/numpy/report and/or post a question on the numpy-discussion mailing list (http://scipy.org/Mailing_Lists). The developers read that mailing list and should be able to give you much better advice than I can. Maybe even fix it, if it's a bug.
unutbu
Thanks a lot, I'll do that.
Emma