Debian Codegraph @

tests/testthat/test-prune.R

raw
1 # library(testthat)
2 # library(dendextend)
3
4 context("prune.R")
5
6
7 test_that("prune_leaf works", {
8
9 hc <- USArrests[1:10, ] %>%
10 dist() %>%
11 hclust()
12 dend <- as.dendrogram(hc)
13
14 # if duplicate label
15 duplicated_dend <- dend
16 labels(duplicated_dend)[1] <- labels(duplicated_dend)[2]
17 expect_warning(
18 result <- prune_leaf(duplicated_dend, "Florida")
19 )
20 expect_identical(
21 result,
22 duplicated_dend
23 )
24
25 })
26
27
28 test_that("prune works for non-dendrogram objects", {
29
30 hc <- USArrests[1:10, ] %>%
31 dist() %>%
32 hclust()
33 dend <- as.dendrogram(hc)
34 ph <- ape::as.phylo(hc)
35
36 # should achieve the same result regardless of object type
37 result <- prune.hclust(hc, "Florida")
38 expect_equal(
39 labels(as.dendrogram(result)),
40 labels(prune_leaf(dend, "Florida"))
41 )
42 result <- prune.phylo(ph, "Florida")
43 expect_equal(
44 labels(as.dendrogram(result)),
45 labels(prune_leaf(dend, "Florida"))
46 )
47 # rpart is not a dependency for dendextend. so long as that is true, this will create an error in tests
48 # however, for local machine testing, rpart is likely installed. so, to force an error we use a non-tree object
49 expect_error(
50 prune.rpart(1:4, -1000)
51 )
52
53 # no default for prune
54 expect_error(
55 prune.default(dend)
56 )
57 })
58
59
60 test_that("prune_common_subtrees.dendlist works", {
61 suppressWarnings(RNGversion("3.5.0"))
62 set.seed(1)
63
64 hc <- USArrests[1:10, ] %>%
65 dist() %>%
66 hclust()
67 dend1 <- as.dendrogram(hc)
68 dend2 <- shuffle(dend1)
69 dend12 <- dendlist(dend1, dend2)
70
71 # dend1 and dend2 have the same subtrees, so resulting dend1 should be unchanged
72 result <- prune_common_subtrees.dendlist(dend12)
73 expect_identical(
74 result[[1]],
75 dend1
76 )
77
78 # if wrong objects passed in
79 expect_error(
80 prune_common_subtrees.dendlist(dend12[1])
81 )
82 expect_error(
83 prune_common_subtrees.dendlist(list(dend1, dend2))
84 )
85
86 })
87
88
89 test_that("intersect_trees works", {
90
91 hc <- USArrests[1:10, ] %>%
92 dist() %>%
93 hclust()
94 dend1 <- as.dendrogram(hc)
95 hc <- USArrests[2:11, ] %>%
96 dist() %>%
97 hclust()
98 dend2 <- as.dendrogram(hc)
99
100 # if there is an issue with label overlap
101 expect_warning(
102 intersect_trees(dend1, dend2, warn = T)
103 )
104 hc <- USArrests[11:20, ] %>%
105 dist() %>%
106 hclust()
107 dend2 <- as.dendrogram(hc)
108 expect_warning(
109 intersect_trees(dend1, dend2, warn = T)
110 )
111
112 })