Debian Codegraph @

tests/testthat/test-cutree.dendrogram.R

raw
1 # library(testthat)
2 # library(dendextend)
3
4 # set this for testing
5 dendextend_options("warn", TRUE)
6
7 context("Cutting a dendrogram")
8
9
10 test_that("Checking if a number is natural", {
11 expect_true(is.natural.number(1)) # is TRUE
12 expect_true(all(is.natural.number(seq(1, 5, by = 1))))
13 expect_false(all(is.natural.number(seq(0, 5, by = 1))))
14 expect_false(all(is.natural.number(seq(-1, 5, by = 0.5))))
15 expect_error(is.natural.number("a"))
16 })
17
18
19
20 test_that("Turning factor into an integer", {
21 x <- factor(2:4)
22 expect_equal(as.numeric(x), 1:3)
23 names(x) <- letters[x]
24 expect_equal(as.numeric(x), 1:3)
25 # dput(fac2num(x))
26 expect_identical(
27 fac2num(x),
28 structure(c(2, 3, 4), .Names = c("a", "b", "c"))
29 )
30 expect_identical(fac2num(x, keep_names = FALSE), c(2, 3, 4))
31 })
32
33
34 test_that("cutree a dendrogram by height h", {
35 # data
36 hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
37 dend <- as.dendrogram(hc)
38
39 # we need h!
40 expect_error(cutree_1h.dendrogram(dend))
41
42 # the same as cutree
43 expect_identical(
44 cutree_1h.dendrogram(dend, h = 100),
45 cutree(hc, h = 100)
46 )
47
48 expect_identical(
49 cutree_1h.dendrogram(dend, h = 30),
50 cutree(hc, h = 30)
51 )
52
53 # the same as cutree - also when there are NO clusters
54 expect_identical(
55 cutree_1h.dendrogram(dend, h = 1000),
56 cutree(hc, h = 1000)
57 )
58
59 # the same as cutree - also when there are NO clusters
60 expect_identical(
61 cutree_1h.dendrogram(dend, h = 0),
62 cutree(hc, h = 0)
63 )
64 expect_identical(
65 cutree_1h.dendrogram(dend, h = 1),
66 cutree(hc, h = 1)
67 )
68
69 # get return in the order of the dendrogram:
70 expect_identical(
71 names(cutree_1h.dendrogram(dend, 100, order_clusters_as_data = FALSE)),
72 labels(dend)
73 )
74
75 # dealing with cutree_1h.dendrogram in negative h!
76 expect_identical(
77 cutree_1h.dendrogram(dend, h = -1),
78 stats::cutree(as.hclust(dend), h = -1)
79 )
80 expect_identical(
81 stats::cutree(as.hclust(dend), k = 5),
82 stats::cutree(as.hclust(dend), h = -1)
83 )
84
85 # dealing with case where multiple h values are passed in, should use the first one
86 expect_warning(
87 result <- cutree_1h.dendrogram(dend, h = c(-1, -2), warn = T)
88 )
89 expect_identical(result, stats::cutree(as.hclust(dend), h = -1))
90
91 # if order.dendrogram indices aren't 1:nleaves(dend)
92 expect_warning(with_mocked_bindings(
93 cutree_1h.dendrogram(dend, h = 50, order_clusters_as_data = T, warn = T),
94 order.dendrogram = function(...) return(2:6),
95 ))
96
97 })
98
99
100
101
102
103 test_that("get dendrogram heights for k clusters", {
104 # data
105 hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
106 dend <- as.dendrogram(hc)
107 unbranch_dend <- unbranch(dend, 2)
108
109 # plot(unbranch_dend)
110
111 dend_heights <- heights_per_k.dendrogram(dend)
112 unbranch_dend_heights <- heights_per_k.dendrogram(unbranch_dend)
113
114 # cutree_1h.dendrogram(dend, h=dend_heights[[3]])
115
116 expect_equal(length(dend_heights), 5)
117 expect_equal(length(unbranch_dend_heights), 4)
118
119 expect_equal(nnodes(unbranch_dend), 8)
120
121 # dput(names(unbranch_dend_heights))
122 expect_equal(names(unbranch_dend_heights), c("1", "3", "4", "5"))
123 })
124
125
126
127
128
129
130 test_that("cutree a dendrogram to k clusters", {
131 # data
132 hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
133 dend <- as.dendrogram(hc)
134 unbranch_dend <- unbranch(dend, 2)
135
136 # plot(unbranch_dend)
137
138
139 # the same as cutree
140 expect_identical(
141 cutree_1k.dendrogram(dend, k = 3),
142 cutree(hc, k = 3)
143 )
144
145 expect_identical(
146 cutree_1k.dendrogram(dend, k = 1),
147 cutree(hc, k = 1)
148 )
149
150 # the same as cutree - also when there are NO clusters
151 expect_identical(
152 cutree_1k.dendrogram(dend, k = 5),
153 cutree(hc, k = 5)
154 )
155
156 # if ignoring the "names" on the vector - the numbers will be identical:
157 expect_identical(
158 unname(cutree_1k.dendrogram(dend, k = 3, use_labels_not_values = FALSE)),
159 unname(cutree(hc, k = 3))
160 )
161
162 # if k is outside the range of possible cluster
163 trace("cutree_1k.dendrogram", quote(k <- -1), at = 9, print = F)
164 expect_warning(cutree_1k.dendrogram(unbranch_dend, 2, warn = TRUE))
165 untrace("cutree_1k.dendrogram")
166
167 # errors:
168 expect_error(cutree_1k.dendrogram(dend)) # we need h!
169 expect_error(cutree_1k.dendrogram(dend, k = -1))
170 expect_error(cutree_1k.dendrogram(dend, k = 0))
171 # expect_error( cutree_1k.dendrogram(dend, k = 1.5)) # I no longer expect an error since it is turned into "as.integer"
172 expect_error(cutree_1k.dendrogram(dend, k = 50))
173 expect_error(cutree(hc, k = 50))
174
175
176 # get return in the order of the dendrogram:
177 expect_identical(
178 names(cutree_1k.dendrogram(dend, k = 3, order_clusters_as_data = FALSE)),
179 labels(dend)
180 )
181
182 # cases of no possible k's:
183 expect_warning(cutree_1k.dendrogram(unbranch_dend, 2, warn = TRUE))
184 expect_equal(cutree_1k.dendrogram(unbranch_dend, 2, warn = FALSE), rep(NA, 5))
185 })
186
187
188
189
190
191
192 test_that("cutree dendrogram method works for k", {
193 # data
194 hc <- USArrests[c(1, 6, 13, 20, 23), ] %>%
195 dist() %>%
196 hclust("ave")
197 dend <- as.dendrogram(hc)
198 unbranch_dend <- unbranch(dend, 2)
199
200 # plot(unbranch_dend)
201
202
203 # the same as cutree
204 expect_identical(
205 cutree(dend, k = 3),
206 cutree(hc, k = 3)
207 )
208
209 expect_identical(
210 cutree(dend, k = 1),
211 cutree(hc, k = 1)
212 )
213
214 # the same as cutree - also when there are NO clusters
215 expect_identical(
216 cutree(dend, k = 5),
217 cutree(hc, k = 5)
218 )
219
220 # if ignoring the "names" on the vector - the numbers will be identical:
221 expect_identical(
222 unname(cutree(dend, k = 3, use_labels_not_values = FALSE)),
223 unname(cutree(hc, k = 3))
224 )
225
226
227 # use_labels_not_values doesn't harm cutree
228 expect_identical(
229 cutree(dend, k = 3, use_labels_not_values = TRUE),
230 cutree(dend, k = 3, use_labels_not_values = FALSE)
231 )
232
233 # use_labels_not_values doesn't harm cutree also when try_cutree_hclust=FALSE
234 expect_identical(
235 unname(cutree(dend, k = 3, use_labels_not_values = TRUE, try_cutree_hclust = FALSE)),
236 unname(cutree(dend, k = 3, use_labels_not_values = FALSE, try_cutree_hclust = FALSE))
237 )
238
239
240 # errors:
241 expect_error(cutree(dend)) # we need h!
242 expect_error(cutree(dend, k = -1))
243 expect_error(cutree(dend, k = 0))
244 # expect_error( cutree(dend, k = 1.5)) # I no longer expect an error since it is turned into "as.integer"
245 expect_error(cutree(dend, k = 50))
246 expect_error(cutree(hc, k = 50))
247
248
249 # get return in the order of the dendrogram:
250 expect_identical(
251 names(cutree(dend, k = 3, order_clusters_as_data = FALSE, try_cutree_hclust = FALSE)),
252 labels(dend)
253 )
254
255 expect_identical(
256 names(cutree(dend, k = 3, order_clusters_as_data = FALSE, try_cutree_hclust = TRUE)),
257 labels(dend)
258 )
259
260 # cases of no possible k's:
261 expect_warning(cutree(unbranch_dend, 2))
262 expect_equal(
263 suppressWarnings(cutree(unbranch_dend, 2, warn = FALSE)),
264 rep(0, 5)
265 )
266
267 # now to check vectorization
268 })
269
270
271
272
273
274 test_that("cutree for flat edges", {
275
276 # cutree(hclust(dist(c(1,1,1,2,2))), k=5)
277 # cutree(hclust(dist(c(1,1,1,2,2))), k=1:5)
278
279 dend <- as.dendrogram(hclust(dist(c(1, 1, 1, 2, 2))))
280 # dendextend:::cutree.dendrogram(dend, k=5)
281 # as.hclust(dend) # Error: all(vapply(s, is.integer, NA)) is not TRUE
282 # cutree(dend,k=5)
283 # plot(dend)
284 # dendextend_cut_lower_fun(dend, -.5, labels)
285 # cut_lower_fun(dend, -.5, labels)
286
287 expect_equal(unname(cutree(dend, k = 2)), c(1, 1, 1, 2, 2))
288 expect_equal(unname(cutree(dend, h = -1)), 1:5) # weird definition
289 expect_equal(unname(cutree(dend, k = 5)), 1:5)
290 expect_warning(cutree(dend, k = 4, try_cutree_hclust = FALSE))
291 expect_equal(suppressWarnings(cutree(dend, k = 4, try_cutree_hclust = FALSE)), rep(0, 5))
292
293 # as of R R 3.2.4 (or 3.3.0 -not sure ) - as.hclust was fixed to deal better with ties on the branch heights.
294 # That means that:
295 # cutree(as.hclust(dend), k=4)
296 # would work (it will give hard-to-interpret results - but it would work)
297
298 # as.hclust(dend)
299 })
300
301
302
303
304
305
306
307
308 test_that("cutree for dendrogram works (k,h and vectorization)", {
309
310 # data
311 hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
312 dend <- as.dendrogram(hc)
313 unbranch_dend <- unbranch(dend, 2)
314
315 # plot(unbranch_dend)
316
317
318
319 # the same as cutree
320 expect_identical(
321 cutree(dend, k = 1:4),
322 cutree(hc, k = 1:4)
323 )
324
325 expect_identical(
326 cutree(dend, h = c(20, 25.5, 50, 170)),
327 cutree(hc, h = c(20, 25.5, 50, 170))
328 )
329
330 expect_warning(cutree(unbranch_dend, k = 1:2))
331
332 # it still works for missing k's, it just returns NA's in the second column
333 cutree_unbranch_dend <- suppressWarnings(cutree(unbranch_dend, k = 1:4, warn = FALSE))
334 expect_true(is.matrix(cutree_unbranch_dend))
335 expect_true(all(cutree_unbranch_dend[, 2] == 0)) # 2nd column is NA.
336
337 cutree_unbranch_dend_2 <- suppressWarnings(cutree(unbranch_dend,
338 k = 1:4,
339 warn = FALSE, order_clusters_as_data = FALSE,
340 try_cutree_hclust = FALSE
341 ))
342 expect_identical(rownames(cutree_unbranch_dend_2), labels(unbranch_dend))
343 })
344
345
346
347
348 # test_that("Making cutted clusters be numbered from left to right",{
349
350
351 test_that("Testing sort_levels_values works", {
352
353 # the function can return the same object:
354 x <- 1:4
355 names(x) <- letters[x]
356 attr(x, "keep_me") <- "a cat"
357 expect_equal(x, sort_levels_values(x))
358 expect_identical(names(x), names(sort_levels_values(x)))
359 expect_identical(attributes(x), attributes(sort_levels_values(x)))
360
361 x <- c(4:1)
362 names(x) <- letters[x]
363 attr(x, "keep_me") <- "a cat"
364 # it will keep the attributes as they are:
365 expect_identical(attributes(x), attributes(sort_levels_values(x)))
366 expect_equivalent(sort(x), sort_levels_values(x)) # not equal since "sort" removes the attr!
367
368 x <- c(4:1, 4, 2)
369 # dput(sort_levels_values(x)) # 1 2 3 4 1 3
370 expect_identical(sort_levels_values(x), c(1, 2, 3, 4, 1, 3))
371
372 x <- c(2, 2, 3, 2, 1)
373 expect_identical(sort_levels_values(x), c(1, 1, 2, 1, 3))
374
375 # works when used on matrices:
376 x <- matrix(1:16, 4, 4)
377 rownames(x) <- letters[1:4]
378 # x
379 expect_equal(x, apply(x, 2, sort_levels_values))
380
381
382 x <- matrix(4:1, 2, 2)
383 rownames(x) <- letters[1:2]
384 # x
385 # dput(apply(x, 2, sort_levels_values))
386 expect_identical(
387 apply(x, 2, sort_levels_values),
388 structure(c(3, 4, 1, 2), .Dim = c(2L, 2L), .Dimnames = list(c(
389 "a",
390 "b"
391 ), NULL))
392 )
393
394 # checking that sort_levels_values can be used on a matrix!
395 x <- matrix(4:1, 2, 2)
396 rownames(x) <- letters[1:2]
397 # x
398 # dput(apply(x, 2, sort_levels_values))
399 expect_identical(apply(x, 2, sort_levels_values), sort_levels_values(x))
400 # Yay!
401
402 # checking that sort_levels_values raises warning for matrix with NA values and returns original matrix
403 x[1,1] <- NA
404 expect_warning(
405 identical_x <- sort_levels_values(x, warn = T)
406 )
407 expect_identical(x, identical_x)
408
409 # checking that sort_levels_values requires numeric matrix
410 x <- matrix(letters[1:4], 2, 2)
411 expect_error(
412 sort_levels_values(x)
413 )
414 })
415
416
417 test_that("Making cutted clusters be numbered from left to right", {
418 hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
419 dend <- as.dendrogram(hc)
420
421 sorted_cutree_hc_orig <- stats::cutree(hc, k = 1:4)
422 sorted_cutree_hc <- dendextend:::cutree.hclust(hc, k = 1:4)
423 sorted_cutree_dend <- dendextend:::cutree.dendrogram(dend, k = 1:4, try_cutree_hclust = FALSE)
424
425 expect_identical(
426 sorted_cutree_hc_orig,
427 sorted_cutree_hc
428 )
429
430
431 expect_identical(
432 mode(sorted_cutree_hc),
433 mode(sorted_cutree_dend)
434 )
435
436 expect_identical(
437 attributes(sorted_cutree_hc),
438 attributes(sorted_cutree_dend)
439 )
440
441 # the same as cutree
442 expect_identical(
443 as.integer(cutree(dend, k = 1:4, try_cutree_hclust = FALSE)),
444 as.integer(cutree(hc, k = 1:4))
445 )
446
447 expect_identical(
448 as.vector(sorted_cutree_hc),
449 as.vector(sorted_cutree_dend)
450 ) # this is identical since we are forcing the numbers to be integers!
451
452 expect_identical(
453 (sorted_cutree_hc),
454 (sorted_cutree_dend)
455 ) # this is identical since we are forcing the numbers to be integers!
456 })
457
458
459
460 test_that("Compare labels which are character vs integer", {
461 iris <- datasets::iris
462
463 # they seem to be identical - but they are not in the way the are coerced!
464 expect_identical(
465 iris[1:150, -5],
466 iris[, -5]
467 )
468
469 # once they are coerced into a matrix - they are NOT identical!
470 # the rownames are now NULL!
471 expect_false(identical(
472 as.matrix(iris[1:150, -5]),
473 as.matrix(iris[, -5])
474 ))
475
476 expect_false(identical(
477 attributes(as.matrix(iris[1:150, -5])),
478 attributes(as.matrix(iris[, -5]))
479 ))
480
481 expect_false(identical(
482 rownames(as.matrix(iris[1:150, -5])),
483 rownames(as.matrix(iris[, -5]))
484 ))
485
486 # it now has no rownames!
487 expect_true(is.null(rownames(as.matrix(iris[, -5]))))
488
489 # what about their dist - not the same!:
490 expect_false(identical(
491 dist(iris[1:150, -5]),
492 dist(iris[, -5])
493 ))
494 # the first one has "labels" and the second one doesn't
495 expect_false(identical(
496 attributes(dist(iris[1:150, -5])),
497 attributes(dist(iris[, -5]))
498 ))
499
500 d_iris <- dist(iris[, -5])
501 hc_iris <- hclust(d_iris)
502 dend_iris <- as.dendrogram(hc_iris)
503 expect_true(is.integer(labels(dend_iris))) # this is a source of BUGS!
504 expect_false(is.character(labels(dend_iris)))
505 })
506
507
508
509
510
511 test_that("heights_per_k.dendrogram", {
512 dend15 <- c(1:5) %>%
513 dist() %>%
514 hclust(method = "average") %>%
515 as.dendrogram()
516 tmp <- heights_per_k.dendrogram(dend15)
517 tmp_should_be <- structure(c(2.75, 2.25, 1.25, 0.75), .Names = c("1", "2", "3", "5"))
518 expect_equal(tmp, tmp_should_be)
519
520 dend15 <- c(1:5) %>%
521 dist() %>%
522 hclust(method = "sin") %>%
523 as.dendrogram()
524 # dput(tmp)
525 expect_warning(tmp <- heights_per_k.dendrogram(dend15))
526 tmp_should_be <- structure(c(Inf, -Inf), .Names = c("1", "5"))
527 expect_equal(tmp, tmp_should_be)
528 })
529
530
531
532
533
534
535
536
537 # library(stats)
538 # library(dendextendRcpp)
539
540 #
541 # test_that("Having cutree work when using a subsetted tree",{
542 # # Wo
543 # # get a dendrogram:
544 # # data(iris)
545 # d_iris <- dist(datasets::iris[1:10,-5])
546 # hc_iris <- hclust(d_iris)
547 # dend_iris <- as.dendrogram(hc_iris) # as.hclust.dendrogram - of course
548 #
549 # # taking a subset of the dendrogram:
550 # sub_dend_iris <- dend_iris[[1]]
551 # hc_sub_dend_iris <- as.hclust(sub_dend_iris)
552 # # We will have NA's:
553 # expect_true(any(is.na(stats:::labels.dendrogram(as.dendrogram(hc_sub_dend_iris )))))
554 #
555 #
556 # #if(require(dendextendRcpp)) {
557 # if("package:dendextendRcpp" %in% search()) {
558 #
559 # # notice that for Rcpp this would be false since the returned vector
560 # # has "NA" characters instead of NA:
561 # expect_false(any(is.na(dendextendRcpp::labels.dendrogram(as.dendrogram(hc_sub_dend_iris )))))
562 # # e.g: "NA" "3" "NA" "NA" "4" "7"
563 # # a[which(a == "NA")] <- NA # this is NOT a good idea, in the case we have a label with "NA" as a character.
564 # }
565 #
566 #
567 # # we will get warnings, but the functions would not collapse!
568 # expect_warning(
569 # dendextend:::cutree.dendrogram(as.dendrogram(hc_sub_dend_iris ), 3, try_cutree_hclust = TRUE)
570 # )
571 # expect_warning(
572 # dendextend:::cutree.dendrogram(as.dendrogram(hc_sub_dend_iris ), 3, try_cutree_hclust = FALSE)
573 # )
574 #
575 # # remove "iris" from the last test...
576 # # if(exists("iris")) # it says it doesn't exists - but it does (in the gloval env)!
577 # # suppressWarnings()
578 # # rm(iris, pos = 1)
579 #
580 # })
581 #
582 #
583
584 test_that("cutree.hclust works", {
585 iris <- datasets::iris
586
587 # covers case where kultiple k values passed in, causing clusters to be a matrix
588 tree <- hclust(dist(iris[, -5]), method = "average")
589 k_values <- c(2, 3, 4)
590 clusters <- cutree.hclust(tree, k = k_values, order_clusters_as_data = F)
591 expect_true(all(dim(clusters) == c(150,3)))
592
593 # test with NA labels
594 labels(tree) = NA
595 expect_warning(
596 clusters <- cutree.hclust(tree, k = 2, warn = T)
597 )
598 })
599
600
601
602 test_that("cutree.phylo works", {
603 hclust_tree <- hclust(dist(iris[1:5, -5]), method = "average")
604 result <- cutree.phylo(hclust_tree, k = 2)
605 expect_true(all(result == c(1,2,2,2,1)))
606 })
607
608 test_that("cutree.agnes works", {
609 hclust_tree <- hclust(dist(iris[1:5, -5]), method = "average")
610 result <- cutree.agnes(hclust_tree, k = 2)
611 expect_true(all(result == c(1,2,2,2,1)))
612 })
613
614 test_that("cutree.diana works", {
615 hclust_tree <- hclust(dist(iris[1:5, -5]), method = "average")
616 result <- cutree.diana(hclust_tree, k = 2)
617 expect_true(all(result == c(1,2,2,2,1)))
618 })
619
620
621
622 test_that("cutree.dendrogram works", {
623 # case when a dendrogram is not passed in
624 mat = matrix(1:4, nrow = 2)
625 expect_error(cutree.dendrogram(mat), k =2)
626
627 # case where both k and h are specified
628 dend <- as.dendrogram(hclust(dist(c(1, 1, 1, 2, 2))))
629 expect_warning(cutree.dendrogram(dend, k = 2, h = 2, warn = T))
630
631 # case where h is specified
632 dend <- as.dendrogram(hclust(dist(c(1, 1, 1, 2, 2))))
633 result <- cutree.dendrogram(dend, h = 0.2, try_cutree_hclust = F)
634 expect_true(all(result == c(1,1,1,2,2)))
635
636 # if order.dendrogram indices aren't 1:nleaves(dend)
637 expect_warning(with_mocked_bindings(
638 cutree.dendrogram(dend, h = 0.2, try_cutree_hclust = T, warn = T),
639 order.dendrogram = function(...) return(2:6)
640 ))
641
642 })
643
644
645
646 dendextend_options("warn", FALSE)