230 | ```
231 |
232 |
233 | ```r
234 | library(ggplot2)
235 | autoplot(bm, type = "ridge")
236 | #> Picking joint bandwidth of 0.0215
237 | ```
238 |
239 | 
240 |
241 |
242 |
--------------------------------------------------------------------------------
/attic/benchmark.Rmd.orig:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Benchmarking parallel predictions"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Benchmarking parallel predictions}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r setup, include = FALSE}
11 | knitr::opts_chunk$set(
12 | collapse = TRUE,
13 | comment = "#>",
14 | options(width = 120),
15 | fig.path = "./"
16 | )
17 | lgr::get_logger("bbotk")$set_threshold("warn")
18 | lgr::get_logger("mlr3")$set_threshold("warn")
19 | lgr::get_logger("mlr3spatial")$set_threshold("warn")
20 | ```
21 |
22 | This benchmark was run on a MacBook Pro 2021 (arm64) with the following specs
23 |
24 | - M1 Pro
25 | - 32 GB RAM
26 |
27 | Note that the differences between the parallel and sequential timings will increase for larger objects as the overhead for starting the parallel workers and collecting the results will decrease.
28 |
29 | It is not fully clear why the parallel approach of the {terra} package is slow than its sequential counterpart but it might relate to the single-core performance of the machine the benchmark was run on in combination with the overhead associated with starting the parallel cluster the way it is done in the {terra} package.
30 |
31 | ```{r prepare}
32 | library(mlr3)
33 | library(mlr3spatial)
34 | library(future)
35 | library(bench)
36 | library(stars)
37 | library(rpart)
38 | ```
39 |
40 | ## Small files
41 |
42 | - ~ 250k values
43 | - 48 MB on disk
44 |
45 | ```{r prepare-small}
46 | # SpatRaster demo stack
47 | stack_terra = demo_stack_spatraster(50)
48 | value = data.table::data.table(ID = c(0, 1), y = c("negative", "positive"))
49 | terra::set.cats(stack_terra, layer = "y", value = value)
50 | colnames = names(stack_terra)
51 | file_terra = tempfile("terra", fileext = ".tif")
52 | terra::writeRaster(stack_terra, file_terra)
53 |
54 | # RasterBrick demo stack
55 | stack_raster = demo_stack_rasterbrick(50)
56 | colnames_raster = names(stack_raster)
57 | file_raster = tempfile("raster", fileext = ".tif")
58 | raster::writeRaster(stack_raster, file_raster)
59 |
60 | # tasks
61 | stack_terra = terra::rast(file_terra)
62 | backend_terra = DataBackendRaster$new(stack_terra)
63 | task_terra = as_task_regr(backend_terra, target = "x_1")
64 |
65 | stack_raster = raster::brick(file_raster)
66 | names(stack_raster) = colnames_raster
67 | backend_raster = DataBackendRaster$new(stack_raster)
68 | task_raster = as_task_regr(backend_raster, target = "x_1")
69 |
70 | # Train learners
71 | set.seed(42)
72 | row_ids = sample(1:task_terra$nrow, 50)
73 |
74 | learner_task_terra = lrn("regr.rpart")
75 | learner_task_terra$parallel_predict = TRUE
76 | learner_task_terra$train(task_terra, row_ids = row_ids)
77 |
78 | learner_task_raster = lrn("regr.rpart")
79 | learner_task_terra$parallel_predict = TRUE
80 | learner_task_raster$train(task_raster, row_ids = row_ids)
81 |
82 | # non-mlr3 models
83 | rpart_task_terra = rpart::rpart(x_1 ~ ., task_terra$data(rows = row_ids))
84 | rpart_task_raster = rpart::rpart(x_1 ~ ., task_raster$data(rows = row_ids))
85 | ```
86 |
87 | ```{r benchmark-small}
88 | bm = bench::mark(
89 |
90 | "01-mlr3-terra-4-cores" = {
91 | plan(multicore, workers = 4)
92 | predict_spatial(task_terra, learner_task_terra, chunksize = 2000L)
93 | },
94 |
95 | "02-terra-4-cores" = terra::predict(stack_terra, rpart_task_terra, cores = 4, cpkgs = "rpart"),
96 |
97 | "03-mlr3-raster-4-cores" = {
98 | plan(multicore, workers = 4)
99 | predict_spatial(task_raster, learner_task_raster, chunksize = 2000L, format = "raster")
100 | },
101 |
102 | "04-raster-4-cores" = {
103 | library(raster)
104 | library(rpart)
105 | beginCluster(4, type = "PSOCK")
106 | clusterR(stack_raster, predict, args = list(model = rpart_task_raster))
107 | },
108 |
109 | check = FALSE, filter_gc = FALSE, min_iterations = 3,
110 | max_iterations = 3, memory = FALSE)
111 |
112 | bm$`itr/sec` = NULL
113 | bm$result = NULL
114 | bm$`gc/sec` = NULL
115 | bm$memory = NULL
116 | bm$mem_alloc = NULL
117 |
118 | print(bm)
119 | ```
120 |
121 | ```{r plot-benchmark-small, fig.cap=""}
122 | library(ggplot2)
123 | autoplot(bm, type = "ridge")
124 | ```
125 |
126 | ```{r save-plot, echo = FALSE, message = FALSE}
127 | ggsave("plot-benchmark-small-1.png")
128 | ```
129 |
130 | ## Large files
131 |
132 | - ~ 25 Mio. values
133 |
134 | - 485 MB on disk
135 |
136 | ```{r prepare-large}
137 | # SpatRaster demo stack
138 | stack_terra = demo_stack_spatraster(500)
139 | value = data.table::data.table(ID = c(0, 1), y = c("negative", "positive"))
140 | terra::set.cats(stack_terra, layer = "y", value = value)
141 | colnames = names(stack_terra)
142 | file_terra = tempfile("terra", fileext = ".tif")
143 | terra::writeRaster(stack_terra, file_terra)
144 |
145 | # RasterBrick demo stack
146 | stack_raster = demo_stack_rasterbrick(500)
147 | colnames_raster = names(stack_raster)
148 | file_raster = tempfile("raster", fileext = ".tif")
149 | raster::writeRaster(stack_raster, file_raster)
150 |
151 | # tasks
152 | stack_terra = terra::rast(file_terra)
153 | backend_terra = DataBackendRaster$new(stack_terra)
154 | task_terra = as_task_regr(backend_terra, target = "x_1")
155 |
156 | stack_raster = raster::brick(file_raster)
157 | names(stack_raster) = colnames_raster
158 | backend_raster = DataBackendRaster$new(stack_raster)
159 | task_raster = as_task_regr(backend_raster, target = "x_1")
160 |
161 | # Train learners
162 | set.seed(42)
163 | row_ids = sample(1:task_terra$nrow, 50)
164 |
165 | learner_task_terra = lrn("regr.rpart")
166 | learner_task_terra$parallel_predict = TRUE
167 | learner_task_terra$train(task_terra, row_ids = row_ids)
168 |
169 | learner_task_raster = lrn("regr.rpart")
170 | learner_task_terra$parallel_predict = TRUE
171 | learner_task_raster$train(task_raster, row_ids = row_ids)
172 |
173 | # non-mlr3 models
174 | rpart_task_terra = rpart::rpart(x_1 ~ ., task_terra$data(rows = row_ids))
175 | rpart_task_raster = rpart::rpart(x_1 ~ ., task_raster$data(rows = row_ids))
176 | ```
177 |
178 | ```{r benchmark}
179 | bm = bench::mark(
180 |
181 | "01-mlr3-terra-4-cores" = {
182 | plan(multicore, workers = 4)
183 | predict_spatial(task_terra, learner_task_terra, chunksize = 2000L)
184 | },
185 |
186 | "02-terra-4-cores" = terra::predict(stack_terra, rpart_task_terra, cores = 4, cpkgs = "rpart"),
187 |
188 | "03-mlr3-raster-4-cores" = {
189 | plan(multicore, workers = 4)
190 | predict_spatial(task_raster, learner_task_raster, chunksize = 2000L, format = "raster")
191 | },
192 |
193 | "04-raster-4-cores" = {
194 | library(raster)
195 | library(rpart)
196 | beginCluster(4, type = "PSOCK")
197 | clusterR(stack_raster, predict, args = list(model = rpart_task_raster))
198 | },
199 |
200 | check = FALSE, filter_gc = FALSE, min_iterations = 3,
201 | max_iterations = 3, memory = FALSE)
202 |
203 | bm$`itr/sec` = NULL
204 | bm$result = NULL
205 | bm$`gc/sec` = NULL
206 | bm$memory = NULL
207 | bm$mem_alloc = NULL
208 |
209 | print(bm)
210 | ```
211 |
212 | ```{r plot-benchmark-large, fig.cap=""}
213 | library(ggplot2)
214 | autoplot(bm, type = "ridge")
215 | ```
216 |
217 | ```{r save-plot-large, echo = FALSE, message = FALSE}
218 | ggsave("plot-benchmark-large-1.png")
219 | ```
220 |
--------------------------------------------------------------------------------
/attic/plot-benchmark-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/attic/plot-benchmark-1.png
--------------------------------------------------------------------------------
/attic/plot-benchmark-large-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/attic/plot-benchmark-large-1.png
--------------------------------------------------------------------------------
/attic/plot-benchmark-small-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/attic/plot-benchmark-small-1.png
--------------------------------------------------------------------------------
/data/leipzig.rda:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/data/leipzig.rda
--------------------------------------------------------------------------------
/inst/WORDLIST:
--------------------------------------------------------------------------------
1 | DataBackend
2 | DataBackendDataTable
3 | DataBackendVector
4 | EPSG
5 | GeoPackage
6 | GeoTIFF
7 | Hyperband
8 | MacBook
9 | Mattermost
10 | NDVI
11 | ORCID
12 | OpenML
13 | Parallelization
14 | SpatRaster
15 | Spatiotemporal
16 | StackOverflow
17 | TaskClassif
18 | TaskClassifST
19 | TaskRegr
20 | TaskRegrST
21 | TaskUnsupervised
22 | WKT
23 | chunksize
24 | cloneable
25 | cmd
26 | dev
27 | github
28 | https
29 | mlr
30 | parallelization
31 | rasters
32 | sfc
33 | socker
34 | spatiotempcv
35 | spatiotemporal
36 | subparts
37 | terra
38 | tracebacks
39 |
--------------------------------------------------------------------------------
/inst/extdata/leipzig_points.gpkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/inst/extdata/leipzig_points.gpkg
--------------------------------------------------------------------------------
/inst/extdata/leipzig_raster.tif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/inst/extdata/leipzig_raster.tif
--------------------------------------------------------------------------------
/man-roxygen/param-chunksize.R:
--------------------------------------------------------------------------------
1 | #' @param chunksize (`integer(1)`)\cr
2 | #' The chunksize determines in how many subparts the prediction task will be
3 | #' split into. The value can be roughly thought of as megabyte of a raster file
4 | #' on disk. For example, if a prediction on a 1 GB file would be carried out
5 | #' with `chunksize = 100L`, the prediction would happen in 10 chunks.
6 | #'
7 | #' The default of `chunksize = 1000L` might be a good compromise between speed
8 | #' and memory usage. If you find yourself running out of memory, reduce this
9 | #' value.
10 |
--------------------------------------------------------------------------------
/man-roxygen/param-data.R:
--------------------------------------------------------------------------------
1 | #' @param data ([terra::SpatRaster])\cr
2 | #' The input [terra::SpatRaster].
3 |
--------------------------------------------------------------------------------
/man-roxygen/param-primary-key.R:
--------------------------------------------------------------------------------
1 | #' @param primary_key (`character(1)` | `integer()`)\cr
2 | #' Name of the primary key column, or integer vector of row ids.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param-quiet.R:
--------------------------------------------------------------------------------
1 | #' @param quiet `[logical]`\cr
2 | #' Whether to suppress possible console output.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param-response-is-factor.R:
--------------------------------------------------------------------------------
1 | #' @param response_is_factor ([`character`])\cr
2 | #' When this backend should be used in a [mlr3::TaskClassif], set `response_is_factor = TRUE`.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param-response.R:
--------------------------------------------------------------------------------
1 | #' @param response ([`character`])\cr
2 | #' The name of the response variable. Only needed when `response_is_factor = TRUE`.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param-task.R:
--------------------------------------------------------------------------------
1 | #' @param task `[Task]`\cr
2 | #' mlr3 task.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param_backend.R:
--------------------------------------------------------------------------------
1 | #' @param backend ([DataBackend])\cr
2 | #' Either a [DataBackend], or any object which is convertible to a [DataBackend] with `as_data_backend()`.
3 | #' E.g., am `sf` will be converted to a [DataBackendDataTable].
4 |
--------------------------------------------------------------------------------
/man-roxygen/param_coordinate_names.R:
--------------------------------------------------------------------------------
1 | #' @param coordinate_names (`character(1)`)\cr
2 | #' The column names of the coordinates in the data.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param_coords_as_features.R:
--------------------------------------------------------------------------------
1 | #' @param coords_as_features (`logical(1)`)\cr
2 | #' If `TRUE`, coordinates are used as features.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param_crs.R:
--------------------------------------------------------------------------------
1 | #' @param crs (`character(1)`)\cr
2 | #' Coordinate reference system.
3 | #' WKT2 or EPSG string.
4 |
--------------------------------------------------------------------------------
/man-roxygen/param_extra_args.R:
--------------------------------------------------------------------------------
1 | #' @param extra_args (named `list()`)\cr
2 | #' Named list of constructor arguments, required for converting task types via [convert_task()].
3 |
--------------------------------------------------------------------------------
/man-roxygen/param_id.R:
--------------------------------------------------------------------------------
1 | #' @param id (`character(1)`)\cr
2 | #' Identifier for the new instance.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param_label.R:
--------------------------------------------------------------------------------
1 | #' @param label (`character(1)`)\cr
2 | #' Label for the new instance.
3 |
--------------------------------------------------------------------------------
/man-roxygen/param_positive.R:
--------------------------------------------------------------------------------
1 | #' @param positive (`character(1)`)\cr
2 | #' Only for binary classification: Name of the positive class.
3 | #' The levels of the target columns are reordered accordingly, so that the first element of `$class_names` is the positive class, and the second element is the negative class.
4 |
--------------------------------------------------------------------------------
/man-roxygen/param_target.R:
--------------------------------------------------------------------------------
1 | #' @param target (`character(1)`)\cr
2 | #' Name of the target column.
3 |
--------------------------------------------------------------------------------
/man/DataBackendRaster.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/DataBackendRaster.R
3 | \name{DataBackendRaster}
4 | \alias{DataBackendRaster}
5 | \title{DataBackend for Raster Objects}
6 | \description{
7 | \link[mlr3:DataBackend]{mlr3::DataBackend} for \link[terra:SpatRaster-class]{terra::SpatRaster} raster objects.
8 | }
9 | \section{Read mode}{
10 |
11 | There are two different ways the reading of values is performed internally:
12 | \itemize{
13 | \item "Block mode" reads complete rows of the raster file and subsets the requested cells.
14 | This mode is faster than "cell mode" if the complete raster file is iterated over.
15 | \item "Cell mode" reads individual cells.
16 | This is faster than "block mode" if only a few cells are sampled.
17 | }
18 |
19 | "Block mode" is activated if \verb{$data(rows)} is used with a increasing integer sequence e.g. \code{200:300}.
20 | If only a single cell is requested, "cell mode" is used.
21 | }
22 |
23 | \section{Super class}{
24 | \code{\link[mlr3:DataBackend]{mlr3::DataBackend}} -> \code{DataBackendRaster}
25 | }
26 | \section{Active bindings}{
27 | \if{html}{\out{}}
28 | \describe{
29 | \item{\code{rownames}}{(\code{integer()})\cr
30 | Returns vector of all distinct row identifiers, i.e. the contents of the primary key column.}
31 |
32 | \item{\code{colnames}}{(\code{character()})\cr
33 | Returns vector of all column names.}
34 |
35 | \item{\code{nrow}}{(\code{integer(1)})\cr
36 | Number of rows (observations).}
37 |
38 | \item{\code{ncol}}{(\code{integer(1)})\cr
39 | Number of columns (variables).}
40 |
41 | \item{\code{stack}}{(\code{SpatRaster})\cr
42 | Raster stack.}
43 | }
44 | \if{html}{\out{
}}
45 | }
46 | \section{Methods}{
47 | \subsection{Public methods}{
48 | \itemize{
49 | \item \href{#method-DataBackendRaster-new}{\code{DataBackendRaster$new()}}
50 | \item \href{#method-DataBackendRaster-data}{\code{DataBackendRaster$data()}}
51 | \item \href{#method-DataBackendRaster-head}{\code{DataBackendRaster$head()}}
52 | \item \href{#method-DataBackendRaster-distinct}{\code{DataBackendRaster$distinct()}}
53 | \item \href{#method-DataBackendRaster-missings}{\code{DataBackendRaster$missings()}}
54 | \item \href{#method-DataBackendRaster-coordinates}{\code{DataBackendRaster$coordinates()}}
55 | }
56 | }
57 | \if{html}{\out{
58 | Inherited methods
59 |
63 |
64 | }}
65 | \if{html}{\out{
}}
66 | \if{html}{\out{}}
67 | \if{latex}{\out{\hypertarget{method-DataBackendRaster-new}{}}}
68 | \subsection{Method \code{new()}}{
69 | Creates a new instance of this \link[R6:R6Class]{R6} class.
70 | \subsection{Usage}{
71 | \if{html}{\out{}}\preformatted{DataBackendRaster$new(data)}\if{html}{\out{
}}
72 | }
73 |
74 | \subsection{Arguments}{
75 | \if{html}{\out{}}
76 | \describe{
77 | \item{\code{data}}{(\link[terra:SpatRaster-class]{terra::SpatRaster})\cr
78 | The input \link[terra:SpatRaster-class]{terra::SpatRaster}.}
79 | }
80 | \if{html}{\out{
}}
81 | }
82 | }
83 | \if{html}{\out{
}}
84 | \if{html}{\out{}}
85 | \if{latex}{\out{\hypertarget{method-DataBackendRaster-data}{}}}
86 | \subsection{Method \code{data()}}{
87 | Returns a slice of the raster in the specified format.
88 | Currently, the only supported formats is \code{"data.table"}.
89 |
90 | The rows must be addressed as vector of cells indices, columns must be
91 | referred to via layer names. Queries for rows with no matching row id and
92 | queries for columns with no matching column name are silently ignored.
93 |
94 | Rows are guaranteed to be returned in the same order as \code{rows}, columns
95 | may be returned in an arbitrary order. Duplicated row ids result in
96 | duplicated rows, duplicated column names lead to an exception.
97 | \subsection{Usage}{
98 | \if{html}{\out{}}\preformatted{DataBackendRaster$data(rows, cols, data_format = "data.table")}\if{html}{\out{
}}
99 | }
100 |
101 | \subsection{Arguments}{
102 | \if{html}{\out{}}
103 | \describe{
104 | \item{\code{rows}}{\code{integer()}\cr
105 | Row indices. Row indices start with 1 in the upper left corner in the
106 | raster, increase from left to right and then from top to bottom. The last
107 | cell is in the bottom right corner and the row index equals the number of
108 | cells in the raster.}
109 |
110 | \item{\code{cols}}{\code{character()}\cr
111 | Column names.}
112 |
113 | \item{\code{data_format}}{(\code{character(1)})\cr
114 | Desired data format. Currently only \code{"data.table"} supported.}
115 | }
116 | \if{html}{\out{
}}
117 | }
118 | }
119 | \if{html}{\out{
}}
120 | \if{html}{\out{}}
121 | \if{latex}{\out{\hypertarget{method-DataBackendRaster-head}{}}}
122 | \subsection{Method \code{head()}}{
123 | Retrieve the first \code{n} rows.
124 | \subsection{Usage}{
125 | \if{html}{\out{}}\preformatted{DataBackendRaster$head(n = 6L)}\if{html}{\out{
}}
126 | }
127 |
128 | \subsection{Arguments}{
129 | \if{html}{\out{}}
130 | \describe{
131 | \item{\code{n}}{(\code{integer(1)})\cr
132 | Number of rows.}
133 | }
134 | \if{html}{\out{
}}
135 | }
136 | \subsection{Returns}{
137 | \code{\link[data.table:data.table]{data.table::data.table()}} of the first \code{n} rows.
138 | }
139 | }
140 | \if{html}{\out{
}}
141 | \if{html}{\out{}}
142 | \if{latex}{\out{\hypertarget{method-DataBackendRaster-distinct}{}}}
143 | \subsection{Method \code{distinct()}}{
144 | Returns a named list of vectors of distinct values for each column
145 | specified. If \code{na_rm} is \code{TRUE}, missing values are removed from the
146 | returned vectors of distinct values. Non-existing rows and columns are
147 | silently ignored.
148 | \subsection{Usage}{
149 | \if{html}{\out{}}\preformatted{DataBackendRaster$distinct(rows, cols, na_rm = TRUE)}\if{html}{\out{
}}
150 | }
151 |
152 | \subsection{Arguments}{
153 | \if{html}{\out{}}
154 | \describe{
155 | \item{\code{rows}}{\code{integer()}\cr
156 | Row indices. Row indices start with 1 in the upper left corner in the
157 | raster, increase from left to right and then from top to bottom. The last
158 | cell is in the bottom right corner and the row index equals the number of
159 | cells in the raster.}
160 |
161 | \item{\code{cols}}{\code{character()}\cr
162 | Column names.}
163 |
164 | \item{\code{na_rm}}{\code{logical(1)}\cr
165 | Whether to remove NAs or not.}
166 | }
167 | \if{html}{\out{
}}
168 | }
169 | \subsection{Returns}{
170 | Named \code{list()} of distinct values.
171 | }
172 | }
173 | \if{html}{\out{
}}
174 | \if{html}{\out{}}
175 | \if{latex}{\out{\hypertarget{method-DataBackendRaster-missings}{}}}
176 | \subsection{Method \code{missings()}}{
177 | Returns the number of missing values per column in the specified slice
178 | of data. Non-existing rows and columns are silently ignored.
179 | \subsection{Usage}{
180 | \if{html}{\out{}}\preformatted{DataBackendRaster$missings(rows, cols)}\if{html}{\out{
}}
181 | }
182 |
183 | \subsection{Arguments}{
184 | \if{html}{\out{}}
185 | \describe{
186 | \item{\code{rows}}{\code{integer()}\cr
187 | Row indices. Row indices start with 1 in the upper left corner in the
188 | raster, increase from left to right and then from top to bottom. The last
189 | cell is in the bottom right corner and the row index equals the number of
190 | cells in the raster.}
191 |
192 | \item{\code{cols}}{\code{character()}\cr
193 | Column names.}
194 | }
195 | \if{html}{\out{
}}
196 | }
197 | \subsection{Returns}{
198 | Total of missing values per column (named \code{numeric()}).
199 | }
200 | }
201 | \if{html}{\out{
}}
202 | \if{html}{\out{}}
203 | \if{latex}{\out{\hypertarget{method-DataBackendRaster-coordinates}{}}}
204 | \subsection{Method \code{coordinates()}}{
205 | Returns the coordinates of \code{rows}.
206 | If \code{rows} is missing, all coordinates are returned.
207 | \subsection{Usage}{
208 | \if{html}{\out{}}\preformatted{DataBackendRaster$coordinates(rows)}\if{html}{\out{
}}
209 | }
210 |
211 | \subsection{Arguments}{
212 | \if{html}{\out{}}
213 | \describe{
214 | \item{\code{rows}}{\code{integer()}\cr
215 | Row indices. Row indices start with 1 in the upper left corner in the
216 | raster, increase from left to right and then from top to bottom. The last
217 | cell is in the bottom right corner and the row index equals the number of
218 | cells in the raster.}
219 | }
220 | \if{html}{\out{
}}
221 | }
222 | \subsection{Returns}{
223 | \code{\link[data.table:data.table]{data.table::data.table()}} of coordinates of \code{rows}.
224 | }
225 | }
226 | }
227 |
--------------------------------------------------------------------------------
/man/DataBackendVector.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/DataBackendVector.R
3 | \name{DataBackendVector}
4 | \alias{DataBackendVector}
5 | \title{DataBackend for Vector Objects}
6 | \description{
7 | \link[mlr3:DataBackend]{mlr3::DataBackend} for \link[sf:sf]{sf::sf} vector objects.
8 | }
9 | \section{Super classes}{
10 | \code{\link[mlr3:DataBackend]{mlr3::DataBackend}} -> \code{\link[mlr3:DataBackendDataTable]{mlr3::DataBackendDataTable}} -> \code{DataBackendVector}
11 | }
12 | \section{Active bindings}{
13 | \if{html}{\out{}}
14 | \describe{
15 | \item{\code{sfc}}{(\link[sf:sfc]{sf::sfc})\cr
16 | Returns the sfc object.}
17 | }
18 | \if{html}{\out{
}}
19 | }
20 | \section{Methods}{
21 | \subsection{Public methods}{
22 | \itemize{
23 | \item \href{#method-DataBackendVector-new}{\code{DataBackendVector$new()}}
24 | }
25 | }
26 | \if{html}{\out{
27 | Inherited methods
28 |
36 |
37 | }}
38 | \if{html}{\out{
}}
39 | \if{html}{\out{}}
40 | \if{latex}{\out{\hypertarget{method-DataBackendVector-new}{}}}
41 | \subsection{Method \code{new()}}{
42 | Creates a new instance of this \link[R6:R6Class]{R6} class.
43 | \subsection{Usage}{
44 | \if{html}{\out{}}\preformatted{DataBackendVector$new(data, primary_key)}\if{html}{\out{
}}
45 | }
46 |
47 | \subsection{Arguments}{
48 | \if{html}{\out{}}
49 | \describe{
50 | \item{\code{data}}{(\code{sf})\cr
51 | A raster object.}
52 |
53 | \item{\code{primary_key}}{(\code{character(1)} | \code{integer()})\cr
54 | Name of the primary key column, or integer vector of row ids.}
55 | }
56 | \if{html}{\out{
}}
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/man/TaskClassifST.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/TaskClassifST.R
3 | \name{TaskClassifST}
4 | \alias{TaskClassifST}
5 | \title{Spatiotemporal Classification Task}
6 | \description{
7 | This task specializes \link{TaskClassif} for spatiotemporal classification problems.
8 |
9 | A spatial example task is available via \code{tsk("ecuador")}.
10 |
11 | The coordinate reference system passed during initialization must match the one which was used during data creation, otherwise offsets of multiple meters may occur.
12 | By default, coordinates are not used as features.
13 | This can be changed by setting \code{coords_as_features = TRUE}.
14 | }
15 | \section{Super classes}{
16 | \code{\link[mlr3:Task]{mlr3::Task}} -> \code{\link[mlr3:TaskSupervised]{mlr3::TaskSupervised}} -> \code{\link[mlr3:TaskClassif]{mlr3::TaskClassif}} -> \code{TaskClassifST}
17 | }
18 | \section{Active bindings}{
19 | \if{html}{\out{}}
20 | \describe{
21 | \item{\code{crs}}{(\code{character(1)})\cr
22 | Returns coordinate reference system of task.}
23 |
24 | \item{\code{coordinate_names}}{(\code{character()})\cr
25 | Returns coordinate names.}
26 |
27 | \item{\code{coords_as_features}}{(\code{logical(1)})\cr
28 | If \code{TRUE}, coordinates are used as features.}
29 | }
30 | \if{html}{\out{
}}
31 | }
32 | \section{Methods}{
33 | \subsection{Public methods}{
34 | \itemize{
35 | \item \href{#method-TaskClassifST-new}{\code{TaskClassifST$new()}}
36 | \item \href{#method-TaskClassifST-coordinates}{\code{TaskClassifST$coordinates()}}
37 | \item \href{#method-TaskClassifST-print}{\code{TaskClassifST$print()}}
38 | \item \href{#method-TaskClassifST-clone}{\code{TaskClassifST$clone()}}
39 | }
40 | }
41 | \if{html}{\out{
42 | Inherited methods
43 |
63 |
64 | }}
65 | \if{html}{\out{
}}
66 | \if{html}{\out{}}
67 | \if{latex}{\out{\hypertarget{method-TaskClassifST-new}{}}}
68 | \subsection{Method \code{new()}}{
69 | Creates a new instance of this \link[R6:R6Class]{R6} class.
70 | The function \code{\link[=as_task_classif_st]{as_task_classif_st()}} provides an alternative way to construct classification tasks.
71 | \subsection{Usage}{
72 | \if{html}{\out{}}\preformatted{TaskClassifST$new(
73 | id,
74 | backend,
75 | target,
76 | positive = NULL,
77 | label = NA_character_,
78 | coordinate_names,
79 | crs = NA_character_,
80 | coords_as_features = FALSE,
81 | extra_args = list()
82 | )}\if{html}{\out{
}}
83 | }
84 |
85 | \subsection{Arguments}{
86 | \if{html}{\out{}}
87 | \describe{
88 | \item{\code{id}}{(\code{character(1)})\cr
89 | Identifier for the new instance.}
90 |
91 | \item{\code{backend}}{(\link{DataBackend})\cr
92 | Either a \link{DataBackend}, or any object which is convertible to a \link{DataBackend} with \code{as_data_backend()}.
93 | E.g., am \code{sf} will be converted to a \link{DataBackendDataTable}.}
94 |
95 | \item{\code{target}}{(\code{character(1)})\cr
96 | Name of the target column.}
97 |
98 | \item{\code{positive}}{(\code{character(1)})\cr
99 | Only for binary classification: Name of the positive class.
100 | The levels of the target columns are reordered accordingly, so that the first element of \verb{$class_names} is the positive class, and the second element is the negative class.}
101 |
102 | \item{\code{label}}{(\code{character(1)})\cr
103 | Label for the new instance.}
104 |
105 | \item{\code{coordinate_names}}{(\code{character(1)})\cr
106 | The column names of the coordinates in the data.}
107 |
108 | \item{\code{crs}}{(\code{character(1)})\cr
109 | Coordinate reference system.
110 | WKT2 or EPSG string.}
111 |
112 | \item{\code{coords_as_features}}{(\code{logical(1)})\cr
113 | If \code{TRUE}, coordinates are used as features.}
114 |
115 | \item{\code{extra_args}}{(named \code{list()})\cr
116 | Named list of constructor arguments, required for converting task types via \code{\link[=convert_task]{convert_task()}}.}
117 | }
118 | \if{html}{\out{
}}
119 | }
120 | }
121 | \if{html}{\out{
}}
122 | \if{html}{\out{}}
123 | \if{latex}{\out{\hypertarget{method-TaskClassifST-coordinates}{}}}
124 | \subsection{Method \code{coordinates()}}{
125 | Returns coordinates of observations.
126 | \subsection{Usage}{
127 | \if{html}{\out{}}\preformatted{TaskClassifST$coordinates(row_ids = NULL)}\if{html}{\out{
}}
128 | }
129 |
130 | \subsection{Arguments}{
131 | \if{html}{\out{}}
132 | \describe{
133 | \item{\code{row_ids}}{(\code{integer()})\cr
134 | Vector of rows indices as subset of \code{task$row_ids}.}
135 | }
136 | \if{html}{\out{
}}
137 | }
138 | \subsection{Returns}{
139 | \code{\link[data.table:data.table]{data.table::data.table()}}
140 | }
141 | }
142 | \if{html}{\out{
}}
143 | \if{html}{\out{}}
144 | \if{latex}{\out{\hypertarget{method-TaskClassifST-print}{}}}
145 | \subsection{Method \code{print()}}{
146 | Print the task.
147 | \subsection{Usage}{
148 | \if{html}{\out{}}\preformatted{TaskClassifST$print(...)}\if{html}{\out{
}}
149 | }
150 |
151 | \subsection{Arguments}{
152 | \if{html}{\out{}}
153 | \describe{
154 | \item{\code{...}}{Arguments passed to the \verb{$print()} method of the superclass.}
155 | }
156 | \if{html}{\out{
}}
157 | }
158 | }
159 | \if{html}{\out{
}}
160 | \if{html}{\out{}}
161 | \if{latex}{\out{\hypertarget{method-TaskClassifST-clone}{}}}
162 | \subsection{Method \code{clone()}}{
163 | The objects of this class are cloneable with this method.
164 | \subsection{Usage}{
165 | \if{html}{\out{}}\preformatted{TaskClassifST$clone(deep = FALSE)}\if{html}{\out{
}}
166 | }
167 |
168 | \subsection{Arguments}{
169 | \if{html}{\out{}}
170 | \describe{
171 | \item{\code{deep}}{Whether to make a deep clone.}
172 | }
173 | \if{html}{\out{
}}
174 | }
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/man/TaskRegrST.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/TaskRegrST.R
3 | \name{TaskRegrST}
4 | \alias{TaskRegrST}
5 | \title{Spatiotemporal Regression Task}
6 | \description{
7 | This task specializes \link{TaskRegr} for spatiotemporal regression problems.
8 |
9 | A spatial example task is available via \code{tsk("cookfarm_mlr3")}.
10 |
11 | The coordinate reference system passed during initialization must match the one which was used during data creation, otherwise offsets of multiple meters may occur.
12 | By default, coordinates are not used as features.
13 | This can be changed by setting \code{coords_as_features = TRUE}.
14 | }
15 | \section{Super classes}{
16 | \code{\link[mlr3:Task]{mlr3::Task}} -> \code{\link[mlr3:TaskSupervised]{mlr3::TaskSupervised}} -> \code{\link[mlr3:TaskRegr]{mlr3::TaskRegr}} -> \code{TaskRegrST}
17 | }
18 | \section{Active bindings}{
19 | \if{html}{\out{}}
20 | \describe{
21 | \item{\code{crs}}{(\code{character(1)})\cr
22 | Returns coordinate reference system of the task.}
23 |
24 | \item{\code{coordinate_names}}{(\code{character()})\cr
25 | Returns coordinate names.}
26 |
27 | \item{\code{coords_as_features}}{(\code{logical(1)})\cr
28 | If \code{TRUE}, coordinates are used as features.}
29 | }
30 | \if{html}{\out{
}}
31 | }
32 | \section{Methods}{
33 | \subsection{Public methods}{
34 | \itemize{
35 | \item \href{#method-TaskRegrST-new}{\code{TaskRegrST$new()}}
36 | \item \href{#method-TaskRegrST-coordinates}{\code{TaskRegrST$coordinates()}}
37 | \item \href{#method-TaskRegrST-print}{\code{TaskRegrST$print()}}
38 | \item \href{#method-TaskRegrST-clone}{\code{TaskRegrST$clone()}}
39 | }
40 | }
41 | \if{html}{\out{
42 | Inherited methods
43 |
63 |
64 | }}
65 | \if{html}{\out{
}}
66 | \if{html}{\out{}}
67 | \if{latex}{\out{\hypertarget{method-TaskRegrST-new}{}}}
68 | \subsection{Method \code{new()}}{
69 | Creates a new instance of this \link[R6:R6Class]{R6} class.
70 | The function \code{\link[=as_task_regr_st]{as_task_regr_st()}} provides an alternative way to construct classification tasks.
71 | \subsection{Usage}{
72 | \if{html}{\out{}}\preformatted{TaskRegrST$new(
73 | id,
74 | backend,
75 | target,
76 | label = NA_character_,
77 | coordinate_names,
78 | crs = NA_character_,
79 | coords_as_features = FALSE,
80 | extra_args = list()
81 | )}\if{html}{\out{
}}
82 | }
83 |
84 | \subsection{Arguments}{
85 | \if{html}{\out{}}
86 | \describe{
87 | \item{\code{id}}{(\code{character(1)})\cr
88 | Identifier for the new instance.}
89 |
90 | \item{\code{backend}}{(\link{DataBackend})\cr
91 | Either a \link{DataBackend}, or any object which is convertible to a \link{DataBackend} with \code{as_data_backend()}.
92 | E.g., am \code{sf} will be converted to a \link{DataBackendDataTable}.}
93 |
94 | \item{\code{target}}{(\code{character(1)})\cr
95 | Name of the target column.}
96 |
97 | \item{\code{label}}{(\code{character(1)})\cr
98 | Label for the new instance.}
99 |
100 | \item{\code{coordinate_names}}{(\code{character(1)})\cr
101 | The column names of the coordinates in the data.}
102 |
103 | \item{\code{crs}}{(\code{character(1)})\cr
104 | Coordinate reference system.
105 | WKT2 or EPSG string.}
106 |
107 | \item{\code{coords_as_features}}{(\code{logical(1)})\cr
108 | If \code{TRUE}, coordinates are used as features.}
109 |
110 | \item{\code{extra_args}}{(named \code{list()})\cr
111 | Named list of constructor arguments, required for converting task types via \code{\link[=convert_task]{convert_task()}}.}
112 | }
113 | \if{html}{\out{
}}
114 | }
115 | }
116 | \if{html}{\out{
}}
117 | \if{html}{\out{}}
118 | \if{latex}{\out{\hypertarget{method-TaskRegrST-coordinates}{}}}
119 | \subsection{Method \code{coordinates()}}{
120 | Returns coordinates of observations.
121 | \subsection{Usage}{
122 | \if{html}{\out{}}\preformatted{TaskRegrST$coordinates(row_ids = NULL)}\if{html}{\out{
}}
123 | }
124 |
125 | \subsection{Arguments}{
126 | \if{html}{\out{}}
127 | \describe{
128 | \item{\code{row_ids}}{(\code{integer()})\cr
129 | Vector of rows indices as subset of \code{task$row_ids}.}
130 | }
131 | \if{html}{\out{
}}
132 | }
133 | \subsection{Returns}{
134 | \code{\link[data.table:data.table]{data.table::data.table()}}
135 | }
136 | }
137 | \if{html}{\out{
}}
138 | \if{html}{\out{}}
139 | \if{latex}{\out{\hypertarget{method-TaskRegrST-print}{}}}
140 | \subsection{Method \code{print()}}{
141 | Print the task.
142 | \subsection{Usage}{
143 | \if{html}{\out{}}\preformatted{TaskRegrST$print(...)}\if{html}{\out{
}}
144 | }
145 |
146 | \subsection{Arguments}{
147 | \if{html}{\out{}}
148 | \describe{
149 | \item{\code{...}}{Arguments passed to the \verb{$print()} method of the superclass.}
150 | }
151 | \if{html}{\out{
}}
152 | }
153 | }
154 | \if{html}{\out{
}}
155 | \if{html}{\out{}}
156 | \if{latex}{\out{\hypertarget{method-TaskRegrST-clone}{}}}
157 | \subsection{Method \code{clone()}}{
158 | The objects of this class are cloneable with this method.
159 | \subsection{Usage}{
160 | \if{html}{\out{}}\preformatted{TaskRegrST$clone(deep = FALSE)}\if{html}{\out{
}}
161 | }
162 |
163 | \subsection{Arguments}{
164 | \if{html}{\out{}}
165 | \describe{
166 | \item{\code{deep}}{Whether to make a deep clone.}
167 | }
168 | \if{html}{\out{
}}
169 | }
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/man/as_data_backend.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/DataBackendRaster.R, R/DataBackendVector.R
3 | \name{as_data_backend.stars}
4 | \alias{as_data_backend.stars}
5 | \alias{as_data_backend.SpatRaster}
6 | \alias{as_data_backend.RasterBrick}
7 | \alias{as_data_backend.RasterStack}
8 | \alias{as_data_backend.sf}
9 | \title{Coerce to spatial DataBackend}
10 | \usage{
11 | \method{as_data_backend}{stars}(data, primary_key = NULL, ...)
12 |
13 | \method{as_data_backend}{SpatRaster}(data, primary_key = NULL, ...)
14 |
15 | \method{as_data_backend}{RasterBrick}(data, primary_key = NULL, ...)
16 |
17 | \method{as_data_backend}{RasterStack}(data, primary_key = NULL, ...)
18 |
19 | \method{as_data_backend}{sf}(data, primary_key = NULL, keep_rownames = FALSE, ...)
20 | }
21 | \arguments{
22 | \item{data}{(\link[terra:SpatRaster-class]{terra::SpatRaster})\cr
23 | The input \link[terra:SpatRaster-class]{terra::SpatRaster}.}
24 |
25 | \item{primary_key}{(\code{character(1)} | \code{integer()})\cr
26 | Name of the primary key column, or integer vector of row ids.}
27 |
28 | \item{...}{(\code{any})\cr
29 | Not used.}
30 |
31 | \item{keep_rownames}{(\code{logical(1)} | \code{character(1)})\cr
32 | If \code{TRUE} or a single string, keeps the row names of \code{data} as a new column.
33 | The column is named like the provided string, defaulting to \code{"..rownames"} for \code{keep_rownames == TRUE}.
34 | Note that the created column will be used as a regular feature by the task unless you manually change the column role.
35 | Also see \code{\link[data.table:as.data.table]{data.table::as.data.table()}}.}
36 | }
37 | \value{
38 | \link{DataBackend}.
39 | }
40 | \description{
41 | Wraps a \link{DataBackend} around spatial objects.
42 | Currently these S3 methods are only alternative ways for writing \code{DataBackendRaster$new()}.
43 | They do not support coercing from other backends yet.
44 | }
45 |
--------------------------------------------------------------------------------
/man/as_task_classif_st.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/as_task_classif_st.R
3 | \name{as_task_classif_st}
4 | \alias{as_task_classif_st}
5 | \alias{as_task_classif_st.TaskClassifST}
6 | \alias{as_task_classif_st.data.frame}
7 | \alias{as_task_classif_st.DataBackend}
8 | \alias{as_task_classif_st.sf}
9 | \alias{as_task_classif_st.TaskRegrST}
10 | \title{Convert to a Spatiotemporal Classification Task}
11 | \usage{
12 | as_task_classif_st(x, ...)
13 |
14 | \method{as_task_classif_st}{TaskClassifST}(x, clone = FALSE, ...)
15 |
16 | \method{as_task_classif_st}{data.frame}(
17 | x,
18 | target,
19 | id = deparse(substitute(x)),
20 | positive = NULL,
21 | coordinate_names,
22 | crs = NA_character_,
23 | coords_as_features = FALSE,
24 | label = NA_character_,
25 | ...
26 | )
27 |
28 | \method{as_task_classif_st}{DataBackend}(
29 | x,
30 | target,
31 | id = deparse(substitute(x)),
32 | positive = NULL,
33 | coordinate_names,
34 | crs,
35 | coords_as_features = FALSE,
36 | label = NA_character_,
37 | ...
38 | )
39 |
40 | \method{as_task_classif_st}{sf}(
41 | x,
42 | target = NULL,
43 | id = deparse(substitute(x)),
44 | positive = NULL,
45 | coords_as_features = FALSE,
46 | label = NA_character_,
47 | ...
48 | )
49 |
50 | \method{as_task_classif_st}{TaskRegrST}(
51 | x,
52 | target = NULL,
53 | drop_original_target = FALSE,
54 | drop_levels = TRUE,
55 | ...
56 | )
57 | }
58 | \arguments{
59 | \item{x}{(any)\cr
60 | Object to convert.}
61 |
62 | \item{...}{(any)\cr
63 | Additional arguments.}
64 |
65 | \item{clone}{(\code{logical(1)})\cr
66 | If \code{TRUE}, ensures that the returned object is not the same as the input \code{x}.}
67 |
68 | \item{target}{(\code{character(1)})\cr
69 | Name of the target column.}
70 |
71 | \item{id}{(\code{character(1)})\cr
72 | Id for the new task.
73 | Defaults to the (deparsed and substituted) name of the data argument.}
74 |
75 | \item{positive}{(\code{character(1)})\cr
76 | Level of the positive class. See \link[mlr3]{TaskClassif}.}
77 |
78 | \item{coordinate_names}{(\code{character(1)})\cr
79 | The column names of the coordinates in the data.}
80 |
81 | \item{crs}{(\code{character(1)})\cr
82 | Coordinate reference system.
83 | WKT2 or EPSG string.}
84 |
85 | \item{coords_as_features}{(\code{logical(1)})\cr
86 | If \code{TRUE}, coordinates are used as features.}
87 |
88 | \item{label}{(\code{character(1)})\cr
89 | Label for the new instance.}
90 |
91 | \item{drop_original_target}{(\code{logical(1)})\cr
92 | If \code{FALSE} (default), the original target is added as a feature.
93 | Otherwise the original target is dropped.}
94 |
95 | \item{drop_levels}{(\code{logical(1)})\cr
96 | If \code{TRUE} (default), unused levels of the new target variable are dropped.}
97 | }
98 | \value{
99 | \link{TaskClassifST}
100 | }
101 | \description{
102 | Convert object to a \link{TaskClassifST}.
103 | This is a S3 generic, specialized for at least the following objects:
104 | \enumerate{
105 | \item \link{TaskClassifST}: Ensure the identity.
106 | \item \code{\link[=data.frame]{data.frame()}} and \link{DataBackend}: Provides an alternative to the constructor of \link{TaskClassifST}.
107 | \item \link[sf:sf]{sf::sf}: Extracts spatial meta data before construction.
108 | \item \link{TaskRegr}: Calls \code{\link[=convert_task]{convert_task()}}.
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/man/as_task_regr_st.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/as_task_regr_st.R
3 | \name{as_task_regr_st}
4 | \alias{as_task_regr_st}
5 | \alias{as_task_regr_st.TaskRegrST}
6 | \alias{as_task_regr_st.data.frame}
7 | \alias{as_task_regr_st.DataBackend}
8 | \alias{as_task_regr_st.sf}
9 | \alias{as_task_regr_st.TaskClassifST}
10 | \title{Convert to a Spatiotemporal Regression Task}
11 | \usage{
12 | as_task_regr_st(x, ...)
13 |
14 | \method{as_task_regr_st}{TaskRegrST}(x, clone = FALSE, ...)
15 |
16 | \method{as_task_regr_st}{data.frame}(
17 | x,
18 | target,
19 | id = deparse(substitute(x)),
20 | coordinate_names,
21 | crs = NA_character_,
22 | coords_as_features = FALSE,
23 | label = NA_character_,
24 | ...
25 | )
26 |
27 | \method{as_task_regr_st}{DataBackend}(
28 | x,
29 | target,
30 | id = deparse(substitute(x)),
31 | coordinate_names,
32 | crs,
33 | coords_as_features = FALSE,
34 | label = NA_character_,
35 | ...
36 | )
37 |
38 | \method{as_task_regr_st}{sf}(
39 | x,
40 | target = NULL,
41 | id = deparse(substitute(x)),
42 | coords_as_features = FALSE,
43 | label = NA_character_,
44 | ...
45 | )
46 |
47 | \method{as_task_regr_st}{TaskClassifST}(
48 | x,
49 | target = NULL,
50 | drop_original_target = FALSE,
51 | drop_levels = TRUE,
52 | ...
53 | )
54 | }
55 | \arguments{
56 | \item{x}{(any)\cr
57 | Object to convert.}
58 |
59 | \item{...}{(any)\cr
60 | Additional arguments.}
61 |
62 | \item{clone}{(\code{logical(1)})\cr
63 | If \code{TRUE}, ensures that the returned object is not the same as the input \code{x}.}
64 |
65 | \item{target}{(\code{character(1)})\cr
66 | Name of the target column.}
67 |
68 | \item{id}{(\code{character(1)})\cr
69 | Id for the new task.
70 | Defaults to the (deparsed and substituted) name of the data argument.}
71 |
72 | \item{coordinate_names}{(\code{character(1)})\cr
73 | The column names of the coordinates in the data.}
74 |
75 | \item{crs}{(\code{character(1)})\cr
76 | Coordinate reference system.
77 | WKT2 or EPSG string.}
78 |
79 | \item{coords_as_features}{(\code{logical(1)})\cr
80 | If \code{TRUE}, coordinates are used as features.}
81 |
82 | \item{label}{(\code{character(1)})\cr
83 | Label for the new instance.}
84 |
85 | \item{drop_original_target}{(\code{logical(1)})\cr
86 | If \code{FALSE} (default), the original target is added as a feature.
87 | Otherwise the original target is dropped.}
88 |
89 | \item{drop_levels}{(\code{logical(1)})\cr
90 | If \code{TRUE} (default), unused levels of the new target variable are dropped.}
91 | }
92 | \value{
93 | \link{TaskRegrST}
94 | }
95 | \description{
96 | Convert object to a \link{TaskRegrST}.
97 | This is a S3 generic, specialized for at least the following objects:
98 | \enumerate{
99 | \item \link{TaskRegrST}: Ensure the identity.
100 | \item \code{\link[=data.frame]{data.frame()}} and \link{DataBackend}: Provides an alternative to the constructor of \link{TaskRegrST}.
101 | \item \link[sf:sf]{sf::sf}: Extracts spatial meta data before construction.
102 | \item \link{TaskClassif}: Calls \code{\link[=convert_task]{convert_task()}}.
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/man/block_size.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/helper.R
3 | \name{block_size}
4 | \alias{block_size}
5 | \title{Split Raster Into Chunks}
6 | \usage{
7 | block_size(raster, chunksize)
8 | }
9 | \arguments{
10 | \item{raster}{(\link[terra:SpatRaster-class]{terra::SpatRaster})\cr
11 | Raster to be split into chunks.}
12 |
13 | \item{chunksize}{(\code{integer(1)})\cr
14 | The chunksize determines in how many subparts the prediction task will be
15 | split into. The value can be roughly thought of as megabyte of a raster file
16 | on disk. For example, if a prediction on a 1 GB file would be carried out
17 | with \code{chunksize = 100L}, the prediction would happen in 10 chunks.
18 |
19 | The default of \code{chunksize = 1000L} might be a good compromise between speed
20 | and memory usage. If you find yourself running out of memory, reduce this
21 | value.}
22 | }
23 | \description{
24 | Splits raster into chunks.
25 | }
26 | \keyword{internal}
27 |
--------------------------------------------------------------------------------
/man/factor_layer.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \name{factor_layer}
4 | \alias{factor_layer}
5 | \title{Factor Layer Generator}
6 | \usage{
7 | factor_layer(id, levels, in_memory = FALSE)
8 | }
9 | \arguments{
10 | \item{id}{(\code{character(1)})\cr
11 | Layer id.}
12 |
13 | \item{levels}{(\code{character()})\cr
14 | Factor levels.}
15 |
16 | \item{in_memory}{(\code{logical(1)})\cr
17 | If \code{FALSE} (default), layer is written to disk.}
18 | }
19 | \value{
20 | Named \code{list()}
21 | }
22 | \description{
23 | Generates a factor layer when passed to \code{\link[=generate_stack]{generate_stack()}}.
24 | }
25 | \keyword{internal}
26 |
--------------------------------------------------------------------------------
/man/figures/land_cover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/man/figures/land_cover.png
--------------------------------------------------------------------------------
/man/figures/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/man/figures/logo.png
--------------------------------------------------------------------------------
/man/figures/sentinel.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/man/figures/sentinel.png
--------------------------------------------------------------------------------
/man/generate_stack.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \name{generate_stack}
4 | \alias{generate_stack}
5 | \title{Generate Raster Stack}
6 | \usage{
7 | generate_stack(
8 | layers,
9 | layer_size = NULL,
10 | dimension = NULL,
11 | multi_layer_file = FALSE
12 | )
13 | }
14 | \arguments{
15 | \item{layers}{(List of \code{\link[=numeric_layer]{numeric_layer()}} and \code{\link[=factor_layer]{factor_layer()}})\cr
16 | List of layers.}
17 |
18 | \item{layer_size}{(\code{numeric(1)})\cr
19 | Size of a single layer in megabytes.}
20 |
21 | \item{dimension}{(\code{integer(1)})\cr
22 | Dimension of the squared layers.}
23 |
24 | \item{multi_layer_file}{(\code{logical(1)})\cr
25 | If \code{TRUE}, raster is written to disk as a single multi-layer file.
26 | Overwrites \code{ìn_memory} argument of \code{numeric_layer()} and \code{factor_layer()}.
27 |
28 | \code{layer_size} and \code{dimension} are mutually exclusive.}
29 | }
30 | \value{
31 | \link[terra:SpatRaster-class]{terra::SpatRaster}
32 | }
33 | \description{
34 | Generates a raster stack.
35 | }
36 | \keyword{internal}
37 |
--------------------------------------------------------------------------------
/man/leipzig.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/TaskClassif_leipzig.R
3 | \docType{data}
4 | \name{leipzig}
5 | \alias{leipzig}
6 | \alias{mlr_tasks_leipzig}
7 | \title{Leipzig Land Cover Task}
8 | \source{
9 | Copernicus Sentinel Data (2021). Retrieved from Copernicus Open Access Hub and processed by European Space Agency.
10 | }
11 | \description{
12 | Point survey of land cover in Leipzig.
13 | Includes Sentinel-2 spectral bands and NDVI.
14 | }
15 | \examples{
16 | if (requireNamespace("sf")) {
17 | library(sf)
18 | data("leipzig", package = "mlr3spatial")
19 | print(leipzig)
20 | }
21 | }
22 | \keyword{data}
23 |
--------------------------------------------------------------------------------
/man/mask_stack.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \name{mask_stack}
4 | \alias{mask_stack}
5 | \title{Sample Points in Raster Stack}
6 | \usage{
7 | mask_stack(stack)
8 | }
9 | \arguments{
10 | \item{stack}{(\link[terra:SpatRaster-class]{terra::SpatRaster})\cr
11 | Raster stack.}
12 | }
13 | \value{
14 | \link[terra:SpatRaster-class]{terra::SpatRaster}
15 | }
16 | \description{
17 | Masks stack to a circular area of interest.
18 | }
19 | \keyword{internal}
20 |
--------------------------------------------------------------------------------
/man/mlr3spatial-package.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/zzz.R
3 | \docType{package}
4 | \name{mlr3spatial-package}
5 | \alias{mlr3spatial}
6 | \alias{mlr3spatial-package}
7 | \title{mlr3spatial: Support for Spatial Objects Within the 'mlr3' Ecosystem}
8 | \description{
9 | \if{html}{\figure{logo.png}{options: style='float: right' alt='logo' width='120'}}
10 |
11 | Extends the 'mlr3' ML framework with methods for spatial objects. Data storage and prediction are supported for packages 'terra', 'raster' and 'stars'.
12 | }
13 | \section{Learn mlr3}{
14 |
15 | \itemize{
16 | \item Book on mlr3: \url{https://mlr3book.mlr-org.com}
17 | \item Use cases and examples gallery: \url{https://mlr3gallery.mlr-org.com}
18 | \item Cheat Sheets: \url{https://github.com/mlr-org/mlr3cheatsheets}
19 | }
20 | }
21 |
22 | \section{mlr3 extensions}{
23 |
24 | \itemize{
25 | \item Preprocessing and machine learning pipelines: \CRANpkg{mlr3pipelines}
26 | \item Analysis of benchmark experiments: \CRANpkg{mlr3benchmark}
27 | \item More classification and regression tasks: \CRANpkg{mlr3data}
28 | \item Connector to \href{https://www.openml.org}{OpenML}: \CRANpkg{mlr3oml}
29 | \item Solid selection of good classification and regression learners: \CRANpkg{mlr3learners}
30 | \item Even more learners: \url{https://github.com/mlr-org/mlr3extralearners}
31 | \item Tuning of hyperparameters: \CRANpkg{mlr3tuning}
32 | \item Hyperband tuner: \CRANpkg{mlr3hyperband}
33 | \item Visualizations for many \pkg{mlr3} objects: \CRANpkg{mlr3viz}
34 | \item Survival analysis and probabilistic regression: \CRANpkg{mlr3proba}
35 | \item Cluster analysis: \CRANpkg{mlr3cluster}
36 | \item Feature selection filters: \CRANpkg{mlr3filters}
37 | \item Feature selection wrappers: \CRANpkg{mlr3fselect}
38 | \item Interface to real (out-of-memory) data bases: \CRANpkg{mlr3db}
39 | \item Performance measures as plain functions: \CRANpkg{mlr3measures}
40 | }
41 | }
42 |
43 | \section{Suggested packages}{
44 |
45 | \itemize{
46 | \item Parallelization framework: \CRANpkg{future}
47 | \item Progress bars: \CRANpkg{progressr}
48 | \item Encapsulated evaluation: \CRANpkg{evaluate}, \CRANpkg{callr} (external process)
49 | }
50 | }
51 |
52 | \section{Package Options}{
53 |
54 | \itemize{
55 | \item \code{"mlr3.debug"}: If set to \code{TRUE}, parallelization via \CRANpkg{future} is
56 | disabled to simplify debugging and provide more concise tracebacks. Note that
57 | results computed with debug mode enabled use a different seeding mechanism
58 | and are not reproducible.
59 | \item \code{"mlr3.allow_utf8_names"}: If set to \code{TRUE}, checks on the feature names
60 | are relaxed, allowing non-ascii characters in column names. This is an
61 | experimental and temporal option to pave the way for text analysis, and will
62 | likely be removed in a future version of the package. analysis.
63 | }
64 | }
65 |
66 | \references{
67 | Becker M, Schratz P (2024).
68 | \emph{mlr3spatial: Support for Spatial Objects Within the 'mlr3' Ecosystem}.
69 | https://mlr3spatial.mlr-org.com,
70 | https://github.com/mlr-org/mlr3spatial.
71 | }
72 | \seealso{
73 | Useful links:
74 | \itemize{
75 | \item \url{https://mlr3spatial.mlr-org.com}
76 | \item \url{https://github.com/mlr-org/mlr3spatial}
77 | \item Report bugs at \url{https://github.com/mlr-org/mlr3spatial/issues}
78 | }
79 |
80 | }
81 | \author{
82 | \strong{Maintainer}: Marc Becker \email{marcbecker@posteo.de} (\href{https://orcid.org/0000-0002-8115-0400}{ORCID})
83 |
84 | Authors:
85 | \itemize{
86 | \item Patrick Schratz \email{patrick.schratz@gmail.com} (\href{https://orcid.org/0000-0003-0748-6624}{ORCID})
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/man/numeric_layer.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \name{numeric_layer}
4 | \alias{numeric_layer}
5 | \title{Numeric Layer Generator}
6 | \usage{
7 | numeric_layer(id, in_memory = FALSE)
8 | }
9 | \arguments{
10 | \item{id}{(\code{character(1)})\cr
11 | Layer id.}
12 |
13 | \item{in_memory}{(\code{logical(1)})\cr
14 | If \code{FALSE} (default), layer is written to disk.}
15 | }
16 | \value{
17 | Named \code{list()}
18 | }
19 | \description{
20 | Generates a numeric layer when passed to \code{\link[=generate_stack]{generate_stack()}}.
21 | }
22 | \keyword{internal}
23 |
--------------------------------------------------------------------------------
/man/predict_spatial.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/predict_spatial.R
3 | \name{predict_spatial}
4 | \alias{predict_spatial}
5 | \title{Predict on Spatial Objects with mlr3 Learners}
6 | \usage{
7 | predict_spatial(
8 | newdata,
9 | learner,
10 | chunksize = 200L,
11 | format = "terra",
12 | filename = NULL
13 | )
14 | }
15 | \arguments{
16 | \item{newdata}{(\link[terra:SpatRaster-class]{terra::SpatRaster} | \code{stars::stars} | \link[sf:sf]{sf::sf} | \code{raster::RasterStack} | \code{raster::RasterBrick}).
17 | New data to predict on. All spatial data formats convertible by \code{as_data_backend()} are supported e.g. \link[terra:SpatRaster-class]{terra::SpatRaster} or \link[sf:sf]{sf::sf}.}
18 |
19 | \item{learner}{(\link{Learner}).
20 | Learner with trained model.}
21 |
22 | \item{chunksize}{(\code{integer(1)})\cr
23 | The chunksize determines in how many subparts the prediction task will be
24 | split into. The value can be roughly thought of as megabyte of a raster file
25 | on disk. For example, if a prediction on a 1 GB file would be carried out
26 | with \code{chunksize = 100L}, the prediction would happen in 10 chunks.
27 |
28 | The default of \code{chunksize = 1000L} might be a good compromise between speed
29 | and memory usage. If you find yourself running out of memory, reduce this
30 | value.}
31 |
32 | \item{format}{(\code{character(1)})\cr
33 | Output class of the resulting object.
34 | Accepted values are \code{"raster"}, \code{"stars"} and \code{"terra"} if the input is a raster.
35 | Note that when choosing something else than \code{"terra"}, the spatial object is converted into the respective format which might cause overhead both in runtime and memory allocation.
36 | For vector data only \code{"sf"} is supported.}
37 |
38 | \item{filename}{(\code{character(1)})\cr
39 | Path where the spatial object should be written to.}
40 | }
41 | \value{
42 | Spatial object of class given in argument \code{format}.
43 | }
44 | \description{
45 | This function allows to directly predict mlr3 learners on various spatial objects.
46 | }
47 | \examples{
48 | library(terra, exclude = "resample")
49 |
50 | # fit rpart on training points
51 | task_train = tsk("leipzig")
52 | learner = lrn("classif.rpart")
53 | learner$train(task_train)
54 |
55 | # load raster
56 | stack = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial"))
57 |
58 | # predict land cover classes
59 | pred = predict_spatial(stack, learner, chunksize = 1L)
60 | }
61 |
--------------------------------------------------------------------------------
/man/sample_stack.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/data.R
3 | \name{sample_stack}
4 | \alias{sample_stack}
5 | \title{Sample Points in Raster Stack}
6 | \usage{
7 | sample_stack(stack, n = 100)
8 | }
9 | \arguments{
10 | \item{stack}{(\link[terra:SpatRaster-class]{terra::SpatRaster})\cr
11 | Raster stack.}
12 |
13 | \item{n}{(\code{integer(1)})\cr
14 | Number of points.}
15 | }
16 | \value{
17 | \link[sf:sf]{sf::sf}
18 | }
19 | \description{
20 | Samples \code{n} points of a raster stack.
21 | }
22 | \keyword{internal}
23 |
--------------------------------------------------------------------------------
/man/write_raster.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/helper.R
3 | \name{write_raster}
4 | \alias{write_raster}
5 | \title{Write a Raster in Chunks}
6 | \usage{
7 | write_raster(data)
8 | }
9 | \arguments{
10 | \item{data}{\verb{[SpatRaster]}\cr
11 | \code{SpatRaster} object.}
12 | }
13 | \description{
14 | Writes square raster to disk in chunks.
15 | Internal helper function.
16 | }
17 | \keyword{internal}
18 |
--------------------------------------------------------------------------------
/mlr3spatial.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: Default
4 | SaveWorkspace: Default
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | UseSpacesForTab: Yes
9 | NumSpacesForTab: 2
10 | Encoding: UTF-8
11 |
12 | RnwWeave: Sweave
13 | LaTeX: pdfLaTeX
14 |
15 | AutoAppendNewline: Yes
16 | StripTrailingWhitespace: Yes
17 |
18 | BuildType: Package
19 | PackageUseDevtools: Yes
20 | PackageCleanBeforeInstall: Yes
21 | PackageInstallArgs: --no-multiarch --with-keep.source
22 | PackageRoxygenize: rd,collate,namespace
23 |
--------------------------------------------------------------------------------
/pkgdown/_pkgdown.yml:
--------------------------------------------------------------------------------
1 | url: https://mlr3spatial.mlr-org.com
2 |
3 | template:
4 | bootstrap: 5
5 | package: mlr3pkgdowntemplate
6 |
7 | development:
8 | mode: auto
9 | version_label: default
10 | version_tooltip: "Version"
11 |
12 | authors:
13 | Patrick Schratz:
14 | href: https://pat-s.me
15 |
16 | navbar:
17 | structure:
18 | left: [reference, news, book, articles]
19 | right: [github, mattermost, stackoverflow, rss]
20 | components:
21 | home: ~
22 | reference:
23 | icon: fa fa-file-alt
24 | text: Reference
25 | href: reference/index.html
26 | mattermost:
27 | icon: fa fa-comments
28 | href: https://lmmisld-lmu-stats-slds.srv.mwn.de/mlr_invite/
29 | book:
30 | text: mlr3book
31 | icon: fa fa-link
32 | href: https://mlr3book.mlr-org.com
33 | stackoverflow:
34 | icon: fab fa-stack-overflow
35 | href: https://stackoverflow.com/questions/tagged/mlr3
36 | rss:
37 | icon: fa-rss
38 | href: https://mlr-org.com/
39 |
40 | reference:
41 | - title: mlr3spatial
42 | contents:
43 | - mlr3spatial-package
44 | - title: Spatiotemporal Tasks
45 | contents:
46 | - contains("Task")
47 | - contains("as_task_")
48 | - leipzig
49 | - title: Data Backends
50 | contents:
51 | - contains("Backend")
52 | - contains("_backend")
53 | - title: Prediction
54 | contents:
55 | - predict_spatial
56 |
--------------------------------------------------------------------------------
/pkgdown/apple-touch-icon-120x120.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/apple-touch-icon-120x120.png
--------------------------------------------------------------------------------
/pkgdown/apple-touch-icon-152x152.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/apple-touch-icon-152x152.png
--------------------------------------------------------------------------------
/pkgdown/apple-touch-icon-180x180.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/apple-touch-icon-180x180.png
--------------------------------------------------------------------------------
/pkgdown/apple-touch-icon-60x60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/apple-touch-icon-60x60.png
--------------------------------------------------------------------------------
/pkgdown/apple-touch-icon-76x76.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/apple-touch-icon-76x76.png
--------------------------------------------------------------------------------
/pkgdown/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/apple-touch-icon.png
--------------------------------------------------------------------------------
/pkgdown/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon-16x16.png
--------------------------------------------------------------------------------
/pkgdown/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon-32x32.png
--------------------------------------------------------------------------------
/pkgdown/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon.ico
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-120x120.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/apple-touch-icon-120x120.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-152x152.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/apple-touch-icon-152x152.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-180x180.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/apple-touch-icon-180x180.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-60x60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/apple-touch-icon-60x60.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-76x76.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/apple-touch-icon-76x76.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/apple-touch-icon.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/favicon-16x16.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/favicon-32x32.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/pkgdown/favicon/favicon.ico
--------------------------------------------------------------------------------
/tests/testthat.R:
--------------------------------------------------------------------------------
1 | if (requireNamespace("testthat", quietly = TRUE)) {
2 | library("checkmate")
3 | library("testthat")
4 | library("mlr3spatial")
5 | test_check("mlr3spatial")
6 | }
7 |
--------------------------------------------------------------------------------
/tests/testthat/helper_expectations.R:
--------------------------------------------------------------------------------
1 | expect_backend = function(b) {
2 | checkmate::expect_r6(b, cloneable = FALSE,
3 | public = c("nrow", "ncol", "colnames", "rownames", "head", "data", "hash"),
4 | private = c(".data", ".hash", ".calculate_hash"))
5 | checkmate::expect_subset(b$data_formats, mlr3::mlr_reflections$data_formats, empty.ok = FALSE)
6 | testthat::expect_output(print(b), "^ 0 zero length
76 |
77 | ## missings are handled by distinct?
78 | d = b$distinct(rn, cn, na_rm = TRUE)
79 | checkmate::qexpectr(d, "V")
80 |
81 | d = b$distinct(rn, cn, na_rm = FALSE)
82 | m = b$missings(rn, cn)
83 | testthat::expect_equal(vapply(d, checkmate::anyMissing, FUN.VALUE = logical(1)), m > 0L)
84 |
85 | # $missings()
86 | x = b$missings(b$rownames, b$colnames)
87 | checkmate::expect_integerish(x, lower = 0L, upper = b$nrow, any.missing = FALSE)
88 | checkmate::expect_names(names(x), permutation.of = b$colnames)
89 | checkmate::expect_integerish(b$missings(b$rownames, "_not_existing_"), len = 0L, names = "named")
90 | checkmate::expect_integerish(b$missings(b$rownames[0L], b$colnames), len = b$ncol, names = "unique")
91 | checkmate::expect_integerish(b$missings(b$rownames[0L], "_not_existing_"), len = 0L, names = "unique")
92 |
93 | # $hash
94 | checkmate::expect_string(b$hash)
95 | }
96 |
--------------------------------------------------------------------------------
/tests/testthat/helper_future.R:
--------------------------------------------------------------------------------
1 | with_future = function(backend, expr, ...) {
2 | requireNamespace("future")
3 | oplan = force(future::plan(backend, ...))
4 | on.exit(future::plan(oplan), add = TRUE)
5 | force(expr)
6 | }
7 |
--------------------------------------------------------------------------------
/tests/testthat/helper_learner.R:
--------------------------------------------------------------------------------
1 | # Predicts first feature in task
2 | LearnerRegrFeature = R6Class("LearnerRegrFeature", inherit = LearnerRegr,
3 | public = list(
4 | #' @description
5 | #' Creates a new instance of this [R6][R6::R6Class] class.
6 | initialize = function() {
7 | super$initialize(
8 | id = "regr.featureless",
9 | feature_types = mlr_reflections$task_feature_types,
10 | predict_types = "response",
11 | param_set = paradox::ps(),
12 | properties = character(0),
13 | label = "Feature Regression Learner",
14 | man = "mlr3::mlr_learners_regr.featureless",
15 | )
16 | }
17 | ),
18 |
19 | private = list(
20 | .train = function(task) {
21 | set_class(list(feature = task$feature_names[1]), "regr.feature_model")
22 | },
23 |
24 | .predict = function(task) {
25 | list(response = task$data()[, self$model$feature, with = FALSE][[1]])
26 | }
27 | )
28 | )
29 |
--------------------------------------------------------------------------------
/tests/testthat/setup.R:
--------------------------------------------------------------------------------
1 | old_opts = options(
2 | warnPartialMatchArgs = TRUE,
3 | warnPartialMatchAttr = TRUE,
4 | warnPartialMatchDollar = TRUE
5 | )
6 |
7 | # https://github.com/HenrikBengtsson/Wishlist-for-R/issues/88
8 | old_opts = lapply(old_opts, function(x) if (is.null(x)) FALSE else x)
9 |
10 | # logger
11 | lg_mlr3 = lgr::get_logger("mlr3")
12 | old_threshold_mlr3 = lg_mlr3$threshold
13 | lg_mlr3$set_threshold("warn")
14 | lg_bbotk = lgr::get_logger("bbotk")
15 | old_threshold_bbotk = lg_bbotk$threshold
16 | lg_bbotk$set_threshold("warn")
17 | lg_mlr3spatial = lgr::get_logger("mlr3spatial")
18 | old_threshold_mlr3spatial = lg_mlr3spatial$threshold
19 | lg_mlr3spatial$set_threshold("warn")
20 |
21 | # future
22 | old_plan = future::plan()
23 | lg$set_threshold("warn")
24 | future::plan("sequential")
25 |
--------------------------------------------------------------------------------
/tests/testthat/teardown.R:
--------------------------------------------------------------------------------
1 | options(old_opts)
2 | lg_mlr3$set_threshold(old_threshold_mlr3)
3 | lg_bbotk$set_threshold(old_threshold_bbotk)
4 | lg_mlr3spatial$set_threshold(old_threshold_mlr3spatial)
5 | future::plan(old_plan)
6 |
--------------------------------------------------------------------------------
/tests/testthat/test_DataBackendVector.R:
--------------------------------------------------------------------------------
1 | test_that("DataBackendVector works", {
2 | vector = sf::read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)
3 | primary_key = "..row_id"
4 | vector[[primary_key]] = seq_row(vector)
5 | backend = DataBackendVector$new(vector, primary_key = primary_key)
6 |
7 | expect_class(backend, "DataBackendVector")
8 | expect_class(backend$sfc, "sfc")
9 | })
10 |
11 | test_that("as_data_backend.sf works", {
12 | vector = sf::read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)
13 | backend = as_data_backend(vector)
14 |
15 | expect_class(backend, "DataBackendVector")
16 | expect_class(backend$sfc, "sfc")
17 | })
18 |
--------------------------------------------------------------------------------
/tests/testthat/test_LearnerClassifSpatial.R:
--------------------------------------------------------------------------------
1 | test_that("LearnerClassifSpatial ignores observations with missing values", {
2 | skip_if_not_installed("mlr3learners")
3 | require_namespaces("mlr3learners")
4 |
5 | # train task
6 | stack = generate_stack(list(
7 | numeric_layer("x_1"),
8 | factor_layer("y", levels = c("a", "b"))),
9 | dimension = 100)
10 | vector = sample_stack(stack, n = 100)
11 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
12 | learner = lrn("classif.ranger")
13 | learner$train(task_train)
14 |
15 | # predict task
16 | stack$y = NULL
17 | stack = mask_stack(stack)
18 | task_predict = as_task_unsupervised(stack, id = "test")
19 | learner_spatial = LearnerClassifSpatial$new(learner)
20 | pred = learner_spatial$predict(task_predict)
21 |
22 | expect_true(all(is.na(pred$response[seq(100)])))
23 | expect_numeric(pred$response, any.missing = TRUE, all.missing = FALSE)
24 | })
25 |
--------------------------------------------------------------------------------
/tests/testthat/test_LearnerRegrSpatial.R:
--------------------------------------------------------------------------------
1 | test_that("LearnerRegrSpatial ignores observations with missing values", {
2 | skip_if_not_installed("mlr3learners")
3 | require_namespaces("mlr3learners")
4 |
5 | # train task
6 | stack = generate_stack(list(
7 | factor_layer("c_1", levels = c("a", "b")),
8 | numeric_layer("y")),
9 | dimension = 100)
10 | vector = sample_stack(stack, n = 100)
11 | task_train = as_task_regr_st(vector, id = "test_vector", target = "y")
12 | learner = lrn("regr.ranger")
13 | learner$train(task_train)
14 |
15 | # predict task
16 | stack$y = NULL
17 | stack = mask_stack(stack)
18 | task_predict = as_task_unsupervised(stack, id = "test")
19 | learner_spatial = LearnerRegrSpatial$new(learner)
20 | pred = learner_spatial$predict(task_predict)
21 |
22 | expect_true(all(is.na(pred$response[seq(100)])))
23 | expect_numeric(pred$response, any.missing = TRUE, all.missing = FALSE)
24 | })
25 |
--------------------------------------------------------------------------------
/tests/testthat/test_TaskClassifST.R:
--------------------------------------------------------------------------------
1 | test_that("TaskClassifST throws an error when backend is an sf object", {
2 | stack = generate_stack(list(
3 | numeric_layer("x_1"),
4 | factor_layer("y", levels = c("a", "b"))),
5 | dimension = 100)
6 | vector = st_as_sf(sample_stack(stack, n = 100))
7 |
8 | expect_error(TaskClassifST$new(id = "test", backend = vector, target = "y"), "convert an sf objects into a task")
9 | })
10 |
--------------------------------------------------------------------------------
/tests/testthat/test_TaskRegrST.R:
--------------------------------------------------------------------------------
1 | test_that("TaskRegrST throws an error when backend is an sf object", {
2 | stack = generate_stack(list(
3 | numeric_layer("x_1"),
4 | numeric_layer("y")),
5 | dimension = 100)
6 | vector = st_as_sf(sample_stack(stack, n = 100))
7 |
8 | expect_error(TaskRegrST$new(id = "test", backend = vector, target = "y"), "convert an sf objects into a task")
9 | })
10 |
--------------------------------------------------------------------------------
/tests/testthat/test_as_task_classif_st.R:
--------------------------------------------------------------------------------
1 | test_that("as_task_classif_st works on data.frame objects", {
2 | stack = generate_stack(list(
3 | numeric_layer("x_1"),
4 | factor_layer("y", levels = c("a", "b"))),
5 | dimension = 100)
6 | vector = st_as_sf(sample_stack(stack, n = 100))
7 | data = as.data.frame(vector)
8 | data$geometry = NULL
9 | data = cbind(data, st_coordinates(vector))
10 |
11 | task = as_task_classif_st(data, target = "y", coordinate_names = c("X", "Y"), crs = "EPSG:4326")
12 | expect_class(task, "TaskClassifST")
13 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
14 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
15 | expect_equal(task$coordinate_names, c("X", "Y"))
16 | expect_equal(task$crs, "EPSG:4326")
17 | expect_equal(task$col_roles$feature, "x_1")
18 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
19 | })
20 |
21 | test_that("as_task_classif_st works on DataBackendDataTable objects", {
22 | stack = generate_stack(list(
23 | numeric_layer("x_1"),
24 | factor_layer("y", levels = c("a", "b"))),
25 | dimension = 100)
26 | vector = st_as_sf(sample_stack(stack, n = 100))
27 | data = as.data.frame(vector)
28 | data$geometry = NULL
29 | data = cbind(data, st_coordinates(vector))
30 | backend = as_data_backend(data)
31 |
32 | task = as_task_classif_st(backend, target = "y", coordinate_names = c("X", "Y"), crs = "EPSG:4326")
33 | expect_class(task, "TaskClassifST")
34 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
35 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
36 | expect_equal(task$coordinate_names, c("X", "Y"))
37 | expect_equal(task$crs, "EPSG:4326")
38 | expect_equal(task$col_roles$feature, "x_1")
39 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
40 | })
41 |
42 | test_that("as_task_classif_st works on sf objects", {
43 | stack = generate_stack(list(
44 | numeric_layer("x_1"),
45 | factor_layer("y", levels = c("a", "b"))),
46 | dimension = 100)
47 | vector = st_as_sf(sample_stack(stack, n = 100))
48 |
49 | task = as_task_classif_st(vector, target = "y")
50 | expect_class(task, "TaskClassifST")
51 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
52 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
53 | expect_equal(task$coordinate_names, c("X", "Y"))
54 | expect_equal(task$crs, st_crs(vector)$wkt)
55 | expect_equal(task$col_roles$feature, "x_1")
56 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
57 | })
58 |
59 | test_that("as_task_classif_st works on TaskClassifST objects", {
60 | stack = generate_stack(list(
61 | numeric_layer("x_1"),
62 | factor_layer("y", levels = c("a", "b"))),
63 | dimension = 100)
64 | vector = st_as_sf(sample_stack(stack, n = 100))
65 |
66 | task = as_task_classif_st(vector, target = "y")
67 | task = as_task_classif_st(task)
68 | expect_class(task, "TaskClassifST")
69 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
70 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
71 | expect_equal(task$coordinate_names, c("X", "Y"))
72 | expect_equal(task$crs, st_crs(vector)$wkt)
73 | expect_equal(task$col_roles$feature, "x_1")
74 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
75 | })
76 |
77 | test_that("convert from TaskRegrST to TaskClassifST", {
78 | stack = generate_stack(list(
79 | numeric_layer("x_1"),
80 | factor_layer("x_2", levels = c("a", "b")),
81 | numeric_layer("y")),
82 | dimension = 100)
83 | vector = st_as_sf(sample_stack(stack, n = 100))
84 |
85 | task = as_task_regr_st(vector, target = "y")
86 | task = as_task_classif_st(task, target = "x_2", drop_original_target = TRUE)
87 | expect_class(task, "TaskClassifST")
88 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
89 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
90 | expect_equal(task$coordinate_names, c("X", "Y"))
91 | expect_equal(task$crs, st_crs(vector)$wkt)
92 | expect_equal(task$col_roles$feature, c("x_1", "y"))
93 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
94 | })
95 |
--------------------------------------------------------------------------------
/tests/testthat/test_as_task_regr_st.R:
--------------------------------------------------------------------------------
1 |
2 | test_that("as_task_regr_st works on data.frame objects", {
3 | stack = generate_stack(list(
4 | numeric_layer("x_1"),
5 | numeric_layer("y")),
6 | dimension = 100)
7 | vector = st_as_sf(sample_stack(stack, n = 100))
8 | data = as.data.frame(vector)
9 | data$geometry = NULL
10 | data = cbind(data, st_coordinates(vector))
11 |
12 | task = as_task_regr_st(data, target = "y", coordinate_names = c("X", "Y"), crs = "EPSG:4326")
13 | expect_class(task, "TaskRegrST")
14 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
15 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
16 | expect_equal(task$coordinate_names, c("X", "Y"))
17 | expect_equal(task$crs, "EPSG:4326")
18 | expect_equal(task$col_roles$feature, "x_1")
19 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
20 | })
21 |
22 | test_that("as_task_regr_st works on DataBackendDataTable objects", {
23 | stack = generate_stack(list(
24 | numeric_layer("x_1"),
25 | numeric_layer("y")),
26 | dimension = 100)
27 | vector = st_as_sf(sample_stack(stack, n = 100))
28 | data = as.data.frame(vector)
29 | data$geometry = NULL
30 | data = cbind(data, st_coordinates(vector))
31 | backend = as_data_backend(data)
32 |
33 | task = as_task_regr_st(backend, target = "y", coordinate_names = c("X", "Y"), crs = "EPSG:4326")
34 | expect_class(task, "TaskRegrST")
35 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
36 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
37 | expect_equal(task$coordinate_names, c("X", "Y"))
38 | expect_equal(task$crs, "EPSG:4326")
39 | expect_equal(task$col_roles$feature, "x_1")
40 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
41 | })
42 |
43 | test_that("as_task_regr_st works on sf objects", {
44 | stack = generate_stack(list(
45 | numeric_layer("x_1"),
46 | numeric_layer("y")),
47 | dimension = 100)
48 | vector = st_as_sf(sample_stack(stack, n = 100))
49 |
50 | task = as_task_regr_st(vector, target = "y")
51 | expect_class(task, "TaskRegrST")
52 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
53 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
54 | expect_equal(task$coordinate_names, c("X", "Y"))
55 | expect_equal(task$crs, st_crs(vector)$wkt)
56 | expect_equal(task$col_roles$feature, "x_1")
57 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
58 | })
59 |
60 | test_that("as_task_regr_st works on TaskRegrST objects", {
61 | stack = generate_stack(list(
62 | numeric_layer("x_1"),
63 | numeric_layer("y")),
64 | dimension = 100)
65 | vector = st_as_sf(sample_stack(stack, n = 100))
66 |
67 | task = as_task_regr_st(vector, target = "y")
68 | task = as_task_regr_st(task)
69 | expect_class(task, "TaskRegrST")
70 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
71 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
72 | expect_equal(task$coordinate_names, c("X", "Y"))
73 | expect_equal(task$crs, st_crs(vector)$wkt)
74 | expect_equal(task$col_roles$feature, "x_1")
75 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
76 | })
77 |
78 | test_that("convert from TaskClassifST to TaskRegrST", {
79 | stack = generate_stack(list(
80 | numeric_layer("x_1"),
81 | numeric_layer("x_2"),
82 | factor_layer("y", levels = c("a", "b"))),
83 | dimension = 100)
84 | vector = st_as_sf(sample_stack(stack, n = 100))
85 |
86 | task = as_task_classif_st(vector, target = "y")
87 | task = as_task_regr_st(task, target = "x_2", drop_original_target = TRUE)
88 | expect_class(task, "TaskRegrST")
89 | expect_data_table(task$coordinates(), types = "numeric", ncols = 2, nrows = 100)
90 | expect_names(colnames(task$coordinates()), identical.to = c("X", "Y"))
91 | expect_equal(task$coordinate_names, c("X", "Y"))
92 | expect_equal(task$crs, st_crs(vector)$wkt)
93 | expect_equal(task$col_roles$feature, c("x_1", "y"))
94 | expect_equal(task$col_roles$coordinate, c("X", "Y"))
95 | })
96 |
--------------------------------------------------------------------------------
/tests/testthat/test_as_task_unsupervised.R:
--------------------------------------------------------------------------------
1 | test_that("as_task_unsupervised works on stars objects", {
2 | skip_if_not_installed("stars")
3 | requireNamespace("stars", quietly = TRUE)
4 |
5 | stack = generate_stack(list(
6 | numeric_layer("x_1"),
7 | numeric_layer("y")),
8 | dimension = 100)
9 | stack = invoke(stars::st_as_stars, .x = stack, .opts = allow_partial_matching)
10 |
11 | expect_class(as_task_unsupervised(stack), "TaskUnsupervised")
12 | })
13 |
14 | test_that("as_task_unsupervised works on SpatRaster objects", {
15 | stack = generate_stack(list(
16 | numeric_layer("x_1"),
17 | numeric_layer("y")),
18 | dimension = 100)
19 |
20 | expect_class(as_task_unsupervised(stack), "TaskUnsupervised")
21 | })
22 |
23 | test_that("as_task_unsupervised works on RasterBrick objects", {
24 | skip_if_not_installed("raster")
25 | requireNamespace("raster", quietly = TRUE)
26 |
27 | stack = generate_stack(list(
28 | numeric_layer("x_1"),
29 | numeric_layer("y")),
30 | dimension = 100, multi_layer_file = TRUE)
31 | stack = invoke(raster::brick, stack, .opts = allow_partial_matching)
32 |
33 | expect_class(as_task_unsupervised(stack), "TaskUnsupervised")
34 | })
35 |
36 | test_that("as_task_unsupervised works on RasterStack objects", {
37 | skip_if_not_installed("raster")
38 | requireNamespace("raster", quietly = TRUE)
39 |
40 | stack = generate_stack(list(
41 | numeric_layer("x_1"),
42 | numeric_layer("y")),
43 | dimension = 100)
44 | stack = invoke(raster::stack, x = stack, .opts = allow_partial_matching)
45 | raster::crs(stack) = "EPSG:4326"
46 |
47 | expect_class(as_task_unsupervised(stack), "TaskUnsupervised")
48 | })
49 |
50 | test_that("as_task_unsupervised works on sf objects", {
51 | vector = sf::read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)
52 | vector$land_cover = NULL
53 |
54 | expect_class(as_task_unsupervised(vector), "TaskUnsupervised")
55 | })
56 |
--------------------------------------------------------------------------------
/tests/testthat/test_bock_size.R:
--------------------------------------------------------------------------------
1 | test_that("chunk size is 1 out of 8 rows", {
2 | raster = generate_stack(list(
3 | numeric_layer("x_1")),
4 | dimension = 8)
5 |
6 | bs = block_size(raster, chunksize = 64 * 1e-6)
7 | expect_equal(bs$cells_seq, cumsum(c(1, rep(8, 7))))
8 | expect_equal(bs$cells_to_read, rep(8, 8))
9 | })
10 |
11 | test_that("chunk size is 2 out of 8 rows", {
12 | raster = generate_stack(list(
13 | numeric_layer("x_1")),
14 | dimension = 8)
15 |
16 | bs = block_size(raster, chunksize = 64 * 2 * 1e-6)
17 | expect_equal(bs$cells_seq, cumsum(c(1, rep(16, 3))))
18 | expect_equal(bs$cells_to_read, rep(16, 4))
19 | })
20 |
21 | test_that("chunk size is 1/2 out of 8 rows", {
22 | raster = generate_stack(list(
23 | numeric_layer("x_1")),
24 | dimension = 8)
25 |
26 | bs = block_size(raster, chunksize = 32 * 1e-6) # chunk size is round up to 1 rows
27 | expect_equal(bs$cells_seq, cumsum(c(1, rep(8, 7))))
28 | expect_equal(bs$cells_to_read, rep(8, 8))
29 | })
30 |
31 | test_that("chunk size is 1 1/2 out of 8 rows", {
32 | raster = generate_stack(list(
33 | numeric_layer("x_1")),
34 | dimension = 8)
35 |
36 | bs = block_size(raster, chunksize = 96 * 1e-6) # chunk size is round up to 2 rows
37 | expect_equal(bs$cells_seq, cumsum(c(1, rep(16, 3))))
38 | expect_equal(bs$cells_to_read, rep(16, 4))
39 | })
40 |
41 | test_that("chunk size is 3 rows out of 8 rows", {
42 | raster = generate_stack(list(
43 | numeric_layer("x_1")),
44 | dimension = 8)
45 |
46 | bs = block_size(raster, chunksize = 64 * 3 * 1e-6)
47 | expect_equal(bs$cells_seq, cumsum(c(1, rep(24, 2))))
48 | expect_equal(bs$cells_to_read, c(24, 24, 16))
49 | })
50 |
51 | test_that("chunk size is 8 out of 8 rows", {
52 | raster = generate_stack(list(
53 | numeric_layer("x_1")),
54 | dimension = 8)
55 |
56 | bs = block_size(raster, chunksize = 64 * 8 * 1e-6)
57 | expect_equal(bs$cells_seq, 1)
58 | expect_equal(bs$cells_to_read, 64)
59 | })
60 |
61 | test_that("chunk size is 9 out of 8 rows", {
62 | raster = generate_stack(list(
63 | numeric_layer("x_1")),
64 | dimension = 8)
65 |
66 | bs = block_size(raster, chunksize = 64 * 9 * 1e-6)
67 | expect_equal(bs$cells_seq, 1)
68 | expect_equal(bs$cells_to_read, 64)
69 | })
70 |
71 | test_that("chunk size is 16 out of 8 rows", {
72 | raster = generate_stack(list(
73 | numeric_layer("x_1")),
74 | dimension = 8)
75 |
76 | bs = block_size(raster, chunksize = 64 * 16 * 1e-6)
77 | expect_equal(bs$cells_seq, 1)
78 | expect_equal(bs$cells_to_read, 64)
79 | })
80 |
--------------------------------------------------------------------------------
/tests/testthat/test_data.R:
--------------------------------------------------------------------------------
1 | test_that("categorical layer is set", {
2 | stack = generate_stack(list(
3 | factor_layer("y", levels = c("a", "b"))),
4 | dimension = 2)
5 | task = as_task_unsupervised(stack)
6 | expect_factor(task$data()$y, levels = c("a", "b"), len = 4)
7 | })
8 |
--------------------------------------------------------------------------------
/tests/testthat/test_predict_spatial.R:
--------------------------------------------------------------------------------
1 | # raster predictions -----------------------------------------------------------
2 |
3 | test_that("predictions are written to raster", {
4 | skip_if_not_installed("paradox")
5 | # [1] [2] [2]
6 | # [1] [1] [1]
7 | # [2] [2] [1]
8 | # [2] [2] [2]
9 | c_1 = y = c(1, 2, 2, 1, 1, 1, 2, 2, 1, 2, 2, 2)
10 | raster = terra::rast(matrix(c_1, ncol = 3, byrow = TRUE))
11 | terra::set.names(raster, "c_1")
12 |
13 | # train task
14 | task = as_task_regr(data.table(cbind(c_1, y)), id = "test", target = "y")
15 |
16 | learner = LearnerRegrFeature$new()
17 | learner$train(task)
18 |
19 | # predict task
20 | task_predict = as_task_unsupervised(raster, id = "test")
21 |
22 | # chunk size is 3 out of 12 cells
23 | raster = predict_spatial(task_predict, learner, chunksize = 8 * 3 * 1e-6)
24 | expect_equal(terra::values(raster)[, 1], y)
25 |
26 | # chunk size is 4 out of 12 cells
27 | raster = predict_spatial(task_predict, learner, chunksize = 8 * 4 * 1e-6)
28 | expect_equal(terra::values(raster)[, 1], y)
29 |
30 | # chunk size is 7 out of 12 cells
31 | raster = predict_spatial(task_predict, learner, chunksize = 8 * 7 * 1e-6)
32 | expect_equal(terra::values(raster)[, 1], y)
33 |
34 | # chunk size is 12 out of 12 cells
35 | raster = predict_spatial(task_predict, learner, chunksize = 8 * 12 * 1e-6)
36 | expect_equal(terra::values(raster)[, 1], y)
37 |
38 | # chunk size is 13 out of 12 cells
39 | raster = predict_spatial(task_predict, learner, chunksize = 8 * 12 * 1e-6)
40 | expect_equal(terra::values(raster)[, 1], y)
41 |
42 | # chunk size is 25 out of 12 cells
43 | raster = predict_spatial(task_predict, learner, chunksize = 8 * 12 * 1e-6)
44 | expect_equal(terra::values(raster)[, 1], y)
45 | })
46 |
47 | # sequential raster predict ---------------------------------------------------
48 |
49 | test_that("sequential execution works", {
50 | # train
51 | stack = generate_stack(list(
52 | numeric_layer("x_1"),
53 | factor_layer("y", levels = c("a", "b"))),
54 | layer_size = 1)
55 | vector = sample_stack(stack, n = 100)
56 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
57 | learner = lrn("classif.rpart")
58 | learner$train(task_train)
59 |
60 | # predict
61 | stack$y = NULL
62 | task_predict = as_task_unsupervised(stack, id = "test")
63 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
64 | expect_class(pred, "SpatRaster")
65 | })
66 |
67 | test_that("sequential execution works in chunks", {
68 | # train
69 | stack = generate_stack(list(
70 | numeric_layer("x_1"),
71 | factor_layer("y", levels = c("a", "b"))),
72 | layer_size = 2)
73 | vector = sample_stack(stack, n = 100)
74 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
75 | learner = lrn("classif.rpart")
76 | learner$train(task_train)
77 |
78 | # predict
79 | stack$y = NULL
80 | task_predict = as_task_unsupervised(stack, id = "test")
81 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
82 | expect_class(pred, "SpatRaster")
83 | })
84 |
85 | # parallel raster predict ------------------------------------------------------
86 |
87 | test_that("parallel execution works with multicore", {
88 | skip_on_os("windows")
89 | # train
90 | stack = generate_stack(list(
91 | numeric_layer("x_1"),
92 | factor_layer("y", levels = c("a", "b"))),
93 | layer_size = 2)
94 | vector = sample_stack(stack, n = 100)
95 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
96 | learner = lrn("classif.rpart")
97 | learner$parallel_predict = TRUE
98 | learner$train(task_train)
99 |
100 | # predict
101 | stack$y = NULL
102 | task_predict = as_task_unsupervised(stack, id = "test")
103 | with_future("multicore", workers = 2, {
104 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
105 | })
106 | expect_class(pred, "SpatRaster")
107 | })
108 |
109 | test_that("parallel execution works with multisession", {
110 | # train
111 | stack = generate_stack(list(
112 | numeric_layer("x_1"),
113 | factor_layer("y", levels = c("a", "b"))),
114 | layer_size = 2)
115 | vector = sample_stack(stack, n = 100)
116 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
117 | learner = lrn("classif.rpart")
118 | learner$parallel_predict = TRUE
119 | learner$train(task_train)
120 |
121 | # predict
122 | stack$y = NULL
123 | task_predict = as_task_unsupervised(stack, id = "test")
124 | with_future("multisession", workers = 2, {
125 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
126 | })
127 | expect_class(pred, "SpatRaster")
128 | })
129 |
130 | test_that("parallel execution works with callr", {
131 | # train
132 | stack = generate_stack(list(
133 | numeric_layer("x_1"),
134 | factor_layer("y", levels = c("a", "b"))),
135 | layer_size = 2)
136 | vector = sample_stack(stack, n = 100)
137 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
138 | learner = lrn("classif.rpart")
139 | learner$parallel_predict = TRUE
140 | learner$train(task_train)
141 |
142 | # predict
143 | stack$y = NULL
144 | task_predict = as_task_unsupervised(stack, id = "test")
145 | with_future(future.callr::callr, workers = 2, {
146 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
147 | })
148 | expect_class(pred, "SpatRaster")
149 | })
150 |
151 | # raster output formats --------------------------------------------------------
152 |
153 | test_that("stars output works", {
154 | skip_if_not_installed("stars")
155 | skip_on_os("mac")
156 |
157 | # train
158 | stack = generate_stack(list(
159 | numeric_layer("x_1"),
160 | numeric_layer("y")),
161 | layer_size = 2)
162 | terra::crs(stack) = "EPSG:4326"
163 | vector = sample_stack(stack, n = 100)
164 | task_train = as_task_regr_st(vector, id = "test_vector", target = "y")
165 | learner = lrn("regr.rpart")
166 | learner$train(task_train)
167 |
168 | # predict
169 | stack$y = NULL
170 | task_predict = as_task_unsupervised(stack, id = "test")
171 | pred = predict_spatial(task_predict, learner, chunksize = 1L, format = "stars")
172 | expect_class(pred, "stars")
173 | })
174 |
175 | test_that("raster output works", {
176 | skip_if_not_installed("raster")
177 | library(raster)
178 |
179 | # train
180 | stack = generate_stack(list(
181 | numeric_layer("x_1"),
182 | numeric_layer("y")),
183 | layer_size = 2)
184 | vector = sample_stack(stack, n = 100)
185 | task_train = as_task_regr_st(vector, id = "test_vector", target = "y")
186 | learner = lrn("regr.rpart")
187 | learner$train(task_train)
188 |
189 | # predict
190 | stack$y = NULL
191 | task_predict = as_task_unsupervised(stack, id = "test")
192 | pred = predict_spatial(task_predict, learner, chunksize = 1L, format = "raster")
193 | expect_class(pred, "RasterLayer")
194 | })
195 |
196 | # raster with missing values ---------------------------------------------------
197 |
198 | test_that("prediction on classification task works with missing values", {
199 | skip_if_not_installed("mlr3learners")
200 | require_namespaces("mlr3learners")
201 |
202 | # train task
203 | stack = generate_stack(list(
204 | numeric_layer("x_1"),
205 | factor_layer("y", levels = c("a", "b"))),
206 | dimension = 100)
207 | vector = sample_stack(stack, n = 100)
208 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
209 | learner = lrn("classif.ranger")
210 | learner$train(task_train)
211 |
212 | # predict task
213 | stack$y = NULL
214 | stack = mask_stack(stack)
215 | task_predict = as_task_unsupervised(stack, id = "test")
216 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
217 | expect_class(pred, "SpatRaster")
218 | expect_true(all(is.na(terra::values(pred[["y"]])[seq(10)])))
219 | expect_numeric(terra::values(pred[["y"]]), any.missing = TRUE, all.missing = FALSE)
220 | })
221 |
222 | test_that("prediction on regression task works with missing values", {
223 | skip_if_not_installed("mlr3learners")
224 | require_namespaces("mlr3learners")
225 |
226 | # train task
227 | stack = generate_stack(list(
228 | numeric_layer("x_1"),
229 | numeric_layer("y")),
230 | dimension = 100)
231 | vector = sample_stack(stack, n = 10)
232 | task_train = as_task_regr_st(vector, id = "test_vector", target = "y")
233 | learner = lrn("regr.ranger")
234 | learner$train(task_train)
235 |
236 | # predict task
237 | stack$y = NULL
238 | stack = mask_stack(stack)
239 | task_predict = as_task_unsupervised(stack, id = "test")
240 | pred = predict_spatial(task_predict, learner, chunksize = 1L)
241 | expect_true(all(is.na(terra::values(pred[["y"]])[seq(10)])))
242 | expect_numeric(terra::values(pred[["y"]]), any.missing = TRUE, all.missing = FALSE)
243 | })
244 |
245 | # vector prediction ------------------------------------------------------------
246 |
247 | test_that("prediction are written to sf vector", {
248 | task = tsk("leipzig")
249 | learner = lrn("classif.rpart")
250 | learner$train(task)
251 |
252 | vector = sf::read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)
253 | vector$land_cover = NULL
254 | task_predict = as_task_unsupervised(vector)
255 | pred = predict_spatial(task_predict, learner)
256 | expect_class(pred, "sf")
257 | expect_equal(nrow(pred), 97)
258 | expect_named(pred, c("land_cover", "geometry"))
259 | expect_class(pred$geometry, "sfc")
260 | })
261 |
--------------------------------------------------------------------------------
/vignettes/.gitignore:
--------------------------------------------------------------------------------
1 | *.html
2 | *.R
3 |
--------------------------------------------------------------------------------
/vignettes/benchmark.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Benchmark Parallel Predictions"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Benchmark Parallel Predictions}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 |
11 |
12 | This benchmark was run on a MacBook Pro 2021 M1 Pro.
13 | If you rerun this result on your machine, your results will differ - both in relative and absolute values and maybe by a lot.
14 | This is due to the multicore performance component of your CPU and how efficient the overhead introduced by parallelization is handled (i.e. splitting and combining the chunks).
15 |
16 | {terra} is using a socker-based parallelization by default (which the user cannot change).
17 | The equivalent in {future} is `plan("multisession")`.
18 | Using `plan(multicore)` on UNIX based systems might speed up the {mlr3} approach even more.
19 | This might also be a major part of the speedup of the `mlr3-all-cores` setting.
20 |
21 | Also note that using all available cores does not always result in faster processing.
22 | The parallelization overhead can be substantial and for small tasks you might be better of using less cores.
23 | Nevertheless, if your processing time is the range of minutes or higher, you might usually be better of using all cores (if possible).
24 |
25 |
26 | ```r
27 | library(mlr3spatial)
28 | library(mlr3learners)
29 | library(future)
30 | ```
31 |
32 | ## Preparations
33 |
34 |
35 | ```r
36 | stack = generate_stack(list(
37 | numeric_layer("x_1"),
38 | factor_layer("y", levels = c("a", "b"))),
39 | layer_size = 10)
40 | vector = sample_stack(stack, n = 500)
41 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
42 | learner = lrn("classif.ranger", num.threads = 1)
43 | learner$train(task_train)
44 | ```
45 |
46 |
47 | ```r
48 | terra_rf = ranger::ranger(y ~ ., data = task_train$data(), num.threads = 1)
49 | ```
50 |
51 |
52 | ```r
53 | stack$y = NULL
54 | task_predict = as_task_unsupervised(stack, id = "test")
55 | learner$parallel_predict = TRUE
56 | ```
57 |
58 | ## Benchmark
59 |
60 |
61 | ```r
62 | bm = bench::mark(
63 |
64 | "01-mlr3-4-cores" = {
65 | plan(multicore, workers = 4)
66 | pred = predict_spatial(task_predict, learner, chunksize = 10L)
67 | },
68 |
69 | "02-terra-4-cores" = {
70 | library(terra)
71 | pred = predict(stack, terra_rf, cores = 4, cpkgs = "ranger",
72 | fun = function(model, ...) predict(model, ...)$predictions)
73 | },
74 |
75 | "03-mlr3-all-cores" = {
76 | plan(multicore)
77 | pred = predict_spatial(task_predict, learner, chunksize = 10L)
78 | },
79 |
80 | "04-terra-all-cores" = {
81 | library(terra)
82 | pred = predict(stack, terra_rf, cores = parallelly::availableCores(), cpkgs = "ranger",
83 | fun = function(model, ...) predict(model, ...)$predictions)
84 | },
85 |
86 | check = FALSE, filter_gc = FALSE, min_iterations = 3,
87 | max_iterations = 3, memory = FALSE)
88 |
89 | bm$`itr/sec` = NULL
90 | bm$result = NULL
91 | bm$`gc/sec` = NULL
92 | bm$memory = NULL
93 | bm$mem_alloc = NULL
94 |
95 | print(bm)
96 | #> # A tibble: 4 × 8
97 | #> expression min median n_itr n_gc total_time time gc
98 | #>
99 | #> 1 01-mlr3-4-cores 20.7s 21.4s 3 57 1.07m
100 | #> 2 02-terra-4-cores 21.9s 23.1s 3 31 1.14m
101 | #> 3 03-mlr3-all-cores 12.5s 12.5s 3 51 37.45s
102 | #> 4 04-terra-all-cores 32.6s 32.7s 3 77 1.64m
103 | ```
104 |
105 |
106 | ```r
107 | library(ggplot2)
108 | autoplot(bm, type = "ridge")
109 | #> Picking joint bandwidth of 0.00417
110 | ```
111 |
112 | 
113 |
114 |
115 |
--------------------------------------------------------------------------------
/vignettes/benchmark.Rmd.orig:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Benchmark Parallel Predictions"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Benchmark Parallel Predictions}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | %\VignetteEncoding{UTF-8}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | knitr::opts_chunk$set(
12 | collapse = TRUE,
13 | comment = "#>",
14 | fig.path = "./"
15 | )
16 | lgr::get_logger("bbotk")$set_threshold("warn")
17 | lgr::get_logger("mlr3")$set_threshold("warn")
18 | lgr::get_logger("mlr3spatial")$set_threshold("warn")
19 | ```
20 |
21 | This benchmark was run on a MacBook Pro 2021 M1 Pro.
22 | If you rerun this result on your machine, your results will differ - both in relative and absolute values and maybe by a lot.
23 | This is due to the multicore performance component of your CPU and how efficient the overhead introduced by parallelization is handled (i.e. splitting and combining the chunks).
24 |
25 | {terra} is using a socker-based parallelization by default (which the user cannot change).
26 | The equivalent in {future} is `plan("multisession")`.
27 | Using `plan(multicore)` on UNIX based systems might speed up the {mlr3} approach even more.
28 | This might also be a major part of the speedup of the `mlr3-all-cores` setting.
29 |
30 | Also note that using all available cores does not always result in faster processing.
31 | The parallelization overhead can be substantial and for small tasks you might be better of using less cores.
32 | Nevertheless, if your processing time is the range of minutes or higher, you might usually be better of using all cores (if possible).
33 |
34 | ```{r setup}
35 | library(mlr3spatial)
36 | library(mlr3learners)
37 | library(future)
38 | ```
39 |
40 | ## Preparations
41 |
42 | ```{r}
43 | stack = generate_stack(list(
44 | numeric_layer("x_1"),
45 | factor_layer("y", levels = c("a", "b"))),
46 | layer_size = 10)
47 | vector = sample_stack(stack, n = 500)
48 | task_train = as_task_classif_st(vector, id = "test_vector", target = "y")
49 | learner = lrn("classif.ranger", num.threads = 1)
50 | learner$train(task_train)
51 | ```
52 |
53 | ```{r}
54 | terra_rf = ranger::ranger(y ~ ., data = task_train$data(), num.threads = 1)
55 | ```
56 |
57 | ```{r}
58 | stack$y = NULL
59 | task_predict = as_task_unsupervised(stack, id = "test")
60 | learner$parallel_predict = TRUE
61 | ```
62 |
63 | ## Benchmark
64 |
65 | ```{r}
66 | bm = bench::mark(
67 |
68 | "01-mlr3-4-cores" = {
69 | plan(multicore, workers = 4)
70 | pred = predict_spatial(task_predict, learner, chunksize = 10L)
71 | },
72 |
73 | "02-terra-4-cores" = {
74 | library(terra)
75 | pred = predict(stack, terra_rf, cores = 4, cpkgs = "ranger",
76 | fun = function(model, ...) predict(model, ...)$predictions)
77 | },
78 |
79 | "03-mlr3-all-cores" = {
80 | plan(multicore)
81 | pred = predict_spatial(task_predict, learner, chunksize = 10L)
82 | },
83 |
84 | "04-terra-all-cores" = {
85 | library(terra)
86 | pred = predict(stack, terra_rf, cores = parallelly::availableCores(), cpkgs = "ranger",
87 | fun = function(model, ...) predict(model, ...)$predictions)
88 | },
89 |
90 | check = FALSE, filter_gc = FALSE, min_iterations = 3,
91 | max_iterations = 3, memory = FALSE)
92 |
93 | bm$`itr/sec` = NULL
94 | bm$result = NULL
95 | bm$`gc/sec` = NULL
96 | bm$memory = NULL
97 | bm$mem_alloc = NULL
98 |
99 | print(bm)
100 | ```
101 |
102 | ```{r plot-benchmark, fig.cap=""}
103 | library(ggplot2)
104 | autoplot(bm, type = "ridge")
105 | ```
106 |
107 | ```{r save-plot, echo = FALSE, message = FALSE}
108 | ggsave("plot-benchmark-1.png")
109 | ```
110 |
--------------------------------------------------------------------------------
/vignettes/plot-benchmark-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mlr-org/mlr3spatial/f8e417455697159103f126ae89fc858a70861af8/vignettes/plot-benchmark-1.png
--------------------------------------------------------------------------------
/vignettes/precompile.R:
--------------------------------------------------------------------------------
1 | # Must manually move image files from eia/ to eia/vignettes/ after knit
2 |
3 | time = Sys.time()
4 | library(knitr)
5 | knit(here::here("vignettes/benchmark.Rmd.orig"), here::here("vignettes/benchmark.Rmd"))
6 | # unlink(here::here("plot-benchmark-small-1.png"))
7 | unlink(here::here("plot-benchmark-1.png"))
8 | Sys.time() - time
9 |
--------------------------------------------------------------------------------