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

WIP: added withMeta parameter to nlapply #501

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion R/neuronlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ as.data.frame.neuronlist<-function(x, row.names = names(x), optional = FALSE, ..
#' error. The default value (\code{NA}) will result in nlapply stopping with
#' an error message the moment there is an error. For other values, see
#' details.
#' @param withMeta whether or not to include the metadata from the neuronlist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is 100% clear yet. I think you need to add something like "in the body of each object in the output list"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intention that the metadata dataframe should be removed from the output? It is. Maybe this should be signalled.

#' data.frame
#' @param .progress Character vector specifying the type of progress bar (see
#' Progress bar section for details) The default value of \code{"auto"} shows
#' a progress bar in interactive use after 2s. The default value can be
Expand Down Expand Up @@ -479,7 +481,7 @@ as.data.frame.neuronlist<-function(x, row.names = names(x), optional = FALSE, ..
#' options(nat.progress=NULL)
#' sl=nlapply(Cell07PNs, FUN = seglengths)
#' }
nlapply<-function (X, FUN, ..., subset=NULL, OmitFailures=NA,
nlapply<-function (X, FUN, ..., subset=NULL, OmitFailures=NA, withMeta=FALSE,
.progress=getOption('nat.progress', default='auto')){
if(isTRUE(.progress=='auto')) {
.progress = ifelse(interactive(), "natprogress", "none")
Expand All @@ -495,6 +497,12 @@ nlapply<-function (X, FUN, ..., subset=NULL, OmitFailures=NA,
class(X)
else c("neuronlist", 'list')

if (withMeta && ncol(X[,]) > 0) {
meta_list <- split(X[,], seq(nrow(X[,])))
Xlist = mapply(append, X, meta_list, SIMPLIFY = FALSE)
X = as.neuronlist(lapply(Xlist, as.neuron))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work if X contains dotprops rather than neuron objects. I think you need to set the class based on the class of each element of X before appending. I guess you have to use append in case your metadata overwrites one of the existing fields in the elements of X? NB I have tried to avoid assuming that every element of a neuronlist has the same class.

}

if(!is.null(subset)){
if(!is.character(subset)) subset=names(X)[subset]
Y=X
Expand Down
2 changes: 1 addition & 1 deletion R/neuronlistfh.R
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ remotesync.default<-function(x, remote=attr(x,'remote'), download.missing=TRUE,
#' @rdname remotesync
#' @examples
#' \dontrun{
#' kcs20=read.neuronlistfh('http://flybrain.mrc-lmb.cam.ac.uk/si/nblast/flycircuit/kcs20.rds')
#' kcs20=read.neuronlistfh('http://virtualflybrain.org/data/VFB/nblast/flycircuit/kcs20.rds')
#' # update object from the web
#' kcs20=remotesync(kcs20)
#' # download all neurons with significant innervation of the vertical lobe
Expand Down
4 changes: 4 additions & 0 deletions man/nlapply.Rd

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

2 changes: 1 addition & 1 deletion man/remotesync.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-.neuronlistfh-remote-nocran.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("we can download a neuronlistfh object with MD5'd objects", {
localdir <- tempfile()
dir.create(localdir)
on.exit(unlink(localdir, recursive=TRUE))
kcs20.url="http://flybrain.mrc-lmb.cam.ac.uk/si/nblast/flycircuit/kcs20.rds"
kcs20.url="http://virtualflybrain.org/data/VFB/nblast/flycircuit/kcs20.rds"
kcs20md5 <- read.neuronlistfh(kcs20.url, localdir=localdir, quiet=TRUE)
# test trying to read in neuronlistfh object which is now available locally
# before we have downloaded any data objects
Expand All @@ -26,7 +26,7 @@ test_that("we can synchronise a neuronlistfh object with its remote", {
localdir <- tempfile()
dir.create(localdir)
on.exit(unlink(localdir, recursive=TRUE))
kcs20fh.remote <- read.neuronlistfh("http://flybrain.mrc-lmb.cam.ac.uk/si/nblast/flycircuit/kcs20.rds",
kcs20fh.remote <- read.neuronlistfh("http://virtualflybrain.org/data/VFB/nblast/flycircuit/kcs20.rds",
localdir=localdir)
expect_equal(dim(kcs20fh.remote[[1]]$points), c(284, 3))
# make a neuronlistfh object from the local data bundled with this package
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-neuronlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,24 @@ test_that("[<-.neuronlist does the right thing",{
expect_null(colnames(kcs13[,]<-NULL))
})

test_that("neuronlist includes metadata",{
kcs13=kcs20[1:3]

# expect that withMeta output will contain more attributes
out1 = nlapply(kcs13, function(x) x)
out2 = nlapply(kcs13, function(x) x, withMeta = T)
expect_true(
length(attributes(out1[[2]])$names) < length(attributes(out2[[2]])$names)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this was the intention:

> out1
'neuronlist' containing 3 'dotprops' objects and 'data.frame' with 14 vars [75.9 kB]
> out2
'neuronlist' containing 3 'list' objects and 'data.frame' with 0 vars [94.5 kB]


# or parts of columns
data.frame(kcs13) <- NULL
out3 = nlapply(kcs13, function(x) x, withMeta = T)
expect_true(
all(names(out1[[1]]) == names(out3[[1]]))
)
})

test_that("prune twigs of a neuronlist", {
n = Cell07PNs[1:3]

Expand Down