Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style all scopes (v2) #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 @@ -351,8 +351,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
6 changes: 3 additions & 3 deletions R/TaskGeneratorMoons.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ TaskGeneratorMoons = R6Class("TaskGeneratorMoons",
x = c(runif(n1, 0, pi), runif(n2, pi, 2 * pi))

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),
x2 = 10 * sin(x) + rnorm(n, mean = mu, sd = sigma)
y = factor(rep(c("A", "B"), c(n1, n2)), levels = c("A", "B")),
x1 = 5 * cos(x) + rnorm(n, mean = mu, sd = sigma),
x2 = 10 * sin(x) + rnorm(n, mean = mu, sd = sigma)
Comment on lines +60 to +62
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lorenzwalthert This indentation looks off - is this causes because l61/62 start with numbers?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indention or spacing after the first =? It was aligned before and styler does not allow complicated left alignment, so I made it right-aligned. We can also unalign if you want.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I see - cos and sin were aligned before but complicated left alignment is not possible.

I think right alignment here is somehow non-standard - I think I'd prefer non-matching left alignment after =.

        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
3 changes: 2 additions & 1 deletion 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
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
20 changes: 10 additions & 10 deletions R/bibentries.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ 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"
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"
),

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
6 changes: 3 additions & 3 deletions R/mlr_reflections.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ local({
### Task
# task types + constructors
mlr_reflections$task_types = rowwise_table(.key = "type",
~type, ~package, ~task, ~learner, ~prediction, ~measure,
"regr", "mlr3", "TaskRegr", "LearnerRegr", "PredictionRegr", "MeasureRegr",
"classif", "mlr3", "TaskClassif", "LearnerClassif", "PredictionClassif", "MeasureClassif"
~type, ~package, ~task, ~learner, ~prediction, ~measure,
"regr", "mlr3", "TaskRegr", "LearnerRegr", "PredictionRegr", "MeasureRegr",
"classif", "mlr3", "TaskClassif", "LearnerClassif", "PredictionClassif", "MeasureClassif"
Comment on lines -77 to +79
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

mlr_reflections$task_feature_types = c(
Expand Down
3 changes: 2 additions & 1 deletion R/mlr_resamplings.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ as.data.table.DictionaryResampling = function(x, ...) { # nolint
setkeyv(map_dtr(x$keys(), function(key) {
r = tryCatch(x$get(key),
missingDefaultError = function(e) NULL)
if (is.null(r))
if (is.null(r)) {
return(list(key = key))
}

list(key = key, params = list(r$param_set$ids()), iters = r$iters)
}, .fill = TRUE), "key")[]
Expand Down
Loading