Conditional Probablity of touch type|trial type and choice | touch type, trial type


In [1]:
# Import libraries
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import seaborn as sns
import numpy as np
from sklearn.metrics import confusion_matrix

In [2]:
# load pro/ret, trial type and choice data
tt = pd.read_csv('~/work/whiskfree/data/tt_36_subset_sorted.csv',header=None)
ch = pd.read_csv('~/work/whiskfree/data/ch_36_subset_sorted.csv',header=None)
proret = pd.read_csv('~/work/whiskfree/data/proret_36_subset_sorted.csv',header=None)

tt = tt.values.reshape(-1,1)
ch = ch.values.reshape(-1,1)
proret = proret.values.reshape(-1,1)

In [3]:
cm_tt = confusion_matrix(tt,proret)

In [4]:
def labelled_image(cm):
    with sns.axes_style("white"):
        plt.imshow(cm,interpolation='none')

    for i in range(0,3):
        for j in range(0,3):
            plt.text(j, i, "{0:.2f}".format(cm[i,j]), va='center', ha='center',bbox=dict(facecolor='white',edgecolor='white', alpha=0.5))


    xlabels = ['Retraction','Protraction','No Touch']
    ylabels = ['Posterior','Anterior','No Go']
    plt.title('Touch type | Trial type') 

    plt.xlabel('Touch type')
    plt.ylabel('Trial type')

    plt.xticks([0,1,2],xlabels)
    plt.yticks([0,1,2],ylabels)

In [5]:
labelled_image(cm_tt)



In [30]:
cm_tt/np.sum(cm_tt)


Out[30]:
array([[ 0.23826715,  0.03369434,  0.09987966],
       [ 0.09747292,  0.13237064,  0.07942238],
       [ 0.01083032,  0.18170878,  0.12635379]])

In [31]:
print(cm_tt)
print(sum(cm_tt.T))


[[198  28  83]
 [ 81 110  66]
 [  9 151 105]]
[309 257 265]

In [6]:
norm_cm_tt = cm_tt.T/sum(cm_tt.T)

In [7]:
norm_cm_tt = norm_cm_tt.T

In [36]:
198/(198+28+83)
81/(81+110+66)


Out[36]:
0.3151750972762646

In [8]:
labelled_image(norm_cm_tt)
plt.title('P(Touch type|Trial type)')


Out[8]:
<matplotlib.text.Text at 0x11431b470>

In [9]:
norm_cm_tch = cm_tt/sum(cm_tt)
labelled_image(norm_cm_tch)
plt.title('P(Trial type | Touch type)')


Out[9]:
<matplotlib.text.Text at 0x11459a2e8>

Now compute P (choice | trial type, touch type)


In [10]:
ch_given_ttpr = np.zeros([3,3,3])

for i in range(len(tt)):
    tt_i = tt[i]
    ch_i = ch[i]
    pr_i = proret[i]
    ch_given_ttpr[tt_i-1,pr_i-1,ch_i-1] += 1

In [11]:
x = plt.hist(ch)



In [14]:
labelled_image(ch_given_ttpr[:,:,0])
plt.title('Trial type | Touch type, Choice = 0')
print(np.sum(ch_given_ttpr[:,:,0]))


347.0

In [15]:
labelled_image(ch_given_ttpr[:,:,1])
plt.title('Trial type | Touch type, Choice = 1')
print(np.sum(ch_given_ttpr[:,:,1]))


242.0

In [16]:
labelled_image(ch_given_ttpr[:,:,2])
plt.title('Trial type | Touch type, Choice = 2')
print(np.sum(ch_given_ttpr[:,:,2]))


242.0

In [63]:
for i in range(3):
    print(i)


0
1
2

In [76]:
plt.plot(ch[:100])


Out[76]:
[<matplotlib.lines.Line2D at 0x115f90630>]

In [80]:
labelled_image(confusion_matrix(tt,ch))
plt.xticks([0,1,2],ylabels)


Out[80]:
([<matplotlib.axis.XTick at 0x1154a6860>,
  <matplotlib.axis.XTick at 0x1149bd5c0>,
  <matplotlib.axis.XTick at 0x1145792b0>],
 <a list of 3 Text xticklabel objects>)

In [ ]: