Python Code:
import h5py
import hdf5storage
from functools import reduce
import numpy as np
from operator import mul
sz = 128,256,512
a = np.random.normal(size=reduce(mul,sz)).reshape(sz)
save_dict = {'data':a}
spath = r"test.mat"
hdf5storage.savemat(spath, mdict=save_dict, append_mat=False,
store_python_metadata=True, format='7.3')
with h5py.File(spath, 'r') as file:
b = np.array(file['data'])
# Reads in the correct shape, but is F-contiguous. Scipy doesn't work with v7.3 files.
c = hdf5storage.loadmat(spath)['data']
When a is created, it has a shape (128,256,512). However, when I save a to the .mat file using hdf5storage, and then load it into b using h5py, b is transposed as has a shape of (512,256,128). Both arrays are C-contiguous when checking their flags.
Is there any way to prevent this transpose from happening? I was under the impression that hdf5 format saves row-major.
I looked again at the
abc.h5file described in:how to import .mat-v7.3 file using h5py
It was created in Octave with:
Using
h5py:So it's a transposed C order array. Apparently that's how MATLAB developers have chosen to store their matrices in HDF5.
I could tranpose it in numpy:
As is normal, a C-order array becomes F-order when transposed.
The Octave matrices saved with the older .mat format
So the
h5pyarray, transposed, matches the convention thatio.loadmathas been using for quite some time.I don't have
hdf5storageinstalled on this OS. But by your tests, it is following theio.loadmatconvention - correct shape but F order.