From: Francesc A. <fa...@ca...> - 2005-06-16 16:03:40
|
A Thursday 16 June 2005 14:17, phi...@ho... va escriure: > for retrieving all the data from an Array (or a Table) object, i read > the data and save it into a numarray.array using the read() function: > my_numarray_array =3D my_object.read(). > > for example: > readout =3D h.root.detector.readout > b=3Dreadout.read() > print b > > That's really nice to use and i can easilly treat data after that. With *Array objects you can just select multimensional slices: readout[2,3:20,...,:] But filtering by value is not supported for *Array objects, at least in a efficient way. You should use Table objects instead. > My question is about selecting only a part of data: > Can i create directly a numarray.array object using a python list > comprehension on an Array or a Table? > > For the moment using list comprehension, i only get a list. > > for example: > >>> pressure =3D h.root.columns.pressure > >>> a=3D[p for p in pressure] > >>> print a > > [25.0, 36.0, 49.0] > > >>> a=3D[p for p in pressure if p <36] > >>> print a > > [25.0] > > >>> type(a) > > <type 'list'> > > I want to get a numarray.array. > > 1. Can i cast the list into a numarray.array? Yes: array(a). Read the numarray manual. > 2. Can i copy all the data from the list to a numarray.array once? Yes, as above. > 3. Must i instrospect the list using the for elt in list: and add each > data in a numarray? You can, but this is quite inefficient. Anyhow, if you want to do efficient searches and get numarray object as a result take a look at this example: In [25]:rout =3D f.root.detector.readout In [26]:rout Out[26]: /detector/readout (Table(10L,)) 'Readout example' description :=3D { "ADCcount": Col(dtype=3D'UInt16', shape=3D1, dflt=3DNone, pos=3D0, indexe= d=3DFalse), "TDCcount": Col(dtype=3D'UInt8', shape=3D1, dflt=3DNone, pos=3D1, indexed= =3DFalse), "energy": Col(dtype=3D'Float64', shape=3D1, dflt=3DNone, pos=3D2, indexed= =3DFalse), "grid_i": Col(dtype=3D'Int32', shape=3D1, dflt=3DNone, pos=3D3, indexed= =3DFalse), "grid_j": Col(dtype=3D'Int32', shape=3D1, dflt=3DNone, pos=3D4, indexed= =3DFalse), "idnumber": Col(dtype=3D'Int64', shape=3D1, dflt=3DNone, pos=3D5, indexed= =3DFalse), "name": StringCol(length=3D16, dflt=3DNone, shape=3D1, pos=3D6, indexed= =3DFalse), "pressure": Col(dtype=3D'Float32', shape=3D1, dflt=3DNone, pos=3D7, index= ed=3DFalse)} byteorder :=3D little In [28]:rout.cols.pressure[:] Out[28]:array([ 0., 1., 4., 9., 16., 25., 36., 49., 64., 81.],= =20 type=3DFloat32) In [31]:idx =3D rout.getWhereList(rout.cols.pressure < 36, flavor=3D"NumArr= ay") In [32]:idx Out[32]:array([0, 1, 2, 3, 4, 5], type=3DInt64) In [33]:f.root.detector.readout.readCoordinates(idx, field=3D"pressure") Out[33]:array([ 0., 1., 4., 9., 16., 25.], type=3DFloat32) This is really fast as most of the operations are made in C space; besides, you don't need to load all your table data in memory. If you want further speed, you might want to index any of the columns you are using to filter the data. See the PyTables manual for more info on indexing capabilities. Cheers, =2D-=20 >0,0< Francesc Altet =A0 =A0 http://www.carabos.com/ V V C=E1rabos Coop. V. =A0=A0Enjoy Data "-" |