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/matching by col numeric #504

Merged
merged 4 commits into from
Nov 27, 2022
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
22 changes: 20 additions & 2 deletions R/neuronlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ as.data.frame.neuronlist<-function(x, row.names = names(x), optional = FALSE, ..
nn=names(x)
matching_rows=intersect(nn, rownames(value))
if (length(matching_rows)!=length(nn) && any(nn %in% value[,1])) {
matching_rows_fc=intersect(nn, value[,1])
# need to turn them into string
candids <- id2char(value[,1])
matching_rows_fc=intersect(nn, candids)
if (length(matching_rows)<length(matching_rows_fc)){
warning("Matching neurons by first column.")
matching_rows=matching_rows_fc
rownames(value) <- value[,1]
rownames(value) <- candids
}
}
if(length(matching_rows)){
Expand Down Expand Up @@ -1140,3 +1142,19 @@ prune_twigs.neuronlist <- function(x, twig_length, OmitFailures=NA, ...) {
if(missing(twig_length)) stop("Missing twig_length argument")
nlapply(x, prune_twigs, twig_length=twig_length, OmitFailures=OmitFailures, ...)
}

# helper function to convert numeric ids to character
id2char <- function(x) {
if(is.character(x)) return(x)
if(is.integer(x) || inherits(x, "integer64")) return(as.character(x))
# if(is.integer(x)) return(as.character(x))
if(is.numeric(x)) {
# this is the tricky one
# unfortunately it is slower than bit64::as.integer64
return(format(x, scientific=F))
}
warning("unrecognised id class:", paste(class(x), collapse = ','))
# we don't want to turn logical values into character
if(is.logical(x)) return(x)
as.character(x)
}
16 changes: 14 additions & 2 deletions tests/testthat/test-neuronlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ test_that("as.data.frame.neuronlist behaves", {
expect_equal(kcs20nodf[,], kcs20[,])
expect_equal(rownames(kcs20[,]), rownames(kcs20nodf[,]))

# numeric id column
df$gene_name=1e3+seq_along(kcs20)-1
kcs20nodf=kcs20
data.frame(kcs20nodf)=NULL
names(kcs20nodf)=df$gene_name
expect_warning(kcs20nodf[,] <- df, 'Matching neurons by first column')
expect_equal(rownames(kcs20nodf[,]), as.character(df$gene_name))
})

context("neuronlist: [")
Expand Down Expand Up @@ -336,6 +343,11 @@ test_that("prune twigs of a neuronlist", {
expect_lt(pruned_nl[[1]]$NumSegs,n[[1]]$NumSegs)
expect_lt(pruned_nl[[2]]$NumSegs,n[[2]]$NumSegs)
expect_lt(pruned_nl[[3]]$NumSegs,n[[3]]$NumSegs)


})


test_that("id2char works", {
expect_equal(id2char(1e5), id2char("100000"))
expect_equal(id2char(100000L), "100000")
expect_equal(id2char(c(100000L, NA)), c("100000", NA))
})