示例#1
0
    def test_array_vs_known(self):

        interp = Bcinterp( np.arange(1000**2),
                           0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                           
        xa = np.array([1.5, 20.3])
        ya = np.array([1.5, 2.0])
        
        i = interp.evaluate(xa, ya)
        assert_almost_equal( i, np.array([15015.0, 20203.0]) )
示例#2
0
 def setup(self):
             
     # initialize parameters
     dim1 = 100
     dim2 = 100
     
     self.vals = np.abs(np.random.randn(dim1 * dim2))
     self.x_space = 0.1
     self.y_space = 0.1
     self.Xdim = dim1
     self.Ydim = dim2
     self.x_corner = 0.0
     self.y_corner = 0.0
             
     self.interp = Bcinterp( self.vals, 
                             self.x_space, self.y_space, 
                             self.Xdim, self.Ydim,
                             self.x_corner, self.y_corner )
                             
     # generate a reference -- need better reference
     self.new_x = np.arange(0.0, 8.0, .01) + 0.1
     self.new_y = np.arange(0.0, 8.0, .01) + 0.1
示例#3
0
文件: math2.py 项目: rinkelp/odin
def find_center(image2d, mask=None, initial_guess=None, pix_res=0.1):
    """
    Locates the center of a circular image.
    """

    logger.info('Finding the center of the strongest ring...')

    x_size = image2d.shape[0]
    y_size = image2d.shape[1]

    if mask != None:
        image2d *= mask.astype(np.bool)

    if initial_guess == None:
        initial_guess = np.array(image2d.shape) / 2.0

    # generate a radial grid
    num_phi = 360
    phi = np.linspace(0.0,
                      2.0 * np.pi * (float(num_phi - 1) / num_phi),
                      num=num_phi)
    r = np.arange(pix_res, np.min([x_size / 3., y_size / 3.]), pix_res)
    num_r = len(r)

    rx = np.repeat(r, num_phi) * np.cos(np.tile(phi, num_r))
    ry = np.repeat(r, num_phi) * np.sin(np.tile(phi, num_r))

    # interpolate the image
    interp = Bcinterp(image2d.T.flatten(), 1.0, 1.0, x_size, y_size, 0.0, 0.0)

    def objective(center):
        """
        Returns the peak height in radial space.
        """

        ri = interp.evaluate(rx + center[0], ry + center[1])
        a = np.mean(ri.reshape(num_r, num_phi), axis=1)
        m = np.max(a)

        return -1.0 * m

    logger.debug('optimizing center position')
    center = optimize.fmin_powell(objective,
                                  initial_guess,
                                  xtol=pix_res,
                                  disp=0)
    logger.debug('Optimal center: %s (Delta: %s)' %
                 (center, initial_guess - center))

    return center
示例#4
0
 def test_two_dim(self):
     
     xa = np.array([1.5, 20.3])
     ya = np.array([1.5, 2.0])
     
     interp1 = Bcinterp( np.arange(1000**2),
                        0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                        
     interp2 = Bcinterp( np.arange(1000**2).reshape(1000,1000),
                        0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                        
     assert_array_almost_equal(interp1.evaluate(xa, ya), interp2.evaluate(xa, ya))
示例#5
0
 def test_point_vs_known(self):
     interp = Bcinterp( np.arange(1000**2),
                        0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                        
     # the below were generated from a ref implementation, but can also
     # be evaluated readily by eye
     
     i = interp.evaluate(1.01, 1.01)
     assert_almost_equal(i, 10110.1, decimal=1)
     
     i = interp.evaluate(1.5, 1.5)
     assert_almost_equal(i, 15015.0)
     
     i = interp.evaluate(2.3, 2.0)
     assert_almost_equal(i, 20023.0)
     
     i = interp.evaluate(20.3, 2.0)
     assert_almost_equal(i, 20203.0)
示例#6
0
 def test_trivial(self):
     interp = Bcinterp( np.ones(10*10), 1.0, 1.0, 10, 10, 0.0, 0.0 )
     interp_vals = interp.evaluate( np.arange(1,9) + 0.1, np.ones(8) + 0.1 )
     assert_allclose(np.ones(8), interp_vals)
示例#7
0
class TestBcinterp():
    
    def setup(self):
                
        # initialize parameters
        dim1 = 100
        dim2 = 100
        
        self.vals = np.abs(np.random.randn(dim1 * dim2))
        self.x_space = 0.1
        self.y_space = 0.1
        self.Xdim = dim1
        self.Ydim = dim2
        self.x_corner = 0.0
        self.y_corner = 0.0
                
        self.interp = Bcinterp( self.vals, 
                                self.x_space, self.y_space, 
                                self.Xdim, self.Ydim,
                                self.x_corner, self.y_corner )
                                
        # generate a reference -- need better reference
        self.new_x = np.arange(0.0, 8.0, .01) + 0.1
        self.new_y = np.arange(0.0, 8.0, .01) + 0.1


    def test_for_smoke(self):
        ip = self.interp.evaluate(self.new_x, self.new_y)
        if np.all( ip == 0.0 ):
            print "Interpolator not working, likely cause: OMP failure."
            print "Try reinstalling with no OMP: python setup.py install --no-openmp"
            raise Exception()
    
    def test_point_vs_known(self):
        interp = Bcinterp( np.arange(1000**2),
                           0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                           
        # the below were generated from a ref implementation, but can also
        # be evaluated readily by eye
        
        i = interp.evaluate(1.01, 1.01)
        assert_almost_equal(i, 10110.1, decimal=1)
        
        i = interp.evaluate(1.5, 1.5)
        assert_almost_equal(i, 15015.0)
        
        i = interp.evaluate(2.3, 2.0)
        assert_almost_equal(i, 20023.0)
        
        i = interp.evaluate(20.3, 2.0)
        assert_almost_equal(i, 20203.0)
        
    def test_two_dim(self):
        
        xa = np.array([1.5, 20.3])
        ya = np.array([1.5, 2.0])
        
        interp1 = Bcinterp( np.arange(1000**2),
                           0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                           
        interp2 = Bcinterp( np.arange(1000**2).reshape(1000,1000),
                           0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                           
        assert_array_almost_equal(interp1.evaluate(xa, ya), interp2.evaluate(xa, ya))
        
    def test_trivial(self):
        interp = Bcinterp( np.ones(10*10), 1.0, 1.0, 10, 10, 0.0, 0.0 )
        interp_vals = interp.evaluate( np.arange(1,9) + 0.1, np.ones(8) + 0.1 )
        assert_allclose(np.ones(8), interp_vals)
        
    def test_array_vs_known(self):

        interp = Bcinterp( np.arange(1000**2),
                           0.1, 0.1, 1000, 1000, 0.0, 0.0 )
                           
        xa = np.array([1.5, 20.3])
        ya = np.array([1.5, 2.0])
        
        i = interp.evaluate(xa, ya)
        assert_almost_equal( i, np.array([15015.0, 20203.0]) )