Repository navigation
Expand file tree
/
Copy pathChannelDescriptor.m
More file actions
499 lines (423 loc) · 18.4 KB
/
Copy pathChannelDescriptor.m
File metadata and controls
499 lines (423 loc) · 18.4 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
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
classdef ChannelDescriptor < matlab.mixin.Heterogeneous
% Use the factory builder methods in subclasses rather than constructing
% directly
properties
description = ''; % extended description
meta % anything you'd like
displayGroup string = "" % solely for display purposes or logical grouping, not actual data field grouping
end
properties(SetAccess={?TrialDataInterface,?TrialData,?ChannelDescriptor})
name char = ''; % short name, must be valid field name
end
properties(SetAccess={?TrialDataInterface,?TrialData,?ChannelDescriptor})
groupName = ''; % name of group to which this channel belongs
special = false; % whether this channel is a "special" identifier channel used by TrialData
required = true; % whether data for this channel is required or not
end
% set by factory builder methods or inferAttributesFromData
% EACH OF THE FOLLOWING PROPERTIES MUST HAVE THE SAME SIZE (nFields x 1)
properties(SetAccess={?ChannelDescriptor,?ChannelImpl})
% these are fixed strings like "data" or "time" that describes what each field contains
fieldIds = {};
% one of the element type Constants defined below
elementTypeByField = [];
% name of each field in the data struct
dataFields = {}
% string describing units of each field
unitsByField = {}
% original class() of each field, used to maintain the same type in memory .memoryClassByFIeld
originalDataClassByField = {};
catAlongFirstDimByField = []; % manual override specifying that values may be concatenated along dim 1, this will set collectAsCell to be false
end
properties(Constant, Hidden) % element type constants
UNKNOWN = 0;
BOOLEAN = 1; % scalar logical
SCALAR = 2; % scalar non-logical
VECTOR = 3;
NUMERIC = 4;
STRING = 5;
DATENUM = 6;
CELL = 7;
CATEGORICAL = 8;
end
properties(Dependent)
nFields
% nFields x 1 arrays or cells
collectAsCellByField
missingValueByField
missingValueByFieldMemory
isNumericScalarByField % true for logical as well
isBooleanByField
isStringByField
isVectorByField
isVectorizableByField % elements may be concatenated into vector
isCategoricalByField
% indicates whether a given field is shareable between multiple
% channels. If a field is marked as shareable, it will be copied
% when this channel's data is updated, so that the other channel's
% are not affected. See getIsShareableByField for implementation
isShareableByField
% data class of each field as accessed
accessClassByField % cell array specifying data class to convert each
% data class of each field as stored in .data
memoryClassByField
% persistent storage data class of each field (or empty if unknown / mixed)
storageClassByField
% these are shortcuts to the first element of the properties above
% since the first data field is considered the primary data field
unitsPrimary
dataFieldPrimary
end
methods(Abstract)
% return a string with this channels short type description string
type = getType(cdesc);
% return a string with a short description of the channel excluding
% channel name
str = describe(cdesc);
% update internal properties so as to handle specific data types in
% vararargin{1}, varargin{2}, ... varargin{nDataFields}
cd = inferAttributesFromData(cd, varargin);
% return a subclass of ChannelImpl
cimpl = getImpl(cd);
end
methods(Access=protected) % Constructor
function cd = ChannelDescriptor(varargin)
p = inputParser();
p.addOptional('name', '', @(x) ischar(x) || isstring(x));
p.addParameter('displayGroup', '', @(x) ischar(x) || isstring(x));
p.parse(varargin{:});
cd.name = p.Results.name;
cd.displayGroup = p.Results.displayGroup;
end
end
methods
function cd = initialize(cd)
% gives the class a chance to reinitialize itself after the
% user makes changes
cd.warnIfNoArgOut(nargout);
if isempty(cd.catAlongFirstDimByField)
cd.catAlongFirstDimByField = false(cd.nFields, 1);
end
end
function cd = validate(cd)
cd.warnIfNoArgOut(nargout);
% check that all field values are okay
nFields = numel(cd.dataFields); %#ok<*PROP>
assert(numel(cd.fieldIds) == nFields, 'fieldIds has wrong size');
assert(numel(cd.elementTypeByField) == nFields, 'elementTypeByField has wrong size');
assert(numel(cd.unitsByField) == nFields, 'unitsByField has wrong size');
assert(numel(cd.originalDataClassByField) == nFields, 'originalDataClassByField has wrong size');
assert(numel(cd.catAlongFirstDimByField) == nFields, 'catAlongFirstDimByField has wrong size');
end
function [tf, idx] = hasFieldId(cd, fieldId)
fieldId = string(fieldId);
if isnumeric(fieldId)
tf = fieldId >= 1 & fieldId <= cd.nFields;
idx = fieldId;
else
[tf, idx] = ismember(fieldId, cd.fieldIds);
end
end
function idx = lookupFieldId(cd, fieldId)
if isnumeric(fieldId)
idx = fieldId;
else
[tf, idx] = ismember(fieldId, cd.fieldIds);
if ~all(tf)
error('Field with id %s not found for channel %s', fieldId{find(~tf, 1)}, cd.name);
end
end
end
function info = getFieldInfoById(cd, fieldId)
ind = cd.getFieldIndexById(fieldId);
info.id = fieldId;
info.dataField = cd.dataFields{ind};
info.elementType = cd.elementTypesByField{ind};
info.units = cd.unitsByField{ind};
info.originalDataClass = cd.originalDataClassByField{ind};
info.catAlongFirstDim = cd.catAlongFirstDimByField(ind);
info.accessClass = cd.accessClassByField{ind};
info.memoryClass = cd.memoryClassByField{ind};
info.storageClass = cd.storageClassByField{ind};
info.isShareable = cd.isShareableByField(ind);
end
% used by trial data when it needs to change field names
function name = suggestFieldName(cd, fieldIdx)
fieldIdx = cd.lookupFieldId(fieldIdx);
if fieldIdx == 1
name = cd.name;
else
name = sprintf('%s_f%s', cd.fieldIds{fieldIdx});
end
end
function c = getAccessClassByField(cd)
% implements get.accessClassByField
memClass = cd.getMemoryClassByField();
c = cell(1, cd.nFields);
for iF = 1:cd.nFields
switch cd.elementTypeByField(iF)
case cd.BOOLEAN
c{iF} = 'logical';
case {cd.SCALAR, cd.VECTOR, cd.NUMERIC, cd.DATENUM, cd.CELL}
if ismember(memClass{iF}, {'uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'single'})
c{iF} = 'single';
elseif ismember(memClass{iF}, {'logical'})
c{iF} = 'logical';
elseif ismember(memClass{iF}, {'categorical'})
c{iF} = 'categorical';
elseif ismember(memClass{iF}, {'string'})
c{iF} = 'string';
else
c{iF} = 'double';
end
case cd.STRING
c{iF} = 'string';
case cd.CATEGORICAL
c{iF} = 'categorical';
otherwise
c{iF} = '';
end
end
end
function c = getMemoryClassByField(cd)
% implements get.memoryClassByField, differs in that numeric
% types default to their original data class
c = cell(1, cd.nFields);
for iF = 1:cd.nFields
switch cd.elementTypeByField(iF)
case cd.BOOLEAN
c{iF} = 'logical';
case {cd.SCALAR, cd.VECTOR, cd.NUMERIC, cd.DATENUM, cd.CELL}
if ~isempty(cd.originalDataClassByField{iF})
c{iF} = cd.originalDataClassByField{iF};
else
c{iF} = 'double';
end
case cd.STRING
c{iF} = 'string';
case cd.CATEGORICAL
c{iF} = 'categorical';
otherwise
c{iF} = cd.originalDataClassByField{iF};
end
end
end
function c = getStorageClassByField(cd)
c = cell(1, cd.nFields);
for iF = 1:cd.nFields
switch cd.elementTypeByField(iF)
case cd.BOOLEAN
c{iF} = 'logical';
case {cd.SCALAR, cd.VECTOR, cd.NUMERIC, cd.CELL}
if ~isempty(cd.originalDataClassByField{iF})
c{iF} = cd.originalDataClassByField{iF};
else
c{iF} = 'double';
end
case cd.DATENUM
c{iF} = 'double';
case cd.STRING
c{iF} = 'string';
case cd.CATEGORICAL
c{iF} = 'categorical';
otherwise
c{iF} = '';
end
end
end
function str = getAxisLabelPrimary(cd)
if isempty(cd.unitsByField{1})
str = sprintf('%s', cd.name);
else
str = sprintf('%s (%s)', cd.name, cd.unitsPrimary);
end
end
function [cd, dataFieldRenameStruct] = rename(cd, newName)
% renames the channel and any fields within
% dataFieldRenameStruct indicates which .data fields will need
% to be renamed, as dataFieldRenameStruct.oldName = newName
cd.warnIfNoArgOut(nargout);
oldName = cd.name;
cd.name = newName;
dataFieldRenameStruct = struct();
if strcmp(cd.dataFields{1}, oldName)
dataFieldRenameStruct.(oldName) = newName;
cd.dataFields{1} = newName;
end
cd = cd.initialize();
end
function cd = renameDataField(cd, iF, newName)
cd.warnIfNoArgOut(nargout);
iF = cd.lookupFieldId(iF);
assert(iF ~= 1, 'Should not rename primary field using this method');
cd.dataFields{iF} = newName;
cd = cd.initialize();
end
function cd = setPrimaryUnits(cd, unitName)
assert(ischar(unitName));
cd.unitsByField{1} = unitName;
end
function [tf, idx] = hasSubChannel(~, name)
% overwritten by subclasses
if ischar(name)
tf = false;
idx = NaN;
else
tf = false(numel(name), 1);
idx = nan(numel(name), 1);
end
end
function cd = buildSubChannelDescriptor(~, nameOrIdx) %#ok<STOUT,INUSD>
error('Not implemented');
end
function [names, chidx] = listNamedSubChannels(~)
names = string([]);
chidx = [];
end
end
methods(Sealed)
function warnIfNoArgOut(obj, nargOut)
if nargOut == 0 && ~isa(obj, 'handle')
warning('%s is not a handle class. If the instance handle returned by this method is not stored, this call has no effect', ...
class(obj));
end
end
end
% Dependent property definitions, some refer out to getProperty methods
% of the same name to give subclasses an opportunity to redefine their
% implementation
methods
function grp = get.groupName(cd)
grp = cd.getGroupName(cd.groupName);
end
% allows for sub classes to overwrite
function groupName = getGroupName(cd, groupName) %#ok<INUSL>
end
function n = get.nFields(cd)
n = numel(cd.dataFields);
end
function tf = getIsShareableByField(cd)
tf = true(1, cd.nFields);
tf(1) = false;
end
function vals = get.missingValueByField(cd)
vals = cd.getMissingValueByField(false);
end
function vals = get.missingValueByFieldMemory(cd)
vals = cd.getMissingValueByField(true);
end
function vals = getMissingValueByField(cd, inMemory)
if nargin < 2
inMemory = false;
end
missingVals = {false, NaN, [], [], string(missing), NaN, [], categorical(missing)};
vals = missingVals(cd.elementTypeByField);
if inMemory
accClasses = string(cd.memoryClassByField); % was memory class, changed to access class so that int types converted to single get filled as NaN
else
accClasses = string(cd.accessClassByField); % was memory class, changed to access class so that int types converted to single get filled as NaN
end
for iF = 1:numel(vals)
if ismember(cd.elementTypeByField, [cd.VECTOR, cd.NUMERIC])
if ~cd.collectAsCellByField(iF)
if ismember(accClasses{iF}, ["double", "single"])
% replace [] with NaN since this will be collected as
% matrix
vals{iF} = nan(1, accClasses{iF});
else
vals{iF} = zeros(1, accClasses{iF});
end
else
vals{iF} = zeros(0, 1, accClasses{iF});
end
elseif cd.elementTypeByField(iF) == cd.SCALAR
if strcmp(accClasses{iF}, 'logical')
vals{iF} = false;
elseif ismember(accClasses(iF), ["string", "char"])
vals{iF} = string(missing);
else
vals{iF} = ChannelImpl.icast(vals{iF}, accClasses{iF});
end
elseif cd.elementTypeByField(iF) == cd.CELL
vals{iF} = {};
end
end
end
function c = get.accessClassByField(cd)
% defer to method to make it overrideable in subclasses
c = cd.getAccessClassByField();
end
function c = get.memoryClassByField(cd)
% defer to method to make it overrideable in subclasses
c = cd.getMemoryClassByField();
end
function c = get.storageClassByField(cd)
c = cd.getStorageClassByField();
end
function tf = get.collectAsCellByField(cd)
tf = ~cd.isVectorizableByField;
if ~isempty(cd.catAlongFirstDimByField)
tf = tf & makerow(~cd.catAlongFirstDimByField);
end
end
function tf = get.isStringByField(cd)
tf = cd.elementTypeByField == cd.STRING;
end
function tf = get.isCategoricalByField(cd)
tf = cd.elementTypeByField == cd.CATEGORICAL;
end
function tf = get.isVectorizableByField(cd) % returns true for boolean and scalar including string
tf = ismember(cd.elementTypeByField, [cd.BOOLEAN, cd.SCALAR, cd.DATENUM, cd.STRING, cd.CATEGORICAL]);
end
function tf = get.isNumericScalarByField(cd)
tf = ismember(cd.elementTypeByField, [cd.BOOLEAN, cd.SCALAR, cd.DATENUM]) & ...
ismember(cd.accessClassByField, {'logical', 'uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'double', 'single'});
end
function tf = get.isVectorByField(cd)
tf = ismember(cd.elementTypeByField, cd.VECTOR);
end
function tf = get.isBooleanByField(cd)
tf = cd.elementTypeByField == cd.BOOLEAN;
end
function tf = get.isShareableByField(cd)
% defer to overrideable function
tf = cd.getIsShareableByField();
end
function u = get.unitsPrimary(cd)
if isempty(cd.unitsByField)
u = '';
else
u = cd.unitsByField{1};
end
end
function f = get.dataFieldPrimary(cd)
if isempty(cd.dataFields)
f = '';
else
f = cd.dataFields{1};
end
end
end
methods(Static) % Utility methods
function cd = loadobj(s)
cd = builtin('loadobj', s);
cd = cd.initialize();
cd = cd.validate();
end
function [ch, idx] = parseIndexedChannelName(name)
% if it is a name like analogGroup(5), make a sub channel
% channelDescriptor for analogGroup referring to column 5
tokens = regexp(name, '(?<ch>[\w_]+)\((?<idx>\d+)\)', 'names');
if isempty(tokens)
ch = name;
idx = NaN;
else
ch = tokens.ch;
idx = str2double(tokens.idx);
end
end
function cls = getSubChannelClass()
cls = 'ChannelDescriptor';
end
end
end