Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
4db7cfc
first upload on HiwiBranch
awinterstetter Jun 26, 2025
4f4716c
27.06.2025 - 16:35
awinterstetter Jun 27, 2025
b5eb4dd
before logger change
awinterstetter Jun 28, 2025
c585791
code aufgeräumt
awinterstetter Jun 29, 2025
e237d8b
private.output implemented
awinterstetter Jun 29, 2025
d4daed6
push before changing train function
awinterstetter Jun 29, 2025
2e7613e
updates in the night
awinterstetter Jun 29, 2025
0289424
final changes sunday
awinterstetter Jun 29, 2025
42f826d
morning changes
awinterstetter Jun 30, 2025
5bd71ce
Änderungen nach Meeting mit Martin
awinterstetter Jun 30, 2025
e581ace
test file 1st upload
awinterstetter Jul 4, 2025
1e03403
Intendation fixed, invisible print, string at the top of each console…
awinterstetter Jul 4, 2025
66f53a8
prediction object print implementiert
awinterstetter Jul 4, 2025
582f2ac
first tests implemented
awinterstetter Jul 4, 2025
22f4b77
output test translated into a loop
awinterstetter Jul 6, 2025
c3e8ca1
collect_multiplicity test implemented
awinterstetter Jul 6, 2025
eee3071
malformed log target test implemented
awinterstetter Jul 6, 2025
d01a2fa
collect_multiplicity Test wurde modifiziert
awinterstetter Jul 6, 2025
554ea1a
changes after meeting with Martin (7.7.)
awinterstetter Jul 7, 2025
e229245
small changes
awinterstetter Jul 7, 2025
faf68a8
changes after meeting
awinterstetter Jul 7, 2025
bd64c52
self§state implemented; test input vs output implemented
awinterstetter Jul 10, 2025
bfadd92
changes Friday Morning
awinterstetter Jul 11, 2025
5e6fbcb
devtools::load_all() fix
awinterstetter Jul 11, 2025
d06ec8f
nach ewig langem rumfixen, geht jetzt devtools::test(...)
awinterstetter Jul 11, 2025
e1692f9
Saturday updates
awinterstetter Jul 12, 2025
dafd8a6
ignire
awinterstetter Jul 13, 2025
8c36fc7
regex test "$data" for task implemented
awinterstetter Jul 13, 2025
0e48999
regex test implemented
awinterstetter Jul 14, 2025
9595990
changes after meeting mit Martin (14.07.2025)
awinterstetter Jul 14, 2025
79e3def
PipeOp Info tests impemented
awinterstetter Jul 18, 2025
18a38fb
expect_printer_output fixed (no more capture_warnings etc.)
awinterstetter Aug 8, 2025
35d3749
pipeopinfo test modified
awinterstetter Aug 9, 2025
e8e6495
PipeOpIsomap changes
awinterstetter Aug 10, 2025
df15662
PipeOp Isomap Predictions vergleichbar mit S4 Algorithmus
awinterstetter Aug 11, 2025
78ecd59
PipeOPIsomap working similar on iris data
awinterstetter Aug 13, 2025
8bfd2fa
tag = "debug" added
awinterstetter Aug 13, 2025
dbcd05b
tag = "debug" added at the appropriate spot
awinterstetter Aug 13, 2025
5d69ec6
c() replaced by list() at multiple instances
awinterstetter Aug 13, 2025
bddb834
capture_warning/capture_messages bei test_that("pattern check") erset…
awinterstetter Aug 13, 2025
85b8de9
redundanten Part entfernt
awinterstetter Aug 13, 2025
3f6034e
tag = "debug" removed, so that tests have no warning
awinterstetter Aug 13, 2025
a6f0d51
test for collect_multiplicity refined
awinterstetter Aug 13, 2025
16e62c1
implemented predict-test in "regex pattern" test and standardized var…
awinterstetter Aug 13, 2025
550bbfd
test update on github
awinterstetter Aug 14, 2025
4b6dc5b
undo test
awinterstetter Aug 14, 2025
ec76f78
MacBook next try
awinterstetter Aug 14, 2025
489bac7
test
awinterstetter Aug 14, 2025
6e3f109
test different PAT
awinterstetter Aug 14, 2025
fda57c0
changes in the documentation
awinterstetter Aug 14, 2025
2a7c707
changes in train and predict function
awinterstetter Aug 15, 2025
3250bf8
test code in attic
awinterstetter Aug 15, 2025
6a89a04
minor changes regarding hyperparameters
awinterstetter Aug 15, 2025
dbd387a
changes in comments and test documentation
awinterstetter Aug 17, 2025
85a5dba
changes in default parameters
awinterstetter Aug 17, 2025
59d46d7
test code changes
awinterstetter Aug 17, 2025
5cb4622
minor changes in "pattern-check"-test
awinterstetter Aug 18, 2025
76189db
pipeop isomap reworked and pipe_op_info test cleaned
awinterstetter Aug 18, 2025
3ca9089
test "original printer overwritten" is fixed
awinterstetter Aug 18, 2025
cea1990
removed test code file
awinterstetter Aug 18, 2025
aa11906
pipeopisomap added in description
awinterstetter Aug 19, 2025
5a090db
skip if not installed rpart
awinterstetter Aug 19, 2025
77c8105
changes in description and global definition for predict and capture.…
awinterstetter Aug 19, 2025
1beeb22
backports replaced by utils
awinterstetter Aug 19, 2025
e8d4ea7
backports completely replaced by utils
awinterstetter Aug 19, 2025
52724b0
first tests implemented for isomap
awinterstetter Aug 20, 2025
333dcaa
devtools::document() ran in repo folder
awinterstetter Aug 21, 2025
f0fa24b
push after devtools::document()
awinterstetter Aug 21, 2025
42a3102
test for isomap implemented
awinterstetter Aug 21, 2025
7134076
update (pls work validity checks)
awinterstetter Aug 21, 2025
90a7795
fixing predict error in isomap
awinterstetter Aug 22, 2025
5f6ee94
removed default = NULL
awinterstetter Aug 22, 2025
ef2330a
@method predict Graph added in Graph.R
awinterstetter Aug 22, 2025
a274e81
added RANN and RSpectra to suggests in DESCRIPTION
awinterstetter Aug 22, 2025
809bf6e
changes on documentation for PipeOpInfo
awinterstetter Aug 25, 2025
0058a7a
slight changes to pipeopinfo
awinterstetter Aug 25, 2025
014698d
Fixes for hyperparameters get_geod and .mute
awinterstetter Aug 25, 2025
cdb7b60
fix for test
awinterstetter Aug 25, 2025
05a6b29
added isomap tests
awinterstetter Aug 26, 2025
08f70e3
minor changes in tests isomap
awinterstetter Aug 26, 2025
d2542ff
changes in HP so that they refer correctly to the set default
awinterstetter Aug 27, 2025
5643285
changes after meeting with Martin (PipeOpIsomap has dimRed::predict, …
awinterstetter Aug 27, 2025
120eecd
comment out test for mute
awinterstetter Aug 27, 2025
233b1ee
change in feature types in super$initialize
awinterstetter Aug 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 17 additions & 65 deletions R/PipeOpIsomap.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,85 +45,37 @@ PipeOpIsomap = R6Class("PipeOpIsomap",
public = list(
initialize = function(id = "isomap", param_vals = list(), get_geod = FALSE, keep_org_data = TRUE, diag = FALSE) {
ps = ps(
k = p_int(default = 50, lower = 1, upper = Inf, tags = "train"), # tag isomap?
knn = p_int(default = 50, lower = 1, upper = Inf, tags = "train"), # tag isomap?
ndim = p_int(default = 2, lower = 1, upper = Inf, tags = "train"), #tag isomap?
eps = p_dbl(default = 0, tags = "train")) # tag isomap?
ps$values = list(k = 50, ndim = 2, eps = 0)
get_geod = p_lgl(default = FALSE, tags = "train"),
.mute = p_uty(default = NULL, tags = "train")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having 'NULL' default can make problems some times, it's probably best to just not set a default here.

)
ps$values = list(knn = 50, ndim = 2)
super$initialize(id = id, param_set = ps, param_vals = param_vals, feature_types = c("numeric", "integer"))
private$.get_geod = get_geod
private$.keep_org_data = keep_org_data
private$.diag = diag
}
),
active = list(),
private = list(
.get_geod = NULL,
.keep_org_data = NULL,
.diag = NULL,
.make_knn_graph = function(x) {
pv = self$param_set$get_values(tags = "train")
INF_VAL = 1.340781e+15
NA_IDX = 0
## select parameters
M = nrow(x)
if (pv$eps == 0) searchtype = "standard" else searchtype = "priority"
## RANN::nn2 returns the points in data with respect to query
## e.g. the rows in the output are the points in query and the
## columns the points in data.
nn2res = RANN::nn2(data = x, query = x, k = pv$k + 1, treetype = "kd",
searchtype = searchtype, eps = pv$eps)
## create graph: the first ny nodes will be y, the last nx nodes
## will be x, if x != y
g = igraph::make_empty_graph(M, directed = TRUE)
g[from = if (private$.diag) rep(seq_len(M), times = pv$k + 1)
else rep(seq_len(M), times = pv$k),
to = if (private$.diag) as.vector(nn2res$nn.idx)
else as.vector(nn2res$nn.idx[, -1]),
attr = "weight"] =
if (private$.diag) as.vector(nn2res$nn.dists)
else as.vector(nn2res$nn.dists[, -1])
igraph::as_undirected(g, mode = "collapse", edge.attr.comb = "first")
},
.train_dt = function(dt, levels, target) {
#browser()
pv = self$param_set$get_values(tags = "train")
knn_graph = private$.make_knn_graph(dt)
geodist = igraph::distances(knn_graph, algorithm = "dijkstra")
k = geodist ^ 2
k = .Call(stats:::C_DoubleCentre, k)
k = - k / 2
## TODO: explicit symmetrizing
## TODO: return eigenvectors?
e = RSpectra::eigs_sym(k, pv$ndim, which = "LA", opts = list(retvec = TRUE))
e_values = e$values
e_vectors = e$vectors
neig = sum(e_values > 0)
if (neig < pv$ndim) {
warning("Isomap: eigenvalues < 0, returning less dimensions!")
e_values = e_values[seq_len(neig)]
e_vectors = e_vectors[, seq_len(neig), drop = FALSE]
}
e_vectors = e_vectors * rep(sqrt(e_values), each = nrow(e_vectors))
colnames(e_vectors) = paste("iso", seq_len(neig))
self$state = list(geodist = geodist, e_vectors = e_vectors, e_values = e_values, orgdata = dt, target = target)
dt
embed_result = dimRed::embed(dt, "Isomap", knn = pv$knn, ndim = pv$ndim, .mute = NULL)
self$state = list(embed_result = embed_result)
embed_result@data@data
},
.predict_dt = function(dt, levels) {
browser()
#browser()
pv = self$param_set$get_values(tags = "train")
if (ncol(self$state$orgdata) != ncol(dt))
stop("x must have the same number of dimensions as the original data")
nindata = nrow(dt)
norg = nrow(self$state$orgdata)
lknng = private$.make_knn_graph(rbind(dt, self$state$orgdata))
lgeodist = igraph::distances(lknng, seq_len(nindata), nindata + seq_len(norg))
dammu = sweep(lgeodist ^ 2, 2, colMeans(self$state$geodist ^ 2), "-")
Lsharp = sweep(self$state$e_vectors, 2, self$state$e_values, "/")
out = -0.5 * (dammu %*% Lsharp)
dt
predict(self$state$embed_result, dt)
}
)
)

mlr_pipeops$add("isomap", PipeOpIsomap)

#po = po("isomap", knn = 40, ndim = 2)
#po$train(list(tsk("iris")))
#po$predict(list(tsk("iris")))

#samp <- sample(nrow(dat), size = 70)
#emb2 <- embed(dat[samp,], "Isomap", .mute = NULL, knn = 30)
#emb3 <- predict(emb2, dat[-samp,])
49 changes: 26 additions & 23 deletions tests/testthat/test_pipeop_info.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
context("PipeOpInfo")


##### pipeop$new ersetzen mit po("")


test_that("basic properties", {
po = PipeOpInfo$new("info")
po = po("info")
expect_pipeop(po)
expect_pipeop_class(PipeOpInfo, list(id = "info"))
})
Expand All @@ -17,7 +21,7 @@ test_that("check whether input and output are equal", {
output = list("lgr::mlr3/mlr3pipelines::info", "cat", "warning", "message", "none")
for (j in input) {
for (i in seq_along(output)) {
poinfo = PipeOpInfo$new(id = "info", log_target = output[[i]])
poinfo = po("info", log_target = output[[i]])
suppressMessages(suppressWarnings(invisible(capture.output(expect_identical(poinfo$train(list(j))[[1]], j)))))
suppressMessages(suppressWarnings(invisible(capture.output(expect_identical(poinfo$predict(list(j))[[1]], j)))))
}
Expand All @@ -39,7 +43,7 @@ test_that("console output type depending on log_target is correct", {
expect_func = list(expect_output, expect_output, expect_warning, expect_message, expect_silent)
for (j in input) {
for (i in seq_along(output)) {
poinfo = PipeOpInfo$new(id = "info", log_target = output[[i]])
poinfo = po("info", log_target = output[[i]])
expect_func[[i]](poinfo$train(list(j)))
expect_func[[i]](poinfo$predict(list(j)))
}
Expand All @@ -58,7 +62,7 @@ test_that("logger is addressed when log_target is set to a logger", {
prediction = lrn_rpart$predict_newdata(mtcars)
prediction_new = lrn_rpart$predict_newdata(mtcars_new)
# Actual Test
poinfo = PipeOpInfo$new(id = "info", log_target = "lgr::debug_logger::info")
poinfo = po("info", log_target = "lgr::debug_logger::info")
input = list(tsk("iris"), prediction, prediction_new, NULL, "default_string")
for (j in input) {
poinfo$train(list(j))
Expand All @@ -85,15 +89,15 @@ test_that("PipeOp recognizes class of input objects and prints information accor
regex_list = list("\\$task.*\\$data", "\\$prediction.*\\$score", "\\$prediction", "NULL", "default_string")
for (j in seq_along(input)) {
for (i in seq_along(output)) {
poinfo = PipeOpInfo$new(id = "info", log_target = output[[i]])
poinfo = po("info", log_target = output[[i]])
console_output_train = tryCatch(capture.output(poinfo$train(list(input[[j]]))),
warning = function(w) {as.character(conditionMessage(w))},
message = function(m) {as.character(conditionMessage(m))})
warning = function(w) as.character(conditionMessage(w)),
message = function(m) as.character(conditionMessage(m)))
expect_match(paste0(console_output_train, collapse = ""), regex_list[[j]], all = FALSE)
suppressMessages(suppressWarnings(capture.output(poinfo$train(list(input[[j]])))))
console_output_predict = tryCatch(capture.output(poinfo$predict(list(input[[j]]))),
warning = function(w) {as.character(conditionMessage(w))},
message = function(m) {as.character(conditionMessage(m))})
warning = function(w) as.character(conditionMessage(w)),
message = function(m) as.character(conditionMessage(m)))
expect_match(paste0(console_output_predict, collapse = ""), regex_list[[j]], all = FALSE)
}
}
Expand All @@ -102,7 +106,7 @@ test_that("PipeOp recognizes class of input objects and prints information accor
test_that("malformed log_target handled accordingly", {
malformed_log_target = list("malformed", "::", "::::::", "log::", "log::log_level", "log::log_level::", "log::log_level::message::", "::log")
for (i in seq_along(malformed_log_target)) {
expect_error(PipeOpInfo$new("info", log_target = malformed_log_target[[i]]))
expect_error(po("info", log_target = malformed_log_target[[i]]))
}
})

Expand All @@ -118,16 +122,15 @@ test_that("original printer can be overwritten", {
regex_list = list("azbycxdw", "azbycxdwev", "azbycxdwevfu", "azbycxdwevfugt")
for (j in seq_along(input)) {
for (i in seq_along(output)) {
browser()
poinfo = PipeOpInfo$new(id = "info", log_target = output[[i]],
printer = list(Task = function(x) {"azbycxdw"},
Prediction = function(x) {"azbycxdwev"},
`NULL` = function(x) {"azbycxdwevfu"},
default= function(x) {"azbycxdwevfugt"}
poinfo = po("info", log_target = output[[i]],
printer = list(Task = function(x) "azbycxdw",
Prediction = function(x) "azbycxdwev",
`NULL` = function(x) "azbycxdwevfu",
default = function(x) "azbycxdwevfugt"
))
console_output_train = tryCatch(capture.output(poinfo$train(list(input[[i]]))),
warning = function(w) {conditionMessage(w)},
message = function(m) {conditionMessage(m)})
warning = function(w) conditionMessage(w),
message = function(m) conditionMessage(m))
expect_match(console_output_train, regex_list[[j]], all = FALSE)
console_output_predict = tryCatch({
capture.output(poinfo$train(list(input[[i]])))
Expand All @@ -150,15 +153,15 @@ test_that("handling of multiplicity objects controlled by field collect_multipli
input = list(OVR, list(OVR))
for (i in seq_along(output)) {
for (j in seq_along(collect_multiplicity)) {
poinfo = PipeOpInfo$new(id = "info", collect_multiplicity = collect_multiplicity[[j]], log_target = output[[i]], printer = list(default = function(x) "abc", Multiplicity = function(x) "xyz"))
poinfo = po("info", collect_multiplicity = collect_multiplicity[[j]], log_target = output[[i]], printer = list(default = function(x) "abc", Multiplicity = function(x) "xyz"))
console_output_train = tryCatch(capture.output(poinfo$train(input[[j]])),
warning = function(w) {conditionMessage(w)},
message = function(m) {conditionMessage(m)})
warning = function(w) conditionMessage(w),
message = function(m) conditionMessage(m))
expect_match(paste0(console_output_train, collapse = ""), test_string[[j]], all = FALSE)
suppressMessages(suppressWarnings(capture.output(poinfo$train(input[[j]]))))
console_output_predict = tryCatch(capture.output(poinfo$predict(input[[j]])),
warning = function(w) {conditionMessage(w)},
message = function(m) {conditionMessage(m)})
warning = function(w) conditionMessage(w),
message = function(m) conditionMessage(m))
expect_match(paste0(console_output_predict, collapse = ""), test_string[[j]], all = FALSE)
}
}
Expand Down
Loading