Runs fine locally, but crashes when using a slurm plugin with error:
AttributeError: ‘module’ object has no attribute ‘fve’
Originally, I had the input and variable name also as fve, but saw a SO post saying to name differently. Didn’t help. Also, I don’t even call the fve_func in foo (I commented out that line to debug as in my above example), so somehow it’s the passing of the function that is causing problems.
Make a string form of the function node1.inputs.fve = f2s(fve)
Inside node1, or its called functions, turn string back into function from nipype.utils.misc import create_function_from_source as s2f fve = s2f(fve)
Beware of python’s pass by reference default, which means that using the same name for the string form and function form of “fve” could lead to s2f(fve) or f2s(fve) operating on the incorrect form.
Also note: f2s() does not work on class definitions (so it seems). Class definitions can be defined in a separate file, added to path as in option 2, and then imported inside the function.
If all your function and class defs are in a jupyter notebook, and the notebook is being executed, then nothing needs to be turned into a string or imported because the defs exist within the scope of the whole notebook. This is true even if slurm is being used to launch jobs from within the notebook provided the notebook itself is being run headerless. If the notebook is being executed interactively, and some jobs are being sent to slurm, then all the scope problems occur that prompted this question.
Print statements must be written in functional form within functions that are turned into strings aka print statements must follow python 3.x rules e.g. print('hello world') NOT python 2.7 print 'hello world'