natverse / nat

NeuroAnatomy Toolbox: An R package for the (3D) visualisation and analysis of biological image data, especially tracings of single neurons.
https://natverse.org/nat/
62 stars 26 forks source link

consider adding a `nid()` function to handle numeric ids #505

Open jefferis opened 1 year ago

jefferis commented 1 year ago

ids are identifiers not indices. Something like this might be possible:

nid <- function(...) {
  sc <- sys.call()
  scc=as.character(sc)
  stopifnot(scc[1]=='nid')
  i64=bit64::as.integer64(scc[-1])
  max64=bit64::as.integer64("9223372036854775807")
  if(isTRUE(any(i64<0, na.rm = T)) || 
     isTRUE(any(i64==max64, na.rm = T))) {
    badids=!fafbseg::valid_id(i64)
    if(any(badids)) {
      warning("there are bad ids:", paste(scc[-1][badids], collapse = ','))
      i64[badids]=NA
    }
  }
  class(i64)=union("nid", class(i64))
  i64
}

It doesn't look like there is a conflicting nid definition in packages that natverse users typically have. This might even belong in nat.utils. Would also need to figure out how to handle arguments that contain numeric ids rather than actually being one. This could be part of the solution to the common problem of indexing a neuronlist with numeric ids.