[Numpy-discussion] vectorize doesn't like functools.partial

Neal Becker ndbecker2@gmail....
Wed Sep 8 10:17:17 CDT 2010


josef.pktd@gmail.com wrote:

> On Wed, Sep 8, 2010 at 10:53 AM, Neal Becker <ndbecker2@gmail.com> wrote:
>> josef.pktd@gmail.com wrote:
>>
>>> On Wed, Sep 8, 2010 at 7:44 AM, Neal Becker <ndbecker2@gmail.com> wrote:
>>>> If I  try to use vectorize on the result of functools.partial, I seem
>>>> to get:
>>>>
>>>> ValueError: failed to determine the number of arguments for
>>>> <functools.partial object at 0x4e396d8>
>>>>
>>>> Anything I can do about it?
>>>
>>> Set .nin (attribute of vectorized function, I think) directly with
>>> number of arguments (int)
>>>
>>> Josef
>>>
>>>>
>>>> _______________________________________________
>>>> NumPy-Discussion mailing list
>>>> NumPy-Discussion@scipy.org
>>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>>
>> Not sure what you mean.  Here is a demo of 2 problems of interop of numpy
>> and functools.partial.  Tried both vectorize and frompyfunc.  vectorize
>> gives:
>> ValueError: failed to determine the number of arguments for
>> <functools.partial object at 0x230b1b0>
>>
>> frompyfunc gives:
>> TypeError: H() got multiple values for keyword argument 'iir'
>>
>> Note that doc for frompyfunc says:
>> "Takes an arbitrary Python function..."
>>
>> import numpy as np
>>
>> class iir (object):
>> def H (z):
>> return 1
>>
>> def H (iir, f):
>> z = complex (cos (2*pi*f/fs), sin (2*pi*f/fs))
>> return iir.H (z)
>>
>> f = np.linspace (0, 10e6, 1000)
>> #bug1
>> from functools import partial
>> the_iir = iir()
>> p = partial (H, iir=the_iir)
>> resp_pll = np.vectorize (p)(f)
>> #bug2
>> resp_pll = np.frompyfunc (p, 1, 1)(f)
> 
> looks like a limitation of vectorize (fails in constructor), and nin
> cannot be included in the constructor
> 
> this should be a bug report or enhancement ticket
> 
> a work around, after fixing other problems in your code example:
> 
> p = partial(H, iir=the_iir)
> fun = np.vectorize(lambda x: p(x))
> 
>>>> fun.nin
> 1
>>>> fun(5)
> array(1)
>>>> fun(f)[:10]
> array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
> 
> Josef
Yes, I found that using lamda there is no need for partial at all.
fun = np.vectorize (lambda f: H(the_iir, f))



More information about the NumPy-Discussion mailing list