-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathas_task_classif_st.R
More file actions
145 lines (135 loc) · 3.77 KB
/
Copy pathas_task_classif_st.R
File metadata and controls
145 lines (135 loc) · 3.77 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
#' @title Convert to a Spatiotemporal Classification Task
#'
#' @description
#' Convert object to a [TaskClassifST].
#' This is a S3 generic, specialized for at least the following objects:
#'
#' 1. [TaskClassifST]: Ensure the identity.
#' 2. [data.frame()] and [mlr3::DataBackend]: Provides an alternative to the constructor of [TaskClassifST].
#' 3. [sf::sf]: Extracts spatial meta data before construction.
#' 4. [mlr3::TaskRegr]: Calls [mlr3::convert_task()].
#'
#' @inheritParams mlr3::as_task_classif
#' @template param_coords_as_features
#' @template param_crs
#' @template param_coordinate_names
#'
#' @return [TaskClassifST]
#' @export
as_task_classif_st = function(x, ...) {
UseMethod("as_task_classif_st")
}
#' @rdname as_task_classif_st
#' @export as_task_classif_st.TaskClassifST
#' @exportS3Method
#nolint next
as_task_classif_st.TaskClassifST = function(x, clone = FALSE, ...) {
if (clone) x$clone() else x
}
#' @rdname as_task_classif_st
#' @export as_task_classif_st.data.frame
#' @exportS3Method
as_task_classif_st.data.frame = function(
x,
target,
id = deparse(substitute(x)),
positive = NULL,
coordinate_names,
crs = NA_character_,
coords_as_features = FALSE,
label = NA_character_,
...
) {
ii = which(map_lgl(keep(x, is.double), anyInfinite))
if (length(ii)) {
warningf("Detected columns with unsupported Inf values in data: %s", str_collapse(names(ii)))
}
TaskClassifST$new(
id = id,
backend = x,
target = target,
positive = positive,
coords_as_features = coords_as_features,
crs = crs,
coordinate_names = coordinate_names,
label = label
)
}
#' @rdname as_task_classif_st
#' @export as_task_classif_st.DataBackend
#' @exportS3Method
as_task_classif_st.DataBackend = function(
x,
target,
id = deparse(substitute(x)),
positive = NULL,
coordinate_names,
crs,
coords_as_features = FALSE,
label = NA_character_,
...
) {
TaskClassifST$new(
id = id,
backend = x,
target = target,
positive = positive,
coords_as_features = coords_as_features,
crs = crs,
coordinate_names = coordinate_names,
label = label
)
}
#' @rdname as_task_classif_st
#' @export as_task_classif_st.sf
#' @exportS3Method
as_task_classif_st.sf = function(
x,
target = NULL,
id = deparse(substitute(x)),
positive = NULL,
coords_as_features = FALSE,
label = NA_character_,
...
) {
id = as.character(id)
geometries = as.character(unique(sf::st_geometry_type(x)))
if (!test_names(geometries, identical.to = "POINT")) {
stop("Simple feature may not contain geometries of type '%s'", str_collapse(setdiff(geometries, "POINT")))
}
if (any(c("X", "Y") %in% colnames(x))) {
stopf(
paste(
"Data contains columns named 'X' and 'Y' which are reserved for coordinates.",
"The sf object might contain coordinates in the geometry column and the `X` and `Y` columns.",
"Please remove or rename them before converting to a task."
)
)
}
# extract spatial meta data
crs = sf::st_crs(x)$wkt
coordinates = as.data.frame(sf::st_coordinates(x))
coordinate_names = colnames(coordinates)
# convert sf to data.frame
x[[attr(x, "sf_column")]] = NULL
attr(x, "sf_column") = NULL
x = as.data.frame(x)
# add coordinates
x = cbind(x, coordinates)
as_task_classif_st(
x,
target = target,
id = id,
positive = positive,
coords_as_features = coords_as_features,
crs = crs,
coordinate_names = coordinate_names,
label = label
)
}
#' @rdname as_task_classif_st
#' @export as_task_classif_st.TaskRegrST
#' @exportS3Method
as_task_classif_st.TaskRegrST = function(x, target = NULL, drop_original_target = FALSE, drop_levels = TRUE, ...) {
convert_task(intask = x, target = target, new_type = "classif_st", drop_original_target = FALSE, drop_levels = TRUE)
}