Debian Codegraph @

tests/testthat/test-distinct_edges.R

raw
1 # cat("\n")
2 # library(testthat)
3
4
5 context("Distinct edges")
6
7
8 test_that("partition_leaves works", {
9 # library(magrittr)
10
11 x <- 1:3 %>%
12 dist() %>%
13 hclust() %>%
14 as.dendrogram()
15 # partition_leaves(x)
16 # plot(x)
17 expect_identical(partition_leaves(x), list(c(3L, 1L, 2L), 3L, 1:2, 1L, 2L))
18
19 # check case where something other than a dendrogram is passed in
20 x <- matrix(1:4, nrow=2)
21 expect_error(partition_leaves(x))
22 })
23
24
25
26 test_that("distinct_edges works", {
27 x <- 1:5 %>%
28 dist() %>%
29 hclust() %>%
30 as.dendrogram()
31 y <- set(x, "labels", 5:1)
32 expect_identical(distinct_edges(x, y), c(5L, 7L))
33 expect_identical(distinct_edges(y, x), c(2L, 5L))
34 })
35
36
37
38 test_that("dist.dendlist work", {
39 x <- 1:5 %>%
40 dist() %>%
41 hclust() %>%
42 as.dendrogram()
43 y <- set(x, "labels", 5:1)
44
45 # dend_diff(x,y)
46 tmp <- dist.dendlist(dendlist(x, x, y))
47
48 expect_identical(class(tmp), "dist")
49
50 # dput(as.matrix(tmp))
51 expect_identical(
52 as.matrix(tmp),
53 structure(c(0, 0, 4, 0, 0, 4, 4, 4, 0), .Dim = c(3L, 3L), .Dimnames = list(
54 c("1", "2", "3"), c("1", "2", "3")
55 ))
56 )
57
58 # check case where something other than a dendrogram is passed in
59 x <- matrix(1:4, nrow=2)
60 expect_error(dist.dendlist(x))
61 })
62
63
64
65 test_that("highlight_distinct_edges.dendrogram works", {
66 dend1 <- as.dendrogram(hclust(dist(iris[1:5, -5]), method = "average"))
67 dend2 <- as.dendrogram(hclust(dist(iris[2:6, -5]), method = "average"))
68
69 # check that edge color was indeed changed to identify which edges differ
70 result <- highlight_distinct_edges.dendrogram(dend1, dend2, value = 3, edgePar = "col")
71 expect_identical(attributes(result)$edgePar$col, 3)
72 })
73
74
75
76 test_that("highlight_distinct_edges.dendlist works", {
77 dend1 <- as.dendrogram(hclust(dist(iris[1:5, -5]), method = "average"))
78 dend2 <- as.dendrogram(hclust(dist(iris[2:6, -5]), method = "average"))
79 dend_list <- dendlist(dend1, dend2)
80
81 # check that edge color was indeed changed to identify which edges differ for both inserted dendrograms
82 result <- highlight_distinct_edges.dendlist(dend_list, value = 3, edgePar = "col")
83 expect_identical(attributes(result[[1]])$edgePar$col, 3)
84 expect_identical(attributes(result[[2]])$edgePar$col, 3)
85 })
86
87
88
89 test_that("dend_diff.dendrogram works", {
90 dend1 <- as.dendrogram(hclust(dist(iris[1:5, -5]), method = "average"))
91 dend2 <- as.dendrogram(hclust(dist(iris[2:6, -5]), method = "average"))
92
93 # check that edge color was indeed changed to identify which edges differ
94 pdf(file = NULL)
95 capture.output(suppressWarnings(
96 result <- dend_diff.dendrogram(dend1, dend2, value = 3, edgePar = "col")
97 ))
98 dev.off()
99 expect_identical(result, dendlist(dend1, dend2))
100 })
101
102
103
104 test_that("dend_diff.dendlist works", {
105 dend1 <- as.dendrogram(hclust(dist(iris[1:5, -5]), method = "average"))
106 dend2 <- as.dendrogram(hclust(dist(iris[2:6, -5]), method = "average"))
107 dend_list <- dendlist(dend1, dend2)
108
109 # check that edge color was indeed changed to identify which edges differ
110 pdf(file = NULL)
111 capture.output(suppressWarnings(
112 result <- dend_diff.dendlist(dend_list, value = 3, edgePar = "col")
113 ))
114 dev.off()
115 expect_identical(result, dendlist(dend1, dend2))
116 })