mllg / batchtools

Tools for computation on batch systems
https://mllg.github.io/batchtools/
GNU Lesser General Public License v3.0
169 stars 51 forks source link

Error in (function () : unused argument (x = 3) #290

Open pulidofabs opened 1 year ago

pulidofabs commented 1 year ago

Hi,

I am having an issue with getting batchtools to work. I am following this tutorial and have edited the function to work with my phyloseq function. When I just the function on it on and get the results it works. However, when I submit it with batchtools it always fails. Can you please help.

Here is my complete code and errors: #Create function

myFct<- function() {
Fun <- tax_glom(physeq, "Function")
Fun1<-psmelt(Fun)
FunTrt <- merge_samples(Fun, "Treatment") 
FunTrt1<-psmelt(FunTrt)
names(FunTrt1)[2]<-"Trt" 
return(FunTrt1)
}

Run function to make sure it actually works

myFctNam()

#Submit slurm job


reg <- makeRegistry(file.dir="myregdir", conf.file=".batchtools.conf.R")
Njobs <- 1:4 # Define number of jobs (here 4)
ids <- batchMap(fun=myFct, x=Njobs) 
done <- submitJobs(ids, reg=reg, resources=list(partition="short", walltime=60, ntasks=10, ncpus=1, memory=1024))
waitForJobs()
``` # Wait until jobs are completed

**#Results**
**waitForJobs() # Wait until jobs are completed                               
[1] FALSE **

**getStatus()** 
Status for 4 jobs at 2023-01-13 21:27:41:
  Submitted    : 4 (100.0%)
  -- Queued    : 0 (  0.0%)
  -- Started   : 4 (100.0%)
  ---- Running : 0 (  0.0%)
  ---- Done    : 0 (  0.0%)
  ---- Error   : 4 (100.0%)
  ---- Expired : 0 (  0.0%)

**> getErrorMessages(ids, reg = getDefaultRegistry())**
   job.id terminated error                                          message
1:      1       TRUE  TRUE Error in (function ()  : unused argument (x = 1)
2:      2       TRUE  TRUE Error in (function ()  : unused argument (x = 2)
3:      3       TRUE  TRUE Error in (function ()  : unused argument (x = 3)
4:      4       TRUE  TRUE Error in (function ()  : unused argument (x = 4)
HenrikBengtsson commented 1 year ago

Hi.

Conceptually, call batchMap() the same way as you call lapply() or purrr::map(). So, when you do:

Njobs <- 1:4
ids <- batchMap(fun=myFct, x=Njobs)

and alternative would be to call:

Njobs <- 1:4
res <- lapply(FUN=myFct, X=Njobs)

If you try the latter, you'll get the same type of error;

Error in FUN(X[[i]], ...) : unused argument (X[[i]])

So, the problem is that lapply(), and batchMap() ends up calling myFct(1), myFct(2), ..., but your myFct() function does not take any arguments.

This leads to the second thinko, I think you've got: Your myFct() does not take any arguments. If you call the same function four times, wouldn't you expect it to do the exact same type of calculations four times? I suspect you don't want that. Instead, it looks like you're using physeq as some type of input data; if so, you probably need to figure out how to load different physeq data for each iteration.

PS. Please have a look at https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax how to do code-formatting here on GitHub; makes it all easier to read.

pulidofabs commented 1 year ago

@HenrikBengtsson Thank you so much for your quick response, and my apologies for the formatting on the previous post.

So the issue here is then with my function. I have never created or even attempted to create a function, so this was my first try. When I run the function as a stand-alone using this code (see code 1), the code works correctly; it just breaks when submitting the job to the cluster.

Any advice on how to get this going would be greatly appreciated!

Code 1

myFct<- function() {
Fun <- tax_glom(physeq, "Function")
Fun1<-psmelt(Fun)
FunTrt <- merge_samples(Fun, "Treatment") 
FunTrt1<-psmelt(FunTrt)
names(FunTrt1)[2]<-"Trt" 
return(FunTrt1)
}

myFctNam() #get results 
HenrikBengtsson commented 1 year ago

When I run the function as a stand-alone using this code (see code 1), the code works correctly

What do you expect from your function when you call it multiple times?

myFctNam() #get results 
myFctNam() #get results 
myFctNam() #get results 
myFctNam() #get results 

I'd say, it should be your first goal to figure that part out. That'll solve half of your problem.

I would not worry about batchtools at all, until you solved that. Instead, focus on how it should work with a for-loop or an lapply() call.

I don't have the resources to work with you on this - I recommend you reach out on Stack Overflow, or to some local R folks if you have those around.