-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final Code.m
405 lines (347 loc) · 9.26 KB
/
Final Code.m
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
clc;
clear all;
close all;
%Written by Alinstein Jose, University of Victoria.
%last modified: April 02, 2019.
%Nopatient: Number of patients
%Nseg: Total number of training and testing segments for patients
%ntr: Number of train segments for patients
%nte: Number of test segments for patients
%K: Size of dictonary is d by K
%st1,st2,st3: Initial random states.
%d: Length of window used to create a local segment
% gp: number of samples a local segment differs from the next local segment.
Nopatient=5;
ntr=10;
nte=10;
d=160;
K=2000;
st1=9;
st2=17;
st3=30;
d=160;
gap=1;
% File ind_mit2019 stores the name of all patients files
Nseg = ntr + nte;
Dtr = [];
Dte = [];
load ind_mit2019
InG = ind_mit2019;
L = length(InG);
rand('state',st1)
Lr = randperm(L);
Lr = Lr(1:Nopatient);
store=[];
%Required data patients are extracted from the file names from index
for i = 1:Nopatient
ni = Lr(i);
fname = sprintf('a%.0f.mat',InG(ni));
si = load(fname);
ai = struct2cell(si);
di = cell2mat(ai);
di=di(1:500000);
store(:,i)= di;
end
%Following loop takes Nseg number of segments(for both testing and-
%training) for all the patients
for j=1:Nopatient
Li = length(store(:,j));
rand('state',st2+j-1)
r = randperm(Li);
for i=1:Nseg
rq=r(i);
segtemp(i,:)=store(rq:rq+999,j)';
end
Trsegments(j,:,:)=segtemp(1:ntr,:);
Tesegments(j,:,:)=segtemp(ntr+1:Nseg,:);
end
%p stores numbers of windows applied to each segment
p = 1 + floor((1000 - d)/gap);
Ktr = size(Trsegments,2);
%This loop applies window to training segments to obtain local segments
for k=1:Nopatient
for j=1:ntr
for i=1:p
temp=Trsegments(k,j,(i-1)*gap+1:(i-1)*gap+d);
temp=reshape(temp,d,1);
temp=temp/norm(temp);
TrainLseg(k,(j-1)*p+i,:)=temp;
TrainLseg4d(k,j,i,:)=temp;
end
end
end
%This loop applies window to testing segments to obtain local segments
for k=1:Nopatient
for j=1:nte
for i=1:p
temp=Tesegments(k,j,(i-1)*gap+1:(i-1)*gap+d);
temp=reshape(temp,d,1);
temp=temp/norm(temp);
TestLseg(k,(j-1)*p+i,:)=temp;
TestLseg4d(k,j,i,:)=temp;
end
end
end
TestLseg4d=permute(TestLseg4d,[4 3 2 1]);
TrainLseg4d=permute(TrainLseg4d,[4 3 2 1]);
%create Random dictionary with dimension d and K
randn('state',st3)
D=randn(d,K);
D=orth(D')';
%lamda is chosen as 0.25/sqrt(d)
lamda=0.25/sqrt(d);
Btr = [];
tic
%This block calculates the sparse vector for each local segment using cvx
%for training set
Btr = [];
for i = 1:Nopatient
for j = 1:ntr
t = (i-1)*ntr*p+(j-1)*p;
Atr = [];
for k = 1:p
xk = Xtr(:,t+k);
cvx_begin quiet
variable a(K,1)
minimize(0.5*norm(D*a-xk)+lam*norm(a,1))
cvx_end
Atr = [Atr a];
current_state_tr = [i j k]
end
b = max(abs(Atr)')';
Btr = [Btr b];
end
end
%Following calculates absolute value of each sparse vector
for k=1:Nopatient
for j=1:ntr
for i=1:p
mspar(:,i,j,k)=abs(spar(:,i,j,k));
end
end
end
%This block calculates the max pooling, which takes the largest
%coefficient from all columns
for k=1:Nopatient
for j=1:ntr
beta(:,j,k)=max(mspar(:,:,j,k),[],2);
end
end
Bte = [];
%This block calculates the sparse vector for each local segment using cvx
%for testing set
tic
for k=1:Nopatient
for j = 1:nte%Nseg
Ate = [];
for i=1:p
cvx_begin quiet
variable alp(K,1)
minimize ( (0.5*norm(TestLseg4d(:,i,j,k)-D*alp)+lamda*norm(alp,1)) )
cvx_end
spartes(:,i,j,k)=alp;
Ate = [Ate alp];
current_state_tr = [i j k];
end
b = max(abs(Ate)')';
Bte = [Bte b];
end
end
%end
%Following calculates absolute value of each sparse vector
toc
for k=1:Nopatient
for j=1:nte
for i=1:size(TestLseg4d,2)
mspartes(:,i,j,k)=abs(spartes(:,i,j,k));
end
end
end
%This block calculates the max pooling, which takes the largest
%coefficient from all columns
for k=1:Nopatient
for j=1:nte
betates(:,j,k)=max(mspartes(:,:,j,k),[],2);
end
end
effciency_of_using_cvx_is = [ checkeffciency(Btr,Bte,ntr,nte)]
tic
%This block calculates the sparse vector for each local segment using
%proximal method for training set
Btr = [];
for k=1:Nopatient
for j = 1:ntr
Atr = [];
for i=1:p
theta2=zeros(2000,1);
for kq=1:1000
z1=(theta2+D'*(TrainLseg4d(:,i,j,k)-D*theta2));
theta2=sign(z1).*max((abs(z1)-lamda/0.9),0);
end
alp=theta2;
spar(:,i,j,k)=alp;
current_state_tr = [i j k]
Atr = [Atr alp];
end
b = max(abs(Atr)')';
Btr = [Btr b];
end
end
%Following calculates absolute value of each sparse vector
for k=1:Nopatient
for j=1:ntr
for i=1:p
mspar(:,i,j,k)=abs(spar(:,i,j,k));
end
end
end
%This block calculates the max pooling, which takes the largest
%coefficient from all columns
for k=1:Nopatient
for j=1:ntr
beta(:,j,k)=max(mspar(:,:,j,k),[],2);
end
end
Bte = [];
%This block calculates the sparse vector for each local segment using
%proximal method for testing set
for k=1:Nopatient
for j = 1:nte%Nseg
Ate = [];
for i=1:p
theta2=zeros(2000,1);
for kq=1:1000
z1=(theta2+D'*(TestLseg4d(:,i,j,k)-D*theta2));
theta2=sign(z1).*max((abs(z1)-lamda/.9),0);
end
alp=theta2;
spartes(:,i,j,k)=alp;
Ate = [Ate alp];
current_state_te = [i j k ]
end
b = max(abs(Ate)')';
Bte = [Bte b];
end
end
%Following calculates absolute value of each sparse vector
for k=1:Nopatient
for j=1:nte
for i=1:size(TestLseg4d,2)
mspartes(:,i,j,k)=abs(spartes(:,i,j,k));
end
end
end
%This block calculates the max pooling, which takes the largest
%coefficient from all columns
for k=1:Nopatient
for j=1:nte
betates(:,j,k)=max(mspartes(:,:,j,k),[],2);
end
end
effciency_of_using_proximal_method_is = [ checkeffciency(Btr,Bte,ntr,nte)]
toc
tic
%This block calculates the sparse vector for each local segment using
%ADMM for training set
Btr = [];
r4=0;
d4=0;
alppha=lamda;
Inv=(D'*D+alppha*eye(K));
v=inv(Inv);
for k=1:Nopatient
for j = 1:ntr
Atr = [];
for i=1:p
xk1(:,1)=zeros(2000,1);lamk(:,1)=zeros(2000,1);
yk1(:,1)=zeros(2000,1);
v1=D'*TrainLseg4d(:,i,j,k);
for f=1 :100
xk1(:,f+1)=v*(v1+alppha*yk1(:,f)-lamk(:,f));
kk1=xk1(:,f+1)+lamk(:,f)/alppha;
yk1(:,f+1)=sign(kk1).*max((abs(kk1)-lamda),0);
lamk(:,f+1)=lamk(:,f)+alppha*(xk1(:,f+1)-yk1(:,f+1));
r4(f)=norm(xk1(:,f+1)-yk1(:,f+1),2);
d4(f)=norm(alppha*(yk1(:,f+1)-yk1(:,f)),2);
if(r4(f)<1e-3)
if (d4(f)<1e-3)
break;
end
end
end
alp=xk1(:,f);
spar(:,i,j,k)=alp;
current_state_tr = [i j k f]
Atr = [Atr alp];
end
b = max(abs(Atr)')';
Btr = [Btr b];
end
end
%Following calculates absolute value of each sparse vector
for k=1:Nopatient
for j=1:ntr
for i=1:p
mspar(:,i,j,k)=abs(spar(:,i,j,k));
end
end
end
%This block calculates the max pooling, which takes the largest
%coefficient from all columns
for k=1:Nopatient
for j=1:ntr
beta(:,j,k)=max(mspar(:,:,j,k),[],2);
end
end
r4=0;
d4=0;
Bte = [];
%This block calculates the sparse vector for each local segment using
%ADMM for testing set
for k=1:Nopatient
for j = 1:nte%Nseg
Ate = [];
for i=1:p
xk1(:,1)=zeros(2000,1);lamk(:,1)=zeros(2000,1);
yk1(:,1)=zeros(2000,1);
v1=D'*TestLseg4d(:,i,j,k);
for f=1 :100
xk1(:,f+1)=v*(v1+alppha*yk1(:,f)-lamk(:,f));
kk1=xk1(:,f+1)+lamk(:,f)/alppha;
yk1(:,f+1)=sign(kk1).*max((abs(kk1)-lamda),0);
lamk(:,f+1)=lamk(:,f)+alppha*(xk1(:,f+1)-yk1(:,f+1));
r4(f)=norm(xk1(:,f+1)-yk1(:,f+1),2);
d4(f)=norm(alppha*(yk1(:,f+1)-yk1(:,f)),2);
if(r4(f)<1e-3)
if (d4(f)<1e-3)
break;
end
end
end
alp=xk1(:,f);
spartes(:,i,j,k)=alp;
Ate = [Ate alp];
current_state_te = [i j k]
end
b = max(abs(Ate)')';
Bte = [Bte b];
end
end
%end
%Following calculates absolute value of each sparse vector
for k=1:Nopatient
for j=1:nte
for i=1:size(TestLseg4d,2)
mspartes(:,i,j,k)=abs(spartes(:,i,j,k));
end
end
end
%This block calculates the max pooling, which takes the largest
%coefficient from all columns
for k=1:Nopatient
for j=1:nte
betates(:,j,k)=max(mspartes(:,:,j,k),[],2);
end
end
effciency_of_using_ADMM_is = [ checkeffciency(Btr,Bte,ntr,nte)]
toc