Debian Codegraph @

tests/testthat/test-untangle.R

raw
1 library(testthat)
2 context("Untangle two dendrograms for plotting a tanglegram")
3
4 test_that("shuffle works", {
5 suppressWarnings(RNGversion("3.5.0"))
6 # library(magrittr)
7 dend <- USArrests %>%
8 dist() %>%
9 hclust() %>%
10 as.dendrogram()
11 set.seed(234238)
12 dend2 <- dend %>% shuffle()
13
14 # different tree layouts:
15 # tanglegram(dend, dend2, margin_inner=7)
16 expect_identical(round(entanglement(dend, dend2), 3), 0.398)
17 expect_false(identical(dend, dend2))
18
19 # same topology
20 expect_true(identical(sort(dend), sort(dend2)))
21
22 dend12 <- dendlist(dend, dend2)
23 expect_false(identical(dend12, shuffle(dend12)))
24 expect_equal(length(shuffle(dend12)), 2)
25 })
26
27
28
29 test_that("Flip leaves work", {
30 dend1 <- USArrests[1:5, ] %>%
31 dist() %>%
32 hclust() %>%
33 as.dendrogram()
34 dend2 <- flip_leaves(dend1, c(3, 5), c(1, 2))
35 # tanglegram(dend1,dend2)
36 # tanglegram(dend1,flip_leaves(dend1, c(3,5, 1,2), c(4)))
37
38 expect_identical(entanglement(dend1, dend2, L = 2), 0.4)
39 })
40
41
42
43 test_that("all_couple_rotations_at_k work", {
44 dend1 <- USArrests[1:5, ] %>%
45 dist() %>%
46 hclust() %>%
47 as.dendrogram()
48 dend2 <- all_couple_rotations_at_k(dend1, k = 2)[[2]]
49 # tanglegram(dend1,dend2)
50
51 expect_identical(entanglement(dend1, dend2, L = 2), 0.5)
52
53 # returns original dend if k == 1
54 dend2 <- all_couple_rotations_at_k(dend1, k = 1)
55 expect_identical(dend1, dend2)
56 # uses first element of k if k is a vector and raises a warning
57 expect_warning(
58 dend2 <- all_couple_rotations_at_k(dend1, k = c(2, 3))
59 )
60
61 # if k_cluster_leaves is NA
62 trace("all_couple_rotations_at_k", quote(k_cluster_leaves <- c(NA)), at = 8, print = FALSE)
63 result <- all_couple_rotations_at_k(dend1, k = 4)
64 untrace("all_couple_rotations_at_k")
65 expect_identical(
66 labels(result),
67 "1"
68 )
69
70 # if km1_cluster_leaves is NA
71 trace("all_couple_rotations_at_k", quote(km1_cluster_leaves <- c(NA)), at = 8, print = FALSE)
72 expect_warning(
73 all_couple_rotations_at_k(dend1, k = 4)
74 )
75 untrace("all_couple_rotations_at_k")
76
77 })
78
79
80
81
82 test_that("untangle_step_rotate_1side work", {
83 suppressWarnings(RNGversion("3.5.0"))
84 dend1 <- USArrests[1:10, ] %>%
85 dist() %>%
86 hclust() %>%
87 as.dendrogram()
88 set.seed(3525)
89 dend2 <- shuffle(dend1)
90 # tanglegram(dend1,dend2)
91 expect_identical(round(entanglement(dend1, dend2, L = 2), 2), 0.47)
92
93 # Fixing the problem :)
94 dend2_corrected <- untangle.dendrogram(dend2, dend1, "step1side", leaves_matching_method = "order")[[1]]
95 # tanglegram(dend1,dend2_corrected) # FIXED.
96 expect_identical(round(entanglement(dend1, dend2_corrected, L = 2), 2), 0)
97
98 # the other direction may also work:
99 dend2_corrected <- untangle.dendrogram(dend2, dend1, "step1side", direction = "backward")[[1]]
100 # tanglegram(dend1,dend2_corrected) # FIXED.
101 expect_identical(round(entanglement(dend1, dend2_corrected, L = 2), 2), 0)
102
103 # if leaf mismatch in dend1 and dend after ordering
104 dendextend_options("warn", T)
105 expect_warning(expect_error(with_mocked_bindings(
106 untangle_step_rotate_1side(dend2, dend1, "step1side", leaves_matching_method = "order"),
107 match_order_by_labels = function(dend1, dend2, ...) return(dend2)
108 )))
109 dendextend_options("warn", F)
110
111 })
112
113
114
115
116 test_that("untangle_step_rotate_2side work", {
117 suppressWarnings(RNGversion("3.5.0"))
118 set.seed(1)
119
120 dend1 <- USArrests[1:20, ] %>%
121 dist() %>%
122 hclust() %>%
123 as.dendrogram()
124 noisy_USArrests = USArrests[1:20, ] %>% scale() + rnorm(80)
125 dend2 <- noisy_USArrests %>%
126 dist() %>%
127 hclust(method = "med") %>%
128 as.dendrogram() %>%
129 shuffle()
130
131 expect_identical(round(entanglement(dend1, dend2, L = 2), 2), 0.24)
132
133 # enable print_times for test case but avoid cluttering output by capturing print statement
134 # warnings suppressed as they were previously
135 suppressWarnings(capture.output(
136 # Fixing the problem :)
137 dend12_corrected <- untangle_step_rotate_2side(dend1, dend2, L = 2, print_times = T, max_n_iterations = 20)
138 ))
139 expect_identical(round(entanglement(dend12_corrected[[1]],dend12_corrected[[2]], L = 2),3) , 0.059)
140
141 # if times reaches 2+ iterations
142 expect_output(with_mocked_bindings(
143 result <- untangle_step_rotate_2side(dend1, dend2, k = 4, print_times = T),
144 untangle_step_rotate_1side = function(dend1, dend2, ...) return(list(shuffle(dend2), dend1))
145 ))
146
147 })
148
149
150
151 test_that("untangle_step_rotate_both_side work", {
152 suppressWarnings(RNGversion("3.5.0"))
153 set.seed(1)
154 # Entanglement should be zero after applying algorithm, per Fig. 4 of 'Shuffle & untangle: novel untangle methods for solving the tanglegram layout problem' (Nguyen et al. 2022)
155 example_labels <- c("Versicolor 90", "Versicolor 54", "Versicolor 81", "Versicolor 63", "Versicolor 72", "Versicolor 99", "Virginica 135", "Virginica 117", "Virginica 126", "Virginica 108", "Virginica 144", "Setosa 27", "Setosa 18", "Setosa 36", "Setosa 45", "Setosa 9")
156
157 iris_modified <- datasets::iris
158 iris_modified$Row <- seq_len(nrow(iris_modified))
159 iris_modified$Label <- paste(tools::toTitleCase(as.character(iris_modified$Species)), iris_modified$Row)
160 iris_modified <- iris_modified[iris_modified$Label %in% example_labels, ]
161
162 iris_numeric <- iris_modified[,1:4]
163 rownames(iris_numeric) <- iris_modified$Label
164
165 dend1 <- as.dendrogram(hclust(dist(iris_numeric), method = "single"))
166 dend2 <- as.dendrogram(hclust(dist(iris_numeric), method = "complete"))
167 # enable print_times for test case but avoid cluttering output by capturing print statement
168 capture.output(
169 result <- untangle.dendrogram(dend1, dend2, "stepBothSides", print_times = T)
170 )
171 dend1 <- result[[1]]
172 dend2 <- result[[2]]
173 expect_identical(entanglement(dend1, dend2, L = 2), 0)
174
175 })
176
177
178
179
180 #
181 # # This one is an example of how 2side rotation is better. But it takes longer
182 # # So I don't run it:
183 #
184 # test_that("untangle_step_rotate_2side works better than 1side",{
185 #
186 #
187 # dend1 <- as.dendrogram(hclust(dist(USArrests[1:20,])))
188 # dend2 <- as.dendrogram(hclust(dist(USArrests[1:20,]), method = "single"))
189 # set.seed(3525)
190 # dend2 <- shuffle(dend2)
191 # # tanglegram(dend1,dend2, margin_inner=6.5)
192 # expect_identical(round(entanglement(dend1,dend2, L = 2),2) , 0.79)
193 #
194 # dend2_corrected <- untangle_step_rotate_1side(dend2, dend1)
195 # # tanglegram(dend1,dend2_corrected, margin_inner=6.5) # Good.
196 # expect_identical(round(entanglement(dend1,dend2_corrected, L = 2),3) , 0.007)
197 # # it is better, but not perfect. Can we improve it?
198 #
199 # dend12_corrected <- untangle_step_rotate_2side(dend1, dend2,print_times=FALSE)
200 # # tanglegram(dend12_corrected[[1]],dend12_corrected[[2]], margin_inner=6.5) # Better...
201 # expect_identical(round(entanglement(dend12_corrected[[1]],dend12_corrected[[2]], L=2),3) , 0.005)
202 #
203 # # best combination:
204 # dend12_corrected_1 <- untangle_random_search(dend1, dend2)
205 # dend12_corrected_2 <- untangle_step_rotate_2side(dend12_corrected_1[[1]],dend12_corrected_1[[2]],print_times=FALSE)
206 # # tanglegram(dend12_corrected_2[[1]],dend12_corrected_2[[2]], margin_inner=6.5) # Better...
207 # expect_identical(round(entanglement(dend12_corrected_2[[1]],dend12_corrected_2[[2]], L=2),3) , 0)
208 # # 0 - PERFECT.
209 #
210 # })
211 #
212
213
214
215
216 test_that("untangle (main function) works for random search", {
217 suppressWarnings(RNGversion("3.5.0"))
218 dend1 <- USArrests[1:5, ] %>%
219 dist() %>%
220 hclust() %>%
221 as.dendrogram()
222 dend2 <- flip_leaves(dend1, c(3, 5), c(1, 2))
223 # tanglegram(dend1,dend2)
224 # tanglegram(dend1,flip_leaves(dend1, c(3,5, 1,2), c(4)))
225 dend12 <- dendlist(dend1, dend2)
226
227 set.seed(123123)
228 out1 <- untangle(dend12, method = "ran", R = 10)
229 set.seed(123123)
230 out2 <- untangle_random_search(dend1, dend2, R = 10)
231 expect_identical(out1, out2)
232 })
233
234
235
236
237 test_that("untangle (main function) works for 2 step", {
238 suppressWarnings(RNGversion("3.5.0"))
239 dend1 <- USArrests[1:5, ] %>%
240 dist() %>%
241 hclust() %>%
242 as.dendrogram()
243 dend2 <- flip_leaves(dend1, c(3, 5), c(1, 2))
244 # tanglegram(dend1,dend2)
245 # tanglegram(dend1,flip_leaves(dend1, c(3,5, 1,2), c(4)))
246 dend12 <- dendlist(dend1, dend2)
247
248 out1 <- untangle(dend12, method = "step2")
249 set.seed(123123)
250 out2 <- untangle_step_rotate_2side(dend1, dend2)
251 expect_identical(out1, out2)
252 })
253
254
255
256
257 test_that("untangle_best_k_to_rotate_by_1side works", {
258 suppressWarnings(RNGversion("3.5.0"))
259 set.seed(226)
260
261 dend1 <- USArrests[1:10, ] %>%
262 dist() %>%
263 hclust() %>%
264 as.dendrogram()
265 dend2 <- shuffle(dend1)
266 original_entanglement <- entanglement(dend1, dend2)
267 expect_identical(round(original_entanglement, 3), 0.248)
268 # resolve entanglement
269 dend1_corrected <- untangle_best_k_to_rotate_by_1side(dend1, dend2)
270 corrected_entanglement <- entanglement(dend1_corrected, dend2)
271
272 # reduces entanglement from 0.248 to 0
273 expect_identical(round(corrected_entanglement, 3), 0)
274 })
275
276
277
278
279 test_that("untangle_best_k_to_rotate_by_2side_backNforth works", {
280 suppressWarnings(RNGversion("3.5.0"))
281 set.seed(30)
282
283 dend1 <- USArrests[1:10, ] %>%
284 dist() %>%
285 hclust() %>%
286 as.dendrogram()
287 dend2 <- shuffle(dend1)
288 original_entanglement <- entanglement(dend1, dend2)
289 expect_identical(round(original_entanglement, 3), 0.251)
290 # enable print_times for test case but avoid cluttering output by capturing print statement
291 capture.output(
292 # resolve entanglement
293 dends_corrected <- untangle_best_k_to_rotate_by_2side_backNforth(dend1, dend2, L = 1, print_times = T)
294 )
295 corrected_entanglement <- entanglement(dends_corrected[[1]], dends_corrected[[2]])
296
297 # reduces entanglement from 0.251 to 0
298 expect_identical(round(corrected_entanglement, 3), 0)
299 })
300
301
302
303
304 test_that("collapse_with_pipes works", {
305 x <- c("before pipe ", " after pipe")
306 collapsed_vector = collapse_with_pipes(x)
307 expect_identical(collapsed_vector, "before pipe || after pipe")
308 })
309
310
311
312
313 test_that("untangle.default works", {
314 suppressWarnings(RNGversion("3.5.0"))
315 set.seed(1)
316
317 dend1 <- USArrests[1:10, ] %>%
318 dist() %>%
319 hclust() %>%
320 as.dendrogram()
321 dend2 <- shuffle(dend1)
322
323 expect_error(untangle.default(dend1, dend2))
324 })
325
326
327
328
329 test_that("flip_1_and_2 works", {
330 x <- c(1,2,2,1)
331 flipped <- flip_1_and_2(x)
332 expect_identical(flipped, c(2,1,1,2))
333 })
334
335
336
337
338 test_that("untangle_labels works", {
339 suppressWarnings(RNGversion("3.5.0"))
340 set.seed(1)
341
342 # Create two example dendrograms with different label orders
343 dend1 <- as.dendrogram(hclust(dist(USArrests[1:5, ]), method = "complete"))
344 dend2 <- as.dendrogram(hclust(dist(USArrests[5:1, ]), method = "complete"))
345 # use untangle_labels to reorder dend2 based on dend1
346 result <- untangle.dendrogram(dend1, dend2, "labels")
347 expect_identical(labels(result[[1]]), labels(result[[2]]))
348 })
349
350
351
352
353 test_that("untangle_DendSer works", {
354 skip_if_not_installed("DendSer")
355
356 suppressWarnings(RNGversion("3.5.0"))
357 set.seed(232)
358
359 ss <- sample(1:150, 20)
360 dend1 <- iris[ss, -5] %>%
361 dist() %>%
362 hclust("com") %>%
363 as.dendrogram()
364 dend2 <- iris[ss, -5] %>%
365 dist() %>%
366 hclust("sin") %>%
367 as.dendrogram()
368 dend12 <- dendlist(dend1, dend2)
369
370 bad_entanglement =
371 dend12 %>%
372 untangle("step2") %>%
373 entanglement()
374 expect_identical(round(bad_entanglement, 3), 0.014)
375
376 best_entanglement =
377 untangle.dendrogram(dend1, dend2, "DendSer") %>%
378 untangle_DendSer() %>%
379 untangle("step2") %>%
380 entanglement()
381 # reduces entanglement from 0.014 to 0
382 expect_identical(best_entanglement, 0)
383 })
384
385
386
387
388 test_that("ladderize works", {
389 suppressWarnings(RNGversion("3.5.0"))
390 set.seed(1)
391
392 dend1 <- as.dendrogram(hclust(dist(mtcars[1:5, ]), method = "average"))
393 dend2 <- as.dendrogram(hclust(dist(mtcars[6:10, ]), method = "ward.D2"))
394
395 result <- untangle.dendrogram(dend1, dend2, "ladderize")
396
397 # check that both dendrograms were ladderized
398 expect_false(identical(order.dendrogram(dend1), order.dendrogram(result[[1]])))
399 expect_false(identical(order.dendrogram(dend2), order.dendrogram(result[[2]])))
400
401 # the first dendrogram should be ladderized, but the second should remain the same
402 result_partial <- untangle.dendrogram(dend1, dend2, "ladderize", right = TRUE, which = 1)
403 expect_false(identical(order.dendrogram(dend1), order.dendrogram(result_partial[[1]])))
404 expect_identical(order.dendrogram(dend2), order.dendrogram(result_partial[[2]]))
405
406 # check that the orders should differ after changing the 'right' argument
407 result_right_true <- untangle.dendrogram(dend1, dend2, "ladderize", right = TRUE)
408 result_right_false <- untangle.dendrogram(dend1, dend2, "ladderize", right = FALSE)
409 expect_false(identical(order.dendrogram(result_right_true[[1]]), order.dendrogram(result_right_false[[1]])))
410 expect_false(identical(order.dendrogram(result_right_true[[2]]), order.dendrogram(result_right_false[[2]])))
411 })
412
413
414
415
416 test_that("untangle.dendlist works", {
417 suppressWarnings(RNGversion("3.5.0"))
418 set.seed(1)
419
420 dend1 <- as.dendrogram(hclust(dist(mtcars[6:10, ]), method = "complete"))
421 dend2 <- as.dendrogram(hclust(dist(mtcars[6:10, ]), method = "single"))
422 dend3 <- as.dendrogram(hclust(dist(mtcars[6:10, ]), method = "average"))
423 dend_list <- dendlist(dend1 = dend1, dend2 = dend2, dend3 = dend3)
424
425 result <- untangle.dendlist(dend_list, method = "step2side")
426
427 # entanglement improved from 0.522 to 0
428 initial_entanglement <- entanglement(dend_list[[1]], dend_list[[2]])
429 expect_identical(round(initial_entanglement, 3), 0.522)
430
431 final_entanglement <- entanglement(result[[1]], result[[2]])
432 expect_identical(round(final_entanglement, 3), 0)
433 })
434
435
436
437
438 test_that("untangle_random_search works", {
439 suppressWarnings(RNGversion("3.5.0"))
440 set.seed(1)
441
442 dend1 <- as.dendrogram(hclust(dist(iris[1:50, -5]), method = "average"))
443 dend2 <- as.dendrogram(hclust(dist(iris[50:1, -5]), method = "single"))
444
445 result <- untangle_random_search(dend1, dend2, R = 10, leaves_matching_method = "order")
446
447 # entanglement improved from 0.579 to 0.311
448 initial_entanglement <- entanglement(dend1, dend2)
449 expect_identical(round(initial_entanglement, 3), 0.579)
450
451 final_entanglement <- entanglement(result[[1]], result[[2]])
452 expect_identical(round(final_entanglement, 3), 0.311)
453
454 dendextend_options("warn", T)
455 expect_warning(
456 untangle_random_search(dend1, dend2, R = 10, leaves_matching_method = "order")
457 )
458 dendextend_options("warn", F)
459 })
460
461
462
463
464 test_that("untangle_intercourse works", {
465 suppressWarnings(RNGversion("3.5.0"))
466 set.seed(1)
467
468 brother_1_dend1 <- as.dendrogram(hclust(dist(iris[1:10, -5]), method = "complete"))
469 brother_1_dend2 <- as.dendrogram(hclust(dist(iris[10:1, -5]), method = "single"))
470
471 sister_2_dend1 <- as.dendrogram(hclust(dist(iris[11:20, -5]), method = "average"))
472 sister_2_dend2 <- as.dendrogram(hclust(dist(iris[20:11, -5]), method = "ward.D2"))
473
474 result <- untangle_intercourse(brother_1_dend1, brother_1_dend2, sister_2_dend1, sister_2_dend2, L = 1)
475
476 # entanglement reduced from 0.866 to 0.045
477 initial_entanglement_dend1 <- entanglement(brother_1_dend1, brother_1_dend2)
478 expect_identical(round(initial_entanglement_dend1, 3), 0.866)
479 entanglement_child1 <- entanglement(result[[1]], result[[2]])
480 expect_identical(round(entanglement_child1, 3), 0.045)
481
482 # entanglement reduced from 0.391 to 0
483 initial_entanglement_dend2 <- entanglement(sister_2_dend1, sister_2_dend2)
484 expect_identical(round(initial_entanglement_dend2, 3), 0.391)
485 entanglement_child2 <- entanglement(result[[3]], result[[4]])
486 expect_identical(round(entanglement_child2, 3), 0)
487 })
488
489
490
491
492 test_that("entanglement_return_best_brother works", {
493 suppressWarnings(RNGversion("3.5.0"))
494 set.seed(1)
495
496 brother_1_dend1 <- as.dendrogram(hclust(dist(iris[1:10, -5]), method = "complete"))
497 brother_1_dend2 <- as.dendrogram(hclust(dist(iris[10:1, -5]), method = "single"))
498
499 brother_2_dend1 <- as.dendrogram(hclust(dist(iris[11:20, -5]), method = "average"))
500 brother_2_dend2 <- as.dendrogram(hclust(dist(iris[20:11, -5]), method = "ward.D2"))
501
502 result <- entanglement_return_best_brother(brother_1_dend1, brother_1_dend2, brother_2_dend1, brother_2_dend2, L = 1)
503 # brother_1 is more entangled, therefore we expect to get brother_2 in result
504 expect_true(entanglement(brother_1_dend1, brother_1_dend2) > entanglement(brother_2_dend1, brother_2_dend2))
505 expect_identical(result, dendlist(brother_2_dend1, brother_2_dend2))
506
507 # if brother_2 is more entangled
508 expect_no_error(
509 entanglement_return_best_brother(brother_2_dend1, brother_2_dend2, brother_1_dend1, brother_1_dend2, L = 1)
510 )
511 })
512
513
514
515
516 test_that("untangle_intercourse_evolution works", {
517 suppressWarnings(RNGversion("3.5.0"))
518 set.seed(1)
519
520 brother_1_dend1 <- as.dendrogram(hclust(dist(iris[1:10, -5]), method = "complete"))
521 brother_1_dend2 <- as.dendrogram(hclust(dist(iris[10:1, -5]), method = "single"))
522
523 brother_2_dend1 <- as.dendrogram(hclust(dist(iris[11:20, -5]), method = "average"))
524 brother_2_dend2 <- as.dendrogram(hclust(dist(iris[20:11, -5]), method = "ward.D2"))
525
526 intercourse = dendlist(brother_1_dend1, brother_1_dend2, brother_2_dend1, brother_2_dend2)
527 result <- untangle_intercourse_evolution(intercourse, L = 1)
528 # brother_1 is more entangled, therefore we expect to get brother_2 in result
529 expect_true(entanglement(brother_1_dend1, brother_1_dend2) > entanglement(brother_2_dend1, brother_2_dend2))
530 expect_identical(result, dendlist(brother_2_dend1, brother_2_dend2))
531 })
532
533
534
535
536 test_that("untangle_evolution works", {
537 suppressWarnings(RNGversion("3.5.0"))
538 set.seed(1)
539
540 brother_1_dend1 <- as.dendrogram(hclust(dist(iris[1:10, -5]), method = "complete"))
541 brother_1_dend2 <- as.dendrogram(hclust(dist(iris[10:1, -5]), method = "single"))
542
543 sister_2_dend1 <- as.dendrogram(hclust(dist(iris[11:20, -5]), method = "average"))
544 sister_2_dend2 <- as.dendrogram(hclust(dist(iris[20:11, -5]), method = "ward.D2"))
545
546 # determine which set of dendrograms can be better untangled
547 intercourse_result <- untangle_intercourse(brother_1_dend1, brother_1_dend2, sister_2_dend1, sister_2_dend2, L = 1)
548 entanglement_child1 <- entanglement(intercourse_result[[1]], intercourse_result[[2]])
549 entanglement_child2 <- entanglement(intercourse_result[[3]], intercourse_result[[4]])
550 expect_true(entanglement_child1 > entanglement_child2)
551 # the untangled version of the better dendrograms should be the result
552 evolution_result <- untangle_evolution(brother_1_dend1, brother_1_dend2, brother_1_dend1, brother_1_dend2, L = 1)
553 expect_identical(evolution_result, dendlist(intercourse_result[[1]], intercourse_result[[2]]))
554 })
555
556
557