
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

sys.path.insert(0, '')

'''
This file takes as input a .csv file generated by the graph_influence.py 
script and produces a plot in two dimensions showing the relationship/correlation between the two objective functions.

'''

def get_PF(myArray):
    myArray = myArray[myArray[:,0].argsort()]
    # Add first row to pareto_frontier
    pareto_frontier = myArray[0:1,:]
    # Test next row against the last row in pareto_frontier
    for row in myArray[1:,:]:
        if sum([row[x] >= pareto_frontier[-1][x]
                for x in range(len(row))]) == len(row):
            # If it is better on all features add the row to pareto_frontier
            pareto_frontier = np.concatenate((pareto_frontier, [row]))
    return pareto_frontier

def get_values(filename):
    df = pd.read_csv(filename, sep=",")
    x=  df["n_nodes"].to_list()
    z = df["influence"].to_list()
    z, x = zip(*sorted(zip(z, x)))

    return z,x
if __name__ == '__main__':
    graphs = ['facebook_combined',  'fb_politician', 'fb_org', 'fb-pages-public-figure', 'pgp','deezerEU']
    models = ['IC', 'WC']
    for idx, name in enumerate(graphs):
        fig,((ax1, ax3), (ax2, ax4)) = plt.subplots(2,2, figsize=(10,4.5), sharey=True, sharex=False)

        for model in models:
            filename = 'experiments_moea/{0}_8-{1}/run-{2}.csv'.format(name, model, 1)
            a ,b = get_values(filename)
            t8 = np.array([[b[i],a[i]] for i in range(len(b))])

            filename = 'experiments_moea/{0}_4-{1}/run-{2}.csv'.format(name, model, 1)
            a ,b = get_values(filename)
            t4 = np.array([[b[i],a[i]] for i in range(len(b))])

            filename = 'experiments_moea/{0}_2-{1}/run-{2}.csv'.format(name, model, 1)
            a ,b = get_values(filename)
            t2 = np.array([[b[i],a[i]] for i in range(len(b))])
            
            filename = 'experiments_moea/{0}-{1}/run-{2}.csv'.format(name, model, 1)
            a ,b = get_values(filename)
            t = np.array([[b[i],a[i]] for i in range(len(b))])



            filename = 'experiments_upscaling/pfs_upscaling/{0}_{1}_8-page_rank.csv'.format(name, model)
            a ,b = get_values(filename)
            m8 = np.array([[b[i],a[i]] for i in range(len(b))])
            m8 = get_PF(m8)

            filename = 'experiments_upscaling/pfs_upscaling/{0}_{1}_4-page_rank.csv'.format(name, model)
            a ,b = get_values(filename)
            m4 = np.array([[b[i],a[i]] for i in range(len(b))])

            m4 = get_PF(m4)


            filename = 'experiments_upscaling/pfs_upscaling/{0}_{1}_2-page_rank.csv'.format(name, model)
            a ,b = get_values(filename)
            m2 = np.array([[b[i],a[i]] for i in range(len(b))])
            
            m2 = get_PF(m2)
            
            if model == 'IC':
                list_leg  = []
                leg = ax1.scatter(t[:,1],t[:,0],color="red",label='Original')
                list_leg.append(leg)
                leg = ax1.scatter(t2[:,1],t2[:,0],color="orange",label='s=2')
                list_leg.append(leg)
                leg = ax1.scatter(t4[:,1],t4[:,0],color="blue",label='s=4')
                list_leg.append(leg)
                leg = ax1.scatter(t8[:,1],t8[:,0],color="green",label='s=8')
                list_leg.append(leg)
                ax1.set_xlabel('% Influenced Nodes',fontsize=12)                
                ax2.set_xlabel('% Influenced Nodes',fontsize=12)
                ax1.set_ylabel('% Nodes as seed set',fontsize=12)
                ax2.set_ylabel('% Nodes as seed set',fontsize=12)
                ax1.set_ylim(0,2.5)
                ax1.set_title('Original and \nDownscaled Solutions', x=0.2, y=0.4,fontsize=12)

                
                ## AXES 2
                ax2.scatter(t[:,1],t[:,0],color="red",label='Original')
                ax2.scatter(m2[:,1],m2[:,0],color="orange",label='$\it{s}$=2')
                ax2.scatter(m4[:,1],m4[:,0],color="blue",label='$\it{s}$=4')
                ax2.scatter(m8[:,1],m8[:,0],color="green",label='$\it{s}$=8')
                ax2.set_title('Original and \nUpscaled Solutions', x=0.2, y=0.3,fontsize=12)
                ax1.set_xlim(0,max((t[:,1])+0.15*max((t[:,1]))))
                ax2.set_xlim(0,max((t[:,1])+0.15*max((t[:,1]))))
                ax1.set_xticklabels([])
                ax2.xaxis.get_label().set_fontsize(12)
                ax2.yaxis.get_label().set_fontsize(12)
                ax2.legend(fontsize=10)
                ax1.text(max((t[:,1]))/2,2.5,'IC Model', fontsize=12,weight="bold")
                ax2.text(max((t[:,1]))/2,2.5,'IC Model', fontsize=12,weight="bold")
            else:
                ax3.scatter(t[:,1],t[:,0],color="red",label='Original')
                ax3.scatter(t2[:,1],t2[:,0],color="orange",label='s=2')   
                ax3.scatter(t4[:,1],t4[:,0],color="blue",label='s=4')
                ax3.scatter(t8[:,1],t8[:,0],color="green",label='s=8')
                ax3.set_ylim(0,2.7)
                ax3.set_xticklabels([])
                ax3.set_xlim(0,int(max((t[:,1])+0.1*max((t[:,1])))))
                ax3.xaxis.get_label().set_fontsize(12)
                ax3.yaxis.get_label().set_fontsize(12)
                ax3.xaxis.get_label().set_fontsize(12)
                ax3.yaxis.get_label().set_fontsize(12)
                ax3.text(max((t[:,1]))/2,2.5,'WC Model', fontsize=12,weight="bold")

                ## AXES 2
                ax4.set_xlabel('% Influenced Nodes',fontsize=12)
                ax4.scatter(t[:,1],t[:,0],color="red",label='Original')
                ax4.scatter(m2[:,1],m2[:,0],color="orange",label='2 map')
                ax4.scatter(m4[:,1],m4[:,0],color="blue",label='4 map')
                ax4.scatter(m8[:,1],m8[:,0],color="green",label='8 map')
                ax3.set_title('Original and \nDownscaled Solutions', x=0.2, y=0.4,fontsize=12)
                ax4.set_title('Original and \nUpscaled Solutions', x=0.2, y=0.3,fontsize=12)
                ax4.text(max((t[:,1]))/2,2.5,'WC Model', fontsize=12,weight="bold")
                ax4.set_xlim(0,int(max((t[:,1])+0.1*max((t[:,1])))))

        plt.subplots_adjust(left=0.07,
        bottom=0.1, 
        right=0.99, 
        top=0.98, 
        wspace=0., 
        hspace=0.0)

        plt.savefig('Figure-3_{0}.eps'.format(name), format='eps')
        plt.savefig('Figure-3_{0}.pdf'.format(name), format='pdf')

        plt.show()

           
        