[SciPy-user] object too deep??

Christian K ckkart@hoc....
Fri Jun 29 07:54:42 CDT 2007


Emanuele Zattin wrote:
> I have this optimization problem:
> 
> this function returns the sum of some gaussians given their parameters
> in arrays:
> 
> def gaussian(height, center_x, center_y, width):
>     """Returns a gaussian function with the given parameters"""
>     width = float(width)
>     return lambda x,y:
> sum(height*exp(-(((center_x-x)/width)**2+((center_y-y)/width)**2)/2))
> 
> this function tries to fit given a starting image:
> 
> def fitgaussian(data, obj_x, obj_y, obj_v):
>     """Returns (height, x, y, width)
>     the gaussian parameters of a 2D distribution found by a fit"""
>     #params = moments(data)
>     params = obj_v, obj_x-obj_x[0]+2, obj_y-obj_y[0]+2, ones(len(obj_x))

params is a tuple of some objects which you don't tell us what they are and at
least one ndarray (ones(....)). This is probably the error. params has to be a
list or array contatining only scalars.

>     errorfunction = lambda p: ravel(gaussian(*p)(*indices(data.shape)) - data)
>     p, success = leastsq(errorfunction, params)
>     return p
> 
> and i use them with:
> 
> # how many maxima here?
> max_list = [i]
> for j in range(len(obj_x)):
> 	if obj_x[j] >= x1 and obj_x[j] < x2 and obj_y[j] >= y1 and obj_y[j] <
> y2 and j != i:
> 		max_list.append(j)
> #for indices in max_list:
> ml = array(max_list)
> params = fitgaussian(neigh, obj_x[ml], obj_y[ml], obj_v[ml])
> print len(max_list), params
> 
> but i get an error like:
> 
> In [9]: run cutoff
> ---------------------------------------------------------------------------
> <type 'exceptions.ValueError'>            Traceback (most recent call last)
> 
> /home/emanuelez/Tesi/Code/cutoff.py in <module>()
>     174 # FIND OBJECTS PROPERTIES
>     175 # -----------------------
> --> 176 get_objects_info(blurred, 2, obj_x, obj_y, obj_v)
>     177
>     178
> 
> /home/emanuelez/Tesi/Code/cutoff.py in get_objects_info(image, size,
> obj_x, obj_y, obj_v)
>     143                 #for indices in max_list:
>     144                 ml = array(max_list)
> --> 145                 params = fitgaussian(neigh, obj_x[ml],
> obj_y[ml], obj_v[ml])
>     146                 print len(max_list), params
>     147
> 
> /home/emanuelez/Tesi/Code/cutoff.py in fitgaussian(data, obj_x, obj_y, obj_v)
>     124     params = obj_v, obj_x-obj_x[0]+2, obj_y-obj_y[0]+2, ones(len(obj_x))
>     125     errorfunction = lambda p:
> ravel(gaussian(*p)(*indices(data.shape)) - data)
> --> 126     p, success = leastsq(errorfunction, params)
>     127     return p
>     128
> 
> /usr/lib/python2.5/site-packages/scipy/optimize/minpack.py in
> leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol,
> gtol, maxfev, epsfcn, factor, diag)
>     264         if (maxfev == 0):
>     265             maxfev = 200*(n+1)
> --> 266         retval =
> _minpack._lmdif(func,x0,args,full_output,ftol,xtol,gtol,maxfev,epsfcn,factor,diag)
>     267     else:
>     268         if col_deriv:
> 
> <type 'exceptions.ValueError'>: object too deep for desired array
> WARNING: Failure executing file: <cutoff.py>
> 
> 
> What does "object too deep for desired array" mean? I'm really puzzled
> about this.
> 
> Thanks for any help or suggestion!
> 
> Emanuele

All those lambdas look pretty complicated to me. But I'll believe you if you say
that this is clever programming :)

Christian



More information about the SciPy-user mailing list