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

Fix handling of unique bodyids #42

Merged
merged 4 commits into from
Jan 27, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions R/connectivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ neuprint_get_adjacency_matrix <- function(bodyids, dataset = NULL, all_segments
"WHERE n.bodyId IN input AND m.bodyId IN input",
"RETURN n.bodyId AS upstream, m.bodyId AS downstream, c.weight AS weight, n.%s AS upName, m.%s AS downName"
),
id2json(bodyids),
id2json(bodyids, uniqueids = TRUE),
all_segments.json,
namefield,
namefield
Expand Down Expand Up @@ -50,17 +50,21 @@ neuprint_connection_table <- function(bodyids, prepost = c("PRE","POST"), roi =
prepost = match.arg(prepost)
conn=neuprint_login(conn)
all_segments.json = ifelse(all_segments,"Segment","Neuron")
bodyids=unique(id2bit64(bodyids))
if(!is.null(roi)){
roicheck = neuprint_check_roi(rois=roi, dataset = dataset, conn = conn, ...)
}
if(progress){
d = do.call(rbind, pbapply::pblapply(bodyids, function(bi) tryCatch(neuprint_connection_table(
# sadly you can't lapply a set of int64 ...
d = do.call(rbind, pbapply::pblapply(as.character(bodyids), function(bi) tryCatch(neuprint_connection_table(
bodyids = bi,
prepost = prepost,
roi = roi,
progress = FALSE,
dataset = dataset, conn = conn, ...),
error = function(e) NULL)))
d <- d[order(d$weight,decreasing=TRUE),]
rownames(d) <- NULL
return(d)
}
cypher = sprintf(paste("WITH %s AS bodyIds UNWIND bodyIds AS bodyId",
Expand All @@ -85,6 +89,7 @@ neuprint_connection_table <- function(bodyids, prepost = c("PRE","POST"), roi =
d$weight <- as.integer(d$weight)
d$prepost <- ifelse(prepost=="PRE",0,1)
d <- d[order(d$weight,decreasing=TRUE),]
rownames(d) <- NULL
d[,c("bodyid", "partner", "roi","weight", "prepost")]
}

Expand Down Expand Up @@ -119,7 +124,7 @@ neuprint_common_connectivity <- function(bodyids, statuses = NULL,
all_segments = ifelse(all_segments,"true","false")
Payload = noquote(sprintf('{"dataset":"%s","neuron_ids":%s,"statuses":%s,"find_inputs":%s,"all_segments":%s}',
dataset,
id2json(bodyids),
id2json(bodyids, uniqueids = TRUE),
ifelse(is.null(statuses),jsonlite::toJSON(list()),jsonlite::toJSON(statuses)),
find_inputs,
all_segments))
Expand Down Expand Up @@ -169,6 +174,8 @@ neuprint_simple_connectivity <- function(bodyids,
...){
prepost = match.arg(prepost)
find_inputs = ifelse(prepost=="PRE", "false","true")
# nb looks odd, but convert back to character to allow pblapply ...
bodyids=as.character(unique(id2bit64(bodyids)))
if(length(bodyids)>10){
m = Reduce(function(x,y,...) dplyr::full_join(x,y,by=c("name",ifelse(prepost=="PRE","output","input"),"type")),(pbapply::pblapply(bodyids, function(bi) tryCatch(neuprint_simple_connectivity(
bodyids = bi,
Expand Down Expand Up @@ -254,8 +261,8 @@ neuprint_get_paths <- function(body_pre, body_post, n, weightT=5, roi=NULL,
"ALL (x in relationships(p) WHERE x.weight >= %s %s)",
"RETURN length(p) AS `length(path)`,[n in nodes(p) | [n.bodyId, n.instance, n.type]] AS path,[x in relationships(p) | x.weight] AS weights"
),
id2json(body_pre),
id2json(body_post),
id2json(body_pre, uniqueids = TRUE),
id2json(body_post, uniqueids = TRUE),
all_segments.json,
n[1]-1,
n[2],
Expand Down Expand Up @@ -320,8 +327,8 @@ neuprint_get_shortest_paths <- function(body_pre,body_post,weightT=5,roi=NULL,da
"ALL (x in relationships(p) WHERE x.weight >= %s %s)",
"RETURN length(p) AS `length(path)`,[n in nodes(p) | [n.bodyId, n.instance, n.type]] AS path,[x in relationships(p) | x.weight] AS weights"
),
id2json(body_pre),
id2json(body_post),
id2json(body_pre, uniqueids = TRUE),
id2json(body_post, uniqueids = TRUE),
all_segments.json,
all_segments.json,
weightT,
Expand Down
2 changes: 1 addition & 1 deletion R/ids.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# private function to convert 1 or more 64 bit ids (e.g. body ids) to JSON
id2json <- function(x, uniqueids=TRUE, ...) {
id2json <- function(x, uniqueids=FALSE, ...) {
bx=id2bit64(x)
if(isTRUE(uniqueids)) bx=unique(bx)
jsonlite::toJSON(bx, ...)
Expand Down
9 changes: 9 additions & 0 deletions R/name.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
#' }
neuprint_get_neuron_names <- function(bodyids, dataset = NULL, all_segments = FALSE, conn = NULL, ...) {
all_segments.json = ifelse(all_segments,"Segment","Neuron")
bodyids <- id2bit64(bodyids)
if(any(duplicated(bodyids))) {
ubodyids=unique(bodyids)
unames=neuprint_get_neuron_names(bodyids=ubodyids, dataset=dataset,
conn=conn, all_segments=all_segments, ...)
res=unames[match(bodyids, ubodyids)]
return(res)
}

cypher = sprintf("WITH %s AS bodyIds UNWIND bodyIds AS bodyId MATCH (n:`%s`) WHERE n.bodyId=bodyId RETURN n.instance AS name",
id2json(bodyids),
all_segments.json)
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-connectivity.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
skip_if(as.logical(Sys.getenv("SKIP_NP_SERVER_TESTS")))

test_that("neuprint_connection_table works", {
expect_is(t1 <- neuprint_connection_table(c(818983130, 1796818119)),
'data.frame')
# ensure we get the same answer with progress=TRUE
expect_equal(neuprint_connection_table(c(818983130, 1796818119),
progress = TRUE),
t1)
})
6 changes: 3 additions & 3 deletions tests/testthat/test-ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ test_that("id conversion works", {
med.json)

expect_equal(as.character(id2json(c(bigid, bigid))),
big.json)
expect_equal(as.character(id2json(c(bigid, bigid), uniqueids=FALSE)),
big.json2)
expect_equal(as.character(id2json(list(bigid, bigid))),
expect_equal(as.character(id2json(c(bigid, bigid), uniqueids=TRUE)),
big.json)
expect_equal(as.character(id2json(list(bigid, bigid))),
big.json2)

df=data.frame(bodyId=bigid)
expect_equal(as.character(id2json(df)),
Expand Down