-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvalGui.m
executable file
·380 lines (330 loc) · 11.9 KB
/
EvalGui.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
classdef EvalGui < handle
properties
fig;
mtree;
StatusQueue=struct([]);
Interface=struct();
Log=fullfile(pwd,'AutoTipTrack_log.txt');
end
methods
%% queue handling
function E=addTree(E,folder,varargin)
if nargin>0
% create a new GUI window
disp('creating GUI');
E.fig=figure('DockControls','off','IntegerHandle','off','MenuBar','none','Name',...
'evaluateManyExperiments - Status','NumberTitle','off','Tag','hEvalGui');
fPlaceFig(E.fig,'big');
set(E.fig,'Units','pixels');
warning('off','MATLAB:uitreenode:DeprecatedFunction');
if ischar(folder)
%we assume folder is a path and need to make sure folder ends
%with a filesep otherwise uitree cannot expand subdirectories
if ~strendswith(folder,filesep)
folder=[folder filesep];
end
end
%create the uitree either from the folder node or from the path
E.mtree = uitree('v0', 'Root', folder);
E.mtree.expand(E.mtree.Root)
drawnow;
if nargin < 3
p=get(E.fig,'pos');
E.mtree.Position(3)=p(3);
else
E.Interface = varargin;
E.mtree.NodeSelectedCallback=@(mtree,value)EvalGui.selectNode(mtree,value,E);
E.mtree.setSelectedNode(E.StatusQueue(1).Node)
end
else
end
end
function t=watchProgress(E)
disp('starting refresh timer');
t = timer('StartDelay',0.5,'TimerFcn',@E.refreshProgress,'StopFcn',@E.timerStopped,'Period',4,'TasksToExecute', Inf,'Tag','AutoTipTrack:EvalGui:ProgressTimer',...
'ExecutionMode','fixedRate','BusyMode','queue','UserData',E);
start(t)
end
function E=enqueueStatusFolder(E,QueueEl,node)
E.StatusQueue=[E.StatusQueue struct('QueueElement',QueueEl,'Node',node,'TotalTime',0)];
end
function E=refreshFolder(E,queuePos)
status='progress';
if exist(E.StatusQueue(queuePos).QueueElement.StatusFolder,'file')==7
files = dir(fullfile(E.StatusQueue(queuePos).QueueElement.StatusFolder , '*.mat'));
n=length(files);
%if n==0 we don't have status files yet and don't have anything to do
if n>0
w=20; %the width of the progress bar
[curTime, id, ~, maxNum, ~, message] = E.getInfo(files(end).name);
notFound=true;
%now we try to find the first file with the same id as the current file
while notFound
info=strsplit(files(n).name,'_','CollapseDelimiters',false);
if ~strcmp(id,info{3})
notFound=false;
n=n+1;%go back to the last file that had the same id
elseif n<2
%if n=1 we only have one kind of status file and files(1) is
%the first file with that id
notFound=false;
else
n=n-1;
end
end
%get the frequency from the first file since we sometimes change
%the frequency to 1 for the last frame to make sure we always
%catch the last frame
[startTime,~,~,~,freq,~] = E.getInfo(files(n).name);
filecount=length(files)-n+1;
doneNum=(filecount-1)*freq;%the files are created at the start of the loop so we subtract one here.
if doneNum>=maxNum %if we are done
elapsed=etime(curTime,startTime);
ETR=E.sec2timestr(elapsed);
ETR_str='ElT:';
bar=E.progBar(w,100);
doneNum=maxNum;
status='done';
firstTime=E.getInfo(files(1).name);
E.StatusQueue(queuePos).TotalTime=etime(curTime,firstTime);
else
elapsed=etime(curTime,startTime);
timeleft = elapsed*maxNum/doneNum - elapsed;
ETR_str='ETR:';
percent=doneNum/maxNum*100;
bar=E.progBar(w,percent);
ETR=E.sec2timestr(timeleft);
end
msg=sprintf('%s: %s %d/%d %s %s | %s',id,bar,doneNum,maxNum,ETR_str,ETR,message);
E.renameNode(E.StatusQueue(queuePos).Node,msg,status);
E.mtree.reloadNode(E.StatusQueue(queuePos).Node);
end
else
E.stopWatchingFolder(queuePos);
end
end
function E=stopWatchingFolder(E,queuePos)
if E.StatusQueue(queuePos).QueueElement.Success
status='done';
msg='Analysis finished.';
else
if isempty(E.StatusQueue(queuePos).QueueElement.Message)
%if we don't have an error message, we are likely using
%monitorProgress, let's try to figure out if tracking was already
%successful by loading the results file
[status,msg]=E.testSuccess(queuePos);
else
status='error';
msg=['Error: ', E.StatusQueue(queuePos).QueueElement.Message];
end
end
ETR=E.sec2timestr(E.StatusQueue(queuePos).TotalTime);
msg=sprintf('Total ElT: %s. %s',ETR,msg);
E.renameNode(E.StatusQueue(queuePos).Node,msg,status);
E.mtree.reloadNode(E.StatusQueue(queuePos).Node);
drawnow;
if length(E.StatusQueue) > queuePos
E.StatusQueue(queuePos)=[];
end
end
function [status,msg]=testSuccess(E,queuePos)
if ~E.StatusQueue(queuePos).QueueElement.reloadResults ...
&& E.StatusQueue(queuePos).QueueElement.Config.Tasks.Connect...
|| ~E.StatusQueue(queuePos).QueueElement.reloadResults('Objects')...
&& E.StatusQueue(queuePos).QueueElement.Config.Tasks.Track
status='error';
msg='Warning: No tracks found. An error may have occurred, check the log files.';
else
status='done';
msg='Analysis finished.';
end
end
function E=setRootNode(E,rootNode)
E.mtree.setRoot(rootNode)
end
function E=addUitree(E,rootNode)
E.mtree = uitree('v0', 'Root', rootNode);
end
function expandAllNodes(E,node)
if nargin<2
node=E.mtree.getRoot;
end
if ~node.isLeafNode
E.mtree.expand(node);
for n=1:node.getChildCount
child=node.getChildAt(n-1);
E.expandAllNodes(child);
end
end
end
function [found,node]=findNode(E,path,node)
if nargin<3
node=E.mtree.getRoot;
end
found=false;
nodePath=node.getValue;
% a filesep at the end of either path does not matter. Make sure we
% match the node even if one has a fielesep at the end and the other
% does not.
if strendswith(path,filesep)
path=path(1:end-1);
end
if strendswith(nodePath,filesep)
nodePath=nodePath(1:end-1);
end
%if the paths match, we have found our node
if strcmp(path,nodePath)
found=true;
return;
end
%if node is not a leafnode, we recursively search through the child
%nodes
if ~node.isLeafNode
for n=1:node.getChildCount
child=node.getChildAt(n-1);
[found,child]=E.findNode(path,child);
if found
node=child;
return;
end
end
end
end
function queuePos=findQueuePos(E,path)
queuePos=[];
for n=1:length(E.StatusQueue)
if strcmp(E.StatusQueue(n).Node.getValue,path)
queuePos=n;
end
end
end
function E=reenqueueForRefresh(E,QueueEl,path)
[found,node]=findNode(E,path);
if ~found
error('MATLAB:AutoTipTrack:EvalGui:reenqueuePath','No node found that corresponds to path: %s',path)
end
E.enqueueStatusFolder(QueueEl,node);
end
function E=refresh(E)
%go through the status queue in reverse order because some queue
%elements might be deleted by refreshFolder.
for n=length(E.StatusQueue):-1:1
E.refreshFolder(n);
end
drawnow;
end
%% closing UI
function close(E,~,~)
if ishandle(E.fig)
close(E.fig);
end
E.mtree.NodeSelectedCallback=@(mtree,value)true; %need to reset the callback to avoid a circular dependency between EvalGui and mtree;
clear('E');
end
function closeFig(E,~,~)
close(E.fig);
end
end
methods (Static)
function renameNode(node, msg,status)
persistent progressnum;
switch status
case 'done'
color='green';
jImage = java.awt.Toolkit.getDefaultToolkit.createImage(which('Done_16px.png'));
case 'progress'
if isempty(progressnum)||progressnum==1
progressnum=12;
else
progressnum=progressnum-1;
end
imagename=sprintf('progress_%d.png', progressnum);
color='orange';
jImage = java.awt.Toolkit.getDefaultToolkit.createImage(which(imagename));
otherwise
color='red';
jImage = java.awt.Toolkit.getDefaultToolkit.createImage(which('warning_16.png'));
end
oldname=strsplit(char(node.getName),'<html>');
if length(oldname)>1
nameparts=strsplit(oldname{2},'<font');
name=nameparts{1};
else
name=oldname{1};
end
node.setName([ '<html>', name, '<font face="Courier" color="', color, '"> ', msg, '</font></html>']);
node.setIcon(jImage);
end
function refreshProgress(obj, ~)
E=get(obj,'UserData');
if length(E.StatusQueue)<1
drawnow;
stop(obj);
return;
end
E.refresh;
end
function timerStopped(obj, ~)
delete(obj);
end
function bar=progBar(w,percent)
scale=w/100;
done=floor(percent*scale);
todo=w-done;
if todo==0
sep='=';
else
sep='>';
end
bar=['[',repmat('=',1,done),sep,repmat(' ',1,todo),']'];
end
function [dateTime, id, curN, maxNum, freq, message] = getInfo(filename)
info=strsplit(filename,'_','CollapseDelimiters',false);
date=info{1};
time=info{2};
dateTime=datevec([date,'_', time],'yyyy-mm-dd_HH-MM-SS-FFF');
id=info{3};
curN=str2double(info{4});
maxNum=str2double(info{5});
freq=str2double(info{6});
message=info{7};
end
function timestr = sec2timestr(sec)
if isnan(sec)||isinf(sec)
timestr='unknown ';
else
h = floor(sec/3600); % Hours
sec = sec - h*3600;
m = floor(sec/60); % Minutes
sec = sec - m*60;
s = floor(sec); % Seconds
timestr = sprintf('%02d:%02d:%02d',h,m,s);
end
end
function node=createNode(path,isFolder)
[~,name,ext]=fileparts(path);
if isFolder
jImage = which('folder_16.png');
else
jImage = which('image_16.png');
end
node=uitreenode('v0',path,[name ext],jImage,~isFolder);
end
function selectNode(mtree,~,E)
Selected=mtree.getSelectedNodes;
if ~isempty(Selected)
Path=Selected(1).getValue;
QueuePos=E.findQueuePos(Path);
Interactive = InteractiveGUI(E.StatusQueue(QueuePos).QueueElement.Config.exportConfigStruct);
Interactive.StatusFolder = E.StatusQueue(QueuePos).QueueElement.StatusFolder;
Interactive.Stack = E.StatusQueue(QueuePos).QueueElement.Stack;
Interactive.setupInteractivePanel(E.Interface{:});
uiwait(Interactive.UIFig);
if isempty(E.StatusQueue(QueuePos).QueueElement.Stack) && ~isempty(Interactive.Stack)
E.StatusQueue(QueuePos).QueueElement.Stack = Interactive.Stack;
end
clear('Interactive');
end
end
end
end