-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFeaturesReporter.m
More file actions
359 lines (275 loc) · 12.5 KB
/
FeaturesReporter.m
File metadata and controls
359 lines (275 loc) · 12.5 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
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
classdef FeaturesReporter < handle
properties
name;
featuresColor = [0 0 1];
end
properties (Transient)
controller
featuresRange
end
properties (Transient, Access = private)
waitBarHandle
cachedFeatureTypes
end
properties (Access = private)
featureList = {}
featureListSize
featureCount
end
properties (Dependent = true)
duration
end
events
FeatureTypesDidChange
FeaturesDidChange
end
methods(Static)
function n = typeName()
% Return a name for this type of detector.
n = 'Feature';
end
function ft = possibleFeatureTypes()
% Return a list of the types of features this reporter can report.
ft = {};
end
function initialize
% Perform any set up for all instances of this detector type.
end
function n = actionName()
n = 'Reporting features...';
end
end
methods
function obj = FeaturesReporter(controller, varargin)
obj = obj@handle();
obj.controller = controller;
obj.featureList = cell(1, 1000);
obj.featureListSize = 1000;
obj.featureCount = 0;
end
function fs = features(obj, featureType)
fs = obj.featureList(1:obj.featureCount);
if nargin > 1
inds = cellfun(@(f) strcmp(f.type, featureType), fs);
fs = fs(inds);
end
end
function setFeatures(obj, featureList)
if iscell(featureList)
obj.featureList = featureList;
else
obj.featureList = num2cell(featureList);
end
obj.featureListSize = length(featureList);
obj.featureCount = obj.featureListSize;
obj.cachedFeatureTypes = {};
notify(obj, 'FeaturesDidChange');
end
function types = featureTypes(obj)
% Return the list of feature types that are being reported.
if isempty(obj.cachedFeatureTypes)
obj.cachedFeatureTypes = unique(cellfun(@(f) f.type, obj.featureList(1:obj.featureCount), 'UniformOutput', false));
end
types = obj.cachedFeatureTypes;
end
function addFeatures(obj, features)
% Add the features to the list.
numFeatures = length(features);
obj.featureCount = obj.featureCount + numFeatures;
while obj.featureCount > obj.featureListSize
% Pre-allocate space for another 1000 features.
obj.featureList = horzcat(obj.featureList, cell(1, 1000));
obj.featureListSize = obj.featureListSize + 1000;
end
for i = 1:numFeatures
if iscell(features)
features{i}.reporter = obj;
obj.featureList{obj.featureCount - numFeatures + i} = features{i};
else
features(i).reporter = obj;
obj.featureList{obj.featureCount - numFeatures + i} = features(i);
end
end
% Check if any of the new features have a new type.
if iscell(features)
~all(ismember(cellfun(@(f) f.type, features, 'UniformOutput', false), obj.featureTypes()));
else
[tmp{1:length(features)}]=deal(features.type);
~all(ismember(tmp, obj.featureTypes()));
end
if ans
obj.cachedFeatureTypes = []; % indicate that the types must be recalculated
notify(obj, 'FeatureTypesDidChange', FeaturesChangedEventData('add', features));
end
obj.featuresRange = []; % indicate that the range must be recalculated
notify(obj, 'FeaturesDidChange', FeaturesChangedEventData('add', features));
end
function removeFeatures(obj, features)
featureWasRemoved = false;
for j = 1:length(features)
if iscell(features)
feature = features{j};
else
feature = features(j);
end
pos = cellfun(@(f) eq(f, feature), obj.featureList(1:obj.featureCount));
obj.featureList(pos) = [];
obj.featureListSize = obj.featureListSize - 1;
obj.featureCount = obj.featureCount - 1;
featureWasRemoved = true;
end
if featureWasRemoved
obj.featuresRange = []; % indicate that the range must be recalculated
notify(obj, 'FeaturesDidChange', FeaturesChangedEventData('remove', features));
end
end
function d = get.duration(obj)
range = obj.featuresRange();
d = range(2);
end
function range = get.featuresRange(obj)
% Return the maximum range of all features in time and frequency.
if isempty(obj.featuresRange)
% Calculate the maximum range of all features.
obj.featuresRange = [0 0 -inf inf];
features = obj.featureList(1:obj.featureCount);
if ~isempty(features)
% Get the min and max in time and frequency of all features.
minTime = min(cellfun(@(f) f.startTime, features));
maxTime = max(cellfun(@(f) f.endTime, features));
lowFreqs = cellfun(@(f) f.lowFreq, features);
minFreq = min(lowFreqs(lowFreqs > -Inf));
if isempty(minFreq)
minFreq = -Inf;
end
maxFreqs = cellfun(@(f) f.highFreq, features);
maxFreq = max(maxFreqs(maxFreqs < Inf));
if isempty(maxFreq)
maxFreq = Inf;
end
obj.featuresRange = [minTime maxTime minFreq maxFreq];
end
end
range = obj.featuresRange;
end
function handled = keyWasPressedInPanel(obj, keyEvent, panel) %#ok<INUSD>
handled = false;
end
function exportFeatures(obj)
if isprop(obj,'featuresFilePath')
[p,n,e] = fileparts(obj.featuresFilePath);
elseif length(obj.controller.recordings)>0
[p,n,e] = fileparts(obj.controller.recordings{1}.filePath);
else
p=''; n='';
end
[fileName, pathName, filterIndex] = uiputfile({'*.mat', 'MATLAB file';'*.txt', 'Text file'}, 'Save features as', [fullfile(p,n) '-features.mat']);
if ischar(fileName)
features = obj.features();
[~, ind] = sort(cellfun(@(f) f.startTime, features));
features = features(ind);
lowFreqs = cellfun(@(f) f.lowFreq, features);
highFreqs = cellfun(@(f) f.highFreq, features);
haveFreqRanges = any(~isinf(lowFreqs)) || any(~isinf(highFreqs));
if filterIndex == 1
% Export as a MATLAB file
s.features = features;
s.featureTypes = cellfun(@(f) f.type, features, 'UniformOutput', false);
s.startTimes = cellfun(@(f) f.startTime, features);
s.stopTimes = cellfun(@(f) f.endTime, features);
if haveFreqRanges
s.lowFreqs = lowFreqs;
s.highFreqs = highFreqs;
end
save(fullfile(pathName, fileName), '-struct', 's');
else
% Export as an Excel tsv file
fid = fopen(fullfile(pathName, fileName), 'w');
propNames = {};
ignoreProps = {'type', 'range', 'color', 'startTime', 'endTime', 'lowFreq', 'highFreq', 'duration'};
% Find all of the feature properties so we know how many columns there will be.
for f = 1:length(features)
feature = features(f);
props = properties(feature);
for p = 1:length(props)
if ~ismember(props{p}, ignoreProps)
propName = [feature.type ':' props{p}];
if ~ismember(propName, propNames)
propNames{end + 1} = propName; %#ok<AGROW>
end
end
end
end
propNames = sort(propNames);
% Export a header row.
fprintf(fid, 'Type\tStart Time\tEnd Time');
if haveFreqRanges
fprintf(fid, '\tLow Freq\tHigh Freq');
end
for p = 1:length(propNames)
fprintf(fid, '\t%s', propNames{p});
end
fprintf(fid, '\n');
% Export the features.
for i = 1:length(features)
feature = features(i);
fprintf(fid, '%s\t%f\t%f', feature.type, feature.startTime, feature.endTime);
if haveFreqRanges
if isinf(feature.lowFreq)
fprintf(fid, '\t');
else
fprintf(fid, '\t%f', feature.lowFreq);
end
if isinf(feature.highFreq)
fprintf(fid, '\t');
else
fprintf(fid, '\t%f', feature.highFreq);
end
end
propValues = cell(1, length(propNames));
props = sort(properties(feature));
for j = 1:length(props)
if ~ismember(props{j}, ignoreProps)
propName = [feature.type ':' props{j}];
index = strcmp(propNames, propName);
value = feature.(props{j});
if isnumeric(value)
value = num2str(value);
end
propValues{index} = value;
end
end
for p = 1:length(propNames)
fprintf(fid, '\t%s', propValues{p});
end
fprintf(fid, '\n');
end
fclose(fid);
end
end
end
end
methods (Sealed)
function startProgress(obj)
action = obj.actionName();
obj.waitBarHandle = waitbar(0, action, 'Name', obj.name);
axesH = findobj(obj.waitBarHandle, 'type', 'axes');
titleH = get(axesH, 'Title');
set(titleH, 'FontSize', 12)
end
function updateProgress(obj, message, fractionComplete)
global DEBUG
if nargin < 3
fractionComplete = 0;
end
waitbar(fractionComplete, obj.waitBarHandle, message);
if DEBUG
disp(message);
end
end
function endProgress(obj)
close(obj.waitBarHandle);
obj.waitBarHandle = [];
end
end
end