In [127]:
'''''''''''''This code shows how to import .mat file (MATLAB format) into dictionary using scipy.io'''''''''''''
'Quotation test'
# '''''''''''''''''''''''''''''''''''''''''''''''''''''''test'''''''''''''''''''''''''''''''''''''''''''''''''''''''
# """"""""""""""""""""""""""""""""""""""""""""""""""""somthing""""""""""""""""""""""""""""""""""""""""""""""""""""
# ''''''''''''''''''''''''''''''''''''''''First we will import the scipy.io''''''''''''''''''''''''''''''''''''''''
import scipy.io
# load .mat file into dictionary x
# x = scipy.io.loadmat('/home/bot/Dropbox/course_work/python_study/fMRI_basic/easymatfile.mat')
x = scipy.io.loadmat('/home/arasdar/data/Training_data/DATA_01_TYPE01.mat')
x.keys(), x.values()
x_sig = x['sig']
x_sig_ = np.array(x_sig)
x_sig_.shape, x_sig.size/6
import matplotlib.pyplot as plt
plt.plot(x_sig[0, :1000], label='ECG')
plt.plot(x_sig[1, :1000], label='PPG-1')
plt.plot(x_sig[2, :1000], label='PPG-2')
# plt.plot(x_sig[3, :1000], label='ACC-X')
# plt.plot(x_sig[4, :1000], label='SCC-Y')
# plt.plot(x_sig[5, :1000], label='ACC-Z')
plt.legend()
plt.show()
In [132]:
# Reading the 2nd file in training set with similar name which is prolly the labels
y = scipy.io.loadmat('/home/arasdar/data/Training_data/DATA_01_TYPE01_BPMtrace.mat')
y.keys(), y.values()
y_BPM0 = y['BPM0']
y_BPM0.size
y = np.array(y_BPM0)
y.shape, y_BPM0.size
plt.plot(y[:50, :], label='BPM0')
plt.show()
In [35]:
for iter in range(100):
# format codes for .format in python - keyword for Google
filename = '/home/arasdar/data/Training_data/DATA_{:02}_TYPE{}.mat'.format(iter, iter)
# print('Iter-{} training loss: {:.4f}'.format(iter, loss))
print(filename)
In [ ]:
# # Loading Matlab .mat data in Python
# # SEPTEMBER 06, 2014
# # A friend of mine just asked me for some tips with this.
# # I thought I would reply using a blog post so that it can be useful to other people too.
# # If you collect data with Matlab but want to work on it using Python (e.g. making nice graphs with matplotlib)
# # you can export a .mat file and then import that into Python using SciPy.
# # First let's save some example data in Matlab:
# # function savematlabdata
# # % save some data in a .mat
# a = [1, 2, 3; 4, 5, 6];
# S.b = [7, 8, 9; 10, 11, 12];
# M(1).c = [2, 4, 6; 8, 10, 12];
# M(2).c = [1, 3, 5; 7, 9, 11];
# save('data.mat','a','S','M')
# # return
# # Now we have a file "data.mat" which stores the array a, the structure S containing an array b, and an array of structures M where each of those contains an array c. Now we can load that data in Python with the scipy.io module and use the "print" function to prove it's there:
# # filename: loadmatlabdata.py
# # description : load in data from a .mat file
# # author: Alex Baldwin
# #==============================================
# import scipy.io as spio
# mat = spio.loadmat('data.mat', squeeze_me=True)
# a = mat['a'] # array
# S = mat['S'] # structure containing an array
# M = mat['M'] # array of structures
# print a[:,:]
# print S['b'][()][:,:] # structures need [()]
# print M[0]['c'][()][:,:]
# print M[1]['c'][()][:,:]
# '''This code shows how to import .mat file (MATLAB format) into dictionary using scipy.io'''
# # First we will import the scipy.io
# import scipy.io
# # load .mat file into dictionary x
# # x = scipy.io.loadmat('/home/bot/Dropbox/course_work/python_study/fMRI_basic/easymatfile.mat')
# # # easymatfile.mat contains 3 matlab variables
# # # a: [10x30]
# # # b: [20x100]
# # # c: [1x1]
# # # # in order to get a, b and c we say
# # # pyA = x['a']
# # # pyB = x['b']
# # # pyC = x['c']
# # # Now we want plot the figures
# # from pylab import *
# # from matplotlib import *
# # # matshow(pyA,1)
# # # matshow(pyB,2)
# # # matshow(pyC,3)
# # # matshow(x_sig, 1)
# # # Now, we will show all three figures
# # show()
In [ ]: