Ejemplo n.º 1
0
def stack_cross_correlation(ctx, config, debug=0, nwise=2, stack=None):
    """Perform a Stack Cross Correlation analysis
    
    Parameters
    ----------
    ctx : :class:`wise.project.AnalysisContext`
    config : :class:`wise.scc.SCCConfiguration`
    debug : int, optional
    nwise : int, optional
    stack : :class:`libwise.plotutils.FigureStack`, optional
    
    Returns
    -------
    :class:`wise.scc.StackCrossCorrelation` : a StackCrossCorrelation containing the results of the analysis


    .. _tags: task_scc
    """
    scc_result = scc.StackCrossCorrelation(config, debug=debug, stack=stack)
    all_files = nputils.nwise(ctx.files, nwise)

    for pair in all_files:
        img1 = ctx.open_file(pair[0])
        img2 = ctx.open_file(pair[-1])

        prj = ctx.get_projection(img1)

        delta_t, velocity_pix, tol_pix = scc_result.get_velocity_resolution(prj, img1, img2)

        if not nputils.in_range(tol_pix, config.get("tol_pix_range")):
            print "-> Skip: Not in the allowed range of pixel velocity resolution:", tol_pix
            continue

        res1 = ctx.detection(img1, filter=config.get("filter1"))
        print "-> Numbers of detected SSP: %s" % ", ".join([str(k.size()) for k in res1])
        res2 = ctx.detection(img2, filter=config.get("filter2"))

        scc_result.process(prj, res1, res2)

    return scc_result
Ejemplo n.º 2
0
def bootstrap_scc(ctx, config, output_dir, n, nwise = 2, append=False, 
                  verbose=False, seperate_scales=False):
    """Perform Stack Cross Correlation analysis n time and store results in output_dir
    
    Parameters
    ----------
    ctx : :class:`wise.project.AnalysisContext`
    config : :class:`wise.scc.SCCConfiguration`
    output_dir : str
    n : int
    append : bool, optional
        Append results
    seperate_scales : bool, optional


    .. _tags: task_scc
    """
    random_shift = config.get("img_rnd_shift")

    if config.get("shuffle") == config.get("rnd_pos_shift"):
        print "Configuration Error: either 'shuffle' or 'rnd_pos_shift' need to be set"
        return

    all_files = list(ctx.files)

    prj = ctx.get_projection(ctx.open_file(all_files[0]))

    all_res1 = dict()
    all_res2 = dict()
    all_epochs = []

    for file1 in ctx.files:
        img1 = ctx.open_file(file1)
        img1.data = nputils.shift2d(img1.data, np.random.uniform(-random_shift, random_shift, 2))

        img2 = ctx.open_file(file1)
        img2.data = nputils.shift2d(img2.data, np.random.uniform(-random_shift, random_shift, 2))

        res1 = ctx.detection(img1, filter=config.get("filter1"))
        print "-> Numbers of detected SSP: %s" % ", ".join([str(k.size()) for k in res1])
        res2 = ctx.detection(img2, filter=config.get("filter2"))
        print "-> Numbers of detected SSP: %s" % ", ".join([str(k.size()) for k in res2])

        all_res1[file1] = res1
        all_res2[file1] = res2
        all_epochs.append(img1.get_epoch())

    t = time.time()

    # all_segments2_img = dict()
    # for file, segments2 in all_res2.items():
    #     all_segments2_img[file] = [k.get_img().data.copy() for k in segments2]

    if not os.path.exists(output_dir):
        os.mkdir(output_dir)

    files = os.listdir(output_dir)
    if append and len(files) > 0:
        if seperate_scales and os.path.isdir(os.path.join(output_dir, files[0])):
            files = os.listdir(os.path.join(output_dir, files[0]))
        all_i = sorted([int(os.path.splitext(file)[0].split('_')[-1]) for file in files])
        if len(all_i) == 0:
            start = 0
        else:
            start = all_i[-1] + 1
    else:
        start = 0

    for i in range(n):
        eta = ""
        if i > 0:
            remaining = (np.round((time.time() - t) / float(i) * (n - i)))
            eta = " (ETA: %s)" % time.strftime("%H:%M:%S", time.localtime(time.time() + remaining))
        print "Run %s / %s%s" % (i + 1, n, eta)

        if config.get("shuffle"):
            # np.random.shuffle(all_files)
            shuffled = nputils.permutation_no_succesive(all_files)
            files_pair = nputils.nwise(shuffled, nwise)
        else:
            files_pair = nputils.nwise(all_files, nwise)
        epochs_pair = nputils.nwise(all_epochs, nwise)

        scc_result = scc.StackCrossCorrelation(config, verbose=verbose)

        for shuffled_pair, epoch_pair in zip(files_pair, epochs_pair):
            res1 = all_res1[shuffled_pair[0]]
            res2 = all_res2[shuffled_pair[-1]]

            # for segments2, segments2_img in zip(res2, all_segments2_img[shuffled_pair[-1]]):
            #     segments2.get_img().data = nputils.shift2d(segments2_img, 
            #                                     np.random.uniform(-random_shift, random_shift, 2))

            res1.epoch = epoch_pair[0]
            res2.epoch = epoch_pair[-1]

            delta_t, velocity_pix, tol_pix = scc_result.get_velocity_resolution(prj, res1, res2)

            if not nputils.in_range(tol_pix, config.get("tol_pix_range")):
                print "-> Skip: Not in the allowed range of pixel velocity resolution:", tol_pix
                continue

            scc_result.process(prj, res1, res2)

        if seperate_scales:
            for scale, gncc_map in scc_result.get_mean_ncc_scales(smooth_len=1).items():
                save_dir = os.path.join(output_dir, "scale_%s" % scale)
                
                if not os.path.exists(save_dir):
                    os.mkdir(save_dir)

                imgutils.Image(gncc_map).save_to_fits(os.path.join(save_dir, "gncc_map_%s.fits" % (start + i)))
        else:
            gncc_map = scc_result.get_global_ncc(smooth_len=1)
            imgutils.Image(gncc_map).save_to_fits(os.path.join(output_dir, "gncc_map_%s.fits" % (start + i)))

    print "Done"