如何加载.mat文件夹和文件

问题描述 投票:1回答:1

我正在尝试将.mat数据集加载到我的数据框中。因此,我每次只能使用

从Folder 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个回答
0
投票

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

[我为您提供了读取文件夹中所有.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代码示例执行以下操作:

  • 使用mat获取文件夹中所有glob.glob('*.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"

更新:

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

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

  • 名为data的父字典。
    • datadata['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.matenter image description here

A0002.matenter image description here

© www.soinside.com 2019 - 2024. All rights reserved.