Mouse-Imaging-Centre / RMINC

Statistics for MINC volumes: A library to integrate voxel-based statistics for MINC volumes into the R environment. Supports getting and writing of MINC volumes, running voxel-wise linear models, correlations, etc.; correcting for multiple comparisons using the False Discovery Rate, and more. With contributions from Jason Lerch, Chris Hammill, Jim Nikelski and Matthijs van Eede. Some additional information can be found here:
https://mouse-imaging-centre.github.io/RMINC
Other
22 stars 17 forks source link

Can't run pMincApply example code: object 'fun' of mode 'function' was not found #299

Open gdevenyi opened 2 years ago

gdevenyi commented 2 years ago
testdir = getRMINCTestData() 
gf <- read.csv(paste0(testdir, "test_data_set.csv")
ma <- mincApply(gf$jacobians_fixed_2,quote(mean(x))); 
mincWriteVolume(ma, "means.mnc")
### run the whole thing in parallel on the local machine
testFunc <- function (x) { return(c(1,2))}
pout <- pMincApply(gf$jacobians_fixed_2, 
                   quote(testFunc(x)),
                   workers = 4,
                   global = c('gf','testFunc'),
                    local=T)
mincWriteVolume(pout, "pmincOut.mnc")
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'fun' of mode 'function' was not found
bcdarwin commented 2 years ago

Having not looked into this yet, I wonder whether we still support the rather hackish quote(...(x)) syntax or whether it should just be mincApply(..., mean), etc.

gdevenyi commented 2 years ago

We have tried both. The error is identical.

bcdarwin commented 2 years ago

I have reproduced this behaviour with R 3.6.x and develop RMINC which otherwise passes the test suite. However,

pMincApply(gf$jacobians_fixed_2, testFunc, workers = 4, local = TRUE)

works correctly. If there's some other option combination without using quote which fails unexpectedly, please let me know.

Also, where in the documentation have you found this example? The test suite doesn't test pMincApply with quote, which I am tempted to consider a deprecated mode anyway (although supporting dplyr function syntax ~ . etc or even our lenses package's extended x ~ x + 2 syntax would be nice).

gdevenyi commented 2 years ago

However,

I didn't know to try testFunc bare. We were using testfunc(x) without quote. This works! Hurrah!

Also, where in the documentation have you found this example?

The first entry in examples here, https://rdrr.io/github/Mouse-Imaging-Centre/RMINC/man/mincApply.html

bcdarwin commented 2 years ago

Made some minor updates to the source documentation in 807ffc5886ae, which should be reflected downstream after the next release (when we regenerate the docs).

gdevenyi commented 2 years ago

Sorry to follow up again, we're having new issues with pMincApply and I'm not sure what to do

The goal here is to attempt to solve #298 by writing our own code.

plslmer <- function(x, data) {
  mydata = cbind(data,x)
    eff = coef(lmer(x ~time_month + (time_month||Id), data = mydata,REML = T, lmerControl(optimizer ="Nelder_Mead")))
    return(c(eff$Id$`(Intercept)`,eff$Id$time_month))
} 

testrun <- pMincApply(data$jacobians, plslmer, workers = 4 
                      , local = T, tinyMask = T, global=c("data","plslmer")) 

But we get this:

Screen Shot 2022-01-06 at 5 29 53 PM

bcdarwin commented 2 years ago

I think pMincApply expects a single-argument function; assuming your handling of the coefficients is correct, what about

plslmer <- function (data) function (x) { ... }

testrun <- pMincApply(data$jacobians, plslmer(data), ...)

although I'm not sure exactly how this interacts with global but maybe for local = T it doesn't matter.

gdevenyi commented 2 years ago

Sadly your suggestion produces the same error.

bcdarwin commented 2 years ago

So in order, I would try: (i) look for more informative errors in the log files; (ii) try mincApply instead to see if this fails in the same way; (iii) create a reprex we can look at.

jasonlerch commented 2 years ago

I will try and look at this tonight/tomorrow. A reprex would be handy, but if I think there's enough in this thread to get a handle on what you're trying to do even without one.

gdevenyi commented 2 years ago

I've tried to generate a reproducer using the test data and been unable to. Looking into how to provide a pared down dataset.