Spinmob/spinmob

Constants are ignored when a function is supplied to a `fitter` as a callable

Closed this issue · 6 comments

When a model function is supplied to a fitter instance as a string, everything works as expected. Supplying an actual callable function, though, results in constants being ignored (all arguments are treated as parameters). The inspect module has some metaprogramming stuff that might be helpful in finding a fix—namely, getargspec plus some substitution and a lambda could probably be made to do something useful.

Having the ability to input functions as well as strings sounds like a nice improvement. inspect is part of the standard library, which is good.

@jaxankey , what do you think?

The functionality for using callables already (mostly) exists; see https://github.com/Spinmob/spinmob/blob/master/_data.py#L1236-L1242

The issue is that, when this is how functions are supplied, they don't seem to "fix" any parameters, even those listed as constants.

Right. Looks like a bug, not an enhancement.

This docstring says it can be a function:
https://github.com/Spinmob/spinmob/blob/master/_data.py#L1142-L1146

That's a good work-around; hadn't thought of that. Rather than disallowing supplying callables all together why not just change

self.f.append(f[n])
self._fnames.append(f[n].__name__)

to something along the lines of

self._globals[f[n].__name__] = f[n]
self.f.append(eval('lambda ' + pstring + ': {}(x)'.format(f[n].__name__), self._globals))
self._fnames.append(f[n].__name__)

at https://github.com/Spinmob/spinmob/blob/master/_data.py#L1241-L1242? Would that fix the constants issue?

(I haven't tested this)