[SciPy-user] structure arrays in SciPy

Robert Kern rkern at ucsd.edu
Fri May 13 09:41:08 EDT 2005


Ryan Krauss wrote:
> Maybe an example would make this clearer.  What if I want to store a 
> collection of objects with the following properties:
> entry1.input='a1'
> entry1.output='j2'
> entry1.datafile=['file1','file2','file3']
> entry1.data1=[1.2,1.47,3.56,....]
> entry1.data2=[-1.5,-19.5,35.5,....]
> 
> where data1 and data2 are actually Numeric or numarray arrays/matrices.  
> How should I go about storing a collection of these objects in SciPy?  
> Should I be defining my own class and simply make a list of them and 
> pickle the list?

I use and highly recommend PyTables for such tasks.

http://pytables.sourceforge.net

If you're restricted to Scipy only, you could use scipy.io.save().

In [2]:class Params(dict):
    ...:    def __init__(self, **kwds):
    ...:        dict.__init__(self, **kwds)
    ...:        self.__dict__ = self
    ...:

In [4]:entry1 = Params(input='a1', output='j2')

In [5]:entry1.datafile = ['file1', 'file2', 'file3']

In [6]:entry1.data1=[1.2,1.47,3.56]

In [7]:entry1.data2=[-1.5,-19.5,35.5]

In [9]:io.save?
Type:           function
Base Class:     <type 'function'>
String Form:    <function save at 0x5c7cf0>
Namespace:      Interactive
File: 
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/scipy/io/data_store.py
Definition:     io.save(file_name=None, data=None)
Docstring:
     Save the dictionary "data" into
     a module and shelf named save


In [10]:io.save('entry1', entry1)

In [13]:import entry1

In [14]:entry1.
entry1.__class__         entry1.__new__           entry1.data_store
entry1.__delattr__       entry1.__reduce__        entry1.datafile
entry1.__dict__          entry1.__reduce_ex__     entry1.dir
entry1.__doc__           entry1.__repr__          entry1.entry1
entry1.__file__          entry1.__setattr__       entry1.input
entry1.__getattribute__  entry1.__str__           entry1.output
entry1.__hash__          entry1.dat               entry1.py
entry1.__init__          entry1.data1             entry1.pyc
entry1.__name__          entry1.data2

In [14]:entry1.input
Out[14]:'a1'

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the SciPy-User mailing list