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/naids #126

Merged
merged 9 commits into from
Aug 22, 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
15 changes: 10 additions & 5 deletions R/ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ looks_like_bodyid <- function(x) {

# this is more stringent
valid_id <- function(x) {
!isFALSE(tryCatch(id2bit64(x), error=function(e) FALSE))
converted=tryCatch(id2bit64(x), error=function(e) NA)
length(converted)>0 && !any(is.na(converted))
}


# private function to convert 1 or more 64 bit ids (e.g. body ids) to JSON
id2json <- function(x, uniqueids=FALSE, ...) {
bx=id2bit64(x)
if(any(is.na(bx))) stop("cannot reliably JSON encode NA ids!")
if(isTRUE(uniqueids)) bx=unique(bx)
jsonlite::toJSON(bx, ...)
}
Expand All @@ -32,7 +34,7 @@ id2bit64 <- function(x) {
}
if(isTRUE(is.character(x))) {
# bit64::as.integer64("") returns 0 so we need to flag these as NA
x[nchar(x)==0]=NA_character_
x[nchar(x)==0 | is.na(x)]=NA_character_
} else if(bit64::is.integer64(x)) {
# do nothing
} else if(is.integer(x)){
Expand All @@ -43,18 +45,21 @@ id2bit64 <- function(x) {
} else if(is.double(x)) {
# biggest int that can be represented as double 2^(mantissa bits + 1)
BIGGESTFLOAT=2^53+1
if(any(x>BIGGESTFLOAT))
if(any(x>BIGGESTFLOAT, na.rm = TRUE))
stop("Some 64 bit ids cannot be exactly represented as floating point ",
"(double) numbers!\nPlease use character or bit64!")
} else if(is.logical(x) && all(is.na(x))){
# as a special case let these through because NA, a logical value,
# is often used instead of NA_real_, NA_character_ etc
} else {
stop("Unexpected data type for id. Use character, bit64, or numeric!")
}
bx <- bit64::as.integer64(x)
if(any(bx < 0 | is.na(bx)))
if(any(bx < 0, na.rm = TRUE))
stop("Invalid id!")
# unfortunately if we pass a number >9223372036854775807 then we will get
# 9223372036854775807. So we must reject ids >= than this.
if(any(bx>=bit64::as.integer64('9223372036854775807')))
if(any(bx>=bit64::as.integer64('9223372036854775807'), na.rm = TRUE))
stop('I can only cope with ids < 9223372036854775807')
bx
}
Expand Down
2 changes: 1 addition & 1 deletion R/name.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ neuprint_search <- function(search, field = "name", fixed=FALSE, exact=NULL,
#' @rdname neuprint_search
#' @importFrom stats na.omit
neuprint_ids <- function(x, mustWork=TRUE, unique=TRUE, fixed=TRUE, conn=NULL, dataset=NULL, ...) {
if(is.character(x) && length(x)==1 && !looks_like_bodyid(x)) {
if(is.character(x) && length(x)==1 && !looks_like_bodyid(x) && !is.na(x)) {
x <- neuprint_search(x, meta = F, field = 'type', fixed=fixed,
conn=conn, dataset=dataset, ...)
}
Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ test_that("id conversion works", {

toobigid="9223372036854775807"
expect_error(id2bit64(toobigid))
expect_error(id2bit64(-1))
expect_equal(id2bit64(c(1,NA)), bit64::as.integer64(c("1", NA)))
expect_equal(id2bit64(c(1, NA, "")), bit64::as.integer64(c("1", NA, NA)))

expect_equal(id2char(NULL), character(0))
expect_equal(id2char(character(0)), character(0))
expect_equal(id2char(logical(0)), character(0))
expect_equal(id2char(numeric(0)), character(0))
expect_equal(id2char(factor()), character(0))
expect_error(id2char(""))
expect_equal(id2char(""), NA_character_)
expect_equal(id2char(NA), NA_character_)

expect_equal(neuprint_ids(bigid), bigid)
expect_equal(neuprint_ids(1:4), as.character(1:4))
Expand All @@ -48,8 +52,22 @@ test_that("id conversion works", {
expect_error(neuprint_ids(-1))
expect_error(neuprint_ids(numeric()))
expect_equal(neuprint_ids(numeric(), mustWork = FALSE), character())
expect_error(neuprint_ids(NA))
expect_equal(neuprint_ids(NA, mustWork = F), NA_character_)
})

test_that("valid_id works", {
expect_true(valid_id(1))
expect_false(valid_id(NA))
expect_false(valid_id(NULL))
expect_false(valid_id(logical()))
expect_false(valid_id(-1))
toobigid="9223372036854775807"
expect_false(valid_id(toobigid))
})


skip_if_offline()
skip_if(as.logical(Sys.getenv("SKIP_NP_SERVER_TESTS")))

test_that("neuprint_ids works", {
Expand Down