Skip to content

Commit 1d05f18

Browse files
Merge branch 'fix-as_refactor' into dev
2 parents cebeda0 + 7888fb3 commit 1d05f18

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(edgeList,SparseBlockMatrixR)
4+
S3method(sparse,SparseBlockMatrixR)
35
export(ccdr.run)
46
importFrom(Rcpp,sourceCpp)
7+
importFrom(sparsebnUtils,edgeList)
58
importFrom(sparsebnUtils,get.adjacency.matrix)
69
importFrom(sparsebnUtils,is.zero)
710
importFrom(sparsebnUtils,num.edges)

R/s3-SparseBlockMatrixR.R

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ is.SparseBlockMatrixR <- function(x){
5656
inherits(x, "SparseBlockMatrixR")
5757
} # END IS.SPARSEBLOCKMATRIXR
5858

59+
as.SparseBlockMatrixR <- function(x){
60+
SparseBlockMatrixR(x) # NOTE: S3 delegation is implicitly handled by the constructor here
61+
}
62+
5963
#------------------------------------------------------------------------------#
6064
# reIndexC.SparseBlockMatrixR
6165
# Re-indexing TO C for SparseBlockMatrixR objects
@@ -201,30 +205,6 @@ SparseBlockMatrixR.matrix <- function(x, sigmas, ...){
201205
SparseBlockMatrixR(sparsebnUtils::as.sparse(x), sigmas, ...)
202206
} # END SPARSEBLOCKMATRIXR.MATRIX
203207

204-
#------------------------------------------------------------------------------#
205-
# as.SparseBlockMatrixR.list
206-
# Convert FROM list TO SparseBlockMatrixR
207-
#
208-
as.SparseBlockMatrixR.list <- function(x){
209-
SparseBlockMatrixR(x)
210-
} # END AS.SPARSEBLOCKMATRIXR.LIST
211-
212-
#------------------------------------------------------------------------------#
213-
# as.SparseBlockMatrixR.sparse
214-
# Convert FROM sparse TO SparseBlockMatrixR
215-
#
216-
as.SparseBlockMatrixR.sparse <- function(x){
217-
SparseBlockMatrixR(x)
218-
} # END AS.SPARSEBLOCKMATRIXR.SPARSE
219-
220-
#------------------------------------------------------------------------------#
221-
# as.SparseBlockMatrixR.matrix
222-
# Convert FROM matrix TO SparseBlockMatrixR
223-
#
224-
as.SparseBlockMatrixR.matrix <- function(x){
225-
SparseBlockMatrixR(x)
226-
} # END AS.SPARSEBLOCKMATRIXR.MATRIX
227-
228208
#------------------------------------------------------------------------------#
229209
# as.list.SparseBlockMatrixR
230210
# Convert FROM SparseBlockMatrixR TO list
@@ -261,10 +241,11 @@ as.matrix.SparseBlockMatrixR <- function(x){
261241
} # END AS.MATRIX.SPARSEBLOCKMATRIXR
262242

263243
#------------------------------------------------------------------------------#
264-
# as.edgeList.SparseBlockMatrixR
244+
# edgeList.SparseBlockMatrixR
265245
# Coerce SBM to edge list
266246
#
267-
as.edgeList.SparseBlockMatrixR <- function(x){
247+
#' @export
248+
edgeList.SparseBlockMatrixR <- function(x){
268249
#
269250
# We have to be careful in obtaining the edge list of a SparseBlockMatrixR object:
270251
# It is NOT the same as the rows slot since some of these components may have
@@ -278,12 +259,13 @@ as.edgeList.SparseBlockMatrixR <- function(x){
278259
el <- mapply(function(x, y){ y[which(abs(x) > sparsebnUtils::zero_threshold())]}, x$vals, x$rows)
279260

280261
sparsebnUtils::edgeList(el)
281-
} # AS.EDGELIST.SPARSEBLOCKMATRIXR
262+
} # EDGELIST.SPARSEBLOCKMATRIXR
282263

283264
#------------------------------------------------------------------------------#
284265
# sparse.SparseBlockMatrixR
285266
# 2016-01-22: Migrated to this file from s3-sparse.R
286267
#
268+
#' @export
287269
sparse.SparseBlockMatrixR <- function(x, index = "R", ...){
288270

289271
if(index != "R" && index != "C") stop("Invalid entry for index parameter: Must be either 'R' or 'C'!")
@@ -321,16 +303,16 @@ sparse.SparseBlockMatrixR <- function(x, index = "R", ...){
321303
}
322304
} # END SPARSE.SPARSEBLOCKMATRIXR
323305

324-
#------------------------------------------------------------------------------#
325-
# as.sparse.SparseBlockMatrixR
326-
# Convert FROM SparseBlockMatrixR TO sparse
327-
# By default, return the object using R indexing. If desired, the method can return C-style indexing by setting
328-
# index = "C".
329-
# 2016-01-22: Migrated to this file from s3-sparse.R
330-
#
331-
as.sparse.SparseBlockMatrixR <- function(x, index = "R", ...){
332-
sparse.SparseBlockMatrixR(x, index)
333-
} # END AS.SPARSE.SPARSEBLOCKMATRIXR
306+
# #------------------------------------------------------------------------------#
307+
# # as.sparse.SparseBlockMatrixR
308+
# # Convert FROM SparseBlockMatrixR TO sparse
309+
# # By default, return the object using R indexing. If desired, the method can return C-style indexing by setting
310+
# # index = "C".
311+
# # 2016-01-22: Migrated to this file from s3-sparse.R
312+
# #
313+
# as.sparse.SparseBlockMatrixR <- function(x, index = "R", ...){
314+
# sparse.SparseBlockMatrixR(x, index)
315+
# } # END AS.SPARSE.SPARSEBLOCKMATRIXR
334316

335317
# to_graphNEL.SparseBlockMatrixR
336318
# Convert SBM object to graphNEL object
@@ -347,7 +329,7 @@ to_graphNEL.SparseBlockMatrixR <- function(x){
347329
} # END TO_GRAPHNEL.SPARSEBLOCKMATRIXR
348330

349331
get.adjacency.matrix.SparseBlockMatrixR <- function(x){
350-
sparsebnUtils::get.adjacency.matrix(as.edgeList.SparseBlockMatrixR(x))
332+
sparsebnUtils::get.adjacency.matrix(as.edgeList(x))
351333
} # END GET.ADJACENCY.MATRIX.SPARSEBLOCKMATRIXR
352334

353335
num.nodes.SparseBlockMatrixR <- function(x){
@@ -357,7 +339,7 @@ num.nodes.SparseBlockMatrixR <- function(x){
357339

358340
num.edges.SparseBlockMatrixR <- function(x){
359341
### The number of nodes should be exactly the same as the length of the rows list
360-
sparsebnUtils::num.edges(as.edgeList.SparseBlockMatrixR(x))
342+
sparsebnUtils::num.edges(as.edgeList(x))
361343
} # END NUM.EDGES.SPARSEBLOCKMATRIXR
362344

363345
# This function is (so far) only used in unit tests

R/s3-generics.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@
1414

1515
# Generics for SparseBlockMatrixR ------------------------------------------------------------
1616
SparseBlockMatrixR <- function(x, ...) UseMethod("SparseBlockMatrixR", x)
17-
as.SparseBlockMatrixR <- function(x) UseMethod("as.SparseBlockMatrixR", x)
1817
to_B <- function(x) UseMethod("to_B", x)

R/zzz.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#' @importFrom sparsebnUtils num.nodes
1515
#' @importFrom sparsebnUtils num.edges
1616
#' @importFrom sparsebnUtils is.zero
17+
#' @importFrom sparsebnUtils edgeList
18+
#' @importFrom sparsebnUtils sparse
1719

1820
.onAttach <- function(libname, pkgname){
1921
### Only sparsebn needs a package startup message

tests/testthat/test-s3_as.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ test_that("as.sparse -> as.SparseBlockMatrixR -> as.sparse makes no changes", {
7777
m <- matrix(rep(0, 1), ncol = 1)
7878
sp <- sparsebnUtils::as.sparse(m)
7979
sbm <- suppressWarnings(sparsebnUtils::as.sparse(as.SparseBlockMatrixR(sp)))
80-
expect_that(sbm, equals(sp))
80+
expect_equivalent(sbm, sp)
8181

8282
m <- matrix(rep(0, 4), ncol = 2)
8383
sp <- sparsebnUtils::as.sparse(m)
8484
sbm <- suppressWarnings(sparsebnUtils::as.sparse(as.SparseBlockMatrixR(sp)))
85-
expect_that(sbm, equals(sp))
85+
expect_equivalent(sbm, sp)
8686

8787
### NOTE: Cannot test on random sparse matrix since SBM class ASSUMES a block structure,
8888
### i.e. induced by a DAG
@@ -91,7 +91,7 @@ test_that("as.sparse -> as.SparseBlockMatrixR -> as.sparse makes no changes", {
9191
m <- random.dag.matrix(10, 10)
9292
sp <- sparsebnUtils::as.sparse(m)
9393
sbm <- suppressWarnings(sparsebnUtils::as.sparse(as.SparseBlockMatrixR(sp)))
94-
expect_that(sbm, equals(sp))
94+
expect_equivalent(sbm, sp)
9595
})
9696

9797
### SparseBlockMatrixR -> edgeList -> matrix

0 commit comments

Comments
 (0)