"""RANSAC estimation."""

import random
from math import sqrt
import numpy as NP

# TODO comments
# TODO threshold

def points_to_line((x1, y1), (x2, y2)):
    return (y2 - y1, x1 - x2, x2 * y1 - x1 * y2)

def filter_near(data, line, distance):
    a, b, c = line
    dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b)
    is_near = lambda p: dst(p) <= distance
    return [p for p in data if is_near(p)]

def least_squares(data):
    x = NP.matrix([(a, 1) for (a, b) in data])
    xt = NP.transpose(x)
    y = NP.matrix([[b] for (a, b) in data])
    [a,c] = NP.dot(NP.linalg.inv(NP.dot(xt, x)), xt).dot(y).flat
    return (a, -1, c)

class Linear_model:
    def __init__(self, data):
        self.data = data

    def get(self, sample):
        if len(sample) == 2:
            return points_to_line(*sample)
        else:
            return least_squares(sample)

    def initial(self):
        return random.sample(self.data, 2)

    def score(self, est, dist):
        cons = []
        score = 0
        a, b, c = est
        dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b)
        for p in self.data:
            d = dst(p)
            if d <= dist:
                cons.append(p)
            score += min(d, dist)
        return score, cons

def iterate(model, distance):
    score = float("inf")
    consensual = model.initial()
    estimate = model.get(consensual)
    new_score, consensual = model.score(estimate, distance)
    while (new_score < score):
        score = new_score
        try:
            estimate = model.get(consensual)
        except NP.linalg.LinAlgError:
            pass
        estimate = model.get(consensual)
        new_score, consensual = model.score(estimate, distance)
    return score, estimate, consensual
        
def estimate(data, dist, k, modelClass=Linear_model):
    model = modelClass(data)
    best = float("inf")
    estimate = None
    consensual = None
    for i in xrange(0, k):
        new, new_estimate, new_consensual = iterate(model, dist)
        if new < best:
            best = new
            estimate = new_estimate
            consensual = new_consensual

    return estimate, consensual

def ransac_duo(data, dist, k, mk, modelClass=Linear_model):
    cons = []
    for i in xrange(mk):
        model, cons = estimate(set(data) - set(cons), dist, k, modelClass)
    return (model, cons), estimate(set(data) - set(cons), dist, k, modelClass)

