A Monday 01 June 2009 21:09:58 escriguéreu:
> On Jun 1, 2009, at 12:00 PM, Francesc Alted wrote:
> > A Monday 01 June 2009 19:25:17 Robert Ferrell escrigué:
> >> I'm trying to write a table of numpy records, but failing.
> >>
> >> I have a numpy dtype
> >>
> >> dtp = np.dtype([('x',np.float64), ('y', np.float64)])
> >>
> >> I was hoping I could make columns with this:
> >>
> >> class DoesNotWork(tables.IsDescription):
> >>    XY = tables.Col.from_atom(tables.Atom.from_dtype(dtype=dtp))
> >>
> >> but PyTables doesn't like that.  The complaint is "compound data
> >> types
> >> are not supported: dtype([('x', '<f8'), ('y', '<f8')])".
> >>
> >> I can make a compound data type manually just fine:
> >>
> >> class ThisWorks(tables.IsDescription):
> >>    x = tables.Float64Col()
> >>    y = tables.Float64Col()
> >>
> >> Is it possible to make a table description directly from my dtype?
> >> If
> >> not, is there some other way to create the description directly from
> >> the dtype?
> >
> > Mmh, the `createTable()` constructor does accept a record array as
> > input.
> > Would that be enough for you?
>
> I'm not sure.  What I actually have is a list of (label, array)
> tuples, where label is a string and array has dtype like above.  So
> what I really want is something like:
>
> class MyTable(tables.IsDescription):
>               label = tables.StringCol(16)
>               XY    = tables.Col.from_atom(tables.Atom.from_dtype(dtype=dtp))
>
> That would define a row, and then I'd just loop over my list and
> insert the rows.
>
> for l in myList:
>       row['label'] = l[0]
>       row['XY'] = l[1]
>       row.append()
>
> Am I close?

Ah, OK.  You can pass a dictionary instead.  See this example:


import numpy as np
import tables as tb

descr = {'label':tb.StringCol(16)}  # Initial table description
dtp = np.dtype('f8,f4')  # NumPy type to append to above description

# Create a dictionary with the fields in PyTables format
dtp_ = dict([(f, tb.Col.from_dtype(t[0])) for f, t in dtp.fields.items()])
descr.update(dtp_)   # Update the description dictionary

f = tb.openFile("test.h5", 'w')
t = f.createTable(f.root, 'table', descr)   # Create the table
print `t`
f.close()


and the output:

/table (Table(0,)) ''
  description := {
  "f0": Float64Col(shape=(), dflt=0.0, pos=0),
  "f1": Float32Col(shape=(), dflt=0.0, pos=1),
  "label": StringCol(itemsize=16, shape=(), dflt='', pos=2)}
  byteorder := 'little'
  chunkshape := (292,)


Is that what you are after?

-- 
Francesc Alted

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users

Reply via email to