From eacb4e5e918f2ecaf6ec20ce2da52b2db584976d Mon Sep 17 00:00:00 2001 From: Michael Hahsler Date: Tue, 25 Jun 2024 10:18:11 -0500 Subject: [PATCH] Updated tests for testthat edition 3. --- NEWS.md | 4 + tests/testthat.R | 4 +- tests/testthat/test-apriori.R | 104 +------------------------ tests/testthat/test-associations.R | 9 --- tests/testthat/test-concise.R | 3 - tests/testthat/test-confint.R | 5 -- tests/testthat/test-crossTable.R | 3 - tests/testthat/test-discretize.R | 6 -- tests/testthat/test-eclat.R | 75 ++++++++++++++++++ tests/testthat/test-extract.R | 6 -- tests/testthat/test-fim4r.R | 4 - tests/testthat/test-interestMeasures.R | 12 +-- tests/testthat/test-itemCoding.R | 7 -- tests/testthat/test-itemMatrix.R | 6 -- tests/testthat/test-matrix.R | 5 -- tests/testthat/test-mine_concise.R | 32 ++++++++ tests/testthat/test-missing.R | 1 - tests/testthat/test-read_write.R | 11 +-- tests/testthat/test-ruleInduction.R | 6 -- tests/testthat/test-sets.R | 5 -- tests/testthat/test-tidLists.R | 6 -- tests/testthat/test-transactions.R | 5 -- tests/testthat/test-warm.R | 5 -- 23 files changed, 119 insertions(+), 205 deletions(-) create mode 100644 tests/testthat/test-eclat.R create mode 100644 tests/testthat/test-mine_concise.R diff --git a/NEWS.md b/NEWS.md index 0eb7695..ac29648 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ ## Bugfixes * Fixed 'Error in .basicRuleMeasure(..) unused argument (complement = TRUE)' reported by bachnguyen-tomo. + +## Changes +* Updated tests for testthat edition 3. # arules 1.7-7 (11/28/2023) @@ -12,6 +15,7 @@ * improved some error messages. * Added Wikipedia links to measures.md. * Added interest measure LIC. + ## Bugfixes * Fixed appearance code for fim4r (reported by mytarmail). diff --git a/tests/testthat.R b/tests/testthat.R index 6697230..0d7f4f8 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,4 +1,4 @@ library("testthat") - library("arules") -test_check("arules") \ No newline at end of file + +test_check("arules") diff --git a/tests/testthat/test-apriori.R b/tests/testthat/test-apriori.R index 1961b48..56f868c 100644 --- a/tests/testthat/test-apriori.R +++ b/tests/testthat/test-apriori.R @@ -1,8 +1,4 @@ -library("arules") -library("testthat") - verb <- FALSE - options(digits = 2) data <- list( @@ -17,17 +13,12 @@ data <- list( names(data) <- paste("Tr", c(1:7), sep = "") trans <- transactions(data) - -########################################################## -# test the APRIORI interface -context("APRIORI interface") - ### rules (all warnings are for low support) r <- apriori(trans, parameter = list(supp = 0.25, conf = 0), control = list(verb = verb)) -expect_identical(length(r), 18L) +expect_length(r, 18L) #r #summary(r) @@ -41,7 +32,7 @@ r <- apriori( control = list(verb = verb) ) -expect_identical(length(r), 6L) +expect_length(r, 6L) #inspect(r) r <- apriori( @@ -50,7 +41,7 @@ r <- apriori( appearance = list(lhs = c("a", "b"), rhs = "c"), control = list(verb = verb) ) -expect_identical(length(r), 2L) +expect_length(r, 2L) r <- apriori( trans, @@ -58,7 +49,7 @@ r <- apriori( appearance = list(none = c("a", "b")), control = list(verb = verb) ) -expect_identical(length(r), 3L) +expect_length(r, 3L) expect_error(as(list( rhs = c("a", "b"), @@ -82,71 +73,6 @@ r <- apriori( expect_true("coverage" %in% colnames(quality(r))) #inspect(r[1:2]) - - -########################################################## -# test the ECLAT interface - -context("ECLAT interface") - -f <- eclat(trans, control = list(verb = verb)) - -expect_identical(length(f), 20L) -sup <- c( - 0.14, - 0.14, - 0.14, - 0.29, - 0.29, - 0.29, - 0.14, - 0.14, - 0.29, - 0.14, - 0.14, - 0.14, - 0.14, - 0.29, - 0.57, - 0.71, - 0.71, - 0.43, - 0.43, - 0.29 -) -expect_equal(round(quality(f)$support, 2), sup) -expect_equal(labels(f)[5], "{a,d}") - -#f -#summary(f) -#inspect(f[1:2]) -#labels(f[1:2]) - - -### test subset -f.sub <- subset(f, subset = items %in% "a") -l <- labels(f.sub) -expect_equal(l, grep("a", l, value = T)) - -### test tidlists -f <- - eclat(trans, - parameter = list(tidLists = TRUE), - control = list(verb = verb)) - -#f -#summary(f) -tl <- tidLists(f) - -expect_identical(dim(tl), c(20L, 7L)) - -#tl -#summary(tl) -#inspect(tl) - -expect_equal(as(tl[5], "list"), list('{a,d}' = c("Tr3", "Tr7"))) - - ## Compare if APRIOR and ECLAT produce the same results data("Income") fsets <- @@ -165,25 +91,3 @@ esets <- ## compare if output is the same expect_true(all(table(match(fsets, esets)) == 1)) -########################################################## -# test maximal and closed itemset mining - -context("Maximal and closed itemsets") - -is_a_freq <- apriori(trans, parameter = list(target = "frequent"), control = list(verb = verb)) -is_a_max <- apriori(trans, parameter = list(target = "max"), control = list(verb = verb)) -is_a_closed <- apriori(trans, parameter = list(target = "closed"), control = list(verb = verb)) -is_a_gen <- apriori(trans, parameter = list(target = "generator"), control = list(verb = verb)) - -is_e_freq <- eclat(trans, parameter = list(target = "frequent"), control = list(verb = verb)) -is_e_max <- eclat(trans, parameter = list(target = "max"), control = list(verb = verb)) -is_e_closed <- eclat(trans, parameter = list(target = "closed"), control = list(verb = verb)) -is_e_gen <- eclat(trans, parameter = list(target = "generator"), control = list(verb = verb)) - -expect_true(setequal(is_a_freq, is_e_freq)) -expect_true(setequal(is_a_max, is_e_max)) -expect_true(setequal(is_a_closed, is_e_closed)) -expect_true(setequal(is_a_gen, is_e_gen)) - -#inspect(is_a_freq) -#inspect(is_e_freq) diff --git a/tests/testthat/test-associations.R b/tests/testthat/test-associations.R index 234bf7b..6ad062b 100644 --- a/tests/testthat/test-associations.R +++ b/tests/testthat/test-associations.R @@ -1,8 +1,3 @@ -library("testthat") -library("arules") - -context("associations") - set.seed(20070611) m <- matrix(as.integer(runif(100000) > 0.8), ncol = 20) @@ -13,7 +8,6 @@ t <- as(m, "transactions") expect_identical(dim(t), dim(m)) - r <- apriori(t, parameter = list(supp = 0.01, conf = 0.1), @@ -27,7 +21,6 @@ ss <- subset(r, subset = lift > 1.4 & lhs %in% "item3") expect_identical(labels(lhs(ss)), "{item2,item3}") expect_true(quality(ss)$lift > 1.4) - f <- eclat(t, parameter = list(supp = 0.01), control = list(verb = FALSE)) @@ -73,7 +66,6 @@ qual <- expect_warning(r <- rules(lhs, rhs, quality = qual)) #inspect(r) -context("subsetting") ## subsetting (also tests itemMatrix) # numeric @@ -175,7 +167,6 @@ expect_identical(r[2:10] %in% r, 2:10) expect_identical(match(f[2:10], f), 2:10) expect_identical(f[2:10] %in% f, 2:10) - expect_identical(rhs(r[1:10]) %pin% "item1", c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)) diff --git a/tests/testthat/test-concise.R b/tests/testthat/test-concise.R index 953615a..fc303e7 100644 --- a/tests/testthat/test-concise.R +++ b/tests/testthat/test-concise.R @@ -12,7 +12,6 @@ trans_list <- list( trans <- transactions(trans_list) its <- apriori(trans, support = 1/4, target = "frequent itemsets", control = list(verb = FALSE)) -context("is.generator") # with minsup = 1, frequent generator itemsets are: emptyset, b, c, d, bd, cd. ig <- is.generator(its) gens <- its[ig] @@ -25,7 +24,6 @@ expect_true(setequal(gens, gens_ap)) expect_true(setequal(gens, gens_ec)) -context("is.closed") # with minsup = 1, frequent closed itemsets are: a, ac, ad, abc, abcd. ic <- is.closed(its) expect_true(setequal(names(ic)[ic], c("{a}", "{a,c}", "{a,d}", "{a,b,c}", "{a,b,c,d}"))) @@ -33,7 +31,6 @@ expect_true(setequal(names(ic)[ic], c("{a}", "{a,c}", "{a,d}", "{a,b,c}", "{a,b, ic2 <- apriori(trans, support = 1/4, target = "closed frequent itemsets", control = list(verb = FALSE)) expect_true(setequal(its[ic], ic2)) -context("is.maximal") # im <- is.maximal(its) expect_true(setequal(names(im)[im], c("{a,b,c,d}"))) diff --git a/tests/testthat/test-confint.R b/tests/testthat/test-confint.R index e2a0b2a..305c694 100644 --- a/tests/testthat/test-confint.R +++ b/tests/testthat/test-confint.R @@ -1,10 +1,5 @@ -library("arules") -library("testthat") - debug <- FALSE -context("confint") - data("Adult") ## Mine association rules. rules <- apriori(Adult, diff --git a/tests/testthat/test-crossTable.R b/tests/testthat/test-crossTable.R index 4a38b2f..f855374 100644 --- a/tests/testthat/test-crossTable.R +++ b/tests/testthat/test-crossTable.R @@ -1,6 +1,3 @@ -library("testthat") -library("arules") - data(Groceries) itset <- new("itemsets", diff --git a/tests/testthat/test-discretize.R b/tests/testthat/test-discretize.R index cb452d5..9e6029d 100644 --- a/tests/testthat/test-discretize.R +++ b/tests/testthat/test-discretize.R @@ -1,11 +1,5 @@ -library("arules") -library("testthat") - - options(digits=2) -context("discretize") - nums1 <- sample(rep(0:10, time = 5)) nums2 <- diff --git a/tests/testthat/test-eclat.R b/tests/testthat/test-eclat.R new file mode 100644 index 0000000..dd27b35 --- /dev/null +++ b/tests/testthat/test-eclat.R @@ -0,0 +1,75 @@ +library("arules") +library("testthat") + +verb <- FALSE + +options(digits = 2) + +data <- list( + c("a", "b", "c"), + c("a", "b"), + c("a", "b", "d"), + c("b", "e"), + c("a", "c"), + c("c", "e"), + c("a", "b", "d", "e") +) +names(data) <- paste("Tr", c(1:7), sep = "") +trans <- transactions(data) + +f <- eclat(trans, control = list(verb = verb)) + +expect_identical(length(f), 20L) +sup <- c( + 0.14, + 0.14, + 0.14, + 0.29, + 0.29, + 0.29, + 0.14, + 0.14, + 0.29, + 0.14, + 0.14, + 0.14, + 0.14, + 0.29, + 0.57, + 0.71, + 0.71, + 0.43, + 0.43, + 0.29 +) +expect_equal(round(quality(f)$support, 2), sup) +expect_equal(labels(f)[5], "{a,d}") + +#f +#summary(f) +#inspect(f[1:2]) +#labels(f[1:2]) + + +### test subset +f.sub <- subset(f, subset = items %in% "a") +l <- labels(f.sub) +expect_equal(l, grep("a", l, value = T)) + +### test tidlists +f <- + eclat(trans, + parameter = list(tidLists = TRUE), + control = list(verb = verb)) + +#f +#summary(f) +tl <- tidLists(f) + +expect_identical(dim(tl), c(20L, 7L)) + +#tl +#summary(tl) +#inspect(tl) + +expect_equal(as(tl[5], "list"), list('{a,d}' = c("Tr3", "Tr7"))) diff --git a/tests/testthat/test-extract.R b/tests/testthat/test-extract.R index bb78f01..608b7df 100644 --- a/tests/testthat/test-extract.R +++ b/tests/testthat/test-extract.R @@ -1,13 +1,7 @@ -library("arules") -library("testthat") - -context("extract") - data("Adult") expect_equal(Adult, c(Adult[1:100], Adult[-(1:100)])) - ec <- eclat(Adult,control=list(verb=FALSE)) expect_equal(ec, c(ec[1:100], ec[-(1:100)])) diff --git a/tests/testthat/test-fim4r.R b/tests/testthat/test-fim4r.R index 8db841f..dc703d6 100644 --- a/tests/testthat/test-fim4r.R +++ b/tests/testthat/test-fim4r.R @@ -1,10 +1,6 @@ -library("arules") -library("testthat") - skip_if_not_installed("fim4r") verb <- FALSE - options(digits = 2) data <- list( diff --git a/tests/testthat/test-interestMeasures.R b/tests/testthat/test-interestMeasures.R index 30bdd4d..877ab35 100644 --- a/tests/testthat/test-interestMeasures.R +++ b/tests/testthat/test-interestMeasures.R @@ -1,8 +1,3 @@ -library("arules") -library("testthat") - -context("interestMeasures") - options(digits = 2) data <- list(c("A", "B"), @@ -110,8 +105,6 @@ m5 <- interestMeasure(rules[0], transactions = trans, reuse = TRUE) expect_equal(nrow(m5), 0L) ## is.redundant (this test does not help much)! -context("is.redundant") - red <- is.redundant(rules) imp <- interestMeasure(rules, measure = "improvement") expect_equal(red, imp <= 0) @@ -119,9 +112,7 @@ expect_equal(red, imp <= 0) #inspect(rules[!red]) #inspect(rules[red]) - -context("support") - +# test support s_tid <- support(rules, trans, control = list(method = "tidlist")) s_ptree <- support(rules, trans, control = list(method = "ptree")) expect_equal(s_tid, s_ptree) @@ -129,7 +120,6 @@ expect_equal(s_tid, quality(rules)$support) ## FIXME: test others -context("test with previous version") data("Adult") ## Mine association rules. rules <- apriori(Adult, diff --git a/tests/testthat/test-itemCoding.R b/tests/testthat/test-itemCoding.R index 6821cbd..cbfc9b9 100644 --- a/tests/testthat/test-itemCoding.R +++ b/tests/testthat/test-itemCoding.R @@ -1,8 +1,3 @@ -library("testthat") -library("arules") - -context("itemCoding") - data("Adult") list <- LIST(Adult[1:5], decode = FALSE) @@ -14,7 +9,6 @@ names(list_decoded2) <- NULL expect_equal(list_decoded1, list_decoded2) - ## Example 2: Manually create an itemMatrix data <- list( c("income=small", "age=Young"), @@ -23,7 +17,6 @@ data <- list( iM <- encode(data, itemLabels = Adult) - ### non existing item are dropped with a warning now. data2 <- list( c("income=small", "age=Young"), diff --git a/tests/testthat/test-itemMatrix.R b/tests/testthat/test-itemMatrix.R index 51be66e..36061ab 100644 --- a/tests/testthat/test-itemMatrix.R +++ b/tests/testthat/test-itemMatrix.R @@ -1,10 +1,5 @@ -library("arules") -library("testthat") - set.seed(20070611) -context("itemMatrix") - ### Generate random data and coerce data to itemMatrix. m <- matrix(runif(50)>0.8, ncol=5) dn <- list(paste("trans", seq(nrow(m)), sep=""), @@ -12,7 +7,6 @@ dn <- list(paste("trans", seq(nrow(m)), sep=""), dimnames(m) <- dn i <- as(m, "itemMatrix") - ### number of rows expect_identical(length(i), nrow(m)) expect_identical(dim(i), dim(m)) diff --git a/tests/testthat/test-matrix.R b/tests/testthat/test-matrix.R index c87f98b..5008c5f 100644 --- a/tests/testthat/test-matrix.R +++ b/tests/testthat/test-matrix.R @@ -1,8 +1,3 @@ -library("arules") -library("testthat") - -context("ngCMatrix") - e <- new("ngCMatrix") x <- new("ngCMatrix", p = as.integer(c(0,3,5,7,7,10)), i = as.integer(c(1,2,4,1,2,0,4,0,2,5)), diff --git a/tests/testthat/test-mine_concise.R b/tests/testthat/test-mine_concise.R new file mode 100644 index 0000000..bcd1480 --- /dev/null +++ b/tests/testthat/test-mine_concise.R @@ -0,0 +1,32 @@ +verb <- FALSE +options(digits = 2) + +data <- list( + c("a", "b", "c"), + c("a", "b"), + c("a", "b", "d"), + c("b", "e"), + c("a", "c"), + c("c", "e"), + c("a", "b", "d", "e") +) +names(data) <- paste("Tr", c(1:7), sep = "") +trans <- transactions(data) + +is_a_freq <- apriori(trans, parameter = list(target = "frequent"), control = list(verb = verb)) +is_a_max <- apriori(trans, parameter = list(target = "max"), control = list(verb = verb)) +is_a_closed <- apriori(trans, parameter = list(target = "closed"), control = list(verb = verb)) +is_a_gen <- apriori(trans, parameter = list(target = "generator"), control = list(verb = verb)) + +is_e_freq <- eclat(trans, parameter = list(target = "frequent"), control = list(verb = verb)) +is_e_max <- eclat(trans, parameter = list(target = "max"), control = list(verb = verb)) +is_e_closed <- eclat(trans, parameter = list(target = "closed"), control = list(verb = verb)) +is_e_gen <- eclat(trans, parameter = list(target = "generator"), control = list(verb = verb)) + +expect_true(setequal(is_a_freq, is_e_freq)) +expect_true(setequal(is_a_max, is_e_max)) +expect_true(setequal(is_a_closed, is_e_closed)) +expect_true(setequal(is_a_gen, is_e_gen)) + +#inspect(is_a_freq) +#inspect(is_e_freq) \ No newline at end of file diff --git a/tests/testthat/test-missing.R b/tests/testthat/test-missing.R index 90cd291..2b49038 100644 --- a/tests/testthat/test-missing.R +++ b/tests/testthat/test-missing.R @@ -1,5 +1,4 @@ ## FIXME: missing tests -## * itemCoding ## * aggregate ## * dissimilarity ## * pmml diff --git a/tests/testthat/test-read_write.R b/tests/testthat/test-read_write.R index e3534e9..abedeef 100644 --- a/tests/testthat/test-read_write.R +++ b/tests/testthat/test-read_write.R @@ -1,8 +1,3 @@ -library("arules") -library("testthat") - -context("read.transactions") - setwd(tempdir()) ## test skip and quote @@ -63,11 +58,7 @@ expect_equal(dim(tr), c(2L, 2L)) ## tidy up unlink("demo_single") - - - -context("write transactions") - +## Write transactions ## Note: write basket looses transactionID write(tr, file = "demo_write") tr2 <- read.transactions("demo_write") diff --git a/tests/testthat/test-ruleInduction.R b/tests/testthat/test-ruleInduction.R index 6246df8..c7148f3 100644 --- a/tests/testthat/test-ruleInduction.R +++ b/tests/testthat/test-ruleInduction.R @@ -1,7 +1,3 @@ -library("testthat") -library("arules") - - data <- list( c("a","b","c"), c("a","b"), @@ -14,8 +10,6 @@ data <- list( names(data) <- paste("Tr",c(1:7), sep = "") trans <- as(data, "transactions") -context("Rule Induction") - ### rules is <- apriori(trans, parameter=list(supp=0.25, target = "frequent"), control=list(verb=FALSE)) diff --git a/tests/testthat/test-sets.R b/tests/testthat/test-sets.R index 89019bf..cd366ba 100644 --- a/tests/testthat/test-sets.R +++ b/tests/testthat/test-sets.R @@ -1,8 +1,3 @@ -library("arules") -library("testthat") - -context("set operations") - data <- list( c("A", "B"), c("A", "B", "C", "G"), diff --git a/tests/testthat/test-tidLists.R b/tests/testthat/test-tidLists.R index 327cac1..2b2dad4 100644 --- a/tests/testthat/test-tidLists.R +++ b/tests/testthat/test-tidLists.R @@ -1,9 +1,3 @@ -library("arules") -library("testthat") - - -context("tidLists") - data <- list( c("a","b","c"), c("a","b"), diff --git a/tests/testthat/test-transactions.R b/tests/testthat/test-transactions.R index d936c41..58bebe7 100644 --- a/tests/testthat/test-transactions.R +++ b/tests/testthat/test-transactions.R @@ -1,8 +1,3 @@ -library("arules") -library("testthat") - -context("transactions") - data <- list( c("a","b","c"), c("a","b"), diff --git a/tests/testthat/test-warm.R b/tests/testthat/test-warm.R index 914931d..1584db5 100644 --- a/tests/testthat/test-warm.R +++ b/tests/testthat/test-warm.R @@ -1,8 +1,3 @@ -library("testthat") -library("arules") - -context("weighted association mining") - data("Income") ## find and some frequent itemsets