Skip to content

[WIP] units #498

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

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Imports:
checkmate,
stringr,
pbapply,
units,
zip
Suggests:
spelling,
Expand Down
31 changes: 28 additions & 3 deletions R/neuron.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#' @param NeuronName Character vector containing name of neuron or a function
#' with one argument (the full path) which returns the name. The default
#' (\code{NULL}) sets NeuronName to the file name without the file extension.
#' @param unit a unit
#' @param MD5 Logical indicating whether to calculate MD5 hash of input
#' @importFrom tools md5sum
#' @examples
Expand Down Expand Up @@ -82,8 +83,8 @@
#' plot(gstem2)
#' plot(as.neuron(gstem2))
neuron<-function(d, NumPoints=nrow(d), StartPoint, BranchPoints=integer(), EndPoints,
SegList, SubTrees=NULL, InputFileName=NULL, NeuronName=NULL, ...,
MD5=TRUE){get
SegList, SubTrees=NULL, InputFileName=NULL, NeuronName=NULL,
unit=NULL, ..., MD5=TRUE){get

coreFieldOrder=c("NumPoints", "StartPoint", "BranchPoints",
"EndPoints", "nTrees", "NumSegs", "SegList", "SubTrees","d" )
Expand Down Expand Up @@ -202,14 +203,15 @@ normalise_swc<-function(x, requiredColumns=c('PointNo','Label','X','Y','Z','W','
#' @param origin Root vertex, matched against names (aka PointNo) when
#' available (see details)
#' @param Verbose Whether to be verbose (default: FALSE)
#' @param unit a unit
#' @return A list with elements:
#' (NumPoints,StartPoint,BranchPoints,EndPoints,nTrees,NumSegs,SegList,
#' [SubTrees]) NB SubTrees will only be present when nTrees>1.
#' @export
#' @importFrom igraph V V<- vcount decompose.graph
#' @rdname neuron
#' @seealso \code{\link{graph.dfs}, \link{as.seglist}}
as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){
as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, unit=NULL, ...){
# translate origin into raw vertex id if necessary
if(length(origin)){
vertex_names=igraph::V(x)$name
Expand Down Expand Up @@ -263,6 +265,7 @@ as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){
# TODO refactor this into a separate function e.g. normalise.swc since
# we need to do something similar in as.neuron.dataframe and seglist2swc etc
d=data.frame(PointNo=get.vertex.attribute(x,'name'))

if(is.null(vertexData)){
# get vertex information from graph object
xyz=xyzmatrix(x)
Expand Down Expand Up @@ -290,6 +293,8 @@ as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){

d=seglist2swc(x=subtrees,d=d)
d=normalise_swc(d)
if (!is.null(unit))
d=assign_unit(d, unit)
n=list(d=d,NumPoints=igraph::vcount(masterg),
StartPoint=StartPoint,
BranchPoints=branchpoints(masterg, original.ids='vid'),
Expand Down Expand Up @@ -1615,3 +1620,23 @@ reroot.neuronlist<-function(x, idx=NULL, pointno=NULL, point=NULL, ...){
}
res
}

#' Assigns a unit to the distance matrix
#'
#' @param d distance matrix
#' @param unit a unit
#'
#' @importFrom units set_units
assign_unit <- function(d, unit = NULL) {
for (k in c("X", "Y", "Z", "W")) {
d[[k]] <- set_units(d[[k]], unit, mode = "standard")
}
d
}

# Set unit for a neuron
#' @export
set_units.neuron <- function(n, unit = NULL, ...) {
n$d <- assign_unit(n$d, unit)
n
}
6 changes: 6 additions & 0 deletions R/neuronlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -1132,3 +1132,9 @@ prune_twigs.neuronlist <- function(x, twig_length, OmitFailures=NA, ...) {
if(missing(twig_length)) stop("Missing twig_length argument")
nlapply(x, prune_twigs, twig_length=twig_length, OmitFailures=OmitFailures, ...)
}

# Set unit for a neuronlist
#' @export
set_units.neuronlist <- function(nl, unit = NULL, ...) {
nlapply(nl, function(x) set_units.neuron(x, unit = unit))
}
8 changes: 8 additions & 0 deletions tests/testthat/test-neuron.R
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,11 @@ test_that("rerooting neuronlist", {
rpns=reroot.neuronlist(pns, point = points)
expect_equal(rpns[[1]]$StartPoint, 12)
})

test_that("set_units neuron", {
pn<-Cell07PNs[[1]]
pnu<-set_units.neuron(pn, 'um')
# units setting works correctly
expect_equal(class(pnu$d$X), "units")
expect_equal(class(pnu$d$W), "units")
})