From 1504e1742751be8a527eb9f3249d523a0af6d447 Mon Sep 17 00:00:00 2001 From: Gregory Jefferis Date: Sat, 21 Sep 2024 16:46:08 +0100 Subject: [PATCH 1/4] teach keys to expand id lists * document and test --- R/ids.R | 6 ++++++ man/keys.Rd | 3 +++ tests/testthat/test-ids.R | 2 ++ 3 files changed, 11 insertions(+) diff --git a/R/ids.R b/R/ids.R index c5ae9f9..469b274 100644 --- a/R/ids.R +++ b/R/ids.R @@ -55,6 +55,9 @@ extract_ids <- function(x) { #' #' \donttest{ #' keys(cf_ids(hemibrain=12345, flywire='4611686018427387904')) +#' +#' # NB this runs the query for hemibrain type MBON01 and then maps ids -> keys +#' keys(cf_ids(hemibrain='MBON01')) #' } #' @rdname keys keys <- function(x, idcol='id') { @@ -63,6 +66,9 @@ keys <- function(x, idcol='id') { else if(inherits(x, 'dendrogram')) x <- labels(x) + # expand an id list + if(inherits(x, 'cidlist')) + x=c(x,NULL) if(is.list(x) && !is.data.frame(x)) { x=data.frame(id=unlist(x), dataset=rep(abbreviate_datasets(names(x)), lengths(x))) diff --git a/man/keys.Rd b/man/keys.Rd index 58ded57..474bdf7 100644 --- a/man/keys.Rd +++ b/man/keys.Rd @@ -59,6 +59,9 @@ keys(" fw:4611686018427387904, hb:12345 ") \donttest{ keys(cf_ids(hemibrain=12345, flywire='4611686018427387904')) + +# NB this runs the query for hemibrain type MBON01 and then maps ids -> keys +keys(cf_ids(hemibrain='MBON01')) } \donttest{ diff --git a/tests/testthat/test-ids.R b/tests/testthat/test-ids.R index b75d766..ed8bc43 100644 --- a/tests/testthat/test-ids.R +++ b/tests/testthat/test-ids.R @@ -37,6 +37,8 @@ test_that("key handling works", { flywire=as.character(1:5))) expect_output(print(res), regexp = 'flywire.*hemibrain') + + expect_equal(keys(cf_ids(hemibrain = '/MBON01')), cf_ids(hemibrain = '/MBON01', keys = T)) }) test_that("fanc/banc ids/metadata", { From 4ba1a477c336a7ba6cd81f0336fdf603054c822a Mon Sep 17 00:00:00 2001 From: Gregory Jefferis Date: Sat, 10 Aug 2024 01:11:02 +0100 Subject: [PATCH 2/4] WIP check for missing query neurons --- R/cosine.R | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/R/cosine.R b/R/cosine.R index 8527c1a..81355ca 100644 --- a/R/cosine.R +++ b/R/cosine.R @@ -289,19 +289,46 @@ cf_cosine_plot <- function(ids=NULL, ..., threshold=5, #' are the input or output neurons. multi_connection_table <- function(ids, partners=c("inputs", "outputs"), threshold=1L, - group='type') { + group='type', check_missing=TRUE) { partners=match.arg(partners, several.ok = T) if(length(partners)>1) { + kk=keys(ids) l=sapply(partners, simplify = F, function(p) - multi_connection_table(ids, partners=p, threshold = threshold, group=group)) + multi_connection_table(kk, partners=p, threshold = threshold, group=group, + check_missing=F)) l=dplyr::bind_rows(l) + if(check_missing) { + query_keys <- l %>% group_by(partners) %>% + mutate(query=case_when( + partners=='inputs' ~ post_key, + partners=='outputs' ~ pre_key, + )) %>% + pull(query) + missing_keys=setdiff(unique(kk), query_keys) + nmissing=length(missing_keys) + if(nmissing>0) + warning("Dropping ",nmissing, " keys. Try decreasing threshold!") + } + return(l) } - x <- cf_partners(ids, threshold = threshold, partners = partners) + kk=keys(ids) + x <- cf_partners(kk, threshold = threshold, partners = partners) if(is.character(group)) x <- match_types(x, group, partners=partners) # mark which column was used for the query x$partners=partners + # check if some incoming ids were dropped + if(check_missing) { + missing_keys <- if(partners=='inputs') { + setdiff(unique(kk), x$post_key) + } else { + setdiff(unique(kk), x$pre_key) + } + nmissing=length(missing_keys) + if(nmissing>0) + warning("Dropping ",nmissing, " keys. Try decreasing threshold!") + } x } From 40f800da07187385e8ab192dc41f15b443692a02 Mon Sep 17 00:00:00 2001 From: Gregory Jefferis Date: Sat, 21 Sep 2024 21:41:25 +0100 Subject: [PATCH 3/4] suppress warning in test --- tests/testthat/test-ids.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-ids.R b/tests/testthat/test-ids.R index ed8bc43..f931aec 100644 --- a/tests/testthat/test-ids.R +++ b/tests/testthat/test-ids.R @@ -45,7 +45,7 @@ test_that("fanc/banc ids/metadata", { skip_if_not_installed('fancr') skip_if_not_installed('reticulate') expect_in( - cf_ids(fanc='/type:DNa01', expand = TRUE)$fanc, + suppressWarnings(cf_ids(fanc='/type:DNa01', expand = TRUE)$fanc), fancr::fanc_latestid(c("648518346488820970", "648518346475464576"), version='latest')) From 6cdc593cadb99a76e359e0dde144223ee8552cce Mon Sep 17 00:00:00 2001 From: Gregory Jefferis Date: Fri, 25 Oct 2024 22:17:47 +0100 Subject: [PATCH 4/4] man: document new arg --- R/cosine.R | 4 +++- man/cf_cosine_plot.Rd | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/R/cosine.R b/R/cosine.R index 81355ca..87c0084 100644 --- a/R/cosine.R +++ b/R/cosine.R @@ -281,6 +281,8 @@ cf_cosine_plot <- function(ids=NULL, ..., threshold=5, #' @importFrom dplyr distinct all_of +#' @param check_missing Whether to report if any query neurons are dropped (due +#' to insufficient partner neurons) (default:\code{TRUE}). #' @rdname cf_cosine_plot #' @export #' @return \code{multi_connection_table} returns a connectivity dataframe as @@ -307,7 +309,7 @@ multi_connection_table <- function(ids, partners=c("inputs", "outputs"), missing_keys=setdiff(unique(kk), query_keys) nmissing=length(missing_keys) if(nmissing>0) - warning("Dropping ",nmissing, " keys. Try decreasing threshold!") + warning("Dropping ", nmissing, " keys. Try decreasing threshold!") } return(l) diff --git a/man/cf_cosine_plot.Rd b/man/cf_cosine_plot.Rd index 5ecca66..5c57fac 100644 --- a/man/cf_cosine_plot.Rd +++ b/man/cf_cosine_plot.Rd @@ -25,7 +25,8 @@ multi_connection_table( ids, partners = c("inputs", "outputs"), threshold = 1L, - group = "type" + group = "type", + check_missing = TRUE ) } \arguments{ @@ -69,6 +70,9 @@ neurons in interactive mode.} similarity.} \item{method}{The cluster method to use (see \code{\link{hclust}})} + +\item{check_missing}{Whether to report if any query neurons are dropped (due +to insufficient partner neurons) (default:\code{TRUE}).} } \value{ The result of \code{\link{heatmap}} invisibly including the row and