-
Notifications
You must be signed in to change notification settings - Fork 147
/
order_components.m
59 lines (46 loc) · 1.77 KB
/
order_components.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
function [srt,n_events,n_act] = order_components(YrA,C,sn,options)
% order components by how unlikely it is to produce the data under a no
% spiking hypothesis. The function counts how many times the trace is above
% the sum of the mode and a given number of standard deviations for a given
% number of consecutive intervals.
% INPUTS
% YrA: Noise for each temporal component
% C: Temporal components
% sn: Standard deviation of each component (optional, estimated if not given)
% options: Options structure
% options.nsd: number of standard deviations to set threshold
% options.nfr: number of consecutive frames for considering an event
% OUPUTS
% srt: Sorted components in decreasing order
% n_event: Number of events for each components in decreasing order
% n_act: Number of components with events (The srt(1:n_act) is the set of active components)
% Written by:
% Eftychios A. Pnevmatikakis, Simons Foundation, 2016
if nargin < 3 || isempty(sn)
sn = std(YrA,[],2);
end
defoptions = CNMFSetParms;
if nargin < 4 || isempty(options)
options = defoptions;
end
if ~isfield(options,'nsd') || isempty(options.nsd); options.nsd = defoptions.nsd; end % number of sd above the mode
if ~isfield(options,'nfr') || isempty(options.nfr); options.nfr = defoptions.nfr; end % number of consecutive frames
[K,T] = size(C);
CY = C + YrA;
md = zeros(K,1);
%bd = zeros(K,1);
for i = 1:K
[bandwidth,density,xmesh]=kde(CY(i,:),2^floor(log2(T)-1));
[~,id] = max(density);
md(i) = xmesh(id);
%bd(i) = bandwidth;
end
FF = false(size(CY));
FF2 = false(size(CY));
for i = 1:K
ff = (CY(i,:)>md(i)+options.nsd*sn(i));
FF(i,:) = ff;
FF2(i,:) = bwareaopen(ff,options.nfr);
end
[n_events,srt] = sort(sum(FF2,2),'descend');
n_act = sum(n_events>0);