# [SciPy-user] object too deep??

Christian K ckkart@hoc....
Fri Jun 29 09:36:44 CDT 2007

```Emanuele Zattin wrote:
> hmm... i see... that must be it.
> the fact is that the number of gaussians to fit is not constant so how
> can i build a function that will fit them if params only accepts
> scalars?

Well, per set of parameters height, center_x, center_y, width you have to create
one gaussian-lambda and sum them up in the end.

> On 6/29/07, Christian K <ckkart@hoc.net> wrote:
>> 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))

gaussian() returns only a single peak 'object'. So no reason to sum here.

>>>
>>> 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)

Here you could do the summing of the gaussian peaks. By hand it would be like this:

gaussian(p[:4])(*indices(data.shape)+gaussian(p[4:8])(*indices(data.shape)+....

It should be possible to find an automatic way though.

Christian

```