|
| 1 | +library("TreeTools", quietly = TRUE) |
| 2 | + |
1 | 3 | test_that("Hierarchical Mutual Information", { |
2 | | - skip_if_not_installed("TreeTools") |
3 | | - library("TreeTools", quietly = TRUE) |
4 | 4 |
|
5 | 5 | # Create test trees |
6 | 6 | tree1 <- BalancedTree(8) |
@@ -38,97 +38,12 @@ test_that("Hierarchical Mutual Information", { |
38 | 38 | expect_equal(hmi_self_norm, 1, tolerance = 1e-10) |
39 | 39 |
|
40 | 40 | # Test error handling |
41 | | - expect_error(HierarchicalMutualInfo(tree1), "tree2 must be provided") |
| 41 | + expect_equal(HierarchicalMutualInfo(tree1), SelfHMI(tree1)) |
| 42 | + expect_warning(expect_equal(HierarchicalMutualInfo(tree1, norm = TRUE), 1), |
| 43 | + "tree2") |
42 | 44 |
|
43 | 45 | # Test with different tip numbers (should error) |
44 | 46 | tree_small <- BalancedTree(6) |
45 | | - expect_error(HierarchicalMutualInfo(tree1, tree_small)) |
46 | | - |
47 | | - # Test reportMatching |
48 | | - hmi_with_matching <- HierarchicalMutualInfo(tree1, tree2, reportMatching = TRUE) |
49 | | - expect_true(is.numeric(hmi_with_matching)) |
50 | | - expect_true("matching" %in% names(attributes(hmi_with_matching))) |
51 | | - |
52 | | - # Test expected value for bal6 vs pec6 (should be approximately 0.24) |
53 | | - bal6 <- BalancedTree(6) |
54 | | - pec6 <- PectinateTree(6) |
55 | | - hmi_bal_pec <- HierarchicalMutualInfo(bal6, pec6) |
56 | | - |
57 | | - # The expected value is 0.24 based on Python reference implementation |
58 | | - expect_equal(hmi_bal_pec, 0.24, tolerance = 0.02) |
59 | | -}) |
60 | | - |
61 | | -test_that("HMI helper functions", { |
62 | | - skip_if_not_installed("TreeTools") |
63 | | - library("TreeTools", quietly = TRUE) |
64 | | - |
65 | | - tree <- BalancedTree(8) |
66 | | - |
67 | | - # Test hierarchical partition building |
68 | | - partition <- as.HPart(tree) |
69 | | - |
70 | | - expect_true(is.list(partition)) |
71 | | - |
72 | | - # Test HMI recursive calculation |
73 | | - tree2 <- PectinateTree(8) |
74 | | - partition2 <- as.HPart(tree2) |
75 | | - |
76 | | - result <- .CalculateHMIRecursive(partition, partition2) |
77 | | - expect_true(is.list(result)) |
78 | | - expect_true("n_ts" %in% names(result)) |
79 | | - expect_true("I_ts" %in% names(result)) |
80 | | - expect_true(is.numeric(result$n_ts)) |
81 | | - expect_true(is.numeric(result$I_ts)) |
82 | | - expect_true(result$n_ts >= 0) |
83 | | - expect_true(result$I_ts >= 0) |
84 | | -}) |
85 | | - |
86 | | -test_that("HMI comparison with standard mutual information", { |
87 | | - library("TreeTools", quietly = TRUE) |
88 | | - |
89 | | - tree1 <- BalancedTree(8) |
90 | | - tree2 <- PectinateTree(8) |
91 | | - |
92 | | - # Compare HMI with some basic principles |
93 | | - hmi <- HierarchicalMutualInfo(tree1, tree2) |
94 | | - |
95 | | - # Both should be positive for different trees |
96 | | - expect_true(hmi >= 0) |
97 | | - |
98 | | - # Test with identical trees |
99 | | - hmi_identical <- HierarchicalMutualInfo(tree1, tree1) |
100 | | - |
101 | | - expect_true(hmi_identical >= 0) |
102 | | - expect_true(is.numeric(hmi)) |
103 | | - expect_true(is.numeric(hmi_identical)) |
104 | | -}) |
105 | | - |
106 | | -test_that("HMI with list inputs", { |
107 | | - library("TreeTools", quietly = TRUE) |
108 | | - |
109 | | - trees <- list( |
110 | | - BalancedTree(8), |
111 | | - PectinateTree(8), |
112 | | - RandomTree(8, 1) |
113 | | - ) |
114 | | - |
115 | | - # Test with list input |
116 | | - hmi_result <- HierarchicalMutualInfo(trees) |
117 | | - |
118 | | - expect_true(inherits(hmi_result, "dist")) |
119 | | - expect_equal(length(hmi_result), 3) # 3 pairwise distances for 3 trees |
120 | | - |
121 | | - # Convert to full matrix to test properties |
122 | | - hmi_matrix <- as.matrix(hmi_result) |
123 | | - expect_equal(dim(hmi_matrix), c(3, 3)) |
124 | | - |
125 | | - # Matrix should be symmetric |
126 | | - expect_equal(hmi_matrix[1, 2], hmi_matrix[2, 1], tolerance = 1e-10) |
127 | | - expect_equal(hmi_matrix[1, 3], hmi_matrix[3, 1], tolerance = 1e-10) |
128 | | - expect_equal(hmi_matrix[2, 3], hmi_matrix[3, 2], tolerance = 1e-10) |
129 | | - |
130 | | - # Diagonal should be zero (distance from tree to itself in distance matrix) |
131 | | - expect_equal(hmi_matrix[1, 1], 0, tolerance = 1e-10) |
132 | | - expect_equal(hmi_matrix[2, 2], 0, tolerance = 1e-10) |
133 | | - expect_equal(hmi_matrix[3, 3], 0, tolerance = 1e-10) |
| 47 | + expect_error(HierarchicalMutualInfo(tree1, tree_small), |
| 48 | + "number of leaves") |
134 | 49 | }) |
0 commit comments