[SciPy-Dev] usage of inspect.getargspec ?
Thu Jan 5 22:07:58 CST 2012
On Thu, Jan 5, 2012 at 9:09 PM, <email@example.com> wrote:
> triggered by the recent commit, I started to look a bit more at
> Until now I have seen it mainly in convenience code were it makes it
> easier for users but that I usually don't use, in this case it is part
> of essential code.
> The main problem with inspect.getargspec is that it gets easily
> confused by `self` if it is an instance method and by keywords and
> flexible arguments.
Heh--you might be projecting a bit here. You or I might get confused, but
I bet getargspec knows exactly what it is doing. :)
> recent fix for curve_fit https://github.com/scipy/scipy/pull/92
> my only other experience is with numpy.vectorize that has some tricky
> features and IIRC has some cases of flexible arguments that don't
> I don't think the changes to fmin_ncg break anything in statsmodels
> since we have identical simple signatures for the main functions,
> hessians and gradients, but I think it would break if we add a keyword
> argument to the Hessians.
> It also would be possible to work around any limitations by writing a
> wrapper or a lambda function.
> What experience do others have with inspect?
> My experience is mostly unpleasant but I never looked much at the
> details of inspect, just avoided it as much as possible.
> Is it ok to use it in basic library code like the optimizers?
> Just asking for general comments and since the pull request is closed.
getargspec does not work with, for example, an instance of a class that
implements __callable__, so the docstring for minimize is being too
optimistic when it says that hess must be callable. It actually must be a
function or method.
It looks like jac will also not work if it is an instance of a callable
In : p = np.poly1d([3,2,1])
In : minimize(p, 1, jac=p.deriv(), method='Newton-CG')
AttributeError Traceback (most recent call last)
/Users/warren/<ipython-input-29-4ff757edc58a> in <module>()
----> 1 minimize(p, 1, jac=p.deriv(), method='Newton-CG')
in minimize(fun, x0, args, method, jac, hess, options, full_output,
203 elif method.lower() == 'newton-cg':
204 return _minimize_newtoncg(fun, x0, args, jac, hess, options,
--> 205 full_output, retall, callback)
206 elif method.lower() == 'anneal':
207 if callback:
in _minimize_newtoncg(fun, x0, args, jac, hess, options, full_output,
1202 Also note that the `jac` parameter (Jacobian) is required.
-> 1204 if jac == None:
1205 raise ValueError('Jacobian is required for Newton-CG
1206 f = fun
in __eq__(self, other)
1160 def __eq__(self, other):
-> 1161 return NX.alltrue(self.coeffs == other.coeffs)
1163 def __ne__(self, other):
AttributeError: 'NoneType' object has no attribute 'coeffs'
Of course, it is easy enough to wrap it in a lambda expression:
In : minimize(p, 1, jac=lambda x: p.deriv()(x), method='Newton-CG')
Optimization terminated successfully.
Current function value: 0.666667
Function evaluations: 3
Gradient evaluations: 6
Hessian evaluations: 0
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-Dev