Skip to content

Commit

Permalink
fix: don't use delayedAssign
Browse files Browse the repository at this point in the history
this simply was not necessary
  • Loading branch information
sebffischer committed Oct 16, 2023
1 parent 3e43a28 commit 47f8cda
Show file tree
Hide file tree
Showing 36 changed files with 3,483 additions and 3,590 deletions.
153 changes: 75 additions & 78 deletions R/learner_CoxBoost_surv_coxboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,96 +24,93 @@
#' @template seealso_learner
#' @template example
#' @export
delayedAssign(
"LearnerSurvCoxboost",
R6Class("LearnerSurvCoxboost",
inherit = mlr3proba::LearnerSurv,
LearnerSurvCoxboost = R6Class("LearnerSurvCoxboost",
inherit = mlr3proba::LearnerSurv,

public = list(
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
unpen.index = p_uty(tags = "train"),
standardize = p_lgl(default = TRUE, tags = "train"),
stepno = p_int(default = 100, lower = 0, tags = "train"),
penalty = p_dbl(tags = "train"),
criterion = p_fct(default = "pscore", levels = c("pscore", "score", "hpscore", "hscore"), tags = "train"),
stepsize.factor = p_dbl(default = 1, tags = "train"),
sf.scheme = p_fct(default = "sigmoid", levels = c("sigmoid", "linear"), tags = "train"),
pendistmat = p_uty(tags = "train"),
connected.index = p_uty(tags = "train"),
x.is.01 = p_lgl(default = FALSE, tags = "train"),
return.score = p_lgl(default = TRUE, tags = "train"),
trace = p_lgl(default = FALSE, tags = "train"),
at.step = p_uty(tags = "predict")
)
public = list(
#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function() {
ps = ps(
unpen.index = p_uty(tags = "train"),
standardize = p_lgl(default = TRUE, tags = "train"),
stepno = p_int(default = 100, lower = 0, tags = "train"),
penalty = p_dbl(tags = "train"),
criterion = p_fct(default = "pscore", levels = c("pscore", "score", "hpscore", "hscore"), tags = "train"),
stepsize.factor = p_dbl(default = 1, tags = "train"),
sf.scheme = p_fct(default = "sigmoid", levels = c("sigmoid", "linear"), tags = "train"),
pendistmat = p_uty(tags = "train"),
connected.index = p_uty(tags = "train"),
x.is.01 = p_lgl(default = FALSE, tags = "train"),
return.score = p_lgl(default = TRUE, tags = "train"),
trace = p_lgl(default = FALSE, tags = "train"),
at.step = p_uty(tags = "predict")
)

super$initialize(
# see the mlr3book for a description: https://mlr3book.mlr-org.com/extending-mlr3.html
id = "surv.coxboost",
packages = c("mlr3extralearners", "CoxBoost", "pracma"),
feature_types = c("integer", "numeric"),
predict_types = c("distr", "crank", "lp"),
param_set = ps,
properties = "weights",
man = "mlr3extralearners::mlr_learners_surv.coxboost",
label = "Likelihood-based Boosting"
)
}
),
super$initialize(
# see the mlr3book for a description: https://mlr3book.mlr-org.com/extending-mlr3.html
id = "surv.coxboost",
packages = c("mlr3extralearners", "CoxBoost", "pracma"),
feature_types = c("integer", "numeric"),
predict_types = c("distr", "crank", "lp"),
param_set = ps,
properties = "weights",
man = "mlr3extralearners::mlr_learners_surv.coxboost",
label = "Likelihood-based Boosting"
)
}
),

private = list(
.train = function(task) {
pars = self$param_set$get_values(tags = "train")
private = list(
.train = function(task) {
pars = self$param_set$get_values(tags = "train")

if ("weights" %in% task$properties) {
pars$weights = as.numeric(task$weights$weight)
}
if ("weights" %in% task$properties) {
pars$weights = as.numeric(task$weights$weight)
}

data = task$data()
tn = task$target_names
time = data[[tn[1L]]]
status = data[[tn[2L]]]
data = as.matrix(data[, !tn, with = FALSE])
data = task$data()
tn = task$target_names
time = data[[tn[1L]]]
status = data[[tn[2L]]]
data = as.matrix(data[, !tn, with = FALSE])

with_package("CoxBoost", {
invoke(
CoxBoost::CoxBoost,
time = time,
status = status,
x = data,
.args = pars
)
})
},
with_package("CoxBoost", {
invoke(
CoxBoost::CoxBoost,
time = time,
status = status,
x = data,
.args = pars
)
})
},

.predict = function(task) {
.predict = function(task) {

pars = self$param_set$get_values(tags = "predict")
pars = self$param_set$get_values(tags = "predict")

# get newdata and ensure same ordering in train and predict
newdata = as.matrix(ordered_features(task, self))
# get newdata and ensure same ordering in train and predict
newdata = as.matrix(ordered_features(task, self))

lp = as.numeric(invoke(predict,
self$model,
newdata = newdata,
.args = pars,
type = "lp"))
lp = as.numeric(invoke(predict,
self$model,
newdata = newdata,
.args = pars,
type = "lp"))

surv = invoke(predict,
self$model,
newdata = newdata,
.args = pars,
type = "risk",
times = sort(unique(self$model$time)))
surv = invoke(predict,
self$model,
newdata = newdata,
.args = pars,
type = "risk",
times = sort(unique(self$model$time)))

mlr3proba::.surv_return(times = sort(unique(self$model$time)),
surv = surv,
lp = lp)
}
)
mlr3proba::.surv_return(times = sort(unique(self$model$time)),
surv = surv,
lp = lp)
}
)
)

.extralrns_dict$add("surv.coxboost", function() LearnerSurvCoxboost$new())
.extralrns_dict$add("surv.coxboost", LearnerSurvCoxboost)
Loading

0 comments on commit 47f8cda

Please sign in to comment.