Debian Codegraph @

tests/testthat/test-cut_lower_fun.R

raw
1 # library(testthat)
2
3 context("cut_lower_fun works")
4
5 # library(dendextend)
6 # library(dendextendRcpp)
7
8 test_that("cut_lower_fun works", {
9 dend <- datasets::iris[1:4, -5] %>%
10 dist() %>%
11 hclust() %>%
12 as.dendrogram()
13 # this is really cool!
14 expect_identical(
15 cut_lower_fun(dend, .4, labels),
16 lapply(cut(dend, h = .4)$lower, labels)
17 )
18
19 expect_identical(
20 cut_lower_fun(dend, 0, labels),
21 lapply(cut(dend, h = 0)$lower, labels)
22 )
23
24 # this is the way to test this:
25 # expect_identical(
26 # old_cut_lower_fun(dend[[1]], .4, function(x)x),
27 # list(dend[[1]])
28 # )
29
30 # function on a leaf gives what we expect
31 expect_identical(
32 cut_lower_fun(dend[[1]], .4, labels),
33 list("1")
34 )
35
36 # cut, however, returns an empty list instead of itself...
37 expect_identical(
38 cut(dend[[1]], h = .4)$lower,
39 list()
40 )
41
42
43
44
45 # library(microbenchmark)
46 # microbenchmark(
47 # cut_lower_fun(dend, .4, order.dendrogram),
48 # lapply(cut(dend, h = .4)$lower, order.dendrogram)
49 # )
50 # # Rcpp is 3 times faster...
51 #
52
53
54 # dend_big = as.dendrogram(hclust(dist(iris[1:150,-5])))
55 # microbenchmark(
56 # cut_lower_fun(dend_big, .4, order.dendrogram),
57 # lapply(cut(dend_big, h = .4)$lower, order.dendrogram)
58 # )
59 # Rcpp is 16 times faster...
60
61 # if non dendrogram object is passed in
62 hc <- datasets::iris[1:4, -5] %>%
63 dist() %>%
64 hclust()
65 expect_error(
66 cut_lower_fun(hc)
67 )
68
69 })
70
71
72
73
74
75 test_that("cut_lower_fun in dendextend", {
76 dend <- as.dendrogram(hclust(dist(c(1, 1, 1, 2, 2))))
77 expect_identical(
78 cut_lower_fun(dend, -.5, labels),
79 list()
80 )
81 # But this will NOT be the same with dendextendRcpp !!
82 })