Ejemplo n.º 1
0
def _clean_fringes(f_classes, f_locs):
    d = list(is_valid_3(*p) for p in triple_wise_periodic(f_classes))
    # rotate to deal with offset of 1 in is_valid_3
    d = d[-1:] + d[:-1]
    # if there are any in-valid fringes, we need to try and patch them up
    if not all(d):
        # if an invalid run spans the loop over point, rotate everything and has at least 2 fringes padding
        while d[0] is False or d[-1] is False or d[-1] is False or d[-2] is False:
            d = d[-1:] + d[:-1]
            f_classes = f_classes[-1:] + f_classes[:-1]
            f_locs = f_locs[-1:] + f_locs[:-1]

        # get the bad runs
        bad_runs = find_bad_runs(d)
        # provide some more padding
        bad_runs = [(br[0], br[1]) for br in bad_runs]
        fixes = list()
        for br in bad_runs:
            slc = slice(*br)
            working_classes = f_classes[slc]
            working_locs = f_locs[slc]
            if any([f.charge == 0 for f in working_classes]):
                # we have at least one zero fringe
                fix_ = _valid_run_fixes_with_hinting(working_classes, working_locs)
                fixes.append(fix_)
            else:
                fix_ = _valid_run_fixes(working_classes, working_locs)

                fixes.append([fix_[0],
                              [fix_[1]] * len(fix_[0])])

        best_lst = None
        min_miss = np.inf
        zero_count = 0
        prop_c_lsts, prop_l_lsts = list(zip(*fixes))

        for p_cls, p_loc in zip(product(*prop_c_lsts), product(*prop_l_lsts)):
            # get a copy of the list so we can mutate it
            working_class_lst = list(f_classes)
            working_locs_lst = list(f_locs)

            # apply the proposed changes
            for p, loc, br in zip(p_cls[::-1], p_loc[::-1], bad_runs[::-1]):
                slc = slice(*br)
                working_class_lst[slc] = p
                working_locs_lst[slc] = loc

            # count up how much we missed by
            miss_count = np.sum([forward_dh_dict[a, b] for a, b in pairwise_periodic(working_class_lst)])

            if miss_count == 0:
                zero_count += 1
            # if better than current best, store this one
            if np.abs(miss_count) < min_miss:
                min_miss = np.abs(miss_count)
                best_lst = (working_class_lst, working_locs_lst)
        print(zero_count, '/', np.prod([len(_p) for _p in prop_l_lsts]))
        f_classes, f_locs = best_lst
    return f_classes, f_locs
Ejemplo n.º 2
0
def _clean_fringes(f_classes, f_locs):
    d = list(is_valid_3(*p) for p in triple_wise_periodic(f_classes))
    # rotate to deal with offset of 1 in is_valid_3
    d = d[-1:] + d[:-1]
    # if there are any in-valid fringes, we need to try and patch them up
    if not all(d):
        # if an invalid run spans the loop over point, rotate everything and has at least 2 fringes padding
        while d[0] is False or d[-1] is False or d[-1] is False or d[
                -2] is False:
            d = d[-1:] + d[:-1]
            f_classes = f_classes[-1:] + f_classes[:-1]
            f_locs = f_locs[-1:] + f_locs[:-1]

        # get the bad runs
        bad_runs = find_bad_runs(d)
        # provide some more padding
        bad_runs = [(br[0], br[1]) for br in bad_runs]
        fixes = list()
        for br in bad_runs:
            slc = slice(*br)
            working_classes = f_classes[slc]
            working_locs = f_locs[slc]
            if any([f.charge == 0 for f in working_classes]):
                # we have at least one zero fringe
                fix_ = _valid_run_fixes_with_hinting(working_classes,
                                                     working_locs)
                fixes.append(fix_)
            else:
                fix_ = _valid_run_fixes(working_classes, working_locs)

                fixes.append([fix_[0], [fix_[1]] * len(fix_[0])])

        best_lst = None
        min_miss = np.inf
        zero_count = 0
        prop_c_lsts, prop_l_lsts = list(zip(*fixes))

        for p_cls, p_loc in zip(product(*prop_c_lsts), product(*prop_l_lsts)):
            # get a copy of the list so we can mutate it
            working_class_lst = list(f_classes)
            working_locs_lst = list(f_locs)

            # apply the proposed changes
            for p, loc, br in zip(p_cls[::-1], p_loc[::-1], bad_runs[::-1]):
                slc = slice(*br)
                working_class_lst[slc] = p
                working_locs_lst[slc] = loc

            # count up how much we missed by
            miss_count = np.sum([
                forward_dh_dict[a, b]
                for a, b in pairwise_periodic(working_class_lst)
            ])

            if miss_count == 0:
                zero_count += 1
            # if better than current best, store this one
            if np.abs(miss_count) < min_miss:
                min_miss = np.abs(miss_count)
                best_lst = (working_class_lst, working_locs_lst)
        print(zero_count, '/', np.prod([len(_p) for _p in prop_l_lsts]))
        f_classes, f_locs = best_lst
    return f_classes, f_locs
Ejemplo n.º 3
0
def is_valid_run_periodic(args):
    '''Returns if this *periodic* run is a valid'''
    return all([p in valid_precede_dict[f] for p, f in pairwise_periodic(args)])
Ejemplo n.º 4
0
def is_valid_run_periodic(args):
    '''Returns if this *periodic* run is a valid'''
    return all(
        [p in valid_precede_dict[f] for p, f in pairwise_periodic(args)])