-
Notifications
You must be signed in to change notification settings - Fork 0
/
runAnalysis.m
102 lines (93 loc) · 4.2 KB
/
runAnalysis.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
function h = runAnalysis(hObject, eventdata, h)
pars = loadPars;
batchSize = pars.batchSize;
blinksOnly = h.BlinksOnlyCheck.Value;
fps = 0; % prevents an occasional weird bug when updating analysis status text
if hObject.Value
% set h.framesToAnalyze (might not be set correctly)
etGUI('OverwriteCheck_Callback',h.OverwriteCheck, eventdata, h);
h = guidata(h.OverwriteCheck);
nFramesToAnalyze = length(h.framesToAnalyze);
iStart = 1:batchSize:nFramesToAnalyze;
iEnd = min(iStart+batchSize-1, nFramesToAnalyze);
nBatches = length(iStart);
hObject.BackgroundColor = 'red';
hObject.String= 'Stop';
drawnow;
tStart = tic;
iBatch = 1;
while hObject.Value && iBatch<=nBatches
frameIdx = h.framesToAnalyze(iStart(iBatch):iEnd(iBatch));
fprintf('Reading frames from file ..')
ticRead = tic;
frames = readBatch(h.vr, frameIdx);
fprintf('. done (%3.1f seconds)\n', toc(ticRead));
fprintf('Analyzing frames ..')
ticAnalyze = tic;
if ~blinksOnly
params.gaussStd = h.FilterSizeEdit.Value;
params.thresh = h.ThresholdSlider.Value;
xSpan = h.roi(1):sum(h.roi([1, 3]))-1;
ySpan = h.roi(2):sum(h.roi([2, 4]))-1;
res = analyzeBatch(frames(ySpan, xSpan, :), params);
xShift = h.roi(1)-1;
yShift = h.roi(2)-1;
xShiftCell = repmat({xShift}, length(frameIdx), 1);
yShiftCell = repmat({yShift}, length(frameIdx), 1);
h.results.x(frameIdx) = res.x0+xShift;
h.results.y(frameIdx) = res.y0+yShift;
h.results.area(frameIdx) = res.area;
h.results.aAxis(frameIdx) = res.a;
h.results.bAxis(frameIdx) = res.b;
h.results.theta(frameIdx) = res.theta;
h.results.goodFit(frameIdx) = res.isEllipse;
h.results.gaussStd(frameIdx) = params.gaussStd;
h.results.threshold(frameIdx) = params.thresh;
h.results.roi(frameIdx, :) = repmat(h.roi, length(frameIdx), 1);
h.results.equation(frameIdx) = res.eq;
h.results.xxContour(frameIdx) = cellfun(@plus, res.xxEdge, xShiftCell, 'UniformOutput', false);
h.results.yyContour(frameIdx) = cellfun(@plus, res.yyEdge, yShiftCell, 'UniformOutput', false);
h.results.xxEllipse(frameIdx) = cellfun(@plus, res.xxEllipse, xShiftCell, 'UniformOutput', false);
h.results.yyEllipse(frameIdx) = cellfun(@plus, res.yyEllipse, yShiftCell, 'UniformOutput', false);
h.analyzedFrames(frameIdx) = true;
end
[isBlink, blinkRho, blinkMean] = detectBlinkBatch(frames, h);
h.results.blink(frameIdx) = isBlink;
h.results.blinkRho(frameIdx) = blinkRho;
h.results.blinkMean(frameIdx) = blinkMean;
h.results.blinkRoi(frameIdx, :) = repmat(h.blinkRoi, length(frameIdx), 1);
h = updateBlinks(h); % to update the soft blinks
fprintf('. done (%3.1f seconds)\n', toc(ticAnalyze));
tNow = toc(tStart);
fps = iBatch*batchSize/tNow;
tLeft = (nFramesToAnalyze-iBatch*batchSize)/fps;
h.AnalysisStatusText.String = ...
sprintf('%d/%d\t %3.0f fps \t%s left', ...
iEnd(iBatch), nFramesToAnalyze, fps, ...
char(duration(seconds(tLeft), 'Format', 'hh:mm:ss')));
guidata(hObject, h);
drawnow;
iBatch = iBatch + 1;
end
if iBatch>1 % if there actually has been some analysis done
h.AnalysisStatusText.String = ...
sprintf('%d/%d\t %3.0f fps \t%s left', ...
iEnd(iBatch-1), nFramesToAnalyze, fps, ...
char(duration(seconds(tLeft), 'Format', 'hh:mm:ss')));
end
h.framesToAnalyze = [];
h.OverwriteCheck.Value = 0;
hObject.Value = 0;
hObject.BackgroundColor = [0.94 0.94 0.94];
hObject.String= 'Run';
drawnow;
else
hObject.BackgroundColor = [0.94 0.94 0.94];
hObject.String= 'Run';
drawnow;
end
len = sum(h.analyzedFrames);
h.ReplaySlider.Max = len;
h.ReplaySlider.Min = min(1, len);
[~, h.ReplaySlider.Value] = min(abs(find(h.analyzedFrames)-h.iFrame));
h.ReplaySlider.SliderStep = [min(1, 1/len), min(1, 10/len)];