Skip to content

Commit e77b8ab

Browse files
committed
Add NeuroVecSeq matrix and dense coercions
1 parent f6e0517 commit e77b8ab

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

R/neurovecseq.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,57 @@ setMethod(f = "linear_access",
248248
out
249249
})
250250

251+
# Coerce NeuroVecSeq to matrix.
252+
setAs(from="NeuroVecSeq", to="matrix",
253+
function(from) {
254+
as.matrix(from)
255+
})
256+
257+
# Coerce NeuroVecSeq to DenseNeuroVec.
258+
setAs(from="NeuroVecSeq", to="DenseNeuroVec",
259+
function(from) {
260+
as.dense(from)
261+
})
262+
263+
# Coerce NeuroVecSeq to the plain in-memory NeuroVec representation.
264+
setAs(from="NeuroVecSeq", to="NeuroVec",
265+
function(from) {
266+
as.dense(from)
267+
})
268+
269+
#' Convert a NeuroVecSeq to a matrix
270+
#'
271+
#' @description
272+
#' Converts a \code{\linkS4class{NeuroVecSeq}} to a dense voxel-by-time matrix.
273+
#'
274+
#' @param x A NeuroVecSeq object
275+
#' @param ... Additional arguments
276+
#' @return A matrix with one row per voxel and one column per time point.
277+
#'
278+
#' @rdname as.matrix-methods
279+
#' @export
280+
setMethod(f="as.matrix", signature=signature(x="NeuroVecSeq"),
281+
def=function(x, ...) {
282+
mats <- lapply(x@vecs, as.matrix)
283+
do.call(cbind, mats)
284+
})
285+
286+
#' Convert a NeuroVecSeq to a DenseNeuroVec
287+
#'
288+
#' @description
289+
#' Materializes a \code{\linkS4class{NeuroVecSeq}} as a single
290+
#' \code{\linkS4class{DenseNeuroVec}} with the sequence's combined space.
291+
#'
292+
#' @param x A NeuroVecSeq object
293+
#' @return A DenseNeuroVec containing all sequence volumes concatenated in time.
294+
#'
295+
#' @rdname as.dense-methods
296+
#' @export
297+
setMethod(f="as.dense", signature=signature(x="NeuroVecSeq"),
298+
def=function(x) {
299+
DenseNeuroVec(as.matrix(x), space(x))
300+
})
301+
251302

252303
#' @rdname series-methods
253304
#' @export

man/as.dense-methods.Rd

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/as.matrix-methods.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-vecseq-coverage.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,37 @@ test_that("NeuroVecSeq length equals sum of time dims", {
5252
expect_equal(dim(vs)[4], 10L)
5353
})
5454

55+
test_that("as.matrix on NeuroVecSeq matches concatenated NeuroVec", {
56+
v1 <- make_test_vec(4L)
57+
v2 <- make_test_vec(6L)
58+
vs <- NeuroVecSeq(v1, v2)
59+
ref <- concat(v1, v2)
60+
61+
expect_equal(as.matrix(vs), as.matrix(ref))
62+
expect_equal(as(vs, "matrix"), as.matrix(ref))
63+
})
64+
65+
test_that("NeuroVecSeq can be materialized as DenseNeuroVec", {
66+
v1 <- make_test_vec(4L)
67+
v2 <- make_test_vec(6L)
68+
vs <- NeuroVecSeq(v1, v2)
69+
ref <- concat(v1, v2)
70+
71+
dense <- as.dense(vs)
72+
coerced <- as(vs, "DenseNeuroVec")
73+
plain <- as(vs, "NeuroVec")
74+
75+
expect_s4_class(dense, "DenseNeuroVec")
76+
expect_s4_class(coerced, "DenseNeuroVec")
77+
expect_s4_class(plain, "DenseNeuroVec")
78+
expect_false(inherits(plain, "NeuroVecSeq"))
79+
expect_equal(dim(dense), dim(ref))
80+
expect_equal(space(dense), space(ref))
81+
expect_equal(as.matrix(dense), as.matrix(ref))
82+
expect_equal(as.matrix(coerced), as.matrix(ref))
83+
expect_equal(as.matrix(plain), as.matrix(ref))
84+
})
85+
5586
# ---------------------------------------------------------------------------
5687
# [[ extraction
5788
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)