[SciPy-Dev] usage of inspect.getargspec ?

Warren Weckesser warren.weckesser@enthought....
Thu Jan 5 22:07:58 CST 2012

On Thu, Jan 5, 2012 at 9:09 PM, <josef.pktd@gmail.com> wrote:

> triggered by the recent commit, I started to look a bit more at
> inspect.getargspec
> https://github.com/scipy/scipy/commit/c9c2d66701583984589c88c08c478e2fc6d9f4ec#L1R1213
> 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
> work.
> 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 [28]: p = np.poly1d([3,2,1])

In [29]: 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,
callback, retall)
    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,
retall, callback)
   1202     Also note that the `jac` parameter (Jacobian) is required.
   1203     """
-> 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 [30]: minimize(p, 1, jac=lambda x: p.deriv()(x), method='Newton-CG')
Optimization terminated successfully.
         Current function value: 0.666667
         Iterations: 2
         Function evaluations: 3
         Gradient evaluations: 6
         Hessian evaluations: 0
Out[30]: array([-0.33333333])

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-dev/attachments/20120105/6f56b97b/attachment.html 

More information about the SciPy-Dev mailing list