Skip to content

Commit

Permalink
Replaced dgCMatrix with CsparseMatrix in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarbo committed Aug 24, 2022
1 parent 59edce1 commit de2d7dd
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Encoding: UTF-8
Type: Package
Package: fastTopics
Version: 0.6-138
Version: 0.6-139
Date: 2022-08-24
Title: Fast Algorithms for Fitting Topic Models and Non-Negative
Matrix Factorizations to Count Data
Expand Down
2 changes: 1 addition & 1 deletion R/fit_poisson_nmf.R
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ fit_poisson_nmf <- function (X, k, fit0, numiter = 100,
method.text <- "CCD"
cat(sprintf("Running %d %s updates, %s extrapolation ",numiter,
method.text,ifelse(control$extrapolate,"with","without")))
cat("(fastTopics 0.6-138).\n")
cat("(fastTopics 0.6-139).\n")
}

# INITIALIZE ESTIMATES
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test_de_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test_that(paste("All variants of fit_poisson_models should produce the",
out <- add_pseudocounts(X,s*L,0.1)
X <- out$X
L <- out$L
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")

# Fit the univariate Poisson models using glm and scd.
F1 <- fit_poisson_models(X,L,method = "glm")
Expand Down Expand Up @@ -115,7 +115,7 @@ test_that(paste("de_analysis with and without multithreading, using a",
k <- 4
dat <- simulate_multinom_gene_data(n,m,k,sparse = FALSE)
X <- dat$X
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")
L <- dat$L

# Run de_analysis twice, using the single-threaded computations (nc
Expand Down Expand Up @@ -161,7 +161,7 @@ test_that(paste("de_analysis with and without multithreading, using a",
k <- 4
dat <- simulate_multinom_gene_data(n,m,k,sparse = FALSE)
X <- dat$X
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")
L <- dat$L

# Run de_analysis twice, using the single-threaded computations (nc
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_fit_multinom_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test_that("fit_multinom_model gives correct factor estimates",{
k <- 3
out <- simulate_toy_gene_data(n,m,k,s = 1000)
X <- out$X
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")

# Force "hard" topic assignments.
cluster <- factor(apply(force_hard_topic_assignments(out$L),1,which.max))
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test_fit_poisson_nmf.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test_that(paste("multiplicative and EM updates produce same result, and",
control = list(numiter = 1,nc = nc)))

# Store the counts as a sparse matrix.
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")

# Run 20 EM updates a third time, this time using the sparse counts
# matrix.
Expand Down Expand Up @@ -165,7 +165,7 @@ test_that(paste("ccd and scd updates produce the same result, and",
control = list(numiter = 1,nc = 1)))

# Redo the SCD updates with a sparse matrix.
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")
capture.output(
fit3 <- fit_poisson_nmf(Y,fit0 = fit0,numiter = numiter,method = "scd",
control = list(numiter = 1,nc = 1)))
Expand Down Expand Up @@ -492,7 +492,7 @@ test_that("Fixed factors and loadings to not change (aside from rescaling)",{
k <- 3
out <- generate_test_data(n,m,k)
X <- out$X
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")

# Check that the factors remain the same (up to a rescaling) when
# update.factors is NULL.
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test_likelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that(paste("R and Rcpp versions of cost function return same result",
X <- out$X
F <- out$F
L <- out$L
Y <- as(X,"dgCMatrix")
Y <- as(X,"CsparseMatrix")

# Compute the loss function.
f1 <- cost(X,L,t(F),version = "R")
Expand Down Expand Up @@ -43,7 +43,7 @@ test_that(paste("loglik_poisson_nmf gives correct result for sparse and",
# Compute the log-likelikhood.
f1 <- loglik_poisson_nmf_with_dpois(X,fit)
f2 <- loglik_poisson_nmf(X,fit,e = 0)
f3 <- loglik_poisson_nmf(as(X,"dgCMatrix"),fit,e = 0)
f3 <- loglik_poisson_nmf(as(X,"CsparseMatrix"),fit,e = 0)
names(f1) <- rownames(X)

# The likelihood calculations should all be the same.
Expand All @@ -68,7 +68,7 @@ test_that(paste("loglik_multinom_topic_model gives correct result for",
# Compute the log-likelikhood.
f1 <- loglik_multinom_topic_model_with_dmultinom(X,poisson2multinom(fit))
f2 <- loglik_multinom_topic_model(X,fit,e = 0)
f3 <- loglik_multinom_topic_model(as(X,"dgCMatrix"),fit,e = 0)
f3 <- loglik_multinom_topic_model(as(X,"CsparseMatrix"),fit,e = 0)

# Compute the multinomial log-likelihood a different way: first
# compute the Poisson log-likelihood, then subtract out the
Expand Down Expand Up @@ -98,7 +98,7 @@ test_that(paste("deviance_poisson_topic_nmf gives correct result for sparse",
# Compute the deviances.
d1 <- deviance_poisson_nmf_with_poisson(X,fit)
d2 <- deviance_poisson_nmf(X,fit,e = 0)
d3 <- deviance_poisson_nmf(as(X,"dgCMatrix"),fit,e = 0)
d3 <- deviance_poisson_nmf(as(X,"CsparseMatrix"),fit,e = 0)

# The deviance calculations should all be the same.
expect_equal(d1,d2)
Expand All @@ -117,7 +117,7 @@ test_that("poisson_nmf_kkt gives same result for sparse and dense matrices",{

# Compute the KKT residuals, and check that they are the same.
out1 <- poisson_nmf_kkt(X,F,L)
out2 <- poisson_nmf_kkt(as(X,"dgCMatrix"),F,L)
out2 <- poisson_nmf_kkt(as(X,"CsparseMatrix"),F,L)
expect_equal(out1,out2,tolerance = 1e-15,scale = 1)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_poisson2multinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_that("multinom2poisson recovers original Poisson NMF model fit",{
fit2a$s <- NULL
fit3 <- multinom2poisson(fit2)
fit4 <- multinom2poisson(fit2a,X)
fit5 <- multinom2poisson(fit2a,as(X,"dgCMatrix"))
fit5 <- multinom2poisson(fit2a,as(X,"CsparseMatrix"))
Y1 <- with(fit1,tcrossprod(L,F))
Y3 <- with(fit3,tcrossprod(L,F))
Y4 <- with(fit4,tcrossprod(L,F))
Expand Down

0 comments on commit de2d7dd

Please sign in to comment.