import os, sys, inspect, json
import numpy as np
import matplotlib.pyplot as plt

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)

try:
    from ..bal import Bal
    from ..mybal import MyBal
    from ..mybal import balFloor
    from .util import trainTwo, touchFilter, filt
except Exception: #ImportError
    from bal import Bal
    from mybal import MyBal
    from mybal import balFloor
    from util import trainTwo, touchFilter, filt


def plotNSQ(good, bad):
    resGood = good.train(labeled, epoch=1000, track=False)
    resBad = bad.train(labeledRaw, epoch=1000, track=False)

    replot = plt.subplot(311)

    replot.set_title("mean sqr")
    replot.plot([i for i, _, _ in resGood], label='Processed')
    replot.plot([i for i, _, _ in resBad], label='Raw')
    replot.legend()
    plt.show()


def plotErr(good, bad):
    resGood = good.train(labeled, epoch=1000, track=False)
    resBad = bad.train(labeledRaw, epoch=1000, track=False)

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

    errplot.plot([i for _, (i, _), _ in resGood], label='Processed')
    errplot.plot([i for _, (i, _), _ in resBad], label='Raw')
    errplot.legend()
    plt.show()






# raw data
fraw = open("./data.data")
rawData = json.load(fraw)
labeledRaw = [(np.array(i["proprio"]), np.array(i["touch"])) for i in rawData]

# init training data
with open("./hand_som.somdata") as f:
    data = json.load(f)
labeled = [(np.array(i['pos']), np.array(i["touch"])) for i in data]


vanila = Bal(len(rawData[0]["proprio"]), 70, len(rawData[0]["touch"]))
vanilaTrns = Bal(len(data[0]["pos"]), 180, len(data[0]["touch"]))
trainTwo(model1=vanila, train1=labeledRaw, name1="raw", model2=vanilaTrns, train2=labeled[0:60], name2="processed")



