Пример #1
0
 def test_cholesky(self):
     self.noise_inv.shape = (self.size, self.size)
     # Do a normal cholesky and compare.
     lapack_chol = linalg.cholesky(self.noise_inv)
     _c.call_cholesky(self.noise_inv)
     for ii in range(self.size):
         self.assertTrue(
             sp.allclose(self.noise_inv[ii, ii:], lapack_chol[ii, ii:]))
Пример #2
0
 def test_cholesky(self):
     self.noise_inv.shape = (self.size, self.size)
     # Do a normal cholesky and compare.
     lapack_chol = linalg.cholesky(self.noise_inv)
     _c.call_cholesky(self.noise_inv)
     for ii in range(self.size):
         self.assertTrue(sp.allclose(self.noise_inv[ii,ii:],
                                     lapack_chol[ii,ii:]))
Пример #3
0
def solve(noise_inv_filename, noise_inv, dirty_map, 
                                         return_noise_diag=False, feedback=0):
    """Solve for the clean map.

    Matrix and vector passed in as algebra objects with no block diagonality.
    The system is solved using a GPU accelerated cholesky decomposition.
    Optionally, the diagonal on the noise matrix is returned.
    """
    
    # Import the cython stuff locally so some clean maps can be made on any
    # machine.
    import _cholesky as _c
    # Put into the 2D matrix shape.
    expanded = noise_inv.view()
    side_size = noise_inv.shape[0] * noise_inv.shape[1] * noise_inv.shape[2]
    expanded.shape = (side_size,) * 2
    # Allowcate memory for the cholesky and copy the upper triangular data.
    # Instead of copying, open the matrix in copy on write mode.
    if feedback > 1:
        print "Copying matrix."
    time_before = time.time() / 60.
    # OLD WAY.
    # tri_copy = _c.up_tri_copy(expanded)
    # NEW WAY.
    tri_copy = up_tri_copy_from_file(noise_inv_filename)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nMatrix copying time: %.2f minutes." % (time_after-time_before) 
    #tri_copy = expanded
    # Get the diagonal of the giant noise inverse.
    if feedback > 1:
        print "Getting noise inverse diagonal."
    time_before = time.time() / 60.
    tri_copy.shape = side_size*side_size
    noise_inv_diag = tri_copy[::side_size+1]
    tri_copy.shape = (side_size,side_size)
    noise_inv_diag.shape = dirty_map.shape
    noise_inv_diag = algebra.as_alg_like(noise_inv_diag, dirty_map)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nNoise inverse diagonal gotten in: %.2f minutes." \
                                                 % (time_after-time_before)
    # Cholesky decompose it.
    if feedback > 1:
        print "Cholesky decomposition."
    time_before = time.time() / 60.
    _c.call_cholesky(tri_copy)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nCholesky decomposition time: %.2f minutes." \
                                                 % (time_after-time_before) 
    # Solve for the clean map.
    flat_map = dirty_map.view()
    flat_map.shape = (flat_map.size,)
    if feedback > 1:
        print "Solving for clean map."
    time_before = time.time() / 60.
    clean_map = _c.cho_solve(tri_copy, flat_map)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nClean map solving time: %.2f minutes." % (time_after-time_before) 
    # Reshape and cast as a map.
    clean_map.shape = dirty_map.shape
    clean_map = algebra.as_alg_like(clean_map, dirty_map)
    if not return_noise_diag:
        return clean_map, noise_inv_diag, tri_copy
    else:
        if feedback > 1:
            print "Getting noise diagonal."
        noise_diag = sp.empty(side_size, dtype=float)
        time_before = time.time() / 60.
        _c.inv_diag_from_chol(tri_copy, noise_diag)
        time_after = time.time() / 60.
        if feedback > 1:
            print "\nNoise diagonal gotten in: %.2f minutes." \
                                                 % (time_after-time_before) 
        noise_diag.shape = dirty_map.shape
        noise_diag = algebra.as_alg_like(noise_diag, dirty_map)
        return clean_map, noise_diag, noise_inv_diag, tri_copy
Пример #4
0
def solve(noise_inv_filename,
          noise_inv,
          dirty_map,
          return_noise_diag=False,
          feedback=0):
    """Solve for the clean map.

    Matrix and vector passed in as algebra objects with no block diagonality.
    The system is solved using a GPU accelerated cholesky decomposition.
    Optionally, the diagonal on the noise matrix is returned.
    """

    # Import the cython stuff locally so some clean maps can be made on any
    # machine.
    import _cholesky as _c
    # Put into the 2D matrix shape.
    expanded = noise_inv.view()
    side_size = noise_inv.shape[0] * noise_inv.shape[1] * noise_inv.shape[2]
    expanded.shape = (side_size, ) * 2
    # Allowcate memory for the cholesky and copy the upper triangular data.
    # Instead of copying, open the matrix in copy on write mode.
    if feedback > 1:
        print "Copying matrix."
    time_before = time.time() / 60.
    # OLD WAY.
    # tri_copy = _c.up_tri_copy(expanded)
    # NEW WAY.
    tri_copy = up_tri_copy_from_file(noise_inv_filename)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nMatrix copying time: %.2f minutes." % (time_after -
                                                        time_before)
    #tri_copy = expanded
    # Get the diagonal of the giant noise inverse.
    if feedback > 1:
        print "Getting noise inverse diagonal."
    time_before = time.time() / 60.
    tri_copy.shape = side_size * side_size
    noise_inv_diag = tri_copy[::side_size + 1]
    tri_copy.shape = (side_size, side_size)
    noise_inv_diag.shape = dirty_map.shape
    noise_inv_diag = algebra.as_alg_like(noise_inv_diag, dirty_map)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nNoise inverse diagonal gotten in: %.2f minutes." \
                                                 % (time_after-time_before)
    # Cholesky decompose it.
    if feedback > 1:
        print "Cholesky decomposition."
    time_before = time.time() / 60.
    _c.call_cholesky(tri_copy)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nCholesky decomposition time: %.2f minutes." \
                                                 % (time_after-time_before)
    # Solve for the clean map.
    flat_map = dirty_map.view()
    flat_map.shape = (flat_map.size, )
    if feedback > 1:
        print "Solving for clean map."
    time_before = time.time() / 60.
    clean_map = _c.cho_solve(tri_copy, flat_map)
    time_after = time.time() / 60.
    if feedback > 1:
        print "\nClean map solving time: %.2f minutes." % (time_after -
                                                           time_before)
    # Reshape and cast as a map.
    clean_map.shape = dirty_map.shape
    clean_map = algebra.as_alg_like(clean_map, dirty_map)
    if not return_noise_diag:
        return clean_map, noise_inv_diag, tri_copy
    else:
        if feedback > 1:
            print "Getting noise diagonal."
        noise_diag = sp.empty(side_size, dtype=float)
        time_before = time.time() / 60.
        _c.inv_diag_from_chol(tri_copy, noise_diag)
        time_after = time.time() / 60.
        if feedback > 1:
            print "\nNoise diagonal gotten in: %.2f minutes." \
                                                 % (time_after-time_before)
        noise_diag.shape = dirty_map.shape
        noise_diag = algebra.as_alg_like(noise_diag, dirty_map)
        return clean_map, noise_diag, noise_inv_diag, tri_copy