In [1]:
%matplotlib inline


/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
  warnings.warn(self.msg_depr % (key, alt_key))

In [2]:
import pandas as pd
import numpy as np
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

In [3]:
sns.set(style='whitegrid')

In [11]:
firing_and_regions.one.resample('10ms', how='sum').plot()


Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x10b770c10>

In [4]:
def inputs_and_regions_for(energy_name):
    inputData = pd.read_csv('SampleLogs/{}_Input.csv'.format(energy_name), index_col='time', \
                            names= ['time', 'source', 'destination', 'inhibitor'])
    regions = pd.read_csv('SampleLogs/{}_Region.csv'.format(energy_name), index_col='neuron', \
                          names=['neuron', 'region'])
    lowerLimit = pd.to_datetime(5, unit='s')
    inputData.index = pd.to_datetime(inputData.index, unit='ms')
    inputData = inputData[lowerLimit:]
    inputs_and_regions = inputData.join(regions, \
                                        on='source').join(regions, \
                                        on= 'destination', lsuffix='_src', rsuffix='_dst')
    inputs_and_regions['efective_input'] = 1*(1-inputs_and_regions.inhibitor) -1*inputs_and_regions.inhibitor
    answer = inputs_and_regions.groupby([inputs_and_regions.region_src  == inputs_and_regions.region_dst, 'region_src']).\
            efective_input.count().unstack().T
    answer['total'] = answer.sum(axis=1)
    answer = answer.rename(columns={True: 'within_regions', False:'between_regions'})
    return answer

In [5]:
medium = inputs_and_regions_for('MediumConst')

In [6]:
medium.head()


Out[6]:
between_regions within_regions total
region_src
1 144 1496 1640
2 157 696 853
3 100 931 1031
4 24 80 104
5 148 932 1080

In [7]:
low  = inputs_and_regions_for('LowConst')

In [8]:
low.head()


Out[8]:
between_regions within_regions total
region_src
1 233 2049 2282
2 166 2272 2438
3 152 9 161
4 232 1745 1977
5 151 1402 1553

In [9]:
high  = inputs_and_regions_for('HighConst')

In [10]:
(medium-low).hist()


Out[10]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10d89c790>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x106ad2390>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x1078b5250>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10ebcbe10>]], dtype=object)

In [11]:
(high-medium).hist()


Out[11]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10ebe3650>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10ee9e350>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x10ef1f310>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10ef81a50>]], dtype=object)

In [12]:
(high-low).hist()


Out[12]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10f3d30d0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10f467710>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x10f4e76d0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x10f547d10>]], dtype=object)

In [13]:
region_location = pd.read_csv('../static/models/regions.obj', header=None, sep=' ')
region_location.index = range(1, len(region_location)+1)
region_location = region_location.drop(0, axis=1)
region_location.columns = ['x', 'y', 'z']
region_location.head()


Out[13]:
x y z
1 12.957983 -24.991597 -28.873950
2 45.045872 0.825688 -7.412844
3 -46.607595 2.202532 -38.379747
4 45.020408 39.551020 -11.265306
5 29.526882 -2.860215 -40.516129

In [14]:
merged = medium.join(region_location)
merged.head()


Out[14]:
between_regions within_regions total x y z
region_src
1 144 1496 1640 12.957983 -24.991597 -28.873950
2 157 696 853 45.045872 0.825688 -7.412844
3 100 931 1031 -46.607595 2.202532 -38.379747
4 24 80 104 45.020408 39.551020 -11.265306
5 148 932 1080 29.526882 -2.860215 -40.516129

In [15]:
def brain_heatmap(data):
    merged = data.join(region_location)
    fig = plt.figure(figsize=plt.figaspect(1/4.0))
    for idx, column in enumerate(['within_regions', 'between_regions', 'total']):
        ax = fig.add_subplot(1, 3,idx+1, projection='3d')
        ax.set_title(column)
        brain_heatmap_for(merged[column], ax, fig)

In [16]:
def brain_heatmap_for(data, ax, fig):
    cmap = sns.cubehelix_palette(as_cmap=True, start=0.6, light=1, dark=0.1)
    colors = cmap(data/data.max())
    colmap = plt.cm.ScalarMappable(cmap=cmap)
    colmap.set_array(data)
    yg = ax.scatter(merged.x, merged.y, merged.z, c=colors, marker='o')
    cb = fig.colorbar(colmap)

In [72]:
brain_heatmap(high-low)



In [73]:
brain_heatmap(high-medium)



In [74]:
brain_heatmap(medium-low)



In [17]:
region_names = pd.read_csv('1013090_DTI_region_names_full_file.txt')
region_names.head()


Out[17]:
Brain-Stem
0 Right Planum Polare
1 Left Temporal Pole
2 Right Frontal Pole
3 Right Temporal Fusiform anterior
4 Left Frontal Pole

In [18]:
region_names.index = region_names.index+1

In [19]:
high.join(region_names).sort_values('total', ascending=False).head(40)


Out[19]:
between_regions within_regions total Brain-Stem
region_src
170 985 8824 9809 Left Cingulate anterior
69 1134 8528 9662 Right Precentral
21 1225 8420 9645 Right Crus I
84 766 8666 9432 Left Temporal Occipital Fusiform
112 623 8792 9415 Right Temporal Pole
26 953 8433 9386 Right Insular
183 723 8512 9235 Right Precuneous
92 903 8128 9031 Right Lingual
181 1154 7857 9011 Right Cingulate posterior
22 1159 7849 9008 Left Postcentral
121 1441 7521 8962 Left I-IV
108 486 8465 8951 Left Lingual
10 787 8026 8813 Left Temporal Fusiform anterior
1 693 8084 8777 Right Planum Polare
83 975 7639 8614 Left Middle Temporal temporooccipital
19 1009 7602 8611 Left Thalamus
70 687 7895 8582 Brain-Stem
128 1039 7523 8562 Left Frontal Pole
42 881 7670 8551 Right Postcentral
164 1114 7399 8513 Left Frontal Pole
14 1148 7354 8502 Left Supramarginal anterior
137 396 8078 8474 Right Lingual
100 950 7447 8397 Left Subcallosal
178 1053 7273 8326 Right Superior Temporal posterior
46 849 7474 8323 Left Middle Frontal
27 643 7662 8305 Left Insular
160 951 7353 8304 Left Lateral Occipital superior
167 976 7293 8269 Right Temporal Fusiform posterior
93 984 7278 8262 Left Temporal Fusiform posterior
186 526 7720 8246 Right Temporal Pole
68 1113 7101 8214 Right Temporal Fusiform posterior
105 821 7370 8191 Right Frontal Pole
15 697 7491 8188 Right Occipital Pole
148 1088 7047 8135 Right Frontal Pole
80 1022 7079 8101 Left Frontal Pole
44 822 7211 8033 Right Insular
82 671 7358 8029 Right Superior Parietal Lobule
114 803 7221 8024 Left Precentral
146 600 7374 7974 Left Occipital Pole
182 1025 6916 7941 Left Temporal Occipital Fusiform

In [109]:
high.join(region_names).sort_values('total', ascending=True).head(40)


Out[109]:
between_regions within_regions total Brain-Stem
region_src
89 NaN 23 23 Right Precentral
142 89 NaN 89 Left Inferior Temporal posterior
62 162 NaN 162 Left Frontal Pole
109 139 112 251 Right Postcentral
9 277 12 289 Left Middle Temporal posterior
34 306 NaN 306 Right Crus II
52 325 59 384 Left Middle Frontal
138 390 37 427 Right Superior Frontal
36 430 75 505 Right Precuneous
90 529 24 553 Right Cingulate anterior
18 527 35 562 Left Caudate
97 533 45 578 Left Precentral
177 535 49 584 Left Planum Temporale
134 420 1683 2103 Left Precentral
40 618 1629 2247 Left Angular
6 303 2637 2940 Left Precuneous
16 685 2427 3112 Left Paracingulate
136 544 2633 3177 Right Lateral Occipital superior
151 468 3185 3653 Right Frontal Pole
110 511 3403 3914 Left Parietal Operculum
101 715 3290 4005 Left Crus I
113 466 3917 4383 Left Occipital Pole
30 779 3606 4385 Left Middle Temporal posterior
185 441 4209 4650 Right VI
126 767 3886 4653 Left Precuneous
103 496 4218 4714 Left Postcentral
55 558 4305 4863 Left Frontal Pole
187 472 4414 4886 Left Lateral Occipital superior Brain-Stem
48 486 4423 4909 Right Crus II
51 419 4627 5046 Left Superior Parietal Lobule
60 409 4723 5132 Right Postcentral
4 594 4588 5182 Right Temporal Fusiform anterior
188 536 4822 5358 NaN
33 338 5023 5361 Right Putamen
116 490 4917 5407 Left Frontal Pole
81 828 4589 5417 Right Thalamus
64 925 4522 5447 Left Middle Frontal
12 820 4744 5564 Right Caudate
130 498 5067 5565 Right Angular
104 646 4926 5572 Right Postcentral

In [106]:
high.join(region_names).sort_values('between_regions', ascending=False).head(20)


Out[106]:
between_regions within_regions total Brain-Stem
region_src
121 1441 7521 8962 Left I-IV
127 1375 4950 6325 Left Middle Temporal anterior
91 1245 5201 6446 Left Frontal Pole
23 1229 6696 7925 Left Lateral Occipital superior
21 1225 8420 9645 Right Crus I
31 1197 5439 6636 Right Lateral Occipital superior
2 1178 5950 7128 Left Temporal Pole
157 1165 4906 6071 Brain-Stem
22 1159 7849 9008 Left Postcentral
181 1154 7857 9011 Right Cingulate posterior
14 1148 7354 8502 Left Supramarginal anterior
69 1134 8528 9662 Right Precentral
158 1128 6660 7788 Right Paracingulate
164 1114 7399 8513 Left Frontal Pole
68 1113 7101 8214 Right Temporal Fusiform posterior
148 1088 7047 8135 Right Frontal Pole
178 1053 7273 8326 Right Superior Temporal posterior
128 1039 7523 8562 Left Frontal Pole
182 1025 6916 7941 Left Temporal Occipital Fusiform
180 1024 6151 7175 Left Lateral Occipital superior

In [107]:
(high-low).join(region_names).sort_values('between_regions', ascending=False).head(20)


Out[107]:
between_regions within_regions total Brain-Stem
region_src
121 1248 5842 7090 Left I-IV
127 1241 3436 4677 Left Middle Temporal anterior
23 1152 5188 6340 Left Lateral Occipital superior
181 1148 NaN 9005 Right Cingulate posterior
2 1012 3678 4690 Left Temporal Pole
157 1011 4897 5908 Brain-Stem
31 1005 4251 5256 Right Lateral Occipital superior
21 986 6992 7978 Right Crus I
93 980 7172 8152 Left Temporal Fusiform posterior
148 979 5319 6298 Right Frontal Pole
91 969 4131 5100 Left Frontal Pole
182 959 6911 7870 Left Temporal Occipital Fusiform
14 955 5868 6823 Left Supramarginal anterior
22 947 5839 6786 Left Postcentral
80 935 NaN 8014 Left Frontal Pole
149 906 6317 7223 Right Parietal Operculum
69 893 6977 7870 Right Precentral
164 893 6117 7010 Left Frontal Pole
68 881 5507 6388 Right Temporal Fusiform posterior
57 878 4591 5469 Right Inferior Temporal posterior

In [108]:
(high-low).join(region_names).sort_values('between_regions', ascending=False).tail(30)


Out[108]:
between_regions within_regions total Brain-Stem
region_src
71 286 3654 3940 Right Paracingulate
169 276 3714 3990 Left Frontal Pole
33 262 NaN 5285 Right Putamen
116 260 2234 2494 Left Frontal Pole
51 249 3012 3261 Left Superior Parietal Lobule
74 243 3143 3386 Right Central Opercular
141 241 4007 4248 Left Frontal Orbital
35 235 2964 3199 Left Paracingulate
52 217 -1646 -1429 Left Middle Frontal
137 215 6540 6755 Right Lingual
36 212 -1838 -1626 Right Precuneous
5 212 5313 5525 Left Frontal Pole
48 209 2469 2678 Right Crus II
138 205 -1275 -1070 Right Superior Frontal
61 180 4474 4654 Right Superior Temporal posterior
9 176 -985 -809 Left Middle Temporal posterior
135 174 3212 3386 Right Supramarginal anterior
60 164 2975 3139 Right Postcentral
88 157 4927 5084 Left Insular
47 156 3890 4046 Right Lateral Occipital superior
134 152 461 613 Left Precentral
34 128 NaN -1218 Right Crus II
6 106 1941 2047 Left Precuneous
103 105 1808 1913 Left Postcentral
62 81 NaN 61 Left Frontal Pole
142 24 NaN 20 Left Inferior Temporal posterior
109 -76 -2334 -2410 Right Postcentral
39 NaN 6211 6834 Brain-Stem
89 NaN -2229 -2481 Right Precentral
180 NaN NaN NaN Left Lateral Occipital superior

In [ ]: