예제 #1
0
def svd_decomp(array, angle_list, size_patch, inrad, outrad, sca, k_list,
               collapse_func, neg_ang=True, lr_mode='eigen', nproc=1,
               interp='bilinear', mode='mlar'):
    """
    """
    cube = array
    nfr, frsize, _ = cube.shape
    half_sizep = np.ceil(size_patch / 2.)
    inradius = inrad - half_sizep - 1
    outradius = outrad + half_sizep + 1

    matrix, ann_ind = prepare_matrix(cube, scaling=sca, mask_center_px=None,
                                     mode='annular', inner_radius=inradius,
                                     outer_radius=outradius, verbose=False)

    V = svd_wrapper(matrix, lr_mode, k_list[-1], False, False, to_numpy=False)
    cube_residuals = []

    if neg_ang:
        fac = -1
    else:
        fac = 1

    for k in k_list:
        if lr_mode in ['cupy', 'randcupy', 'eigencupy']:
            matrix = cupy.array(matrix)
            transformed = cupy.dot(V[:k], matrix.T)
            reconstructed = cupy.dot(transformed.T, V[:k])
            residuals_ann = matrix - reconstructed
            residuals_ann = cupy.asnumpy(residuals_ann)
        # elif lr_mode in ['pytorch', 'randpytorch', 'eigenpytorch']:
        #     matrix = matrix.astype('float32')
        #     matrix_gpu = torch.Tensor.cuda(torch.from_numpy(matrix))
        #     transformed = torch.mm(V[:k], torch.transpose(matrix_gpu, 0, 1))
        #     reconstructed = torch.mm(torch.transpose(transformed, 0, 1), V[:k])
        #     residuals_ann = matrix_gpu - reconstructed
        else:
            transformed = np.dot(V[:k], matrix.T)
            reconstructed = np.dot(transformed.T, V[:k])
            residuals_ann = matrix - reconstructed

        # TODO: fix bottleneck when nframes grows. Derot. in parallel batches
        residual_frames = np.zeros((nfr, frsize, frsize))
        residual_frames[:, ann_ind[0], ann_ind[1]] = residuals_ann
        residual_frames_rot = cube_derotate(residual_frames, fac * angle_list,
                                            nproc=nproc, interpolation=interp)
        if mode == 'mlar':
            cube_residuals.append(collapse_func(residual_frames_rot, axis=0))
        elif mode == 'tmlar':
            cube_residuals.append(residual_frames_rot)
        elif mode == 'tmlar4d':
            cube_residuals.append(residual_frames_rot)

    cube_residuals = np.array(cube_residuals)
    if mode == 'tmlar':
        cube_residuals = np.mean(cube_residuals, axis=0)
    elif mode == 'tmlar4d':
        cube_residuals = np.moveaxis(cube_residuals, 1, 0)

    return cube_residuals
예제 #2
0
def _compute_residual_frame(cube,
                            angle_list,
                            radius,
                            fwhm,
                            wavelengths=None,
                            mode='pca',
                            ncomp=2,
                            svd_mode='lapack',
                            scaling=None,
                            collapse='median',
                            imlib='opencv',
                            interpolation='lanczos4'):
    """
    """
    if cube.ndim == 3:
        if mode == 'pca':
            annulus_width = 3 * fwhm
            data, ind = prepare_matrix(cube,
                                       scaling,
                                       mode='annular',
                                       annulus_radius=radius,
                                       verbose=False,
                                       annulus_width=annulus_width)
            yy, xx = ind
            V = svd_wrapper(data, svd_mode, ncomp, False, False)
            transformed = np.dot(V, data.T)
            reconstructed = np.dot(transformed.T, V)
            residuals = data - reconstructed
            cube_empty = np.zeros_like(cube)
            cube_empty[:, yy, xx] = residuals
            cube_res_der = cube_derotate(cube_empty,
                                         angle_list,
                                         imlib=imlib,
                                         interpolation=interpolation)
            res_frame = cube_collapse(cube_res_der, mode=collapse)

        elif mode == 'median':
            res_frame = median_sub(cube, angle_list, verbose=False)

    elif cube.ndim == 4:
        if mode == 'pca':
            pass

        elif mode == 'median':
            res_frame = median_sub(cube,
                                   angle_list,
                                   scale_list=wavelengths,
                                   verbose=False)

    return res_frame
예제 #3
0
def prepare_patches(cube,
                    angle_list,
                    xy,
                    fwhm,
                    patch_size_px,
                    delta_rot=0.5,
                    normalization='slice',
                    imlib='opencv',
                    interpolation='bilinear',
                    debug=False):
    """ Prepare patches for SODINN-PW.
    """
    centy_fr, centx_fr = frame_center(cube[0])

    angle_list = check_pa_vector(angle_list)

    xy_dist = dist(centy_fr, centx_fr, xy[1], xy[0])
    res = _pairwise_diff_residuals(cube,
                                   angle_list,
                                   ann_center=xy_dist,
                                   fwhm=fwhm,
                                   delta_rot=delta_rot,
                                   debug=False)

    res_der = cube_derotate(res,
                            angle_list,
                            imlib=imlib,
                            interpolation=interpolation)
    res_der_crop = cube_crop_frames(res_der,
                                    patch_size_px,
                                    xy=xy,
                                    verbose=False,
                                    force=True)

    patches = normalize_01_pw(res_der_crop, normalization)

    if debug:
        print('dist : {}'.format(xy_dist))
        plot_frames(
            tuple(patches),
            axis=False,
            colorbar=False,
        )
    return patches
예제 #4
0
def _compute_residual_frame(cube,
                            angle_list,
                            radius,
                            fwhm,
                            wavelengths=None,
                            mode='pca',
                            n_ks=3,
                            svd_mode='randsvd',
                            scaling='temp-standard',
                            collapse='median',
                            imlib='opencv',
                            interpolation='bilinear',
                            debug=False):
    """
    """
    if cube.ndim == 3:
        annulus_width = 4 * fwhm
        inrad = radius - int(np.round(annulus_width / 2.))
        outrad = radius + int(np.round(annulus_width / 2.))

        if mode == 'pca':
            angle_list = check_pa_vector(angle_list)
            svdecomp = SVDecomposer(cube,
                                    mode='annular',
                                    inrad=inrad,
                                    outrad=outrad,
                                    svd_mode=svd_mode,
                                    scaling=scaling,
                                    wavelengths=None,
                                    verbose=False)
            _ = svdecomp.get_cevr(plot=False)

            # n_ks == 1 or n_ks == 3
            k_list = [max(1, svdecomp.cevr_to_ncomp(0.90))]
            if n_ks == 3:
                k_list.append(max(2, svdecomp.cevr_to_ncomp(0.95)))
                k_list.append(max(3, svdecomp.cevr_to_ncomp(0.99)))

            if debug:
                print(k_list)

            res_frame = []
            for k in k_list:
                transformed = np.dot(svdecomp.v[:k], svdecomp.matrix.T)
                reconstructed = np.dot(transformed.T, svdecomp.v[:k])
                residuals = svdecomp.matrix - reconstructed
                cube_empty = np.zeros_like(cube)
                cube_empty[:, svdecomp.yy, svdecomp.xx] = residuals
                cube_res_der = cube_derotate(cube_empty,
                                             angle_list,
                                             imlib=imlib,
                                             interpolation=interpolation)
                res_frame.append(cube_collapse(cube_res_der, mode=collapse))

        elif mode == 'median':
            res_frame = median_sub(cube, angle_list, verbose=False)

    elif cube.ndim == 4:
        inrad = max(1, radius - int(np.round(2 * fwhm)))
        outrad = min(int(cube.shape[-1] / 2.),
                     radius + int(np.round(5 * fwhm)))

        if mode == 'pca':
            z, n, y_in, x_in = cube.shape
            angle_list = check_pa_vector(angle_list)
            scale_list = check_scal_vector(wavelengths)
            svdecomp = SVDecomposer(cube,
                                    mode='annular',
                                    inrad=inrad,
                                    outrad=outrad,
                                    svd_mode=svd_mode,
                                    scaling=scaling,
                                    wavelengths=scale_list,
                                    verbose=False)
            _ = svdecomp.get_cevr(plot=False)

            # n_ks == 1 or n_ks == 3
            k_list = [max(1, svdecomp.cevr_to_ncomp(0.90))]
            if n_ks == 3:
                k_list.append(max(2, svdecomp.cevr_to_ncomp(0.95)))
                k_list.append(max(3, svdecomp.cevr_to_ncomp(0.99)))

            if debug:
                print(k_list)

            res_frame = []
            for k in k_list:
                transformed = np.dot(svdecomp.v[:k], svdecomp.matrix.T)
                reconstructed = np.dot(transformed.T, svdecomp.v[:k])
                residuals = svdecomp.matrix - reconstructed
                res_cube = np.zeros(svdecomp.cube4dto3d_shape)
                res_cube[:, svdecomp.yy, svdecomp.xx] = residuals

                # Descaling the spectral channels
                resadi_cube = np.zeros((n, y_in, x_in))
                for i in range(n):
                    frame_i = scwave(res_cube[i * z:(i + 1) * z, :, :],
                                     scale_list,
                                     full_output=False,
                                     inverse=True,
                                     y_in=y_in,
                                     x_in=x_in,
                                     collapse=collapse)
                    resadi_cube[i] = frame_i

                cube_res_der = cube_derotate(resadi_cube,
                                             angle_list,
                                             imlib=imlib,
                                             interpolation=interpolation)
                res_frame.append(cube_collapse(cube_res_der, mode=collapse))

        elif mode == 'median':
            res_frame = median_sub(cube,
                                   angle_list,
                                   scale_list=wavelengths,
                                   verbose=False)

    return res_frame
def derot_tess(tess):
    derot = np.sum(cube_derotate(np.sum(tess, axis=1), get_angle_list(tess)),
                   axis=0)
    return derot
def contrast_curve():
    thrus = np.zeros((4, 5, 3))  # 4 conts, 5 rad, 3 types
    r = range(73)
    stds = np.zeros((1, len(r), 3))
    fwhm = config['data']['fwhm']
    planet_locs = np.array([[39, 36], [46, 44], [53, 51], [60, 59], [66, 65]])
    loc_rads = np.sqrt(np.sum((mp.array_size[0] // 2 - planet_locs)**2,
                              axis=1))

    imlistsfile = 'imlists.npy'
    if os.path.exists(imlistsfile):
        with open(imlistsfile, 'rb') as f:
            imlists = np.load(f)
    else:
        imlists = []
        alldata = load_meta(kind='pt_outputs')
        for ix, rev_ind in enumerate(range(1, 21, 1)):  #1,21

            cur_seg, pred_seg_res, cur_data, _, trainbool, _ = alldata[
                -rev_ind]
            metrics = get_bin_measures(cur_seg, pred_seg_res, sum=False)

            true_star_photons = np.concatenate(
                (cur_data[metrics[1]], cur_data[metrics[3]]), axis=0)

            bins = [
                np.linspace(true_star_photons[:, 0].min(),
                            true_star_photons[:, 0].max(), sp.numframes + 1),
                np.linspace(true_star_photons[:, 1].min(),
                            true_star_photons[:, 1].max(), ap.n_wvl_final + 1),
                np.linspace(-200, 200, 151),
                np.linspace(-200, 200, 151)
            ]

            true_star_tess, edges = np.histogramdd(true_star_photons,
                                                   bins=bins)

            true_star_tess = np.transpose(true_star_tess, axes=(1, 0, 2, 3))
            angle_list = -np.linspace(
                0, sp.numframes * sp.sample_time * config['data']['rot_rate'] /
                60, true_star_tess.shape[1])
            star_derot = np.sum(cube_derotate(np.sum(true_star_tess, axis=0),
                                              angle_list,
                                              imlib='opencv',
                                              interpolation='lanczos4'),
                                axis=0)

            reduced_images = get_reduced_images(ind=-rev_ind, plot=False)

            PCA = reduced_images[1, 0]
            PCD = reduced_images[0, 2]
            PCDPCA = reduced_images[1, 2]

            imlist = np.array([star_derot, PCA, PCD, PCDPCA])
            # imlist[imlist <= 0] = 0.01
            imlists.append(imlist)

        with open(imlistsfile, 'wb') as f:
            np.save(f, np.array(imlists))

    trios = []
    for ix in range(20):
        imlist = imlists[ix]
        star_derot, PCA, PCD, PCDPCA = imlist
        cont_ind = ix // 5
        r_ind = ix % 5

        trio = np.array([PCA, PCD, PCDPCA])
        trio[trio <= 0] = 0.1
        trios.append(trio)

        if ix % 5 == 4:
            print(ix)
            grid(trios, logZ=True, vlim=(1, 60), show=False)
            trios = []

        plot = False
        # if cont_ind >= 0:
        #     plot = True

        # true_flux = aperture_flux(planet_derot, [planet_locs[r_ind,0]],[planet_locs[r_ind,1]], fwhm, plot=plot)[0]
        # measured = np.array([aperture_flux(PCA, [planet_locs[r_ind,0]],[planet_locs[r_ind,1]], fwhm, plot=plot)[0],
        #                      aperture_flux(PCD, [planet_locs[r_ind,0]], [planet_locs[r_ind,1]], fwhm, plot=plot)[0],
        #                      aperture_flux(PCDPCA, [planet_locs[r_ind,0]], [planet_locs[r_ind,1]], fwhm, plot=plot)[0]
        #                      ])

        # print(ix, cont_ind, r_ind, measured/true_flux, true_flux)
        # thrus[cont_ind, r_ind] = measured/true_flux

        if cont_ind == 0:  #4-1:
            std = np.array([
                noise_per_annulus(PCA, 1, fwhm)[0],
                noise_per_annulus(PCD, 1, fwhm)[0],
                noise_per_annulus(PCDPCA, 1, fwhm)[0]
            ])
            stds[0] = std.T

    plt.show()

    # mean_thrus = np.mean(thrus[2:], axis=0)
    full_thru = noise_per_annulus(star_derot, 1, fwhm, mean_per_ann=True)[0]
    # full_thrus = []
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 3, 1)
    ax2 = fig.add_subplot(1, 3, 2)
    ax3 = fig.add_subplot(1, 3, 3)
    labels = ['PCA', 'PCD (sum)', 'PCD + PCA']
    for i in range(3):
        # ax1.plot(loc_rads[::-1], mean_thrus[:,i][::-1], 'o')
        # f = InterpolatedUnivariateSpline(loc_rads[::-1], mean_thrus[:,i][::-1], k=1, ext=3)  # switch order since furthest out is first
        # full_thru = f(r)
        # full_thru[full_thru<=0] = 0.01

        sensitivity = stds[0, :, i] / max(full_thru)
        ax1.plot(full_thru)
        ax2.plot(stds[0, :, i])
        ax3.plot(sensitivity)
    ax1.set_yscale("log")
    ax2.set_yscale("log")
    ax3.set_yscale("log")

    # plt.plot(sensitivity/1e5, label=labels[i])
    # plt.yscale("log")
    # plt.legend()
    plt.show(block=True)
def get_reduced_images(ind=1,
                       use_spec=True,
                       plot=False,
                       use_pca=True,
                       verbose=False):
    """ Looks for reduced images file in the home folder and returns if it exists. If you're on the local machine
    and the file has not been transferred it will throw a FileNotFoundError """

    all_photons, star_photons, planet_photons = get_photons(amount=-ind)
    all_tess, star_tess, planet_tess = (
        get_tess(photons)
        for photons in [all_photons, star_photons, planet_photons])

    all_tess = np.transpose(all_tess, axes=(1, 0, 2, 3))
    star_tess = np.transpose(star_tess, axes=(1, 0, 2, 3))
    planet_tess = np.transpose(planet_tess, axes=(1, 0, 2, 3))

    all_raw = np.sum(all_tess, axis=(0, 1))
    star_raw = np.sum(star_tess, axis=(0, 1))
    planet_raw = np.sum(planet_tess, axis=(0, 1))

    sp.numframes = 80
    sp.sample_time = 15

    angle_list = -np.linspace(
        0, sp.numframes * sp.sample_time * config['data']['rot_rate'] / 60,
        all_tess.shape[1])
    print(angle_list, 'angle')

    if use_spec:
        wsamples = np.linspace(ap.wvl_range[0], ap.wvl_range[1],
                               all_tess.shape[0])
        scale_list = wsamples / (ap.wvl_range[1] - ap.wvl_range[0])

        all_pca = pca.pca(all_tess,
                          angle_list=angle_list,
                          scale_list=scale_list,
                          mask_center_px=None,
                          adimsdi='double',
                          ncomp=(None, 1),
                          collapse='sum',
                          verbose=verbose)

        star_pca = pca.pca(star_tess,
                           angle_list=angle_list,
                           scale_list=scale_list,
                           mask_center_px=None,
                           adimsdi='double',
                           ncomp=(None, 1),
                           collapse='sum',
                           verbose=verbose)

        planet_pca = pca.pca(planet_tess,
                             angle_list=angle_list,
                             scale_list=scale_list,
                             mask_center_px=None,
                             adimsdi='double',
                             ncomp=(None, 1),
                             collapse='sum',
                             verbose=verbose)
        # reduced_images = np.array([[all_raw, star_raw, planet_raw], [all_pca, star_pca, planet_pca]])

        all_med = medsub_source.median_sub(all_tess,
                                           angle_list=angle_list,
                                           scale_list=scale_list,
                                           collapse='sum')
        star_med = medsub_source.median_sub(star_tess,
                                            angle_list=angle_list,
                                            scale_list=scale_list,
                                            collapse='sum')
        planet_med = medsub_source.median_sub(planet_tess,
                                              angle_list=angle_list,
                                              scale_list=scale_list,
                                              collapse='sum')

    else:
        pass
        # all_med = medsub_source.median_sub(np.sum(all_tess, axis=0), angle_list=angle_list, collapse='sum')
        # star_med = medsub_source.median_sub(np.sum(star_tess, axis=0), angle_list=angle_list, collapse='sum')
        # planet_med = medsub_source.median_sub(np.sum(planet_tess, axis=0), angle_list=angle_list,
        #                                       collapse='sum')

    all_derot = np.sum(cube_derotate(np.sum(all_tess, axis=0),
                                     angle_list,
                                     imlib='opencv',
                                     interpolation='lanczos4'),
                       axis=0)
    star_derot = np.sum(cube_derotate(np.sum(star_tess, axis=0),
                                      angle_list,
                                      imlib='opencv',
                                      interpolation='lanczos4'),
                        axis=0)
    planet_derot = np.sum(cube_derotate(np.sum(planet_tess, axis=0),
                                        angle_list,
                                        imlib='opencv',
                                        interpolation='lanczos4'),
                          axis=0)

    # all_snr = snrmap(all_pca, fwhm=5, plot=True)
    # star_snr = snrmap(star_pca, fwhm=5, plot=True)
    # planet_snr = snrmap(planet_pca, fwhm=5, plot=True)

    reduced_images = np.array([[all_raw, star_raw, planet_raw],
                               [all_derot, star_derot, planet_derot],
                               [all_pca, star_pca, planet_pca],
                               [all_med, star_med, planet_med]
                               ])  #, )  # [all_snr, star_snr, planet_snr]

    planet_loc = (92, 60)
    fwhm = config['data']['fwhm']
    nproc = 8

    # reduced_images = np.array([[all_med, snrmap(all_med, fwhm, known_sources=planet_loc, nproc=nproc)],
    #                            [all_pca, snrmap(all_pca, fwhm, known_sources=planet_loc,  nproc=nproc)],
    #                            [planet_derot, snrmap(planet_derot, fwhm, known_sources=planet_loc, nproc=nproc)],
    #                            [planet_pca, snrmap(planet_pca, fwhm, known_sources=planet_loc,  nproc=nproc)]])
    # plt.imshow(planet_derot)
    # plt.imshow(snrmap(planet_derot, fwhm, nproc=nproc))
    # plt.show()
    # reduced_images = np.array([[all_raw, star_raw, planet_raw]])

    # grid(reduced_images, logZ=True, vlim=(1,50))  #, vlim=(1,70)
    if plot:
        grid(reduced_images, vlim=(-10, 106))

    return reduced_images