[SciPy-user] vectorize(function)/arraymap did not return arrays?

Yichun Wei yichunwe at usc.edu
Thu Jan 20 15:24:09 EST 2005


Hello Travis,

Sorry I'm posting this for more detailed description.

I have problem with vectorize class and/or arraymap method. I supposed 
vectorized callable objects constructed by scipy.vectorize class could 
return arrays, (I was evaluating a function over a 2-d array, and each 
function call should return an 1-d array at every point in this 2-d 
array as a result. I would like to construct a 3-d array with these 
results.) However I could not get the results as expected:

#------------------------------
a = ones((16,16,100))
k = ones((16,16,10))

def helper(x,y):
     return myconvolve (a[x,y], k[x,y]) [NewAxis,NewAxis,:]

vec_helper = vectorize(helper)
x, y  = ogrid[ 0:16, 0:16 ]

lres  = zeros((16,16),'O')
lres  = vec_helper(x,y)
#------------------------------

I got the following error messages:

Traceback (most recent call last):
   File "convolvefft.py", line 54, in ?
     lres  = vec_helper(x,y)
   File "C:\Python23\Lib\site-packages\scipy_base\function_base.py", 
line 457, in
  __call__
     return squeeze(arraymap(self.thefunc,args,self.otypes))
TypeError: only length-1 arrays can be converted to Python scalars.

I read through the previous posts on this list by Travis:
http://mail.python.org/pipermail/python-list/1999-June/004440.html
Also I find on Travis's pylab pages:
"There is also a class that allows wrapping an arbitrary Python function 
with scalar inputs or outpus so that the wrapped function behaves like a 
ufunc (taking array arguments and returning array array arguments". Thus 
I supposed this be a mature feature in Scipy. However I could not get my 
results as expected. Is it a wrong function call here? The call to 
arraymap is implemented in C, so I really do not know what can I do to 
manage this.


The second problem is, according to a posts long ago 
(http://mail.python.org/pipermail/python-list/1999-June/004440.html)
The code

lres  = zeros((16,16),'O')
lres  = vec_helper(x,y)

does not work either even when vec_helper returns arrays as expected.
There was no concrete anwser to this. The work-around suggested by 
Konrad Hinsen does not work for me. I suppose there should be some 
better elegant way to manage this, otherwise people won't stop talking 
about this issue... I dug a little via google but it could not tell me 
more on it.

Thanks in advance! Also thanks for your fftconvolve, I saw it in CVS 
version of scipy.


- yichun




More information about the SciPy-User mailing list