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/
64 stars 29 forks source link

subset neuronlist doesn't work with functions #489

Closed dokato closed 2 years ago

dokato commented 2 years ago

For a neuronlist of neurons when I call:

> trimmed_partners <- subset(n_partners, function(x) {
+   sq_diffs <- t(t(x) - mid_point)^2
+   sqrt(rowSums(sq_diffs)) < radius
+ })

getting

Error in subset.neuronlist(n_partners, function(x) { : 
  Use of subset with functions is deprecated. Please use filterfun argument

Following the suggesting, I tried with filterfun:

trimmed_partners <- subset(n_partners, filterfun = function(x) {
+   sq_diffs <- t(t(x) - mid_point)^2
+   sqrt(rowSums(sq_diffs)) < radius
+ })
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL'
[..]
Warning message:
In subset.neuronlist(n_partners, filterfun = function(x) { :
  filterfun failed to evaluate for 7 entries in neuronlist

but this fails too. I can make it work with nlapply, but I thought that subset with filterfun should work in that case.

jefferis commented 2 years ago

Thanks @dokato. Could you provide a full reprex?

dokato commented 2 years ago

Sure, see below:

library(nat)
mid_point = colMeans(xyzmatrix(Cell07PNs[[1]]))
trimmed_partners <- subset(Cell07PNs[2:11], filterfun = function(x) {
   sq_diffs <- t(t(x) - mid_point)^2
   sqrt(rowSums(sq_diffs)) < 100
 })
dokato commented 2 years ago

Actually, while writing that I realised that the subset.neuronlist and subset.neuron serve very different purpose. I wanted to extrapolate points selection from a single neuron and extend it to a collection, whereas subset.neuronlist let's you select specific neruons from a list, given the condition (which might be specified by function).

Well, in that case using nlapply sounds logical...