| 1 |
# library(testthat) |
| 2 |
|
| 3 |
context("labels assignment") |
| 4 |
|
| 5 |
test_that("labels assginment works for vectors", { |
| 6 |
x <- 1:3 |
| 7 |
# expect_that(labels(x), equals(as.character(1:3))) # notice this is 1:3 as character |
| 8 |
expect_equal(labels(x), (as.character(1:3))) # notice this is 1:3 as character |
| 9 |
|
| 10 |
labels(x) <- letters[1:3] |
| 11 |
expect_equal(labels(x), (letters[1:3])) |
| 12 |
# labels(x) # [1] "a" "b" "c" |
| 13 |
labels(x)[1] <- "one" |
| 14 |
expect_equal(labels(x), c("one", "b", "c")) # checking specific assignment |
| 15 |
}) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
# test_that("labels assginment works for matrix",{ |
| 20 |
# y <- matrix(1:9, 3,3) |
| 21 |
# expect_equal(labels(y), (NULL)) |
| 22 |
|
| 23 |
# labels(y) <- letters[1:3] # defaults to assign labels to columns |
| 24 |
# expect_equal(labels(y), (letters[1:3])) |
| 25 |
# expect_equal(colnames(y), (letters[1:3])) |
| 26 |
|
| 27 |
# labels(y, which = "rownames") <- letters[24:26] |
| 28 |
# expect_equal(labels(y, which = "rownames"), letters[24:26]) |
| 29 |
# expect_equal(rownames(y), letters[24:26]) |
| 30 |
|
| 31 |
# labels(y)[1] <- "one" |
| 32 |
# expect_equal(labels(y), c("one", "b", "c")) # checking specific assignment |
| 33 |
# }) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
test_that("labels (with order=TRUE, by default), before and after assginment, works for hclust", { |
| 39 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 40 |
|
| 41 |
# plot(hc) |
| 42 |
# expect_that(labels(hc), equals(c("Alabama", "Alaska", "Arizona"))) |
| 43 |
expect_equal(labels(hc), c("Arizona", "Alabama", "Alaska")) |
| 44 |
|
| 45 |
labels(hc) <- letters[1:3] |
| 46 |
expect_equal(labels(hc), letters[1:3]) |
| 47 |
|
| 48 |
labels(hc)[1] <- "one" |
| 49 |
expect_equal(labels(hc), c("one", "b", "c")) # checking specific assignment |
| 50 |
}) |
| 51 |
|
| 52 |
|
| 53 |
test_that("labels (without order!) works for hclust", { |
| 54 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 55 |
# plot(hc) |
| 56 |
# expect_that(labels(hc, order = FALSE), equals(c("Alabama", "Alaska", "Arizona"))) |
| 57 |
expect_equal(labels(hc, order = FALSE), c("Alabama", "Alaska", "Arizona")) |
| 58 |
}) |
| 59 |
|
| 60 |
|
| 61 |
test_that("labels (without order!) works differently than labels assignment (which are WITH order) for hclust", { |
| 62 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 63 |
# plot(hc) |
| 64 |
# expect_that(labels(hc, order = FALSE), equals(c("Alabama", "Alaska", "Arizona"))) |
| 65 |
expect_equal(labels(hc, order = FALSE), c("Alabama", "Alaska", "Arizona")) |
| 66 |
|
| 67 |
labels(hc) <- letters[1:3] |
| 68 |
|
| 69 |
# expect_that(identical(labels(hc, order = FALSE) , letters[1:3]), |
| 70 |
# is_false()) |
| 71 |
expect_false(identical(labels(hc, order = FALSE), letters[1:3])) |
| 72 |
}) |
| 73 |
|
| 74 |
|
| 75 |
test_that("labels assginment works for dendrogram", { |
| 76 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 77 |
dend <- as.dendrogram(hc) |
| 78 |
|
| 79 |
expect_equal(labels(dend), c("Arizona", "Alabama", "Alaska")) |
| 80 |
|
| 81 |
labels(dend) <- letters[1:3] |
| 82 |
expect_equal(labels(dend), letters[1:3]) |
| 83 |
|
| 84 |
labels(dend)[1] <- "one" |
| 85 |
expect_equal(labels(dend), c("one", "b", "c")) # checking specific assignment |
| 86 |
|
| 87 |
# if 'value' parameter missing |
| 88 |
dendextend_options("warn", T) |
| 89 |
expect_warning( |
| 90 |
`labels<-`(dend) |
| 91 |
) |
| 92 |
dendextend_options("warn", F) |
| 93 |
}) |
| 94 |
|
| 95 |
|
| 96 |
test_that("labels for hclust and dendrogram are (by default) the same", { |
| 97 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 98 |
dend <- as.dendrogram(hc) |
| 99 |
# hc and dend labels should NOT be identical. |
| 100 |
expect_true(identical(labels(dend), labels(hc))) |
| 101 |
}) |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
test_that("labels assginment recycles properly and consistently", { |
| 107 |
x <- 1:3 |
| 108 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 109 |
dend <- as.dendrogram(hc) |
| 110 |
# y <- matrix(1:9, 3,3) |
| 111 |
|
| 112 |
suppressWarnings({ |
| 113 |
labels(x) <- letters[1] |
| 114 |
labels(hc) <- letters[1] |
| 115 |
labels(dend) <- letters[1] |
| 116 |
# labels(y) <- letters[1] # defaults to assign labels to columns |
| 117 |
# labels(y, which = "rownames") <- letters[24] |
| 118 |
}) |
| 119 |
|
| 120 |
expect_equal(labels(x), (rep(letters[1], 3))) |
| 121 |
expect_equal(labels(hc), (rep(letters[1], 3))) |
| 122 |
expect_equal(labels(dend), (rep(letters[1], 3))) |
| 123 |
# expect_equal(labels(y), (rep(letters[1], 3))) |
| 124 |
# expect_equal(labels(y, which = "rownames"), (rep(letters[24], 3))) |
| 125 |
# labels(x) # [1] "a" "b" "c" |
| 126 |
}) |
| 127 |
|
| 128 |
|
| 129 |
test_that("labels assginment issues warning when using recycling", { |
| 130 |
x <- 1:3 |
| 131 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 132 |
dend <- as.dendrogram(hc) |
| 133 |
# y <- matrix(1:9, 3,3) |
| 134 |
|
| 135 |
# expect_that(labels(x) <- letters[1], gives_warning()) |
| 136 |
expect_warning(labels(x) <- letters[1]) |
| 137 |
expect_warning(labels(hc) <- letters[1]) |
| 138 |
expect_warning(labels(dend) <- letters[1]) |
| 139 |
# expect_warning(labels(y) <- letters[1]) |
| 140 |
# expect_warning(labels(y, which = "rownames") <- letters[24]) |
| 141 |
}) |
| 142 |
|
| 143 |
|
| 144 |
test_that("labels assginment to dendrogram keeps the child nodes as NOT of dendrogram class", { |
| 145 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 146 |
dend <- as.dendrogram(hc) |
| 147 |
labels(dend) <- letters[1:3] |
| 148 |
|
| 149 |
expect_true(class(unclass(dend)[[2]]) == "list") |
| 150 |
}) |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
test_that("order of leaves in sub-dendrogram and as.hclust", { |
| 155 |
# For example: |
| 156 |
hc <- hclust(dist(USArrests[1:6, ]), "ave") |
| 157 |
dend <- as.dendrogram(hc) |
| 158 |
sub_dend <- dend[[1]] |
| 159 |
|
| 160 |
# bad order |
| 161 |
expect_equal(order.dendrogram(sub_dend), c(4, 6)) |
| 162 |
# bad labels: |
| 163 |
# expect_equal(labels(as.hclust(sub_dend)), as.character(rep(NA,2))) |
| 164 |
# since R 3.1.1-patched - the above will produce an error. (as it should) |
| 165 |
|
| 166 |
# let's fix it: |
| 167 |
order.dendrogram(sub_dend) <- rank(order.dendrogram(sub_dend), ties.method = "first") |
| 168 |
expect_equal(labels(as.hclust(sub_dend)), c("Arkansas", "Colorado")) |
| 169 |
# We now have labels :) |
| 170 |
}) |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
test_that("labels of hclust on data without rownames is an integer", { |
| 175 |
# this is a problem when some function assumes that labels are "character" |
| 176 |
DATA <- USArrests |
| 177 |
rownames(DATA) <- NULL |
| 178 |
hc <- hclust(dist(DATA)) |
| 179 |
dend <- as.dendrogram(hc) |
| 180 |
expect_true(length(labels(hc)) == 0) # hc has NO labels |
| 181 |
expect_true(is.integer(labels(dend))) # the labels the dend gets are Integer |
| 182 |
}) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
test_that("order of leaves can be extracted and changed", { |
| 190 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 191 |
dend <- as.dendrogram(hc) |
| 192 |
|
| 193 |
expect_identical(order.dendrogram(dend), c(3L, 1L, 2L)) |
| 194 |
|
| 195 |
# change order: |
| 196 |
order.dendrogram(dend) <- 1:3 |
| 197 |
expect_identical(order.dendrogram(dend), 1:3) |
| 198 |
|
| 199 |
# change order (with replications): |
| 200 |
expect_warning(order.dendrogram(dend) <- c(1, 2)) |
| 201 |
expect_identical(order.dendrogram(dend), as.integer(c(1, 2, 1))) |
| 202 |
}) |
| 203 |
|
| 204 |
|
| 205 |
test_that("labels assignment works for phylo objects", { |
| 206 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 207 |
ph <- ape::as.phylo(hc) |
| 208 |
|
| 209 |
labels(ph) <- c("Al", "Ak", "Az") |
| 210 |
expect_identical( |
| 211 |
labels.phylo(ph), |
| 212 |
c("Al", "Ak", "Az") |
| 213 |
) |
| 214 |
|
| 215 |
# if replacement labels are incorrect length |
| 216 |
expect_warning( |
| 217 |
labels(ph) <- c("Al", "Ak") |
| 218 |
) |
| 219 |
# if 'value' parameter missing |
| 220 |
dendextend_options("warn", T) |
| 221 |
expect_warning( |
| 222 |
`labels<-.phylo`(ph) |
| 223 |
) |
| 224 |
dendextend_options("warn", F) |
| 225 |
}) |
| 226 |
|
| 227 |
|
| 228 |
test_that("order.dendrogram works", { |
| 229 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 230 |
dend <- as.dendrogram(hc) |
| 231 |
|
| 232 |
# if non-numeric order passed in |
| 233 |
expect_warning( |
| 234 |
order.dendrogram(dend) <- c("1", "2", "3") |
| 235 |
) |
| 236 |
|
| 237 |
}) |
| 238 |
|
| 239 |
|
| 240 |
test_that("order.hclust works", { |
| 241 |
x <- dist(USArrests[1:3, ]) |
| 242 |
hc <- hclust(x, "ave") |
| 243 |
|
| 244 |
expect_equal( |
| 245 |
order.hclust(hc), |
| 246 |
c(3, 1, 2) |
| 247 |
) |
| 248 |
|
| 249 |
# if non-hclust object passed in |
| 250 |
expect_error( |
| 251 |
order.hclust(x) |
| 252 |
) |
| 253 |
|
| 254 |
}) |
| 255 |
|
| 256 |
|
| 257 |
test_that("place_labels works", { |
| 258 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 259 |
dend <- as.dendrogram(hc) |
| 260 |
|
| 261 |
result <- place_labels(dend, labels(dend)) |
| 262 |
expect_identical( |
| 263 |
labels(result), |
| 264 |
c("Alaska", "Arizona", "Alabama") |
| 265 |
) |
| 266 |
|
| 267 |
}) |