Python hdf5storage正在转换我的数据?

Python hdf5storage正在转换我的数据?,python,hdf5,h5py,hdf5storage,Python,Hdf5,H5py,Hdf5storage,Python代码: 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(

Python代码:

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']
创建时,它具有一个形状(128256512)。但是,当我使用hdf5storage将a保存到.mat文件中,然后使用h5py将其加载到b中时,b被转换为(512256128)的形状。两个数组在检查其标志时都是C连续的


有没有办法防止这种转置发生?我的印象是hdf5格式保存了行主键

我再次查看了下面描述的
abc.h5
文件:

它是在八度音阶中创建的,具有:

>> A = [1,2,3;4,5,6];
>> B = [1,2,3,4];
>> save -hdf5 abc.h5 A B
使用
h5py

In [102]: f = h5py.File('abc.h5','r')
In [103]: A = f['A']['value'][:]
In [104]: A
Out[104]: 
array([[1., 4.],
       [2., 5.],
       [3., 6.]])
In [105]: A.shape
Out[105]: (3, 2)
In [106]: A.flags
Out[106]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  ...
In [107]: A.ravel()
Out[107]: array([1., 4., 2., 5., 3., 6.])
这是一个转置的C阶数组。显然,这就是MATLAB开发人员选择在HDF5中存储矩阵的方式

我可以用numpy来表达:

In [108]: At = A.T
In [109]: At
Out[109]: 
array([[1., 2., 3.],
       [4., 5., 6.]])
In [110]: At.flags
Out[110]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  ....
通常,C阶数组在转置时变为F阶

以旧的.mat格式保存的倍频程矩阵

In [115]: data = io.loadmat('../abc.mat')
In [116]: data['A']
Out[116]: 
array([[1., 2., 3.],
       [4., 5., 6.]])
In [117]: _.flags
Out[117]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : True
因此,转置的
h5py
数组与
io.loadmat
已经使用了相当一段时间的约定相匹配


我没有在此操作系统上安装
hdf5storage
。但是通过您的测试,它遵循的是
io.loadmat
约定-形状正确,但顺序为F。

MATLAB约定是F-连续的。您可以在通过
scipy.io.loadmat
加载的数组中看到这一点。
HDF5
约定是C-连续的。我刚刚看了一下MATLAB/Octave生成的HDF5文件(使用
h5py
)。我不知道什么样的游戏
hdf5storage
正在使用这些约定。不幸的是,我不知道还有什么东西支持v7.3.mat文件。当尝试使用scipy时,会弹出一个错误,提示对该类型的文件使用h5py。我的同事只使用MATLAB,所以我尝试创建数据文件(.mat),我们可以轻松地来回传递。只要你与MATLAB交互,你就必须处理顺序差异和转置。在numpy中,第一个轴是最外层的。在MATLAB中,它是最后一个。