-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGroup.m
281 lines (232 loc) · 8.64 KB
/
Group.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
%% Clarinet: Electrophysiology time series analysis
% Copyright (C) 2018-2020 Luca Della Santina
%
% This file is part of Clarinet
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% This software is released under the terms of the GPL v3 software license
%
classdef Group < KeyValueEntity
properties (Access = protected)
featureMap % Feature map with key as FeatureDescription.type and value as @see Feature instance
end
properties(SetAccess = private)
name % Descriptive name of the Abstract Group
end
methods
function obj = Group(name)
obj.name = name;
obj.featureMap = containers.Map('KeyType', 'char', 'ValueType', 'any');
obj.uuid = char(java.util.UUID.randomUUID);
obj.attributes = containers.Map();
end
function feature = createFeature(obj, id, data, varargin)
key = varargin(1 : 2 : end);
value = varargin(2 : 2 : end);
propertyMap = containers.Map('KeyType', 'char', 'ValueType', 'any');
if ~ isempty(key)
for i = 1 : numel(key)
propertyMap(key{i}) = value{i};
end
end
id = obj.makeValidKey(id);
propertyMap('id') = id;
description = FeatureDescription(propertyMap);
description.id = id;
oldFeature = obj.getFeatures(id);
% The order of if's is important here
if isempty(oldFeature)
feature = Feature(description, data);
obj.featureMap(id) = feature;
return
end
if ~ isempty(oldFeature) && isKey(propertyMap, 'append') && propertyMap('append')
feature = Feature(description, data);
obj.appendFeature(feature);
return
end
if ~ isempty(oldFeature)
oldFeature.data = data;
oldFeature.description = description;
disp(['warning :' id ' for node ' obj.name]);
end
end
function features = getFeatures(obj, keys)
% getFeatures - returns the feature based on FeatureDescription
% reference
features = [];
if ischar(keys)
keys = {keys};
end
keys = unique(keys);
for i = 1 : numel(keys)
key = keys{i};
if isKey(obj.featureMap, key)
feature = obj.featureMap(key);
features = [features, feature]; %#ok
end
end
end
function keySet = getFeatureKey(obj)
if numel(obj) > 1
result = arrayfun(@(ref) ref.featureMap.keys, obj, 'UniformOutput', false);
keySet = unique([result{:}]);
return
end
keySet = obj.featureMap.keys;
end
function data = getFeatureData(obj, key)
data = [];
features = [];
if iscellstr(key) && numel(key) > 1
disp('Multiple feature key present');
end
if isKey(obj.featureMap, obj.makeValidKey(key))
features = obj.featureMap(obj.makeValidKey(key));
end
if ~ isempty(features)
data = obj.getData(features);
end
end
function tf = isFeatureEntity(~, refs)
tf = all(cellfun(@(ref) isa(ref, 'Feature'), refs));
end
function k = makeValidKey(~, key)
k = upper(key);
end
end
methods (Hidden)
function appendFeature(obj, newFeatures)
for i = 1 : numel(newFeatures)
key = newFeatures(i).description.id;
f = obj.getFeatures(key);
if ~ isempty(f) && ismember({newFeatures(i).uuid}, {f.uuid})
continue;
end
obj.featureMap = addToMap(obj.featureMap, key, newFeatures(i));
end
end
function setParameters(obj, parameters)
% setParameters - Copies from parameters to obj.parameters
% @see setParameter
if isempty(parameters)
return
end
if isstruct(parameters)
names = fieldnames(parameters);
for i = 1 : length(names)
obj.addParameter(names{i}, parameters.(names{i}));
end
end
if isa(parameters,'containers.Map')
names = parameters.keys;
for i = 1 : length(names)
obj.addParameter(names{i}, parameters(names{i}));
end
end
end
function appendParameter(obj, key, value)
% append key, value pair to obj.parameters. On empty field it
% creates the new field,value else it appends to existing value
% if it NOT exist
% @see setParameter
if isempty(value)
return
end
old = obj.get(key);
if isempty(old)
obj.addParameter(key, value);
return
end
new = addToCell(old, value);
if all(cellfun(@isnumeric, new))
new = cell2mat(new);
elseif obj.isFeatureEntity(new)
new = [new{:}];
end
obj.addParameter(key, new);
end
function update(obj, epochGroup, in, out)
% Generic code to handle merge from source epochGroup to destination
% obj(epochGroup). It merges following,
%
% 1. properties
% 2. Feature
% 3. parameters 'matlab structure'
%
% arguments
% epochGroup - source epochGroup
% in - It may be one of source epochGroup property, parameter and feature
% out - It may be one of destination obj(epochGroup) property, parameter and feature
if nargin < 4
out = in;
end
in = char(in);
out = char(out);
if strcmp(out, 'id')
error('id:update:prohibited', 'cannot updated instance id');
end
% case 1 - epochGroup.in and obj.out is present has properties
if isprop(obj, out) && isprop(epochGroup, in)
old = obj.(out);
obj.(out) = addToCell(old, epochGroup.(in));
return
end
% case 2 - epochGroup.in is struct parameters & obj.out is class property
if isprop(obj, out)
old = obj.(out);
obj.(out) = addToCell(old, epochGroup.get(in));
return
end
% case 3 epochGroup.in is class property but obj.out is struct
% parameters
if isprop(epochGroup, in)
obj.appendParameter(out, epochGroup.(in));
return
end
% case 4 in == out and its a key of featureMap
keys = epochGroup.featureMap.keys;
if ismember(in, keys)
if ~ strcmp(in, out)
error('in:out:mismatch', 'In and out should be same for appending feature map')
end
obj.appendFeature(epochGroup.featureMap(in))
return
end
% case 5 just append the in to out struct parameters
% for unknown in parameters, it creates empty out paramters
obj.appendParameter(out, epochGroup.get(in));
end
end
methods(Access = protected)
function addParameter(obj, property, value)
% setParameters - set property, value pair to parameters
obj.attributes(property) = value;
end
function data = getData(obj, features)
try
data = [features.data];
catch
data = {features.data};
end
end
function header = getHeader(obj)
try
header = ['Displaying Epoch group information for [ ' obj.name ' ] for unique values'];
catch
header = getHeader@matlab.mixin.CustomDisplay(obj);
end
end
end
end