From 6c0cc53978d9f2f82c8e71b10b2d8f97276a73fa Mon Sep 17 00:00:00 2001 From: Romain Franconville Date: Wed, 15 Jan 2020 11:28:11 -0500 Subject: [PATCH 1/2] Fixed common_connectivity. Also got faster because we're only looping through the actual common connections --- R/connectivity.R | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/R/connectivity.R b/R/connectivity.R index 38a941e4..347db309 100644 --- a/R/connectivity.R +++ b/R/connectivity.R @@ -135,20 +135,20 @@ neuprint_common_connectivity <- function(bodyids, statuses = NULL, all_segments)) class(Payload) = "json" com.conn = neuprint_fetch(path = 'api/npexplorer/commonconnectivity', body = Payload, conn = conn, ...) - m = matrix(0,nrow = length(bodyids),ncol = length(com.conn$data[[1]][[1]])) - rownames(m) = paste0(bodyids,"_weight") - connected = c() - for(i in 1:length(com.conn$data[[1]][[1]])){ - s = com.conn$data[[1]][[1]][[i]] - find = match(names(s),rownames(m)) - add = find[!is.na(find)] - m[add,i] = unlist(s)[!is.na(find)] - connected = c(connected,ifelse(is.null(s$input),s$output,s$input)) + partnerType <- ifelse(prepost=="PRE","output","input") + partnerNames <- sapply(com.conn$data[[1]][[1]],function(d) d[[partnerType]]) + partnerCount <- table(partnerNames) + commonPartners <- names(partnerCount[partnerCount == length(bodyids)]) + m <- matrix(0,nrow = length(bodyids),ncol = length(commonPartners)) + rownames(m) <- paste0(bodyids,"_weight") + colnames(m) <- commonPartners + comData <- com.conn$data[[1]][[1]][which(partnerNames %in% commonPartners)] + for(i in 1:length(comData)){ + s <- comData[[i]] + rName <- names(s)[names(s) %in% rownames(m)] + m[rName,as.character(s[[partnerType]])] <- s[[rName]] } - m = t(apply(m,1,as.numeric)) - colnames(m) = connected - rownames(m) = bodyids - m = m[,apply(m,2,function(x) sum(x==0)==0)] + rownames(m) <- bodyids m } From a657ffb880f0fd23cc6e0507240ca30ac52d4fe9 Mon Sep 17 00:00:00 2001 From: Romain Franconville Date: Wed, 15 Jan 2020 11:32:35 -0500 Subject: [PATCH 2/2] Small fix (in case they are no common partners) --- R/connectivity.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/connectivity.R b/R/connectivity.R index 347db309..e8547a46 100644 --- a/R/connectivity.R +++ b/R/connectivity.R @@ -143,8 +143,8 @@ neuprint_common_connectivity <- function(bodyids, statuses = NULL, rownames(m) <- paste0(bodyids,"_weight") colnames(m) <- commonPartners comData <- com.conn$data[[1]][[1]][which(partnerNames %in% commonPartners)] - for(i in 1:length(comData)){ - s <- comData[[i]] + + for(s in comData){ rName <- names(s)[names(s) %in% rownames(m)] m[rName,as.character(s[[partnerType]])] <- s[[rName]] }