Skip to content

Commit

Permalink
Merge pull request #24 from stephenslab/eweine/add_dgTmatrix
Browse files Browse the repository at this point in the history
Added support for dgTmatrix.
  • Loading branch information
pcarbo authored Jun 26, 2024
2 parents d8a963d + 9e496fd commit e81975e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Encoding: UTF-8
Type: Package
Package: fastglmpca
Version: 0.1-104
Version: 0.1-105
Date: 2024-01-30
Title: Fast Algorithms for Generalized Principal Component Analysis
Authors@R: c(person("Eric","Weine",role=c("aut","cre"),
Expand Down Expand Up @@ -44,4 +44,4 @@ LazyData: true
LazyDataCompression: xz
NeedsCompilation: yes
VignetteBuilder: knitr
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
2 changes: 1 addition & 1 deletion R/argument_checking.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Return true if x is a compressed, sparse, column-oriented numeric
# matrix.
is.sparse.matrix <- function (x)
inherits(x,"dgCMatrix") && is.numeric(x@x)
inherits(x,"dsparseMatrix") && is.numeric(x@x)

# Verify that x is matrix with finite, numeric entries.
verify.matrix <- function (x, arg.name = deparse(substitute(x))) {
Expand Down
2 changes: 1 addition & 1 deletion R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
#'
#' @param Y The n x m matrix of counts; all entries of \code{Y} should
#' be non-negative. It can be a sparse matrix (class
#' \code{"dgCMatrix"}) or dense matrix (class \code{"matrix"}).
#' \code{"dsparseMatrix"}) or dense matrix (class \code{"matrix"}).
#'
#' @param K Integer 1 or greater specifying the rank of the matrix
#' factorization. This should only be provided if the initial fit
Expand Down
2 changes: 1 addition & 1 deletion man/fit_glmpca_pois.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/testthat/test_fit_glmpca_pois.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,44 @@ test_that("Test fit works with input covariates",{
expect_equivalent(crossprod(fit_quick$U),diag(3),scale = 1,tolerance = 1e-8)
expect_equivalent(crossprod(fit_quick$V),diag(3),scale = 1,tolerance = 1e-8)
})

test_that("Results are the same with different classes of sparse matrices",{

# Simulate a 100 x 200 data set to factorize.
set.seed(1)
n <- 100
m <- 200
Y <- generate_glmpca_data_pois(n,m,K = 3)$Y

Y_dgC <- as(Y, "CsparseMatrix")
Y_dgT <- as(Y, "dgTMatrix")
Y_dgR <- as(Y, "dgRMatrix")

set.seed(1)
fit0_dgC <- init_glmpca_pois(Y_dgC,K = 3)
set.seed(1)
fit0_dgT <- init_glmpca_pois(Y_dgT,K = 3)
set.seed(1)
fit0_dgR <- init_glmpca_pois(Y_dgR,K = 3)

suppressWarnings(capture.output(
fit_quick_dgC <- fit_glmpca_pois(Y_dgC,fit0 = fit0_dgC,
control = list(maxiter = 20))))

suppressWarnings(capture.output(
fit_quick_dgT <- fit_glmpca_pois(Y_dgT,fit0 = fit0_dgT,
control = list(maxiter = 20))))

suppressWarnings(capture.output(
fit_quick_dgR <- fit_glmpca_pois(Y_dgR,fit0 = fit0_dgR,
control = list(maxiter = 20))))

expect_equal(fit_quick_dgC$progress$loglik, fit_quick_dgR$progress$loglik)
expect_equal(fit_quick_dgC$progress$loglik, fit_quick_dgT$progress$loglik)
expect_equal(fit_quick_dgC$U, fit_quick_dgR$U)
expect_equal(fit_quick_dgC$d, fit_quick_dgR$d)
expect_equal(fit_quick_dgC$V, fit_quick_dgR$V)
expect_equal(fit_quick_dgC$U, fit_quick_dgT$U)
expect_equal(fit_quick_dgC$d, fit_quick_dgT$d)
expect_equal(fit_quick_dgC$V, fit_quick_dgT$V)
})

0 comments on commit e81975e

Please sign in to comment.