diff --git a/DESCRIPTION b/DESCRIPTION index 4a017a68..d3f912d8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,7 +45,7 @@ Roxygen: list(markdown = TRUE, r6 = TRUE) RoxygenNote: 7.3.2 Collate: 'LearnerClust.R' - 'aaa.R' + 'zzz.R' 'LearnerClustAffinityPropagation.R' 'LearnerClustAgnes.R' 'LearnerClustBICO.R' @@ -82,4 +82,3 @@ Collate: 'as_task_clust.R' 'bibentries.R' 'helper.R' - 'zzz.R' diff --git a/R/LearnerClustAffinityPropagation.R b/R/LearnerClustAffinityPropagation.R index f0afd1c2..a6ed9013 100644 --- a/R/LearnerClustAffinityPropagation.R +++ b/R/LearnerClustAffinityPropagation.R @@ -81,5 +81,5 @@ LearnerClustAP = R6Class("LearnerClustAP", ) ) -#' @include aaa.R -learners[["clust.ap"]] = LearnerClustAP +#' @include zzz.R +register_learner("clust.ap", LearnerClustAP) diff --git a/R/LearnerClustAgnes.R b/R/LearnerClustAgnes.R index 5cdebea3..74643b0e 100644 --- a/R/LearnerClustAgnes.R +++ b/R/LearnerClustAgnes.R @@ -90,5 +90,5 @@ LearnerClustAgnes = R6Class("LearnerClustAgnes", ) ) -#' @include aaa.R -learners[["clust.agnes"]] = LearnerClustAgnes +#' @include zzz.R +register_learner("clust.agnes", LearnerClustAgnes) diff --git a/R/LearnerClustBICO.R b/R/LearnerClustBICO.R index 3f164d94..ed6318a0 100644 --- a/R/LearnerClustBICO.R +++ b/R/LearnerClustBICO.R @@ -61,5 +61,5 @@ LearnerClustBICO = R6Class("LearnerClustBICO", ) ) -#' @include aaa.R -learners[["clust.bico"]] = LearnerClustBICO +#' @include zzz.R +register_learner("clust.bico", LearnerClustBICO) diff --git a/R/LearnerClustBIRCH.R b/R/LearnerClustBIRCH.R index f42fee64..8fcdbbd1 100644 --- a/R/LearnerClustBIRCH.R +++ b/R/LearnerClustBIRCH.R @@ -62,5 +62,5 @@ LearnerClustBIRCH = R6Class("LearnerClustBIRCH", ) ) -#' @include aaa.R -learners[["clust.birch"]] = LearnerClustBIRCH +#' @include zzz.R +register_learner("clust.birch", LearnerClustBIRCH) diff --git a/R/LearnerClustCMeans.R b/R/LearnerClustCMeans.R index 763e77f6..dcb6d817 100644 --- a/R/LearnerClustCMeans.R +++ b/R/LearnerClustCMeans.R @@ -80,5 +80,5 @@ LearnerClustCMeans = R6Class("LearnerClustCMeans", ) ) -#' @include aaa.R -learners[["clust.cmeans"]] = LearnerClustCMeans +#' @include zzz.R +register_learner("clust.cmeans", LearnerClustCMeans) diff --git a/R/LearnerClustCobweb.R b/R/LearnerClustCobweb.R index 8d0ed0fd..81fefed6 100644 --- a/R/LearnerClustCobweb.R +++ b/R/LearnerClustCobweb.R @@ -58,5 +58,5 @@ LearnerClustCobweb = R6Class("LearnerClustCobweb", ) ) -#' @include aaa.R -learners[["clust.cobweb"]] = LearnerClustCobweb +#' @include zzz.R +register_learner("clust.cobweb", LearnerClustCobweb) diff --git a/R/LearnerClustDBSCAN.R b/R/LearnerClustDBSCAN.R index 7386485b..af360995 100644 --- a/R/LearnerClustDBSCAN.R +++ b/R/LearnerClustDBSCAN.R @@ -67,5 +67,5 @@ LearnerClustDBSCAN = R6Class("LearnerClustDBSCAN", ) ) -#' @include aaa.R -learners[["clust.dbscan"]] = LearnerClustDBSCAN +#' @include zzz.R +register_learner("clust.dbscan", LearnerClustDBSCAN) diff --git a/R/LearnerClustDBSCANfpc.R b/R/LearnerClustDBSCANfpc.R index 958acb5a..8f730a97 100644 --- a/R/LearnerClustDBSCANfpc.R +++ b/R/LearnerClustDBSCANfpc.R @@ -77,5 +77,5 @@ LearnerClustDBSCANfpc = R6Class("LearnerClustDBSCANfpc", ) ) -#' @include aaa.R -learners[["clust.dbscan_fpc"]] = LearnerClustDBSCANfpc +#' @include zzz.R +register_learner("clust.dbscan_fpc", LearnerClustDBSCANfpc) diff --git a/R/LearnerClustDiana.R b/R/LearnerClustDiana.R index 235b7105..859687b3 100644 --- a/R/LearnerClustDiana.R +++ b/R/LearnerClustDiana.R @@ -71,5 +71,5 @@ LearnerClustDiana = R6Class("LearnerClustDiana", ) ) -#' @include aaa.R -learners[["clust.diana"]] = LearnerClustDiana +#' @include zzz.R +register_learner("clust.diana", LearnerClustDiana) diff --git a/R/LearnerClustEM.R b/R/LearnerClustEM.R index d99cb105..858327ce 100644 --- a/R/LearnerClustEM.R +++ b/R/LearnerClustEM.R @@ -69,5 +69,5 @@ LearnerClustEM = R6Class("LearnerClustEM", ) ) -#' @include aaa.R -learners[["clust.em"]] = LearnerClustEM +#' @include zzz.R +register_learner("clust.em", LearnerClustEM) diff --git a/R/LearnerClustFanny.R b/R/LearnerClustFanny.R index 3d8dd1a7..b9135091 100644 --- a/R/LearnerClustFanny.R +++ b/R/LearnerClustFanny.R @@ -73,5 +73,5 @@ LearnerClustFanny = R6Class("LearnerClustFanny", ) ) -#' @include aaa.R -learners[["clust.fanny"]] = LearnerClustFanny +#' @include zzz.R +register_learner("clust.fanny", LearnerClustFanny) diff --git a/R/LearnerClustFarthestFirst.R b/R/LearnerClustFarthestFirst.R index 8dfc8d38..f9eb5243 100644 --- a/R/LearnerClustFarthestFirst.R +++ b/R/LearnerClustFarthestFirst.R @@ -59,5 +59,5 @@ LearnerClustFarthestFirst = R6Class("LearnerClustFF", ) ) -#' @include aaa.R -learners[["clust.ff"]] = LearnerClustFarthestFirst +#' @include zzz.R +register_learner("clust.ff", LearnerClustFarthestFirst) diff --git a/R/LearnerClustFeatureless.R b/R/LearnerClustFeatureless.R index 2c2c8b26..1b8321c0 100644 --- a/R/LearnerClustFeatureless.R +++ b/R/LearnerClustFeatureless.R @@ -80,5 +80,5 @@ LearnerClustFeatureless = R6Class("LearnerClustFeatureless", ) ) -#' @include aaa.R -learners[["clust.featureless"]] = LearnerClustFeatureless +#' @include zzz.R +register_learner("clust.featureless", LearnerClustFeatureless) diff --git a/R/LearnerClustHDBSCAN.R b/R/LearnerClustHDBSCAN.R index 37645301..447a534c 100644 --- a/R/LearnerClustHDBSCAN.R +++ b/R/LearnerClustHDBSCAN.R @@ -59,5 +59,5 @@ LearnerClustHDBSCAN = R6Class("LearnerClustHDBSCAN", ) ) -#' @include aaa.R -learners[["clust.hdbscan"]] = LearnerClustHDBSCAN +#' @include zzz.R +register_learner("clust.hdbscan", LearnerClustHDBSCAN) diff --git a/R/LearnerClustHclust.R b/R/LearnerClustHclust.R index 482b64ea..e5204d50 100644 --- a/R/LearnerClustHclust.R +++ b/R/LearnerClustHclust.R @@ -83,5 +83,5 @@ LearnerClustHclust = R6Class("LearnerClustHclust", ) ) -#' @include aaa.R -learners[["clust.hclust"]] = LearnerClustHclust +#' @include zzz.R +register_learner("clust.hclust", LearnerClustHclust) diff --git a/R/LearnerClustKKMeans.R b/R/LearnerClustKKMeans.R index 12f894c2..0e682a32 100644 --- a/R/LearnerClustKKMeans.R +++ b/R/LearnerClustKKMeans.R @@ -100,5 +100,5 @@ LearnerClustKKMeans = R6Class("LearnerClustKKMeans", ) ) -#' @include aaa.R -learners[["clust.kkmeans"]] = LearnerClustKKMeans +#' @include zzz.R +register_learner("clust.kkmeans", LearnerClustKKMeans) diff --git a/R/LearnerClustKMeans.R b/R/LearnerClustKMeans.R index 7147cbb3..2d152cfc 100644 --- a/R/LearnerClustKMeans.R +++ b/R/LearnerClustKMeans.R @@ -74,5 +74,5 @@ LearnerClustKMeans = R6Class("LearnerClustKMeans", ) ) -#' @include aaa.R -learners[["clust.kmeans"]] = LearnerClustKMeans +#' @include zzz.R +register_learner("clust.kmeans", LearnerClustKMeans) diff --git a/R/LearnerClustMclust.R b/R/LearnerClustMclust.R index dac8cd31..89720712 100644 --- a/R/LearnerClustMclust.R +++ b/R/LearnerClustMclust.R @@ -64,5 +64,5 @@ LearnerClustMclust = R6Class("LearnerClustMclust", ) ) -#' @include aaa.R -learners[["clust.mclust"]] = LearnerClustMclust +#' @include zzz.R +register_learner("clust.mclust", LearnerClustMclust) diff --git a/R/LearnerClustMeanShift.R b/R/LearnerClustMeanShift.R index ea158b25..d3ad6147 100644 --- a/R/LearnerClustMeanShift.R +++ b/R/LearnerClustMeanShift.R @@ -70,5 +70,5 @@ LearnerClustMeanShift = R6Class("LearnerClustMeanShift", ) ) -#' @include aaa.R -learners[["clust.meanshift"]] = LearnerClustMeanShift +#' @include zzz.R +register_learner("clust.meanshift", LearnerClustMeanShift) diff --git a/R/LearnerClustMiniBatchKMeans.R b/R/LearnerClustMiniBatchKMeans.R index d6a9178a..789edc0c 100644 --- a/R/LearnerClustMiniBatchKMeans.R +++ b/R/LearnerClustMiniBatchKMeans.R @@ -85,5 +85,5 @@ LearnerClustMiniBatchKMeans = R6Class("LearnerClustMiniBatchKMeans", ) ) -#' @include aaa.R -learners[["clust.MBatchKMeans"]] = LearnerClustMiniBatchKMeans +#' @include zzz.R +register_learner("clust.MBatchKMeans", LearnerClustMiniBatchKMeans) diff --git a/R/LearnerClustOPTICS.R b/R/LearnerClustOPTICS.R index e07dda2d..a91ac884 100644 --- a/R/LearnerClustOPTICS.R +++ b/R/LearnerClustOPTICS.R @@ -68,5 +68,5 @@ LearnerClustOPTICS = R6Class("LearnerClustOPTICS", ) ) -#' @include aaa.R -learners[["clust.optics"]] = LearnerClustOPTICS +#' @include zzz.R +register_learner("clust.optics", LearnerClustOPTICS) diff --git a/R/LearnerClustPAM.R b/R/LearnerClustPAM.R index f1f01bfe..19d521c2 100644 --- a/R/LearnerClustPAM.R +++ b/R/LearnerClustPAM.R @@ -78,5 +78,5 @@ LearnerClustPAM = R6Class("LearnerClustPAM", ) ) -#' @include aaa.R -learners[["clust.pam"]] = LearnerClustPAM +#' @include zzz.R +register_learner("clust.pam", LearnerClustPAM) diff --git a/R/LearnerClustSimpleKMeans.R b/R/LearnerClustSimpleKMeans.R index 6c82ab7e..217bbdcf 100644 --- a/R/LearnerClustSimpleKMeans.R +++ b/R/LearnerClustSimpleKMeans.R @@ -73,5 +73,5 @@ LearnerClustSimpleKMeans = R6Class("LearnerClustSimpleKMeans", ) ) -#' @include aaa.R -learners[["clust.SimpleKMeans"]] = LearnerClustSimpleKMeans +#' @include zzz.R +register_learner("clust.SimpleKMeans", LearnerClustSimpleKMeans) diff --git a/R/LearnerClustXMeans.R b/R/LearnerClustXMeans.R index 4f64f826..31f0416c 100644 --- a/R/LearnerClustXMeans.R +++ b/R/LearnerClustXMeans.R @@ -72,5 +72,5 @@ LearnerClustXMeans = R6Class("LearnerClustXMeans", ) ) -#' @include aaa.R -learners[["clust.xmeans"]] = LearnerClustXMeans +#' @include zzz.R +register_learner("clust.xmeans", LearnerClustXMeans) diff --git a/R/TaskClust_ruspini.R b/R/TaskClust_ruspini.R index eff69900..e7d5daf9 100644 --- a/R/TaskClust_ruspini.R +++ b/R/TaskClust_ruspini.R @@ -22,5 +22,5 @@ load_task_ruspini = function(id = "ruspini") { task } -#' @include aaa.R -tasks[["ruspini"]] = load_task_ruspini +#' @include zzz.R +register_task("ruspini", load_task_ruspini) diff --git a/R/TaskClust_usarrest.R b/R/TaskClust_usarrest.R index 48aa5eb3..c2b83815 100644 --- a/R/TaskClust_usarrest.R +++ b/R/TaskClust_usarrest.R @@ -25,5 +25,5 @@ load_task_usarrests = function(id = "usarrests") { task } -#' @include aaa.R -tasks[["usarrests"]] = load_task_usarrests +#' @include zzz.R +register_task("usarrests", load_task_usarrests) diff --git a/R/aaa.R b/R/aaa.R deleted file mode 100644 index 1b05fa0e..00000000 --- a/R/aaa.R +++ /dev/null @@ -1,2 +0,0 @@ -learners = list() -tasks = list() diff --git a/R/as_prediction_clust.R b/R/as_prediction_clust.R index bc2ad087..c3992783 100644 --- a/R/as_prediction_clust.R +++ b/R/as_prediction_clust.R @@ -50,7 +50,7 @@ as_prediction_clust.data.frame = function(x, ...) { # nolint if (length(prob_cols)) { if (!all(startsWith(prob_cols, "prob."))) { - stopf("Table may only contain columns 'row_ids', 'partition' as well as columns prefixed with 'prob.' for class probabilities") + stopf("Table may only contain columns 'row_ids', 'partition' as well as columns prefixed with 'prob.' for class probabilities") # nolint } prob = as.matrix(x[, prob_cols, with = FALSE]) colnames(prob) = substr(colnames(prob), 6L, nchar(colnames(prob))) diff --git a/R/zzz.R b/R/zzz.R index 42c598e9..0a91339a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,70 +1,81 @@ +#' @import checkmate #' @import data.table +#' @import mlr3 #' @import mlr3misc #' @import paradox -#' @import mlr3 -#' @import checkmate #' @importFrom R6 R6Class #' @importFrom clue cl_predict -#' @importFrom fpc cluster.stats #' @importFrom cluster silhouette +#' @importFrom fpc cluster.stats #' @importFrom stats model.frame terms predict runif dist "_PACKAGE" +utils::globalVariables("type") + +mlr3cluster_tasks = new.env() +mlr3cluster_learners = new.env() + +register_task = function(name, constructor) { + if (name %in% names(mlr3cluster_tasks)) stopf("task %s registered twice", name) + mlr3cluster_tasks[[name]] = constructor +} + +register_learner = function(name, constructor) { + if (name %in% names(mlr3cluster_learners)) stopf("learner %s registered twice", name) + mlr3cluster_learners[[name]] = constructor +} + register_mlr3 = function() { # reflections - x = utils::getFromNamespace("mlr_reflections", ns = "mlr3") - - # task - x$task_types = x$task_types[!"clust"] - x$task_types = setkeyv(rbind(x$task_types, rowwise_table( + mlr_reflections = utils::getFromNamespace("mlr_reflections", ns = "mlr3") + mlr_reflections$task_types = mlr_reflections$task_types[type != "clust"] + mlr_reflections$task_types = setkeyv(rbind(mlr_reflections$task_types, rowwise_table( ~type, ~package, ~task, ~learner, ~prediction, ~prediction_data, ~measure, "clust", "mlr3cluster", "TaskClust", "LearnerClust", "PredictionClust", "PredictionDataClust", "MeasureClust" ), fill = TRUE), "type") - x$task_col_roles$clust = x$task_col_roles$regr - x$task_properties$clust = x$task_properties$regr - x$learner_properties$clust = c( + mlr_reflections$task_col_roles$clust = mlr_reflections$task_col_roles$regr + mlr_reflections$task_properties$clust = mlr_reflections$task_properties$regr + mlr_reflections$learner_properties$clust = c( "missings", "partitional", "hierarchical", "exclusive", "overlapping", "fuzzy", "complete", "partial", "density" ) - - # measure - x$measure_properties$clust = x$measure_properties$regr - - # learner - x$learner_predict_types$clust = list(partition = "partition", prob = c("partition", "prob")) - x$default_measures$clust = "clust.dunn" + mlr_reflections$learner_predict_types$clust = list(partition = "partition", prob = c("partition", "prob")) + mlr_reflections$measure_properties$clust = mlr_reflections$measure_properties$regr + mlr_reflections$default_measures$clust = "clust.dunn" # tasks - x = utils::getFromNamespace("mlr_tasks", ns = "mlr3") - x$add("usarrests", load_task_usarrests) - x$add("ruspini", load_task_ruspini) + mlr_tasks = utils::getFromNamespace("mlr_tasks", ns = "mlr3") + iwalk(as.list(mlr3cluster_tasks), function(task, id) mlr_tasks$add(id, task)) # learners - x = utils::getFromNamespace("mlr_learners", ns = "mlr3") - iwalk(learners, function(obj, nm) x$add(nm, obj)) + mlr_learners = utils::getFromNamespace("mlr_learners", ns = "mlr3") + iwalk(as.list(mlr3cluster_learners), function(learner, id) mlr_learners$add(id, learner)) # measures - x = utils::getFromNamespace("mlr_measures", ns = "mlr3") - x$add("clust.silhouette", MeasureClustSil, name = "silhouette", label = "Silhouette") - x$add("clust.dunn", MeasureClustFPC, name = "dunn", label = "Dunn") - x$add("clust.ch", MeasureClustFPC, name = "ch", label = "Calinski Harabasz") - x$add("clust.wss", MeasureClustFPC, name = "wss", label = "Within Sum of Squares") + mlr_measures = utils::getFromNamespace("mlr_measures", ns = "mlr3") + mlr_measures$add("clust.silhouette", MeasureClustSil, name = "silhouette", label = "Silhouette") + mlr_measures$add("clust.dunn", MeasureClustFPC, name = "dunn", label = "Dunn") + mlr_measures$add("clust.ch", MeasureClustFPC, name = "ch", label = "Calinski Harabasz") + mlr_measures$add("clust.wss", MeasureClustFPC, name = "wss", label = "Within Sum of Squares") } .onLoad = function(libname, pkgname) { backports::import(pkgname) - register_mlr3() + register_namespace_callback(pkgname, "mlr3", register_mlr3) } .onUnload = function(libpaths) { # nolint - mlr_learners = mlr3::mlr_learners - mlr_measures = mlr3::mlr_measures - mlr_tasks = mlr3::mlr_tasks - - walk(names(learners), function(id) mlr_learners$remove(id)) + walk(names(mlr3cluster_tasks), function(id) mlr_tasks$remove(id)) + walk(names(mlr3cluster_learners), function(id) mlr_learners$remove(id)) walk(names(measures), function(id) mlr_measures$remove(paste("clust", id, sep = "."))) - walk(names(tasks), function(id) mlr_tasks$remove(id)) + + mlr_reflections$task_types = mlr_reflections$task_types[type != "clust"] + reflections = c( + "measure_properties", "default_measures", "learner_properties", + "learner_predict_types", "task_properties", "task_col_roles" + ) + walk(reflections, function(x) mlr_reflections[[x]] = remove_named(mlr_reflections[[x]], "clust")) } leanify_package() diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 3afe5708..d2568080 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -1,4 +1,4 @@ -library(testthat) +library(checkmate) library(mlr3) library(mlr3cluster) -library(checkmate) +library(testthat)