natverse / neuprintr

R client utilities for interacting with the neuPrint connectome analysis service
http://natverse.org/neuprintr
3 stars 3 forks source link

neuprint_get_shortest_paths returns empty tibble if one of the cells has NULL as name or type #164

Open artxz opened 1 year ago

artxz commented 1 year ago

for example, this works neuprint_get_shortest_paths(1633411652, 1663767674) but this does not neuprint_get_shortest_paths(1850310331, 1663767674)

the returned queries look ok but the second one bodyid = 1850310331 has no name or type. It seems to cause a problem when building the returned dataframe

connTable <- dplyr::bind_rows(lapply(nc$data, function(d){ l <- d[[1]] tryCatch( dplyr::bind_rows(lapply(1:l, function(i){ data.frame(from=as.character(d[[2]][[i]][[1]]), to=as.character(d[[2]][[i+1]][[1]]), weight=d[[3]][[i]], depth=i, name.from=d[[2]][[i]][[2]],name.to=d[[2]][[i+1]][[2]], type.from=d[[2]][[i]][[3]],type.to=d[[2]][[i+1]][[3]], stringsAsFactors = FALSE) })), error = function(e) NULL) }))

One possible solution is to use, eg., I(list(d[[2]][[i]][[2]])) instead of d[[2]][[i]][[2]]

Similar problem with neuprint_get_paths

jefferis commented 1 year ago

ah that's annoying. code looks pretty crufty

artxz commented 1 year ago

maybe it's not that bad with indentation?

      data.frame(from=as.character(d[[2]][[i]][[1]]),
                 to=as.character(d[[2]][[i+1]][[1]]),
                 weight=d[[3]][[i]],
                 depth=i,
                 name.from=I(list(d[[2]][[i]][[2]])), name.to=I(list(d[[2]][[i+1]][[2]])),
                 type.from=I(list(d[[2]][[i]][[3]])), type.to=I(list(d[[2]][[i+1]][[3]])),
                 stringsAsFactors = FALSE)

Another possibility

connTable <- dplyr::bind_rows(lapply(nc$data, function(d){
  l <- d[[1]]
  tryCatch( dplyr::bind_rows(lapply(1:l, function(i){
    d2 <- do.call(rbind, d[[2]][i:(i+1)])
    data.frame(from=as.character(d2[1]),
               to=as.character(d2[2]),
               weight=d[[3]][[i]],
               depth=i,
               name.from=I(d2[3]),name.to=I(d2[4]),
               type.from=I(d2[5]),type.to=I(d2[6]),
               stringsAsFactors = FALSE)
  })), error = function(e) NULL)
}))
artxz commented 1 year ago

also suggest add an ordinal number for each path, maybe something like this

connTable$path <- rep(seq(1,length(nc$data)), each= nc$data[[1]][[1]])
CesiumChloride commented 6 months ago

Hi I am wondering whether there is a fix or work around on this issue? Thanks!

jefferis commented 6 months ago

@artxz’s suggestion was good but I didn’t get to implement. Will take a day or two to get to unless someone wants to offer a PR.

CesiumChloride commented 6 months ago

Sounds great! It would be super helpful if it is solved. Thanks!