示例#1
0
    def plot_loss(self, X, Y, loss, show=True):
        # Graphic display
        title = str(self.step) + ': Epochs:' + str(self.epochs) + \
            " batch:" + str(self.batch_size) + \
            " alpha:" + str(self.learning_rate)
        train_X1 = X[:, 0]
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.text2D(0.05, 0.95, title, transform=ax.transAxes)
        # trisurf plot
        # ax.plot_trisurf(np.ravel(train_X1),
        #                 np.ravel(train_X2), np.ravel(Y), color='b')
        ax.plot_trisurf(np.ravel(train_X1), np.ravel(Y), np.ravel(loss))

        ax.set_xlabel('Weight')
        ax.set_ylabel('bias')
        ax.set_zlabel('Loss')
        ax.legend()

        # save image to buffer
        buf = io.BytesIO()
        if show:
            # If want to view image in Tensorboard, do not show plot => Strange
            # bug!!!
            image_path = self.logs_path + "/images"
            if not os.path.exists(image_path):
                os.mkdir(image_path)
            filename = image_path + "/" + \
                "loss-" + \
                time.strftime("%Y%m%d-%H%M%S", time.localtime()) + ".png"
            plt.savefig(filename, format='png')
            plt.show()
        else:
            plt.savefig(buf, format='png')
            buf.seek(0)
        plt.close()
        # plt.clf()
        return buf
示例#2
0
#   cPickle.dump([difficulties, generosities, ave_switches], handle)



#read
with open(filename, 'rb') as handle:
   data = cPickle.load(handle)


difficulties=data[0]
generosities=data[1]
ave_switches=data[2]
fig = plt.figure()
ax = Axes3D(fig)
Axes3D.scatter(ax, difficulties, generosities, ave_switches, cmap=cm.jet)
Axes3D.plot_trisurf(ax, difficulties, generosities, ave_switches, cmap=cm.jet)
plt.show()



print ave_switches


 # griddata and contour.     
xi = np.linspace(min(difficulties),max(difficulties),15)
yi = np.linspace(min(generosities),max(generosities),15)
xi = np.linspace(0,1,15)
yi = np.linspace(0,1,15)

print len(difficulties), len(generosities), len(ave_switches)
示例#3
0
import glob

def read_and_overview(filename):
    """Read NetCDF using read_generic_netcdf and print upper level dictionary keys
    """
    test = read_generic_netcdf(filename)
    print("\nPrint keys for file %s" % os.path.basename(filename))
    for key in test.keys():
        print("\t%s" % key)

pfad = ('/automount/ags/velibor/gpmdata/nicht3dComposit/*.nc')

pfad_3d = sorted(glob.glob(pfad))[1]

comp3d = get_wradlib_data_file(pfad_3d)
read_and_overview(comp3d)

from netCDF4 import Dataset
import numpy as np

comp3d = Dataset(pfad_3d, mode='r')



import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Axes3D.plot_trisurf(x,y,z)
示例#4
0
def block_analysis(model=False):
    
    # BLOCK BY BLOCK
    pre_metacog_switches={0:[], 1:[], 2:[]}
    post_metacog_switches={0:[], 1:[], 2:[]}
    difficulties={0:[], 1:[], 2:[]}
    generosities={0:[], 1:[], 2:[]}
    pre_met_swi_rews={0:[], 1:[], 2:[]}
    post_met_swi_rews={0:[], 1:[], 2:[]}
    
    for condition in set(data.conditions):
        for payrate in set(data.payrates):
            pre_metacog_switches[condition].append(gameAnalyzer.metacog_switches(data.bandit_data[condition][payrate], n_trials=subject_trials, post_metacog=False))
            post_metacog_switches[condition].append(gameAnalyzer.metacog_switches(data.bandit_data[condition][payrate], n_trials=subject_trials, post_metacog=True))
            difficulties[condition].append(1-abs(payrate[1]-payrate[0]))
            generosities[condition].append((payrate[1]+payrate[0])/2)
            pre_met_swi_rews[condition].append(gameAnalyzer.metacog_rewards(data.bandit_data[condition][payrate], n_trials=subject_trials, post_metacog=False))
            post_met_swi_rews[condition].append(gameAnalyzer.metacog_rewards(data.bandit_data[condition][payrate], n_trials=subject_trials, post_metacog=True))

    print pre_metacog_switches
    print post_metacog_switches

    print np.mean(pre_metacog_switches[0])
    print np.mean(pre_metacog_switches[1])
    print np.mean(pre_metacog_switches[2])

    print np.mean(post_metacog_switches[0])
    print np.mean(post_metacog_switches[1])
    print np.mean(post_metacog_switches[2])

    s0=sum(post_metacog_switches[0])
    s1=sum(post_metacog_switches[1])
    print s0, s1


    print pre_met_swi_rews
    print post_met_swi_rews

    print np.mean(post_met_swi_rews[0]), np.mean(post_met_swi_rews[1])

    normdif0=np.array(post_metacog_switches[0])-np.array(post_met_swi_rews[0])
    normdif1=np.array(post_metacog_switches[1])-np.array(post_met_swi_rews[1])

    print normdif0
    print normdif1
    print np.mean(normdif0), np.mean(normdif1)


    collapsing=np.array(post_metacog_switches[0])-np.array(post_metacog_switches[1])
    normcollapsing=normdif0-normdif1

    # p=0.057
    print 'pvalue from ttest: {0}'.format(stats.ttest_rel(post_metacog_switches[0],post_metacog_switches[1])[1])
    print 'same for rewards (pvalue from ttest): {0}'.format(stats.ttest_rel(post_met_swi_rews[0], post_met_swi_rews[1])[1])
    print '...and subtracted: {0}'.format(stats.ttest_rel(normdif0, normdif1)[1])


    fig = plt.figure()
    ax = Axes3D(fig)
    Axes3D.scatter(ax, difficulties[0], generosities[0], collapsing, cmap=cm.jet)
    Axes3D.plot_trisurf(ax, difficulties[0], generosities[0], collapsing, cmap=cm.jet)
    plt.show()

    # griddata and contour.     
    xi = np.linspace(min(difficulties[0]),max(difficulties[0]),15)
    yi = np.linspace(min(generosities[0]),max(generosities[0]),15)
    xi = np.linspace(0,1,15)
    yi = np.linspace(0,1,15)

    print len(difficulties[0]), len(generosities[0]), len(collapsing)

    zi = griddata(difficulties[0],generosities[0],collapsing,xi,yi,interp='nn')#linear')

    #plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
    plt.contourf(xi,yi,zi,cmap=cm.jet)#,
                 #norm=plt.normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))

    plt.scatter(difficulties[0], generosities[0], marker='s', c=collapsing, s=200, cmap=cm.jet)
    plt.colorbar() # draw colorba
    plt.xlim([0,1])
    plt.ylim([0,1])
    plt.show()

    # plt.scatter(difficulties[0], generosities[0], marker='s', c=collapsing, s=200, cmap=cm.coolwarm)
    # plt.colorbar()
    # plt.xlim([0,1])
    # plt.ylim([0,1])
    # plt.show()




    if model:

        #MCMC inference for rate difference
        modeldata=np.array([post_metacog_switches[0],post_metacog_switches[1]])
        #modeldata=np.array([post_met_swi_rews[0],post_met_swi_rews[1]]) #this is dubious, but good, nonsignificant
        #modeldata=np.array([normdif0,normdif1]) #this does not work, negative values.. should adapt model for negative reward triggering switch.

        model=pymc.MCMC(rateDifferenceModel.make_model(modeldata, subject_trials))
        model.sample(iter=10100, burn=100, thin=10, progress_bar=False)


        
        deltas=model.trace('delta')[:,:]
        mdeltas=np.mean(deltas,0)
        print 'supermean deltas: {0}'.format(np.mean(mdeltas))

        alldeltas=deltas.reshape(deltas.shape[0]*deltas.shape[1])
        print 'testing: {0}, {1}'.format(sum(alldeltas<0), len(alldeltas))
        print 'aproximate pvalue from bayesian inference: {0}'.format(float(sum(alldeltas<0))/len(alldeltas))
       
        # p=0.058
        print 'pvalue for deltas != 0 from ttest (taking mean): {0}'.format(stats.ttest_1samp(mdeltas, 0))
        print 'pvalue for deltas != 0 from ttest (all together): {0}'.format(stats.ttest_1samp(alldeltas, 0))

        print 'delta shape: {0}'.format(deltas.shape)
        pvals=np.asfarray(np.sum(deltas<0,0))/deltas.shape[0]
        print 'try something better? 16 p values: {0}'.format(pvals)
        print 'same, bonferroni corrected: {0}'.format(pvals*len(pvals))

        print np.where(pvals==pvals.min())
        npgens0=np.array(generosities[0])
        npgens1=np.array(generosities[1])
        npdifs0=np.array(difficulties[0])
        npdifs1=np.array(difficulties[1])

        print 'most significant effect @G={0}, D={1}'.format(npgens0[pvals==pvals.min()],npdifs0[pvals==pvals.min()])
        print 'just a check @G={0}, D={1}'.format(npgens1[pvals==pvals.min()],npdifs1[pvals==pvals.min()])

        # plt.hist(np.mean(deltas,0))
        # plt.show()

        plt.hist(alldeltas)
        plt.show()

        # utils.plot_and_correlate(difficulties[0],mdeltas) 
        # utils.plot_and_correlate(difficulties[0],pre_metacog_switches[0])
        # utils.plot_and_correlate(difficulties[0],post_metacog_switches[0])
        # utils.plot_and_correlate(difficulties[0],post_metacog_switches[1])
        # utils.plot_and_correlate(generosities[0],mdeltas) 
        # utils.plot_and_correlate(generosities[0],pre_metacog_switches[0])
        # utils.plot_and_correlate(generosities[0],post_metacog_switches[0])
        # utils.plot_and_correlate(generosities[0],post_metacog_switches[1])

        # utils.plot_and_correlate(generosities[0], difficulties[0])
        fig = plt.figure()
        ax = Axes3D(fig)
        Axes3D.scatter(ax, difficulties[0], generosities[0], mdeltas, cmap=cm.jet)
        Axes3D.plot_trisurf(ax, difficulties[0], generosities[0], mdeltas, cmap=cm.jet)
        plt.show()


        # griddata and contour.     
        xi = np.linspace(min(difficulties[0]),max(difficulties[0]),15)
        yi = np.linspace(min(generosities[0]),max(generosities[0]),15)
        xi = np.linspace(0,1,15)
        yi = np.linspace(0,1,15)

        zi = griddata(difficulties[0],generosities[0],mdeltas,xi,yi,interp='nn')#linear')

        #plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
        plt.contourf(xi,yi,zi,cmap=cm.jet)#,
                     #norm=plt.normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))

        plt.scatter(difficulties[0], generosities[0], marker='s', c=mdeltas, s=200, cmap=cm.jet)
        plt.colorbar() # draw colorba
        plt.xlim([0,1])
        plt.ylim([0,1])
        plt.show()
示例#5
0
    def plot(self, X, Y, show=True):
        # Graphic display
        predicted = self.sess.run(self.predict_model(X))
        title = str(self.step) + ': Epochs:' + str(self.epochs) + \
            " batch:" + str(self.batch_size) + \
            " alpha:" + str(self.learning_rate)
        if self.n_features == 1:  # plot 2D image
            plt.title(title)
            # plt.plot(self.train_X, self.train_Y, c='b', label='Original data')
            # plt.plot(self.train_X, predicted, c='r', label='Fitted')
            plt.scatter(X, Y, c='b', label='Original data')
            plt.scatter(X, predicted, c='r', label='Fitted')
            plt.legend()
        else:  # plot 3D for X1, X2
            if (self.n_features == 2):
                train_X1, train_X2 = np.hsplit(X, self.n_features)
            else:
                train_X1, train_X2, _ = np.hsplit(X, self.n_features)
            fig = plt.figure()
            ax = fig.gca(projection='3d')
            ax.text2D(0.05, 0.95, title, transform=ax.transAxes)
            # line plot
            # x = np.squeeze(np.asarray(train_X1))
            # y = np.squeeze(np.asarray(train_X2))
            # z = np.squeeze(np.asarray(Y))
            # z1 = np.squeeze(np.asarray(predicted))
            # ax.plot(x, y, zs=z, c='b', label='Original data')
            # ax.plot(x, y, zs=z1, c='r', label='Fitted')

            # statter plot
            ax.scatter(train_X1,
                       train_X2,
                       Y,
                       c='b',
                       marker='s',
                       label='Original data')
            # ax.scatter(train_X1, train_X2, predicted,
            #            c='r', marker='v', label='Fitted')

            # trisurf plot
            # ax.plot_trisurf(np.ravel(train_X1),
            #                 np.ravel(train_X2), np.ravel(Y), color='b')
            ax.plot_trisurf(np.ravel(train_X1),
                            np.ravel(train_X2),
                            np.ravel(predicted),
                            color='r')

            ax.set_xlabel('X1')
            ax.set_ylabel('X2')
            ax.set_zlabel('Y')
            ax.legend()

        # save image to buffer
        buf = io.BytesIO()
        if show:
            # If want to view image in Tensorboard, do not show plot => Strange
            # bug!!!
            image_path = self.logs_path + "/images"
            if not os.path.exists(image_path):
                os.mkdir(image_path)
            filename = image_path + "/" + \
                time.strftime("%Y%m%d-%H%M%S", time.localtime()) + ".png"
            plt.savefig(filename, format='png')
            plt.show()
        else:
            plt.savefig(buf, format='png')
            buf.seek(0)
        plt.close()
        # plt.clf()
        return buf