def plotTimeDistanceTarget(foldername):
    rs = ReadSetupFile()

    dicoTime = {}
 
    for i in range(len(rs.sizeOfTarget)):
        name =  rs.CMAESpath + str(rs.sizeOfTarget[i]) + "/" + foldername + "/TrajTime/"

        trajTimes = getTrajTimeData(name)

        for k, v in trajTimes.items():
            for j in range(len(v)):
                distance = round(rs.getDistanceToTarget(v[j][0],v[j][1]),2)
                if not distance in dicoTime.keys():
                    dicoTime[distance] = {}
                if not rs.sizeOfTarget[i] in dicoTime[distance].keys():
                    dicoTime[distance][rs.sizeOfTarget[i]] = []
                dicoTime[distance][rs.sizeOfTarget[i]].append(v[j][2])
 
    plotTab = []

    fig = plt.figure(1, figsize=(16,9))
    plt.ylabel("time (s)")
    plt.xlabel("Target size (mm)")
    for key in sorted(dicoTime.keys()):
        plotTab.append(plt.plot([i for i in sorted(dicoTime[key].keys())], [np.mean(dicoTime[key][i]) for i in sorted(dicoTime[key].keys())], label = str("Distance: " + str(key))))
    plt.legend(loc = 0)
    plt.savefig("ImageBank/timedist"+foldername+'.png', bbox_inches='tight')
    plt.show(block = True)
def plotTimeDistanceTarget(foldername):
    rs = ReadSetupFile()

    dicoTime = {}

    for i in range(len(rs.sizeOfTarget)):
        name = rs.CMAESpath + str(
            rs.sizeOfTarget[i]) + "/" + foldername + "/TrajTime/"

        trajTimes = getTrajTimeData(name)

        for k, v in trajTimes.items():
            for j in range(len(v)):
                distance = round(rs.getDistanceToTarget(v[j][0], v[j][1]), 2)
                if not distance in dicoTime.keys():
                    dicoTime[distance] = {}
                if not rs.sizeOfTarget[i] in dicoTime[distance].keys():
                    dicoTime[distance][rs.sizeOfTarget[i]] = []
                dicoTime[distance][rs.sizeOfTarget[i]].append(v[j][2])

    plotTab = []

    fig = plt.figure(1, figsize=(16, 9))
    plt.ylabel("time (s)")
    plt.xlabel("Target size (mm)")
    for key in sorted(dicoTime.keys()):
        plotTab.append(
            plt.plot([i for i in sorted(dicoTime[key].keys())], [
                np.mean(dicoTime[key][i]) for i in sorted(dicoTime[key].keys())
            ],
                     label=str("Distance: " + str(key))))
    plt.legend(loc=0)
    plt.savefig("ImageBank/timedist" + foldername + '.png',
                bbox_inches='tight')
    plt.show(block=True)
def plotFittsLaw(foldername, rbfn=False):
    rs = ReadSetupFile()

    timeDistWidth = []
    for i in range(len(rs.sizeOfTarget)):
        name = rs.CMAESpath + str(
            rs.sizeOfTarget[i]) + "/" + foldername + "/TrajTime/"

        trajTimes = getTrajTimeData(name)

        for k, v in trajTimes.items():
            for j in range(len(v)):
                distance = rs.getDistanceToTarget(v[j][0], v[j][1])
                trajtime = v[j][2]
                size = rs.sizeOfTarget[i]
                timeDistWidth.append((distance, size, trajtime))

    MT, DI = [], []
    for el in timeDistWidth:
        MT.append(el[2])
        DI.append(np.log2(el[0] / el[1]))
    slope, intercept, r_value, p_value, std_err = stats.linregress(DI, MT)
    yLR = slope * np.asarray(DI) + intercept
    plt.figure()

    for el in timeDistWidth:
        if el[0] <= 0.15:
            plt.scatter(np.log2(el[0] / el[1]), el[2], c='blue')
        elif el[0] <= 0.28:
            plt.scatter(np.log2(el[0] / el[1]), el[2], c='green')
        else:
            plt.scatter(np.log2(el[0] / el[1]), el[2], c='red')

    plt.plot(DI, yLR)
    plt.title("a = " + str(slope) + " b = " + str(intercept) + " r^2 = " +
              str(r_value**2))
    plt.xlabel("log(D/W)/log(2)")
    plt.ylabel("Movement time (s)")
    plt.savefig("ImageBank/fitts" + foldername + ".png", bbox_inches='tight')
    plt.show(block=True)
def plotFittsLaw(foldername, rs, rbfn = False):
    plt.figure(1, figsize=(16,9))
    timeDistWidth = []
    for i in range(len(rs.target_size)):
        name =  rs.OPTIpath + str(rs.target_size[i]) + "/" + foldername + "/TrajTime/"

        trajTimes = getTrajTimeData(name)

        for _, v in trajTimes.items():
            for j in range(len(v)):
                distance = rs.getDistanceToTarget(v[j][0],v[j][1])
                trajtime = v[j][2]
                size = rs.target_size[i]
                timeDistWidth.append((distance, size, trajtime))
            
    MT, DI = [], []
    for el in timeDistWidth:
        MT.append(el[2])
        DI.append(np.log2(el[0]/el[1]))
    slope, intercept, r_value, _, _ = stats.linregress(DI,MT)
    yLR = slope * np.asarray(DI) + intercept


    for el in timeDistWidth:
            if el[0]<=0.15:
                plt.scatter(np.log2(el[0]/el[1]), el[2], c ='blue')
            elif el[0]<=0.28:
                plt.scatter(np.log2(el[0]/el[1]), el[2], c ='green')
            else:
                plt.scatter(np.log2(el[0]/el[1]), el[2], c ='red')
        
    plt.plot(DI, yLR)
    plt.title("a = " + str(slope) + " b = " + str(intercept) + " r^2 = " + str(r_value**2))
    plt.xlabel("log(D/W)/log(2)")
    plt.ylabel("Movement time (s)")
    imageFolder = rs.OPTIpath + "/ImageBank/"
    checkIfFolderExists(imageFolder)  
    plt.savefig(imageFolder+"Fitts.svg", bbox_inches='tight')
    plt.show(block = True)
def plotFittsLaw(foldername, rbfn = False):
    rs = ReadSetupFile()

    timeDistWidth = []
    for i in range(len(rs.sizeOfTarget)):
        name =  rs.CMAESpath + str(rs.sizeOfTarget[i]) + "/" + foldername + "/TrajTime/"

        trajTimes = getTrajTimeData(name)

        for k, v in trajTimes.items():
            for j in range(len(v)):
                distance = rs.getDistanceToTarget(v[j][0],v[j][1])
                trajtime = v[j][2]
                size = rs.sizeOfTarget[i]
                timeDistWidth.append((distance, size, trajtime))
            
    MT, DI = [], []
    for el in timeDistWidth:
        MT.append(el[2])
        DI.append(np.log2(el[0]/el[1]))
    slope, intercept, r_value, p_value, std_err = stats.linregress(DI,MT)
    yLR = slope * np.asarray(DI) + intercept
    plt.figure()

    for el in timeDistWidth:
            if el[0]<=0.15:
                plt.scatter(np.log2(el[0]/el[1]), el[2], c ='blue')
            elif el[0]<=0.28:
                plt.scatter(np.log2(el[0]/el[1]), el[2], c ='green')
            else:
                plt.scatter(np.log2(el[0]/el[1]), el[2], c ='red')
        
    plt.plot(DI, yLR)
    plt.title("a = " + str(slope) + " b = " + str(intercept) + " r^2 = " + str(r_value**2))
    plt.xlabel("log(D/W)/log(2)")
    plt.ylabel("Movement time (s)")
    plt.savefig("ImageBank/fitts"+foldername+".png", bbox_inches='tight')
    plt.show(block = True)
def plotTimeColorMap(what, foldername = "None", targetSize = "All"):
    '''
    Cette fonction permet d'afficher le profil de temps des trajectoires
    
    Entrees:      -what: choix des donnees a afficher
    '''
    rs = ReadSetupFile()
    fig = plt.figure(1, figsize=(16,9))

    if what == "CMAES" and targetSize == "All":
        for i in range(len(rs.sizeOfTarget)):
            ax = plt.subplot2grid((2,2), (i/2,i%2))
            name =  rs.CMAESpath + str(rs.sizeOfTarget[i]) + "/" + foldername + "/TrajTime/"
            times = getTrajTimeData(name)

            x0 = []
            y0 = []
            time = []

            for k, v in times.items():
                for j in range(len(v)):
                    x0.append(v[j][0])
                    y0.append(v[j][1])
                    time.append(v[j][2])

            xi = np.linspace(-0.4,0.4,100)
            yi = np.linspace(0.12,0.58,100)
            zi = griddata(x0, y0, time, xi, yi)

            t1 = ax.scatter(x0, y0, c=time, marker=u'o', s=50, cmap=cm.get_cmap('RdYlBu'))
            ax.scatter(rs.XTarget, rs.YTarget, c ='g', marker='v', s=200)
            CS = ax.contourf(xi, yi, zi, 15, cmap=cm.get_cmap('RdYlBu'))
            ax.set_xlabel("X (m)")
            ax.set_ylabel("Y (m)")
            ax.set_title(str("Time map for target " + str(rs.sizeOfTarget[i])))
            fig.colorbar(t1, shrink=0.5, aspect=5)
            t1 = ax.scatter(x0, y0, c='b', marker=u'o', s=20)

    else:
        if what == "CMAES":
            name = rs.CMAESpath + targetSize + "/" + foldername + "/TrajTime/"
        elif what == "Brent":
            name = BrentTrajectoriesFolder
        else:
            name = rs.NNpath + foldername + "/TrajTime/"

        times = getTrajTimeData(name)
   
        x0 = []
        y0 = []
        time = []

        for k, v in times.items():
            for j in range(len(v)):
                x0.append(v[j][0])
                y0.append(v[j][1])
                time.append(v[j][2])

        xi = np.linspace(-0.4,0.4,100)
        yi = np.linspace(0.12,0.58,100)
        zi = griddata(x0, y0, time, xi, yi)
    
        t1 = plt.scatter(x0, y0, c=time, marker=u'o', s=50, cmap=cm.get_cmap('RdYlBu'))
        plt.scatter(rs.XTarget, rs.YTarget, c ='g', marker='v', s=200)
        CS = plt.contourf(xi, yi, zi, 15, cmap=cm.get_cmap('RdYlBu'))
        fig.colorbar(t1, shrink=0.5, aspect=5)
        plt.scatter(x0, y0, c='b', marker=u'o', s=20)
        plt.xlabel("X (m)")
        plt.ylabel("Y (m)")

    plt.savefig("ImageBank/"+what+'_timemap'+foldername+'.png', bbox_inches='tight')
    plt.show(block = True)
def plotTimeColorMap(what, foldername="None", targetSize="All"):
    '''
    Cette fonction permet d'afficher le profil de temps des trajectoires
    
    Entrees:      -what: choix des donnees a afficher
    '''
    rs = ReadSetupFile()
    fig = plt.figure(1, figsize=(16, 9))

    if what == "CMAES" and targetSize == "All":
        for i in range(len(rs.sizeOfTarget)):
            ax = plt.subplot2grid((2, 2), (i / 2, i % 2))
            name = rs.CMAESpath + str(
                rs.sizeOfTarget[i]) + "/" + foldername + "/TrajTime/"
            times = getTrajTimeData(name)

            x0 = []
            y0 = []
            time = []

            for k, v in times.items():
                for j in range(len(v)):
                    x0.append(v[j][0])
                    y0.append(v[j][1])
                    time.append(v[j][2])

            xi = np.linspace(-0.4, 0.4, 100)
            yi = np.linspace(0.12, 0.58, 100)
            zi = griddata(x0, y0, time, xi, yi)

            t1 = ax.scatter(x0,
                            y0,
                            c=time,
                            marker=u'o',
                            s=50,
                            cmap=cm.get_cmap('RdYlBu'))
            ax.scatter(rs.XTarget, rs.YTarget, c='g', marker='v', s=200)
            CS = ax.contourf(xi, yi, zi, 15, cmap=cm.get_cmap('RdYlBu'))
            ax.set_xlabel("X (m)")
            ax.set_ylabel("Y (m)")
            ax.set_title(str("Time map for target " + str(rs.sizeOfTarget[i])))
            fig.colorbar(t1, shrink=0.5, aspect=5)
            t1 = ax.scatter(x0, y0, c='b', marker=u'o', s=20)

    else:
        if what == "CMAES":
            name = rs.CMAESpath + targetSize + "/" + foldername + "/TrajTime/"
        elif what == "Brent":
            name = BrentTrajectoriesFolder
        else:
            name = rs.RBFNpath + foldername + "/TrajTime/"

        times = getTrajTimeData(name)

        x0 = []
        y0 = []
        time = []

        for k, v in times.items():
            for j in range(len(v)):
                x0.append(v[j][0])
                y0.append(v[j][1])
                time.append(v[j][2])

        xi = np.linspace(-0.4, 0.4, 100)
        yi = np.linspace(0.12, 0.58, 100)
        zi = griddata(x0, y0, time, xi, yi)

        t1 = plt.scatter(x0,
                         y0,
                         c=time,
                         marker=u'o',
                         s=50,
                         cmap=cm.get_cmap('RdYlBu'))
        plt.scatter(rs.XTarget, rs.YTarget, c='g', marker='v', s=200)
        CS = plt.contourf(xi, yi, zi, 15, cmap=cm.get_cmap('RdYlBu'))
        fig.colorbar(t1, shrink=0.5, aspect=5)
        plt.scatter(x0, y0, c='b', marker=u'o', s=20)
        plt.xlabel("X (m)")
        plt.ylabel("Y (m)")

    plt.savefig("ImageBank/" + what + '_timemap' + foldername + '.png',
                bbox_inches='tight')
    plt.show(block=True)