Skip to content

Commit

Permalink
neuronlistz ordered by metadata df
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferis committed Apr 21, 2021
1 parent 01ddc77 commit af52fb0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
16 changes: 11 additions & 5 deletions R/neuronlistz.R
Expand Up @@ -7,7 +7,8 @@
#'
#' @param zip Path to the zip file
#' @param patt Optional regex pattern or function to specify a subset of files.
#' @param df A data.frame of metadata that will be attached to the
#' @param df A \code{data.frame} of metadata that will be attached to the
#' \code{neuronlistz} and will define the order of the objects inside it.
#' @param ... Additional arguments currently ignored
#'
#' @return A \code{\link{neuronlist}} object with additional class
Expand Down Expand Up @@ -40,6 +41,7 @@ neuronlistz <- function(zip, patt=NULL, df=NULL, ...) {
ff=setdiff(ff, dff)
if(is.null(df))
df <- read_from_zip(zip, dff, neuron=FALSE)

}

keyfilemap <- if(!is.null(patt)) {
Expand All @@ -57,13 +59,17 @@ neuronlistz <- function(zip, patt=NULL, df=NULL, ...) {
names(keyfilemap)=tools::file_path_sans_ext(basename(keyfilemap))
if(any(duplicated(names(keyfilemap))))
stop("keyfilemap must have unique names as elements!")

nlf=structure(rep(F,length(keyfilemap)),.Names=names(keyfilemap))
attr(nlf,'keyfilemap')=keyfilemap
if(is.null(df)) {
if(!is.null(df) && all(names(keyfilemap) %in% rownames(df))) {
# reorder neurons to match supplied data.frame
keyfilemap=keyfilemap[intersect(rownames(df), names(keyfilemap))]
} else {
# make a default data.frame
df=data.frame(id=names(keyfilemap), stringsAsFactors = FALSE)
rownames(df)=df[['id']]
}

nlf=structure(rep(F,length(keyfilemap)),.Names=names(keyfilemap))
attr(nlf,'keyfilemap')=keyfilemap
attr(nlf,'db')=zip
class(nlf)=c('neuronlistz','neuronlist',class(nlf))
data.frame(nlf)=df
Expand Down
3 changes: 2 additions & 1 deletion man/neuronlistz.Rd

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

16 changes: 9 additions & 7 deletions tests/testthat/test-neuronlistz.R
@@ -1,20 +1,22 @@
test_that("neuronlistz works", {
tf=tempfile(fileext = '.zip')
on.exit(unlink(tf))
write.neurons(Cell07PNs[1:5], tf, format='rds')
expect_true(is.neuronlist(nz <- neuronlistz(tf, df=Cell07PNs[,])))
write.neurons(rev(Cell07PNs), tf, format='rds', include.data.frame = T)
expect_equal(rev(neuronlistz(tf)), Cell07PNs)
write.neurons(Cell07PNs, tf, format='rds', include.data.frame = T, Force = T)

expect_true(is.neuronlist(nz <- neuronlistz(tf)))
expect_equal(nz[1:5], Cell07PNs[1:5])

tf2=tempfile(fileext = '.zip')
on.exit(unlink(tf2), add = T)
skip_if_not_installed('qs')
write.neurons(Cell07PNs[1:5], tf2, format='qs', include.data.frame = T)
write.neurons(Cell07PNs, tf2, format='qs', include.data.frame = T)
nz <- neuronlistz(tf2)
expect_equal(nz[1:5], Cell07PNs[1:5])

expect_equal(nvertices(nz), nvertices(Cell07PNs[1:5]))
expect_equal(nz[1:5], Cell07PNs[1:5])
expect_equal(nz *1, Cell07PNs[1:5])
expect_equal(nz[seq_along(nz)], Cell07PNs)
expect_equal(nvertices(nz), nvertices(Cell07PNs))
expect_equal(nz *1, Cell07PNs)

nz2 <- neuronlistz(tf2, patt = "EBH.*")
expect_equal(rownames(nz2[,]), c("EBH11R", "EBH20L", "EBH20R"))
Expand Down

0 comments on commit af52fb0

Please sign in to comment.