【问题标题】:How to Load .mat Folder and files如何加载 .mat 文件夹和文件
【发布时间】:2020-04-10 16:14:53
【问题描述】:

我正在尝试将 .mat 数据集加载到我的数据框中。所以,我一次只能从文件夹 TrainingSet1 中加载一个文件

 os.chdir('/Users/Ashi/Downloads/TrainingSet2')
 data = loadmat('A2001.mat') 

我可以看到其中的数据,但我应该如何加载整个 TrainingSet1 文件夹,以便我可以查看整个内容。 另外,如何将 .mat 文件视为图像?

这是我的代码,

%reload_ext autoreload
   %autoreload 2
   %matplotlib inline 

   from fastai.vision import *
   from fastai.metrics import error_rate
   from mat4py import loadmat
   from pylab import*
   import matplotlib
   import os

   os.chdir('/Users/Ashi/Downloads/TrainingSet2')

   data = loadmat('A2001.mat')
   data
   {'ECG': {'sex': 'Male', 'age': 68,
     'data': [[0.009784321006571624,
    0.006006033870606647,
   ...This is roughly how the data looks like   

   imshow('A2001.mat',[])  
   ---------------------------------------------------------------            
   TypeError      Traceback (most recent call last)
   <ipython-input-52-23bbdf3a7668> in <module>
   ----> 1 imshow('A2001.mat',[])...A long error is displayed
   TypeError: unhashable type: 'list'

感谢您的帮助

【问题讨论】:

    标签: python performance opencv mat


    【解决方案1】:

    很难从您的帖子中看出什么是输入格式,以及您想要的输出格式是什么。

    我给你一个读取文件夹中所有.mat文件的例子,以及一个如何将data['data']显示为图像的例子。

    我希望这个例子足以让你自己继续前进。

    我使用 MATLAB 创建了一个示例数据集 'A2001.mat''A2002.mat''A2003.mat'
    如果您安装了 MATLAB,我建议您执行以下代码来创建示例输入(以使 Python 示例可重现):

    ECG.sex = 'Male';
    ECG.age = 68;
    data = im2double(imread('cameraman.tif')) / 10; % Divide by 10 for simulating range [0, 0.1] instead of [0, 1]   
    save('A2001.mat', 'ECG', 'data');
    
    ECG.sex = 'Male';
    ECG.age = 46;
    data = im2double(imread('cell.tif'));
    save('A2002.mat', 'ECG', 'data');
    
    ECG.sex = 'Female';
    ECG.age = 54;
    data = im2double(imread('tire.tif'));
    save('A2003.mat', 'ECG', 'data');
    

    Python 代码示例执行以下操作:

    • 使用glob.glob('*.mat') 获取文件夹中所有mat 文件的列表。
    • 迭代mat 文件,从文件中加载数据,并将数据附加到列表中。
      循环的结果是一个名为alldata 的列表,其中包含来自所有mat 文件的数据。
    • 迭代 alldata 并将 data['data'] 显示为图像。
      (假设data['data'] 是您要显示为图像的矩阵)。

    代码如下:

    from matplotlib import pyplot as plt
    from mat4py import loadmat
    import glob
    import os
    
    os.chdir('/Users/Ashi/Downloads/TrainingSet2')
    
    # Get a list for .mat files in current folder
    mat_files = glob.glob('*.mat')
    
    # List for stroring all the data
    alldata = []
    
    # Iterate mat files
    for fname in mat_files:
        # Load mat file data into data.
        data = loadmat(fname)
    
        # Append data to the list
        alldata.append(data)
    
    
    # Iterate alldata elelemts, and show images
    for data in alldata:
        # Assume image is stored in matrix named data in MATLAB.
        # data['data'], access data with string 'data', becuase data is a dictionary
        img = data['data']
    
        # Show data as image using matplotlib
        plt.imshow(img, cmap='gray')
        plt.show(block=True) # Show image with "blocking"
    

    更新:

    心电图数据不是图像,而是 12 个数据样本的列表。

    数据的内部结构(data = loadmat(fname)之后)是:

    • 父字典名为data
      • data 包含 data['ECG'] 中的字典。
        • data['ECG']['data'] 是 12 个列表的列表。

    以下代码迭代mat文件并将ECG数据显示为图形:

    from matplotlib import pyplot as plt
    from mat4py import loadmat
    import glob
    import os
    import numpy as np
    
    os.chdir('/Users/Ashi/Downloads/TrainingSet2')
    
    # Get a list for .mat files in current folder
    mat_files = glob.glob('*.mat')
    
    # List for stroring all the data
    alldata = []
    
    # Iterate mat files
    for fname in mat_files:
        # Load mat file data into data.
        data = loadmat(fname)
    
        # Append data to the list
        alldata.append(data)
    
    
    # Iterate alldata elelemts, and show images
    for data in alldata:
        # The internal structure of the data is a dictionary with a dictionary.
        ecg = data['ECG']
        data = ecg['data'] # Data is a list of lists
    
        # Convert data to NumPy array
        ecg_data = np.array(data)
    
        # Show data as image using matplotlib
        #plt.imshow(img, cmap='gray')
        plt.plot(ecg_data.T)  # Plot the data as graph.
        plt.show(block=True)  # Show image with "blocking"
    

    结果:

    A0001.mat:

    A0002.mat:


    带标签的图表:

    # Iterate alldata elements, and show images
    for data in alldata:
        # The internal structure of the data is a dictionary with a dictionary.
        ecg = data['ECG']
        data = ecg['data'] # Data is a list of lists
    
        # Convert data to NumPy array
        #ecg_data = np.array(data)
    
        # Show data as graph using matplotlib
        # Iterate data list:
        for i in range(len(data)):
            # Plot the data as graph.
            # Set labels d0, d1, d2...
            plt.plot(data[i], label='d'+str(i))
    
        plt.legend()  # Add legend
        plt.show(block=True)  # Show image with "blocking"
    

    结果:

    【讨论】:

    • 嘿@Rotem,那没用。我既没有任何错误,也没有图像。
    • 没有数据我无法重现问题。你能以某种方式发布A2001.matA2002.mat 吗?
    • 嘿@Rotem,你是在问这两个mat文件中的数据吗?那我应该打印出来吗?
    • 我要文件。除非您可以发布用于创建文件的代码(我假设为 MATLAB 代码)。
    • 嘿@Rotem。文件太大了,比如 2gb。我可以向您发送这些文件中数据的外观图像。除此之外,我的代码 [stackoverflow.com/questions/61111371/… 也有另一个问题。请帮我解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2013-03-22
    相关资源
    最近更新 更多