def fitGaussian2D(data, rangeFactor=0.1, xy=None): if xy is None: xyIndices = dnp.indices(data.shape) xy=[ xyIndices[1], xyIndices[0] ] ; # h0, xc0, yc0, xw0, yw0 = moments(data); # gf=dnp.fit.fit(myGaussian2DFunc, xy, data, [h0, xc0, yc0, xw0, yw0], [(4, 6), (80, 120), (80, 120), (10,30), (30,50)], optimizer='global') p0 = moments(data); rp=[(p*(1.0-rangeFactor), p*(1.0+rangeFactor)) for p in p0] gf=dnp.fit.fit(myGaussian2DFunc, xy, data, p0, rp, optimizer='local') print gf; height, center_x, center_y, sigma_x, sigma_y=gf.parameters; return [height, center_x, center_y, sigma_x, sigma_y];
def moments(data): """ Returns (height, x, y, sigma_x, sigma_y) the gaussian parameters of a 2D distribution by calculating its moments """ total = data.sum() xyIndices = dnp.indices(data.shape) X=xyIndices[1]; Y=xyIndices[0]; x = (X*data).sum()/total y = (Y*data).sum()/total row = data[int(y), :] sigma_x = math.sqrt(dnp.abs((dnp.arange(row.size)-x)**2*row).sum()/row.sum()) col = data[:, int(x)] sigma_y = math.sqrt( dnp.abs((dnp.arange(col.size)-y)**2*col).sum()/col.sum() ) height = data.max() return [height, x, y, sigma_x, sigma_y];
def testIndices(self): print 'Indices testing' x, y = np.indices((516, 516))
def testIndices(self): print "Indices testing" x, y = np.indices((516, 516))