Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xyzmatrix2list xyzmatrix<-.list #485

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ S3method("xyzmatrix<-",default)
S3method("xyzmatrix<-",dotprops)
S3method("xyzmatrix<-",hxsurf)
S3method("xyzmatrix<-",igraph)
S3method("xyzmatrix<-",list)
S3method("xyzmatrix<-",mesh3d)
S3method("xyzmatrix<-",neuron)
S3method("xyzmatrix<-",neuronlist)
Expand Down Expand Up @@ -375,6 +376,7 @@ export(xform)
export(xformimage)
export(xformpoints)
export(xyzmatrix)
export(xyzmatrix2list)
export(xyzmatrix2str)
export(xyzpos)
import(grDevices)
Expand Down
21 changes: 21 additions & 0 deletions R/xyzmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ xyzmatrix.mesh3d<-function(x, ...){
#' ))
`xyzmatrix<-`<-function(x, value) UseMethod("xyzmatrix<-")

#' @export
`xyzmatrix<-.list`<-function(x, value) {
if(!isTRUE(nrow(value)==length(x)))
stop("target list and new value have incompatible sizes")
xyzmatrix2list(value)
}

#' @export
`xyzmatrix<-.default`<-function(x, value){
# count number of elements in matrices/data.frames and vectors
Expand Down Expand Up @@ -274,6 +281,20 @@ xyzmatrix2str <- function(x, format="%g,%g,%g", sep=NULL) {
sprintf(format, xyz[,1], xyz[,2], xyz[,3])
}


#' @rdname xyzmatrix
#' @export
#' @description \code{xyzmatrix2list} will convert the Nx3 matrix of XYZ
#' locations associated with an object to a list of length N with each element
#' a vector of length 3.
#' @examples
#' xyzmatrix2list(kcs20[[1]])[1:2]
xyzmatrix2list <- function(x) {
xyz <- unname(xyzmatrix(x))
ll=lapply(1:nrow(xyz), function(i) xyz[i,])
ll
}

#' @export
#' @rdname xyzmatrix
`xyzmatrix<-.neuron`<-function(x, value){
Expand Down
8 changes: 8 additions & 0 deletions man/xyzmatrix.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-xyzmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ test_that("can extract xyz coords from a matrix and other objects",{
baseline,
tolerance = 1e-6
)

xyzmatrix(arrowdf$pt_position) <- xyzmatrix(arrowdf$pt_position)+1
expect_equal(xyzmatrix(arrowdf$pt_position),
baseline+1, tolerance = 1e-6)
})

test_that("can replace xyz coords of a matrix",{
Expand Down Expand Up @@ -193,6 +197,17 @@ test_that("can replace xyz coords of a data.frame",{
})


test_that("can get/replace xyz coords in a list",{
xyzm <- xyzmatrix(kcs20)
df <- data.frame(a=1:sum(nvertices(kcs20)))
expect_true(is.list(xyzml <- xyzmatrix2list(kcs20)))
expect_true(all(lengths(xyzml)==3L))
df$pos=xyzmatrix2list(kcs20)
xyzmatrix(df$pos)=xyzmatrix(df$pos)+1
expect_equal(xyzmatrix(df$pos), xyzmatrix(kcs20+1))
})


test_that("can extract xyz coords from a neuronlist",{
xyz12=rbind(xyzmatrix(kcs20[[1]]),xyzmatrix(kcs20[[2]]))
expect_is(xyzmatrix(kcs20[1:2]),'matrix')
Expand Down