forked from KamitaniLab/GenericObjectDecoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createfigure.py
164 lines (118 loc) · 5.79 KB
/
createfigure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'''Create figures for results of classification_pairwise'''
from __future__ import print_function
import os
import pickle
import numpy as np
import pandas as pd
import scipy.stats as st
import matplotlib.pyplot as plt
import bdpy.fig as bfig
import god_config as config
# Main #################################################################
def main():
results_file = config.results_file
output_file_featpred = os.path.join('results', config.analysis_name + '_featureprediction.pdf')
output_file_catident = os.path.join('results', config.analysis_name + '_categoryidentification.pdf')
roi_label = config.roi_labels
# Load results -----------------------------------------------------
with open(results_file, 'rb') as f:
print('Loading %s' % results_file)
results = pickle.load(f)
# Figure settings
plt.rcParams['font.size'] = 8
# Plot (feature prediction) ----------------------------------------
fig = plt.figure(figsize=[2 * 11.69, 2 * 8.27], dpi=100)
subplotpos_image = [43, 37, 31, 25, 19, 13, 7, 1, 44, 38, 32, 26, 20]
subplotpos_catpt = [45, 39, 33, 27, 21, 15, 9, 3, 46, 40, 34, 28, 22]
subplotpos_catim = [47, 41, 35, 29, 23, 17, 11, 5, 48, 42, 36, 30, 24]
# Image
plotresults(fig, results, value_key='mean_profile_correlation_image',
roi_label=roi_label, subplot_index=subplotpos_image,
caption='image_seen; ', ylabel='Corr. coef.', ylim=[-0.2, 0.6], ytick=[-0.2, 0, 0.2, 0.4])
# Category, seen
plotresults(fig, results, value_key='mean_profile_correlation_cat_percept',
roi_label=roi_label, subplot_index=subplotpos_catpt,
caption='category_seen; ', ylabel='Corr. coef.', ylim=[-0.2, 0.6], ytick=[-0.2, 0, 0.2, 0.4])
# Category, imagined
plotresults(fig, results, value_key='mean_profile_correlation_cat_imagery',
roi_label=roi_label, subplot_index=subplotpos_catim,
barcolor=[0.8, 0.8, 0.8],
caption='category_imagined; ', ylabel='Corr. coef.', ylim=[-0.2, 0.6], ytick=[-0.2, 0, 0.2, 0.4])
# Draw path to the script
fpath = os.path.abspath(__file__)
bfig.draw_footnote(fig, fpath)
# Save the figure
plt.savefig(output_file_featpred)
print('Saved %s' % output_file_featpred)
plt.show()
# Plot (category identification) -----------------------------------
fig = plt.figure(figsize=[2 * 11.69, 2 * 8.27], dpi=100)
subplotpos_percept = [44, 38, 32, 26, 20, 14, 8, 2, 45, 39, 33, 27, 21]
subplotpos_imagery = [46, 40, 34, 28, 22, 16, 10, 4, 47, 41, 35, 29, 23]
# Image
plotresults(fig, results, value_key='catident_correct_rate_percept',
roi_label=roi_label, subplot_index=subplotpos_percept,
caption='seen; ', ylabel='Accuracy', ylim=[0.4, 1.0], ytick=[0.4, 0.6, 0.8, 1.0], textpos=[0, 0.92])
# Category, seen
plotresults(fig, results, value_key='catident_correct_rate_imagery',
roi_label=roi_label, subplot_index=subplotpos_imagery,
barcolor=[0.8, 0.8, 0.8],
caption='imagined; ', ylabel='Accuracy', ylim=[0.4, 1.0], ytick=[0.4, 0.6, 0.8, 1.0], textpos=[0, 0.92])
# Draw path to the script
fpath = os.path.abspath(__file__)
bfig.draw_footnote(fig, fpath)
# Save the figure
plt.savefig(output_file_catident)
print('Saved %s' % output_file_catident)
plt.show()
# Functions ############################################################
def plotresults(fig, results, value_key='', roi_label=[], feature_label=[],
subplot_index=[], caption='', barcolor=[0.4, 0.4, 0.4],
ylabel='', ylim=[-1, 1], ytick=[], textpos=[0, -0.12]):
'''Draw results of feature prediction'''
# Get mean and confidence interval ---------------------------------
tb_mean = pd.pivot_table(results, index=['roi'], columns=['feature'],
values=[value_key], aggfunc=np.mean)
tb_sem = pd.pivot_table(results, index=['roi'], columns=['feature'],
values=[value_key], aggfunc=st.sem)
tb_num = pd.pivot_table(results, index=['roi'], columns=['feature'],
values=[value_key], aggfunc=len)
tb_mean = tb_mean.reindex(index=roi_label)
tb_sem = tb_sem.reindex(index=roi_label)
tb_num = tb_num.reindex(index=roi_label)
ci = st.t.interval(1 - 0.05, tb_num, loc=tb_mean, scale=tb_sem)
tb_yerr = (tb_mean - ci[0], ci[1] - tb_mean)
# Plot -------------------------------------------------------------
xpos = range(len(roi_label))
for feat, si in zip(tb_mean, subplot_index):
plt.subplot(8, 6, si)
# Draw results
y = tb_mean[feat]
yerr = (tb_yerr[0][feat], tb_yerr[1][feat])
plt.bar(xpos, y, align='center',
color=barcolor, edgecolor=barcolor,
linewidth=2,
yerr=yerr, ecolor='k', capsize=0)
# Draw 'feature' name
feat_name = caption + feat[1]
plt.text(textpos[0], textpos[1], feat_name, fontsize=12)
# Modify x and y axes
plt.xticks(xpos, roi_label)
plt.xlim([-0.5, len(xpos) - 0.5])
plt.ylabel(ylabel)
plt.yticks(ytick)
plt.ylim(ylim)
bfig.box_off(plt.gca())
# Remove ticks on x and y axes
plt.gca().xaxis.set_ticks_position('none')
plt.gca().yaxis.set_ticks_position('none')
# Horizontal grid lines
plt.gca().yaxis.grid(True, linestyle='-', linewidth=1, color='gray')
plt.gca().set_axisbelow(True)
# Adjust subplots
plt.subplots_adjust(wspace=0.4, hspace=0.2)
# Run as a scirpt ######################################################
if __name__ == '__main__':
# To avoid any use of global variables,
# do nothing except calling main() here
main()