-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluorescenceVideoBrowser.m
280 lines (219 loc) · 11.6 KB
/
fluorescenceVideoBrowser.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
function fig = fluorescenceVideoBrowser(V,mddff0,baselineFrames,frameRate,fovMoves,mddff0Thresholded,smddff0,hulls,masks,centroidBinary,centroidWeighted,maxX,maxY)
sizeMDDFF0 = size(mddff0);
if ndims(mddff0) > 4
mddff0 = reshape(mddff0,[sizeMDDFF0(1:3) prod(sizeMDDFF0(4:end))]);
end
if nargin < 7 || isempty(smddff0) || any(~isfinite(smddff0(:)))
smddff0 = squeeze(sum(sum(mddff0)));
end
framesPerTrial = size(mddff0,3);
nStimuli = size(mddff0,4);
if nargin < 8
hulls = cell(framesPerTrial,nStimuli);
end
if nargin < 9
centroidBinary = nan(framesPerTrial,2,nStimuli);
end
if nargin < 10
centroidWeighted = nan(framesPerTrial,2,nStimuli);
end
if nargin < 12
maxX = nan(framesPerTrial,nStimuli);
maxY = nan(framesPerTrial,nStimuli);
end
fig = figure;
set(fig,'Position',[100 100 400 600]);
subplot('Position',[0.15 0.55 0.8 0.45]);
function updateImage(varargin)
if get(findobj(fig,'Tag','thresholdcheckbox'),'Value')
J = mddff0Thresholded(:,:,frameIndex,stimulusIndex);
else
J = mddff0(:,:,frameIndex,stimulusIndex);
end
set(img,'CData',J);
hull = hulls{frameIndex,stimulusIndex};
if ~isempty(hull)
set(hullPlot,'XData',hull(:,1),'YData',hull(:,2));
end
set(maxPoint,'XData',maxX(frameIndex,stimulusIndex),'YData',maxY(frameIndex,stimulusIndex));
set(cbPoint,'XData',centroidBinary(frameIndex,1,stimulusIndex),'YData',centroidBinary(frameIndex,2,stimulusIndex));
set(cwPoint,'XData',centroidWeighted(frameIndex,1,stimulusIndex),'YData',centroidWeighted(frameIndex,2,stimulusIndex));
end
if nargin < 6 || isempty(mddff0Thresholded) || any(~isfinite(mddff0Thresholded(:)))
isThresholdCheckboxEnabled = 'off';
else
isThresholdCheckboxEnabled = 'on';
end
uicontrol(fig,'Style','checkbox','Tag','thresholdcheckbox','String','Half-max threshold (within frame)','Enable',isThresholdCheckboxEnabled,'Units','normalized','Position',[0.15 0.525 0.8 0.025],'Callback',@updateImage);
frameIndex = baselineFrames(1);
stimulusIndex = nStimuli;
img = imagesc(mddff0(:,:,frameIndex,stimulusIndex));
hold on;
hullPlot = plot(NaN,NaN,'r');
maxPoint = plot(NaN,NaN,'m*');
cbPoint = plot(NaN,NaN,'g+');
cwPoint = plot(NaN,NaN,'gx');
caxis([min(mddff0(:)) max(mddff0(:))]);
daspect([1 1 1]);
set(gca,'XTick',[],'YTick',[]);
updateImage();
ax = subplot('Position',[0.15 0.275 0.8 0.25]);
hold on;
t = ((1:framesPerTrial)-baselineFrames(2))/frameRate;
h = plot(t,100*smddff0/numel(V(:,:,1)));
marker = plot(t(frameIndex),100*smddff0(frameIndex)/numel(V(:,:,1)),'LineStyle','none','Marker','o');
xlim(t([baselineFrames(1) framesPerTrial]));
yy = 100*[min(smddff0(:)) max(smddff0(:))]/numel(V(:,:,1));
ylim(yy);
line([0 0],yy,'Color','k','LineStyle','--');
% legend({'Data' 'Current Frame' 'Stimulus'});
xlabel('Time from stimulus onset (s)');
ylabel('{\Delta}F/F{_0}');
function updateLabelAndSlider(tagPrefix,value)
set(findobj(fig,'Tag',[tagPrefix 'slider']),'Value',value);
set(findobj(fig,'Tag',[tagPrefix 'label']),'String',sprintf('%s%s %d',upper(tagPrefix(1)),lower(tagPrefix(2:end)),value));
end
function update(dimension,value)
index = round(value);
switch dimension
case 1
frameIndex = index;
tagPrefix = 'frame';
case 2
stimulusIndex = index;
tagPrefix = 'stim';
otherwise
errordlg('This should never happen.');
end
updateLabelAndSlider(tagPrefix,index);
updateImage();
set(marker,'XData',t(frameIndex),'YData',100*smddff0(frameIndex,stimulusIndex)/numel(V(:,:,1)));
end
set(ax,'ButtonDownFcn',@(~,event) update(1,event.IntersectionPoint(1)));
set(h,'ButtonDownFcn',@(~,event) update(1,event.IntersectionPoint(1)));
uicontrol(fig,'Style','text','String','Frame 1','Tag','framelabel','Units','normalized','Position',[0.0125 0.175 0.125 0.025]);
uicontrol(fig,'Style','slider','Tag','frameslider','Min',1,'Max',framesPerTrial,'SliderStep',[1 10]./(framesPerTrial-1),'Value',frameIndex,'Units','normalized','Position',[0.15 0.175 0.8 0.025],'Callback',@(slider,varargin) update(1,get(slider,'Value')));
function updateCAxis(varargin)
minSlider = findobj(fig,'Tag','cminslider');
maxSlider = findobj(fig,'Tag','cmaxslider');
cmin = round(get(minSlider,'Value'));
cmax = round(get(maxSlider,'Value'));
caxis(get(img,'Parent'),[cmin cmax]/1000);
set(minSlider,'Value',cmin,'Max',cmax-1,'SliderStep',[1 1]./(cmax-1-fmin));
set(maxSlider,'Value',cmax,'Min',cmin+1,'SliderStep',[1 1]./(fmax-cmin-1));
set(findobj(fig,'Tag','cminslider'),'String',sprintf('CMin %d',cmin/1000));
set(findobj(fig,'Tag','cmaxslider'),'String',sprintf('CMax %d',cmax/1000));
end
fmin = round(1000*min(mddff0(:)));
fmax = round(1000*max(mddff0(:)));
if nStimuli > 1
uicontrol(fig,'Style','text','String',sprintf('Stim %d',stimulusIndex),'Tag','stimlabel','Units','normalized','Position',[0.0125 0.15 0.125 0.025]);
uicontrol(fig,'Style','slider','Tag','stimslider','Min',1,'Max',nStimuli,'SliderStep',[1 1]./(nStimuli-1),'Value',stimulusIndex,'Units','normalized','Position',[0.15 0.15 0.8 0.025],'Callback',@(slider,varargin) update(2,get(slider,'Value')));
end
uicontrol(fig,'Style','text','String',sprintf('CMin %d',fmin/1000),'Tag','cminlabel','Units','normalized','Position',[0.0125 0.125 0.125 0.025]);
uicontrol(fig,'Style','slider','Tag','cminslider','Min',fmin,'Max',fmax-1,'SliderStep',[1 1]./(fmax-1-fmin),'Value',fmin,'Units','normalized','Position',[0.15 0.125 0.8 0.025],'Callback',@updateCAxis);
uicontrol(fig,'Style','text','String',sprintf('CMax %d',fmax/1000),'Tag','cmaxlabel','Units','normalized','Position',[0.0125 0.1 0.125 0.025]);
uicontrol(fig,'Style','slider','Tag','cmaxslider','Min',fmin+1,'Max',fmax,'SliderStep',[1 1]./(fmax-fmin-1),'Value',fmax,'Units','normalized','Position',[0.15 0.1 0.8 0.025],'Callback',@updateCAxis);
roiPosition = [NaN NaN NaN NaN];
function chooseROI(varargin)
roi = imrect(get(img,'Parent'));
if isempty(roi)
return
end
roiPosition = getPosition(roi);
delete(roi);
end
uicontrol(fig,'Style','pushbutton','String','Choose ROI','Tag','chooseroibutton','Units','normalized','Position',[0.05 0.025 0.2 0.025],'Callback',@chooseROI);
function highlightROI(varargin)
isUserSpecifiedROI = ~isnan(roiPosition(1));
pxpmm = 2.020758017492712e+02/16;
figure;
set(gcf,'Position',[1000 118 560*numel(fovMoves) 840]); %1120 840]);
cols = numel(fovMoves);
yyrois = [Inf -Inf];
for hh = 1:numel(fovMoves)
if hh == numel(fovMoves)
stimulusIndices = fovMoves(hh):nStimuli;
else
stimulusIndices = fovMoves(hh):(fovMoves(hh+1)-1);
end
subplot(2,cols,hh);
imagesc(V(:,:,hh));
colormap(gray);
daspect([1 1 1]);
hold on;
if isUserSpecifiedROI
fill(roiPosition(1)+[0 0 roiPosition(3) roiPosition(3)],roiPosition(2)+[0 roiPosition(4) roiPosition(4) 0],[0 0 0],'EdgeColor','g','FaceColor','none');
else
set(gca,'ColorOrderIndex',stimulusIndices(1));
for ii = stimulusIndices
if isempty(hulls{frameIndex,ii})
plot(NaN,NaN); % plot ignores empty arrays, so plots NaNs instead to keep the ColorOrder consistent and create a fake handle in the plot browser
else
plot(hulls{frameIndex,ii}(:,1),hulls{frameIndex,ii}(:,2));
end
end
end
scaleBar = fill([0 1 1 0]*pxpmm+2,[47 47 46 46],[1 1 1],'EdgeColor','none');
scaleText = text(2+pxpmm/2,46,'1 mm','Color',[1 1 1],'FontSize',8,'FontWeight','bold','HorizontalAlignment','center','VerticalAlignment','bottom');
horizontalArrow = annotation('arrow',[0.01 0.11],[0.06 0.06],'Color',[1 1 1],'LineWidth',2);
verticalArrow = annotation('arrow',[0.06 0.06],[0.01 0.11],'Color',[1 1 1],'LineWidth',2);
subplot(2,cols,numel(fovMoves)+hh);
if isUserSpecifiedROI
roiX = max(1,min(64,round(roiPosition(1)+(0:roiPosition(3)-1))));
roiY = max(1,min(48,round(roiPosition(2)+(0:roiPosition(4)-1))));
mask = ones(size(V(:,:,1)));
N = numel(roiX)*numel(roiY);
else
roiX = 1:size(V,2);
roiY = 1:size(V,1);
mask = cat(4,masks{frameIndex,stimulusIndices});
N = sum(sum(mask));
end
hs = gobjects(numel(stimulusIndices)+4,1);
smddff0roi = squeeze(bsxfun(@rdivide,sum(sum(bsxfun(@times,mddff0(roiY,roiX,baselineFrames(1):end,stimulusIndices),mask))),N));
hold on
set(gca,'ColorOrderIndex',stimulusIndices(1));
hs(1:numel(stimulusIndices)) = plot(t(baselineFrames(1):end),100*smddff0roi);
set(gca,'ColorOrderIndex',stimulusIndices(1));
plot(t(baselineFrames(1):end),100*smddff0(baselineFrames(1):end,stimulusIndices)/numel(V(:,:,1)),'LineStyle',':');
plot(t(frameIndex),100*smddff0roi(frameIndex-baselineFrames(1)+1),'Color','k','LineStyle','none','Marker','o');
hs(end-3) = plot(NaN,NaN,'Color','k');
hs(end-2) = plot(NaN,NaN,'Color','k','LineStyle',':');
yyroi = ylim;
hs(end-1) = line([0 0],[-100 100],'Color','k','LineStyle','--');
hs(end) = line([1 1]*t(frameIndex),[-100 100],'Color','k','LineStyle','-.');
legend(hs,[arrayfun(@(ii) sprintf('Stim %d',ii),stimulusIndices,'UniformOutput',false) {'ROI' 'Whole Image' 'Stim Onset' 'Response Time'}],'Location','NorthWest');
xlabel('Time from stimulus onset (s)');
xlim(t([baselineFrames(1) framesPerTrial]));
ylabel('{\Delta}F/F{_0} (%)');
yyrois(1) = min(yyrois(1),yyroi(1));
yyrois(2) = max(yyrois(2),yyroi(2));
% a1 = subplot(2,2,2);
% imagesc(mddff0(:,:,frameIndex,stimulusIndex));
% daspect([1 1 1]);
% % colormap(jet);
% cc1 = caxis(a1);
% copyobj(outline,a1);
% copyobj(scaleBar,a1);
% copyobj(scaleText,a1);
%
% a2 = subplot(2,2,4);
% imagesc(mddff0(:,:,frameIndex,1));
% daspect([1 1 1]);
% % colormap(jet);
% cc2 = caxis(a2);
% copyobj(outline,a2);
% copyobj(scaleBar,a2);
% copyobj(scaleText,a2);
%
% cc = [min(cc1(1),cc2(1)) max(cc1(2),cc2(2))];
% caxis(a1,cc);
% caxis(a2,cc);
end
for hh = 1:numel(fovMoves)
ylim(subplot(2,cols,numel(fovMoves)+hh),yyrois);
end
end
uicontrol(fig,'Style','pushbutton','String','Highlight ROI On First Frame','Tag','highlightroibutton','Units','normalized','Position',[0.3 0.025 0.5 0.025],'Callback',@highlightROI);
end