Пример #1
0
def bootstrap_matching(ctx, n=100, filter=None, cb_post_match=None):
    all_res = dict()
    all_epochs = []
    all_imgs = dict()
    for i, file in enumerate(ctx.files):
        img = ctx.open_file(file)
        all_epochs.append(img.get_epoch())

        res = ctx.detection(img, filter=filter)
        all_res[img.get_epoch()] = res
        all_imgs[img.get_epoch()] = img

    t = time.time()
    all_match_ratio = []
    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)
        
        shuffled = nputils.permutation_no_succesive(all_epochs)
        match_ratio_list = []
        match_results = project.AnalysisResult(ctx.config)

        for epoch1, epoch2 in nputils.pairwise(shuffled):
            res1 = all_res[epoch1].copy()
            res2 = all_res[epoch2].copy()
            # print "Matching:", res1.get_epoch(), "vs", res2.get_epoch()

            res1.epoch = epoch1
            for segments in res1:
                segments.epoch = epoch1

            res2.epoch = epoch2
            for segments in res2:
                segments.epoch = epoch2

            full_match_res = ctx.match(res1, res2, verbose=False)
            match_results.add_match_result(full_match_res)
            match_results.add_detection_result(all_imgs[epoch1], res1)

            for match_res in full_match_res:
                n_segments = match_res.segments1.size()
                match_ratio = (match_res.get_match().size()) / (float(n_segments))
                match_ratio_list.append(match_ratio)

        all_match_ratio.append(np.mean(match_ratio_list))

        if cb_post_match is not None:
            cb_post_match(shuffled, match_results)

        # print "Match ratio stat:", nputils.stat(match_ratio_list)

    # print all_match_ratio

    print "Match ratio stat:", nputils.stat(all_match_ratio)
Пример #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"