#!/usr/bin/python 

""" 
    Skeleton code for k-means clustering mini-project.
"""




import pickle
import numpy as np
import matplotlib.pyplot as plt
import sys
sys.path.append("../tools/")
from feature_format import featureFormat, targetFeatureSplit




def Draw(pred, features, poi, mark_poi=False, name="image.png", f1_name="feature 1", f2_name="feature 2"):
    """ some plotting code designed to help you visualize your clusters """

    ### plot each cluster with a different color--add more colors for
    ### drawing more than five clusters
    colors = ["b", "c", "k", "m", "g"]
    for ii, pp in enumerate(pred):
        plt.scatter(features[ii][0], features[ii][1], color = colors[pred[ii]])

    ### if you like, place red stars over points that are POIs (just for funsies)
    if mark_poi:
        for ii, pp in enumerate(pred):
            if poi[ii]:
                plt.scatter(features[ii][0], features[ii][1], color="r", marker="*")
    plt.xlabel(f1_name)
    plt.ylabel(f2_name)
    plt.savefig(name)
    #plt.show()



### load in the dict of dicts containing all the data on each person in the dataset
data_dict = pickle.load( open("../final_project/final_project_dataset.pkl", "r") )
### there's an outlier--remove it! 
data_dict.pop("TOTAL", 0)


### the input features we want to use 
### can be any key in the person-level dictionary (salary, director_fees, etc.) 
feature_1 = "salary"
#feature_2 = "exercised_stock_options"
feature_2 = "from_messages"         #added for Lesson 10-17
#feature_3 = "total_payments"    # for Lesson 9-21
poi  = "poi"
features_list = [poi, feature_1, feature_2]
data = featureFormat(data_dict, features_list )
poi, finance_features = targetFeatureSplit( data )


### in the "clustering with 3 features" part of the mini-project,
### you'll want to change this line to 
### for f1, f2, _ in finance_features:
### (as it's currently written, the line below assumes 2 features)
# f3 is for Lesson 9-21
#for f1, f2, f3 in finance_features:
for f1, f2 in finance_features:
    plt.scatter( f1, f2 )
#plt.show()
plt.savefig('three_feat.png')


### cluster here; create predictions of the cluster labels
### for the data and store them to a list called pred

from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2, random_state=0).fit(finance_features)
pred = kmeans.predict(finance_features)


'''
Lesson 10-17 not needed, you can use finance_features instead, they are as valid as building a numpy array from raw_data
stock_options = []
salary_options = []
for key,value in data_dict.iteritems():
    #if value['exercised_stock_options'] != 'NaN':      #Lesson8-22
    if value['salary'] != 'NaN':
        salary_options.append(value['salary'] * 1.)
    elif value['salary'] == 'NaN':
        salary_options.append(0.)

for key,value in data_dict.iteritems():
    if value['exercised_stock_options'] != 'NaN':
        stock_options.append(value['exercised_stock_options'] * 1.)  #Lesson 8-22
    elif value['exercised_stock_options'] == 'NaN':
        stock_options.append(0.)


all_data = np.column_stack((salary_options, stock_options))
'''

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
#scaler = scaler.fit(all_data)
scaler = scaler.fit(finance_features)

X_test = np.array([[200000.,1000000.]])
print(scaler.transform(X_test))

''' #this was my solution, but it didn't worked, because NaN is treated as 0 by the cleanup! ->  0 is not the correct answer for the minimum
def max_value(inputlist, pos):
        return max([sublist[pos] for sublist in inputlist])

def min_value(inputlist, pos):
        return min([sublist[pos] for sublist in inputlist])
print(max_value(finance_features,1))
print(min_value(finance_features,1))

maxval = max(map(lambda x: x[1], finance_features))    
minval = min(map(lambda x: x[1], finance_features))    
print(minval, maxval)
'''
#this one is from the forum: https://discussions.udacity.com/t/stock-option-range/32452/9

stock_options = []
for key, value in data_dict.iteritems():
    #if value['exercised_stock_options'] != 'NaN':      #Lesson8-22
    if value['salary'] != 'NaN':
        #stock_options.append(value['exercised_stock_options'])  #Lesson 8-22
        stock_options.append(value['salary'])

print( "max value" , max(stock_options))
print( "min value", min(stock_options))



### rename the "name" parameter when you change the number of features
### so that the figure gets saved to a different file
try:
    #Draw(pred, finance_features, poi, mark_poi=False, name="clusters.pdf", f1_name=feature_1, f2_name=feature_2)
    Draw(pred, finance_features, poi, mark_poi=False, name="clusters_with3.pdf", f1_name=feature_1, f2_name=feature_2)
except NameError:
    print "no predictions object named pred found, no clusters to plot"
