import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dt
import time

metody = ["EM-kC", "EM-vM", "MM-kC", "MM-vM", "Adaboost", "NS", "HyT-EM", "FreMEn", "Histogram", "Průměr"]
parametry = ["30", "30", "5", "2", "1", "4", "3", "5", "24", ""]
trenovaci = "../../data/greg_door_2016_min/"

for i in range(0,2):
	fig, ax = plt.subplots()
	times = np.loadtxt(trenovaci+"test_times_"+str(i)+".txt")
	dates=[dt.datetime.fromtimestamp(ts) for ts in times]
	data = np.loadtxt(trenovaci+"test_data_"+str(i)+".txt")

	xfmt = md.DateFormatter('%Y-%m-%d')
	ax.xaxis.set_major_formatter(xfmt)
	ax.plot(dates, data, label="data")

	for j in range(0, len(metody)):
		fajl = str(i)+metody[j]
		if i == 0:
			fajl = fajl+"_"+parametry[j]+".txt"
		else:
			fajl = fajl+"_model.txt"
		predikce = np.loadtxt(fajl)
		ax.plot(dates, predikce[:,1], label=metody[j]+" "+parametry[j])

	if i == 0:
		plt.subplots_adjust(bottom=0.25, right=0.8)
	else:
		plt.subplots_adjust(bottom=0.2, right=0.75)
	ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
	ax.grid(axis="both")
	
	xt = None
	if i == 0:
		xt = np.arange(np.min(times),np.max(times)+1,86400*7)
	else:
		xt = np.arange(np.min(times),np.max(times)+1,86400)
	xt = xt - np.mod(xt, 86400)
	ax.xaxis.set_ticks([dt.datetime.fromtimestamp(ts) for ts in xt])
	plt.xticks(rotation=90)
	
	ax.set_ylabel('data nebo predikce')
	if i == 0:
		ax.set_title('Úloha s dveřmi: Trénovací sada')
	else:
		ax.set_title('Úloha s dveřmi: Testovací sada '+str(i))
	
	plt.show()

"""
	file=fopen(argv[2],"r");
	string test = string(argv[2]);
	ofstream myfile0("predictions/"+test.substr(test.length()-5, 1)+string(argv[3])+"_"+string(argv[4])+".txt");

	while (feof(file)==0){
		fscanf(file,"%i\n",&testingTime);
		float prediction = temporalModel->predict(testingTime);
		myfile0 << testingTime << " " << prediction << std::endl;
		predictions[testingLength++] = prediction;
	}
	fclose(file);
	myfile0.close();
"""
