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

Feature/cosine plot warn on drop #26

Merged
merged 4 commits into from
Oct 25, 2024
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
35 changes: 32 additions & 3 deletions R/cosine.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@


#' @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
Expand All @@ -289,19 +291,46 @@
#' 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!")

Check warning on line 312 in R/cosine.R

View check run for this annotation

Codecov / codecov/patch

R/cosine.R#L312

Added line #L312 was not covered by tests
}

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)

Check warning on line 326 in R/cosine.R

View check run for this annotation

Codecov / codecov/patch

R/cosine.R#L325-L326

Added lines #L325 - L326 were not covered by tests
} else {
setdiff(unique(kk), x$pre_key)

Check warning on line 328 in R/cosine.R

View check run for this annotation

Codecov / codecov/patch

R/cosine.R#L328

Added line #L328 was not covered by tests
}
nmissing=length(missing_keys)
if(nmissing>0)
warning("Dropping ",nmissing, " keys. Try decreasing threshold!")

Check warning on line 332 in R/cosine.R

View check run for this annotation

Codecov / codecov/patch

R/cosine.R#L330-L332

Added lines #L330 - L332 were not covered by tests
}
x
}

Expand Down
6 changes: 6 additions & 0 deletions R/ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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)))
Expand Down
6 changes: 5 additions & 1 deletion man/cf_cosine_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/keys.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/testthat/test-ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ 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", {
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'))

Expand Down
Loading