#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec  8 16:07:28 2021

@author: pfaure
"""
from numpy import arange
from sklearn.neighbors import NearestNeighbors
import numpy as np

from myplotlib import print_1d_data, print_2d_data
from mydatalib import extract_data_2d, scale_data, apply_mean_shift, evaluate

path = './artificial/'
dataset_name = "xclara"
method_name = "mean-shift"
save = True

print("-----------------------------------------------------------")
print("     Chargement du dataset : " + dataset_name)
data = extract_data_2d(path + dataset_name)
print_2d_data(data, dataset_name=dataset_name +
              "_brutes", stop=False, save=save)

print("-----------------------------------------------------------")
print("     Mise à l'échelle")
data_scaled = scale_data(data)
print_2d_data(data_scaled, dataset_name=dataset_name +
              "_scaled", stop=False, save=save)

# Application de Affinity Propagation pour plusieurs valeurs de préférence
# et evaluation de la solution

k_max = 2

k = []
durations = []
silouettes = []
daviess = []
calinskis = []
for bandwidth in arange(0.1, k_max, 0.1):
    # Application du clustering
    (model, duration) = apply_mean_shift(
        data_scaled, bandwidth=bandwidth)
    # Affichage des clusters
    print_2d_data(data_scaled, dataset_name=dataset_name,
                  method_name=method_name, k=bandwidth,
                  stop=False, save=save, c=model.labels_)
    # Evaluation de la solution de clustering
    (silouette, davies, calinski) = evaluate(data_scaled, model)
    # Enregistrement des valeurs
    k += [bandwidth]
    durations += [duration]
    silouettes += [silouette]
    daviess += [davies]
    calinskis += [calinski]

# Affichage des résultats
print_1d_data(k, k, x_name="k", y_name="k", dataset_name=dataset_name,
              method_name=method_name, stop=False, save=save)
print_1d_data(k, durations, x_name="k", y_name="temps_de_calcul", y_unit="ms",
              dataset_name=dataset_name,
              method_name=method_name, stop=False, save=save)
print_1d_data(k, silouettes, x_name="k", y_name="coeficient_de_silhouette",
              dataset_name=dataset_name,
              method_name=method_name, stop=False, save=save)
print_1d_data(k, daviess, x_name="k", y_name="coeficient_de_Davies",
              dataset_name=dataset_name,
              method_name=method_name, stop=False, save=save)
print_1d_data(k, calinskis, x_name="k", y_name="coeficient_de_Calinski",
              dataset_name=dataset_name,
              method_name=method_name, stop=False, save=save)
