将Matlab文件加载到python中时出现关键错误

时间:2019-04-29 03:07:19

标签: python matlab scipy

所以我有32个Matlab文件,每个文件大约1.2 GB。我以相同的方式加载它们(如下所示)。我从名为“ data_mat”的行中获取了所有值,但是它起作用了,但是当我遇到第27个人时,它会错误地给出一个关键错误并说“ data_mat”

KeyError:“ data_mat” 但是我知道'data_mat'存在,因为我已经检查过了,有时第27个人将没有任何问题运行,其他时候它给我这个错误。对于这个人来说,它出错的次数比没有出错的次数多,但是似乎在27到32之后的每个文件都发生同样的错误。有时它会在哪里工作,有时会给我这个错误。

下面是一些如何加载这些文件并在主类中运行它们的示例。

class Channels():
def __init__(self):
    self = self

def all(self, i, c):

    if i == 1:
        # Value error: need to double-check correct columns labeling for the classification of each sleep/dream wake cycle
        a = hdf5storage.loadmat("Sub1.mat")
        b = a['data_mat']
        person1 = pd.DataFrame(b[c])
        person1.columns = ["Dream + Recall(NREM)","Dream + Recall(NREM)", "No Experience(NREM)", "No Experience(NREM)", "No Experience(NREM)", "Dream + Recall(NREM)", "Dream + Recall(REM)","Dream + Recall(NREM)", "Dream + Recall(NREM)"]
        #person1.columns = ["Dream + Recall(NREM)","Dream + Recall(NREM)", "No Experience(NREM)", "No Experience(NREM)", "No Experience(NREM)", "Dream + Recall(NREM)", "Dream + Recall(REM)","Dream + Recall(NREM)", "Dream + Recall(NREM)", "Dream + Recall(REM)"]
        return person1

    elif i == 2:
        a = spio.loadmat("Sub2.mat")
        b = a['data_mat']
        person2 = pd.DataFrame(b[c])
        person2.columns = ["Dream - Recall(NREM)", "Dream - Recall(NREM)", "Dream - Recall(NREM)", "No Experience(NREM)", "Dream + Recall(NREM)", "No Experience(NREM)", "Dream - Recall(NREM)", "Dream - Recall(NREM)", "Dream + Recall(REM)", "Dream - Recall(NREM)", "Dream + Recall(NREM)", "Dream + Recall(NREM)"]
        return person2

    elif i == 27:
        #v7.3 HDF
        a = hdf5storage.loadmat("Sub27.mat")
        b = a['data_mat']
        person27= pd.DataFrame(b[c])
        person27.columns = ["Dream - Recall(NREM)","Dream + Recall(NREM)","Dream - Recall(NREM)","Dream + Recall(NREM)","Dream - Recall(NREM)","Dream + Recall(NREM)","Dream + Recall(NREM)","Dream + Recall(NREM)","Dream + Recall(REM)","Dream + Recall(NREM)","Dream + Recall(NREM)", "Dream - Recall(REM)", "Dream + Recall(NREM)","Dream + Recall(REM)"]
        return person27 

main():

person = Channels().all(27, 0)

如果可能的话,我不想更改文件的加载方式,因为我知道它们适用于大多数人。但是,我该如何解决这个问题,以便有时无法得到这个关键错误,并且所有错误都可以顺利运行?

附加说明:我从不修改文件,我只是从文件中提取数据,加载时a是dict类型,b是numpy类型数组,person是熊猫数据框。

请帮助,我将非常感谢。

0 个答案:

没有答案