二维 ndarray 的 Numpy fromfile 的填充顺序是什么?

What is the fill order of Numpy fromfile for a 2-D ndarray?

我正在尝试使用 numpy.fromfile() 函数读取结构化二进制文件。在我的例子中,我有一个 numpy.dtype() 用于定义用户定义的数据类型以与 np.fromfile().
一起使用 我将在这里复制数据结构的相关部分(因为完整的结构相当长):

('RawData', np.int32, (2, BlockSize))  

这会将 BlockSize*2 个 int32 读入字段 RawData,将生成一个 2xBlockSize 矩阵。这是我遇到麻烦的地方,因为我想复制 Matlab fread() function, in which the matric is filled in column order 的行为。至于NumPy的fromfile(),这个没有提到(至少我没找到)。

NumPy 的 fromfile() 应该像 Matlab 的 fread() 一样工作并不重要,但我必须知道 NumPy 的 fromfile() 如何工作以进行相应的编码。

现在的问题是,在使用自定义数据类型时,NumPy fromfile() 函数中二维数组的填充顺序是什么?

默认情况下,在创建新的二维数组时,NumPy 将使用 "C" 排序,即 row-major。这与 Matlab 使用的顺序相反。

例如BlockSize为4,原始数据为

0 1 2 3 4 5 6 7

那么 2 x 4 数组将是

[[0, 1, 2, 3],
 [4, 5, 6, 7]]

使用 Matlab 和相同的原始数据,2 x 4 数组将是

[[0, 2, 4, 6],
 [1, 3, 5, 7]]

fromfiletofile read/write 平面,1d,数组:

In [204]: x = np.arange(1,11).astype('int32')                                                          
In [205]: x.tofile('data615')                                                                          

fromfile returns 一维数组:

In [206]: np.fromfile('data615',np.int32)                                                              
Out[206]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int32)

x.reshape(2,5).tofile(...) 会保存同样的东西。 tofile 不保存 dtypeshape 信息。

整形为2d,默认顺序为'C':

In [207]: np.fromfile('data615',np.int32).reshape(2,5)                                                 
Out[207]: 
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]], dtype=int32)

但它可以像这样更改为 MATLAB:

In [208]: np.fromfile('data615',np.int32).reshape(2,5, order='F')                                      
Out[208]: 
array([[ 1,  3,  5,  7,  9],
       [ 2,  4,  6,  8, 10]], dtype=int32)

基础databuffer是相同的,只是一维字节数组。

编辑

该文件可以作为 2 整数结构读取:

In [249]: np.fromfile('data615','i4,i4')                                                               
Out[249]: 
array([(1,  2), (3,  4), (5,  6), (7,  8), (9, 10)],
      dtype=[('f0', '<i4'), ('f1', '<i4')])
In [250]: _['f0']                                                                                      
Out[250]: array([1, 3, 5, 7, 9], dtype=int32)

它仍然是一维数组,但数字按 2 分组。

转换为复数:

In [252]: xx = np.fromfile('data615','i4,i4')                                                          
In [253]: xx['f0']+1j*xx['f1']                                                                         
Out[253]: array([1. +2.j, 3. +4.j, 5. +6.j, 7. +8.j, 9.+10.j])
In [254]: _.dtype                                                                                      
Out[254]: dtype('complex128')

如果数据已经保存为浮点数,我们可以直接将它们加载为复数:

In [255]: x.astype(np.float32).tofile('data615f')                                                      
In [257]: xx = np.fromfile('data615f',np.complex64)                                                    
In [258]: xx                                                                                           
Out[258]: array([1. +2.j, 3. +4.j, 5. +6.j, 7. +8.j, 9.+10.j], dtype=complex64)

从整数序列得到复数的另一种方法:

In [261]: np.fromfile('data615', np.int32).reshape(5,2)                                                
Out[261]: 
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]], dtype=int32)
In [262]: xx = np.fromfile('data615', np.int32).reshape(5,2)                                           
In [263]: xx[:,0]+1j*xx[:,1]                                                                           
Out[263]: array([1. +2.j, 3. +4.j, 5. +6.j, 7. +8.j, 9.+10.j])