Christopher A Mejia
unread,Apr 22, 2009, 12:50:17 PM4/22/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scipy...@scipy.org
Hi,
I'm trying to write a NumPy record array
using the savemat function, using the format='5' default, but I am not
having much success. Here's an example using a NumPy record array
defined in the NumPy User Guide:
-----------------------------------------
>>> import numpy as np
>>> x = np.zeros(3, dtype=[(’x’,’f4’),(’y’,np.float32),(’value’,’f4’,(2,2))])
SyntaxError: invalid syntax
>>> x = np.zeros(3, dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))])
>>> x
array([(0.0, 0.0, [[0.0, 0.0], [0.0,
0.0]]),
(0.0, 0.0,
[[0.0, 0.0], [0.0, 0.0]]),
(0.0, 0.0,
[[0.0, 0.0], [0.0, 0.0]])],
dtype=[('x', '<f4'),
('y', '<f4'), ('value', '<f4', (2, 2))])
>>> from scipy.io.matlab.mio
import savemat
>>> savemat('record_array_test.mat',
{'x': x})
Traceback (most recent call last):
File "<pyshell#6>",
line 1, in <module>
savemat('record_array_test.mat',
{'x': x})
File "C:\Python25\lib\site-packages\scipy\io\matlab\mio.py",
line 159, in savemat
MW.put_variables(mdict)
File "C:\Python25\lib\site-packages\scipy\io\matlab\mio5.py",
line 974, in put_variables
mat_writer.write()
File "C:\Python25\lib\site-packages\scipy\io\matlab\mio5.py",
line 736, in write
self.arr = self.arr.astype('f8')
ValueError: setting an array element
with a sequence.
>>>
-----------------------------------------
Actually, what I'd like to do is to
be able to handle an arbitrarily nested record array, as in:
-----------------------------------------
>>> spam = np.zeros(2, dtype=[('a','f4'),
('b', [('x', 'f4'), ('y', 'f4', (2,2))])])
>>> spam
array([(0.0, (0.0, [[0.0, 0.0], [0.0,
0.0]])),
(0.0, (0.0,
[[0.0, 0.0], [0.0, 0.0]]))],
dtype=[('a', '<f4'),
('b', [('x', '<f4'), ('y', '<f4', (2, 2))])])
>>> savemat('record_array_test2.mat',
{'spam': spam})
Traceback (most recent call last):
File "<pyshell#9>",
line 1, in <module>
savemat('record_array_test2.mat',
{'spam': spam})
File "C:\Python25\lib\site-packages\scipy\io\matlab\mio.py",
line 159, in savemat
MW.put_variables(mdict)
File "C:\Python25\lib\site-packages\scipy\io\matlab\mio5.py",
line 974, in put_variables
mat_writer.write()
File "C:\Python25\lib\site-packages\scipy\io\matlab\mio5.py",
line 736, in write
self.arr = self.arr.astype('f8')
ValueError: setting an array element
with a sequence.
-----------------------------------------
As you can see, I get the same error
for the nested case. I know what I am trying to do is possible, because
I can generate my desired nested structure array in MATLAB, then do a "round-trip"
loadmat(,struct_as_record=True) and savemat() to get back the same thing
in MATLAB. However, I cannot seem to reverse engineer what loadmat(,struct_as_record=True)
does to create the NumPy record array. Two differences appear to
be that the dtype definition created by loadmat(,struct_as_record=True)
does not print out as being nested, it just shows a '|04' type (set by
the keyword "object"); also scalars and one-dimensional vectors
appear to be upconverted to 2-d matrices. Perhaps someone has a routine
that I can use to pre-process my nested record array so it works with savemat?
FYI, I'm using Python 2.5.4, NumPy 1.2.1
and SciPy 0.7.0.
Thanks in advance for any help,
--Chris
( P.S. I apologize in advance
if this post shows up twice...my first attempt seems to have gotten lost.)