-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use register funtions for loading and unloading (#84)
- Loading branch information
Showing
31 changed files
with
102 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
library(testthat) | ||
library(checkmate) | ||
library(mlr3) | ||
library(mlr3cluster) | ||
library(checkmate) | ||
library(testthat) |