<commit_before>'''
plot the best PSO positions.
'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.mlab import griddata
from mpl_toolkits.axes_grid1 import make_axes_locatable
import sys

def rdcsv(name):
    f = open(name,"r")
    data = []
    tmp = f.readline()
    while tmp !="":
        onerow = tmp.split(",")
        last = len(onerow)-1
        k = len(onerow[last])
        if k!= 1:
           onerow[last] = onerow[last][0:k-1]
        if len(onerow) !=  0:
           data.append(onerow)
        tmp = f.readline()
    f.close()
    return(data)

def plot_topo(fnm,show=True,savefig=False):
    params = rdcsv('topology.ranges')
    print params
    
    par_names=[]
    for i in range(0,len(params)):
        par_names.append(params[i][0])
    
    data = np.loadtxt(fnm)
    print np.shape(data)
    
    mpl.rcParams['figure.figsize'] = 15,12
    mpl.rcParams['font.size'] = 10
    n = len(par_names)
    f, axarr = plt.subplots(n,n)
    fitness = data[:,0]
    print np.min(fitness), np.max(fitness)
    fitness = np.log(fitness[~np.isnan(fitness)])
    print np.shape(fitness)
    print np.min(fitness), np.max(fitness)
    j = 0
    for v in par_names:
        i = 0
        for p in par_names:
            if (j < i):
                px = data[:,i+1]
                py = data[:,j+1]
                px = px[~np.isnan(fitness)]
                py = py[~np.isnan(fitness)]
                xi = np.linspace(min(px), max(px),25)
                yi = np.linspace(min(py), max(py),25)
                zi = griddata(px, py, fitness, xi, yi,interp='linear')
                p = axarr[i,j].pcolorfast(xi,yi,zi,cmap='jet')
                axarr[i,j].set_xlabel(par_names[i])
                axarr[i,j].set_ylabel(par_names[j])
                axarr[i,j].set_xlim(float(params[i][1]),float(params[i][2]))
                axarr[i,j].set_ylim(float(params[j][1]),float(params[j][2]))
                if i == 1 :
                    divider = make_axes_locatable(axarr[3,4])
                    cax = divider.append_axes("left", size="10%", pad=0.1)
                    cb = plt.colorbar(p,cax=cax)
                    cb.set_label("Fitness",fontsize=12)
            else:
                f.delaxes(axarr[i,j])
            plt.setp(axarr[i,j].get_xticklabels(), rotation=30)
            i += 1
        j += 1
    #f.subplots_adjust(right=0.8)
    #cbar_ax = f.add_axes([0.85, 0.15, 0.05, 0.7])
    #f.colorbar(p, cax=cbar_ax)
    f.set_tight_layout(True)
    if show: plt.show()
    if savefig: f.savefig('parameter_matrix.pdf',type='PDF',bbox_inches='tight', pad_inches = 0.0)

if __name__ == "__main__":

    plot_topo(sys.argv[1])
<commit_msg>'plot_topo.py' now accepts a sesond file (one of the cost output files, e.g., es_cost.txt) optionally as overlay over fitness topology plot.<commit_after>'''
plot the best PSO positions.
'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.mlab import griddata
from mpl_toolkits.axes_grid1 import make_axes_locatable
import sys

def rdcsv(name):
    f = open(name,"r")
    data = []
    tmp = f.readline()
    while tmp !="":
        onerow = tmp.split(",")
        last = len(onerow)-1
        k = len(onerow[last])
        if k!= 1:
           onerow[last] = onerow[last][0:k-1]
        if len(onerow) !=  0:
           data.append(onerow)
        tmp = f.readline()
    f.close()
    return(data)

def plot_topo(fnm,fnm2=None,show=True,savefig=False):
    params = rdcsv('topology.ranges')
    print params
    
    par_names=[]
    for i in range(0,len(params)):
        par_names.append(params[i][0])
    
    data = np.loadtxt(fnm)
    print np.shape(data)
    if fnm2 is not None:
        data2 = np.loadtxt(fnm2)
        print np.shape(data2)
    
    mpl.rcParams['figure.figsize'] = 15,12
    mpl.rcParams['font.size'] = 10
    n = len(par_names)
    f, axarr = plt.subplots(n,n)
    fitness = data[:,0]
    print np.min(fitness), np.max(fitness)
    fitness = np.log(fitness[~np.isnan(fitness)])
    print np.shape(fitness)
    print np.min(fitness), np.max(fitness)
    j = 0
    for v in par_names:
        i = 0
        for p in par_names:
            if (j < i):
                px = data[:,i+1]
                py = data[:,j+1]
                px = px[~np.isnan(fitness)]
                py = py[~np.isnan(fitness)]
                xi = np.linspace(min(px), max(px),100)
                yi = np.linspace(min(py), max(py),100)
                zi = griddata(px, py, fitness, xi, yi,interp='linear')
                p = axarr[i,j].pcolorfast(xi,yi,zi,cmap='jet')
                if fnm2 is not None:
                    px2 = data2[:,i+2]
                    py2 = data2[:,j+2]
                    axarr[i,j].plot(px2,py2,'ko-',alpha=0.5,mew=0)
                    axarr[i,j].plot(px2[-1],py2[-1],'rs',markersize=7,mew=0)
                axarr[i,j].set_xlabel(par_names[i])
                axarr[i,j].set_ylabel(par_names[j])
                axarr[i,j].set_xlim(float(params[i][1]),float(params[i][2]))
                axarr[i,j].set_ylim(float(params[j][1]),float(params[j][2]))
                if i == 1 :
                    divider = make_axes_locatable(axarr[0,1])
                    cax = divider.append_axes("left", size="10%", pad=0.1)
                    cb = plt.colorbar(p,cax=cax)
                    cb.set_label("Fitness",fontsize=12)
            else:
                f.delaxes(axarr[i,j])
            plt.setp(axarr[i,j].get_xticklabels(), rotation=30)
            i += 1
        j += 1
    f.set_tight_layout(True)
    if show: plt.show()
    if savefig: f.savefig('parameter_matrix.pdf',type='PDF',bbox_inches='tight', pad_inches = 0.0)

if __name__ == "__main__":

    if len(sys.argv) == 2: plot_topo(sys.argv[1])
    if len(sys.argv) == 3: plot_topo(sys.argv[1],sys.argv[2])

{'fraction_non_alphanumeric': 0.1553315105946685, 'fraction_numerical': 0.021872863978127138, 'mean_word_length': 2.0886543535620055, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4904585', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2184, 'n_words': 389}