From 84e0eb61d8e2f0a1970a0badc44f2844e6f4a2e6 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 28 Jul 2022 14:23:35 +0200 Subject: [PATCH 1/2] Translated `computeDiffInCliqCounts()` (#3) --- R/computeDiffInCliqCounts.R | 15 +++++++++++++++ tests/testthat/test-spatialMixture.R | 15 ++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 R/computeDiffInCliqCounts.R diff --git a/R/computeDiffInCliqCounts.R b/R/computeDiffInCliqCounts.R new file mode 100644 index 0000000..cf49d57 --- /dev/null +++ b/R/computeDiffInCliqCounts.R @@ -0,0 +1,15 @@ +computeDiffInCliqCounts <- function(cliques, inds) { + # Laskee muutoksen CLIQCOUNTS:ssa (tai SEPCOUNTS:ssa, jos syצtteen? + # separators) kun yksilצt inds siirretההn. diffInCliqcounts on ncliq*1 taulu, + # joka on CLIQCOUNTS:n sarakkeesta josta yksilצt inds siirretההn ja + # lisהttהv?sarakkeeseen, johon yksilצt siirretההn. + ncliq <- size(cliques, 1) + diffInCliqCounts <- zeros(ncliq, 1) + ninds <- length(inds) + for (i in 1:ninds) { + ind <- inds[i] + rivit <- rowSums(cliques == ind) + diffInCliqCounts <- diffInCliqCounts + rivit + } + return(diffInCliqCounts) +} diff --git a/tests/testthat/test-spatialMixture.R b/tests/testthat/test-spatialMixture.R index 969c2a0..133ee33 100644 --- a/tests/testthat/test-spatialMixture.R +++ b/tests/testthat/test-spatialMixture.R @@ -1,9 +1,14 @@ context("Spatial mixture") test_that("functions work with basic input", { - x <- c(1, 3, 0, 2) - y <- array(c(1, 3, 2, 4, 5, 7, 6, 8), c(2, 2, 2)) - z <- computeCounts(x, 4, 5, y) - expect_equal(z$cliqcounts, t(c(1, 1, 1, 0, 0))) - expect_equal(z$sepcounts, t(c(0, 0, 0, 1, 0))) + q <- c(1, 3, 0, 2) + w <- array(c(1, 3, 2, 4, 5, 7, 6, 8), c(2, 2, 2)) + e <- computeCounts(q, 4, 5, w) + r <- matrix(c(5, 3, 3, 6, 4, 4, 7, 9, 1), 3) + expect_equal(e$cliqcounts, t(c(1, 1, 1, 0, 0))) + expect_equal(e$sepcounts, t(c(0, 0, 0, 1, 0))) + expect_equal(computeDiffInCliqCounts(r, 4), matrix(c(0, 1, 1))) + expect_equal(computeDiffInCliqCounts(r, 3), matrix(c(0, 1, 1))) + expect_equal(computeDiffInCliqCounts(r, 5), matrix(c(1, 0, 0))) + expect_equal(computeDiffInCliqCounts(r, 0), matrix(c(0, 0, 0))) }) From 86a5dda6d6d20de681f24c5be0fe2e61d7b0564a Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 28 Jul 2022 14:43:04 +0200 Subject: [PATCH 2/2] Translated `mysetdiff()` (#3) --- R/mysetdiff.R | 18 ++++++++++++++++++ tests/testthat/test-spatialMixture.R | 3 +++ 2 files changed, 21 insertions(+) create mode 100644 R/mysetdiff.R diff --git a/R/mysetdiff.R b/R/mysetdiff.R new file mode 100644 index 0000000..2e4112f --- /dev/null +++ b/R/mysetdiff.R @@ -0,0 +1,18 @@ +mysetdiff <- function(A, B) { + # MYSETDIFF Set difference of two sets of positive integers (much faster than + # built - in setdiff) + # C <- mysetdiff(A, B) + # C <- A \ B = { things in A that are not in B } + # MATLAB Original by Kevin Murphy, modified by Leon Peshkin + if (is.null(A)) { + return(vector()) + } else if (is.null(B)) { + return(A) + } else { # both non-empty + bits <- zeros(1, base::max(base::max(A), base::max(B))) + bits[A] <- 1 + bits[B] <- 0 + C <- A[as.logical(bits[A])] + } + return(C) +} diff --git a/tests/testthat/test-spatialMixture.R b/tests/testthat/test-spatialMixture.R index 133ee33..0b638c6 100644 --- a/tests/testthat/test-spatialMixture.R +++ b/tests/testthat/test-spatialMixture.R @@ -5,10 +5,13 @@ test_that("functions work with basic input", { w <- array(c(1, 3, 2, 4, 5, 7, 6, 8), c(2, 2, 2)) e <- computeCounts(q, 4, 5, w) r <- matrix(c(5, 3, 3, 6, 4, 4, 7, 9, 1), 3) + t <- 1:5 + y <- c(4, 3, 1, 4, 8) expect_equal(e$cliqcounts, t(c(1, 1, 1, 0, 0))) expect_equal(e$sepcounts, t(c(0, 0, 0, 1, 0))) expect_equal(computeDiffInCliqCounts(r, 4), matrix(c(0, 1, 1))) expect_equal(computeDiffInCliqCounts(r, 3), matrix(c(0, 1, 1))) expect_equal(computeDiffInCliqCounts(r, 5), matrix(c(1, 0, 0))) expect_equal(computeDiffInCliqCounts(r, 0), matrix(c(0, 0, 0))) + expect_equal(mysetdiff(t, y), c(2, 5)) })