-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyzeIntensityGUI_hold_CoV.m
507 lines (457 loc) · 19.9 KB
/
analyzeIntensityGUI_hold_CoV.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
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
function [ ] = analyzeIntensityGUI( intavgNPCorr, outputName)
% analyzeIntensity converts a matrix into it's deltaF over avgF form,
% graphs the result, and analyzes the data
% analyzeIntensityGUI( fakeData, 'fakeDataTest')
%% Defining initial variables
ignoreInitialSpiking = false; % Line 140 to change analyzable part
% Multiplied by negative standard deviation is the threshold for firing
% threshold_factor MINIMUM value is 7, otherwise breaks down
% threshold_factor MAXIMUM value is 10, otherwise large data loss
threshold_factor = 7.0; % threshold = std_neg * threshold_factor;
%%% Any especially noisy neurons above this threshold are labelled as not
%%% firing
stdCutoff = 0.053;
stdCutoff = 0.09;
%%% Removes "cells" with an average intensity below a threshold
%%% indicating that it might be just dark neuropil
darknessThreshold = 600; % Above 1000 seems to strongly indicate a real cell
%darknessThreshold = 0;
% Will run intensity data through a high pass filter
High_Pass_Filter = false;
Exponential_Fit = true;
initialTotalCells = length(intavgNPCorr(:,1));
std_negVals = ones(initialTotalCells,1);
fps = 3.91; % frames per second of the movie
removed_cells = zeros(length(intavgNPCorr(:,1)),1);
frames = length(intavgNPCorr(1,:));
% This designates what kind of HP filter you use
hpFilt = designfilt('highpassiir','FilterOrder',8, ...
'PassbandFrequency',0.0001,'PassbandRipple',0.1, ...
'SampleRate',3.81);
meanIntensities = zeros(initialTotalCells,1);
%% Indicates what cells are too dark to be considered cells
r=1;
while r < length(intavgNPCorr(:,1))
darkLevel = mean(intavgNPCorr(r,:));
if darkLevel < darknessThreshold
removed_cells(r) = 1;
%fprintf('Removing a cell, too dark, light level: %d\n',darkLevel);
fprintf('Removing a cell, too dark, light level: %d\n',darkLevel);
end
brightness_vals(r) = darkLevel;
r=r+1;
end
fprintf('Avg cell brightness: %d\n', mean(brightness_vals));
%% Fitting function, Calculating Delta F over F, Indicates noisy cells
h = waitbar(0,'Finding DF/F and fitting to exponential for each cell');
%%% Intensity data is intavgNPCorr but with problematic cells removed
intensityData = intavgNPCorr;
%%% Two term exponential fit to the data
x = linspace(1,frames,frames);
% Applies the exponential fit to the data and indicates noisy cells
r=1; % r===cell#
while r <= initialTotalCells
if Exponential_Fit
%%% Exponential fitting function
cellIntensity = intensityData(r,:);
x = (1:1:size(cellIntensity'));
f = fit(x',cellIntensity','exp2','Start',[1000,-0.001,3500,0.0001]);
meanIntensities(r) = mean(intensityData(r,:));
intensityDataLinearFit = zeros(initialTotalCells,frames);
for c=1:frames % Calculates DeltaF/F using exponential fit as avg
intensityDataLinearFit(r,c) = (intensityData(r,c)-meanIntensities(r))/meanIntensities(r);
% UNUSED
end
for c=1:frames % Calculates DeltaF/F using exponential fit as avg
intensityData(r,c) = (intensityData(r,c)-f(c))/f(c);
end
else
%intensityData(r,:) = 2*intensityData(r,:)/max(intensityData(r,:));
end
%%% Forces the average to be 0
intensityData(r,:)=intensityData(r,:)-mean(intensityData(r,:));
%%% Remove all "cells" with particularly large standard deviations
A = intensityData(r,:);% For some reason 'A' is needed
std_neg = std(A(A<0));% Negative Std Dev of the signal
std_negVals(r) = std_neg;
if std_neg>stdCutoff
removed_cells(r) = 1;
fprintf('Removing a cell, too noisy, -std dev: %d\n',std_neg);
end
r=r+1;
waitbar(r/initialTotalCells)
end
close(h)
fprintf('Avg brightness negative std deviation: %d\n', mean(std_negVals));
%% Applies a high pass filter onto each cell
%%% Test which filter is most optimal with /.TestFiles/FilterTestRealData.m
if High_Pass_Filter
h = waitbar(0,'Putting each cell signal through a High Pass Filter');
% hpfilt defined earlier, holds filter information
% fvtool(hpFilt) % Displays the properties of the filter
for r=1:initialTotalCells % r===cell#
%dataMir = [-fliplr(intensityData(r,:)), intensityData(r,:), -fliplr(intensityData(r,:))];
%dataMir = filter(hpFilt,dataMir);
%size(dataMir(floor(frames):floor(frames*2)-1))
%size(intensityData(r,:))
%intensityData(r,:) = dataMir(floor(frames):floor(frames*2)-1);
intensityData(r,:) = filter(hpFilt,intensityData(r,:));
waitbar(r/initialTotalCells)
end
close(h)
end
%% Removes cells marked as unusable or fake
%%% Intensity data is intavgNPCorr but with problematic cells removed
intensityData = intensityData(~removed_cells,:);
totalCells = length(intensityData(:,1));
std_negVals = std_negVals(~removed_cells);
brightness_vals = ones(length(intavgNPCorr(:,1)),1);
fprintf('Removed %d cells.\n', length(intavgNPCorr(:,1))-totalCells);
%% Defining variables used for calculations and initializes UI
assignin('base', 'Intensity_DeltaF', intensityData);
firedNeurons=[]; % An array that holds each firing cell's number
firingThresholds=[]; % The minimum threshold the signal must reach
firingTimes=[]; % Holds the frame of every cell that fires
%%% firingTimes is weird. It holds both the cell numbers and their firing times.
%%% #>1 is cell number, followed by (1/frameFired). All numbers greater
%%% than one is a cell number and every number less than one is the
%%% inverted frame on which it fired
% BinaryFiring is a matrix, cell activity on a frame is marked with "1"
binaryFiring = zeros(totalCells,frames);
cellPlot = 1;
numCellPlots = 1;
%%% Coefficients of Variation for each cell (Std_Dev/Mean)
CoVs = ones(totalCells,1);
slmin=4.5;slmax=10;sliderVal=threshold_factor;
f = figure('KeyPressFcn',@keypress,'units','pixels',...
'position',[500 500 600 600],...
'menubar','none',...
'name','Ca Code',...
'numbertitle','off',...
'resize','off');
uicontrol('Style','slider','Callback',@sliderCallback,'Min',slmin,'Max',slmax,...
'SliderStep',[.5 .5]./(slmax-slmin),'Value',sliderVal,...
'Position',[5 5 200 20],'Parent',f);
uicontrol('style','push','unit','pix',...
'position',[620 5 80 20],...
'fontsize',12,'fontweight','bold',...
'string','REMOVE','callback',@button_call,'Parent',f);
uicontrol('style','pop','unit','pix',...
'position',[520 5 80 20],'fontsize',12,...
'fontweight','bold','string',[0,1,2,3,4,5],...
'value',1,'Callback',@dropdownCallback,'Parent',f);
Calculate_Events();
global cellRemove;
global iStart;
global iEnd;
%% Handles every keypress
function keypress(src, evt)
val = double(get(f,'CurrentCharacter'));
switch val
case 28 % <-
if cellPlot <= 1
fprintf('Already on first plot\n');
cellPlot=1;
else
cellPlot=cellPlot-1;
Intensity_DeltaF_Plotter();
end
init_ui();
case 29 % ->
if cellPlot >= numCellPlots
fprintf('Already on last plot\n');
else
cellPlot=cellPlot+1;
Intensity_DeltaF_Plotter();
end
init_ui();
otherwise
fprintf('Button not recognized, value is: %d\n',val);
end
end
%% This function iterates through every point looking for firing events
function Calculate_Events()
%%% Initializing variables to be used for each cell
amplitudes = [];
number_of_events = 0;
avgEventSizes = [];
firedNeurons = [];
firingThresholds = [];
avgEventSizes = [];
firingTimes = [];
%%% For every segmented cell (cell i) run intensity analysis
%%% cell i
for i=1:length(intensityData(:,1))%%% Data analysis
cellFireTimes = (i); % Keeps track of the times at which the cell fires
A = intensityData(i,:);
threshold = std_negVals(i)*threshold_factor;
event_vals=[]; % An array holding every single event size
%%%[FOR THE LAST CELL THIS CODE RUNS IMPROPERLY FOR SOME REASON]
startFrame = 5;
if ignoreInitialSpiking
startFrame = round(length(A)/8);
end
%%% Registers an event only if a value above the threshold is preceded by a value below it
%%% frame ii
for ii=startFrame:(length(A)-2)
%%% The following if statement will determine if a neuron has fired
if (A(ii)<threshold) && (A(ii+1)>threshold) && (A(ii+2)>0) && std_negVals(i)<stdCutoff
number_of_events=number_of_events+1;
cellFireTimes = [cellFireTimes,1/(ii+1)];
amplitudes(number_of_events) = A(ii+1);
iii=ii+1;
%%% Calculating size of the event, add until it is below the threshold
%%% subframe iii
while (A(iii)>threshold/3 && iii<length(A))
event_vals=[event_vals, A(iii)];
binaryFiring(i,iii)=1;
iii=iii+1;
end
%%% 800ms x 3.91fps =~= 3 frames, events cannot be closer
% ii=ii+round(.8*fps);
ii=ii+2; % Events CAN be closer but we skip a lil ahead
end
end
if ~isempty(event_vals)
firedNeurons=[firedNeurons,i];
firingThresholds=[firingThresholds,threshold];
avgEventSizes=[avgEventSizes,sum(event_vals)/number_of_events];
firingTimes = [firingTimes,cellFireTimes];
end
if isempty(event_vals)
firingThresholds=[firingThresholds,0];
end
end
%% Calculating real data
%%% Prints out some info
% DATA
% totalCells activeCells number_of_events std_neg
% avgEventSizes() event_vals() intensityData(firedNeurons(i),:) firedNeurons()
% std_negVals() amplitudes() firingTimes()
% seconds minutes
% number_of_events
binaryFiringAll = binaryFiring;
%%% binaryFiring only keeps cells that have fired.
binaryFiring(all(binaryFiring==0,2),:)=[];
seconds = frames/fps;
minutes = seconds/60;
activeCells=length(firedNeurons);
events_per_active_cell=number_of_events/activeCells;
totalEvents = length(firingTimes(firingTimes<1));
avgStdDev = mean(std_negVals);
% Variables filled inside the following loop
activeCellsEachMin = zeros(1,ceil(minutes));
eventsEachMin = zeros(1,ceil(minutes));
%%% This loop is for calculating % active cells per minute
index = 1; %%% You need to go minute by minute and average the result
while index < length(firingTimes)
if firingTimes(index) >= 1 % if it contains a number >1 than it's the cell #
cellNum = firingTimes(index);
% Binary flag if cell was active during a minute
singleActivePerMin = zeros(1,ceil(minutes));
subindex = index+1;
while firingTimes(subindex)<1 && subindex<length(firingTimes)
fireTime = round(1/firingTimes(subindex));
minuteIndex = ceil((fireTime / fps)/60);
singleActivePerMin(minuteIndex) = 1; % Marks cell being active during this minute
eventsEachMin(minuteIndex) = eventsEachMin(minuteIndex) + 1; % Adds each event
subindex=subindex+1;
end
activeCellsEachMin = activeCellsEachMin + singleActivePerMin;
index = subindex;
end
end
eventsEachMin; %%% Calculated
activeCellsEachMin; %%% Calculated
leftoverMinute = minutes - floor(minutes); % what fraction of a minute is left over
%%% Now we combine the data and calculate percent active cells from the
%%% active cells for each minute, last minute is weighted less than the
avgActiveCellsPerMin = 0; %%% others based on leftoverMinute
for i=1:ceil(minutes)
if i == ceil(minutes)
% on last minute, average together the previous minutes and
% then add the less-weighted last minute data
avgActiveCellsPerMin = (avgActiveCellsPerMin+activeCellsEachMin(i)*...
(leftoverMinute))/minutes;
percentActiveCells = 100 * avgActiveCellsPerMin / totalCells;
else
% add together if not on last minute
avgActiveCellsPerMin = avgActiveCellsPerMin + activeCellsEachMin(i);
end
end
% avgActiveCellsPerMin bins minute by minute and so is always a lower bound
avgActiveCellsPerMin; %%% Calculated
% Mean intensity should be very close to zero (noise) but positive
avgIntensity = mean(mean(intensityData));
disp(' ')
% Allows us to print data to a text file
dataFile = strcat(outputName,'.txt');
fileID = fopen( dataFile,'w');
fprintf(fileID, ['Movie is ',num2str(minutes),' minutes.','\n']);
fprintf(fileID, ['Threshold factor is ',num2str(threshold_factor),'\n']);
fprintf(fileID, ['Total events: ',num2str(number_of_events),'\n']);
fprintf(fileID, ['Mean intensity for all cells: ', num2str(avgIntensity),'\n']);
fprintf(fileID, ['Average negative std deviation: ', num2str(avgStdDev),'\n']);
fprintf(fileID, ['Lowest and Highest Neg.Std.Dev: ', num2str(min(std_negVals)), ...
', ', num2str(max(std_negVals)),'\n']);
fprintf(fileID, ['Size of negative std deviation divided mean intensity: ',...
num2str(avgStdDev/avgIntensity),'\n']);
fprintf(fileID, ['Percent active cells: ',num2str((100*activeCells/totalCells)),'\n']);
fprintf(fileID, ['Percent active cells per minute: ',num2str(percentActiveCells),'\n']);
fprintf(fileID, ['Number of events per minute: ',num2str((number_of_events)/minutes),'\n']);
fprintf(fileID, ['num events per minute per active cell: ',num2str(...
(number_of_events/activeCells)/minutes),'\n']);
fprintf(fileID, ['Avg active cells per minute (lower bound): ',...
num2str(avgActiveCellsPerMin),'\n']);
fprintf(fileID, ['Avg amplitude per event: ',num2str(mean(amplitudes)),'\n']);
fprintf(fileID, ['Lowest and Highest amplitude: ', num2str(min(amplitudes)),...
', ', num2str(max(amplitudes)),'\n']);
fprintf(fileID, ['Avg area under curve per event: ',num2str(mean(avgEventSizes)),'\n']);
%%% Close the file using fclose when you finish writing.
fclose(fileID);
%type dataFile
std_negVals;
meanIntensities;
%%% A high Coefficient of Variation implies that the cell is extremely
%%% noisy (has comparably large std_dev compared to baseline mean intensity)
disp('Coefficients of Variation [CoV] for every cell (x1000)')
size(std_negVals)
size(meanIntensities)
% for c = 1:totalCells
% CoV = std_negVals(c)./meanIntensities(c);
% disp([' Cell ',num2str(c),' CoV: ',num2str(1000*100*CoV)])
% end
for c = 1:totalCells
CoVs(c) = 1000*100*std_negVals(c)./meanIntensities(c);
%disp([' Cell ',num2str(c),' CoV: ',num2str(1000*100*CoVs(c))]);
end
disp([' Avg CoV: ',num2str(mean(CoVs)),' StdDev CoV: ',num2str(std(CoVs))]);
numCellPlots = ceil(length(firedNeurons)/10);
if length(firedNeurons)~=0
Intensity_DeltaF_Plotter();
init_ui();
saveas(gcf,outputName)
end
end
%% Plotting code
function Intensity_DeltaF_Plotter()
%INTENSITY_DELTAF_PLOTTER plots deltaF intensityData
iStart = 1 + (cellPlot-1)*10;
iEnd = min([iStart+9,length(firedNeurons)]);
numPlots=length(firedNeurons(iStart:iEnd));
cellRemove = firedNeurons(iStart);
% plotting the actual data
hold off; % clear remaining figures
clf
inc = 1;
for i=iStart:iEnd
plot( intensityData(firedNeurons(i),:)+(inc-1),'linewidth',1.1)
inc = inc+1;
hold on;
end
% done plotting data
% code for plotting a horozontal line
L_min = 0;
L_max = length(intensityData(1,:));
% L_min = L_max/12;
y = 0; % constant y value
d = 1; % direction
r = 15;% y range
a = linspace(L_min, L_max, 2);
b = linspace(y, y, 2);
plot(a,b,'--k','linewidth',1.5)
for i=1:((numPlots)*2-1)
y = i/2;
if mod(i,2)==1%%% If this horozontal line is a threshold
%y = round(y-0.5)+firingThresholds(round(i/2));
neuronNum = firedNeurons(iStart+round(i/2)-1);
y = (i-1)/2+firingThresholds(neuronNum);
a = linspace(L_min, L_max, 2);
b = linspace(y, y, 2);
plot(a,b,'--k','linewidth',1.2)
else%%% If line is an avg
a = linspace(L_min, L_max, 2);
b = linspace(y, y, 2);
plot(a,b,'--k','linewidth',0.8)
end
end
% done plotting horozontal lines
% Should eliminate white space from the plot (only works w/1 plot)
%set(gca,'LooseInset',get(gca,'TightInset'))
% Chooses range of the plot
axis([0,L_max,-0.5,numPlots-0.2])
% Sets the y tick labels
% set(gca,'YTick',(iStart-1):(iEnd-1),'YTickLabel',firedNeurons)
set(gca,'YTick',0:numPlots-1,'YTickLabel',firedNeurons(iStart:iEnd) )
title(['Cell Intensities with Firing Thresholds (factor=',...
num2str(threshold_factor),')'])
ylabel('Cell Number')
xlabel('Bottom: Frames, Top: Seconds')
% This chunk will create a second set of Axis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax1_pos = get(gca,'Position'); % position of first axes
ax2 = axes('Position',ax1_pos,'XAxisLocation','top',...
'YAxisLocation','right','Color','none');
% sets the y tick labels
set(ax2,'YTick',0:1,'YTickLabel',['' ''])
seconds=round(length(intensityData(1,:))/3.91);
set(ax2,'XTick',0:10,'XTickLabel',[0 seconds])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x0 = 500; y0 = 0;
width = 800; height = 600;
set(gcf,'units','points','position',[x0,y0,width,height])
end
function sliderCallback(src, evt)
sliderVal=get(src, 'Value');
fprintf('Slider value is: %d\n', sliderVal );
threshold_factor = sliderVal;
Calculate_Events();
init_ui();
end
function dropdownCallback(src,evt)
cellRemoveIndex = iStart+get(src, 'Value')-1;
cellRemove = firedNeurons( cellRemoveIndex );
disp( ['Ready to remove cell ',num2str(cellRemove)] )
disp( ['Cell has CoV of ', num2str(CoVs(cellRemove))])
end
function button_call(src, evt)
disp( ['Removing cell ',num2str(cellRemove)] )
% size( zeros( size(intensityData,2),1 ).' )
% size( zeros( size(intensityData,2),0 ).' )
% size(intensityData(cellRemove,:))
intensityData(cellRemove,:)=zeros( size(intensityData,2),1 ).';
% std_negVals(cellRemove)=[];
% binaryFiring(cellRemove,:)=zeros( size(intensityData,2),1 ).';
binaryFiring = zeros(totalCells,frames);
Calculate_Events();
Intensity_DeltaF_Plotter();
init_ui();
end
function init_ui()
%slmin=4.5;slmax=10;
%sliderVal = 7;
uicontrol('Style','slider','Callback',@sliderCallback,'Min',slmin,'Max',slmax,...
'SliderStep',[.5 .5]./(slmax-slmin),'Value',sliderVal,...
'Position',[5 5 200 20]);
uicontrol('style','push','unit','pix',...
'position',[620 5 80 20],...
'fontsize',12,'fontweight','bold',...
'string','REMOVE','callback',@button_call);
%%% Dropdown Menu
uicontrol('style','pop','unit','pix',...
'position',[520 5 80 20],'fontsize',12,...
'fontweight','bold','string',firedNeurons(iStart:iEnd),...
'value',1,'Callback',@dropdownCallback);
movegui('center')
end
pause;
% DATA
% totalCells activeCells number_of_events std_neg
% avgEventSizes() event_vals() intensityData(firedNeurons(i),:) firedNeurons()
% std_negVals() firingThresholds()
% seconds minutes
save(outputName,'intensityData', 'firedNeurons', 'number_of_events', 'threshold',...
'amplitudes','avgEventSizes','firingThresholds','firingTimes','totalCells','binaryFiring');
%save('stddata','std_negVals')
close all
end