<commit_before>import numpy as np
import copy
from models import Transforms

def ransac(matches, target_model_type, iterations, epsilon, min_inlier_ratio, min_num_inlier):
    # model = Model.create_model(target_model_type)
    assert(len(matches[0]) == len(matches[1]))

    best_model = None
    best_model_score = 0 # The higher the better
    best_inlier_mask = None
    best_model_mean_dists = 0
    proposed_model = Transforms.create(target_model_type)

    if proposed_model.MIN_MATCHES_NUM > matches.shape[1]:
        print "RANSAC cannot find a good model because the number of initial matches ({}) is too small.".format(matches.shape[1])
        return None, None, None

    for i in xrange(iterations):
        # choose a minimal number of matches randomly
        min_matches_idxs = np.random.choice(xrange(len(matches[0])), size=proposed_model.MIN_MATCHES_NUM, replace=False)
        # Try to fit them to the model
        proposed_model.fit(matches[0][min_matches_idxs], matches[1][min_matches_idxs])
        # print "proposed_model", proposed_model.to_str()
        # Verify the new model 
        proposed_model_score, inlier_mask, proposed_model_mean = proposed_model.score(matches[0], matches[1], epsilon, min_inlier_ratio, min_num_inlier)
        # If no model was found (invalid score), stop the iterations
        if proposed_model_score == -1:
            break
        # print "proposed_model_score", proposed_model_score
        if proposed_model_score > best_model_score:
            best_model = copy.deepcopy(proposed_model)
            best_model_score = proposed_model_score
            best_inlier_mask = inlier_mask
            best_model_mean_dists = proposed_model_mean
    '''
    if best_model is None:
        print "Cannot find a good model during ransac. best_model_score {}".format(best_model_score)
    else:
        print "RANSAC result: best_model_score", best_model_score, "best_model:", best_model.to_str(), "best_model_mean_dists:", best_model_mean_dists
    '''
    return best_inlier_mask, best_model, best_model_mean_dists


def filter_after_ransac(candidates, model, max_trust, min_num_inliers):
    """
    Estimate the AbstractModel and filter potential outliers by robust iterative regression.
    This method performs well on data sets with low amount of outliers (or after RANSAC).
    """
    # copy the model
    new_model = copy.deepcopy(model)

    # iteratively find a new model, by fitting the candidates, and removing those that are far than max_trust*median-distance
    # until the set of remaining candidates does not change its size<commit_msg>Reverting ransac to its previous state<commit_after>import numpy as np
import copy
from models import Transforms

def ransac(matches, target_model_type, iterations, epsilon, min_inlier_ratio, min_num_inlier):
    # model = Model.create_model(target_model_type)
    assert(len(matches[0]) == len(matches[1]))

    best_model = None
    best_model_score = 0 # The higher the better
    best_inlier_mask = None
    best_model_mean_dists = 0
    proposed_model = Transforms.create(target_model_type)

    if proposed_model.MIN_MATCHES_NUM > matches.shape[1]:
        print "RANSAC cannot find a good model because the number of initial matches ({}) is too small.".format(matches.shape[1])
        return None, None, None

    for i in xrange(iterations):
        # choose a minimal number of matches randomly
        min_matches_idxs = np.random.choice(xrange(len(matches[0])), size=proposed_model.MIN_MATCHES_NUM, replace=False)
        # Try to fit them to the model
        proposed_model.fit(matches[0][min_matches_idxs], matches[1][min_matches_idxs])
        # print "proposed_model", proposed_model.to_str()
        # Verify the new model 
        proposed_model_score, inlier_mask, proposed_model_mean = proposed_model.score(matches[0], matches[1], epsilon, min_inlier_ratio, min_num_inlier)
        # print "proposed_model_score", proposed_model_score
        if proposed_model_score > best_model_score:
            best_model = copy.deepcopy(proposed_model)
            best_model_score = proposed_model_score
            best_inlier_mask = inlier_mask
            best_model_mean_dists = proposed_model_mean
    '''
    if best_model is None:
        print "Cannot find a good model during ransac. best_model_score {}".format(best_model_score)
    else:
        print "RANSAC result: best_model_score", best_model_score, "best_model:", best_model.to_str(), "best_model_mean_dists:", best_model_mean_dists
    '''
    return best_inlier_mask, best_model, best_model_mean_dists


def filter_after_ransac(candidates, model, max_trust, min_num_inliers):
    """
    Estimate the AbstractModel and filter potential outliers by robust iterative regression.
    This method performs well on data sets with low amount of outliers (or after RANSAC).
    """
    # copy the model
    new_model = copy.deepcopy(model)

    # iteratively find a new model, by fitting the candidates, and removing those that are far than max_trust*median-distance
    # until the set of remaining candidates does not change its size
{'fraction_non_alphanumeric': 0.07504397107680282, 'fraction_numerical': 0.004494821184287669, 'mean_word_length': 3.851184834123223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310429', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1511, 'n_words': 428}