Exemplo n.º 1
0
    def evaluate_grid_approx3(self, x0, x1, y0, y1, fx, fy, minval,
                              derivs=False, minradius=3, doslice=True,
                              maxmargin=100):
        '''
        minval: small value at which to stop evaluating

        [x0,x1): (int) X values to evaluate
        [y0,y1): (int) Y values to evaluate
        (fx,fy): (float) pixel offset of the MoG; ie, evaluate MoG shifted by
                this amount.

        'maxmargin': don't render sources more than this distance outside the box.

        If 'doslice' is True, slices the images down to the non-zero
        bounding-box.

        If 'derivs' is True, computes and returns x and y derivatives too.
        
        Unlike evaluate_grid_approx, returns a Patch object.
        '''
        from mix import c_gauss_2d_approx3

        result = np.zeros((y1-y0, x1-x0))
        xderiv = yderiv = mask = None
        if derivs:
            xderiv = np.zeros_like(result)
            yderiv = np.zeros_like(result)

        # guess:
        cx = int(self.mean[0,0] + fx)
        cy = int(self.mean[0,1] + fy)

        if (cx < x0 - maxmargin or cx > x1 + maxmargin or
            cy < y0 - maxmargin or cy > y1 + maxmargin):
            return None #Patch(x0,y0,None)

        try:
            rtn,sx0,sx1,sy0,sy1 = c_gauss_2d_approx3(
                int(x0), int(x1), int(y0), int(y1),
                float(fx), float(fy), float(minval),
                self.amp, self.mean, self.var,
                result, xderiv, yderiv, mask,
                cx, cy, int(minradius))
        except:
            print 'failure calling c_gauss_2d_approx3:'
            print x0, x1, y0, y1
            print fx, fy, minval
            print cx, cy, minradius
            print '-->', int(x0), int(x1), int(y0), int(y1)
            print '-->', float(fx), float(fy), float(minval)
            print '-->', cx, cy, int(minradius)
            raise
        assert(rtn == 0)
        if doslice:
            slc = slice(sy0,sy1),slice(sx0,sx1)
            result = result[slc].copy()
            if derivs:
                xderiv = xderiv[slc].copy()
                yderiv = yderiv[slc].copy()
            x0 += sx0
            y0 += sy0

        if derivs:
            return (Patch(x0,y0,result), Patch(x0,y0,xderiv),
                    Patch(x0,y0,yderiv))

        return Patch(x0,y0,result)
Exemplo n.º 2
0
        plt.imshow(result2 - r2,
                   interpolation='nearest', origin='lower')
        plt.colorbar()
        plt.title('Approx 2 - Grid')
        plt.suptitle('j = %i' % j)
        ps.savefig()

        
        assert(np.all(np.abs(r2 - result2) < minval))

        result3 = np.zeros((H, W))
        xderiv, yderiv, mask = None, None, None
        xc,yc = int(mean[0,0] + dx), int(mean[0,1] + dy)
        args = (x0, x1, y0, y1, dx, dy, minval, amp, mean, var, result3, xderiv, yderiv, mask, xc, yc, minradius)
        print 'args (approx3):', args
        rtn = c_gauss_2d_approx3(*args)
        if rtn == -1:
            raise RuntimeError('c_gauss_2d_approx3 failed')
        print 'Max difference 3:', np.max(np.abs(r2 - result3))

        plt.clf()
        plt.subplot(1,2,1)
        plt.imshow(np.log10(np.maximum(minval * 1e-3, result3)),
                   interpolation='nearest', origin='lower')
        plt.colorbar()
        plt.title('Approx 3')

        plt.subplot(1,2,2)
        plt.imshow(result3 - r2,
                   interpolation='nearest', origin='lower')
        plt.colorbar()