In [1]:
import pandas as pd
import os
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

import json

Begin by defining the output parsing function for usage

Define overnight output file location and parse into dictionary


In [3]:
OVERNIGHT_FILE = '/home/buck06191/Desktop/optimisation_edit.json'
with open(OVERNIGHT_FILE) as f:
    optim_data = json.load(f)
# Check length of each dict section before converting to pandas DF
import copy
x = copy.copy(optim_data)
{k:len(x[k]) for k in x.keys()}


Out[3]:
{'Cortex': 286,
 'Final_Distance': 286,
 'Max_Demand': 286,
 'R_autc': 286,
 'R_auto': 286,
 'R_autp': 286,
 'R_autu': 286,
 'Subject': 286,
 't_c': 286,
 't_o': 286,
 't_p': 286,
 't_u': 286}

In [5]:
overnight_df = pd.DataFrame(optim_data)

Define a function that lets us pull out the optimal fit for a specific cortex


In [6]:
def optimal_fit(xx, cortex):
    df = xx.loc[xx['Cortex']==cortex]
    return df.loc[df['Final_Distance']==df['Final_Distance'].min()]

In [7]:
df_PFC = overnight_df.loc[overnight_df['Cortex']=='PFC']

df_VC = overnight_df.loc[overnight_df['Cortex']=='VC']
optimal_PFC = df_PFC.loc[df_PFC.groupby(['Subject', 'Max_Demand']).Final_Distance.agg('idxmin')]
optimal_PFC


Out[7]:
Cortex Final_Distance Max_Demand R_autc R_auto R_autp R_autu Subject t_c t_o t_p t_u
255 PFC 6.195697 1.0 1.563017 1.959744 4.803173 0.472895 P11 6.438027 21.325728 6.019303 0.642275
179 PFC 10.208966 1.1 1.560094 1.921937 4.964348 0.438660 P11 6.409234 14.737230 4.966353 0.532556
207 PFC 13.944531 1.2 1.604058 1.884541 5.019998 0.412560 P11 6.163953 15.261951 3.953412 0.461357
231 PFC 16.995106 1.3 1.587783 1.891116 5.197086 0.400480 P11 6.013515 17.715816 5.973495 0.645978
208 PFC 21.917251 1.4 1.624444 1.933047 5.082284 0.401548 P11 6.237209 21.528413 6.467041 0.638814
83 PFC 26.045178 1.5 1.643413 1.875358 5.127953 0.404872 P11 6.260404 14.005663 4.178590 0.481915
192 PFC 28.854091 1.6 1.614310 1.999645 5.190076 0.402627 P11 6.401354 14.986166 4.509350 0.614465
247 PFC 35.994290 1.7 1.765249 1.929190 5.189365 0.418802 P11 6.035616 14.425241 4.340306 0.683417
139 PFC 40.082021 1.8 1.519556 1.996341 5.188230 0.428767 P11 5.817282 14.064691 4.384709 0.642087
264 PFC 42.624146 1.9 1.636864 1.945413 5.130129 0.401264 P11 6.053325 15.567831 6.217210 0.686720
263 PFC 45.965281 2.0 1.538308 1.958579 5.155119 0.400663 P11 5.172909 15.089791 3.919324 0.651949
275 PFC 8.137347 1.0 1.507281 1.841437 4.848314 0.529229 P16 6.316175 14.555203 3.615557 0.572584
204 PFC 3.699105 1.1 1.591517 1.865329 4.087723 0.416710 P16 3.669144 21.290948 5.479690 0.637210
15 PFC 8.659316 1.2 2.753630 1.982115 4.692406 0.428608 P16 4.182119 17.449938 4.985039 0.618412
205 PFC 15.742527 1.3 2.837524 1.966852 3.420991 0.404882 P16 3.716456 16.967442 5.556719 0.550862
246 PFC 27.372620 1.4 2.894150 1.912717 4.170832 0.408348 P16 3.944478 15.968562 6.482351 0.684789
61 PFC 41.582620 1.5 2.854877 1.945227 2.822406 0.429079 P16 3.507296 14.014743 5.595843 0.657613
185 PFC 52.119192 1.6 2.811157 1.965334 3.755794 0.414224 P16 3.761071 14.923645 4.557607 0.619157
215 PFC 59.974465 1.7 2.839077 1.962975 2.977261 0.402166 P16 3.711772 14.061716 6.304951 0.635075
157 PFC 72.636446 1.8 2.700455 1.995934 2.970587 0.402907 P16 3.583206 15.260185 4.972474 0.616377
29 PFC 81.838380 1.9 2.873884 1.988412 4.809365 0.402230 P16 3.960011 14.059110 5.881565 0.696135
155 PFC 94.751675 2.0 2.834545 1.999200 3.728509 0.405336 P16 3.968001 14.499800 3.896589 0.539265
254 PFC 9.790687 1.0 1.507410 1.927656 5.145876 0.464918 P19 6.351602 16.938729 6.465263 0.561021
41 PFC 5.435505 1.1 1.607656 1.879436 3.679454 0.495886 P19 3.564318 22.485169 6.351894 0.463397
222 PFC 7.573136 1.2 2.226880 1.901721 4.059207 0.420569 P19 3.564209 24.691495 5.649924 0.649198
214 PFC 11.893003 1.3 2.620396 1.901738 4.872577 0.410428 P19 3.662485 14.843093 6.387762 0.686910
55 PFC 20.299441 1.4 2.870356 1.935540 4.709224 0.408017 P19 3.675579 16.291049 5.608475 0.596193
87 PFC 30.703872 1.5 2.843414 1.971051 5.103378 0.416890 P19 3.563871 14.825087 6.423536 0.688915
17 PFC 42.036395 1.6 2.812167 1.990789 5.125862 0.420206 P19 3.689719 14.240396 6.132779 0.696006
267 PFC 49.050027 1.7 2.856386 1.980432 5.007206 0.401538 P19 3.722959 14.210213 6.173865 0.672128
... ... ... ... ... ... ... ... ... ... ... ... ...
84 PFC 17.886339 1.3 1.816810 1.959639 5.135601 0.400440 P38 5.987440 15.146264 5.248868 0.646079
200 PFC 21.930573 1.4 1.574721 1.991031 5.137032 0.402486 P38 6.037668 15.844416 4.375771 0.473081
65 PFC 28.807148 1.5 1.602218 1.971919 5.121756 0.423936 P38 6.143395 14.019705 4.170575 0.545983
63 PFC 31.996561 1.6 1.590349 1.995852 5.061299 0.401740 P38 5.972560 14.585255 4.899215 0.675187
74 PFC 36.674449 1.7 1.615202 1.992697 5.136335 0.403114 P38 5.533889 14.267714 4.289583 0.620542
42 PFC 42.482697 1.8 1.590354 1.983401 5.107187 0.406799 P38 6.089904 14.234731 5.631923 0.435187
115 PFC 47.169686 1.9 1.558163 1.942507 5.086058 0.402889 P38 6.466195 14.101459 3.713403 0.683278
52 PFC 53.000068 2.0 1.703750 1.973154 5.129516 0.406982 P38 5.975166 15.397811 4.264393 0.608761
237 PFC 3.936852 1.0 2.350028 1.926013 3.399500 0.633518 P39 5.227042 17.563353 6.094889 0.605581
180 PFC 7.453184 1.1 2.803108 1.902098 3.483148 0.416989 P39 5.872922 25.622602 5.885099 0.549929
235 PFC 9.451660 1.2 2.899631 1.906831 5.179763 0.403399 P39 5.723605 15.205047 6.110571 0.400311
57 PFC 13.200254 1.3 2.765950 1.975980 4.927392 0.408138 P39 3.730626 21.136293 6.174249 0.686382
56 PFC 15.821856 1.4 2.824903 1.967544 5.189862 0.408792 P39 5.797345 15.589973 6.081318 0.443868
118 PFC 19.298885 1.5 2.782107 1.934886 5.112200 0.400997 P39 4.847562 19.467494 6.055892 0.406181
54 PFC 22.620200 1.6 2.861558 1.989419 5.127760 0.408330 P39 3.592963 15.873705 5.453842 0.634689
23 PFC 26.517362 1.7 2.871555 1.904436 5.113527 0.402033 P39 3.768114 17.494978 3.926103 0.632106
153 PFC 29.956045 1.8 2.888278 1.978762 5.171884 0.411925 P39 3.573368 17.604471 5.448462 0.602030
93 PFC 31.697606 1.9 2.885957 1.980558 5.166796 0.400877 P39 4.022599 14.388105 6.354172 0.683938
73 PFC 37.211650 2.0 2.854862 1.968794 5.127641 0.411272 P39 4.724220 15.471321 6.303029 0.690021
152 PFC 8.395159 1.0 1.862721 1.962014 5.122758 0.517506 P41 5.980115 25.535700 5.938006 0.627279
182 PFC 9.579564 1.1 1.606500 1.966841 5.159241 0.429017 P41 6.296320 24.313287 6.179288 0.463735
14 PFC 11.618920 1.2 1.537105 1.932074 5.094955 0.411410 P41 5.319040 16.864231 4.877898 0.670030
187 PFC 12.829399 1.3 1.525119 1.952510 5.148015 0.401795 P41 6.076312 14.738919 6.293053 0.465844
228 PFC 16.274527 1.4 1.568499 1.950808 5.103019 0.407597 P41 4.473032 14.863282 4.581120 0.427863
124 PFC 19.913725 1.5 1.637788 1.909709 4.963022 0.403693 P41 6.451466 17.613294 4.994015 0.610017
167 PFC 21.542575 1.6 1.503876 1.911494 5.067967 0.402177 P41 5.402641 15.736979 4.256627 0.693083
250 PFC 25.751861 1.7 1.825260 1.860877 5.149166 0.412261 P41 6.471406 16.776217 4.684194 0.556823
80 PFC 25.992819 1.8 1.658310 1.987658 5.187435 0.401714 P41 5.445440 20.635796 6.141854 0.564022
0 PFC 29.539168 1.9 1.540190 1.886285 5.164799 0.409452 P41 5.418184 14.146504 6.420257 0.495862
268 PFC 33.119849 2.0 1.501035 1.978195 5.177409 0.423197 P41 4.640678 15.048254 6.192140 0.443983

143 rows × 12 columns


In [8]:
optimal_VC = df_VC.loc[df_VC.groupby(['Subject', 'Max_Demand']).Final_Distance.agg('idxmin')]

In [9]:
df = result = pd.concat([optimal_VC, optimal_PFC])
R_corr = df.groupby(['Cortex', 'Max_Demand'])['R_autc'].apply(lambda x: x.corr(df['R_autp']))
t_corr = df.groupby(['Cortex', 'Max_Demand'])['t_c'].apply(lambda x: x.corr(df['t_p']))
print(R_corr)
plt.figure()
plt.plot(R_corr.index.levels[1], R_corr.ix['PFC'], '.r', label='PFC')
plt.plot(R_corr.index.levels[1], R_corr.ix['VC'], '.b', label='VC')
plt.title('R Correlation')
plt.legend()

plt.figure()
plt.plot(t_corr.index.levels[1], t_corr.ix['PFC'], '.r', label='PFC')
plt.title('Time Correlation')
plt.plot(t_corr.index.levels[1], t_corr.ix['VC'], '.b', label='VC')
plt.legend()


Cortex  Max_Demand
PFC     1.0          -0.212942
        1.1          -0.459880
        1.2          -0.243411
        1.3          -0.269683
        1.4           0.012361
        1.5          -0.249515
        1.6          -0.308083
        1.7          -0.302555
        1.8          -0.206488
        1.9          -0.506757
        2.0          -0.247691
VC      1.0          -0.277530
        1.1          -0.094430
        1.2          -0.216465
        1.3          -0.257322
        1.4          -0.212117
        1.5           0.067069
        1.6          -0.315757
        1.7          -0.319581
        1.8          -0.291640
        1.9          -0.265559
        2.0          -0.258613
Name: R_autc, dtype: float64
Out[9]:
<matplotlib.legend.Legend at 0x7f64cca9e748>

In [10]:
g = sns.FacetGrid(df, col="Cortex", row='Max_Demand', hue='Subject')
g = (g.map(plt.scatter, "R_autp", "R_autc", edgecolor="w")).add_legend()



In [11]:
g = sns.FacetGrid(df, col="Cortex", row='Max_Demand', hue='Subject')
g = (g.map(plt.scatter, "t_p", "t_c", edgecolor="w")).add_legend()


Visualising the results

It makes sense to try and visualise the results as a whole. Knowing the single best fit for each cortex doesn't show us the trend across all subjects or the data as a whole.


In [12]:
g=sns.factorplot(data=overnight_df, x='Max_Demand', y='Final_Distance',
                 hue='Cortex', col='Cortex', kind='box', col_wrap=3)



In [14]:
param_list = ['R_autc', 't_c', 'R_autp', 't_p', 'R_autu', 't_u', 'R_auto', 't_o']
for parameter in param_list:
    plt.figure()
    g = sns.jointplot(parameter, 'Final_Distance', overnight_df)
    g.fig.suptitle(parameter)


<matplotlib.figure.Figure at 0x7f64cc9c6d30>
<matplotlib.figure.Figure at 0x7f64cc28ba20>
<matplotlib.figure.Figure at 0x7f64cc376208>
<matplotlib.figure.Figure at 0x7f64cbc0b208>
<matplotlib.figure.Figure at 0x7f64caf6fac8>
<matplotlib.figure.Figure at 0x7f64cbf1c4e0>
<matplotlib.figure.Figure at 0x7f64cf2a2518>
<matplotlib.figure.Figure at 0x7f64cf0c81d0>

In [12]:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
threedee = plt.figure().gca(projection='3d')
threedee.scatter(overnight_df['R_autp'], overnight_df['R_autc'], overnight_df['Final_Distance'])
threedee.set_xlabel('R_autp')
threedee.set_ylabel('R_autc')
threedee.set_zlabel('Final Distance')
plt.show()



In [ ]: