import numpy as np
import matplotlib.pyplot as plt

def trainTwo(model1, train1, name1, model2, train2, name2, epoch=1000):
    res1 = model1.train(train1, epoch=epoch, track=False)
    res2 = model2.train(train2, epoch=epoch, track=False)

    replot = plt.subplot(311)
    replot.set_title("mean squared error")
    replot.plot([i for i, _, _ in res1], label=name1)
    replot.plot([i for i, _, _ in res2], label=name2)
    replot.legend()

    errplot = plt.subplot(313)
    errplot.set_title("classification error [%]")
    errplot.set_ylim([-5, 105])
    errplot.plot([i for _, (i, _), _ in res1], label=name1)
    errplot.plot([i for _, (i, _), _ in res2], label=name2)
    errplot.legend()

    plt.show()


def filt(labeld):
    res = []
    seen = {0: [0]}
    for i, k in labeld:
        key = toNum(i)
        if key not in seen:
            seen[key] = [toNum(k)]
            res.append((i, k))
        else:
            seen[key].append(toNum(k))
    return res


def toNum(l):
    s = "".join(map(str, l))
    return int(s, base=2)


def touchFilter(data):
    tmp = data[0][1]
    return np.array(list(filter(lambda x: not np.all(tmp == x[1]), data)))


def splits(model, data, epoch=150, track=False):
    resGood = model.train(data, epoch=epoch, track=track)

    errplot = plt.subplot(311)
    errplot.set_title("Classification error [%]")
    errplot.set_ylim([-5, 105])

    errplot.plot([i for _, (i, _), _ in resGood])

    errplot = plt.subplot(313)
    errplot.set_title("Classification error [%]")
    errplot.set_ylim([-5, 105])

    errplot.plot([i for _, _, (i, j, k) in resGood], label="predpovedal dotyk")
    errplot.plot([j for _, _, (i, j, k) in resGood], label="predpovedal nedotyk")
    errplot.plot([k for _, _, (i, j, k) in resGood], label='zle miesto')
    errplot.legend()
    plt.show()