Hi Torgil,

A Dijous 19 Octubre 2006 17:46, vàreu escriure:
> > > One thing that I tend o do quite often is to iterate a number of rows
> > > and select a set of columns which meet some criterias on another set
> > > of columns; the result is delivered as an numpy recarray (or a class
> > > that inherits from it). It would be nice if I can build the resulting
> > > dtype directly from the column descriptions. Is there an easy way of
> > > doing this now?
> >
> > Yes. You can do this from PyTables 1.2 on by doing:
> >
> >         mydtype = numpy.dtype(mytable.description._v_nestedDescr)
>
> This is not exactly what I need. I want a subset of both columns and rows.
>
> Lets say I have a table with nested columns,each column named with a
> letter and each sub-column named with a letter and a digit such that
> "A" means the whole group with columns "A1","A2",...
>
> this is not correct syntax, more describing what I mean:
>
> intresting_cols=["A","B/B1","C","H/H3","H/H4"]
> res_dtype=dtype([table[col].dtype for col in interesting_cols])
> data=fromiter(([row[col] for col in intresting_cols] for row in
> table.itersequence(...)),dtype=res_dtype)
>
> a=data.group("A/A1")
> b=data.group("B/B1")
>
> but where for example "B/B2" doesn't appear in data.

Mmm, this is a bit more involved, but still, possible :-). The trick is to 
make use of the Description._v_walk() method (beware, this violates the 
PyTables naming convention of prefixing public methods with _f_, and this 
will be fixed in PyTables 2.x series). The next example should make things 
clearer:

-----------------------------------------------------------------------------
import tables as t
import numpy

# The declaration of the nested table
class Info(t.IsDescription):
    Name = t.StringCol(length=2)
    Value = t.Complex64Col()

class TestTDescr(t.IsDescription):
    x = t.Int32Col(0, shape=2, pos=0)
    y = t.FloatCol(1, shape=(2,2))
    z = t.UInt8Col(1)
    info = Info()
    class Info(t.IsDescription):
        y2 = t.FloatCol(1, pos=1)
        z2 = t.UInt8Col(1)
        class Info2(t.IsDescription):
            z3 = t.EnumCol({'r':4, 'g':2, 'b':1}, 'r', shape=2)
            name = t.StringCol(length=2)
            value = t.Complex64Col(shape=2)

if __name__ == '__main__':
    h5file = t.openFile("prova.h5", "w")
    table = h5file.createTable('/', 'test', TestTDescr)
    interesting_cols = ["info/Name", "Info/z2", "Info/Info2/value"]
    tdescr = table.description
    interesting_descr = [ col.recarrtype for col in tdescr._v_walk('Col')
                          if col._v_pathname in interesting_cols ]
    res_dtype = numpy.dtype(",".join(interesting_descr))
    print "res_dtype-->", res_dtype
    h5file.close()

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Pytables-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pytables-users

Reply via email to