import force_scheme as force
import numpy as np
import math
import matplotlib.pyplot as plt
import kruskal_stress

from numpy import random
from timeit import default_timer as timer
from datetime import timedelta
from matplotlib.colors import ListedColormap
from sys import getsizeof



# read the distance matrix
input_file = "dmat"
size, distance_matrix = force.read_distance_matrix(input_file)

print('Distance matrix size in memory: ', round(getsizeof(distance_matrix) / 1024 / 1024, 2), 'MB')

# execute force scheme
start = timer()
projection = np.random.random((size, 2))
nr_iterations, error = force.execute(distance_matrix, projection, 10000, 0.5, 0.35, 0.0000001)
#nr_iterations, error = force.execute(distance_matrix, projection, int(math.sqrt(size)), 0.5, 0.35, 0.0000001)
end = timer()

print('ForceScheme took {0} to execute {1} iterations with error equals {2}'.format(timedelta(seconds=end - start),nr_iterations, error))

# calculate stress
stress = kruskal_stress.calculate(distance_matrix, projection)
print('Kruskal stress {0}:'.format(stress))

# save projection
np.savetxt(input_file + "_projection.txt", projection, delimiter=" ", fmt="%s")

# show projection
#labels = np.zeros(size)
#plt.figure()
#plt.scatter(projection[:, 0], projection[:, 1], c=labels,
#    cmap=ListedColormap(['blue', 'red', 'green']), edgecolors='face', linewidths=0.5, s=4)
#plt.grid(linestyle='dotted')
#plt.show()


#def main2():
#    # read the distance matrix
#    input_file = "/Users/fpaulovich/Documents/protein_folding/matriz_distancia"
#    size, distance_matrix = force.read_distance_matrix(input_file)

#    print('Distance matrix size in memory: ', round(getsizeof(distance_matrix) / 1024 / 1024, 2), 'MB')

#    # execute force scheme
#    start = timer()
#    projection = np.random.random((size, 2))
#    nr_iterations, error = force.execute(distance_matrix, projection, int(math.sqrt(size)), 0.5, 0.95, 0.0000001)
#    end = timer()

#    print('ForceScheme took {0} to execute {1} iterations with error equals {2}'.format(timedelta(seconds=end - start),
#                                                                                        nr_iterations, error))

#    # calculate stress
#    stress = kruskal_stress.calculate(distance_matrix, projection)
#    print('Kruskal stress {0}:'.format(stress))

#    # save projection
#    np.savetxt(input_file + "_projection.txt", projection, delimiter=" ", fmt="%s")

    # show projection
#    labels = np.zeros(size)
#    for i in range(8000):
#        labels[i] = 0

#    for i in range(8000, 16000):
#        labels[i] = 1

#    for i in range(16000, 25240):
#        labels[i] = 2

#    plt.figure()
#   plt.scatter(projection[:, 0], projection[:, 1], c=labels,
#                cmap=ListedColormap(['blue', 'red', 'green']), edgecolors='face', linewidths=0.5, s=4)
#    plt.grid(linestyle='dotted')
#    plt.show()


#if __name__ == "__main__":
#    main2()
#    exit(0)
