-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignDataToTemplates.m
More file actions
175 lines (127 loc) · 5.14 KB
/
assignDataToTemplates.m
File metadata and controls
175 lines (127 loc) · 5.14 KB
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
function [groupings,peakIdxGroup,likes,allPeakIdx,allNormalizedPeaks,coeffs,projStds,baselines] = ...
assignDataToTemplates(data,templates,isNoise,options)
%Inputs:
%data -> 1-D array of data points
%templates -> L x 1 cell array containing M_i x d arrays of template peaks
%options -> self-explanatory
%Outputs:
%groupings -> L x 1 cell array, each cell containing an N_i x d array
% of grouped peaks
%peakIdxGroup -> L x 1 cell array of index values for each
% corresponding group
%likes -> N x L array of the log-likelihood values that each peak
% belongs to the corresponding model
%allPeakIdx -> all found peak locations
%allNormalizedPeaks -> N x d array containing all found peaks
% corresponding to the locations in allPeakIdx
%coeffs -> L x 1 cell array of model PCA bases
%projStds -> L x 1 cell array of model projection standard deviations
addpath(genpath('./chronux'))
L = length(templates);
d = length(templates{1}(1,:));
if nargin < 4 || isempty(options)
options.setAll = true;
else
options.setAll = false;
end
options = makeDefaultOptions(options);
options.diffThreshold = (d-1) / 2;
if options.noiseLevel <= 0
fprintf(1,' Finding Noise Level\n');
if numel(data) > 1e6
shortdata = data(1:1e6);
else
shortdata = data;
end
if options.smoothSigma > 0
shortdata = gaussianfilterdata(shortdata,options.smoothSigma);
end
[ssf] = sinesongfinder(shortdata,options.fs,12,20,.1,.01,.05,[0 1000]);
%returns ssf, which is structure containing the following fields
noise = findnoise(ssf,3,80,1000);
options.noiseLevel = noise.sigma;
end
%find all potential peaks in the new data set
fprintf(1,' Finding Preliminary Peak Locations\n');
[normalizedPeaks,peakIdx,~] = ...
findNormalizedPeaks(data,options.noiseLevel,options.sigmaThreshold,...
options.diffThreshold,options.smoothSigma);
N = length(normalizedPeaks(:,1));
allPeakIdx = peakIdx;
allNormalizedPeaks = normalizedPeaks;
fprintf(1,' Finding Template Bases and Projections\n');
coeffs = cell(L,1);
projStds = cell(L,1);
means = cell(L,1);
L_templates = zeros(L,1);
for i=1:L
fprintf(1,' Template #%2i\n',i);
L_templates(i) = length(templates{i}(:,1));
%find Data Set Mean
means{i} = mean(templates{i});
%perform PCA on set of normalized peaks
[coeffs{i},scores,~] = princomp(templates{i});
projStds{i} = std(scores);
end
%adjust bases sets for sub-sampled data sets
minLength = min(L_templates);
if minLength < 2*d
q = round(minLength / 2);
else
q = d;
end
for i=1:L
coeffs{i} = coeffs{i}(:,options.first_mode:q);
projStds{i} = projStds{i}(options.first_mode:q);
end
%find baseline noise levels
fprintf(1,' Calculating Baseline Noise Levels\n');
if options.use_likelihood_threshold
baselines = repmat(options.baseline_threshold,sum(~isNoise),1);
else
[baselines,~] = findTemplateBaselines(templates,coeffs,means,projStds,isNoise,options.baseline_quantile);
end
%find likelihood values
fprintf(1,' Finding Likelihoods\n');
likes = zeros(N,L);
for i=1:L
fprintf(1,' Template #%2i\n',i);
projections = (normalizedPeaks - repmat(means{i},N,1))*coeffs{i};
likes(:,i) = findDataSetLikelihoods(projections,zeros(size(means{i})),projStds{i});
end
clear projections
[~,idx] = max(likes,[],2);
%find assignments that are sub-baseline and assign them to to either
%the next super-baseline signal class or the largest noise class
fprintf(1,' Checking Peaks Against Baseline\n');
notNoise = find(~isNoise);
%noiseVals = find(isNoise);
signalLikes = likes(:,~isNoise);
belowThreshold = signalLikes < repmat(baselines',length(idx),1);
%[sortVals,sortIdx] = sort(likes,2,'descend');
for q=1:length(notNoise)
i = notNoise(q);
idx2 = find(idx==i & belowThreshold(:,i));
if ~isempty(idx2)
for j=1:length(idx2)
qq = likes(idx2(j),notNoise);
qq(likes(idx2(j),notNoise) - baselines' < 0) = -10;
[maxVal,maxIdx] = max(qq);
if maxVal < 0
idx(idx2(j)) = L + 1;
else
idx(idx2(j)) = notNoise(maxIdx);
end
end
end
end
if max(idx) == L+1
L = L+1;
end
groupings = cell(L,1);
peakIdxGroup = cell(L,1);
for i=1:L
q = idx == i;
groupings{i} = allNormalizedPeaks(q,:);
peakIdxGroup{i} = allPeakIdx(q);
end