-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.m
148 lines (132 loc) · 5.69 KB
/
features.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
% obj = features(dat, metadata)
% this function extends double's to attach some metadata (e.g. subject
% block identifiers) to vectors. size(metadata,1) must equal size(data,1)
% size(data,1). In other words, you need an identifier for each row of your
% data.
%
% The object will persist across subscripting, subreferencing, and
% concatenation but other operations will promote the object back to it's
% parent type double, erasing the metadata property. This should be fine
% for managing block identifiers for cross validation though.
%
% see here for details on the coding methods used below
% https://www.mathworks.com/help/matlab/matlab_oop/built-in-subclasses-with-properties.html
classdef features < double
properties
metadata;
end
methods
function obj = features(dat, metadata)
if nargin == 0
dat = 0;
metadata = [];
elseif nargin == 1
metadata = [];
elseif ~isempty(metadata)
[n1,~] = size(dat);
[n2,~] = size(metadata);
assert(n1 == n2, 'dat and metadata dimension mismatch');
end
obj = obj@double(dat);
obj.metadata = metadata;
end
function sref = subsref(sref, s)
switch s(1).type
case '.'
switch s(1).subs
case 'metadata'
if length(s)<2
sref = sref.metadata;
else
sref = subsref(sref.(s(1).subs), s(2:end));
end
case 'data'
d = double(sref);
if length(s)<2
sref = d;
elseif length(s)>1 && strcmp(s(2).type,'()')
sref = subsref(d,s(2:end));
end
otherwise
error('Not a supported indexing expression')
end
case '()'
d = double(sref);
newd = subsref(d, s);
if size(sref.metadata,1) == size(sref,1)
% assume we only take corresponding rows of
% metadata table, but all column entries, even if
% specific column entries were specified for dat.
md_s = s;
md_s.subs(2) = {':'};
md_s.subs = md_s.subs(1:2);
if size(sref.metadata,2) == size(sref,2)
warning('features:dim','sref.metadata appears to have as many columns as you have features. This is unexpected and may result in unexpected feature metadata. Please check results of features subscripting.');
end
newmd = subsref(sref.metadata, md_s);
else
newmd = sref.metadata;
end
sref = features(newd, newmd);
case '{}'
error('Not a supported indexing expression');
end
end
function obj = subsasgn(obj, s, b)
switch s(1).type
case '.'
switch s(1).subs
case 'metadata'
obj.metadata = b;
case 'data'
if length(s)<2
obj = features(b,obj.metadata);
elseif length(s)>1 && strcmp(s(2).type,'()')
d = double(obj);
newd = subsasgn(d,s(2:end),b);
obj = features(newd,obj.metadata);
end
otherwise
error('Not a supported indexing expression')
end
case '()'
d = double(obj);
newd = subsasgn(d, s, double(b));
if ~isa(obj,'features')
obj = features(d,[]);
end
if size(obj.metadata,1) == size(obj,1)
if ~isempty(b)
newmd = subsasgn(obj.metadata, s, b.metadata);
else
newmd = subsasgn(obj.metadata, s, b);
end
else
newmd = obj.metadata;
end
obj = features(newd, newmd);
case '{}'
error('Not a supported indexing expression')
end
end
function obj = horzcat(varargin)
d1 = cellfun(@double,varargin,'UniformOutput',false);
data = horzcat(d1{:});
for i = 1:length(varargin)
if ~isa(varargin{i},'features')
varargin{i} = features(varargin{i});
end
end
newmd = cellfun(@(x1)(x1.metadata),varargin,'UniformOutput',false);
newmd = horzcat(newmd{:});
obj = features(data, newmd);
end
function obj = vertcat(varargin)
d1 = cellfun(@double,varargin,'UniformOutput',false);
data = vertcat(d1{:});
newmd = eval(['cellfun(@(x1)(' class(varargin{1}.metadata) '(x1.metadata)), varargin,''UniformOutput'',false);']);
newmd = vertcat(newmd{:});
obj = features(data, newmd);
end
end
end