Skip to content

Commit

Permalink
restyle
Browse files Browse the repository at this point in the history
  • Loading branch information
pat-s committed Apr 30, 2021
1 parent 82762ed commit 4ab95db
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 83 deletions.
7 changes: 4 additions & 3 deletions R/BenchmarkResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ BenchmarkResult = R6Class("BenchmarkResult",
tab = insert_named(tab, scores)

cns = c("uhash", "nr", "resample_result", "task_id", "learner_id", "resampling_id", "iters",
"warnings", "errors", "params", ids(measures))
"warnings", "errors", "params", ids(measures))
cns = intersect(cns, names(tab))
tab[, cns, with = FALSE]
},
Expand Down Expand Up @@ -300,10 +300,11 @@ BenchmarkResult = R6Class("BenchmarkResult",
learner_phashes = NULL

filter_if_not_null = function(column, hashes) {
if (is.null(hashes))
if (is.null(hashes)) {
fact
else
} else {
fact[unique(hashes), on = column, nomatch = NULL]
}
}


Expand Down
11 changes: 8 additions & 3 deletions R/Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ Learner = R6Class("Learner",
}

pdata = learner_predict(self, task, row_ids)
if (is.null(pdata))
if (is.null(pdata)) {
return(NULL)
}

as_prediction(check_prediction_data(pdata))
},
Expand All @@ -269,8 +270,9 @@ Learner = R6Class("Learner",
newdata = as.data.table(assert_data_frame(newdata, min.rows = 1L))

if (is.null(task)) {
if (is.null(self$state$train_task))
if (is.null(self$state$train_task)) {
stopf("No task stored, and no task provided")
}
task = self$state$train_task$clone()
} else {
task = assert_task(as_task(task, clone = TRUE))
Expand Down Expand Up @@ -414,7 +416,10 @@ Learner = R6Class("Learner",
switch(name,
.param_set = value$clone(deep = TRUE),
fallback = if (is.null(value)) NULL else value$clone(deep = TRUE),
state = { value$log = copy(value$log); value },
state = {
value$log = copy(value$log)
value
},
value
)
}
Expand Down
9 changes: 7 additions & 2 deletions R/LearnerClassifDebug.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ LearnerClassifDebug = R6Class("LearnerClassifDebug", inherit = LearnerClassif,
if (!is.null(pv$predict_missing)) {
ii = sample.int(n, n * pv$predict_missing)
prob = switch(missing_type,
"na" = { prob[ii, ] = NA_real_; prob },
"omit" = { prob[ii,, drop = FALSE] }
"na" = {
prob[ii, ] = NA_real_
prob
},
"omit" = {
prob[ii, , drop = FALSE]
}
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion R/MeasureDebug.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ MeasureDebug = R6Class("MeasureDebug",
private = list(
.score = function(prediction, ...) {
na_ratio = self$param_set$get_values()$na_ratio
if (na_ratio > runif(1L))
if (na_ratio > runif(1L)) {
return(NA_integer_)
}
length(prediction$row_ids)
}
)
Expand Down
2 changes: 1 addition & 1 deletion R/PredictionDataClassif.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ c.PredictionDataClassif = function(..., keep_duplicates = TRUE) {
if (!keep_duplicates) {
keep = !duplicated(tab, by = "row_ids", fromLast = TRUE)
tab = tab[keep]
prob = prob[keep,, drop = FALSE]
prob = prob[keep, , drop = FALSE]
}

result = as.list(tab)
Expand Down
11 changes: 7 additions & 4 deletions R/ResampleResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ ResampleResult = R6Class("ResampleResult",
set(tab, j = "learner_id", value = ids(tab[["learner"]]))
set(tab, j = "resampling_id", value = ids(tab[["resampling"]]))
setcolorder(tab, c("task", "task_id", "learner", "learner_id", "resampling", "resampling_id",
"iteration", "prediction"))
"iteration", "prediction"))
}

if (conditions) {
Expand Down Expand Up @@ -224,8 +224,9 @@ ResampleResult = R6Class("ResampleResult",
task = function(rhs) {
assert_ro_binding(rhs)
tab = self$data$tasks(self$view)
if (nrow(tab) == 0L)
if (nrow(tab) == 0L) {
return(NULL)
}
tab$task[[1L]]
},

Expand All @@ -235,8 +236,9 @@ ResampleResult = R6Class("ResampleResult",
learner = function(rhs) {
assert_ro_binding(rhs)
tab = self$data$learners(self$view, states = FALSE)
if (nrow(tab) == 0L)
if (nrow(tab) == 0L) {
return(NULL)
}
tab$learner[[1L]]
},

Expand All @@ -245,8 +247,9 @@ ResampleResult = R6Class("ResampleResult",
resampling = function(rhs) {
assert_ro_binding(rhs)
tab = self$data$resamplings(self$view)
if (nrow(tab) == 0L)
if (nrow(tab) == 0L) {
return(NULL)
}
tab$resampling[[1L]]
},

Expand Down
3 changes: 2 additions & 1 deletion R/ResamplingHoldout.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ ResamplingHoldout = R6Class("ResamplingHoldout", inherit = Resampling,

.combine = function(instances) {
list(train = do.call(c, map(instances, "train")), test = do.call(c, map(instances, "test")))
})
}
)
)

#' @include mlr_resamplings.R
Expand Down
14 changes: 8 additions & 6 deletions R/ResultData.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,20 @@ ResultData = R6Class("ResultData",
#' Returns `NULL` if the [ResultData] is empty.
task_type = function() {
tab = self$data$tasks
if (nrow(tab))
if (nrow(tab)) {
tab$task[[1L]]$task_type
else
} else {
NULL
}
}
),

private = list(
get_view_index = function(view) {
if (is.null(view))
if (is.null(view)) {
return(TRUE)
self$data$fact[list(view), on = "uhash", nomatch = NULL, which = TRUE]
}
self$data$fact[list(view), on = "uhash", nomatch = NULL, which = TRUE]
},

deep_clone = function(name, value) {
Expand Down Expand Up @@ -382,8 +384,8 @@ reassemble_learners = function(learners, states = NULL, param_vals = NULL) {
learners = lapply(learners, function(l) l$clone(deep = TRUE))

if (!is.null(states)) {
Map(function(l, s) {
l$state = s
Map(function(l, s) {
l$state = s
}, l = learners, s = states)
}

Expand Down
4 changes: 2 additions & 2 deletions R/Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ Task = R6Class("Task",

ci = self$col_info[list(keep_cols), on = "id"]
data = do.call(data.table, Map(auto_convert,
value = as.list(data)[ci$id],
id = ci$id, type = ci$type, levels = ci$levels))
value = as.list(data)[ci$id],
id = ci$id, type = ci$type, levels = ci$levels))

data = as_data_backend(data, primary_key = pk)
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/TaskClassif_penguins.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ load_task_penguins = function() {
penguins = as.data.table(palmerpenguins::penguins)
setnames(penguins,
old = c("bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"),
new = c("bill_length", "bill_depth", "flipper_length", "body_mass")
new = c("bill_length", "bill_depth", "flipper_length", "body_mass")
)

b = as_data_backend(penguins)
Expand Down
2 changes: 1 addition & 1 deletion R/TaskGeneratorMoons.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ TaskGeneratorMoons = R6Class("TaskGeneratorMoons",

data.table(
y = factor(rep(c("A", "B"), c(n1, n2)), levels = c("A", "B")),
x1 = 5 * cos(x) + rnorm(n, mean = mu, sd = sigma),
x1 = 5 * cos(x) + rnorm(n, mean = mu, sd = sigma),
x2 = 10 * sin(x) + rnorm(n, mean = mu, sd = sigma)
)
},
Expand Down
2 changes: 1 addition & 1 deletion R/TaskSupervised.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' @export
#' @examples
#' TaskSupervised$new("penguins", task_type = "classif", backend = palmerpenguins::penguins,
#' target = "species")
#' target = "species")
TaskSupervised = R6Class("TaskSupervised", inherit = Task,
public = list(

Expand Down
3 changes: 2 additions & 1 deletion R/as_prediction_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ as_prediction_data.list = function(x, task, row_ids = task$row_ids, check = TRUE
assert_names(names(x), subset.of = predict_types)

x$row_ids = row_ids
if (inherits(task, "TaskSupervised"))
if (inherits(task, "TaskSupervised")) {
x$truth = task$truth(row_ids)
}

pdata = new_prediction_data(x, task_type = task$task_type)
if (check) {
Expand Down
5 changes: 3 additions & 2 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ assert_measure = function(measure, task = NULL, learner = NULL, .var.name = vnam
#' @rdname mlr_assertions
assert_measures = function(measures, task = NULL, learner = NULL, .var.name = vname(measures)) {
lapply(measures, assert_measure, task = task, learner = learner, .var.name = .var.name)
if (anyDuplicated(ids(measures)))
if (anyDuplicated(ids(measures))) {
stopf("Measures need to have unique IDs")
}
invisible(measures)
}

Expand Down Expand Up @@ -269,7 +270,7 @@ assert_prediction_count = function(actual, expected, type) {

assert_row_sums = function(prob) {
for (i in seq_row(prob)) {
x = prob[i,, drop = TRUE]
x = prob[i, , drop = TRUE]
if (anyMissing(x)) {
if (!allMissing(x)) {
stopf("Probabilities for observation %i are partly missing", i)
Expand Down
57 changes: 43 additions & 14 deletions R/auto_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,35 @@ mlr_reflections$auto_converters = ee = new.env(parent = emptyenv())

## from: logical
ee[["logical___integer"]] =
function(value, type, levels) { as.integer(value) }
function(value, type, levels) {
as.integer(value)
}
ee[["logical___numeric"]] =
function(value, type, levels) { as.double(value) }
function(value, type, levels) {
as.double(value)
}
ee[["logical___character"]] =
function(value, type, levels) { as.character(value) }
function(value, type, levels) {
as.character(value)
}
ee[["logical___factor"]] =
function(value, type, levels) { factor(value, levels = union(levels, value), ordered = FALSE) }
function(value, type, levels) {
factor(value, levels = union(levels, value), ordered = FALSE)
}
ee[["logical___ordered"]] =
function(value, type, levels) { if (all(value %in% c(NA_character_, levels))) factor(value, levels = levels, ordered = TRUE) else NULL }
function(value, type, levels) {
if (all(value %in% c(NA_character_, levels))) factor(value, levels = levels, ordered = TRUE) else NULL
}
ee[["logical___POSIXct"]] =
function(value, type, levels) { if (allMissing(value)) .POSIXct(value, tz = "") else value }
function(value, type, levels) {
if (allMissing(value)) .POSIXct(value, tz = "") else value
}

## from: integer
ee[["integer___logical"]] =
function(value, type, levels) { if (test_integerish(value, lower = 0L, upper = 1L)) as.logical(value) else value }
function(value, type, levels) {
if (test_integerish(value, lower = 0L, upper = 1L)) as.logical(value) else value
}
ee[["integer___numeric"]] =
ee[["logical___numeric"]]
ee[["integer___character"]] =
Expand All @@ -35,7 +49,9 @@ ee[["integer___POSIXct"]] =
ee[["numeric___logical"]] =
ee[["integer___logical"]]
ee[["numeric___integer"]] =
function(value, type, levels) { if (test_integerish(value)) as.integer(value) else value }
function(value, type, levels) {
if (test_integerish(value)) as.integer(value) else value
}
ee[["numeric___character"]] =
ee[["logical___character"]]
ee[["numeric___factor"]] =
Expand All @@ -47,27 +63,40 @@ ee[["numeric___POSIXct"]] =

## from: character
ee[["character___logical"]] =
function(value, type, levels) { if (all(value %in% c(NA_character_, "TRUE", "FALSE", "true", "false", "T", "F"))) as.logical(value) else value }
function(value, type, levels) {
if (all(value %in% c(NA_character_, "TRUE", "FALSE", "true", "false", "T", "F"))) as.logical(value) else value
}
ee[["character___integer"]] =
function(value, type, levels) { if (allMissing(value)) as.integer(value) else value }
function(value, type, levels) {
if (allMissing(value)) as.integer(value) else value
}
ee[["character___numeric"]] =
function(value, type, levels) { if (allMissing(value)) as.double(value) else value }
function(value, type, levels) {
if (allMissing(value)) as.double(value) else value
}
ee[["character___factor"]] =
ee[["logical___factor"]]
ee[["character___ordered"]] =
ee[["logical___ordered"]]
ee[["character___POSIXct"]] =
function(value, type, levels) { x = try(as.POSIXct(value, ""), silent = TRUE); if (inherits(x, "try-error")) value else x }
function(value, type, levels) {
x = try(as.POSIXct(value, ""), silent = TRUE)
if (inherits(x, "try-error")) value else x
}

## from: factor
ee[["factor___logical"]] =
ee[["character___logical"]]
ee[["factor___character"]] =
ee[["logical___character"]]
ee[["factor___factor"]] =
function(value, type, levels) { factor(value, levels = union(levels, levels(value)), ordered = FALSE) }
function(value, type, levels) {
factor(value, levels = union(levels, levels(value)), ordered = FALSE)
}
ee[["factor___ordered"]] =
function(value, type, levels) { if (all(levels(value) %in% levels)) factor(value, levels = levels, ordered = TRUE) else NULL }
function(value, type, levels) {
if (all(levels(value) %in% levels)) factor(value, levels = levels, ordered = TRUE) else NULL
}
ee[["factor___POSIXct"]] =
ee[["character___POSIXct"]]

Expand Down
3 changes: 1 addition & 2 deletions R/benchmark_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
#'
#' # manual instantiation (not suited for a fair comparison of learners!)
#' Map(function(task, resampling) {
#' resampling$instantiate(task)
#' resampling$instantiate(task)
#' }, task = grid$task, resampling = grid$resampling)
#'
#' \dontrun{
#' benchmark(grid)
#' }
Expand Down
18 changes: 9 additions & 9 deletions R/bibentries.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ bibentries = c(

bischl_2012 = bibentry("article",
title = "Resampling Methods for Meta-Model Validation with Recommendations for Evolutionary Computation",
author = "Bernd Bischl and Olaf Mersmann and Heike Trautmann and Claus Weihs",
year = "2012",
month = "jun",
journal = "Evolutionary Computation",
publisher = "MIT Press - Journals",
volume = "20",
number = "2",
pages = "249--275",
doi = "10.1162/evco_a_00069"
author = "Bernd Bischl and Olaf Mersmann and Heike Trautmann and Claus Weihs",
year = "2012",
month = "jun",
journal = "Evolutionary Computation",
publisher = "MIT Press - Journals",
volume = "20",
number = "2",
pages = "249--275",
doi = "10.1162/evco_a_00069"
),

breiman_1984 = bibentry("book",
Expand Down
3 changes: 2 additions & 1 deletion R/default_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
#' default_measures("classif")
#' default_measures("regr")
default_measures = function(task_type) {
if (is.null(task_type))
if (is.null(task_type)) {
return(list())
}
assert_choice(task_type, names(mlr_reflections$default_measures))
keys = mlr_reflections$default_measures[[task_type]]
mlr_measures$mget(keys)
Expand Down
Loading

0 comments on commit 4ab95db

Please sign in to comment.