/
extractFDAFeatures.R
210 lines (181 loc) · 8 KB
/
extractFDAFeatures.R
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
#' @title Extract features from functional data.
#'
#' @description
#' Extract non-functional features from functional features using various methods.
#'
#' The function [extractFDAFeatures] performs the extraction for all functional features
#' via the methods specified in `feat.methods` and transforms all mentioned functional
#' (matrix) features into regular data.frame columns.
#' Additionally, a \dQuote{`extractFDAFeatDesc`} object
#' which contains learned coefficients and other helpful data for
#' re-extraction during the predict-phase is returned. This can be used with
#' [reextractFDAFeatures] in order to extract features during the prediction phase.
#'
#' @details
#' The description object contains these slots:
#' * target (`character`): See argument.
#' * coln (`character`): Colum names of data.
#' * fd.cols (`character`): Functional feature names.
#' * extractFDAFeat (`list`): Contains `feature.methods` and relevant
#' parameters for reextraction.
#'
#' @param obj ([Task] | [data.frame])\cr
#' Task or data.frame to extract functional features from.
#' Must contain functional features as matrix columns.
#' @param target (`character(1)`)\cr
#' Task target column. Only necessary for data.frames
#' Default is `character(0)`.
#' @param feat.methods (named [list])\cr
#' List of functional features along with the desired methods for each functional feature.
#' \dQuote{all} applies the [extractFDAFeatures] method to each
#' functional feature.
#' Names of `feat.methods` must match column names of functional features.
#' Available feature extraction methods are available under family `fda_featextractor`.
#' Specifying a functional feature multiple times with different extraction methods allows
#' for the extraction of different features from the same functional.
#' Default is [list()] which does nothing.
#' @param ... (any)\cr
#' Further hyperparameters passed on to the `feat.methods` specified above.
#' @return ([list])
#' - data | task ([data.frame] | [Task]): Extracted features, same type as obj.
#' - desc (`extracFDAFeatDesc`): Description object. See description for details.
#' @family fda
#' @export
#' @examples
#' df = data.frame(x = matrix(rnorm(24), ncol = 8), y = factor(c("a", "a", "b")))
#' fdf = makeFunctionalData(df, fd.features = list(x1 = 1:4, x2 = 5:8), exclude.cols = "y")
#' task = makeClassifTask(data = fdf, target = "y")
#' extracted = extractFDAFeatures(task,
#' feat.methods = list("x1" = extractFDAFourier(), "x2" = extractFDAWavelets(filter = "haar")))
#' print(extracted$task)
#' reextractFDAFeatures(task, extracted$desc)
extractFDAFeatures = function(obj, target = character(0L), feat.methods = list(), ...) {
assertList(feat.methods)
UseMethod("extractFDAFeatures")
}
#' @export
extractFDAFeatures.data.frame = function(obj, target = character(0L), feat.methods = list(), ...) {
fdf = getFunctionalFeatures(obj)
assertDataFrame(fdf, min.cols = 1L)
assertSubset(unique(names(feat.methods)), choices = c(names(fdf), "all"))
assertCharacter(target)
# If the same transform should be applied to all features, rep method and name accordingly
# "all" needs to be first list name.
all.fds = which(names(feat.methods) == "all")
if (length(all.fds) > 0L) {
methods = setNames(rep(feat.methods[all.fds], ncol(fdf)), rep(names(fdf), each = length(feat.methods[all.fds])))
feat.methods[all.fds] = NULL
feat.methods = c(feat.methods, methods)
}
desc = makeS3Obj("extractFDAFeatDesc",
target = target,
coln = colnames(obj),
fd.cols = NULL,
extractFDAFeat = namedList(names(feat.methods))
)
# cleanup empty list items
feat.methods = Filter(Negate(is.null), feat.methods)
# Treat method parameters:
# Overwrite the par.vals from ... so it is set correctly during tuning
feat.args = list(...)
desc$extractFDAFeat = Map(function(x) {
if (!is.null(x$par.set)) {
# Only set relevant params
feat.args = feat.args[names(feat.args) %in% getParamIds(x$par.set)]
}
if (length(feat.args) > 0) {
# Overwrite args
x$args = feat.args
}
return(x)
}, feat.methods)
# Subset fd.cols accordingly
desc$fd.cols = names(desc$extractFDAFeat)
# Apply function from x to all functional features and return as list of
# lists for each functional feature.
# The "learn" function only learns from the training data and returns the "extraction model".
extracts = Map(function(x, fd.col) {
reextract = x$reextract
# Learn an "extraction model which is a trained algo for extracting from train data.
extractor.vals = do.call(x$learn, c(x$args, list(data = obj, target = target, col = fd.col)))
list("reextract" = reextract, "extractor.vals" = extractor.vals)
}, x = desc$extractFDAFeat, fd.col = desc$fd.cols)
# Append Info relevant for reextraction to desc
desc$extractFDAFeat = extracts
extracted = reextractFDAFeatures(obj, desc)
list(data = extracted, desc = desc)
}
#' @export
extractFDAFeatures.Task = function(obj, target = character(0L), feat.methods = list(), ...) {
stopifnot((hasFunctionalFeatures(obj)))
data = getTaskData(obj, functionals.as = "matrix")
target = getTaskTargetNames(obj)
# Extract features from data
extracted = extractFDAFeatures.data.frame(obj = data, target = target, feat.methods = feat.methods, ...)
# And change data so it only contains non-functional features
task = changeData(obj, extracted$data)
list(task = task, desc = extracted$desc)
}
#' @export
print.extractFDAFeatDesc = function(x, ...) {
catf("Extraction of features from functional data:")
catf("Target: %s", collapse(x$target))
catf("Remaining functional Features: %i after extraction on %i functional features",
length(x$fd.cols), length(x$extractFDAFeat))
}
#' Re-extract features from a data set
#'
#' @description
#' This function accepts a data frame or a task and an extractFDAFeatDesc
#' (a FDA feature extraction description)
#' as returned by [extractFDAFeatures] to extract features
#' from previously unseen data.
#' @param obj ([Task] | [data.frame])\cr
#' Task or data.frame to extract functional features from. Must contain functional features
#' as matrix columns.
#' @param desc (`extractFDAFeatDesc`)\cr
#' FDAFeature extraction description as returned by [extractFDAFeatures]
#' @param ... (any)\cr
#' Further args passed on to methods.
#' @return [data.frame] or [Task] containing the extracted Features
#' @family extractFDAFeatures
#' @export
reextractFDAFeatures = function(obj, desc, ...) {
UseMethod("reextractFDAFeatures")
}
#' @export
reextractFDAFeatures.data.frame = function(obj, desc, ...) {
assertClass(desc, classes = "extractFDAFeatDesc")
# check for new columns
new.cols = names(which(names(obj) %nin% desc$coln))
if (length(new.cols)) {
stop("New columns (%s) found in data. Unable to extract.", collapse(new.cols))
}
# reextract features using reextractDescription and return
reextract = Map(
function(xn, x, fd.col) {
df = do.call(x$reextract, c(list(data = obj, target = desc$target, col = fd.col, vals = x$extractor.vals)))
# Check returned vals
if (!is.data.frame(df)) {
stop("feat.method needs to return a data.frame with one row per observation in the original data.")
} else if (nrow(df) != nrow(obj)) {
stop("feat.method needs to return a data.frame with one row per observation in the original data and equal nrow per column.")
}
return(df)
},
xn = names(desc$extractFDAFeat), x = desc$extractFDAFeat, fd.col = desc$fd.cols)
# cbind resulting columns. Use data.table to ensure proper naming.
df = as.data.frame(do.call(cbind, lapply(reextract, setDT)))
# Reappend target and non-functional features
keep.cols = setdiff(colnames(obj), desc$fd.cols)
data = cbind(df, obj[keep.cols])
return(data)
}
#' @export
reextractFDAFeatures.Task = function(obj, desc, ...) {
# get data and pass to extractor
df = getTaskData(obj, functionals.as = "matrix")
extracted = reextractFDAFeatures.data.frame(df, desc)
# Change data of task and return task
changeData(obj, extracted)
}