Exemplo n.º 1
0
def run_scipt():

    '''Run the script'''

    print 'Running Check Residual Script...'

    # Read data files.
    data_rec = np.load(opts.rec_data)
    data_noisy = np.load(opts.noise_data)
    psf = np.load(opts.psf)

    # Convolve PSF with reconstruction.
    data_conv = convolve(data_rec, psf, psf_type=opts.psf_type,
                         data_format=opts.data_format)

    # Convert cube data to map.
    if opts.data_format == 'cube':

        if isinstance(opts.layout, type(None)):
            opts.layout = tuple(mfactor(data_noisy.shape[0]))

        data_rec = cube2map(data_rec, opts.layout)
        data_noisy = cube2map(data_noisy, opts.layout)
        data_conv = cube2map(data_conv, opts.layout)

    # Calculate the residual.
    residual = (data_noisy - data_conv)

    # Return all data.
    return (data_noisy, data_rec, data_conv, residual)
Exemplo n.º 2
0
def do_stuff():

    print "1- Reading full clean image data cube from file."

    # Read data cube binary and store first N objects.
    data_cube = np.load(path + data_file)[: opts.n_obj]

    # Save the sampled data cube sample to a binary file.
    if opts.save_cube:
        np.save(path + "deep_galaxy_cube_" + str(opts.n_obj) + ".npy", data_cube)

    print "2- Constructing data map from cube sample."

    # Find the most appropriate map layout for N objects.
    layout = mfactor(data_cube.shape[0])

    # Transform the data cube into a map.
    data_map = transform.cube2map(data_cube, layout)

    # Save the data map to a binary file.
    if opts.save_map:
        np.save(path + "deep_galaxy_map_" + str(opts.n_obj) + ".npy", data_map)

    if opts.no_psf_gen:
        print "3- Constructing pixel variant PSF from map."

        # Generate the pixel variant PSF stack.
        psf = psf_gen.pixel_var_psf(data_map.shape, opts.variance)

        # Reshape the PSF stack to data cube format and save it to binary file.
        # Notes: The default stack is the number of map pixels, followed by the
        # PSF shape. The data cube format requires that the default stack be
        # the number of galaxies, followed by the number of pixels per galaxy
        # image and finally the PSF shape (hence the strange indexing below).
        if not opts.full_map_psf:
            index = np.arange(psf.shape[0]).reshape(data_map.shape)
            index = transform.map2cube(index, layout=layout).reshape(index.size)
            psf = psf[index].reshape([data_cube.shape[0]] + [np.prod(data_cube.shape[1:])] + list(psf.shape[-2:]))
            np.save(path + "deep_galaxy_psf_cube_" + str(opts.n_obj) + ".npy", psf)

        # Just save the PSF in map format to a binary file.
        else:
            np.save(path + "deep_galaxy_psf_map_" + str(opts.n_obj) + ".npy", psf)
Exemplo n.º 3
0
def get_noise_est():

    print '1- Estimating noise from PSF.'

    if opts.full_map_psf:
        form_tag = 'map_'
    else:
        form_tag = 'cube_'

    if opts.psf_type == 'fixed':
        psf_tag = 'fix'
        psf = np.load(path + opts.psf + '.npy')
        psf = psf_pcs.reshape([1] + list(psf_pcs))

    elif opts.psf_type == 'variant':
        psf_tag = 'var'
        psf_pcs = np.load(path + 'psf_pcs_' + form_tag + str(opts.n_obj) +
                          '.npy')
        psf_coef = np.load(path + 'psf_coef_' + form_tag + str(opts.n_obj) +
                           '.npy')

        if not opts.full_map_psf:
            psf_coef = np.transpose(psf_coef, axes=[1, 0, 2, 3])
            psf_coef = psf_coef.reshape([psf_coef.shape[0]] +
                                        list(mfactor(opts.n_obj) *
                                        np.array(psf_coef.shape[2:])))

    kernel_shape = np.array(psf_pcs[0].shape)
    coef_shape = np.array(psf_coef.shape[-2:])

    print coef_shape

    vec_length = np.prod(kernel_shape)

    pcst = [rot_and_roll(x.T) for x in psf_pcs]
    pcs = [rot_and_roll(x) for x in psf_pcs]
    mask = gen_mask(kernel_shape, coef_shape)

    k_pat = kernel_pattern(kernel_shape, mask)

    k_rolls = roll_sequence(kernel_shape)
    m_rolls = roll_sequence(mask.shape)

    res = []
    i = 0
    val = 0

    widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
               ' ', ETA(), ' ', FileTransferSpeed()]
    pbar = ProgressBar(widgets=widgets, maxval=10000000).start()

    for i in range(np.prod(coef_shape)):
        for k1 in range(psf_pcs.shape[0]):
            for k2 in range(psf_pcs.shape[0]):
                pkt = roll_2d(pcst[k1],
                              roll_rad=k_rolls[k_pat[i]]).reshape(vec_length)
                pk = roll_2d(pcs[k2],
                             roll_rad=k_rolls[k_pat[i]]).reshape(vec_length)
                mi = roll_2d(mask, roll_rad=m_rolls[i])
                dk1 = psf_coef[k1][mi]
                dk2 = psf_coef[k2][mi]
                val += sum(pkt * dk1 * dk2 * pk)
        res.append(val)
        pbar.update(10*i+1)

    pbar.finish()

    res = opts.sigma ** 2 * np.array(res).reshape(coef_shape)

    if not opts.full_map_psf:
        res = transform.map2cube(res, mfactor(opts.n_obj))

    np.save(path + 'psf_noise_est_' + form_tag + str(opts.n_obj)+ '_' +
            psf_tag + '.npy', res)
def deconvolve():

    print '1- Performing deconvolution.'
    print ' - with mode:', opts.mode

    psf = None
    psf_pcs = None
    psf_coef = None

    if opts.full_map_psf:
        form_tag = 'map_'
    else:
        form_tag = 'cube_'

    # Read in fixed PSF file.
    if not isinstance(opts.psf, type(None)):
        psf_tag = 'fix'
        psf = np.load(path + opts.psf + '.npy')
        noise_est = opts.sigma

    # Read in PSF principal components and coefficients, and noise estiamte.
    else:
        psf_tag = 'var'
        psf_pcs = np.load(path + 'psf_pcs_' + form_tag + str(opts.n_obj) +
                          '.npy')
        psf_coef = np.load(path + 'psf_coef_' + form_tag + str(opts.n_obj) +
                           '.npy')
        noise_est = np.load(path + 'psf_noise_est_' + form_tag +
                            str(opts.n_obj) + '_' + psf_tag + '.npy')

    # Read in noisy data file.
    data_noisy = np.load(path + 'deep_galaxy_noise_' + form_tag +
                         str(opts.n_obj) + '_' + psf_tag + '.npy')

    # Set mr_transform wavelet type option.
    wavelet_opt = '-t ' + opts.wavelet_type

    # Perform reconstruction.
    layout = mfactor(opts.n_obj)

    # if opts.full_map_psf:
    #     data_rec = reconstruct4.reconstruct_map(data_noisy, noise_est, layout,
    #                                             psf, psf_pcs, psf_coef,
    #                                             opts.wavelet_levels,
    #                                             wavelet_opt,
    #                                             opts.wave_thresh_factor,
    #                                             opts.lowr_thresh_factor,
    #                                             opts.n_reweights, opts.mode)

    if opts.full_map_psf:
        data_format = 'map'
    else:
        data_format = 'cube'

    primal_rec, dual_rec = reconstruct4.reconstruct(data_noisy, noise_est,
                                                    layout, psf, psf_pcs,
                                                    psf_coef,
                                                    opts.wavelet_levels,
                                                    wavelet_opt,
                                                    opts.wave_thresh_factor,
                                                    opts.lowr_thresh_factor,
                                                    opts.n_reweights,
                                                    opts.mode, opts.no_pos,
                                                    data_format)

    # Save results to a binary file.

    if isinstance(opts.output, type(None)):

        np.save(path + 'deep_galaxy_rec_' + form_tag + str(opts.n_obj) + '_' +
                psf_tag + '_' + opts.mode + '.npy', primal_rec)

        np.save(path + 'deep_galaxy_dual_' + form_tag + str(opts.n_obj) + '_' +
                psf_tag + '_' + opts.mode + '.npy', dual_rec)

    else:

        np.save(path + opts.output + '_primal', primal_rec)
        np.save(path + opts.output + '_dual', dual_rec)

    if opts.full_map_psf:
        return (data_noisy, primal_rec)
    else:
        return (transform.cube2map(data_noisy, layout),
                transform.cube2map(primal_rec, layout))