-
Notifications
You must be signed in to change notification settings - Fork 5
/
fdakma.m
579 lines (537 loc) · 21.9 KB
/
fdakma.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
classdef fdakma
%fdakma A class to provide a kmeans clustering and alignment
% -------------------------------------------------------------------------
% This class provides kmeans and alignment using the
% SRVF framework
%
% Usage: obj = fdakma(f,t)
%
% where:
% f: (M,N): matrix defining N functions of M samples
% time: time vector of length M
%
%
% fdakma Properties:
% f - original functions
% time - time
% fn - aligned functions in cell of clusters
% qn - aligned srvfs in cell of clusters
% gam - warping functions in cell of clusters
% gamI - inverse gamma
% q0 - original srvfs
% labels - cluster labels
% templates - cluster centers
% templates_q - cluster srvf centers
% qun - cost function
% method - optimization method
%
%
% fdakma Methods:
% fdakma - class constructor
% kmeans - perform kmeans
% plot - plot results and functions in object
%
%
% Author : J. D. Tucker (JDT) <jdtuck AT sandia.gov>
% Date : 15-Mar-2018
properties
f % original functions
time % time
fn % aligned functions in cell of clusters
qn % aligned srvfs in cell of clusters
gam % warping functions in cell of clusters
gamI % inverse gamma
q0 % original srvfs
labels % cluster labels
templates % cluster centers
templates_q % cluster srvf centers
qun % cost function
lambda % lambda
method % optimization method
end
methods
function obj = fdakma(f,time)
%fdakma Construct an instance of this class
% Input:
% f: (M,N): matrix defining N functions of M samples
% time: time vector of length M
% check dimension of time
a = size(time,1);
if a == 1
time = time';
end
obj.f = f;
obj.time = time;
end
function obj = kmeans(obj,K,seeds,lambda1, option)
% KMEANS K-Means clustering and alignment
% -------------------------------------------------------------------------
% This function clusters functions and aligns using the elastic square-root
% slope (srsf) framework.
%
% Usage: obj.kmeans(K)
% obj.kmeans(K,seeds)
% obj.kmeans(K,seeds,lambda1)
%
% Input:
% K: number of clusters
% seeds: indexes of cluster center functions (default [])
% lambda1: controls amount of alignment
%
% default options
% option.parallel = 0; % turns offs MATLAB parallel processing (need
% parallel processing toolbox)
% option.alignment = true; % performa alignment
% option.closepool = 0; % determines wether to close matlabpool
% option.smooth = 0; % smooth data using standard box filter
% option.sparam = 25; % number of times to run filter
% option.method = 'DP1'; % optimization method (DP, DP2, SIMUL, RBFGS)
% option.MaxItr = 20; % maximum iterations
% option.thresh = 0.01; % cost function threshold
%
% Output:
% fdakma object
if nargin < 2
seeds = [];
obj.lambda = 0;
option.parallel = 0;
option.alignment = true;
option.closepool = 0;
option.smooth = 0;
option.sparam = 25;
option.method = 'DP1';
option.MaxItr = 20;
option.thresh = 0.01;
elseif nargin < 3
obj.lambda = 0;
option.parallel = 0;
option.alignment = true;
option.closepool = 0;
option.smooth = 0;
option.sparam = 25;
option.method = 'DP1';
option.MaxItr = 20;
option.thresh = 0.01;
elseif nargin < 4
obj.lambda = lambda1;
option.parallel = 0;
option.alignment = true;
option.closepool = 0;
option.smooth = 0;
option.sparam = 25;
option.method = 'DP1';
option.MaxItr = 20;
option.thresh = 0.01;
else
obj.lambda = lambda1;
end
% time warping on a set of functions
if option.parallel == 1
if isempty(gcp('nocreate'))
% prompt user for number threads to use
nThreads = input('Enter number of threads to use: ');
if nThreads > 1
parpool(nThreads);
elseif nThreads > 12 % check if the maximum allowable number of threads is exceeded
while (nThreads > 12) % wait until user figures it out
fprintf('Maximum number of threads allowed is 12\n Enter a number between 1 and 12\n');
nThreads = input('Enter number of threads to use: ');
end
if nThreads > 1
parpool(nThreads);
end
end
end
end
%% Parameters
fprintf('\n lambda = %5.1f \n', obj.lambda);
[M, N] = size(obj.f);
if (isempty(seeds))
template_ind = randsample(N,K);
else
template_ind = seeds;
end
obj.templates = zeros(M,K);
for i=1:K
obj.templates(:,i) = obj.f(:,template_ind(i));
end
cluster_id = zeros(1,N);
obj.qun = zeros(0, option.MaxItr);
if option.smooth == 1
obj.f = smooth_data(obj.f, option.sparam);
end
%% Compute the q-function of the plot
q = f_to_srvf(obj.f,obj.time);
obj.q0 = q;
obj.templates_q = zeros(M,K);
for i=1:K
obj.templates_q(:,i) = q(:,template_ind(i));
end
%% Set initial using the original f space
fprintf('\nInitializing...\n');
f_temp = zeros(length(obj.time),N);
q_temp = zeros(length(obj.time),N);
for r = 1:option.MaxItr
gam1 = {};
Dy = zeros(K,N);
qn1 = {};
fn1 = {};
fprintf('updating step: r=%d\n', r);
if r == option.MaxItr
fprintf('maximal number of iterations is reached. \n');
end
for i=1:K
gamt = zeros(N,size(q,1));
if option.parallel == 1
parfor k = 1:N
q_c = q(:,k); mq_c = obj.templates_q(:,i);
if (option.alignment)
gamt(k,:) = optimum_reparam(mq_c,q_c,obj.time,obj.lambda,option.method, ...
obj.templates(1,i), obj.f(1,k));
else
gamt(k,:) = linspace(0,1,M);
end
f_temp(:,k) = warp_f_gamma(obj.f(:,k),gamt(k,:),obj.time);
q_temp(:,k) = f_to_srvf(f_temp(:,k),obj.time);
Dy(i,k) = sqrt(sum(trapz(obj.time,(q_temp(:,k)-obj.templates_q(:,i)).^2)));
end
else
for k = 1:N
q_c = q(:,k); mq_c = obj.templates_q(:,i);
if (option.alignment)
gamt(k,:) = optimum_reparam(mq_c,q_c,obj.time,obj.lambda,option.method, ...
obj.templates(1,i), obj.f(1,k));
else
gamt(k,:) = linspace(0,1,M);
end
f_temp(:,k) = warp_f_gamma(obj.f(:,k),gamt(k,:),obj.time);
q_temp(:,k) = f_to_srvf(f_temp(:,k),obj.time);
Dy(i,k) = sqrt(sum(trapz(obj.time,(q_temp(:,k)-obj.templates_q(:,i)).^2)));
end
end
gam1{i} = gamt;
qn1{i} = q_temp;
fn1{i} = f_temp;
end
[~,cluster_id] = min(Dy,[],1);
%% Normalization
for i=1:K
id = cluster_id == i;
ftmp = fn1{i}(:,id);
gamtmp = gam1{i}(id,:);
obj.gamI = SqrtMeanInverse(gamtmp);
N1 = size(ftmp,2);
f_temp1 = zeros(M,N1);
q_temp1 = zeros(M,N1);
gam2 = zeros(N1,M);
if option.parallel == 1
parfor k = 1:N1
f_temp1(:,k) = warp_f_gamma(ftmp(:,k),obj.gamI,obj.time);
q_temp1(:,k) = f_to_srvf(f_temp1(:,k),obj.time);
gam2(k,:) = warp_f_gamma(gamtmp(k,:),obj.gamI,obj.time);
end
else
for k = 1:N1
f_temp1(:,k) = warp_f_gamma(ftmp(:,k),obj.gamI,obj.time);
q_temp1(:,k) = f_to_srvf(f_temp1(:,k),obj.time);
gam2(k,:) = warp_f_gamma(gamtmp(k,:),obj.gamI,obj.time);
end
end
gam1{i}(id,:) = gam2;
qn1{i}(:,id) = q_temp1;
fn1{i}(:,id) = f_temp1;
end
%% Template Identification
qun_t = zeros(1,K);
for i = 1:K
id = cluster_id == i;
old_templates_q = obj.templates_q;
obj.templates_q(:,i) = mean(qn1{i}(:,id),2);
obj.templates(:,i) = mean(fn1{i}(:,id),2);
qun_t(i) = norm(obj.templates_q(:,i)-old_templates_q(:,i))/norm(old_templates_q(:,i));
end
obj.qun(r) = mean(qun_t);
if obj.qun(r) < option.thresh || r >= option.MaxItr
break;
end
end
%% Output
ftmp = {};
qtmp = {};
gamtmp = {};
for i = 1:K
id = cluster_id == i;
ftmp{i} = fn1{i}(:,id);
qtmp{i} = qn1{i}(:,id);
gamtmp{i} = gam1{i}(id,:);
end
if option.parallel == 1 && option.closepool == 1
if isempty(gcp('nocreate'))
delete(gcp('nocreate'))
end
end
obj.fn = ftmp;
obj.qn = qtmp;
obj.labels = cluster_id;
obj.method = option.method;
obj.qun = obj.qun(1:r);
end
function obj = DPmeans(obj,lambda1,option)
% DPmeans Dirichlet Process K-Means clustering and alignment
% -------------------------------------------------------------------------
% This function clusters functions and aligns using the elastic square-root
% slope (srsf) framework using Dirchelet Process Mixture Model
%
% Usage: obj.kmeans(lambda)
% obj.kmeans(lambda,option)
%
% Input:
% lambda1 : cluster penalty parameter
%
% default options
% option.parallel = 0; % turns offs MATLAB parallel processing (need
% parallel processing toolbox)
% option.alignment = true; % performa alignment
% option.closepool = 0; % determines wether to close matlabpool
% option.smooth = 0; % smooth data using standard box filter
% option.sparam = 25; % number of times to run filter
% option.method = 'DP1'; % optimization method (DP, SIMUL, RBFGS)
% option.MaxItr = 20; % maximum iterations
% option.thresh = 0.01; % cost function threshold
%
% Output:
% fdakma object
if nargin < 2
obj.lambda = NaN;
option.parallel = 0;
option.alignment = true;
option.closepool = 0;
option.smooth = 0;
option.sparam = 25;
option.method = 'DP1';
option.MaxItr = 20;
option.thresh = 0.01;
elseif nargin < 3
obj.lambda = lambda1;
option.parallel = 0;
option.alignment = true;
option.closepool = 0;
option.smooth = 0;
option.sparam = 25;
option.method = 'DP1';
option.MaxItr = 20;
option.thresh = 0.01;
end
% time warping on a set of functions
if option.parallel == 1
if isempty(gcp('nocreate'))
% prompt user for number threads to use
nThreads = input('Enter number of threads to use: ');
if nThreads > 1
parpool(nThreads);
elseif nThreads > 12 % check if the maximum allowable number of threads is exceeded
while (nThreads > 12) % wait until user figures it out
fprintf('Maximum number of threads allowed is 12\n Enter a number between 1 and 12\n');
nThreads = input('Enter number of threads to use: ');
end
if nThreads > 1
parpool(nThreads);
end
end
end
end
%% Parameters
[M, N] = size(obj.f);
cluster_id = ones(1,N);
obj.qun = zeros(0, option.MaxItr);
%% Compute the q-function of the plot
q = f_to_srvf(obj.f,obj.time);
obj.q0 = q;
if option.smooth == 1
obj.f = smooth_data(obj.f, option.sparam);
end
mnq = mean(q,2);
dqq = sqrt(sum((q - mnq*ones(1,N)).^2,1));
[~, min_ind] = min(dqq);
K = 1;
obj.templates(:,1) = obj.f(:,min_ind);
obj.templates_q(:,1) = q(:,min_ind);
if (isnan(obj.lambda))
fprintf('\n Initializing Lambda \n');
Kinit = 4;
obj.lambda = kpp_init(obj.f,q,obj.time,Kinit,0,option);
end
fprintf('\n lambda = %5.1f \n', obj.lambda);
%% Set initial using the original f space
fprintf('\nInitializing...\n');
f_temp = zeros(length(obj.time),N);
q_temp = zeros(length(obj.time),N);
for r = 1:option.MaxItr
gam1 = {};
Dy = zeros(K,N);
qn1 = {};
fn1 = {};
fprintf('updating step: r=%d\n', r);
if r == option.MaxItr
fprintf('maximal number of iterations is reached. \n');
end
for i=1:K
gamt = zeros(N,size(q,1));
if option.parallel == 1
parfor k = 1:N
q_c = q(:,k); mq_c = obj.templates_q(:,i);
if (option.alignment)
gamt(k,:) = optimum_reparam(mq_c,q_c,obj.time,obj.lambda,option.method, ...
obj.templates(1,i), obj.f(1,k));
else
gamt(k,:) = linspace(0,1,M);
end
f_temp(:,k) = warp_f_gamma(obj.f(:,k),gamt(k,:),obj.time);
q_temp(:,k) = f_to_srvf(f_temp(:,k),obj.time);
Dy(i,k) = sqrt(sum(trapz(obj.time,(q_temp(:,k)-obj.templates_q(:,i)).^2)));
end
else
for k = 1:N
q_c = q(:,k); mq_c = obj.templates_q(:,i);
if (option.alignment)
gamt(k,:) = optimum_reparam(mq_c,q_c,obj.time,obj.lambda,option.method, ...
obj.templates(1,i), obj.f(1,k));
else
gamt(k,:) = linspace(0,1,M);
end
f_temp(:,k) = warp_f_gamma(obj.f(:,k),gamt(k,:),obj.time);
q_temp(:,k) = f_to_srvf(f_temp(:,k),obj.time);
Dy(i,k) = sqrt(sum(trapz(obj.time,(q_temp(:,k)-obj.templates_q(:,i)).^2)));
end
end
gam1{i} = gamt;
qn1{i} = q_temp;
fn1{i} = f_temp;
end
%% Template Identification
[minval,cluster_id] = min(Dy,[],1);
idx = minval > obj.lambda;
for ii = 1:length(idx)
if idx(ii) ~= 0
K = K + 1;
cluster_id(ii) = K;
obj.templates(:,K) = obj.f(:,ii);
obj.templates_q(:,K) = q(:,ii);
end
end
qun1 = zeros(1,K);
for i = 1:K
id = cluster_id == i;
for j = 1:length(id)
qun1(i) = qun1(i) + sqrt(sum(trapz(obj.time,(q(:,id(j))-obj.templates_q(:,i)).^2)));
end
end
obj.qun(r+1) = sum(qun1)+obj.lambda*K;
if abs(obj.qun(r+1)-obj.qun(r)) < option.thresh || r >= option.MaxItr
break;
end
end
%% Output
ftmp = {};
qtmp = {};
gamtmp = {};
for i = 1:K
id = cluster_id == i;
ftmp{i} = fn1{i}(:,id);
qtmp{i} = qn1{i}(:,id);
gamtmp{i} = gam1{i}(id,:);
end
if option.parallel == 1 && option.closepool == 1
if isempty(gcp('nocreate'))
delete(gcp('nocreate'))
end
end
obj.fn = ftmp;
obj.qn = qtmp;
obj.labels = cluster_id;
obj.method = option.method;
obj.qun = obj.qun(1:r);
end
function plot(obj)
% PLOT plot kmeans clustering alignment results
% -------------------------------------------------------------
% Usage: obj.plot()
figure(1); clf;
plot(obj.time, obj.f, 'linewidth', 1);
title('Original data', 'fontsize', 16);
K = length(obj.fn);
colors = varycolor(K);
figure(2); clf; hold all
for k=1:K
plot(obj.time, obj.templates(:,k), 'Color', colors(k,:));
end
title('Cluster Mean Functions');
figure(3); clf;
plot(obj.time, obj.fn{1}, 'Color', colors(1,:));
hold all
for k = 2:K
plot(obj.time, obj.fn{k}, 'Color', colors(k,:));
end
for k = 1:K
plot(obj.time, obj.templates(:,k), 'Color', colors(k,:));
end
title('Clustered Functions');
figure(4); clf;
plot(obj.time, obj.qn{1}, 'Color', colors(1,:));
hold all
for k = 2:K
plot(obj.time, obj.qn{k}, 'Color', colors(k,:));
end
for k = 1:K
plot(obj.time, obj.templates_q(:,k), 'Color', colors(k,:));
end
title('Clustered Functions');
end
end
end
function [lambda] = kpp_init(f,q,time,K,lambda,option)
%k++ init
%lambda: max distance to k++ means
[M,N] = size(q);
mu = zeros(M,K);
mu_f = mu;
idx = ceil(rand*N);
mu(:,1) = q(:,idx);
mu_f(:,1) = f(:,idx);
Dy = inf(K,N);
for i = 2:K
if option.parallel == 1
parfor k = 1:N
q_c = q(:,k); mq_c = mu(:,i-1);
if (option.alignment)
gamt = optimum_reparam(mq_c,q_c,time,lambda,option.method, ...
mu_f(1,i-1), f(1,k));
else
gamt = linspace(0,1,M);
end
f_temp = warp_f_gamma(f(:,k),gamt,time);
q_temp = f_to_srvf(f_temp,time);
Dy(i,k) = sqrt(sum(trapz(time,(q_temp-mq_c).^2)));
end
else
for k = 1:N
q_c = q(:,k); mq_c = mu(:,i-1);
if (option.alignment)
gamt = optimum_reparam(mq_c,q_c,time,lambda,option.method, ...
mu_f(1,i-1), f(1,k));
else
gamt = linspace(0,1,M);
end
f_temp = warp_f_gamma(f(:,k),gamt,time);
q_temp = f_to_srvf(f_temp,time);
Dy(i,k) = sqrt(sum(trapz(time,(q_temp-mq_c).^2)));
end
end
idxold = idx;
idx = find(rand < cumsum(Dy(i,:)/sum(Dy(i,:))),1);
Dy(i,idxold)=max(Dy(i,:));
mu(:,i) = q(:,idx);
mu_f(:,i) = f(:,idx);
end
lambda = max(min(Dy(2:end,:),[],1));
end