Skip to content

Commit

Permalink
Fix verbose meassages
Browse files Browse the repository at this point in the history
  • Loading branch information
fouodo committed Nov 28, 2024
1 parent e371b74 commit a114d59
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 299 deletions.
21 changes: 16 additions & 5 deletions R/Training.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,16 @@ Training <- R6Class("Training",
#' Subset of individuals IDs to be used for training.
#' @param use_var_sel `boolean` \cr
#' If TRUE, selected variables available at each layer are used.
#' @param verbose `boolean` \cr
#' Warning messages will be displayed if set to TRUE.
#' @return
#' Returns the object itself, with a model for each layer.
#' @export
#'
#'
trainLayer = function (ind_subset = NULL,
use_var_sel = FALSE) {
use_var_sel = FALSE,
verbose = TRUE) {
layers = self$getKeyClass()
nb_layers = nrow(layers[layers$class %in% "TrainLayer", ])
if (nb_layers) {
Expand All @@ -162,7 +165,8 @@ Training <- R6Class("Training",
for (k in layers$key) {
layer = self$getFromHashTable(key = k)
layer$train(ind_subset = ind_subset,
use_var_sel = use_var_sel)
use_var_sel = use_var_sel,
verbose = verbose)
}
} else {
stop("No existing layer in the current training object.")
Expand Down Expand Up @@ -233,16 +237,21 @@ Training <- R6Class("Training",
if (!is.list(resampling)) {
stop("The resampling method must return a list of folds, with each fold containing a vector of training IDs.\n See example for details.")
} else {
if (verbose) {
message("Creating fold predictions.\n")
pb <- txtProgressBar(min = 0, max = length(resampling), style = 3)
}
train_layer_res_list = lapply(X = 1:length(resampling),
function (fold) {
if (verbose) {
cat(sprintf("Training for fold %s.\n", fold))
setTxtProgressBar(pb = pb, fold)
}
test_index = resampling[[fold]]
train_index = setdiff(unlist(resampling), test_index)
train_ids = self$getTargetValues()[train_index, 1L]
self$trainLayer(ind_subset = train_ids,
use_var_sel = use_var_sel)
use_var_sel = use_var_sel,
verbose = FALSE)
test_ids = self$getTargetValues()[test_index, 1L]
# Note: The current object is not a TestStudy, but a Training object.
predicting = self$predictLayer(testing = self,
Expand Down Expand Up @@ -339,9 +348,11 @@ Training <- R6Class("Training",
resampling_arg,
use_var_sel = use_var_sel,
impute = private$impute)
cat("\n")
# 2) Train each layer
self$trainLayer(ind_subset = ind_subset,
use_var_sel = use_var_sel)
use_var_sel = use_var_sel,
verbose = self$getVerbose())
# 3) Train the meta layer
# Add layer specific predictions to meta training layer
layers = self$getKeyClass()
Expand Down
2 changes: 1 addition & 1 deletion R/bestLayerLearner.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title The best layer-specific model is used as meta model.
#'
#' @description
#' The meta learner is the best layer-specific learrner.
#' The meta learner is the best layer-specific learner. This function is intended to be (internally) used as meta-learner in fuseMLR.
#'
#' @param x `data.frame` \cr
#' \code{data.frame} of predictors.
Expand Down
7 changes: 4 additions & 3 deletions R/weightedMeanLearner.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title The weighted mean meta-learner
#'
#' @description
#' Modality-specific learner are assessed and weighted based on their predictions.
#' Modality-specific learner are assessed and weighted based on their predictions. This function is intended to be (internally) used as meta-learner in fuseMLR.
#'
#' @param x `data.frame` \cr
#' Modality-specific predictions.
Expand All @@ -14,12 +14,13 @@
#' Function to compute layer-specific performance of learners. If NULL, the Brier Score (classification) or a mean squared error (regression) is used by default as performance measure.
#' Otherwise, the performance function must accept two parameters: \code{observed} (observed values) and \code{predicted} (predicted values).
#' @return
#' A model object of class \code{weightedMeanLearner}.
#' Object of class \code{weightedMeanLearner} with the vector of estimated weights pro layer.\cr
#'
#' @export
#' @examples
#' set.seed(20240624L)
#' x = data.frame(x1 = runif(n = 50L, min = 0, max = 1))
#' x = data.frame(x1 = runif(n = 50L, min = 0, max = 1),
#' x2 = runif(n = 50L, min = 0, max = 1))
#' y = sample(x = 0L:1L, size = 50L, replace = TRUE)
#' my_model = weightedMeanLearner(x = x, y = y)
#'
Expand Down
9 changes: 9 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
title: "fuseMLR"
author: Cesaire J. K. Fouodo
output:
md_document:
variant: gfm
preserve_yaml: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Expand Down
Loading

0 comments on commit a114d59

Please sign in to comment.