import os
import csv
import cv2 as cv
import math

from all_data_splitter import split_all_data
from main import knn, decision_tree, neural_network


all_data = os.path.abspath(os.path.join(__file__, "..", "all_feature_vectors.csv"))
path_to_data = os.path.abspath(os.path.join(__file__, "..", "..", "data"))
training = os.path.abspath(os.path.join(__file__, "..", "training.csv"))
testing = os.path.abspath(os.path.join(__file__, "..", "testing.csv"))
splits = 200

# for i in range(0, splits):
#     split_all_data(all_data, os.path.join(path_to_data, f"training-{i}.csv"), os.path.join(path_to_data, f"testing-{i}.csv"))

err_e_knn = []
err_e_dec = []
err_e_nn = []

for j in range(splits):
    correct_dec = 0
    false_dec = 0
    amount_testing_dec = 0
    # correct_knn, false_knn, amount_testing_knn = knn(os.path.join(path_to_data, f"training-{j}.csv"), os.path.join(path_to_data, f"testing-{j}.csv"), k_param=6)
    # correct_dec, false_dec, amount_testing_dec = decision_tree(training=os.path.join(path_to_data, f"training-{j}.csv"), testing=os.path.join(path_to_data, f"testing-{j}.csv"), cycles=6)
    correct_nn, false_nn, amount_testing_nn = neural_network(training=os.path.join(path_to_data, f"training-{j}.csv"), testing=os.path.join(path_to_data, f"testing-{j}.csv"))
    # err_e_knn.append(false_knn/amount_testing_knn)
    # err_e_dec.append(false_dec/amount_testing_dec)
    err_e_nn.append(false_nn/amount_testing_nn)


# err_ee_dec = sum(err_e_dec)/(len(err_e_dec))
# err_ee_knn = sum(err_e_knn)/(len(err_e_knn))
err_ee_nn = sum(err_e_nn)/(len(err_e_nn))

# s_top_dec = 0
# for l in range(0, len(err_e_dec)):
#     s_top_dec += ((err_e_dec[l] - err_ee_dec) ** 2)

# s_top_knn = 0
# for l in range(0, len(err_e_knn)):
#     s_top_knn += ((err_e_knn[l] - err_ee_knn) ** 2)

s_top_nn = 0
for l in range(0, len(err_e_nn)):
    s_top_nn += ((err_e_nn[l] - err_ee_nn) ** 2)

# standart_abweichung_knn = math.sqrt( s_top_knn / len(err_e_knn))
# standart_abweichung_dec = math.sqrt( s_top_dec / len(err_e_dec))
standart_abweichung_nn = math.sqrt( s_top_nn / len(err_e_nn))

# confidence_dec = 1.96 * (standart_abweichung_dec / math.sqrt(len(err_e_dec)))
# confidence_knn = 1.96 * (standart_abweichung_knn / math.sqrt(len(err_e_knn)))
confidence_nn = 1.96 * (standart_abweichung_nn / math.sqrt(len(err_e_nn)))

# print(err_e_dec)
# print(err_e_knn)
# print(confidence_dec)
# print(confidence_knn)


print(err_e_nn)
print(confidence_nn)