Skip to content

Commit

Permalink
Merge pull request #584 from AllenInstitute/analysis
Browse files Browse the repository at this point in the history
Cleaning up
  • Loading branch information
alexpiet authored Feb 22, 2023
2 parents b185227 + 2dcdbe3 commit a513ded
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 191 deletions.
158 changes: 0 additions & 158 deletions visual_behavior_glm/GLM_perturbation_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,164 +524,6 @@ def get_average_kernels_inner(df):
return df_norm.mean(axis=0),df_norm.std(axis=0)/np.sqrt(np.shape(df_norm)[0])


def plot_PSTH_perturbation_diff(dfs,labels,condition,min_time=-.75,
run_params=None,savefig=False,experience_level="Familiar",
x='Exc',y='y'):
traces = get_PSTH_2D_traces(dfs,labels,condition)
traces['diff_Exc'] = traces['visual_Exc'] - traces['timing_Exc']
traces['diff_Sst'] = traces['visual_Sst'] - traces['timing_Sst']
traces['diff_Vip'] = traces['visual_Vip'] - traces['timing_Vip']
traces['diff_y'] = traces['visual_y'] - traces['timing_y']

height = 4
width=5.25
pre_horz_offset = 2
post_horz_offset = .5
vertical_offset = .75
fig = plt.figure(figsize=(width,height))
h = [Size.Fixed(pre_horz_offset),\
Size.Fixed((width-pre_horz_offset-post_horz_offset))]
v = [Size.Fixed(vertical_offset),
Size.Fixed(height-vertical_offset-.5)]
divider = Divider(fig, (0,0,1,1),h,v,aspect=False)
ax = fig.add_axes(divider.get_position(),\
axes_locator=divider.new_locator(nx=1,ny=1))

ts = np.where(traces['time'] >=min_time)[0]
ax.plot(traces['diff_'+x][ts],traces['diff_'+y][ts],
color='k',lw=3)

p1 = np.where(traces['time'] == 0)[0]
p2 = np.argmin(np.abs(traces['time'] - .75))
p3 = np.where(traces['time'] == 1.5)[0]
if condition == 'omission':
ax.plot(traces['diff_'+x][p1], traces['diff_'+y][p1],
'co',zorder=10,markersize=40)
elif condition == 'hit':
ax.plot(traces['diff_'+x][p1], traces['diff_'+y][p1],
'ro',zorder=10)
elif condition == 'miss':
ax.plot(traces['diff_'+x][p1], traces['diff_'+y][p1],
'rx',zorder=10)
if condition in ['omission','hit','miss']:
ax.plot(traces['diff_'+x][p2], traces['diff_'+y][p2],
'ko',zorder=10,markersize=30)
ax.plot(traces['diff_'+x][p3], traces['diff_'+y][p3],
'o',color='gray',zorder=10,markersize=10)
if condition =='image':
ax.plot(traces['diff_'+x][p1], traces['diff_'+y][p1],
'ko',zorder=10)
#ax.plot(traces['diff_'+x][p2], traces['diff_'+y][p2],
#'ko',zorder=10)
#ax.plot(traces['diff_'+x][p3], traces['diff_'+y][p3],
#'ko',zorder=10)


if y == 'y':
ax.set_ylabel('Vip - Sst',fontsize=16)
else:
ax.set_ylabel(y,fontsize=16)
ax.set_xlabel(x,fontsize=16)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_tick_params(labelsize=12)
ax.yaxis.set_tick_params(labelsize=12)
#if (x=='Exc'):
# if condition in ['hit','miss']:
# ax.set_xlim(0,.008)
# if condition in ['image','omission']:
# ax.set_xlim( 0,0.004)
#if y=='y':
# ax.set_ylim(-0.03,0.045)
ax.axhline(0,color='k',linestyle='--',alpha=.25)
ax.axvline(0,color='k',linestyle='--',alpha=.25)
ax.set_title(condition,fontsize=16)
if savefig:
filepath = run_params['figure_dir']+\
'/strategy/'+condition+'_strategy_perturbation_PSTH_2D_diff_{}_{}_{}.svg'.format(experience_level,x,y)
print('Figure saved to: '+filepath)
plt.savefig(filepath)
return ax

def plot_PSTH_2D_diff(dfs,labels, condition, run_params,
experience_level="Familiar",savefig=True,times=[-.75,2]):

traces = get_PSTH_2D_traces(dfs,labels,condition,
experience_level=experience_level)
traces['diff_Exc'] = traces['visual_Exc'] - traces['timing_Exc']
traces['diff_Sst'] = traces['visual_Sst'] - traces['timing_Sst']
traces['diff_Vip'] = traces['visual_Vip'] - traces['timing_Vip']
traces['diff_y'] = traces['visual_y'] - traces['timing_y']
x = np.vstack([traces['diff_Exc'],traces['diff_Sst'],traces['diff_Vip']])
vec = [1,-1,1]
vec = vec/np.linalg.norm(vec)
#traces['diff_magnitude'] = np.linalg.norm(x,axis=0)
traces['diff_magnitude'] = vec @ x

stamps = np.where((traces['time']>= times[0])&\
(traces['time'] <=times[-1]))[0]
time = traces['time'][stamps]

height = 4
width=8
pre_horz_offset = 1.5
post_horz_offset = 2.5
vertical_offset = .75
fig = plt.figure(figsize=(width,height))
duration = time[-1] -time[0]
h = [Size.Fixed(pre_horz_offset),\
Size.Fixed((width-pre_horz_offset-post_horz_offset)\
/3*duration)]
v = [Size.Fixed(vertical_offset),
Size.Fixed(height-vertical_offset-.5)]
divider = Divider(fig, (0,0,1,1),h,v,aspect=False)
ax = fig.add_axes(divider.get_position(),\
axes_locator=divider.new_locator(nx=1,ny=1))

colors = gvt.project_colors()
ax.plot(time,traces['diff_magnitude'][stamps],
color='k',lw=4)

omitted = 'omission' in condition
change = (not omitted) and (('change' in condition) or \
('hit' in condition) or ('miss' in condition))
psth.plot_flashes_on_trace(ax, time,
change=change, omitted=omitted)
ax.set_xlabel('time from {} (s)'.format(condition),fontsize=16)
ax.set_ylabel(condition+' response\n(Ca$^{2+}$ events)',
fontsize=16)
ax.set_ylim(-.004,.0225)
ax.set_xlim(time[0],time[-1])
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_tick_params(labelsize=12)
ax.yaxis.set_tick_params(labelsize=12)
#ax.set_title(strategy+' strategy',fontsize=16)


if condition =='omission':
ax.plot(0,0,'co',zorder=10,clip_on=False)
elif condition =='hit':
ax.plot(0,0,'ro',zorder=10,clip_on=False)
elif condition == 'miss':
ax.plot(0,0,'rx',zorder=10,clip_on=False)
else:
ax.plot(0,0,'ko',zorder=10,clip_on=False)
if condition in ['omission','hit','miss']:
ax.plot(.75,0,'ko',zorder=10,clip_on=False)
ax.plot(1.5,0,'o',color='gray',zorder=10,clip_on=False)

if savefig:
filepath = run_params['figure_dir']+\
'/strategy/'+condition+'_strategy_perturbation_PSTH_diff_{}_{}.svg'.format(strategy, experience_level)
print('Figure saved to: '+filepath)
plt.savefig(filepath)
return ax





def plot_PSTH_perturbation(dfs,labels,condition,
run_params=None,savefig=False,experience_level="Familiar",
x='Exc',y='Vip',min_time=-.75):
Expand Down
15 changes: 15 additions & 0 deletions visual_behavior_glm/build_dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
BEHAVIOR_VERSION = 21

def add_area_depth(df,experiment_table):
'''
Adds targeted_structure, and layer columns from experiment table
index on oeid
'''
df = pd.merge(df,
experiment_table.reset_index()[[\
'ophys_experiment_id',
Expand All @@ -21,6 +25,11 @@ def add_area_depth(df,experiment_table):

def load_population_df(data,df_type,cre,summary_df=None,first=False,second=False,
image=False,experience_level='Familiar'):
'''
Loads a summary dataframe
data should be 'events', 'filtered_events', or 'dff'
df_type should be 'full_df', or 'image_df'
'''
if first:
extra = '_first_half'
elif second:
Expand All @@ -35,10 +44,12 @@ def load_population_df(data,df_type,cre,summary_df=None,first=False,second=False
elif experience_level == "Novel >1":
extra +='_novelp'

# load summary file
path ='/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/ophys_glm/'\
+df_type+'s/'+data+'/summary_'+cre+extra+'.feather'
df = pd.read_feather(path)

# Add columsn from summary_df
if (df_type =='image_df') and (summary_df is not None):
cols = ['behavior_session_id','visual_strategy_session',
'experience_level']
Expand All @@ -50,6 +61,10 @@ def load_population_df(data,df_type,cre,summary_df=None,first=False,second=False
def build_population_df(summary_df,df_type='image_df',cre='Vip-IRES-Cre',
data='filtered_events',savefile=True,first=False,second=False,image=False,
experience_level='Familiar'):
'''
Generates the summary data files by aggregating over ophys experiment
'''


batch_size=50
batch=False
Expand Down
File renamed without changes.
File renamed without changes.
37 changes: 4 additions & 33 deletions visual_behavior_glm/strategy_paper_figure_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,6 @@
psth.plot_summary_bootstrap_omission_strategy(vip_omission,'vip',first=False,
second=False)

# Post-omission EXC
exc_post_omission = psth.load_image_df(summary_df,'Slc17a7-IRES2-Cre',first=True,
second=False)
exc_post_omission = exc_post_omission.query('post_omitted_1')
psth.plot_summary_bootstrap_omission_strategy(exc_post_omission,'exc',first=True,
second=False,post=True)

# Post-omission Sst
sst_post_omission = psth.load_image_df(summary_df,'Sst-IRES-Cre',first=True,second=False)
sst_post_omission = sst_post_omission.query('post_omitted_1')
psth.plot_summary_bootstrap_omission_strategy(sst_post_omission,'sst',first=True,
second=False,post=True)

# Determine significance for Exc hit/miss
exc_change = psth.load_change_df(summary_df, cre='Slc17a7-IRES2-Cre',data='events',
first=False, second=False, image=True)
Expand All @@ -74,35 +61,19 @@
first=False, second=True)
psth.plot_summary_bootstrap_strategy_pre_change(vip_image,'vip',first=False, second=True)


## Fig. 4E - Running VIP control Omission
################################################################################

vip_omission = psth.load_omission_df(summary_df, cre='Vip-IRES-Cre',data='events')
bootstraps_omission = psth.get_running_bootstraps('vip','omission','events',10000)
psth.running_responses(vip_omission, 'omission',bootstraps=bootstraps_omission)

## Fig. 4F - Running VIP control Omission
################################################################################

vip_image = psth.load_image_df(summary_df, cre='Vip-IRES-Cre',data='events')
bootstraps_image = psth.get_running_bootstraps('vip','image','events',10000)
psth.running_responses(vip_image, 'image',bootstraps=bootstraps_image)


## Hierarchy Supplement
## Fig. 4G - Running VIP control Omission
################################################################################

psth.get_and_plot('vip','omission','events','binned_depth',
nboots, splits=['visual_strategy_session'])
psth.get_and_plot('sst','omission','events','binned_depth',
nboots, splits=['visual_strategy_session'],second=True)
psth.get_and_plot('sst','change','events','binned_depth',
nboots, splits=['visual_strategy_session'],extra='hit',second=True)
psth.get_and_plot('exc','change','events','binned_depth',
nboots, splits=['hit'],extra='visual',first=True)
psth.get_and_plot('exc','change','events','binned_depth',
nboots, splits=['hit'],extra='timing',first=True)
vip_omission = psth.load_omission_df(summary_df, cre='Vip-IRES-Cre',data='events')
bootstraps_omission = psth.get_running_bootstraps('vip','omission','events',10000)
psth.running_responses(vip_omission, 'omission',bootstraps=bootstraps_omission)

## Running Supplement
################################################################################
Expand Down

0 comments on commit a513ded

Please sign in to comment.