[SciPy-user] Possible bug in loadmat and savemat use of fread and fwrite

Souheil Inati souheil.inati at nyu.edu
Wed Dec 21 06:58:12 CST 2005


Hi all,

I am new to the scipy (and python).  I have just gotten scipy working  
on a mac G4 OS X 10.4.4 with python 2.4.2, thanks to Chris  
Fonnesbeck's instructions.  Apologies in advance for the long post  
and if I am asking a question previously answered on the mailing  
list.  I can't figure out how to search the mailing list properly.   
Mailman itself (scipy.net) doesn't have a search and the plone  
interface to mailman on scipy.org does nothing for searching.  Google  
is not terrible.  Anyway....

  I'm having problems with io functions loadmat and savemat. I've dug  
as far as my limited python knowledge can get me and my current  
working hypothesis is that there is a bug in the low-level fread and  
fwrite functions, or maybe the way in which the loadmat and savemat  
functions call them.  The code in io/examples works fine and the  
reading and writing of binary files via a combination of fopen and  
fwrite works fine as well.

Any help or nudge in the proper direction would be greatly appreciated.

Thanks,
Souheil


---------------------------------

Souheil Inati, PhD
Assistant Professor
Center for Neural Science and Department of Psychology
New York University
4 Washington Place, Room 809
New York, N.Y., 10003-6621
Office: (212) 998-3741
Email: souheil.inati at nyu.edu


------------------------------------
------------------------------------
Here's the behavior that i see for loadmat

I created a file in Matlab 7.1
 >> A = randn(4)
 >> save -V6 foo A

In python:
from scipy import *
b = io.loadmat('foo.mat')
gives a bus error.

Reading through io/mio.py at around line 744 in the the beginning of  
the loadmat function is this bit of code:
     fid = fopen(full_name,'rb')
     test_vals = fid.fread(4,'byte')

If I try to run this directly from python, like this:
from scipy import *
fid = fopen('foo.mat','rb')
test_vals = fid.fread(4,'byte')
I get the same bus error.

Using the lower level python file functions directly works:
fid = file('foo.mat','rb')
b = fid.fread(4)
returns a string b = 'MATL'  as expected. So presumable there is an  
error in fread.

------------------------------------
------------------------------------
For the savemat function the problem looks like this:
from scipy import *
io.savemat('foo2.mat',{'a': array([1.,2.,3.])})

------------------------------------------------------------------------ 
---
exceptions.TypeError                                 Traceback (most  
recent call last)

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
packages/scipy/io/mio.py in savemat(filename, dict)
     871         imagf = var.dtypechar in ['F', 'D']
     872         fid.fwrite([var.shape[1], var.shape[0], imagf, len 
(variable)+1],'int')
--> 873         fid.fwrite(variable+'\x00','char')
     874         if imagf:
     875             fid.fwrite(var.real)

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
packages/scipy/io/mio.py in write(self, data, mtype, bs)
     219         howmany,mtype = getsize_type(mtype)
     220         count = product(data.shape)
--> 221         numpyio.fwrite(self,count,data,mtype,bs)
     222         return
     223

TypeError: argument 4 must be char, not None






More information about the SciPy-user mailing list