def make_noise_factorizable(self, weight_prior=2):
        r"""Convert noise weights such that the factor into a function a
        frequency times a function of pixel by taking means over the original
        weights.

        weight_prior used to be 10^-30 before prior applied
        """
        print "making the noise factorizable"

        def make_factorizable(noise):
            r"""factorize the noise"""
            noise[noise < weight_prior] = 1.e-30
            noise = 1. / noise
            noise = ma.array(noise)
            # Get the freqency averaged noise per pixel.  Propagate mask in any
            # frequency to all frequencies.
            for noise_index in range(ma.shape(noise)[0]):
                if sp.all(noise[noise_index, ...] > 1.e20):
                    noise[noise_index, ...] = ma.masked
            noise_fmean = ma.mean(noise, 0)
            noise_fmean[noise_fmean > 1.e20] = ma.masked
            # Get the pixel averaged noise in each frequency.
            noise[noise > 1.e20] = ma.masked
            noise /= noise_fmean
            noise_pmean = ma.mean(ma.mean(noise, 1), 1)
            # Combine.
            noise = noise_pmean[:, None, None] * noise_fmean[None, :, :]
            noise = (1. / noise).filled(0)

            return noise

        noise_inv1 = make_factorizable(self.noise_inv1)
        noise_inv2 = make_factorizable(self.noise_inv2)
        self.noise_inv1 = algebra.as_alg_like(noise_inv1, self.noise_inv1)
        self.noise_inv2 = algebra.as_alg_like(noise_inv2, self.noise_inv2)
Beispiel #2
0
    def make_noise_factorizable(self, weight_prior=2):
        r"""Convert noise weights such that the factor into a function a
        frequency times a function of pixel by taking means over the original
        weights.

        weight_prior used to be 10^-30 before prior applied
        """
        print "making the noise factorizable"

        def make_factorizable(noise):
            r"""factorize the noise"""
            noise[noise < weight_prior] = 1.e-30
            noise = 1. / noise
            noise = ma.array(noise)
            # Get the freqency averaged noise per pixel.  Propagate mask in any
            # frequency to all frequencies.
            for noise_index in range(ma.shape(noise)[0]):
                if sp.all(noise[noise_index, ...] > 1.e20):
                    noise[noise_index, ...] = ma.masked
            noise_fmean = ma.mean(noise, 0)
            noise_fmean[noise_fmean > 1.e20] = ma.masked
            # Get the pixel averaged noise in each frequency.
            noise[noise > 1.e20] = ma.masked
            noise /= noise_fmean
            noise_pmean = ma.mean(ma.mean(noise, 1), 1)
            # Combine.
            noise = noise_pmean[:, None, None] * noise_fmean[None, :, :]
            noise = (1. / noise).filled(0)

            return noise

        noise_inv1 = make_factorizable(self.noise_inv1)
        noise_inv2 = make_factorizable(self.noise_inv2)
        self.noise_inv1 = algebra.as_alg_like(noise_inv1, self.noise_inv1)
        self.noise_inv2 = algebra.as_alg_like(noise_inv2, self.noise_inv2)
Beispiel #3
0
    def degrade_resolution(self, mode="constant"):
        r"""Convolves the maps down to the lowest resolution.

        Also convolves the noise, making sure to deweight pixels near the edge
        as well.  Converts noise to factorizable form by averaging.

        mode is the ndimage.convolve flag for behavior at the edge
        """
        print "degrading the resolution to a common beam: ", self.conv_factor
        noise1 = self.noise_inv1
        noise2 = self.noise_inv2

        # Get the beam data.
        beam_data = sp.array([
            0.316148488246, 0.306805630985, 0.293729620792, 0.281176247549,
            0.270856788455, 0.26745856078, 0.258910010848, 0.249188429031
        ])
        freq_data = sp.array([695, 725, 755, 785, 815, 845, 875, 905],
                             dtype=float)
        freq_data *= 1.0e6
        beam_diff = sp.sqrt(
            max(self.conv_factor * beam_data)**2 - (beam_data)**2)
        common_resolution = beam.GaussianBeam(beam_diff, freq_data)
        # Convolve to a common resolution.
        self.map2 = common_resolution.apply(self.map2)
        self.map1 = common_resolution.apply(self.map1)

        # This block of code needs to be split off into a function and applied
        # twice (so we are sure to do the same thing to each).
        noise1[noise1 < 1.e-30] = 1.e-30
        noise1 = 1. / noise1
        noise1 = common_resolution.apply(noise1, mode=mode, cval=1.e30)
        noise1 = common_resolution.apply(noise1, mode=mode, cval=1.e30)
        noise1 = 1. / noise1
        noise1[noise1 < 1.e-20] = 0.

        noise2[noise2 < 1.e-30] = 1.e-30
        noise2 = 1 / noise2
        noise2 = common_resolution.apply(noise2, mode=mode, cval=1.e30)
        noise2 = common_resolution.apply(noise2, mode=mode, cval=1.e30)
        noise2 = 1. / noise2
        noise2[noise2 < 1.e-20] = 0.

        self.noise_inv1 = algebra.as_alg_like(noise1, self.noise_inv1)
        self.noise_inv2 = algebra.as_alg_like(noise2, self.noise_inv2)
Beispiel #4
0
    def degrade_resolution(self, mode="constant"):
        r"""Convolves the maps down to the lowest resolution.

        Also convolves the noise, making sure to deweight pixels near the edge
        as well.  Converts noise to factorizable form by averaging.

        mode is the ndimage.convolve flag for behavior at the edge
        """
        print "degrading the resolution to a common beam: ", self.conv_factor
        noise1 = self.noise_inv1
        noise2 = self.noise_inv2

        # Get the beam data.
        beam_data = sp.array([0.316148488246, 0.306805630985, 0.293729620792,
                 0.281176247549, 0.270856788455, 0.26745856078,
                 0.258910010848, 0.249188429031])
        freq_data = sp.array([695, 725, 755, 785, 815, 845, 875, 905],
                             dtype=float)
        freq_data *= 1.0e6
        beam_diff = sp.sqrt(max(self.conv_factor * beam_data) ** 2 - (beam_data) ** 2)
        common_resolution = beam.GaussianBeam(beam_diff, freq_data)
        # Convolve to a common resolution.
        self.map2 = common_resolution.apply(self.map2)
        self.map1 = common_resolution.apply(self.map1)

        # This block of code needs to be split off into a function and applied
        # twice (so we are sure to do the same thing to each).
        noise1[noise1 < 1.e-30] = 1.e-30
        noise1 = 1. / noise1
        noise1 = common_resolution.apply(noise1, mode=mode, cval=1.e30)
        noise1 = common_resolution.apply(noise1, mode=mode, cval=1.e30)
        noise1 = 1. / noise1
        noise1[noise1 < 1.e-20] = 0.

        noise2[noise2 < 1.e-30] = 1.e-30
        noise2 = 1 / noise2
        noise2 = common_resolution.apply(noise2, mode=mode, cval=1.e30)
        noise2 = common_resolution.apply(noise2, mode=mode, cval=1.e30)
        noise2 = 1. / noise2
        noise2[noise2 < 1.e-20] = 0.

        self.noise_inv1 = algebra.as_alg_like(noise1, self.noise_inv1)
        self.noise_inv2 = algebra.as_alg_like(noise2, self.noise_inv2)
Beispiel #5
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
Beispiel #6
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