from sklearn.cluster import KMeans
import numpy as np

def printLine(line, status, normValue,wcs):
	printStr = ""
	for value in line.split():
		value = int(value)
		if (value == 0):
			printStr += 'D\t' ###NODE IS DOWN!
		else:
			if (status):
				printStr += '.\t' ###NODE IS NORMAL!
			else:
				result = kmeans.predict([[1,value]])
				if ( result == correct):
					printStr += ".\t" ###NODE IS NORMAL!
				else:
					printStr += "X\t" ###NODE IS ABNORMAL!
	print(printStr,normValue,"\t",wcs)


with open('SG-rand.snapshot') as f:
#with open('SG-fail.snapshot') as f:
	lines = f.read().splitlines()

for line in lines:
	lineT = [int(x) for x in line.split() if x!='0']
	my_dict = {i:lineT.count(i) for i in lineT}
	if(len(my_dict)<2):
		printLine(line,True,lineT[0],-1)
		continue
	X = np.array([[my_dict[i],i] for i in my_dict.keys()])
	kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
	centers = kmeans.cluster_centers_
	withinClusterSum = kmeans.inertia_

	centers[0][1] = round(centers[0][1])
	centers[1][1] = round(centers[1][1])

	if(centers[0][0] > centers[1][0]):
		correct = 0
		normValue = centers[0][1]
	elif(centers[0][0] < centers[1][0]):
		correct = 1
		normValue = centers[1][1]
	else:
		correct = -1
		normValue = centers[0][1]


	if(withinClusterSum < 5):
		printLine(line,True, normValue,withinClusterSum)
		continue


	printLine(line,False,normValue, withinClusterSum)
