import matplotlib.pyplot as plt
import numpy as np
from numpy import genfromtxt
#from pylab import polyfit
from scipy.stats import linregress
import pandas as pd
import sys


progress_file = sys.argv[1]
fitness_file = sys.argv[2]
epochs_file = sys.argv[3]
generated_file = sys.argv[4]

v1 = genfromtxt(progress_file)
#v2 = genfromtxt('migration_ring_salsa_10_320.txt', delimiter=' ')
#print v1

#print v1

#print "first column of v1:\n"

t = [row[2] for row in v1]
min_fitness = [row[3] for row in v1]
avg_fitness = [row[4] for row in v1]
max_fitness = [row[5] for row in v1]

'''
print "t:\n"
print t
print "\n\nmin_fitness:\n"
print min_fitness
print "\n\navg_fitness:\n"
print avg_fitness
print "\n\nmax_fitness:\n"
print max_fitness
'''

# plot it!
fig, ax = plt.subplots(1)


ax.plot(t, avg_fitness, lw=2, label='Population Fitness', color='blue')
ax.fill_between(t, min_fitness, max_fitness, facecolor='blue', alpha=0.25)

#ax.set_yscale('log')
#ax.set_xscale('log')

ax.set_title('EXACT Population Fitness')
ax.legend(loc='upper left')
ax.set_xlabel('Genomes Evaluated')
ax.set_ylabel('Fitness (CNN best error)')
ax.grid()

plt.savefig(fitness_file)

min_epochs = [row[6] for row in v1]
avg_epochs = [row[7] for row in v1]
max_epochs = [row[8] for row in v1]

'''
print "t:\n"
print t
print "\n\nmin_epochs:\n"
print min_epochs
print "\n\navg_epochs:\n"
print avg_epochs
print "\n\nmax_epochs:\n"
print max_epochs
'''

# plot it!
fig, ax = plt.subplots(1)


ax.plot(t, avg_epochs, lw=2, label='Population Epochs', color='green')
ax.fill_between(t, min_epochs, max_epochs, facecolor='green', alpha=0.25)

#ax.set_yscale('log')
#ax.set_xscale('log')

ax.set_title('EXACT Population Epochs')
ax.legend(loc='upper left')
ax.set_xlabel('Genomes Evaluated')
ax.set_ylabel('Epochs Required')
ax.grid()

plt.savefig(epochs_file)


disable_edge = [row[9] for row in v1]
enable_edge = [row[10] for row in v1]
split_edge = [row[11] for row in v1]
add_edge = [row[12] for row in v1]
change_size = [row[13] for row in v1]
change_size_x = [row[14] for row in v1]
change_size_y = [row[15] for row in v1]
crossover = [row[16] for row in v1]
#reset_weights = [row[15] for row in v1]

fig, ax = plt.subplots(1)

ax.plot(t, disable_edge, lw=2, label='Disable Edge')
ax.plot(t, enable_edge, lw=2, label='Enable Edge')
ax.plot(t, split_edge, lw=2, label='Split Edge')
ax.plot(t, add_edge, lw=2, label='Add Edge')
ax.plot(t, change_size, lw=2, label='Change Size')
ax.plot(t, change_size_x, lw=2, label='Change Size X')
ax.plot(t, change_size_y, lw=2, label='Change Size Y')
ax.plot(t, crossover, lw=2, label='Crossover')
#ax.plot(t, reset_weights, lw=2, label='Reset Weights')

#ax.set_yscale('log')
#ax.set_xscale('log')

ax.set_title('EXACT Inserted Genome Statistics')
ax.legend(loc='upper left')
ax.set_xlabel('Genomes Evaluated')
ax.set_ylabel('Genomes Inserted into Population Generated By')
ax.grid()

plt.savefig(generated_file)


