import numpy as np
import copy
from RF import *
import torch
from connected_component import *
from sklearn.metrics.cluster import normalized_mutual_info_score
from sklearn.metrics.cluster import adjusted_mutual_info_score
from sklearn.metrics.cluster import adjusted_rand_score
from sklearn.metrics.cluster import rand_score
import matplotlib.pyplot as plt
import matplotlib.pyplot as plot

MS_clustering_bandwidth       = {'UserKnowledge':0.5,'iris':0.5,'WallRobot':0.2,'WirelessLocalization':0.5}
MS_clustering_iteration       = {'UserKnowledge':100,'iris':100,'WallRobot':100,'WirelessLocalization':100}
MS_clustering_threshold       = {'UserKnowledge':0.9,'iris':0.9,'WallRobot':0.9,'WirelessLocalization':0.9}


def run_MS(X, y,count,dataset):
	iteration = MS_clustering_iteration[dataset]
	bandwidth = MS_clustering_bandwidth[dataset]
	threshold = MS_clustering_threshold[dataset]
	
	X = torch.from_numpy(X)
	current = copy.deepcopy(X)

	for t in range(iteration):
		gram = Gaussian_kernel(current,X,bandwidth)
		adjusted = gram / torch.sum(gram,1).unsqueeze(1)
		current = (X.T @ adjusted.T).T

	final_gram = Gaussian_kernel(current,current,bandwidth)
	labels = CC(final_gram,threshold)

	NMI = normalized_mutual_info_score(labels,y)
	AMI = adjusted_mutual_info_score(labels,y)
	ARS = adjusted_rand_score(labels,y)
	RS = rand_score(labels,y)
	return NMI, AMI, ARS, RS