-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrunTDalg.m
executable file
·242 lines (224 loc) · 8.47 KB
/
runTDalg.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
function runTDalg
global algOptions algs tdalabStatus paraList hmainTDALAB elapsedTime;
global Y Ycap NumOfComp NumOfMode SIRs fit;
TDModelstr=defstr('TDModel');
% Run the algorithm
tdalab('hide');
commandwindow;
pause(0.1);
if isempty(tdalabStatus.algIndex)
errordlg('Please select an algorithm first.','Setting error','modal');
return;
else
if tdalabStatus.advEvaluation
choice=questdlg('Configuration for Monte-Carlo test is detected. Run Monte-Carlo test?','Confirm','Monte Carlo Run','Normal Run','Normal Run');
if strcmpi(choice,'Monte Carlo Run')
MCTDRun;
return;
else
tdalabStatus.algIndex=tdalabStatus.algIndex(1);
tdalabStatus.advEvaluation=false;
end
end
end
fprintf('[TDALAB] Initializing ... \n');
%% initialize
SIRs=[];Ycap=[];Ynoise=[];
algIndex=tdalabStatus.algIndex(1);
tdalg=str2func(algs(algIndex).name);
fprintf('[TDALAB] Checking the parameters ...\n');
%% options
if isempty(algOptions{algIndex})
algOptions{algIndex}=struct;
end
%% specify the number of components automatically
if isfield(algOptions{algIndex},'NumOfComp')
if strcmpi(tdalabStatus.model,'CP')
algOptions{algIndex}.NumOfComp=algOptions{algIndex}.NumOfComp(1);
elseif strcmpi(tdalabStatus.model,'Tucker')&&numel(algOptions{algIndex}.NumOfComp)==1
algOptions{algIndex}.NumOfComp=repmat(algOptions{algIndex}.NumOfComp,1,NumOfMode);
end
end
fnames=fieldnames(algOptions{algIndex});
for n=1:numel(fnames)
if isempty(algOptions{algIndex}.(fnames{n}))
algOptions{algIndex}=rmfield(algOptions{algIndex},fnames{n});
end
end
%% load observations Ynoise [tdalabtemp.mat]
global TEMPFILE;
fprintf('[TDALAB] Preparing the data for decomposition ... \n');
if get(findall(allchild(0),'tag','ckToTensor'),'value')==true
if exist(TEMPFILE,'file')~=0
load(TEMPFILE,'Ynoise','SNR');
else
Ynoise=[];SNR=rand;
end
if isempty(Ynoise)||(SNR~=tdalabStatus.noiseSNR) %% need Ynoise
if isinf(tdalabStatus.noiseSNR)
fprintf('[TDALAB] Generating the full tensor. This may cost a few minutes depending on the problem size. Please wait ...\n');
Ynoise=tensor(Y);
if strcmpi(class(Ynoise),'double')
Ynoise=tensor(Ynoise);
end
SNR=tdalabStatus.noiseSNR;
save(TEMPFILE,'Ynoise','-v7.3','SNR');
else
h=errordlg('[TDALAB] Noise data not found. Please check your noise settings.','Invalid input','modal');
uiwait(h);
tdalab;
return;
end
end
else
Ynoise=Y;
end
%% prepare for running
dispRunInformation;
switch tdalabStatus.model
case {'CP','Tucker','PMF'}
fprintf('[TDALAB] Algorithm is running. Please wait...\n');
ts=tic;
if strcmpi(tdalabStatus.model,'PMF')
Ynoise=double(Ynoise);
try
[y w]=tdalg(Ynoise,algOptions{algIndex});
catch ME
toc(ts);
algerr;
return;
end
Ycap=ktensor(ones(size(y,2),1),{y,w'});
else
if numel(size(Ynoise))<3
fprintf(2,'Number of modes is less than 3. Please use PMF/BSS model.\n');
tdalab;
return;
end
try
Ycap=tdalg(Ynoise,algOptions{algIndex});
catch ME
toc(ts);
algerr;
return;
end
end
elapsedTime=toc(ts);
set(hmainTDALAB,'HandleVisibility','callback','visible','on');
commandwindow;
clsYcap=class(Ycap);
if strcmpi(clsYcap,'ttensor')
Ycap=ttnormalize(Ycap,0);
elseif strcmpi(clsYcap,'ktensor')
Ycap=ktnormalize(Ycap,0);
end
%%
if ~isempty(Ycap)
fprintf('## Calculating the fitting error. This may cost several minutes depending the data size ...\n');
[fit res]=fitness(Y,Ycap);
fprintf('## Elapsed time: %f sesconds. Fit: %f\n',elapsedTime,fit);
tdalabStatus.decomposed=true;
if (~strcmpi(tdalabStatus.inputType,'tensor'))
% SIRs = cellfun(@CalcSIR,Ycap.U(:),Y.U(:),'uni',0)';
SIRs=NaN(max(NumOfComp),NumOfMode);
if numel(NumOfComp)==1
NumOfComp=NumOfComp(ones(1,NumOfMode));
end
for n=1:NumOfMode
if size(Y.U{n},2)==size(Ycap.U{n},2)
SIRs(1:NumOfComp(n),n)=CalcSIR(Y.U{n},Ycap.U{n});
end
end
if ~all(isnan(SIRs))
SIRs=real(SIRs);
CPdispSIRs(SIRs);
end
end
if strcmpi(tdalabStatus.model,'Tucker')
h=findobj('tag','gbExtraConstraints');
hsel=get(h,'SelectedObject');
consType=get(hsel,'string');
if ~isempty(consType)&&~strcmpi(consType,'none')
fprintf('[TDALAB] Imposing constraints on the mode matrices ... \n');
t=refineTucker;
elapsedTime=elapsedTime+t;
fprintf('[TDALAB] Total time consumption: %f. \n',elapsedTime);
end
end
else
tdalabStatus.decomposed=false;
fprintf('[TDALAB] Mission fails. Please carefully check your settings.\n');
end
case {'Parafac2'}
fprintf('[TDALAB] Algorithm is running. Please wait...\n');
ts=tic;
try
Ycap=tdalg(Ynoise,algOptions{algIndex});
catch ME
toc(ts);
algerr;
return;
end
elapsedTime=toc(ts);
set(hmainTDALAB,'HandleVisibility','callback','visible','on');
commandwindow;
fprintf('[TDALAB] Mission complete. Elapsed elapsedTime is %f seconds.\n',elapsedTime);
if ~isempty(Ycap)
tdalabStatus.decomposed=true;
if ~strcmpi(tdalabStatus.inputType,'tensor')
SIRs(1:NumOfComp(1),1)=CalcSIR(Y.U{1},Ycap.A);
SIRs(1:NumOfComp(1),3)=CalcSIR(Y.U{3},Ycap.C);
SIRs(1:NumOfComp(1),2)=CalcSIR(Y.U{2},Ycap.P{1}*Ycap.H);
CPdispSIRs(SIRs);
end
else
tdalabStatus.decomposed=false;
fprintf('[TDALAB] Mission fails. Please carefully check your settings.\n');
end
case 'BCD'
Ynoise=tensor(Ynoise);
fprintf('[TDALAB] Algorithm is running. Please wait...\n');
ts=tic;
try
Ycap=tdalg(Ynoise,algOptions{algIndex});
catch ME
toc(ts);
algerr;
return;
end
elapsedTime=toc(ts);
set(hmainTDALAB,'HandleVisibility','callback','visible','on');
commandwindow;
if ~isempty(Ycap)
tdalabStatus.decomposed=true;
fprintf('Mission complete. Elapsed elapsedTime is %f seconds.\n',elapsedTime);
disp('--------------------------------------------------------------');
disp('Struct: Ycap.');
disp(Ycap);
else
tdalabStatus.decomposed=false;
end
otherwise
fprintf(2,'[TDALAB] Unsuported tensor decomposition model.\n');
return;
end
if tdalabStatus.decomposed
set(allchild(findobj('tag','pnlOutputAnalysis')),'enable','on');
else
set(allchild(findobj('tag','pnlOutputAnalysis')),'enable','off');
end
commandwindow;
fprintf('\n========================== END ==========================\n\n');
fprintf('\n[TDALAB] Type or click <a href="matlab: tdalab">tdalab</a> to return.\n\n');
function algerr
tdalab;
commandwindow;
fprintf(2,'[TDALAB] Error occured during running the [%s] algorithm:\n',dispname(algs(algIndex).details));
fprintf(2,horzcat('[TDALAB] ',ME.message));
for id=numel(ME.stack):-1:1
disp(ME.stack(id));
end
% disp('[TDALAB] Type or click <a href = "matlab: tdalab">tdalab</a> to return.');
fprintf('\n======================= <a href="matlab: tdalab">TDALAB</a> =======================\n\n');
end
end