Skip to content

Commit d1d9fe4

Browse files
authored
Merge pull request #398 from AllenInstitute/analysis
Figure script and statistics
2 parents 15effda + 84810a2 commit d1d9fe4

6 files changed

+370
-32
lines changed

visual_behavior_glm/GLM_analysis_tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def build_weights_df(run_params,results_pivoted, cache_results=False,load_cache=
898898

899899
# Make a metric of omission excitation/inhibition
900900
#weights_df['omission_excited'] = [np.sum(x[0:24]) for x in weights_df['omissions_weights']
901-
weights_df['omissions_excited'] = weights_df_24.apply(lambda x: omission_excitation(x['omissions_weights']),axis=1)
901+
weights_df['omissions_excited'] = weights_df.apply(lambda x: omission_excitation(x['omissions_weights']),axis=1)
902902

903903
# Return weights_df
904904
return weights_df

visual_behavior_glm/GLM_cell_metrics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def evaluate_against_metrics(results_metrics, ymetric='variance_explained_full',
120120
plt.savefig(filepath)
121121
filepath = os.path.join(run_params['figure_dir'], 'variance_explained_vs_'+xmetric+'_'+label+'.svg')
122122
plt.savefig(filepath)
123-
print(filepath)
123+
print('Figure saved to: '+filepath)
124124

125125

126126

visual_behavior_glm/GLM_fit_dev.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ def make_glm(fit, run_params, design, session):
186186

187187
# Make Nested Model plot (rainbow plot)
188188
# A couple versions with more or less detail
189-
schematic_df = gsm.plot_all_dropouts(run_params['version'])
190-
schematic_df = gsm.plot_high_level_dropouts(run_params['version'])
191-
schematic_df = gsm.plot_nice_dropouts(run_params['version'])
189+
schematic_df = gsm.plot_dropouts_final(run_params['version'])
192190

193191
# Make the platform paper schematic examples
194192
oeid = 967008471
@@ -259,7 +257,7 @@ def make_glm(fit, run_params, design, session):
259257
gvt.plot_population_averages_by_area(results_pivoted, run_params, dropouts_to_show=['omissions','omissions_positive','omissions_negative'],extra='_omissions_excitation')
260258
gvt.plot_population_averages_by_depth(results_pivoted, run_params, dropouts_to_show=['omissions','omissions_positive','omissions_negative'],extra='_omissions_excitation',area='VISp')
261259
gvt.plot_population_averages_by_depth(results_pivoted, run_params, dropouts_to_show=['omissions','omissions_positive','omissions_negative'],extra='_omissions_excitation',area='VISl')
262-
gvt.plot_kernel_comparison_by_omission_excitation(weights_df_24, run_params_24)
260+
gvt.plot_kernel_comparison_by_omission_excitation(weights_df, run_params)
263261

264262
def get_analysis_dfs(VERSION):
265263
run_params = glm_params.load_run_json(VERSION)

visual_behavior_glm/GLM_schematic_plots.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def change_breakdown_schematic(run_params):
2525
ax.tick_params(axis='x',labelsize=style['fs2'])
2626
ax.set_yticks([])
2727
plt.tight_layout()
28-
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/change_breakdown_schematic.svg')
28+
filename = '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/change_breakdown_schematic.svg'
29+
print('Figure saved to: '+filename)
30+
plt.savefig(filename)
31+
2932

3033
def omission_breakdown_schematic(run_params):
3134
plt.figure(figsize=(2.5,1.5))
@@ -41,7 +44,9 @@ def omission_breakdown_schematic(run_params):
4144
ax.tick_params(axis='x',labelsize=style['fs2'])
4245
ax.set_yticks([])
4346
plt.tight_layout()
44-
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/omission_breakdown_schematic.svg')
47+
filename = '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/omission_breakdown_schematic.svg'
48+
print('Figure saved to: '+filename)
49+
plt.savefig(filename)
4550

4651
def get_example_style():
4752
style={
@@ -230,9 +235,13 @@ def plot_glm_example_inputs(g,times,style,run_params, ax=None, add_stimulus=True
230235
ax.spines['top'].set_visible(False)
231236
ax.spines['right'].set_visible(False)
232237
#plt.tight_layout()
233-
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_inputs.svg')
238+
234239
if add_stimulus:
235-
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_inputs_add_stimulus.svg')
240+
filename ='/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_inputs_add_stimulus.svg'
241+
else:
242+
filename = '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_inputs.svg'
243+
print('Figure saved to: '+filename)
244+
plt.savefig(filename)
236245

237246
def plot_glm_example_components(g, cell_specimen_id, times, style):
238247
fig = plt.figure(figsize=(8,4))
@@ -339,11 +348,15 @@ def plot_glm_example_trace(g,cell_specimen_id,times,style,include_events=True,ax
339348
ax.set_ylim(-0.035,.9)
340349
ax.set_xlim(times)
341350
#plt.tight_layout()
351+
342352
if model is not None:
343-
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_trace_'+model+'.svg')
353+
filename='/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_trace_'+model+'.svg'
344354
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_trace_'+model+'.png')
345355
else:
346-
plt.savefig('/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_trace.svg')
356+
filename='/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/figures/example_trace.svg'
357+
358+
print('Figure saved to: '+filename)
359+
plt.savefig(filename)
347360
return
348361

349362
def plot_all_dropouts(VERSION):

visual_behavior_glm/GLM_visualization_tools.py

+65-20
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,16 @@ def var_explained_by_experience(results_pivoted, run_params,threshold = 0):
594594
plt.tick_params(axis='both',labelsize=14)
595595
plt.tight_layout()
596596
if threshold !=0:
597-
plt.savefig(run_params['figure_dir']+'/variance_explained_by_experience_filtered.svg')
597+
filename = run_params['figure_dir']+'/variance_explained_by_experience_filtered.svg'
598598
plt.savefig(run_params['figure_dir']+'/variance_explained_by_experience_filtered.png')
599+
print('Figure saved to: ' + filename)
600+
plt.savefig(filename)
599601
else:
600-
plt.savefig(run_params['figure_dir']+'/variance_explained_by_experience.svg')
602+
filename = run_params['figure_dir']+'/variance_explained_by_experience.svg'
601603
plt.savefig(run_params['figure_dir']+'/variance_explained_by_experience.png')
604+
print('Figure saved to: ' + filename)
605+
plt.savefig(filename)
606+
return results_pivoted.groupby(['cell_type','experience_level'])['variance_explained_percent'].describe()
602607

603608
def compare_var_explained_by_version(results=None, fig=None, ax=None, test_data=True, figsize=(9,5), use_violin=True,cre=None,metric='Full',show_equipment=True,zoom_xlim=True,sort_by_signal=True):
604609
'''
@@ -1850,13 +1855,19 @@ def plot_kernel_comparison_by_experience(weights_df, run_params, kernel,threshol
18501855
fig_f.savefig(run_params['fig_kernels_dir']+'/'+kernel+'_familiar_kernel'+extra+'.svg')
18511856
fig_n.savefig(run_params['fig_kernels_dir']+'/'+kernel+'_novel1_kernel'+extra+'.svg')
18521857
fig_np.savefig(run_params['fig_kernels_dir']+'/'+kernel+'_novelp1_kernel'+extra+'.svg')
1858+
print('Figure saved to: '+run_params['fig_kernels_dir']+'/'+kernel+'_familiar_kernel'+extra+'.svg')
1859+
print('Figure saved to: '+run_params['fig_kernels_dir']+'/'+kernel+'_novel1_kernel'+extra+'.svg')
1860+
print('Figure saved to: '+run_params['fig_kernels_dir']+'/'+kernel+'_novelp1_kernel'+extra+'.svg')
18531861

18541862
k, fig_v , ax_v =plot_kernel_comparison(weights_df,run_params,kernel,save_results=False,session_filter=['Familiar','Novel 1','Novel >1'],cell_filter='Vip-IRES-Cre',compare=['experience_level'],threshold=threshold,drop_threshold=drop_threshold)
18551863
k, fig_s , ax_s =plot_kernel_comparison(weights_df,run_params,kernel,save_results=False,session_filter=['Familiar','Novel 1','Novel >1'],cell_filter='Sst-IRES-Cre',compare=['experience_level'],threshold=threshold,drop_threshold=drop_threshold)
18561864
k, fig_e , ax_e =plot_kernel_comparison(weights_df,run_params,kernel,save_results=False,session_filter=['Familiar','Novel 1','Novel >1'],cell_filter='Slc17a7-IRES2-Cre',compare=['experience_level'],threshold=threshold,drop_threshold=drop_threshold)
18571865
fig_v.savefig(run_params['fig_kernels_dir']+'/'+kernel+'_vip_kernel'+extra+'.svg')
18581866
fig_s.savefig(run_params['fig_kernels_dir']+'/'+kernel+'_sst_kernel'+extra+'.svg')
18591867
fig_e.savefig(run_params['fig_kernels_dir']+'/'+kernel+'_exc_kernel'+extra+'.svg')
1868+
print('Figure saved to: '+run_params['fig_kernels_dir']+'/'+kernel+'_vip_kernel'+extra+'.svg')
1869+
print('Figure saved to: '+run_params['fig_kernels_dir']+'/'+kernel+'_sst_kernel'+extra+'.svg')
1870+
print('Figure saved to: '+run_params['fig_kernels_dir']+'/'+kernel+'_exc_kernel'+extra+'.svg')
18601871

18611872
def plot_kernel_comparison_by_omission_excitation(weights_df, run_params):
18621873
plot_kernel_comparison(weights_df,run_params,'omissions',session_filter=['Familiar'],cell_filter='Slc17a7-IRES2-Cre',compare=['omissions_excited'])
@@ -2884,6 +2895,7 @@ def plot_kernel_heatmap_with_dropout(vip_table, sst_table, slc_table, time_vec,k
28842895
#dax3.set_xticklabels(['Coding\n Score'],rotation=-70,fontsize=16)
28852896
filename = os.path.join(run_params['fig_kernels_dir'],kernel+'_heatmap_with_dropout'+extra+'.svg')
28862897
plt.savefig(filename)
2898+
print('Figure saved to: '+filename)
28872899
filename = os.path.join(run_params['fig_kernels_dir'],kernel+'_heatmap_with_dropout'+extra+'.png')
28882900
plt.savefig(filename)
28892901
return zlims
@@ -3497,6 +3509,7 @@ def plot_population_averages_by_depth(results_pivoted, run_params, dropouts_to_s
34973509
experience_levels = np.sort(results_pivoted.experience_level.unique())
34983510
colors = project_colors()
34993511

3512+
summary = {}
35003513
# Iterate cell types and make a plot for each
35013514
for cell_type in cell_types:
35023515
if len(dropouts_to_show) ==3:
@@ -3505,9 +3518,11 @@ def plot_population_averages_by_depth(results_pivoted, run_params, dropouts_to_s
35053518
fig, ax = plt.subplots(1,len(dropouts_to_show),figsize=(10.8,4), sharey=sharey)
35063519
all_data = results_pivoted.query('cell_type ==@cell_type')
35073520
stats = {}
3521+
summary[cell_type + ' data'] = {}
35083522
# Iterate dropouts and plot each by experience
35093523
for index, feature in enumerate(dropouts_to_show):
35103524
stats[feature] = test_significant_dropout_averages_by_depth(all_data,feature)
3525+
summary[cell_type+' data'][feature] = all_data.groupby(['experience_level','coarse_binned_depth'])[feature].describe()
35113526
# Plot all cells in active sessions
35123527
ax[index] = sns.pointplot(
35133528
data = all_data,
@@ -3560,10 +3575,12 @@ def plot_population_averages_by_depth(results_pivoted, run_params, dropouts_to_s
35603575
ax[0].set_ylabel('Coding Score',fontsize=20)
35613576
plt.suptitle(cell_type+', '+area,fontsize=20)
35623577
fig.tight_layout()
3563-
plt.savefig(run_params['figure_dir']+'/dropout_average_by_depth_'+cell_type[0:3]+extra+'.svg')
3578+
filename = run_params['figure_dir']+'/dropout_average_by_depth_'+cell_type[0:3]+extra+'.svg'
35643579
plt.savefig(run_params['figure_dir']+'/dropout_average_by_depth_'+cell_type[0:3]+extra+'.png')
3565-
3566-
3580+
print('Figure saved to: '+filename)
3581+
plt.savefig(filename)
3582+
summary[cell_type + ' stats'] = stats
3583+
return summary
35673584

35683585
def plot_population_averages_by_area(results_pivoted, run_params, dropouts_to_show = ['all-images','omissions','behavioral','task'],sharey=False,include_zero_cells=True,add_stats=True,extra='',equipment="mesoscope"):
35693586
'''
@@ -3609,7 +3626,7 @@ def plot_population_averages_by_area(results_pivoted, run_params, dropouts_to_sh
36093626
cell_types = results_pivoted.cell_type.unique()
36103627
experience_levels = np.sort(results_pivoted.experience_level.unique())
36113628
colors = project_colors()
3612-
3629+
summary = {}
36133630
# Iterate cell types and make a plot for each
36143631
for cell_type in cell_types:
36153632
if len(dropouts_to_show) == 3:
@@ -3618,10 +3635,12 @@ def plot_population_averages_by_area(results_pivoted, run_params, dropouts_to_sh
36183635
fig, ax = plt.subplots(1,len(dropouts_to_show),figsize=(10.8,4), sharey=sharey)
36193636
all_data = results_pivoted.query('cell_type ==@cell_type')
36203637
stats = {}
3638+
summary[cell_type + ' data'] = {}
36213639
# Iterate dropouts and plot each by experience
36223640
for index, feature in enumerate(dropouts_to_show):
36233641
stats[feature] = test_significant_dropout_averages_by_area(all_data,feature)
36243642
# Plot all cells in active sessions
3643+
summary[cell_type+' data'][feature] = all_data.groupby(['experience_level','targeted_structure'])[feature].describe()
36253644
ax[index] = sns.pointplot(
36263645
data = all_data,
36273646
x = 'experience_level',
@@ -3672,9 +3691,12 @@ def plot_population_averages_by_area(results_pivoted, run_params, dropouts_to_sh
36723691
ax[0].set_ylabel('Coding Score',fontsize=20)
36733692
plt.suptitle(cell_type,fontsize=20)
36743693
fig.tight_layout()
3675-
plt.savefig(run_params['figure_dir']+'/dropout_average_by_area_'+cell_type[0:3]+extra+'.svg')
3694+
filename = run_params['figure_dir']+'/dropout_average_by_area_'+cell_type[0:3]+extra+'.svg'
36763695
plt.savefig(run_params['figure_dir']+'/dropout_average_by_area_'+cell_type[0:3]+extra+'.png')
3677-
3696+
plt.savefig(filename)
3697+
print('Figure saved to: '+filename)
3698+
summary[cell_type + ' stats'] = stats
3699+
return summary
36783700

36793701

36803702
def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['all-images','omissions','behavioral','task'],sharey=True,include_zero_cells=True,boxplot=False,add_stats=True,extra='',strict_experience_matching=False,plot_by_cell_type=False):
@@ -3755,7 +3777,9 @@ def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['a
37553777
ax[index].tick_params(axis='y',labelsize=16)
37563778
ax[0].set_ylabel('Coding Score',fontsize=20)
37573779
plt.tight_layout()
3758-
plt.savefig(run_params['figure_dir']+'/dropout_average_combined'+extra+'.svg')
3780+
filename = run_params['figure_dir']+'/dropout_average_combined'+extra+'.svg'
3781+
print('Figure saved to: '+filename)
3782+
plt.savefig(filename)
37593783
#plt.savefig(run_params['figure_dir']+'/dropout_average_combined'+extra+'.png')
37603784

37613785
# Iterate cell types and make a plot for each
@@ -3862,11 +3886,14 @@ def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['a
38623886
ax[index].set_ylim(0,ytop*1.2)
38633887
ax[0].set_ylabel('Coding Score',fontsize=18)
38643888
plt.suptitle(cell_type,fontsize=18)
3865-
fig.tight_layout()
3866-
plt.savefig(run_params['figure_dir']+'/dropout_average_'+cell_type[0:3]+extra+'.svg')
3889+
fig.tight_layout()
3890+
filename = run_params['figure_dir']+'/dropout_average_'+cell_type[0:3]+extra+'.svg'
3891+
plt.savefig(filename)
3892+
print('Figure saved to: '+filename)
38673893

38683894
# Repeat the plots but transposed
38693895
# Iterate cell types and make a plot for each
3896+
summary_data = {}
38703897
for index, feature in enumerate(dropouts_to_show):
38713898
fig, ax = plt.subplots(1,4,figsize=(10.8,4), sharey=sharey)
38723899

@@ -3893,6 +3920,7 @@ def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['a
38933920
ax[3].spines['top'].set_visible(False)
38943921
ax[3].spines['right'].set_visible(False)
38953922

3923+
summary_data[feature + ' data'] = {}
38963924
stats = {}
38973925
# Iterate dropouts and plot each by experience
38983926
for cindex, cell_type in enumerate(cell_types):
@@ -3902,6 +3930,8 @@ def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['a
39023930
strict_matched_data = all_data.query('cell_specimen_id in @strict_matched_cells')
39033931
anova, tukey = test_significant_dropout_averages(all_data,feature)
39043932
stats[cell_type]=(anova, tukey)
3933+
summary_data[feature+' data'][cell_type+' all data'] = all_data.groupby(['experience_level'])[feature].describe()
3934+
summary_data[feature+' data'][cell_type+' matched data'] = matched_data.groupby(['experience_level'])[feature].describe()
39053935
# Plot all cells in active sessions
39063936
if boxplot:
39073937
ax[cindex] = sns.boxplot(
@@ -3945,6 +3975,7 @@ def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['a
39453975

39463976
if strict_experience_matching:
39473977
# Plot cells in matched active sessions
3978+
summary_data[feature+' data'][cell_type+' strict matched data'] = strict_matched_data.groupby(['experience_level'])[feature].describe()
39483979
ax[cindex] = sns.pointplot(
39493980
data = strict_matched_data,
39503981
x = 'experience_level',
@@ -4002,8 +4033,12 @@ def plot_population_averages(results_pivoted, run_params, dropouts_to_show = ['a
40024033
ax[2].set_ylim(bottom=0)
40034034
ax[3].set_ylim(bottom=0)
40044035
fig.tight_layout()
4005-
plt.savefig(run_params['figure_dir']+'/dropout_average_'+clean_feature.replace(' ','_')+extra+'.svg')
4036+
filename = run_params['figure_dir']+'/dropout_average_'+clean_feature.replace(' ','_')+extra+'.svg'
4037+
plt.savefig(filename)
4038+
print('Figure saved to: '+filename)
4039+
summary_data[feature+' stats'] = stats
40064040

4041+
return summary_data
40074042

40084043

40094044
def test_significant_dropout_averages(data,feature):
@@ -4166,13 +4201,16 @@ def plot_dropout_individual_population(results, run_params,ax=None,palette=None,
41664201
if add_title:
41674202
plt.title(run_params['version'])
41684203
if use_violin:
4169-
plt.savefig(run_params['figure_dir']+'/dropout_individual.svg')
4204+
filename = run_params['figure_dir']+'/dropout_individual.svg'
41704205
elif use_single:
4171-
plt.savefig(run_params['figure_dir']+'/dropout_individual_boxplot_single.svg')
4206+
filename = run_params['figure_dir']+'/dropout_individual_boxplot_single.svg'
41724207
else:
4173-
plt.savefig(run_params['figure_dir']+'/dropout_individual_boxplot.svg')
4208+
filename = run_params['figure_dir']+'/dropout_individual_boxplot.svg'
41744209
plt.savefig(run_params['figure_dir']+'/dropout_individual_boxplot.png')
41754210

4211+
plt.savefig(filename)
4212+
print('Figure saved to: '+filename)
4213+
return data_to_plot.groupby(['cre_line','dropout'])['explained_variance'].describe()
41764214

41774215
def plot_dropout_summary_population(results, run_params,dropouts_to_show = ['all-images','omissions','behavioral','task'],ax=None,palette=None,use_violin=False,add_median=True,include_zero_cells=True,add_title=False):
41784216
'''
@@ -4281,8 +4319,11 @@ def plot_dropout_summary_population(results, run_params,dropouts_to_show = ['al
42814319
if use_violin:
42824320
plt.savefig(run_params['figure_dir']+'/dropout_summary.svg')
42834321
else:
4284-
plt.savefig(run_params['figure_dir']+'/dropout_summary_boxplot.svg')
4322+
filename = run_params['figure_dir']+'/dropout_summary_boxplot.svg'
4323+
print('Figure saved to: '+filename)
4324+
plt.savefig(filename)
42854325
plt.savefig(run_params['figure_dir']+'/dropout_summary_boxplot.png')
4326+
return data_to_plot.groupby(['cre_line','dropout'])['explained_variance'].describe()
42864327

42874328
def plot_fraction_summary_population(results_pivoted, run_params,sharey=True,omissions_excitation=False):
42884329
# compute coding fractions
@@ -4352,12 +4393,16 @@ def plot_fraction_summary_population(results_pivoted, run_params,sharey=True,omi
43524393
ax[0].set_ylabel('Fraction of cells \n coding for ',fontsize=20)
43534394
plt.tight_layout()
43544395
if omissions_excitation:
4355-
plt.savefig(run_params['figure_dir']+'/coding_fraction_omissions_summary.svg')
4356-
plt.savefig(run_params['figure_dir']+'/coding_fraction_omissions_summary.png')
4396+
filename = run_params['figure_dir']+'/coding_fraction_omissions_summary.svg'
4397+
plt.savefig(filename)
4398+
plt.savefig(run_params['figure_dir']+'/coding_fraction_omissions_summary.png')
4399+
print('Figure saved to: '+filename)
43574400
else:
4358-
plt.savefig(run_params['figure_dir']+'/coding_fraction_summary.svg')
4401+
filename = run_params['figure_dir']+'/coding_fraction_summary.svg'
4402+
plt.savefig(filename)
43594403
plt.savefig(run_params['figure_dir']+'/coding_fraction_summary.png')
4360-
4404+
print('Figure saved to: '+filename)
4405+
return summary_df
43614406

43624407
def make_cosyne_schematic(glm,cell=1028768972,t_range=5,time_to_plot=3291,alpha=.25):
43634408
'''

0 commit comments

Comments
 (0)