scipy.io.loadmat 嵌套结构(即字典)

Posted

技术标签:

【中文标题】scipy.io.loadmat 嵌套结构(即字典)【英文标题】:scipy.io.loadmat nested structures (i.e. dictionaries) 【发布时间】:2011-10-23 22:18:29 【问题描述】:

使用给定的例程(如何使用 scipy 加载 Matlab .mat 文件),我无法访问更深层的嵌套结构以将它们恢复到字典中

为了更详细地介绍我遇到的问题,我给出了以下玩具示例:

load scipy.io as spio
a = 'b':'c':'d': 3
# my dictionary: a['b']['c']['d'] = 3
spio.savemat('xy.mat',a)

现在我想将 mat-File 读回 python。我尝试了以下方法:

vig=spio.loadmat('xy.mat',squeeze_me=True)

如果我现在想访问我得到的字段:

>> vig['b']
array(((array(3),),), dtype=[('c', '|O8')])
>> vig['b']['c']
array(array((3,), dtype=[('d', '|O8')]), dtype=object)
>> vig['b']['c']['d']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/<ipython console> in <module>()

ValueError: field named d not found.

但是,通过使用选项 struct_as_record=False 可以访问该字段:

v=spio.loadmat('xy.mat',squeeze_me=True,struct_as_record=False)

现在可以通过

>> v['b'].c.d
array(3)

【问题讨论】:

使用默认设置,可以使用如下表达式挖掘嵌套; vig['b']['c'].item()['d'].item(),解析结构化数组和对象数组的混合。 `['b'] 是字典索引,其他的是字段名索引。 【参考方案1】:

找到了解决办法,可以通过以下方式访问“scipy.io.matlab.mio5_params.mat_struct object”的内容:

v['b'].__dict__['c'].__dict__['d']

【讨论】:

你在 loadmat 中使用了哪些选项?【参考方案2】:

这里是重建字典的函数,只使用这个 loadmat 而不是 scipy.io 的 loadmat:

import scipy.io as spio

def loadmat(filename):
    '''
    this function should be called instead of direct spio.loadmat
    as it cures the problem of not properly recovering python dictionaries
    from mat files. It calls the function check keys to cure all entries
    which are still mat-objects
    '''
    data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True)
    return _check_keys(data)

def _check_keys(dict):
    '''
    checks if entries in dictionary are mat-objects. If yes
    todict is called to change them to nested dictionaries
    '''
    for key in dict:
        if isinstance(dict[key], spio.matlab.mio5_params.mat_struct):
            dict[key] = _todict(dict[key])
    return dict        

def _todict(matobj):
    '''
    A recursive function which constructs from matobjects nested dictionaries
    '''
    dict = 
    for strg in matobj._fieldnames:
        elem = matobj.__dict__[strg]
        if isinstance(elem, spio.matlab.mio5_params.mat_struct):
            dict[strg] = _todict(elem)
        else:
            dict[strg] = elem
    return dict

【讨论】:

这需要更好地宣传。 scipy 的 loadmat 的当前实现是一个真正的痛苦。很棒的工作! 其实@jpapon 下面的方法更好,在处理图像之类的数组时很有必要。 非常感谢!这太棒了! 起来,起来,你应该去!请将此发送给 Mathworks,并告诉他们一起行动。 这是迄今为止最好的答案,但仍然不完美,因为它压缩了 1 元素维度。我可能有这个修复的不寻常需要 + 需要保持 1 元素尺寸。【参考方案3】:

只是对合并答案的增强,不幸的是,如果它到达对象的单元格数组,它将停止递归。以下版本将改为列出它们,并在可能的情况下继续递归到元胞数组元素中。

import scipy.io as spio
import numpy as np


def loadmat(filename):
    '''
    this function should be called instead of direct spio.loadmat
    as it cures the problem of not properly recovering python dictionaries
    from mat files. It calls the function check keys to cure all entries
    which are still mat-objects
    '''
    def _check_keys(d):
        '''
        checks if entries in dictionary are mat-objects. If yes
        todict is called to change them to nested dictionaries
        '''
        for key in d:
            if isinstance(d[key], spio.matlab.mio5_params.mat_struct):
                d[key] = _todict(d[key])
        return d

    def _todict(matobj):
        '''
        A recursive function which constructs from matobjects nested dictionaries
        '''
        d = 
        for strg in matobj._fieldnames:
            elem = matobj.__dict__[strg]
            if isinstance(elem, spio.matlab.mio5_params.mat_struct):
                d[strg] = _todict(elem)
            elif isinstance(elem, np.ndarray):
                d[strg] = _tolist(elem)
            else:
                d[strg] = elem
        return d

    def _tolist(ndarray):
        '''
        A recursive function which constructs lists from cellarrays
        (which are loaded as numpy ndarrays), recursing into the elements
        if they contain matobjects.
        '''
        elem_list = []
        for sub_elem in ndarray:
            if isinstance(sub_elem, spio.matlab.mio5_params.mat_struct):
                elem_list.append(_todict(sub_elem))
            elif isinstance(sub_elem, np.ndarray):
                elem_list.append(_tolist(sub_elem))
            else:
                elem_list.append(sub_elem)
        return elem_list
    data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True)
    return _check_keys(data)

【讨论】:

出色的工作。如果可以将其合并到 scipy 中,那就太好了。 这段代码将带有包含双精度数组的字段的 Matlab 结构转换为带有双精度列表列表的 python dict,这可能是作者的意图,但可能不是大多数人想要的。更好的返回值是以 ndarray 作为值的 dict。 我建议使用 improved version 在将 ndarray 转换为列表之前测试结构的数组内容。【参考方案4】:

我在 scipy 邮件列表 (https://mail.python.org/pipermail/scipy-user/) 上被告知还有另外两种方法可以访问这些数据。

这行得通:

import scipy.io as spio
vig=spio.loadmat('xy.mat')
print vig['b'][0, 0]['c'][0, 0]['d'][0, 0]

我的机器上的输出: 3

这种访问的原因:“由于历史原因,在 Matlab 中,所有内容至少是二维数组,甚至是标量。” 所以 scipy.io.loadmat 默认模仿 Matlab 行为。

【讨论】:

我自己盲目地偶然发现了 [0,0] 的东西,我不知道它为什么在那里,但是我在逻辑上没有用级联的 [0,0] 扩​​展它,所以很困惑.很高兴我找到了这个页面。【参考方案5】:

另一种可行的方法:

import scipy.io as spio
vig=spio.loadmat('xy.mat',squeeze_me=True)
print vig['b']['c'].item()['d']

输出:

3

我也在 scipy 邮件列表中学到了这个方法。我当然不明白(还)为什么必须添加“.item()”,并且:

print vig['b']['c']['d']

会抛出一个错误:

IndexError:只有整数、切片 (:)、省略号 (...)、numpy.newaxis (None) 和整数或布尔数组是有效的索引

但我知道后会回来补充解释。 numpy.ndarray.item 的解释(来自 numpy 参考): 将数组的元素复制到标准 Python 标量并返回。

(请注意,这个答案与 hpaulj 对最初问题的评论基本相同,但我觉得评论不够“可见”或无法理解。我在搜索解决方案时当然没有注意到它第一次,几周前)。

【讨论】:

打印 vig['b']['c']['d'] 不起作用的原因: vig['b']['c'] 返回一个 numpy.void 对象,因此如果您尝试直接访问其中的项目,python 会引发错误。 item() 方法返回缓冲区对象 (numpy.org/doc/stable/reference/generated/…),然后您可以访问其内容。

以上是关于scipy.io.loadmat 嵌套结构(即字典)的主要内容,如果未能解决你的问题,请参考以下文章

使用 scipy.io loadmat 将 Matlab 结构导入 python 时的值错误

Scipy IO Loadmat 错误:ValueError:Mat 4 mopt 格式错误

使用 scipy.io.loadmat 在 python 中加载 matlab 表

从使用 Scipy.io.loadmat 加载的 .mat 文件访问数组内容 - python

SciPy中两个模块:io 和misc

如何使用 Scipy.io.loadmat 将 Matlab mat 文件中的字符串单元格数组加载到 Python 列表或元组中