15
15
# '
16
16
# ' @section Construction:
17
17
# ' ```
18
- # ' PipeOpTargetTrafo$new(id, param_set = ps(), param_vals = list() packages = character(0), task_type_in = "Task", task_type_out = task_type_in, tags = NULL)
18
+ # ' PipeOpTargetTrafo$new(id, param_set = ps(), param_vals = list(), packages = character(0), task_type_in = "Task", task_type_out = task_type_in, tags = NULL)
19
19
# ' ```
20
20
# '
21
21
# ' * `id` :: `character(1)`\cr
22
22
# ' Identifier of resulting object. See `$id` slot of [`PipeOp`].
23
23
# ' * `param_set` :: [`ParamSet`][paradox::ParamSet]\cr
24
- # ' Parameter space description. This should be created by the subclass and given to
25
- # ' `super$initialize()`.
24
+ # ' Parameter space description. This should be created by the subclass and given to `super$initialize()`.
26
25
# ' * `param_vals` :: named `list`\cr
27
26
# ' List of hyperparameter settings, overwriting the hyperparameter settings given in `param_set`.
28
27
# ' The subclass should have its own `param_vals` parameter and pass it on to `super$initialize()`.
29
28
# ' Default `list()`.
30
29
# ' * `task_type_in` :: `character(1)`\cr
31
- # ' The class of [`Task`][mlr3::Task] that should be accepted as input. This
32
- # ' should generally be a `character(1)` identifying a type of [`Task`][mlr3::Task], e.g. `"Task"`, `"TaskClassif"` or
33
- # ' `"TaskRegr"` (or another subclass introduced by other packages). Default is `"Task"`.
30
+ # ' The class of [`Task`][mlr3::Task] that should be accepted as input. This should generally be a `character(1)`
31
+ # ' identifying a type of [`Task`][mlr3::Task], e.g. `"Task"`, `"TaskClassif"` or `"TaskRegr"` (or another subclass
32
+ # ' introduced by other packages). Default is `"Task"`.
34
33
# ' * `task_type_out` :: `character(1)`\cr
35
- # ' The class of [`Task`][mlr3::Task] that is produced as output. This
36
- # ' should generally be a `character(1)` identifying a type of [`Task`][mlr3::Task], e.g. `"Task"`, `"TaskClassif"` or
37
- # ' `"TaskRegr"` (or another subclass introduced by other packages). Default is the value of `task_type_in`.
38
- # ' * packages :: `character`\cr
34
+ # ' The class of [`Task`][mlr3::Task] that is produced as output. This should generally be a `character(1)`
35
+ # ' identifying a type of [`Task`][mlr3::Task], e.g. `"Task"`, `"TaskClassif"` or `"TaskRegr"` (or another subclass
36
+ # ' introduced by other packages). Default is the value of `task_type_in`.
37
+ # ' * ` packages` :: `character`\cr
39
38
# ' Set of all required packages for the [`PipeOp`]'s methods. See `$packages` slot. Default is
40
39
# ' `character(0)`.
41
- # ' * tags :: `character` | `NULL`\cr
40
+ # ' * ` tags` :: `character` | `NULL`\cr
42
41
# ' Tags of the resulting `PipeOp`. This is added to the tag `"target transform"`. Default `NULL`.
43
42
# '
44
43
# ' @section Input and Output Channels:
45
- # ' [ `PipeOpTargetTrafo`] has one input channels named `"input"` taking a [`Task`][mlr3::Task] (or whatever class
44
+ # ' `PipeOpTargetTrafo` has one input channels named `"input"` taking a [`Task`][mlr3::Task] (or whatever class
46
45
# ' was specified by the `task_type` during construction) both during training and prediction.
47
46
# '
48
- # ' [ `PipeOpTargetTrafo`] has two output channels named `"fun"` and `"output"`. During training,
47
+ # ' `PipeOpTargetTrafo` has two output channels named `"fun"` and `"output"`. During training,
49
48
# ' `"fun"` returns `NULL` and during prediction, `"fun"` returns a function that can later be used
50
49
# ' to invert the transformation done during training according to the overloaded `.train_invert()`
51
50
# ' and `.invert()` functions. `"output"` returns the modified input [`Task`][mlr3::Task] (or `task_type`)
56
55
# ' `.get_state()` function.
57
56
# '
58
57
# ' @section Internals:
59
- # ' [ `PipeOpTargetTrafo`] is an abstract class inheriting from [`PipeOp`]. It implements the
58
+ # ' `PipeOpTargetTrafo` is an abstract class inheriting from [`PipeOp`]. It implements the
60
59
# ' `private$.train()` and `private$.predict()` functions. These functions perform checks and go on
61
60
# ' to call `.get_state()`, `.transform()`, `.train_invert()`. `.invert()` is packaged and sent along
62
61
# ' the `"fun"` output to be applied to a [`Prediction`][mlr3::Prediction] by [`PipeOpTargetInvert`].
63
- # ' A subclass of [ `PipeOpTargetTrafo`] should implement these functions and be used in combination
62
+ # ' A subclass of `PipeOpTargetTrafo` should implement these functions and be used in combination
64
63
# ' with [`PipeOpTargetInvert`].
65
64
# '
66
65
# ' @section Fields:
70
69
# ' Methods inherited from [`PipeOp`], as well as:
71
70
# ' * `.get_state(task)`\cr
72
71
# ' ([`Task`][mlr3::Task]) -> `list`\cr
73
- # ' Called by [ `PipeOpTargetTrafo`] 's implementation of `private$.train()`. Takes a single
72
+ # ' Called by `PipeOpTargetTrafo`'s implementation of `private$.train()`. Takes a single
74
73
# ' [`Task`][mlr3::Task] as input and returns a `list` to set the `$state`.
75
74
# ' `.get_state()` will be called a single time during *training* right before
76
75
# ' `.transform()` is called. The return value (i.e. the `$state`) should contain info needed in
77
76
# ' `.transform()` as well as in `.invert()`.\cr
78
77
# ' The base implementation returns `list()` and should be overloaded if setting the state is desired.
79
78
# ' * `.transform(task, phase)`\cr
80
79
# ' ([`Task`][mlr3::Task], `character(1)`) -> [`Task`][mlr3::Task]\cr
81
- # ' Called by [ `PipeOpTargetTrafo`] 's implementation of `private$.train()` and
80
+ # ' Called by `PipeOpTargetTrafo`'s implementation of `private$.train()` and
82
81
# ' `private$.predict()`. Takes a single [`Task`][mlr3::Task] as input and modifies it.
83
82
# ' This should typically consist of calculating a new target and modifying the
84
83
# ' [`Task`][mlr3::Task] by using the [`convert_task`][mlr3::convert_task] function. `.transform()` will be called during training and
93
92
# ' This function is abstract and should be overloaded by inheriting classes.
94
93
# ' * `.train_invert(task)`\cr
95
94
# ' ([`Task`][mlr3::Task]) -> `any`\cr
96
- # ' Called by [ `PipeOpTargetTrafo`] 's implementation of `private$.predict()`. Takes a single
95
+ # ' Called by `PipeOpTargetTrafo`'s implementation of `private$.predict()`. Takes a single
97
96
# ' [`Task`][mlr3::Task] as input and returns an arbitrary value that will be given as
98
- # ' `predict_phase_state` to `.invert()`. This should not modify the input [`Task`][mlr3::Task] .\cr
97
+ # ' `predict_phase_state` to `.invert()`. This should not modify the input [`Task`][mlr3::Task].\cr
99
98
# ' The base implementation returns a list with a single element, the `$truth` column of the [`Task`][mlr3::Task],
100
99
# ' and should be overloaded if a more training-phase-dependent state is desired.
101
100
# ' * `.invert(prediction, predict_phase_state)`\cr
102
101
# ' ([`Prediction`][mlr3::Prediction], `any`) -> [`Prediction`][mlr3::Prediction]\cr
103
- # ' Takes a [`Prediction`][mlr3::Prediction] and a `predict_phase_state`
104
- # ' object as input and inverts the prediction. This function is sent as `"fun"` to
105
- # ' [`PipeOpTargetInvert`].\cr
102
+ # ' Takes a [`Prediction`][mlr3::Prediction] and a `predict_phase_state` object as input and inverts the prediction.
103
+ # ' This function is sent as `"fun"` to [`PipeOpTargetInvert`].\cr
106
104
# ' This function is abstract and should be overloaded by inheriting classes. Care should be
107
105
# ' taken that the `predict_type` of the [`Prediction`][mlr3::Prediction] being inverted is handled well.
108
106
# ' * `.invert_help(predict_phase_state)`\cr
@@ -188,7 +186,7 @@ PipeOpTargetTrafo = R6Class("PipeOpTargetTrafo",
188
186
# '
189
187
# ' During prediction phase the function supplied through `"fun"` is called with a `list` containing
190
188
# ' the `"prediction"` as a single element, and should return a `list` with a single element
191
- # ' (a [`Prediction`][mlr3::Prediction]) that is returned by [ `PipeOpTargetInvert`] .
189
+ # ' (a [`Prediction`][mlr3::Prediction]) that is returned by `PipeOpTargetInvert`.
192
190
# '
193
191
# ' @section Construction:
194
192
# ' ```
@@ -201,18 +199,18 @@ PipeOpTargetTrafo = R6Class("PipeOpTargetTrafo",
201
199
# ' List of hyperparameter settings, overwriting the hyperparameter settings that would otherwise be set during construction. Default `list()`.
202
200
# '
203
201
# ' @section Input and Output Channels:
204
- # ' [ `PipeOpTargetInvert`] has two input channels named `"fun"` and `"prediction"`. During
202
+ # ' `PipeOpTargetInvert` has two input channels named `"fun"` and `"prediction"`. During
205
203
# ' training, both take `NULL` as input. During prediction, `"fun"` takes a function and
206
204
# ' `"prediction"` takes a [`Prediction`][mlr3::Prediction].
207
205
# '
208
- # ' [ `PipeOpTargetInvert`] has one output channel named `"output"` and returns `NULL` during
206
+ # ' `PipeOpTargetInvert` has one output channel named `"output"` and returns `NULL` during
209
207
# ' training and a [`Prediction`][mlr3::Prediction] during prediction.
210
208
# '
211
209
# ' @section State:
212
210
# ' The `$state` is left empty (`list()`).
213
211
# '
214
212
# ' @section Parameters:
215
- # ' [ `PipeOpTargetInvert`] has no parameters.
213
+ # ' `PipeOpTargetInvert` has no parameters.
216
214
# '
217
215
# ' @section Internals:
218
216
# ' Should be used in combination with a subclass of [`PipeOpTargetTrafo`].
@@ -283,7 +281,7 @@ mlr_pipeops$add("targetinvert", PipeOpTargetInvert)
283
281
# '
284
282
# ' @section Parameters:
285
283
# ' The parameters are the parameters inherited from [`PipeOpTargetTrafo`], as well as:
286
- # ' * `trafo` :: `function` `data.table` -> `data.table`\cr
284
+ # ' * `trafo` :: `function` `data.table` -> `data.frame` | `data. table` | `matrix `\cr
287
285
# ' Transformation function for the target. Should only be a function of the target, i.e., taking a
288
286
# ' single `data.table` argument, typically with one column. The return value is used as the new
289
287
# ' target of the resulting [`Task`][mlr3::Task]. To change target names, change the column name of the data
@@ -349,8 +347,8 @@ PipeOpTargetMutate = R6Class("PipeOpTargetMutate",
349
347
initialize = function (id = " targetmutate" , param_vals = list (), new_task_type = NULL ) {
350
348
private $ .new_task_type = assert_choice(new_task_type , mlr_reflections $ task_types $ type , null.ok = TRUE )
351
349
ps = ps(
352
- trafo = p_uty(tags = c(" train" , " predict" ), custom_check = crate( function ( x ) check_function( x , nargs = 1L )) ),
353
- inverter = p_uty(tags = " predict" , custom_check = crate( function ( x ) check_function( x , nargs = 1L )) )
350
+ trafo = p_uty(tags = c(" train" , " predict" ), custom_check = check_function ),
351
+ inverter = p_uty(tags = " predict" , custom_check = check_function )
354
352
)
355
353
# We could add a condition here for new_task_type on trafo and inverter when mlr-org/paradox#278 has an answer.
356
354
# HOWEVER conditions are broken in paradox, it is a terrible idea to use them in PipeOps,
@@ -373,8 +371,11 @@ PipeOpTargetMutate = R6Class("PipeOpTargetMutate",
373
371
374
372
.transform = function (task , phase ) {
375
373
new_target = self $ param_set $ values $ trafo(task $ data(cols = task $ target_names ))
374
+ if (! is.data.frame(new_target ) && ! is.matrix(new_target )) {
375
+ stopf(" Hyperparameter 'trafo' must be a function returning a 'data.frame', 'data.table', or 'matrix', not '%s'." , class(new_target )[[1L ]])
376
+ }
376
377
task $ cbind(new_target )
377
- convert_task(task , target = colnames(new_target ), new_type = private $ .new_task_type , drop_original_target = TRUE )
378
+ convert_task(task , target = colnames(new_target ), new_type = private $ .new_task_type , drop_original_target = TRUE , drop_levels = FALSE )
378
379
},
379
380
380
381
.invert = function (prediction , predict_phase_state ) {
@@ -478,7 +479,7 @@ PipeOpTargetTrafoScaleRange = R6Class("PipeOpTargetTrafoScaleRange",
478
479
new_target = self $ state $ offset + x * self $ state $ scale
479
480
setnames(new_target , paste0(colnames(new_target ), " .scaled" ))
480
481
task $ cbind(new_target )
481
- convert_task(task , target = colnames(new_target ), drop_original_target = TRUE )
482
+ convert_task(task , target = colnames(new_target ), drop_original_target = TRUE , drop_levels = FALSE )
482
483
},
483
484
484
485
.invert = function (prediction , predict_phase_state ) {
0 commit comments