I think Mathematica handles .mat file not very well, python is pretty good at this kind of jobs. In this site, there are plenty of question related to calling Mathematica from Python, or calling Python from Mathematica. The answers are only valid for very small data, I believe like hundreds of numbers. Currently, I am working with data a little larger than that, a typical size is about (40960, 256). Thus I want to ask whether there is a solution to transfer data from one to another? Or a better .mat file interface?
Following is a sample of what is inside the .mat file.
The data transfer between R
and python
can be done seamlessly. Check rpy2
for more information. Maybe RLink
can be used in this case.
Update
Handling .mat file in Mathematica is not as fast as in Python. However, thanks for linked page suggested by @The Toad, this process is much better.
Following are some tests:
in Python:
%time matlab.whosmat('test.mat')
CPU times: user 152 ms, sys: 10.2 ms, total: 162 ms
Wall time: 164 ms
Out[153]:
[('exp03', (20480, 2), 'double'),
('exp22', (22528, 2), 'double'),
('exp09', (22528, 2), 'double'),
('exp31', (22528, 2), 'double'),
...
in Mathematica:
In[166]:= Timing@Import["~/test.mat", {"MAT", "Labels"}]
Out[166]= {0.851815, {"exp03", "exp22", "exp09", "exp31", "exp30",
"exp33", "exp32", "exp35", "exp34", "exp19", "exp18", "exp17",
"exp16", "exp15", "exp14", "exp13", "exp12", "exp11", "exp10",
"exp37", "exp36", "exp02", "Data", "exp08", "exp23", "exp20",
"exp21", "exp26", "exp27", "exp24", "exp25", "exp01", "exp28",
"exp29", "exp04", "exp05", "exp06", "exp07"}}
in Python
%time data=matlab.loadmat('test.mat')
CPU times: user 199 ms, sys: 28.2 ms, total: 228 ms
Wall time: 236 ms
in Mathematica:
In[168]:= Timing@Import["~/test.mat", {"MAT", "LabeledData"}];
In[169]:= %[[1]]
Out[169]= 0.838842
For another file:
In Python:
%time data=matlab.loadmat('test2.mat')
CPU times: user 3.55 s, sys: 381 ms, total: 3.93 s
Wall time: 3.98 s
In Mathematica:
In[1]:= Timing@Import["~/test2.mat", {"MAT", "LabeledData"}];
In[2]:= First[%]
Out[2]= 12.558158
HDF5
format as I suggest in the answer linked above. It would just be a replacement for what you're already doing withMAT
files, it seems. And I have yet to see any file format that is faster to import and export thanHDF5
. $\endgroup$HDF5
is impressive, I add some timing results in the question. Maybe I should convert all the data files to .h5. $\endgroup$