This repository was archived by the owner on Sep 27, 2024. It is now read-only.
forked from hadizand/DL_CS_ECG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_run.py
338 lines (261 loc) · 10.7 KB
/
main_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
Compressed Sensing of ECG - Kronecker Technicque - Dictionary Learning
"""
import datetime
import csv
import scipy.io
import numpy as np
import matplotlib.pyplot as plt
import sparseDictionaries as spardic
import measurementMatrix as mesmat
import evaluation as evaluat
from SL0 import SL0
from MOD import MOD
from KSVD import KSVD
# avoid plot blocking
#plt.ion()
# initialize Signal to Noise Ratios
KSVD_SNR = 0
MOD_SNR = 0
DCT_SNR = 0
KSVD_KRON_SNR = 0
MOD_KRON_SNR = 0
DCT_KRON_SNR = 0
# Load the data
data0 = scipy.io.loadmat('100m.mat') # contains val: [2x650000 double]
# data0.val(1,:) are MLII ECG data
# data0.val(2,:) are V5 ECG data
# Train and Test data
MULT = 256 # length of train signal, default 128
TRAIN_NUM = 400 # number of TRAIN signals, default 400
TEST_NUM = 25 # number of TEST signals, default 25
TRAIN_LEN = TRAIN_NUM*MULT
TEST_LEN = TEST_NUM*MULT
START_TRAIN = 0
END_TRAIN = TRAIN_LEN
START_TEST = END_TRAIN
END_TEST = START_TEST + TEST_LEN
trainSet = data0['val'][0][START_TRAIN:END_TRAIN]
testSet = data0['val'][0][START_TEST:END_TEST]
plt.plot(testSet)
plt.title('Test Set')
plt.show()
## Testing Phase Parameters (DO NOT CONFUSE WITH TRAINING PARAMETERS)
x = testSet
if len(x) != TEST_LEN:
print('ERROR: x has not the correct length, check inital parameters')
exit()
N = 16 # Length of each block
CR = 1/4 # Compression ratio
M = int(N * CR) # Length of compressed measurement
SIGNAL_BLOCKS = len(x) // N
KRON_FACT = 8 # kronecker factor
N_KRON = KRON_FACT * N # kron block length
M_KRON = int(N_KRON * CR) # kron compressed meas. length
SIGNAL_BLOCKS_KRON = len(x) // N_KRON
REPEAT = 1 # number of times to REPEAT the experiment
for rep in range(REPEAT):
## --------------------------------
## Adaptive Dictionary Learning
## --------------------------------
## NON KRONECKER
## --------------------------------
T_LEN = N # Length of each training sample
T_NUM = TRAIN_LEN // T_LEN # Number of training samples
trainMat = trainSet.reshape(T_NUM, T_LEN)
REDUND = 2 # 1 for no redundancy
print(trainMat.T.shape)
param = {
'K': REDUND * trainMat.T.shape[0], # Number of dictionary elements (atoms)
'L': 1, # Number of non-zero coefficients to use in OMP
'num_iterations': 10, # Number of iterations for dictionary learning
'preserve_dc_atom': 0, # Not used in the MOD function but included in original parameters
'initialization_method': 'DataElements', # 'DataElements' or 'GivenMatrix'
'initial_dictionary': None # To be initialized if 'GivenMatrix' is set
}
# Initialize initial_dictionary (only used if 'GivenMatrix' is set)
initial_dictionary = np.random.rand(trainMat.T.shape[0], param['K'])
for i in range(param['K']):
initial_dictionary[:, i] /= np.linalg.norm(initial_dictionary[:, i]) # Normalize each column
param['initial_dictionary'] = initial_dictionary
# non-kron dicts
mod_dict, output = MOD(trainMat.T, param)
ksvd_dict, X = KSVD(trainMat.T, param)
print('mod')
spardic.check_matrix_properties(mod_dict)
print('ksvd')
spardic.check_matrix_properties(ksvd_dict)
## KRONECKER
## --------------------------------
T_LEN = N_KRON # Length of each sample
T_NUM = TRAIN_LEN // T_LEN # Number of training samples
trainMat = trainSet.reshape(T_NUM, T_LEN)
REDUND = 2 # 1 for no redundancy
print(trainMat.T.shape)
param = {
'K': REDUND * trainMat.T.shape[0], # Number of dictionary elements (atoms)
'L': 1, # Number of non-zero coefficients to use in OMP
'num_iterations': 10, # Number of iterations for dictionary learning
'preserve_dc_atom': 0, # Not used in the MOD function but included in original parameters
'initialization_method': 'DataElements', # Method for dictionary initialization ('DataElements' or 'GivenMatrix')
'initial_dictionary': None # To be initialized if 'GivenMatrix' is set
}
# Initialize initial_dictionary (only used if 'GivenMatrix' is set)
initial_dictionary = np.random.rand(trainMat.T.shape[0], param['K'])
for i in range(param['K']):
initial_dictionary[:, i] /= np.linalg.norm(initial_dictionary[:, i]) # Normalize each column
param['initial_dictionary'] = initial_dictionary
# kron
mod_dict_kron, output = MOD(trainMat.T, param)
ksvd_dict_kron, X = KSVD(trainMat.T, param)
print('Kron mod')
spardic.check_matrix_properties(mod_dict_kron)
print('Kron svd')
spardic.check_matrix_properties(ksvd_dict_kron)
## Fixed dct dictionary (non-adaptive)
## --------------------------------
dct_dict = spardic.dct_dictionary(N)
dct_dict_kron = spardic.dct_dictionary(N_KRON)
print('dct')
spardic.check_matrix_properties(dct_dict)
print('kron dct')
spardic.check_matrix_properties(dct_dict_kron)
## --------------------------------
##TESTING
## --------------------------------
## Create measurement matrix
## --------------------------------
PHI_STRING = None
# deterministic DBBD
Phi = mesmat.generate_DBBD_matrix(M, N)
PHI_STRING = 'DBBD'
# randomic measurement
#if PHI_STRING == 'DBBD':
# raise ValueError('DBBD is already active')
#PHI_STRING = 'scaled_binary' # need for print at the end: 'scaled_binary', 'binary', 'gaussian'
#Phi = mesmat.generate_random_matrix(M, N, PHI_STRING)
## Create kron measurement matrix
## --------------------------------
Phi_kron = np.kron(np.eye(KRON_FACT), Phi)
## Theta matrix
## --------------------------------
mod_theta = Phi @ mod_dict
ksvd_theta = Phi @ ksvd_dict
dct_theta = Phi @ dct_dict
## Theta kron matrix
## --------------------------------
mod_theta_kron = Phi_kron @ mod_dict_kron
ksvd_theta_kron = Phi_kron @ ksvd_dict_kron
dct_theta_kron = Phi_kron @ dct_dict_kron
# SL0 parameters
sigma_off = 0.001
mod_theta_pinv = np.linalg.pinv(mod_theta)
ksvd_theta_pinv = np.linalg.pinv(ksvd_theta)
dct_theta_pinv = np.linalg.pinv(dct_theta)
mod_theta_kron_pinv = np.linalg.pinv(mod_theta_kron)
ksvd_theta_kron_pinv = np.linalg.pinv(ksvd_theta_kron)
dct_theta_kron_pinv = np.linalg.pinv(dct_theta_kron)
mu_SL0 = 2
L_SL0 = 3
sig_dec_fact = 0.5
if sigma_off > 0:
sigma_min = sigma_off * 4
else:
sigma_min = 0.00001
# Algorithm
# Initialize the sparse code
mod_x = np.zeros(len(x))
ksvd_x = np.zeros(len(x))
dct_x = np.zeros(len(x))
mod_kron_x = np.zeros(len(x))
ksvd_kron_x = np.zeros(len(x))
dct_kron_x = np.zeros(len(x))
# Sampling Phase
Y = np.zeros((M, SIGNAL_BLOCKS))
for i in range(len(x) // N):
Y[:,i] = Phi @ x[i*N:(i+1)*N]
# non-kron recovery
for i in range(SIGNAL_BLOCKS):
y = Y[:,i]
# SL0: Sparse reconstruction
mod_xp = SL0(y, mod_theta, sigma_min, sig_dec_fact,
mu_SL0, L_SL0, mod_theta_pinv, showProgress=False)
ksvd_xp = SL0(y, ksvd_theta, sigma_min, sig_dec_fact,
mu_SL0, L_SL0, ksvd_theta_pinv, showProgress=False)
dct_xp = SL0(y, dct_theta, sigma_min, sig_dec_fact,
mu_SL0, L_SL0, dct_theta_pinv, showProgress=False)
# Recovery Phase
mod_x[i*N:(i+1)*N] = mod_dict @ mod_xp
ksvd_x[i*N:(i+1)*N] = ksvd_dict @ ksvd_xp
dct_x[i*N:(i+1)*N] = dct_dict @ dct_xp
# kron recovery
for i in range(SIGNAL_BLOCKS_KRON):
y = Y[:, i*KRON_FACT : (i+1)*KRON_FACT].flatten(order='F')
# SL0
mod_kron_xp = SL0(y, mod_theta_kron, sigma_min, sig_dec_fact,
mu_SL0, L_SL0, mod_theta_kron_pinv, showProgress=False)
ksvd_kron_xp = SL0(y, ksvd_theta_kron, sigma_min, sig_dec_fact,
mu_SL0, L_SL0, ksvd_theta_kron_pinv, showProgress=False)
dct_kron_xp = SL0(y, dct_theta_kron, sigma_min, sig_dec_fact,
mu_SL0, L_SL0, dct_theta_kron_pinv, showProgress=False)
# Recovery Phase
mod_kron_x[i*N_KRON:(i+1)*N_KRON] = mod_dict_kron @ mod_kron_xp
ksvd_kron_x[i*N_KRON:(i+1)*N_KRON] = ksvd_dict_kron @ ksvd_kron_xp
dct_kron_x[i*N_KRON:(i+1)*N_KRON] = dct_dict_kron @ dct_kron_xp
# Evaluation (in rep loop, NOT in i loop)
MOD_SNR += evaluat.calculate_snr(testSet, mod_x)
KSVD_SNR += evaluat.calculate_snr(testSet, ksvd_x)
DCT_SNR += evaluat.calculate_snr(testSet, dct_x)
MOD_KRON_SNR += evaluat.calculate_snr(testSet, mod_kron_x)
KSVD_KRON_SNR += evaluat.calculate_snr(testSet, ksvd_kron_x)
DCT_KRON_SNR += evaluat.calculate_snr(testSet, dct_kron_x)
# Average the SNRs
MOD_SNR /= REPEAT
KSVD_SNR /= REPEAT
DCT_SNR /= REPEAT
MOD_KRON_SNR /= REPEAT
KSVD_KRON_SNR /= REPEAT
DCT_KRON_SNR /= REPEAT
# Print the average over e of the snr
print(f'Average SNR over {REPEAT} repetitions:')
print(f'MOD SNR: {MOD_SNR}')
print(f'MOD KRON SNR: {MOD_KRON_SNR}')
print(f'KSVD SNR: {KSVD_SNR}')
print(f'KSVD KRON SNR: {KSVD_KRON_SNR}')
print(f'DCT SNR: {DCT_SNR}')
print(f'DCT KRON SNR: {DCT_KRON_SNR}')
# CSV to store old results
with open('results.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([PHI_STRING, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), REPEAT, MOD_SNR, MOD_KRON_SNR, KSVD_SNR, KSVD_KRON_SNR, DCT_SNR, DCT_KRON_SNR])
# plot
evaluat.plot_signals(reconstructed_signal=mod_x, original_signal=testSet,
snr=MOD_SNR,
reconstructed_name='mod_x',
original_name='testSet'
)
evaluat.plot_signals(reconstructed_signal=mod_kron_x, original_signal=testSet,
snr=MOD_KRON_SNR,
reconstructed_name='mod_kron_x',
original_name='testSet'
)
evaluat.plot_signals(reconstructed_signal=ksvd_x, original_signal=testSet,
snr=KSVD_SNR,
reconstructed_name='ksvd_x',
original_name='testSet'
)
evaluat.plot_signals(reconstructed_signal=ksvd_kron_x, original_signal=testSet,
snr=KSVD_KRON_SNR,
reconstructed_name='ksvd_kron_x',
original_name='testSet'
)
evaluat.plot_signals(reconstructed_signal=dct_x, original_signal=testSet,
snr=DCT_SNR,
reconstructed_name='dct_x',
original_name='testSet'
)
evaluat.plot_signals(reconstructed_signal=dct_kron_x, original_signal=testSet,
snr=DCT_KRON_SNR,
reconstructed_name='dct_kron_x',
original_name='testSet'
)