"""
Script to perform LLE

authors : Rohit Bhattacharya, Azwad Sabik
emails  : rohit.bhattachar@gmail.com, azwadsabik@gmail.com
"""

# imports
from time import time
from sklearn import manifold
import pickle
import numpy as np
import matplotlib.pyplot as plt
import random
import sys

X_pickle = sys.argv[1]
Y_pickle = sys.argv[2]
output_name = sys.argv[3]

X = pickle.load(open(X_pickle, 'rb'))
Y = pickle.load(open(Y_pickle, 'rb'))

n_samples, n_features = X.shape
n_neighbors = 10

# Locally linear embedding of the dataset
print("Computing LLE embedding")
clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2,
                                      method='standard')
t0 = time()
X_lle = clf.fit_transform(X)
print("Done. Reconstruction error: %g" % clf.reconstruction_error_)

plt.scatter(X_lle[:, 0], X_lle[:, 1])
for i in range(len(Y)):
    plt.text(X_lle[i, 0], X_lle[i, 1], Y[i], 
            color=plt.cm.Set1(random.randint(1, 110)),
            fontdict={'weight': 'bold', 'size': 4})

plt.savefig('../results/' + output_name + '.png')
plt.show()
