Skip to content

Commit

Permalink
Merge pull request #38 from natverse/feature/normalised-keep-scale
Browse files Browse the repository at this point in the history
Teach normalised NBLAST to keep scale
  • Loading branch information
jefferis authored Jun 18, 2020
2 parents b9be0e5 + 4625f8f commit 63eb018
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
15 changes: 12 additions & 3 deletions R/neuriteblast.r
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,13 @@ NeuriteBlast <- function(query, target, targetBinds=NULL, normalised=FALSE,
if(nat::is.neuronlist(query)) {
res=plyr::llply(query, NeuriteBlast, target=target, targetBinds=targetBinds,
normalised=normalised, OmitFailures=OmitFailures, simplify=simplify, ...=...)
if (!identical(simplify, FALSE) && length(res))
if (!identical(simplify, FALSE) && length(res)){
if(normalised)
selfscores=sapply(res, "attr", "scaled:scale")
res=simplify2array(res, higher = (simplify == "array"))
if(normalised)
attr(res,'scaled:scale')=selfscores
}
return(res)
} else {
if(is.null(targetBinds))
Expand All @@ -315,8 +320,12 @@ NeuriteBlast <- function(query, target, targetBinds=NULL, normalised=FALSE,
}

if(normalised){
if(is.list(scores)) stop("Cannot normalise results when they are not a single number")
scores=scores/NeuriteBlast(query, neuronlist(query), normalised=FALSE, ...)
if(is.list(scores))
stop("Cannot normalise results when they are not a single number")
# nb roll our own scale, since scale turns vector into matrix
selfscore=NeuriteBlast(query, neuronlist(query), normalised=FALSE, ...)
scores=scores/selfscore
attr(scores,'scaled:scale')=selfscore
}
scores
}
Expand Down
59 changes: 46 additions & 13 deletions tests/testthat/test-NBLAST2.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,49 @@ test_that("nblast v2 handles a neuronlist as query", {
expect_equal(scores, scores.expected)
})

test_that("we can calculate scores for non square matrices", {
expect_equal(scoresaba <- nblast_allbyall(testneurons, version=2),
nblast(testneurons, testneurons, version=2))
expect_equal(nblast(testneurons[1:2], testneurons[3:5], version=2),
scoresaba[3:5, 1:2])
})

test_that("we can calculate normalised nblast v2 scores", {
scores <- nblast(testneurons[1:2], testneurons, version=2)
scores.norm=scale(scores, scale=c(scores[1,1],scores[2,2]), center = FALSE)
expect_is(scores <- nblast(testneurons[1:2], testneurons, version=2), 'matrix')

expect_is(scores.norm <-
scale(scores, scale=c(scores[1,1],scores[2,2]), center = FALSE),
'matrix')
expect_equivalent(nblast(testneurons[1:2], testneurons, normalised=TRUE), scores.norm)

expect_equal(scoresaba <- nblast_allbyall(testneurons,
version=2,
normalisation = 'normalised'),
nblast(testneurons, testneurons, version=2, normalised = TRUE))

scores.rect <- nblast(testneurons[1:2], testneurons[3:5], version = 2)
scores.rect.norm <-
nblast(testneurons[1:2],
testneurons[3:5],
version = 2,
normalised = T)
expect_equal(scores.rect.norm,
scale(
scores.rect,
center = FALSE,
scale = attr(scores.rect.norm, 'scaled:scale')
))

# the simple subset will drop the scaled attribute
baseline=scores.rect.norm[, 1, drop = FALSE]
attr(baseline, 'scaled:scale')=attr(scores.rect.norm, 'scaled:scale')[1]
expect_equal(nblast(testneurons[1], testneurons[3:5], version = 2, normalised = T),
baseline)

})

test_that("we can calculate scores for regular neurons",{
library(nat)
nblast_allbyall(Cell07PNs[1:4])
expect_is(nblast_allbyall(Cell07PNs[1:4]), 'matrix')
})

test_that("we can calculate scores using getOption('nat.default.neuronlist')", {
Expand Down Expand Up @@ -57,13 +91,12 @@ test_that("we can handle OmitFailures", {
})

test_that("we can handle all combinations of dotprops and neurons, both as neuronlists and singly", {
nblast(testneurons[[1]], Cell07PNs[1:3])
nblast(testneurons[1:3], Cell07PNs[1:3])
nblast(testneurons[[1]], Cell07PNs[[1]])
nblast(testneurons[1:3], Cell07PNs[[1]])

nblast(Cell07PNs[[1]], testneurons[1:3])
nblast(Cell07PNs[1:3], testneurons[1:3])
nblast(Cell07PNs[[1]], testneurons[[1]])
nblast(Cell07PNs[1:3], testneurons[[1]])
expect_is(nblast(testneurons[[1]], Cell07PNs[1:3]), 'numeric')
expect_is(nblast(testneurons[1:3], Cell07PNs[1:3]), 'matrix')
expect_is(nblast(testneurons[[1]], Cell07PNs[[1]]), 'numeric')
expect_is(nblast(testneurons[1:3], Cell07PNs[[1]]), 'numeric')
expect_is(nblast(Cell07PNs[[1]], testneurons[1:3]), 'numeric')
expect_is(nblast(Cell07PNs[1:3], testneurons[1:3]), 'matrix')
expect_is(nblast(Cell07PNs[[1]], testneurons[[1]]), 'numeric')
expect_is(nblast(Cell07PNs[1:3], testneurons[[1]]), 'numeric')
})

0 comments on commit 63eb018

Please sign in to comment.