[SciPy-user] common storage between matlab and python

Robert Cimrman cimrman3@ntc.zcu...
Wed May 7 10:54:29 CDT 2008


Neal Becker wrote:
> I've been interested in looking at pytables, but the doc makes it look like
> there's a fair learning curve.  Anyone have a _simple_ example, maybe a
> simple numpy vector, or a few vectors?

I used to use the code below, not sure if it works with the current 
versions, but anyway, it can give you an idea how simple it is.

best regards,
r.

def writeSparseMatrixHDF5( fileName, mtx, name = 'a sparse matrix' ):
     """Assume CSR/CSC."""
     fd = pt.openFile( fileName, mode = "w", title = name )
     try:
         info = fd.createGroup( '/', 'info' )
         fd.createArray( info, 'dtype', mtx.dtype.str )
         fd.createArray( info, 'shape', mtx.shape )
         fd.createArray( info, 'format', mtx.format )

         data = fd.createGroup( '/', 'data' )
         fd.createArray( data, 'data', mtx.data )
         fd.createArray( data, 'indptr', mtx.indptr )
         fd.createArray( data, 'indices', mtx.indices )

     except:
         print 'matrix must be in SciPy sparse CSR/CSC format!'
         print mtx.__repr__()
         raise

     fd.close()

def readSparseMatrixHDF5( fileName, outputFormat = None ):
     import scipy.sparse as sp
     constructors = {'csr' : sp.csr_matrix, 'csc' : sp.csc_matrix}

     fd = pt.openFile( fileName, mode = "r" )
     info = fd.root.info
     data = fd.root.data

     format = info.format.read()
     if not isinstance( format, str ):
         format = format[0]

     dtype = info.dtype.read()
     if not isinstance( dtype, str ):
         dtype = dtype[0]

     if outputFormat is None:
         constructor = constructors[format]
     else:
         constructor = constructors[outputFormat]

     if format in ['csc', 'csr']:
         mtx = constructor( (data.data.read(),
                             data.indices.read(), data.indptr.read()),
                            dims = info.shape.read(), dtype = dtype )
     elif format == 'coo':
         mtx = constructor( (data.data.read(),
                             nm.c_[data.rows.read(), data.cols.read()].T),
                            dims = info.shape.read(), dtype = dtype )
     else:
         print format
         raise ValueError
     fd.close()

     if outputFormat in ['csc', 'csr']:
         mtx.sort_indices()

     return mtx


More information about the SciPy-user mailing list