Skip to content

Commit

Permalink
restyle
Browse files Browse the repository at this point in the history
  • Loading branch information
pat-s committed Apr 26, 2021
1 parent 82762ed commit 3451a84
Show file tree
Hide file tree
Showing 36 changed files with 233 additions and 164 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
33 changes: 19 additions & 14 deletions R/LearnerClassifDebug.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ LearnerClassifDebug = R6Class("LearnerClassifDebug", inherit = LearnerClassif,
feature_types = c("logical", "integer", "numeric", "character", "factor", "ordered"),
predict_types = c("response", "prob"),
param_set = ps(
error_predict = p_dbl(0, 1, default = 0, tags = "predict"),
error_train = p_dbl(0, 1, default = 0, tags = "train"),
message_predict = p_dbl(0, 1, default = 0, tags = "predict"),
message_train = p_dbl(0, 1, default = 0, tags = "train"),
predict_missing = p_dbl(0, 1, default = 0, tags = "predict"),
error_predict = p_dbl(0, 1, default = 0, tags = "predict"),
error_train = p_dbl(0, 1, default = 0, tags = "train"),
message_predict = p_dbl(0, 1, default = 0, tags = "predict"),
message_train = p_dbl(0, 1, default = 0, tags = "train"),
predict_missing = p_dbl(0, 1, default = 0, tags = "predict"),
predict_missing_type = p_fct(c("na", "omit"), default = "na", tags = "predict"),
save_tasks = p_lgl(default = FALSE, tags = c("train", "predict")),
segfault_predict = p_dbl(0, 1, default = 0, tags = "predict"),
segfault_train = p_dbl(0, 1, default = 0, tags = "train"),
threads = p_int(1L, tags = c("train", "threads")),
warning_predict = p_dbl(0, 1, default = 0, tags = "predict"),
warning_train = p_dbl(0, 1, default = 0, tags = "train"),
x = p_dbl(0, 1, tags = "train")
save_tasks = p_lgl(default = FALSE, tags = c("train", "predict")),
segfault_predict = p_dbl(0, 1, default = 0, tags = "predict"),
segfault_train = p_dbl(0, 1, default = 0, tags = "train"),
threads = p_int(1L, tags = c("train", "threads")),
warning_predict = p_dbl(0, 1, default = 0, tags = "predict"),
warning_train = p_dbl(0, 1, default = 0, tags = "train"),
x = p_dbl(0, 1, tags = "train")
),
properties = c("twoclass", "multiclass", "missings"),
man = "mlr3::mlr_learners_classif.debug",
Expand Down 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
18 changes: 9 additions & 9 deletions R/LearnerClassifRpart.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ LearnerClassifRpart = R6Class("LearnerClassifRpart", inherit = LearnerClassif,
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
cp = p_dbl(0, 1, default = 0.01, tags = "train"),
keep_model = p_lgl(default = FALSE, tags = "train"),
maxcompete = p_int(0L, default = 4L, tags = "train"),
maxdepth = p_int(1L, 30L, default = 30L, tags = "train"),
maxsurrogate = p_int(0L, default = 5L, tags = "train"),
minbucket = p_int(1L, tags = "train"),
minsplit = p_int(1L, default = 20L, tags = "train"),
cp = p_dbl(0, 1, default = 0.01, tags = "train"),
keep_model = p_lgl(default = FALSE, tags = "train"),
maxcompete = p_int(0L, default = 4L, tags = "train"),
maxdepth = p_int(1L, 30L, default = 30L, tags = "train"),
maxsurrogate = p_int(0L, default = 5L, tags = "train"),
minbucket = p_int(1L, tags = "train"),
minsplit = p_int(1L, default = 20L, tags = "train"),
surrogatestyle = p_int(0L, 1L, default = 0L, tags = "train"),
usesurrogate = p_int(0L, 2L, default = 2L, tags = "train"),
xval = p_int(0L, default = 10L, tags = "train")
usesurrogate = p_int(0L, 2L, default = 2L, tags = "train"),
xval = p_int(0L, default = 10L, tags = "train")
)
ps$values = list(xval = 0L)

Expand Down
18 changes: 9 additions & 9 deletions R/LearnerRegrRpart.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ LearnerRegrRpart = R6Class("LearnerRegrRpart", inherit = LearnerRegr,
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
cp = p_dbl(0, 1, default = 0.01, tags = "train"),
keep_model = p_lgl(default = FALSE, tags = "train"),
maxcompete = p_int(0L, default = 4L, tags = "train"),
maxdepth = p_int(1L, 30L, default = 30L, tags = "train"),
maxsurrogate = p_int(0L, default = 5L, tags = "train"),
minbucket = p_int(1L, tags = "train"),
minsplit = p_int(1L, default = 20L, tags = "train"),
cp = p_dbl(0, 1, default = 0.01, tags = "train"),
keep_model = p_lgl(default = FALSE, tags = "train"),
maxcompete = p_int(0L, default = 4L, tags = "train"),
maxdepth = p_int(1L, 30L, default = 30L, tags = "train"),
maxsurrogate = p_int(0L, default = 5L, tags = "train"),
minbucket = p_int(1L, tags = "train"),
minsplit = p_int(1L, default = 20L, tags = "train"),
surrogatestyle = p_int(0L, 1L, default = 0L, tags = "train"),
usesurrogate = p_int(0L, 2L, default = 2L, tags = "train"),
xval = p_int(0L, default = 10L, tags = "train")
usesurrogate = p_int(0L, 2L, default = 2L, tags = "train"),
xval = p_int(0L, default = 10L, tags = "train")
)
ps$values = list(xval = 0L)

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
2 changes: 1 addition & 1 deletion R/ResamplingBootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ResamplingBootstrap = R6Class("ResamplingBootstrap", inherit = Resampling,
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
ratio = p_dbl(0, upper = 1, tags = "required"),
ratio = p_dbl(0, upper = 1, tags = "required"),
repeats = p_int(1L, tags = "required")
)
ps$values = list(ratio = 1, repeats = 30L)
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
2 changes: 1 addition & 1 deletion R/ResamplingRepeatedCV.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ResamplingRepeatedCV = R6Class("ResamplingRepeatedCV", inherit = Resampling,
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
folds = p_int(2L, tags = "required"),
folds = p_int(2L, tags = "required"),
repeats = p_int(1L)
)
ps$values = list(repeats = 10L, folds = 10L)
Expand Down
2 changes: 1 addition & 1 deletion R/ResamplingSubsampling.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ResamplingSubsampling = R6Class("ResamplingSubsampling", inherit = Resampling,
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
ratio = p_dbl(0, 1, tags = "required"),
ratio = p_dbl(0, 1, tags = "required"),
repeats = p_int(1, tags = "required")
)
ps$values = list(repeats = 30L, ratio = 2 / 3)
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
6 changes: 3 additions & 3 deletions R/TaskGeneratorSimplex.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ TaskGeneratorSimplex = R6Class("TaskGeneratorSimplex",
initialize = function() {
ps = ps(
center = p_lgl(default = TRUE),
d = p_int(1L, default = 3L),
sd = p_dbl(0, default = 0.1),
sides = p_int(1L, default = 1L)
d = p_int(1L, default = 3L),
sd = p_dbl(0, default = 0.1),
sides = p_int(1L, default = 1L)
)

super$initialize(id = "simplex", "classif", "mlbench", ps, man = "mlr3::mlr_task_generators_simplex")
Expand Down
2 changes: 1 addition & 1 deletion R/TaskGeneratorSpirals.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TaskGeneratorSpirals = R6Class("TaskGeneratorSpirals",
initialize = function() {
ps = ps(
cycles = p_int(1L, default = 1L),
sd = p_dbl(0, default = 0)
sd = p_dbl(0, default = 0)
)

super$initialize(id = "spirals", "classif", "mlbench", ps, man = "mlr3::mlr_task_generators_spirals")
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
Loading

0 comments on commit 3451a84

Please sign in to comment.