-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUI.m
More file actions
1850 lines (1645 loc) · 71.2 KB
/
UI.m
File metadata and controls
1850 lines (1645 loc) · 71.2 KB
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = UI(varargin)
clear;
global i_f_method i_lambda i_I i_R i_temporal i_seasonal i_periodic i_mode_Periodic i_mode_Seasonal i_I_Seasonal i_R_Seasonal i_I_Periodic i_R_Periodic i_struct i_order i_order_nnt i_s_angle i_s_corr i_defiend_periodic i_defiend_anomalies i_defiend_changes i_added i_streaming i_noise i_nonnegative i_sparse i_normal i_fixsign i_ns X;
i_I=[];
i_R=[];
i_f_method={};
i_temporal=0;
i_seasonal=0;
i_periodic=0;
i_mode_Periodic=0;
i_mode_Seasonal=0;
i_R_Seasonal=0;
i_I_Seasonal=0;
i_I_Periodic=0;
i_R_Periodic=0;
i_struct='';
i_order=[];
i_order_nnt=0;
i_s_angle=0;
i_s_corr=0;
i_defiend_periodic=0;
i_defiend_seasonal=0;
i_defiend_anomalies=0;
i_defiend_changes=0;
i_streaming=0;
i_added=0;
i_noise=0;
i_nonnegative=0;
i_sparse=0;
i_sparse_param=0.5;
i_normal=0;
i_fixsign=0;
i_lambda=[1 1 1];
i_ns=0;
tabs={'Modes','PeriodWave','SeasonalEffects','Streaming','ChangePoints','Anomalies','Noise','NonNegative','Sparsity','Settings','Generate','About'};
if ~isdeployed
addpath('functions');
addpath('tensor_toolbox_2.6');
end
% *********************************************************************************************************
% F U N C T I O N S
% *********************************************************************************************************
% Export tensor
function save_tensor(hObject, eventdata)
[file,path,filterindex] = uiputfile({'*.mat','Tensor Toolbox Format (*.mat)';'*.mat','Dense MATLAB format (*.mat)';'*.csv','Comma Separated Values (*.csv)';'*.hdf','Hierarchical Data Format - HDF5 (*.hdf)'},'Save file name');
if file
filename=strcat(path,file);
tobj=tab{findtab('Generate')};
edm=findobj(tobj,'Tag','edit_multiline');
line=get(edm, 'String');
filetypes={'Tensor Toolbox','Dense MATLAB array','CSV','HDF5'};
line{length(line)+1}=sprintf('Exporting %s format ... Please wait ...',filetypes{filterindex});
set(edm, 'String',line);
pause(0.5);
if exist(filename, 'file') == 2
delete(filename);
end
switch filterindex
case 1 % Tensor Toolbox Format (incl. sparse tensors)
save(filename,'X');
case 2 % Dense Matlab array
if ~isa(X,'double')
Xd=double(X);
save(filename,'Xd');
else
save(filename,'X');
end
case 3 % CSV
if isa(X,'sptensor')
csvwrite(filename,[X.subs X.vals]);
else
Xs=sptensor(X);
csvwrite(filename,[Xs.subs Xs.vals]);
end
case 4 %HDF
if isa(X,'sptensor')
dat=[X.subs X.vals];
h5create(filename,'/X',size(X));
h5write(filename,'/X',dat);
else
Xd=double(X);
h5create(filename,'/X',size(Xd));
h5write(filename,'/X',Xd);
end
end
line{length(line)+1}=sprintf('Saving to "%s" ... Done',filename);
set(edm, 'String',line);
end
end
% Generate Tensor
function generate_tensor(hObject, eventdata)
tobj=tab{findtab('Generate')};
edm=findobj(tobj,'Tag','edit_multiline');
savebutton=findobj(tobj,'Tag','savebutton');
% **************************************************************
% initialization check
% **************************************************************
MaxP=0;
MaxS=0;
if i_order-i_order_nnt==1
if i_seasonal && i_periodic
MaxP=i_R(i_order_nnt+1);
MaxS=i_R(i_order_nnt+1);
if i_defiend_periodic+i_defiend_seasonal~=MaxP
errordlg(sprintf('You have defined "%i" factors in total for peridoic waves and seasonal effects while it should be equal to "%i" in total!',i_defiend_periodic+i_defiend_seasonal,i_R(i_order_nnt+1)));
tgroup.SelectedTab = tab{findtab('PeriodWave')};
return;
end
end
if i_seasonal==1 && i_periodic==0
MaxS=i_R(i_order_nnt+1);
if i_defiend_seasonal~=MaxS
errordlg(sprintf('You have defined "%i" factors in total for seasonal effects while it should be equal to "%i"!',i_defiend_seasonal,i_R(i_order_nnt+1)));
tgroup.SelectedTab = tab{findtab('SeasonalEffects')};
return;
end
end
if i_seasonal==0 && i_periodic==1
MaxP=i_R(i_order_nnt+1);
if i_defiend_periodic~=MaxP
errordlg(sprintf('You have defined "%i" factors in total for periodic waves while it should be equal to "%i"!',i_defiend_periodic,i_R(i_order_nnt+1)));
tgroup.SelectedTab = tab{findtab('PeriodWave')};
return;
end
end
elseif i_order-i_order_nnt==2
MaxP=i_R(i_order_nnt+1);
MaxS=i_R(i_order_nnt+2);
if i_defiend_periodic~=MaxP
errordlg(sprintf('You have defined "%i" factors in total for periodic waves while it should be equal to "%i"!',i_defiend_periodic,i_R(i_order_nnt+1)));
tgroup.SelectedTab = tab{findtab('PeriodWave')};
return;
end
if i_defiend_seasonal~=MaxS
errordlg(sprintf('You have defined "%i" factors in total for seasonal effects while it should be equal to "%i"!',i_defiend_seasonal,i_R(i_order_nnt+1)));
tgroup.SelectedTab = tab{findtab('SeasonalEffects')};
return;
end
end
if i_defiend_changes
tobj_changes=tab{findtab('ChangePoints')};
data_changes_p=get(findobj(tobj_changes,'Tag','uitable_changepoints_p'),'Data');
data_changes_s=get(findobj(tobj_changes,'Tag','uitable_changepoints_s'),'Data');
for n=1:size(data_changes_p,1)
if cell2mat(data_changes_p(n,1))>i_I(i_mode_Periodic) || cell2mat(data_changes_p(n,2))>i_I(i_mode_Periodic)
errordlg(sprintf('The start point for changepoint exceed the defined size of dimension. You should reset changepoints!'));
tgroup.SelectedTab = tab{findtab('ChangePoints')};
return;
end
end
for n=1:size(data_changes_s,1)
if cell2mat(data_changes_s(n,1))>i_I(i_mode_Seasonal) || cell2mat(data_changes_s(n,2))>i_I(i_mode_Seasonal)
errordlg(sprintf('The start point for changepoint exceed the defined size of dimension. You should reset changepoints!'));
tgroup.SelectedTab = tab{findtab('ChangePoints')};
return;
end
end
cell2mat(data_changes_s(:,1))
cell2mat(data_changes_s(:,2))
end
% ************************
% end
% ************************
line{1}=sprintf('********** SimTensor v1.0 - developed by Hadi Fanaee-T, INESC TEC *************');
line{2}=sprintf('Generating %s-sctructured Tensor with order of %i and size of [%s]...',i_struct,i_order,strrep(num2str(i_I),' ',' '));
set(edm, 'String',line);
if strcmp(i_struct,'CP')
typ='Lambda';
lambdastr=sprintf('[%s]',strrep(num2str(i_lambda),' ',' '));
else
typ='G';
lambdastr=sprintf('[%s] (%s)',strrep(num2str(i_lambda),' ',' '),strrep(num2str(i_R),' ',' '));
end
line{length(line)+1}=sprintf(' %s = %s',typ,lambdastr);
set(edm, 'String',line);
line{length(line)+1}=sprintf(' Generating Factor matrices ...');
set(edm, 'String',line)
for n=1:i_order_nnt
line{length(line)+1}=sprintf(' Mode #%i (Non-temporal): Generate "%i" factors with "%s" distribution, length of "%i" and noise "%f"',n,i_R(n),i_f_method{n},i_I(n),i_ns);
set(edm, 'String',line);
if i_s_angle
line{length(line)+1}=sprintf(' Mode #%i Applying Angle of "%f" between factors',n,i_s_angle);
set(edm, 'String',line);
U0{n}=SimAngle(i_I(n),i_R(n),i_s_angle,eval(strcat('@',i_f_method{n})));
else
U0{n}=SimFacGen(i_f_method{n},i_I(n),i_R(n),i_ns);
end
if i_s_corr
line{length(line)+1}=sprintf(' Mode #%i Applying Correlation of "%f" between factors',n,i_s_corr);
set(edm, 'String',line);
U0{n}=SimCol(U0{n},i_s_corr);
end
end
if i_periodic
line{length(line)+1}=sprintf(' Mode #%i (Perioidc waves)',i_mode_Periodic);
set(edm, 'String',line);
tobj_periodic=tab{findtab('PeriodWave')};
data_periodic=get(findobj(tobj_periodic,'Tag','uitable_periodic_table'),'Data');
%i_p_method = strjoin(data_periodic(:,1),'|');
%i_p_nwaves = strrep(strjoin(cellstr(num2str(cell2mat(data_periodic(:,2)))),'|'),' ','');
%i_p_freq = strrep(strjoin(cellstr(num2str(cell2mat(data_periodic(:,3)))),'|'),' ','');
for n=1:size(data_periodic,1)
line{length(line)+1}=sprintf(' Periodic wave factor #%i with "%s" function, "%i" waves with frequency "%i" , and noise "%f"',n,strjoin(data_periodic(n,1)),cell2mat(data_periodic(n,2)),cell2mat(data_periodic(n,3)),i_ns );
set(edm, 'String',line);
U0{i_mode_Periodic}(:,n)=SimPeriodWave(strjoin(data_periodic(n,1)),i_I_Periodic,cell2mat(data_periodic(n,2)),cell2mat(data_periodic(n,3)),i_ns);
end
end
if i_seasonal
if i_order-i_order_nnt==1 && i_seasonal && i_periodic
prev_n=n;
else
prev_n=0;
end
tobj_seasonal=tab{findtab('SeasonalEffects')};
data_seasonal=get(findobj(tobj_seasonal,'Tag','uitable_seasonal'),'Data');
line{length(line)+1}=sprintf(' Mode #%i (Seasonal Effects)',i_mode_Seasonal);
set(edm, 'String',line);
for n=1:size(data_seasonal,1)
i_t_pat{n}=str2num(cell2mat(data_seasonal(n,1)));
i_t_prd{n}=str2num(cell2mat(data_seasonal(n,2)));
i_t_gr{n}=str2num(cell2mat(data_seasonal(n,3)));
line{length(line)+1}=sprintf(' Seasonal factor #%i with pattern of length "%i" , growth rate of "%f" and noise "%f"',n,length(i_t_prd{n}),i_t_gr{n},i_ns );
set(edm, 'String',line);
[~,U0{i_mode_Seasonal}(:,n+prev_n)]=SimPeriodPattern(i_I_Seasonal,i_t_pat{n},i_t_prd{n},i_ns,i_t_gr{n});
end
end
% Non-negativity
tobj_nonnegative=tab{findtab('NonNegative')};
nng_factors=get(findobj(tobj_nonnegative,'Tag','radiobutton_factors'),'Value');
nng_whole=get(findobj(tobj_nonnegative,'Tag','radiobutton_whole'),'Value');
nng_factors_th=str2num(get(findobj(tobj_nonnegative,'Tag','edit_factors'),'String'));
nng_whole_th=str2num(get(findobj(tobj_nonnegative,'Tag','edit_whole'),'String'));
if nng_factors
line{length(line)+1}=sprintf(' Applying non-negativity constraint (U{i}<%f=0) on factors ...',nng_factors_th);
set(edm, 'String',line);
for n=1:i_order
U0_nng=U0;
U0_nng{n}( U0{n} < nng_factors_th ) = 0;
end
end
if i_sparse==1
line{length(line)+1}=sprintf(' Applying Sparsity on factors...');
set(edm, 'String',line);
for i=1:i_order
line{length(line)+1}=sprintf(' Mode #i: sparsity ratio: %f',i,i_sparse_param);
set(edm, 'String',line);
for j=1:size(U0{i},2)
%nnz1=nnz(U0{i}(:,j));
[U0{i}(:,j),OS{i}(:,j),OMS{i}(:,j)]=remove_random_elements(U0{i}(:,j),i_sparse_param,0);
%nnz2=nnz(U0{i}(:,j));
%line{length(line)+1}=sprintf(' Factor #%i: %f % Sparsed',j,nnz2/nnz1);
%set(edm, 'String',line);
end
U0{i}=sptensor(U0{i});
end
end
if strcmp(i_struct,'Tucker')
G=reshape(i_lambda,i_R)
X0=ttensor(tensor(G),U0);
else
X0=ktensor(i_lambda',U0);
end
if i_normal
if (isa(X0,'ktensor') || isa(X0,'ttensor')) && i_sparse==0
X0 = arrange(X0);
line{length(line)+1}=sprintf(' Normalizing Final Tensor ...');
set(edm, 'String',line);
else
line{length(line)+1}=sprintf(' Warning! Normalizing operation was aborted.');
set(edm, 'String',line);
end
end
if i_fixsign
if (isa(X0,'ktensor') || isa(X0,'ttensor')) && i_sparse==0
line{length(line)+1}=sprintf(' Fixing Signs of Final Tensor ...');
set(edm, 'String',line);
X0 = fixsigns(X0);
else
line{length(line)+1}=sprintf(' Warning! Fixing Sign operation was aborted.');
set(edm, 'String',line);
end
end
if i_sparse==0 || i_sparse==2
X0=full(X0);
end
if i_streaming
if i_temporal==0 || i_nonnegative==0
line{length(line)+1}=sprintf(' Applying Streaming strucutre with variation parameter of "%f"',i_streaming);
set(edm, 'String',line);
X0=SimStream(U0,i_I,i_R,i_streaming);
else
line{length(line)+1}=sprintf(' Warning! Streaming strucutre was ignored due to existance of temporal modes');
set(edm, 'String',line);
end
end
if i_defiend_changes
if ~isa(U0{1},'sptensor')
tobj_changes=tab{findtab('ChangePoints')};
data_changes_p=get(findobj(tobj_changes,'Tag','uitable_changepoints_p'),'Data');
data_changes_s=get(findobj(tobj_changes,'Tag','uitable_changepoints_s'),'Data');
changes_onfactor=get(findobj(tobj_changes,'Tag','changes_onfactor'),'Value');
changes_ontensor=get(findobj(tobj_changes,'Tag','changes_ontensor'),'Value');
if changes_onfactor
cmde='factors';
else
cmde='final tensor';
end
line{length(line)+1}=sprintf(' Adding %i Change-points on %s ...',i_defiend_changes,cmde);
set(edm, 'String',line);
for n=1:size(data_changes_p,1)
i_h_cp_from{n}=cell2mat(data_changes_p(n,1));
i_h_cp_to{n}=cell2mat(data_changes_p(n,2));
i_h_cp_val{n}=cell2mat(data_changes_p(n,4));
i_h_cp_typ{n}=cell2mat(data_changes_p(n,3));
mu(n)= mean(U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n}));
s(n)= std(U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n}));
if strfind(i_h_cp_val{n},'mu')
i_h_cp_val2{n} = strrep(i_h_cp_val{n}, 'mu', '');
i_h_cp_val2{n}=str2num(i_h_cp_val2{n})*mu(n);
elseif strfind(i_h_cp_val{n},'sd')
i_h_cp_val2{n} = strrep(i_h_cp_val{n}, 'sd', '');
i_h_cp_val2{n}=str2num(i_h_cp_val2{n})*s(n);
else
i_h_cp_val2{n}=str2num(i_h_cp_val{n});
end
for k=1:i_R_Periodic
if strcmp(i_h_cp_typ{n},'Multiply')
if changes_onfactor
U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n},k)=U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n},k)*i_h_cp_val2{n};
else
idx = repmat({':'}, 1, length(size(X0)));
idx{i_mode_Periodic}=i_h_cp_from{n}:i_h_cp_to{n};
X0(idx{:})=X0(idx{:})*i_h_cp_val2{n};
end
if k==1
line{length(line)+1}=sprintf(' Mode #%i (Periodic waves): Multiply by %f (%s) the elements in the period between %i to %i',i_mode_Periodic,i_h_cp_val2{n},i_h_cp_val{n},i_h_cp_from{n},i_h_cp_to{n});
set(edm, 'String',line);
end
else
if changes_onfactor
U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n},k)=U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n},k)+i_h_cp_val2{n};
else
idx = repmat({':'}, 1, length(size(X0)));
idx{i_mode_Periodic}=i_h_cp_from{n}:i_h_cp_to{n};
X0(idx{:})=X0(idx{:})+i_h_cp_val2{n};
end
if k==1
line{length(line)+1}=sprintf(' Mode #%i (Periodic waves): Add %f (%s) to the elements in the period between %i to %i',i_mode_Periodic,i_h_cp_val2{n},i_h_cp_val{n},i_h_cp_from{n},i_h_cp_to{n});
set(edm, 'String',line);
end
end
end
end
for n=1:size(data_changes_s,1)
i_h_cp_from{n}=cell2mat(data_changes_s(n,1));
i_h_cp_to{n}=cell2mat(data_changes_s(n,2));
i_h_cp_val{n}=cell2mat(data_changes_s(n,4));
i_h_cp_typ{n}=cell2mat(data_changes_s(n,3));
mu(n)= mean(U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n}));
s(n)= std(U0{i_mode_Periodic}(i_h_cp_from{n}:i_h_cp_to{n}));
if strfind(i_h_cp_val{n},'mu')
i_h_cp_val2{n} = strrep(i_h_cp_val{n}, 'mu', '');
i_h_cp_val2{n}=str2num(i_h_cp_val2{n})*mu(n);
elseif strfind(i_h_cp_val{n},'sd')
i_h_cp_val2{n} = strrep(i_h_cp_val{n}, 'sd', '');
i_h_cp_val2{n}=str2num(i_h_cp_val2{n})*s(n);
else
i_h_cp_val2{n}=str2num(i_h_cp_val{n});
end
for k=1:i_R_Seasonal
if strcmp(i_h_cp_typ{n},'Multiply')
if changes_onfactor
U0{i_mode_Seasonal}(i_h_cp_from{n}:i_h_cp_to{n},k)=U0{i_mode_Seasonal}(i_h_cp_from{n}:i_h_cp_to{n},k)*i_h_cp_val2{n};
else
idx = repmat({':'}, 1, length(size(X0)));
idx{i_mode_Periodic}=i_h_cp_from{n}:i_h_cp_to{n};
X0(idx{:})=X0(idx{:})*i_h_cp_val2{n};
end
if k==1
line{length(line)+1}=sprintf(' Mode #%i (Seasonal effects): Multiply by %f (%s) the elements in the period between %i to %i',i_mode_Seasonal,i_h_cp_val2{n},i_h_cp_val{n},i_h_cp_from{n},i_h_cp_to{n});
set(edm, 'String',line);
end
else
if changes_onfactor
U0{i_mode_Seasonal}(i_h_cp_from{n}:i_h_cp_to{n},k)=U0{i_mode_Seasonal}(i_h_cp_from{n}:i_h_cp_to{n},k)+i_h_cp_val2{n};
else
idx = repmat({':'}, 1, length(size(X0)));
idx{i_mode_Periodic}=i_h_cp_from{n}:i_h_cp_to{n};
X0(idx{:})=X0(idx{:})+i_h_cp_val2{n};
end
if k==1
line{length(line)+1}=sprintf(' Mode #%i (Seasonal effects): Add %f (%s) to the elements in the period between %i to %i',i_mode_Seasonal,i_h_cp_val2{n},i_h_cp_val{n},i_h_cp_from{n},i_h_cp_to{n});
set(edm, 'String',line);
end
end
end
end
else
line{length(line)+1}=sprintf(' Warning! Change-points were aborted ...');
set(edm, 'String',line);
end
end
if i_defiend_anomalies
if ~isa(U0{1},'sptensor')
tobj_anomalies=tab{findtab('Anomalies')};
data_anomalies=get(findobj(tobj_anomalies,'Tag','uitable_anomalies'),'Data');
anomalies_onfactors=get(findobj(tobj_anomalies,'Tag','anomalies_onfactors'),'Value');
anomalies_ontensor=get(findobj(tobj_anomalies,'Tag','anomalies_ontensor'),'Value');
if anomalies_onfactors
cmde='factors';
else
cmde='final tensor';
end
line{length(line)+1}=sprintf(' Adding "%i" anomalies on %s ...',i_defiend_changes,cmde);
set(edm, 'String',line);
for n=1:size(data_anomalies,1)
anomaly_period_start=str2num(cell2mat(data_anomalies(n,1)));
anomaly_period_end=str2num(cell2mat(data_anomalies(n,2)));
tensor_random_generator=strtrim(char(cellstr(data_anomalies(n,3))));
AnomalyTensorR=str2num(cell2mat(data_anomalies(n,4)));
if length(AnomalyTensorR)==1
AnomalyTensorRs=repmat(AnomalyTensorR,length(anomaly_period_start),1);
end
anomaly_lambda_factor=cell2mat(data_anomalies(n,5));
anomaly_lambda=i_lambda*anomaly_lambda_factor;
AnomalyTensorSize=anomaly_period_end-anomaly_period_start+1;
line{length(line)+1}=sprintf(' Anomaly #%i: Random generator: %s, size: [%s], rank: %i, lambda/core: %s',n,tensor_random_generator,strrep(num2str(AnomalyTensorSize),' ',' '),AnomalyTensorR,strrep(num2str(anomaly_lambda),' ',' '));
set(edm, 'String',line);
for i=1:length(AnomalyTensorSize)
set(edm, 'String',line);
AnomalyTensorU{i}=SimFacGen(tensor_random_generator,AnomalyTensorSize(i),AnomalyTensorRs(i),0,0);
if anomalies_onfactors
U0{i}(anomaly_period_start(i):anomaly_period_end(i),:)=AnomalyTensorU{i};
end
end
if anomalies_ontensor
X_anomaly=ktensor(anomaly_lambda',AnomalyTensorU);
for i=1:size(anomaly_period_start,2)
anomaly_period(i)={anomaly_period_start(i):anomaly_period_end(i)};
end
X0(anomaly_period{:})=X_anomaly;
end
end
else
line{length(line)+1}=sprintf(' Warning! Anomalies were aborted ...');
set(edm, 'String',line);
end
end
X=X0;
% Noise
tobj=tab{findtab('Noise')};
radiobutton_whole=get(findobj(tobj,'Tag','radiobutton_whole'),'Value');
edit_noise_snr=str2num(get(findobj(tobj,'Tag','edit_noise_snr'),'String'));
radiobutton_sparse=get(findobj(tobj,'Tag','radiobutton_sparse'),'Value');
edit_noise_level=str2num(get(findobj(tobj,'Tag','edit_noise_level'),'String'));
if radiobutton_whole==1
line{length(line)+1}=sprintf(' Adding white noise (%f dB) on final tensor ...',edit_noise_snr);
set(edm, 'String',line);
X=add_awgn_noise(X0,edit_noise_snr); % by Mathuranathan Viswanathan
end
if radiobutton_sparse==1
line{length(line)+1}=sprintf(' Adding sparse white noise (ratio of %f) on final tensor ...',edit_noise_level);
set(edm, 'String',line);
X=add_sparse_noise(X0,edit_noise_level,1);
end
% Non-negative
if nng_whole
line{length(line)+1}=sprintf(' Applying non-negativity constraint (X<%f=0) on final tensor ...',nng_whole_th);
set(edm, 'String',line);
X_nng=reshape(double(X),prod(size(X)),1);
X_nng(X_nng<nng_whole_th)=0;
X=tensor(reshape(X_nng,size(X)));
end
% Sparsity
tobj_sparsity=tab{findtab('Sparsity')};
radiobutton_sparsetensor=get(findobj(tobj_sparsity,'Tag','radiobutton_sparsetensor'),'Value');
i_m_rate=str2num(get(findobj(tobj_sparsity,'Tag','edit_sparsetensor'),'String'));
checkbox_sparsetensor=get(findobj(tobj_sparsity,'Tag','checkbox_sparsetensor'),'Value');
radiobutton_counttensor=get(findobj(tobj_sparsity,'Tag','radiobutton_counttensor'),'Value');
sparsetensor_R=str2num(get(findobj(tobj_sparsity,'Tag','sparsetensor_R'),'String'));
sparsetensor_R2=str2num(get(findobj(tobj_sparsity,'Tag','sparsetensor_R2'),'String'));
sparsetensor_ceiling=str2num(get(findobj(tobj_sparsity,'Tag','sparsetensor_ceiling'),'String'));
if radiobutton_sparsetensor
line{length(line)+1}=sprintf(' Applying sparsity on final tensor (ratio: %f) ...',i_m_rate);
set(edm, 'String',line);
[X,O,Omega]=remove_random_elements(X,i_m_rate,checkbox_sparsetensor);
end
if radiobutton_counttensor
line(2:length(line))=[];
line{length(line)+1}=sprintf('Warning! All confirguations were ignored, because you chose to generate sparse count tensor (Sparsity tab)',num2str(i_I),sparsetensor_R,sparsetensor_R2);
line{length(line)+1}=sprintf('Generating sparse count tensor (Size: [%s], Rank: %i, Noise components: %i)',num2str(i_I),sparsetensor_R,sparsetensor_R2);
set(edm, 'String',line);
lambda=SimLamGen('CP',sparsetensor_R,'gamma',100,1);
lambda(sparsetensor_R+1:sparsetensor_R2+sparsetensor_R+1-1)=rand(sparsetensor_R2,1);
for n=1:length(i_I);
[~, U0{n}]=SimFacGen('gamma',i_I(n),sparsetensor_R+sparsetensor_R2,0,1);
end
X0=ktensor(lambda',U0);
X=sptensor(poissrnd(double(X0)));
U0=[];
X0=[];
end
line{length(line)+1}=sprintf('The tensor was generated sucessfully! \r\n');
set(edm, 'String',line);
set(savebutton,'Enable','On');
end
function generate_edit(hObject, eventdata)
eventdata.PreviousData
input = get(hObject,'String');
set(eventdata.Source,'String',EventData.PreviousData);
end
function sparsity_update(PushButton, EventData)
tobj=tab{findtab('Sparsity')};
radiobutton_factors=get(findobj(tobj,'Tag','radiobutton_factors'),'Value');
radiobutton_sparsetensor=get(findobj(tobj,'Tag','radiobutton_sparsetensor'),'Value');
radiobutton_counttensor=get(findobj(tobj,'Tag','radiobutton_counttensor'),'Value');
edit_factors=findobj(tobj,'Tag','edit_factors');
edit_sparsetensor=findobj(tobj,'Tag','edit_sparsetensor');
checkbox_sparsetensor=findobj(tobj,'Tag','checkbox_sparsetensor');
sparsetensor_R=findobj(tobj,'Tag','sparsetensor_R');
sparsetensor_R2=findobj(tobj,'Tag','sparsetensor_R2');
sparsetensor_ceiling=findobj(tobj,'Tag','sparsetensor_ceiling');
set(edit_factors,'Enable',offon(radiobutton_factors));
set(edit_sparsetensor,'Enable',offon(radiobutton_sparsetensor));
set(checkbox_sparsetensor,'Enable',offon(radiobutton_sparsetensor));
set(sparsetensor_R,'Enable',offon(radiobutton_counttensor));
set(sparsetensor_R2,'Enable',offon(radiobutton_counttensor));
set(sparsetensor_ceiling,'Enable',offon(radiobutton_counttensor));
if radiobutton_factors==1
i_sparse=1;
i_sparse_param=str2num(get(edit_factors,'String'));
elseif radiobutton_sparsetensor==1
i_sparse=2;
elseif radiobutton_counttensor==1
i_sparse=3;
i_sparse_param=edit_factors;
else
i_sparse=0;
end
status_update();
end
function nonnegative_update(PushButton, EventData)
tobj=tab{findtab('NonNegative')};
radiobutton_none=get(findobj(tobj,'Tag','radiobutton_none'),'Value');
radiobutton_factors=get(findobj(tobj,'Tag','radiobutton_factors'),'Value');
radiobutton_whole=get(findobj(tobj,'Tag','radiobutton_whole'),'Value');
edit_factors=findobj(tobj,'Tag','edit_factors');
edit_whole=findobj(tobj,'Tag','edit_whole');
set(edit_factors,'Enable',offon(radiobutton_factors));
set(edit_whole,'Enable',offon(radiobutton_whole));
if radiobutton_factors==1 || radiobutton_whole==1
i_nonnegative=1;
else
i_nonnegative=0;
end
status_update();
end
function noise_update(PushButton, EventData)
tobj=tab{findtab('Noise')};
radiobutton_nonoise=get(findobj(tobj,'Tag','radiobutton_nonoise'),'Value');
radiobutton_factors=get(findobj(tobj,'Tag','radiobutton_factors'),'Value');
radiobutton_whole=get(findobj(tobj,'Tag','radiobutton_whole'),'Value');
radiobutton_sparse=get(findobj(tobj,'Tag','radiobutton_sparse'),'Value');
edit_noise_factors=findobj(tobj,'Tag','edit_noise_factors');
edit_noise_snr=findobj(tobj,'Tag','edit_noise_snr');
edit_noise_level=findobj(tobj,'Tag','edit_noise_level');
set(edit_noise_factors,'Enable',offon(radiobutton_factors));
set(edit_noise_snr,'Enable',offon(radiobutton_whole));
set(edit_noise_level,'Enable',offon(radiobutton_sparse));
if radiobutton_factors==1 || radiobutton_whole==1 || radiobutton_sparse==1
i_noise=1;
else
i_noise=0;
end
if radiobutton_factors==1
i_ns=str2num(get(edit_noise_factors,'String'));
else
i_ns=0;
end
status_update();
end
function anomalies_empty(PushButton, EventData)
tobj=tab{findtab('Anomalies')};
uitable_anomalies=findobj(tobj,'Tag','uitable_anomalies');
set(uitable_anomalies,'Data',{blanks(0),blanks(0),blanks(0),blanks(0),blanks(0)});
set(uitable_anomalies,'ColumnEditable',logical([0 0 0 0 0]) );
i_defiend_anomalies=0;
status_update();
end
function anomalies_edit(PushButton, EventData)
returnback=0;
col=EventData.Indices(2);
rw=EventData.Indices(1);
dat=get(EventData.Source,'Data');
if col==1 || col==2
prv=str2num(EventData.PreviousData);
nwe=str2num(EventData.NewData);
col1=str2num(dat{rw,1});
col2=str2num(dat{rw,2});
if isnan(nwe)
returnback=1;
end
if length(nwe)~= length(i_I)
returnback=1;
end
if ~isempty(find(nwe<1))
returnback=1;
end
if returnback==0
if ~isempty(find(i_I-nwe <0))
returnback=1;
end
if ~isempty(find(col2-col1<0))
returnback=1;
end
end
end
if col==4
prv=str2num(EventData.PreviousData);
nwe=str2num(EventData.NewData);
if isempty(nwe)
returnback=1;
end
if length(nwe)>1 && length(nwe)~=length(i_I)
returnback=1;
end
if ~isempty(find(nwe<1))
returnback=1;
end
end
if col==5
prv=EventData.PreviousData;
nwe=EventData.NewData;
if isnan(nwe)
returnback=1;
end
end
if returnback==1
dat{rw,col}=num2str(prv);
set(EventData.Source,'Data',dat);
errordlg('Invalid edit! the value was changed to its previous state');
end
end
function anomalies_slide(PushButton, EventData)
tobj=tab{findtab('Anomalies')};
anomalies_length=findobj(tobj,'Tag','anomalies_length');
anomalies_slide_prev=findobj(tobj,'Tag','anomalies_slide_prev');
anomalies_length=get(anomalies_length,'Value');
small_tensor_size=ceil(i_I*anomalies_length);
anomalies_slide_prev.String=mat2str(small_tensor_size);
end
function anomalies_add(PushButton, EventData)
tobj=tab{findtab('Anomalies')};
tbl=findobj(tobj,'Tag','uitable_anomalies');
anomalies_length=get(findobj(tobj,'Tag','anomalies_length'),'Value');
lambdafactor=str2num(get(findobj(tobj,'Tag','lambdafactor'),'String'));
anomalies_factorgenerator=get(findobj(tobj,'Tag','anomalies_factorgenerator'),'Value');
obj3=findobj(tobj,'Tag','anomalies_factorgenerator');
fac_methods=cellstr(get(obj3,'String'))';
anomalies_tensorrank=str2num(get(findobj(tobj,'Tag','anomalies_tensorrank'),'String'));
anomalies_length=ceil(i_I*anomalies_length);
dat=get(tbl,'Data');
if all(cellfun(@isempty, dat(:)))
dat=[];
set(tbl,'Data',dat);
rng_max=i_I;
else
rng_max=str2num(dat{size(dat,1),1})-1;
end
if ~isempty(find(rng_max-anomalies_length+1<1))
errordlg('It is not allowed to add more anomalies!');
return;
end
row{1,1}=strrep(strrep(mat2str(rng_max-anomalies_length+1),'[',''),']','');
row{1,2}=strrep(strrep(mat2str(rng_max),'[',''),']','');
row{1,3}=fac_methods{anomalies_factorgenerator};
row{1,4}=mat2str(anomalies_tensorrank);
row{1,5}=lambdafactor;
dat=[dat; row];
set(tbl,'ColumnEditable',logical([1 1 1 1 1]) );
set(tbl, 'ColumnFormat', {'numeric' 'numeric' fac_methods 'numeric' 'char'});
set(tbl,'Data',dat);
i_defiend_anomalies=i_defiend_anomalies+1;
status_update();
end
function changes_edit(PushButton, EventData)
returnback=0;
col=EventData.Indices(2);
if col==4
rw=EventData.Indices(1);
prv=EventData.PreviousData;
nwe=EventData.NewData;
if strfind(nwe,'mu')
newn2=sprintf('%0.2fmu',str2num(strrep(nwe, 'mu', '')));
elseif strfind(nwe,'sd')
newn2=sprintf('%0.2fsd',str2num(strrep(nwe, 'sd', '')));
elseif strfind(nwe,'i')
newn2=sprintf('%0.2f',str2num(strrep(nwe, 'i', '')));
else
newn2=sprintf('%0.2f',str2num(nwe));
if isempty(str2num(nwe))
newn2=EventData.PreviousData;
errordlg('Invalid edit! the value was changed to its previous state');
end
end
newn2=strrep(newn2, ' ', '');
dat=get(EventData.Source,'Data');
dat(rw,col)={newn2};
set(EventData.Source,'Data',dat);
elseif col==1 || col==2
rw=EventData.Indices(1);
prv=EventData.PreviousData;
nwe=EventData.NewData;
if strfind(PushButton.Tag,'_p')
mx=i_I_Periodic;
else
mx=i_I_Seasonal;
end
dat=get(EventData.Source,'Data');
if nwe<1 || nwe>mx
dat(rw,col)={prv};
errordlg(sprintf('The value should be between 1 and %i! it was changed to its previous state',mx));
set(EventData.Source,'Data',dat);
end
if cell2mat(dat(rw,1)) >= cell2mat(dat(rw,2))
errordlg(sprintf('The start point should be lower than the end point! The value was changed to its previous state',mx));
dat(rw,col)={prv};
set(EventData.Source,'Data',dat);
end
end
end
function changes_reset(PushButton, EventData)
tobj=tab{findtab('ChangePoints')};
tbl_p=findobj(tobj,'Tag','uitable_changepoints_p');
tbl_s=findobj(tobj,'Tag','uitable_changepoints_s');
set(tbl_s,'Data',{blanks(0),blanks(0),blanks(0),blanks(0)});
set(tbl_p,'Data',{blanks(0),blanks(0),blanks(0),blanks(0)});
i_defiend_changes=0;
status_update();
end
function changes_add(PushButton, EventData)
tobj=tab{findtab('ChangePoints')};
cp_number_of_changes=str2num(get(findobj(tobj,'Tag','cp_number_of_changes'),'String'));
cp_seasonal=get(findobj(tobj,'Tag','cp_seasonal'),'Value');
cp_periodic=get(findobj(tobj,'Tag','cp_periodic'),'Value');
cp_length=get(findobj(tobj,'Tag','cp_length'),'Value');
cp_multiply=get(findobj(tobj,'Tag','cp_multiply'),'Value');
cp_add=get(findobj(tobj,'Tag','cp_add'),'Value');
cp_mix=get(findobj(tobj,'Tag','cp_mix'),'Value');
cp_typ(1)=get(findobj(tobj,'Tag','cp_constant'),'Value');
cp_typ(2)=get(findobj(tobj,'Tag','cp_sd_factor'),'Value');
cp_typ(3)=get(findobj(tobj,'Tag','cp_mu_factor'),'Value');
cp_constant_from=str2num(get(findobj(tobj,'Tag','cp_constant_from'),'String'));
cp_constant_to=str2num(get(findobj(tobj,'Tag','cp_constant_to'),'String'));
cp_sd_from=str2num(get(findobj(tobj,'Tag','cp_sd_from'),'String'));
cp_sd_to=str2num(get(findobj(tobj,'Tag','cp_sd_to'),'String'));
cp_mu_from=str2num(get(findobj(tobj,'Tag','cp_mu_from'),'String'));
cp_mu_to=str2num(get(findobj(tobj,'Tag','cp_mu_to'),'String'));
cp_constant_int=get(findobj(tobj,'Tag','cp_constant_int'),'Value');
cp_sd_int=get(findobj(tobj,'Tag','cp_sd_int'),'Value');
cp_mu_int=get(findobj(tobj,'Tag','cp_mu_int'),'Value');
if cp_multiply==1
for i=1:cp_number_of_changes
cp_operator{i}='Multiply';
end
elseif cp_add==1;
for i=1:cp_number_of_changes
cp_operator{i}='Add';
end
else
for i=1:cp_number_of_changes
if randi([1 2],1,1)==1
cp_operator{i}='Add';
else
cp_operator{i}='Multiply';
end
end
end
if cp_constant_int==0
fparams{1}=(cp_constant_to-cp_constant_from).*rand(1,cp_number_of_changes)+cp_constant_from;
elseif cp_constant_int==1
fparams{1}=randi([cp_constant_from cp_constant_to],1,cp_number_of_changes);
end
if cp_sd_int==0
fparams{2}=(cp_sd_to-cp_sd_from).*rand(1,cp_number_of_changes)+cp_sd_from;
elseif cp_sd_int==1
fparams{2}=randi([cp_sd_from cp_sd_to],1,cp_number_of_changes);
end
if cp_mu_int==0
fparams{3}=(cp_mu_to-cp_mu_from).*rand(1,cp_number_of_changes)+cp_mu_from;
elseif cp_mu_int==1
fparams{3}=randi([cp_mu_from cp_mu_to],1,cp_number_of_changes);
end
tidx=find(cp_typ==1);
rts=randi([min(tidx) max(tidx)],1,cp_number_of_changes);
for i=1:cp_number_of_changes
pms{i}=fparams{rts(i)}(i);
if rts(i)==1
pms{i}=sprintf('%0.2f',pms{i});
elseif rts(i)==2
pms{i}=sprintf('%0.2fsd',pms{i});
elseif rts(i)==3
pms{i}=sprintf('%0.2fmu',pms{i});
end
end
tbl_p=findobj(tobj,'Tag','uitable_changepoints_p');
tbl_s=findobj(tobj,'Tag','uitable_changepoints_s');
changepoints_p_title=findobj(tobj,'Tag','changepoints_p_title');
changepoints_s_title=findobj(tobj,'Tag','changepoints_s_title');
cp_length;
cp_length_s=ceil(i_I_Seasonal*cp_length);
cp_length_p=ceil(i_I_Periodic*cp_length);
if cp_seasonal==1
startpoints_s=randperiods(i_I_Seasonal,cp_length_s,cp_number_of_changes);
endpoints_s=startpoints_s+cp_length_s;
end
if cp_periodic==1
startpoints_p=randperiods(i_I_Periodic,cp_length_p,cp_number_of_changes);
endpoints_p=startpoints_p+cp_length_p;
end
if cp_seasonal==1
for i=1:cp_number_of_changes
if endpoints_s(i)>i_I_Seasonal
endpoints_s(i)=0;
startpoints_s(i)=0;
end
if startpoints_s(i)==0
startpoints_s(i)=0;
endpoints_s(i)=0;
end
end
startpoints_s(find(startpoints_s==0))=[];
endpoints_s(find(endpoints_s==0))=[];
end
if cp_periodic==1
for i=1:cp_number_of_changes
if endpoints_p(i)>i_I_Periodic
endpoints_p(i)=0;
startpoints_p(i)=0;
end
if startpoints_p(i)==0
startpoints_p(i)=0;
endpoints_p(i)=0;
end
end
startpoints_p(find(startpoints_p==0))=[];
endpoints_p(find(endpoints_p==0))=[];
end
dat_p={blanks(0),blanks(0),blanks(0),blanks(0)};
if cp_periodic==1
set(tbl_p,'Visible','on');
set(changepoints_p_title,'Visible','on');
for i=1:length(startpoints_p)
dat_p(i,1)={startpoints_p(i)};
dat_p(i,2)={endpoints_p(i)};
dat_p(i,3)={cp_operator{i}};
dat_p(i,4)={pms{i}};
end
end
dat_s={blanks(0),blanks(0),blanks(0),blanks(0)};
if cp_seasonal==1
set(tbl_s,'Visible','on');
set(changepoints_s_title,'Visible','on');
for i=1:length(startpoints_s)
dat_s(i,1)={startpoints_s(i)};
dat_s(i,2)={endpoints_s(i)};
dat_s(i,3)={cp_operator{i}};
dat_s(i,4)={pms{i}};
end
end
set(tbl_s,'Data',dat_s);
set(tbl_p,'Data',dat_p);
for i=1:cp_number_of_changes
RowsN_p(i,1)={['ChangePoint ',sprintf('%i',i)]};
end
set(tbl_p,'RowName',RowsN_p);
for i=1:cp_number_of_changes
RowsN_s(i,1)={['ChangePoint ',sprintf('%i',i)]};
end
set(tbl_s,'RowName',RowsN_s);
set(tbl_s,'ColumnEditable',logical([1 1 1 1]) );
set(tbl_p,'ColumnEditable',logical([1 1 1 1]) );
if cp_seasonal==0
set(tbl_s,'Visible','off');
set(changepoints_s_title,'Visible','off');
end
if cp_periodic==0
set(tbl_p,'Visible','off');
set(changepoints_p_title,'Visible','off');