def plot_data(path, smoothstr, expmean, ctrlmean, expstd, ctrlstd, expn, ctrln, df2): fig2 = plt.figure("Speed Multiplot %s" % smoothstr) ax = fig2.add_subplot(1,1,1) t0=10 tb = dict(where=ctrlmean['laser_state'].values>0, xaxis=ctrlmean['align'].values-t0, ) flymad_plot.plot_timeseries_with_activation(ax, exp=dict(xaxis=expmean['align'].values-t0, value=expmean['v'].values, std=expstd['v'].values, n=expn['v'].values, label='OK371>ShibireTS', ontop=True), ctrl=dict(xaxis=ctrlmean['align'].values-t0, value=ctrlmean['v'].values, std=ctrlstd['v'].values, n=ctrln['v'].values, label='Control'), sem=True, targetbetween=tb, downsample=25 ) ax.set_xlabel('Time (s)') ax.set_ylabel('Speed (%s/s)' % arena.unit) ax.set_xlim([-t0, 40]) ax.set_yticks([0,20,40]) ax.set_xticks(range(0,41,10)) ax.set_title("Speed %s" % smoothstr) plt.savefig(flymad_plot.get_plotpath(path,"speed_plot_%s.png" % smoothstr), bbox_inches='tight') plt.savefig(flymad_plot.get_plotpath(path,"speed_plot_%s.svg" % smoothstr), bbox_inches='tight')
def plot_data(path, data, arena, note): #customize plot colors here. key is genotype name LABELS = {} COLORS = {} ORDERS = {} fig2 = plt.figure("Speed") ax = fig2.add_subplot(1,1,1) datasets = {} for gt in data: gtdf = data[gt] try: datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['v'].values, std=gtdf['std']['v'].values, n=gtdf['n']['v'].values, label=LABELS.get(gt,gt), color=COLORS.get(gt), order=ORDERS.get(gt), df=gtdf['df'], N=len(gtdf['df']['obj_id'].unique())) except KeyError: print "MISSING DATA FOR",gt #assume laser on protocol was identical in all trials ctrlfirst = data[data.keys()[0]]['first'] result_d = flymad_plot.plot_timeseries_with_activation(ax, targetbetween=dict(xaxis=ctrlfirst['t'].values, where=ctrlfirst['laser_state'].values>0), downsample=5, sem=True, note="OK371shits\n%s\n" % note, individual={k:{'groupby':'obj_id','xaxis':'t','yaxis':'v'} for k in datasets}, individual_title='Speed Individual Traces', return_dict=True, **datasets ) figs = result_d['figs'] ax.set_xlabel('Time (s)') ax.set_ylabel('Speed (%s/s)' % arena.unit) # ax.set_xlim([-15, 30]) # ax.set_ylim([0, 18]) # flymad_plot.retick_relabel_axis(ax, [-15, 0, 15, 30], [0, 5, 10, 15]) fig2.savefig(flymad_plot.get_plotpath(path,"speed_plot.png"), bbox_inches='tight') fig2.savefig(flymad_plot.get_plotpath(path,"speed_plot.svg"), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath(path,"speed_plot_individual_%s.png" % efigname), bbox_inches='tight')
def plot_data(path, smoothstr, expmean, ctrlmean, expstd, ctrlstd, expn, ctrln, df2): fig2 = plt.figure("Speed Multiplot %s" % smoothstr) ax = fig2.add_subplot(1, 1, 1) t0 = 10 tb = dict( where=ctrlmean['laser_state'].values > 0, xaxis=ctrlmean['align'].values - t0, ) flymad_plot.plot_timeseries_with_activation( ax, exp=dict(xaxis=expmean['align'].values - t0, value=expmean['v'].values, std=expstd['v'].values, n=expn['v'].values, label='OK371>ShibireTS', ontop=True), ctrl=dict(xaxis=ctrlmean['align'].values - t0, value=ctrlmean['v'].values, std=ctrlstd['v'].values, n=ctrln['v'].values, label='Control'), sem=True, targetbetween=tb, downsample=25) ax.set_xlabel('Time (s)') ax.set_ylabel('Speed (%s/s)' % arena.unit) ax.set_xlim([-t0, 40]) ax.set_yticks([0, 20, 40]) ax.set_xticks(range(0, 41, 10)) ax.set_title("Speed %s" % smoothstr) plt.savefig(flymad_plot.get_plotpath(path, "speed_plot_%s.png" % smoothstr), bbox_inches='tight') plt.savefig(flymad_plot.get_plotpath(path, "speed_plot_%s.svg" % smoothstr), bbox_inches='tight')
def plot_all_data(path, data, arena, note, laser='350iru'): ORDER = ["50660trp","50660","wtrp"] COLORS = {"50660trp":flymad_plot.RED,"50660":flymad_plot.BLACK,"wtrp":flymad_plot.BLUE} datasets = {} figure_title = "Moonwalker" fig = plt.figure(figure_title, figsize=TS_FIGSIZE) ax = fig.add_subplot(1,1,1) for gt in data: gtdf = data[gt][laser][gt] datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['Vfwd'].values, std=gtdf['std']['Vfwd'].values, n=gtdf['n']['Vfwd'].values, order=ORDER.index(gt), label=flymad_analysis.human_label(gt), color=COLORS[gt], N=len(gtdf['df']['obj_id'].unique()), df=gtdf['df'], ) l, axs, figs = flymad_plot.plot_timeseries_with_activation(ax, targetbetween=dict(xaxis=data['50660trp'][laser]['50660trp']['first']['t'].values, where=data['50660trp'][laser]['50660trp']['first']['laser_state'].values>0), downsample=5, sem=True, note="%s\n" % (note,), individual={k:{'groupby':('obj_id','trial'),'xaxis':'t','yaxis':'Vfwd'} for k in datasets}, individual_title=figure_title + ' Individual Traces', **datasets ) ax.axhline(color='k', linestyle='--',alpha=0.8) ax.set_ylim(YLIM) ax.set_xlabel('Time (s)') ax.set_ylabel('Forward velocity (%s/s)' % arena.unit) ax.set_xlim(XLIM) flymad_plot.retick_relabel_axis(ax, XTICKS, YTICKS) fig.savefig(flymad_plot.get_plotpath(path,"moonwalker_simple.png"), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path,"moonwalker_simple.svg"), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath(path,"moonwalker_simple_individual_%s.png" % efigname), bbox_inches='tight')
def plot_data(arena, dirname, smooth, dfs): conditions = dfs.keys() n_conditions = len(conditions) DO_LATENCY_FIGURE = False DO_POSITION_FIGURE = True DO_VELOCITY_FIGURE = True COLORS = {'th1stim_thorax':'r', 'th1stim_head':'b', 'thcstrpa11stim_thorax':'g', 'thgal41stim_head':'k', 'thcstrpa11stim_head':'k', 'thgal41stim_thorax':'g', } if DO_VELOCITY_FIGURE: fig = plt.figure() ax = fig.add_subplot(111) datasets = {} for i, condition in enumerate(conditions): all_dfs = [] for (df,parsed_data,laser_on) in dfs[condition]: df = df.resample('10L') # 10 msec assert len(df) > 100 df['align'] = np.arange(len(df)) all_dfs.append(df) all_dfs = pd.concat([d for d in all_dfs]) aligned = all_dfs.groupby('align', as_index=False) mean_timeseries = aligned.mean().astype(float) datasets[condition] = dict(xaxis=mean_timeseries['align'].values, value=mean_timeseries['v'].values, std=aligned.std().astype(float)['v'].values, color=COLORS[condition], ) from flymad.flymad_plot import plot_timeseries_with_activation plot_timeseries_with_activation(ax, #targetbetween=[headtargetbetween,thoraxtargetbetween], sem=False, **datasets ) if DO_POSITION_FIGURE: fig = plt.figure() max_trials = 0 for i, condition in enumerate(conditions): n_trials = len(dfs[condition]) max_trials = max( max_trials, n_trials ) ax = None for i, condition in enumerate(conditions): for j in range(max_trials): if j>=len(dfs[condition]): break ax = fig.add_subplot(n_conditions, max_trials, i*max_trials+j+1, sharex=ax, sharey=ax) (t_df,parsed_data,laser_on) = dfs[condition][j] post_laser_df = t_df[ t_df.index > (laser_on)]# + datetime.timedelta(seconds=2.0)) ] ax.plot( post_laser_df['x'], post_laser_df['y'], 'r.' ) pre_laser_df = t_df[ t_df.index <= (laser_on)]# + datetime.timedelta(seconds=2.0)) ] ax.plot( pre_laser_df['x'], pre_laser_df['y'], 'k.' ) # how many trials do we want to analyze? condition = conditions[0] r = dfs[conditions[0]] (_,parsed_data,_) = dfs[conditions[0]][0] if parsed_data['trialnum'] is None: trial_num_list = [1] else: trial_num_list = [1,2,3] # now iterate trial by trial for trial_num in trial_num_list: for measurement in ['proboscis', 'wing', 'jump', 'abdomen', ]: counts = {} latency_values = defaultdict(list) n_rows, n_cols = my_subplot( n_conditions ) if DO_LATENCY_FIGURE: fig = plt.figure('timeseries %s, trial %d'%(measurement,trial_num)) df = {'latency':[], 'name_key':[], } df_pooled = {'latency':[], 'name_key':[], } for i, condition in enumerate(conditions): target_location, genotype = NAMES[condition] if DO_LATENCY_FIGURE: ax = fig.add_subplot(n_rows, n_cols, i+1 ) arrs = [] total_trials = 0 action_trials = 0 mean_vels = [] for (t_df,parsed_data,laser_on) in dfs[condition]: if measurement not in t_df: continue if parsed_data['trialnum'] is not None: if int(parsed_data['trialnum']) != trial_num: continue else: if trial_num != 1: continue v_mm = np.sqrt(t_df['vx'].values**2 + t_df['vy'].values**2) mean_v_mm = np.mean(v_mm) mean_vels.append( mean_v_mm ) if DO_LATENCY_FIGURE: ax.plot(t_df[measurement], label='%s (fly %s, trial %s)'%(parsed_data['condition'], parsed_data['condition_flynum'], parsed_data['trialnum'], )) arrs.append( t_df['proboscis'] ) selected_df = t_df[ t_df[measurement] > 0.5 ] if len(selected_df)==0: this_latency = np.inf else: ''' # assume any behavior is caused by laser first_behavior_time = selected_df.index[0] this_latency = (first_behavior_time - laser_on).total_seconds() ''' # assume temporal causality in latency measurement behavior_times = selected_df.index for this_bt in behavior_times: this_latency = (this_bt - laser_on).total_seconds() if this_latency >= 0: break if this_latency < 0: this_latency = np.inf latency_values[condition].append( this_latency ) df['latency'].append( this_latency ) df_pooled['latency'].append( this_latency ) name_key = '%s %s'%(genotype, target_location) if '>' not in genotype: pooled_name_key = 'controls '+target_location else: pooled_name_key = name_key df['name_key'].append(name_key) df_pooled['name_key'].append( pooled_name_key ) if np.any( t_df[measurement] > 0.5 ): action_trials += 1 total_trials += 1 counts[condition] = (action_trials,total_trials, np.mean(mean_vels)) all_arrs = np.array( arrs ) mean_arr = np.mean( all_arrs, axis=0 ) if DO_LATENCY_FIGURE: ax.plot( mean_arr, color='k', lw=2 ) ax.set_title('%s (n=%d)'%(condition, counts[condition][1])) ax.legend() if DO_LATENCY_FIGURE: fig.subplots_adjust(hspace=0.39) df = pd.DataFrame(df) df_fname = '%s_notpooled.df'%(measurement,) df.to_pickle(df_fname) print 'saved',df_fname df_pooled = pd.DataFrame(df_pooled) df_fname = '%s_pooled.df'%(measurement,) df_pooled.to_pickle(df_fname) print 'saved',df_fname print '='*80
def plot_data(path, data, arena, note): #customize plot colors here. key is genotype name LABELS = {} COLORS = {} ORDERS = {} fig2 = plt.figure("Speed") ax = fig2.add_subplot(1, 1, 1) datasets = {} for gt in data: gtdf = data[gt] try: datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['v'].values, std=gtdf['std']['v'].values, n=gtdf['n']['v'].values, label=LABELS.get(gt, gt), color=COLORS.get(gt), order=ORDERS.get(gt), df=gtdf['df'], N=len(gtdf['df']['obj_id'].unique())) except KeyError: print "MISSING DATA FOR", gt #assume laser on protocol was identical in all trials ctrlfirst = data[data.keys()[0]]['first'] result_d = flymad_plot.plot_timeseries_with_activation( ax, targetbetween=dict(xaxis=ctrlfirst['t'].values, where=ctrlfirst['laser_state'].values > 0), downsample=5, sem=True, note="OK371shits\n%s\n" % note, individual={ k: { 'groupby': 'obj_id', 'xaxis': 't', 'yaxis': 'v' } for k in datasets }, individual_title='Speed Individual Traces', return_dict=True, **datasets) figs = result_d['figs'] ax.set_xlabel('Time (s)') ax.set_ylabel('Speed (%s/s)' % arena.unit) # ax.set_xlim([-15, 30]) # ax.set_ylim([0, 18]) # flymad_plot.retick_relabel_axis(ax, [-15, 0, 15, 30], [0, 5, 10, 15]) fig2.savefig(flymad_plot.get_plotpath(path, "speed_plot.png"), bbox_inches='tight') fig2.savefig(flymad_plot.get_plotpath(path, "speed_plot.svg"), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath( path, "speed_plot_individual_%s.png" % efigname), bbox_inches='tight')
def plot_data(path, data): ci_html_buf = '' for exp_name in data: gts = data[exp_name].keys() laser = '130ht' gts_string = 'vs'.join(gts) figname = laser + '_' + gts_string fig = plt.figure("Song (%s)" % figname, figsize=(10, 6)) ax = fig.add_subplot(1, 1, 1) datasets = {} for gt in gts: if flymad_analysis.genotype_is_exp(gt): color = flymad_plot.RED order = 1 elif flymad_analysis.genotype_is_ctrl(gt): color = flymad_plot.BLACK order = 2 elif flymad_analysis.genotype_is_trp_ctrl(gt): order = 3 color = flymad_plot.BLUE else: color = 'cyan' order = 0 gtdf = data[exp_name][gt] datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['zx'].values, std=gtdf['std']['zx'].values, n=gtdf['n']['zx'].values, order=order, df=gtdf, label=flymad_analysis.human_label(gt), color=color, N=len(gtdf['df']['obj_id'].unique())) pvalue_buf = '' for gt in datasets: label = flymad_analysis.human_label(gt) if '>' not in label: continue # OK, this is a Gal4 + UAS - do head vs thorax stats gtdf = data[exp_name][gt]['df'] ci_data = do_cum_incidence(gtdf, label) ci_html_buf += ci_data['buf'] ax_cum = ci_data['ax'] spine_placer(ax_cum, location='left,bottom') ax_cum.set_ylabel('Fraction extending wing (%)') ax_cum.set_xlabel('Time (s)') note = '%s\n%s\np-value: %.3g\n%d flies\nn=%d, %d' % ( label, madplot.p2stars(ci_data['p_value']), ci_data['p_value'], len(gtdf['obj_id'].unique()), ci_data['n_head'], ci_data['n_thorax']) ax_cum.text( 0, 1, #top left note, fontsize=10, horizontalalignment='left', verticalalignment='top', transform=ax_cum.transAxes, zorder=0) ci_data['fig'].savefig(flymad_plot.get_plotpath( path, "song_cum_inc_%s.png" % figname), bbox_inches='tight') ci_data['fig'].savefig(flymad_plot.get_plotpath( path, "song_cum_inc_%s.svg" % figname), bbox_inches='tight') # for i in range(len(head_times)): # head_values = gtdf[gtdf['t']==head_times[i]] # thorax_values = gtdf[gtdf['t']==thorax_times[i]] # test1 = head_values['zx'].values # test2 = thorax_values['zx'].values # hval, pval = kruskal(test1, test2) # pvalue_buf += 'Pulse %d: Head vs thorax WEI p-value: %.3g (n=%d, %d)\n'%( # i+1, pval, len(test1), len(test2) ) #all experiments used identical activation times headtargetbetween = dict( xaxis=data['pIP10']['wtrpmyc']['first']['t'].values, where=data['pIP10']['wtrpmyc']['first']['ttm'].values > 0) thoraxtargetbetween = dict( xaxis=data['pIP10']['wtrpmyc']['first']['t'].values, where=data['pIP10']['wtrpmyc']['first']['ttm'].values < 0) flymad_plot.plot_timeseries_with_activation( ax, targetbetween=[headtargetbetween, thoraxtargetbetween], sem=True, note=pvalue_buf, **datasets) ax.set_xlabel('Time (s)') ax.set_ylabel('Wing extension index') ax.set_ylim([-0.05, 0.4] if gts_string == "40347vswtrpmycvs40347trpmyc" else [-0.05, 1.0]) ax.set_xlim([-10, 180]) flymad_plot.retick_relabel_axis( ax, [0, 60, 120, 180], [0, 0.2, 0.4] if gts_string == "40347vswtrpmycvs40347trpmyc" else [0, 0.5, 1]) fig.savefig(flymad_plot.get_plotpath(path, "song_%s.png" % figname), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path, "song_%s.svg" % figname), bbox_inches='tight') with open(flymad_plot.get_plotpath(path, "song_cum_in.html"), mode='w') as fd: fd.write(ci_html_buf)
def plot_all_data(path, data, arena, note): for gt in data: datasets = {} color_cycle = itertools.cycle(flymad_plot.EXP_COLORS) laser_powers_sorted = sorted(data[gt], cmp=flymad_analysis.cmp_laser, reverse=True) for order, laser in enumerate(laser_powers_sorted): #also sort to make cross-activation first cross_activation = (re.match('[0-9]+iru$',laser) and gt.endswith('chrim')) or \ (re.match('[0-9]+ru$',laser) and gt.endswith('trp')) gtdf = data[gt][laser][gt] laser_desc = flymad_analysis.laser_desc(laser) datasets[laser] = dict( xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['Vfwd'].values, std=gtdf['std']['Vfwd'].values, n=gtdf['n']['Vfwd'].values, label=laser_desc, order=50 if cross_activation else order, color=color_cycle.next(), N=len(gtdf['df']['obj_id'].unique()), df=gtdf['df'], ) if 1: if gt == "50660chrim": cgt = "50660" gtdf = data[cgt]["350iru"][cgt] datasets['control'] = dict( xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['Vfwd'].values, std=gtdf['std']['Vfwd'].values, n=gtdf['n']['Vfwd'].values, label=flymad_analysis.human_label(cgt), #laser_desc, order=100, color=flymad_plot.BLACK, N=len(gtdf['df']['obj_id'].unique()), df=gtdf['df'], ) figure_title = "Moonwalker Thorax %s (%s)" % (gt, smoothstr) fig = plt.figure(figure_title, figsize=(10, 8)) ax = fig.add_subplot(1, 1, 1) l, axs, figs = flymad_plot.plot_timeseries_with_activation( ax, targetbetween=dict(xaxis=gtdf['first']['t'].values, where=gtdf['first']['laser_state'].values > 0), sem=True, downsample=5, note="%s\n%s\n" % (gt, note), individual={ k: { 'groupby': ('obj_id', 'trial'), 'xaxis': 't', 'yaxis': 'Vfwd' } for k in datasets }, individual_title=figure_title + ' Individual Traces', **datasets) ax.axhline(color='k', linestyle='--', alpha=0.8) ax.set_ylim(YLIM) ax.set_xlabel('Time (s)') ax.set_ylabel('Fwd Velocity (%s/s)' % arena.unit) ax.set_xlim(XLIM) flymad_plot.retick_relabel_axis(ax, XTICKS, YTICKS) fig.savefig(flymad_plot.get_plotpath(path, "moonwalker_%s.png" % gt), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path, "moonwalker_%s.svg" % gt), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath( path, "moonwalker_%s_individual_%s.png" % (gt, efigname)), bbox_inches='tight')
def plot_cross_activation_only(path, data, arena, note): PLOTS = [ ('50660chrim', { 'activation': '350ru', 'cross_activation': '350iru' }), ('50660trp', { 'activation': '434iru', 'cross_activation': '350ru' }), ('50660trp', { 'activation': '350iru', 'cross_activation': '350ru' }), ] for gt, lasers in PLOTS: figname = 'vs'.join(lasers.values()) alaser = lasers['activation'] adf = data[gt][alaser][gt] claser = lasers['cross_activation'] cdf = data[gt][claser][gt] datasets = { 'activation': dict(xaxis=adf['mean']['t'].values, value=adf['mean']['Vfwd'].values, std=adf['std']['Vfwd'].values, n=adf['n']['Vfwd'].values, label='Activation (%s)' % flymad_analysis.laser_desc(alaser), order=0, color=flymad_plot.RED, N=len(adf['df']['obj_id'].unique()), df=adf['df']), 'cross_activation': dict(xaxis=cdf['mean']['t'].values, value=cdf['mean']['Vfwd'].values, std=cdf['std']['Vfwd'].values, n=cdf['n']['Vfwd'].values, label='Cross Activation (%s)' % flymad_analysis.laser_desc(claser), order=1, color=flymad_plot.BLACK, N=len(cdf['df']['obj_id'].unique()), df=cdf['df']), } figure_title = "Moonwalker Thorax Crosstalk %s (%s)" % (gt, figname) fig = plt.figure(figure_title, figsize=(10, 8)) ax = fig.add_subplot(1, 1, 1) l, axs, figs = flymad_plot.plot_timeseries_with_activation( ax, targetbetween=dict(xaxis=adf['first']['t'].values, where=adf['first']['laser_state'].values > 0), sem=True, downsample=5, note="%s\n%s\n" % (gt, note), individual={ k: { 'groupby': ('obj_id', 'trial'), 'xaxis': 't', 'yaxis': 'Vfwd' } for k in datasets }, individual_title=figure_title + ' Individual Traces', **datasets) ax.axhline(color='k', linestyle='--', alpha=0.8) ax.set_ylim(YLIM) ax.set_xlabel('Time (s)') ax.set_ylabel('Fwd Velocity (%s/s)' % arena.unit) ax.set_xlim(XLIM) flymad_plot.retick_relabel_axis(ax, XTICKS, YTICKS) fig.savefig(flymad_plot.get_plotpath( path, "moonwalker_%s_%s.png" % (gt, figname)), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath( path, "moonwalker_%s_%s.svg" % (gt, figname)), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath( path, "moonwalker_%s_%s_individual_%s.png" % (gt, figname, efigname)), bbox_inches='tight')
def plot_all_data(path, data, arena, note): for gt in data: datasets = {} color_cycle = itertools.cycle(flymad_plot.EXP_COLORS) laser_powers_sorted = sorted(data[gt], cmp=flymad_analysis.cmp_laser, reverse=True) for order,laser in enumerate(laser_powers_sorted): #also sort to make cross-activation first cross_activation = (re.match('[0-9]+iru$',laser) and gt.endswith('chrim')) or \ (re.match('[0-9]+ru$',laser) and gt.endswith('trp')) gtdf = data[gt][laser][gt] laser_desc = flymad_analysis.laser_desc(laser) datasets[laser] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['Vfwd'].values, std=gtdf['std']['Vfwd'].values, n=gtdf['n']['Vfwd'].values, label=laser_desc, order=50 if cross_activation else order, color=color_cycle.next(), N=len(gtdf['df']['obj_id'].unique()), df=gtdf['df'], ) if 1: if gt == "50660chrim": cgt = "50660" gtdf = data[cgt]["350iru"][cgt] datasets['control'] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['Vfwd'].values, std=gtdf['std']['Vfwd'].values, n=gtdf['n']['Vfwd'].values, label=flymad_analysis.human_label(cgt),#laser_desc, order=100, color=flymad_plot.BLACK, N=len(gtdf['df']['obj_id'].unique()), df=gtdf['df'], ) figure_title = "Moonwalker Thorax %s (%s)" % (gt,smoothstr) fig = plt.figure(figure_title, figsize=(10,8)) ax = fig.add_subplot(1,1,1) l, axs, figs = flymad_plot.plot_timeseries_with_activation(ax, targetbetween=dict(xaxis=gtdf['first']['t'].values, where=gtdf['first']['laser_state'].values>0), sem=True, downsample=5, note="%s\n%s\n" % (gt,note), individual={k:{'groupby':('obj_id','trial'),'xaxis':'t','yaxis':'Vfwd'} for k in datasets}, individual_title=figure_title + ' Individual Traces', **datasets ) ax.axhline(color='k', linestyle='--',alpha=0.8) ax.set_ylim(YLIM) ax.set_xlabel('Time (s)') ax.set_ylabel('Fwd Velocity (%s/s)' % arena.unit) ax.set_xlim(XLIM) flymad_plot.retick_relabel_axis(ax, XTICKS, YTICKS) fig.savefig(flymad_plot.get_plotpath(path,"moonwalker_%s.png" % gt), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path,"moonwalker_%s.svg" % gt), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath(path,"moonwalker_%s_individual_%s.png" % (gt, efigname)), bbox_inches='tight')
def plot_data(path, data, arena, note): LABELS = { 'OK371shits-130h': 'OK371>ShibireTS (head)', 'OK371shits-nolaser': 'Control', 'OK371shits-130t': 'OK371>ShibireTS (thorax)', } COLORS = { 'OK371shits-130h': flymad_plot.RED, 'OK371shits-nolaser': flymad_plot.BLACK, 'OK371shits-130t': flymad_plot.ORANGE, } ORDERS = { 'OK371shits-130h': 2, 'OK371shits-nolaser': 4, 'OK371shits-130t': 3, } fig2 = plt.figure("Speed") ax = fig2.add_subplot(1, 1, 1) datasets = {} for gt in data: gtdf = data[gt] datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['v'].values, std=gtdf['std']['v'].values, n=gtdf['n']['v'].values, label=LABELS[gt], color=COLORS[gt], order=ORDERS[gt], df=gtdf['df'], N=len(gtdf['df']['obj_id'].unique())) ctrlfirst = data['OK371shits-nolaser']['first'] result_d = flymad_plot.plot_timeseries_with_activation( ax, targetbetween=dict(xaxis=ctrlfirst['t'].values, where=ctrlfirst['laser_state'].values > 0), downsample=5, sem=True, note="OK371shits\n%s\n" % note, individual={ k: { 'groupby': 'obj_id', 'xaxis': 't', 'yaxis': 'v' } for k in datasets }, individual_title='Speed Individual Traces', return_dict=True, **datasets) figs = result_d['figs'] ax.set_xlabel('Time (s)') ax.set_ylabel('Speed (%s/s)' % arena.unit) ax.set_xlim([-15, 30]) ax.set_ylim([0, 18]) flymad_plot.retick_relabel_axis(ax, [-15, 0, 15, 30], [0, 5, 10, 15]) fig2.savefig(flymad_plot.get_plotpath(path, "speed_plot.png"), bbox_inches='tight') fig2.savefig(flymad_plot.get_plotpath(path, "speed_plot.svg"), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath( path, "speed_plot_individual_%s.png" % efigname), bbox_inches='tight')
def plot_all_data(path, data, arena, note, laser='350iru'): ORDER = ["50660trp", "50660", "wtrp"] COLORS = { "50660trp": flymad_plot.RED, "50660": flymad_plot.BLACK, "wtrp": flymad_plot.BLUE } datasets = {} figure_title = "Moonwalker" fig = plt.figure(figure_title, figsize=TS_FIGSIZE) ax = fig.add_subplot(1, 1, 1) for gt in data: gtdf = data[gt][laser][gt] datasets[gt] = dict( xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['Vfwd'].values, std=gtdf['std']['Vfwd'].values, n=gtdf['n']['Vfwd'].values, order=ORDER.index(gt), label=flymad_analysis.human_label(gt), color=COLORS[gt], N=len(gtdf['df']['obj_id'].unique()), df=gtdf['df'], ) l, axs, figs = flymad_plot.plot_timeseries_with_activation( ax, targetbetween=dict( xaxis=data['50660trp'][laser]['50660trp']['first']['t'].values, where=data['50660trp'][laser]['50660trp']['first'] ['laser_state'].values > 0), downsample=5, sem=True, note="%s\n" % (note, ), individual={ k: { 'groupby': ('obj_id', 'trial'), 'xaxis': 't', 'yaxis': 'Vfwd' } for k in datasets }, individual_title=figure_title + ' Individual Traces', **datasets) ax.axhline(color='k', linestyle='--', alpha=0.8) ax.set_ylim(YLIM) ax.set_xlabel('Time (s)') ax.set_ylabel('Forward velocity (%s/s)' % arena.unit) ax.set_xlim(XLIM) flymad_plot.retick_relabel_axis(ax, XTICKS, YTICKS) fig.savefig(flymad_plot.get_plotpath(path, "moonwalker_simple.png"), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path, "moonwalker_simple.svg"), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath( path, "moonwalker_simple_individual_%s.png" % efigname), bbox_inches='tight')
def plot_data(arena, dirname, smooth, dfs): conditions = dfs.keys() n_conditions = len(conditions) DO_LATENCY_FIGURE = False DO_POSITION_FIGURE = True DO_VELOCITY_FIGURE = True COLORS = { 'th1stim_thorax': 'r', 'th1stim_head': 'b', 'thcstrpa11stim_thorax': 'g', 'thgal41stim_head': 'k', 'thcstrpa11stim_head': 'k', 'thgal41stim_thorax': 'g', } if DO_VELOCITY_FIGURE: fig = plt.figure() ax = fig.add_subplot(111) datasets = {} for i, condition in enumerate(conditions): all_dfs = [] for (df, parsed_data, laser_on) in dfs[condition]: df = df.resample('10L') # 10 msec assert len(df) > 100 df['align'] = np.arange(len(df)) all_dfs.append(df) all_dfs = pd.concat([d for d in all_dfs]) aligned = all_dfs.groupby('align', as_index=False) mean_timeseries = aligned.mean().astype(float) datasets[condition] = dict( xaxis=mean_timeseries['align'].values, value=mean_timeseries['v'].values, std=aligned.std().astype(float)['v'].values, color=COLORS[condition], ) from flymad.flymad_plot import plot_timeseries_with_activation plot_timeseries_with_activation( ax, #targetbetween=[headtargetbetween,thoraxtargetbetween], sem=False, **datasets) if DO_POSITION_FIGURE: fig = plt.figure() max_trials = 0 for i, condition in enumerate(conditions): n_trials = len(dfs[condition]) max_trials = max(max_trials, n_trials) ax = None for i, condition in enumerate(conditions): for j in range(max_trials): if j >= len(dfs[condition]): break ax = fig.add_subplot(n_conditions, max_trials, i * max_trials + j + 1, sharex=ax, sharey=ax) (t_df, parsed_data, laser_on) = dfs[condition][j] post_laser_df = t_df[t_df.index > ( laser_on)] # + datetime.timedelta(seconds=2.0)) ] ax.plot(post_laser_df['x'], post_laser_df['y'], 'r.') pre_laser_df = t_df[t_df.index <= ( laser_on)] # + datetime.timedelta(seconds=2.0)) ] ax.plot(pre_laser_df['x'], pre_laser_df['y'], 'k.') # how many trials do we want to analyze? condition = conditions[0] r = dfs[conditions[0]] (_, parsed_data, _) = dfs[conditions[0]][0] if parsed_data['trialnum'] is None: trial_num_list = [1] else: trial_num_list = [1, 2, 3] # now iterate trial by trial for trial_num in trial_num_list: for measurement in [ 'proboscis', 'wing', 'jump', 'abdomen', ]: counts = {} latency_values = defaultdict(list) n_rows, n_cols = my_subplot(n_conditions) if DO_LATENCY_FIGURE: fig = plt.figure('timeseries %s, trial %d' % (measurement, trial_num)) df = { 'latency': [], 'name_key': [], } df_pooled = { 'latency': [], 'name_key': [], } for i, condition in enumerate(conditions): target_location, genotype = NAMES[condition] if DO_LATENCY_FIGURE: ax = fig.add_subplot(n_rows, n_cols, i + 1) arrs = [] total_trials = 0 action_trials = 0 mean_vels = [] for (t_df, parsed_data, laser_on) in dfs[condition]: if measurement not in t_df: continue if parsed_data['trialnum'] is not None: if int(parsed_data['trialnum']) != trial_num: continue else: if trial_num != 1: continue v_mm = np.sqrt(t_df['vx'].values**2 + t_df['vy'].values**2) mean_v_mm = np.mean(v_mm) mean_vels.append(mean_v_mm) if DO_LATENCY_FIGURE: ax.plot(t_df[measurement], label='%s (fly %s, trial %s)' % ( parsed_data['condition'], parsed_data['condition_flynum'], parsed_data['trialnum'], )) arrs.append(t_df['proboscis']) selected_df = t_df[t_df[measurement] > 0.5] if len(selected_df) == 0: this_latency = np.inf else: ''' # assume any behavior is caused by laser first_behavior_time = selected_df.index[0] this_latency = (first_behavior_time - laser_on).total_seconds() ''' # assume temporal causality in latency measurement behavior_times = selected_df.index for this_bt in behavior_times: this_latency = (this_bt - laser_on).total_seconds() if this_latency >= 0: break if this_latency < 0: this_latency = np.inf latency_values[condition].append(this_latency) df['latency'].append(this_latency) df_pooled['latency'].append(this_latency) name_key = '%s %s' % (genotype, target_location) if '>' not in genotype: pooled_name_key = 'controls ' + target_location else: pooled_name_key = name_key df['name_key'].append(name_key) df_pooled['name_key'].append(pooled_name_key) if np.any(t_df[measurement] > 0.5): action_trials += 1 total_trials += 1 counts[condition] = (action_trials, total_trials, np.mean(mean_vels)) all_arrs = np.array(arrs) mean_arr = np.mean(all_arrs, axis=0) if DO_LATENCY_FIGURE: ax.plot(mean_arr, color='k', lw=2) ax.set_title('%s (n=%d)' % (condition, counts[condition][1])) ax.legend() if DO_LATENCY_FIGURE: fig.subplots_adjust(hspace=0.39) df = pd.DataFrame(df) df_fname = '%s_notpooled.df' % (measurement, ) df.to_pickle(df_fname) print 'saved', df_fname df_pooled = pd.DataFrame(df_pooled) df_fname = '%s_pooled.df' % (measurement, ) df_pooled.to_pickle(df_fname) print 'saved', df_fname print '=' * 80
def plot_data(path, data): ci_html_buf = '' for exp_name in data: gts = data[exp_name].keys() laser = '130ht' gts_string = 'vs'.join(gts) figname = laser + '_' + gts_string fig = plt.figure("Song (%s)" % figname,figsize=(10,6)) ax = fig.add_subplot(1,1,1) datasets = {} for gt in gts: if flymad_analysis.genotype_is_exp(gt): color = flymad_plot.RED order = 1 elif flymad_analysis.genotype_is_ctrl(gt): color = flymad_plot.BLACK order = 2 elif flymad_analysis.genotype_is_trp_ctrl(gt): order = 3 color = flymad_plot.BLUE else: color = 'cyan' order = 0 gtdf = data[exp_name][gt] datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['zx'].values, std=gtdf['std']['zx'].values, n=gtdf['n']['zx'].values, order=order, df=gtdf, label=flymad_analysis.human_label(gt), color=color, N=len(gtdf['df']['obj_id'].unique())) pvalue_buf = '' for gt in datasets: label=flymad_analysis.human_label(gt) if '>' not in label: continue # OK, this is a Gal4 + UAS - do head vs thorax stats gtdf = data[exp_name][gt]['df'] ci_data = do_cum_incidence(gtdf, label) ci_html_buf += ci_data['buf'] ax_cum = ci_data['ax'] spine_placer(ax_cum, location='left,bottom' ) ax_cum.set_ylabel('Fraction extending wing (%)') ax_cum.set_xlabel('Time (s)') note = '%s\n%s\np-value: %.3g\n%d flies\nn=%d, %d'%(label, madplot.p2stars(ci_data['p_value']), ci_data['p_value'], len(gtdf['obj_id'].unique()), ci_data['n_head'], ci_data['n_thorax']) ax_cum.text(0, 1, #top left note, fontsize=10, horizontalalignment='left', verticalalignment='top', transform=ax_cum.transAxes, zorder=0) ci_data['fig'].savefig(flymad_plot.get_plotpath(path,"song_cum_inc_%s.png" % figname), bbox_inches='tight') ci_data['fig'].savefig(flymad_plot.get_plotpath(path,"song_cum_inc_%s.svg" % figname), bbox_inches='tight') # for i in range(len(head_times)): # head_values = gtdf[gtdf['t']==head_times[i]] # thorax_values = gtdf[gtdf['t']==thorax_times[i]] # test1 = head_values['zx'].values # test2 = thorax_values['zx'].values # hval, pval = kruskal(test1, test2) # pvalue_buf += 'Pulse %d: Head vs thorax WEI p-value: %.3g (n=%d, %d)\n'%( # i+1, pval, len(test1), len(test2) ) #all experiments used identical activation times headtargetbetween = dict(xaxis=data['pIP10']['wtrpmyc']['first']['t'].values, where=data['pIP10']['wtrpmyc']['first']['ttm'].values > 0) thoraxtargetbetween = dict(xaxis=data['pIP10']['wtrpmyc']['first']['t'].values, where=data['pIP10']['wtrpmyc']['first']['ttm'].values < 0) flymad_plot.plot_timeseries_with_activation(ax, targetbetween=[headtargetbetween,thoraxtargetbetween], sem=True, note=pvalue_buf, **datasets ) ax.set_xlabel('Time (s)') ax.set_ylabel('Wing extension index') ax.set_ylim([-0.05,0.4] if gts_string == "40347vswtrpmycvs40347trpmyc" else [-0.05,1.0]) ax.set_xlim([-10,180]) flymad_plot.retick_relabel_axis(ax, [0, 60, 120, 180], [0, 0.2, 0.4] if gts_string == "40347vswtrpmycvs40347trpmyc" else [0, 0.5, 1]) fig.savefig(flymad_plot.get_plotpath(path,"song_%s.png" % figname), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path,"song_%s.svg" % figname), bbox_inches='tight') with open(flymad_plot.get_plotpath(path,"song_cum_in.html"),mode='w') as fd: fd.write(ci_html_buf)
def plot_data(path, data, arena, note): LABELS = {'OK371shits-130h':'OK371>ShibireTS (head)', 'OK371shits-nolaser':'Control', 'OK371shits-130t':'OK371>ShibireTS (thorax)', } COLORS = {'OK371shits-130h':flymad_plot.RED, 'OK371shits-nolaser':flymad_plot.BLACK, 'OK371shits-130t':flymad_plot.ORANGE, } ORDERS = {'OK371shits-130h':2, 'OK371shits-nolaser':4, 'OK371shits-130t':3, } fig2 = plt.figure("Speed") ax = fig2.add_subplot(1,1,1) datasets = {} for gt in data: gtdf = data[gt] datasets[gt] = dict(xaxis=gtdf['mean']['t'].values, value=gtdf['mean']['v'].values, std=gtdf['std']['v'].values, n=gtdf['n']['v'].values, label=LABELS[gt], color=COLORS[gt], order=ORDERS[gt], df=gtdf['df'], N=len(gtdf['df']['obj_id'].unique())) ctrlfirst = data['OK371shits-nolaser']['first'] result_d = flymad_plot.plot_timeseries_with_activation(ax, targetbetween=dict(xaxis=ctrlfirst['t'].values, where=ctrlfirst['laser_state'].values>0), downsample=5, sem=True, note="OK371shits\n%s\n" % note, individual={k:{'groupby':'obj_id','xaxis':'t','yaxis':'v'} for k in datasets}, individual_title='Speed Individual Traces', return_dict=True, **datasets ) figs = result_d['figs'] ax.set_xlabel('Time (s)') ax.set_ylabel('Speed (%s/s)' % arena.unit) ax.set_xlim([-15, 30]) ax.set_ylim([0, 18]) flymad_plot.retick_relabel_axis(ax, [-15, 0, 15, 30], [0, 5, 10, 15]) fig2.savefig(flymad_plot.get_plotpath(path,"speed_plot.png"), bbox_inches='tight') fig2.savefig(flymad_plot.get_plotpath(path,"speed_plot.svg"), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath(path,"speed_plot_individual_%s.png" % efigname), bbox_inches='tight')
def plot_cross_activation_only(path, data, arena, note): PLOTS = [('50660chrim',{'activation':'350ru','cross_activation':'350iru'}), ('50660trp',{'activation':'434iru','cross_activation':'350ru'}), ('50660trp',{'activation':'350iru','cross_activation':'350ru'}), ] for gt,lasers in PLOTS: figname = 'vs'.join(lasers.values()) alaser = lasers['activation'] adf = data[gt][alaser][gt] claser = lasers['cross_activation'] cdf = data[gt][claser][gt] datasets = { 'activation':dict(xaxis=adf['mean']['t'].values, value=adf['mean']['Vfwd'].values, std=adf['std']['Vfwd'].values, n=adf['n']['Vfwd'].values, label='Activation (%s)' % flymad_analysis.laser_desc(alaser), order=0, color=flymad_plot.RED, N=len(adf['df']['obj_id'].unique()), df=adf['df']), 'cross_activation':dict( xaxis=cdf['mean']['t'].values, value=cdf['mean']['Vfwd'].values, std=cdf['std']['Vfwd'].values, n=cdf['n']['Vfwd'].values, label='Cross Activation (%s)' % flymad_analysis.laser_desc(claser), order=1, color=flymad_plot.BLACK, N=len(cdf['df']['obj_id'].unique()), df=cdf['df']), } figure_title = "Moonwalker Thorax Crosstalk %s (%s)" % (gt,figname) fig = plt.figure(figure_title, figsize=(10,8)) ax = fig.add_subplot(1,1,1) l,axs, figs = flymad_plot.plot_timeseries_with_activation(ax, targetbetween=dict(xaxis=adf['first']['t'].values, where=adf['first']['laser_state'].values>0), sem=True, downsample=5, note="%s\n%s\n" % (gt,note), individual={k:{'groupby':('obj_id','trial'),'xaxis':'t','yaxis':'Vfwd'} for k in datasets}, individual_title=figure_title + ' Individual Traces', **datasets ) ax.axhline(color='k', linestyle='--',alpha=0.8) ax.set_ylim(YLIM) ax.set_xlabel('Time (s)') ax.set_ylabel('Fwd Velocity (%s/s)' % arena.unit) ax.set_xlim(XLIM) flymad_plot.retick_relabel_axis(ax, XTICKS, YTICKS) fig.savefig(flymad_plot.get_plotpath(path,"moonwalker_%s_%s.png" % (gt, figname)), bbox_inches='tight') fig.savefig(flymad_plot.get_plotpath(path,"moonwalker_%s_%s.svg" % (gt, figname)), bbox_inches='tight') for efigname, efig in figs.iteritems(): efig.savefig(flymad_plot.get_plotpath(path,"moonwalker_%s_%s_individual_%s.png" % (gt, figname, efigname)), bbox_inches='tight')
def plot_data(arena, path, smoothstr, data): COLORS = {"NINE":flymad_plot.RED, "CSSHITS":flymad_plot.BLUE, "NINGal4":flymad_plot.GREEN, 'pooled controls':flymad_plot.BLACK, } ORDERS = {"NINE":1,"pooled controls":2,"NINGal4":3,"CSSHITS":4} LABELS = {"NINE":"NINE>ShiTS","pooled controls":"Controls","NINGal4":"Gal4-control","CSSHITS":"ShiTS-control"} # ---- pool controls ------------ data['pooled controls'] = copy.deepcopy(data['CSSHITS']) assert np.allclose(data['NINGal4']['save_times'], data['pooled controls']['save_times']) # copy 'NINGal4' data into pooled controls for dname in data['NINGal4']: for row in data['NINGal4'][dname]: if dname in ['save_times','stimulus_velocity']: continue elif dname=='chunk': warnings.warn('not updating old chunk data for pooled controls') else: data['pooled controls'][dname].append(row) # for row in data['NINGal4']['chunk']: # for key in row: # data['pooled controls']['chunk'][key].append(row[key]) # ----- raw timeseries plots ------------ fig_angular_vel = plt.figure() fig_linear_vel = plt.figure() fig_summary_angular = plt.figure() ax_summary_angular = fig_summary_angular.add_subplot(211) ax_summary_linear = fig_summary_angular.add_subplot(212) pool_stats = collections.defaultdict(list) #order = ['NINE', 'CSSHITS', 'NINGal4'] order = ['NINE', 'pooled controls'] n_subplots = len(order) ax = None ax_angular_vel = fig_angular_vel.add_subplot(111) ax_linear_vel = fig_linear_vel.add_subplot(111) did_stimulus_plot = False plot_angular_datasets = collections.OrderedDict() plot_linear_datasets = collections.OrderedDict() for gti,gt in enumerate(order): # --- calculate stimulus ----------- stim_times, stim_vel = data[gt]['stimulus_velocity'] if 1: ''' # 10 revolutions take 25 seconds at standard speed real_stim_vel = 10*2*np.pi/25.0 # 10 revolutions/second print 'real_stim_vel',real_stim_vel print 'gain[0]',real_stim_vel/stim_vel[100] print 'gain[1]',real_stim_vel/stim_vel[1001] ''' gain = 0.00083775 # found using above stim_vel = gain*stim_vel if not did_stimulus_plot: ax_angular_vel.plot( stim_times, stim_vel*R2D,flymad_plot.ORANGE, lw=2, label='Stimulus', clip_on=False) did_stimulus_plot = True transition_idxs = np.nonzero(abs(stim_vel[1:]-stim_vel[:-1]))[0] #print 'stim_times[transition_idxs]',stim_times[transition_idxs] n_transitions = len(transition_idxs) # ---- done calculating stimulus --- all_angular_vel_timeseries = np.array(data[gt]['timeseries_angular_vel']) all_linear_vel_timeseries = np.array(data[gt]['timeseries_vel']) times = data[gt]['save_times'] mean_angular_timeseries = np.mean( all_angular_vel_timeseries, axis=0 ) error_angular_timeseries = np.std( all_angular_vel_timeseries, axis=0 ) mean_linear_timeseries = np.mean( all_linear_vel_timeseries, axis=0 ) error_linear_timeseries = np.std( all_linear_vel_timeseries, axis=0 ) if 1: bin_data = [] ang_means = [] ang_stds = [] lin_means = [] lin_stds = [] for i in range(n_transitions): this_data = dict(index=i, angular_velocity_values=[], linear_velocity_values=[], ) start_time = stim_times[transition_idxs[i]] if (i+1)<n_transitions: stop_time = stim_times[transition_idxs[i+1]] else: # final bin stop_time = stim_times[-1] #print 'start_time, stop_time',start_time, stop_time start_idx = np.argmin(abs(times - start_time)) stop_idx = np.argmin(abs(times - stop_time)) #print 'times[start_idx], times[stop_idx]',times[start_idx], times[stop_idx] this_data['start_time'] = times[start_idx] this_data['stop_time'] = times[stop_idx] assert len( all_angular_vel_timeseries)== len(all_linear_vel_timeseries) for j in range( len(all_angular_vel_timeseries)): this_angular_timeseries = all_angular_vel_timeseries[j] this_linear_timeseries = all_linear_vel_timeseries[j] good = ~np.isnan(this_angular_timeseries[start_idx:stop_idx]) ang = np.mean(this_angular_timeseries[start_idx:stop_idx][good]) lin = np.mean(this_linear_timeseries[start_idx:stop_idx][good]) this_data['angular_velocity_values'].append( ang ) this_data['linear_velocity_values'].append( lin ) pool_stats['gt'].append(gt) pool_stats['bin_number'].append(i) pool_stats['bin_start'].append( this_data['start_time'] ) pool_stats['bin_stop'].append( this_data['stop_time'] ) pool_stats['nth_fly_of_gt'].append(j) pool_stats['mean_angular_velocity_during_bin'].append( ang ) pool_stats['mean_linear_velocity_during_bin'].append( lin ) this_data[ 'ang_mean' ] = np.mean( this_data['angular_velocity_values'] ) this_data[ 'ang_std' ] = np.std( this_data['angular_velocity_values'] ) this_data[ 'lin_mean' ] = np.mean( this_data['linear_velocity_values'] ) this_data[ 'lin_std' ] = np.std( this_data['linear_velocity_values'] ) bin_data.append( this_data ) ang_means.append( this_data['ang_mean'] ) ang_stds.append( this_data['ang_std'] ) lin_means.append( this_data['lin_mean'] ) lin_stds.append( this_data['lin_std'] ) ang_means = np.array(ang_means) ang_stds = np.array(ang_stds) lin_means = np.array(lin_means) lin_stds = np.array(lin_stds) ax_summary_angular.axhline(0,color='black') ax_summary_angular.plot( ang_means*R2D, label=gt, color=COLORS[gt] ) ax_summary_angular.plot( (ang_means+ang_stds)*R2D, color=COLORS[gt] ) ax_summary_angular.plot( (ang_means-ang_stds)*R2D, color=COLORS[gt] ) ax_summary_linear.axhline(0,color='black') ax_summary_linear.plot( lin_means, label=gt, color=COLORS[gt] ) ax_summary_linear.plot( lin_means+lin_stds, color=COLORS[gt] ) ax_summary_linear.plot( lin_means-lin_stds, color=COLORS[gt] ) this_data_angular_vel = {'xaxis':times, 'value':mean_angular_timeseries*R2D, 'std':error_angular_timeseries*R2D, 'label':gt, 'color':COLORS[gt], 'label':LABELS[gt], 'order':ORDERS[gt], } this_data_linear_vel = {'xaxis':times, 'value':mean_linear_timeseries, 'std':error_linear_timeseries, 'label':gt, 'color':COLORS[gt], 'label':LABELS[gt], 'order':ORDERS[gt], } plot_angular_datasets[gt]=this_data_angular_vel plot_linear_datasets[gt]=this_data_linear_vel tb = {'xaxis':data['pooled controls']['save_times'], 'where':data['pooled controls']['laser_power']>1, } flymad_plot.plot_timeseries_with_activation( ax_angular_vel, downsample=2, targetbetween=[tb], **plot_angular_datasets) ax_angular_vel.axhline(0,color='black') ax_angular_vel.set_xlabel('Time (s)') ax_angular_vel.set_ylabel('Angular velocity (deg/s)') ax_angular_vel.set_ylim(-200,200) ax_angular_vel.set_xlim(0,300) flymad_plot.retick_relabel_axis(ax_angular_vel, [0,150,300], [-200,0,200]) figname = 'optoresponse_ts_angular_vel' fig_angular_vel.savefig(flymad_plot.get_plotpath(path,"%s.png" % figname), bbox_inches='tight') fig_angular_vel.savefig(flymad_plot.get_plotpath(path,"%s.svg" % figname), bbox_inches='tight') flymad_plot.plot_timeseries_with_activation( ax_linear_vel, targetbetween=[tb], downsample=3, **plot_linear_datasets) ax_linear_vel.set_ylim([0,40]) ax_linear_vel.set_xlim(0,300) # ax_linear_vel.spines['bottom'].set_bounds(0,330.0) # ax_linear_vel.spines['bottom'].set_linewidth(0.3) # ax_linear_vel.spines['left'].set_linewidth(0.3) ax_linear_vel.set_xlabel('Time (s)') ax_linear_vel.set_ylabel('Velocity (%s/s)' % arena.unit) flymad_plot.retick_relabel_axis(ax_linear_vel, [0,150,300], [0,20,40]) figname = 'optoresponse_ts_linear_vel' fig_linear_vel.savefig(flymad_plot.get_plotpath(path,"%s.png" % figname), bbox_inches='tight') fig_linear_vel.savefig(flymad_plot.get_plotpath(path,"%s.svg" % figname), bbox_inches='tight') if 1: def p2star(p): if p < 0.001: result = '***' elif p < 0.01: result = '**' elif p < 0.05: result = '*' else: result = '' result += ' %.3g'%p return result # calculate p values pool_stats_df = pd.DataFrame(pool_stats) df_fname = 'optomotor_stats.df' pool_stats_df.to_pickle(df_fname) sig = do_stats( df_fname ) for i in range(len(sig['bin_num'])): bin_num = sig['bin_num'][i] p_value = sig['ang'][i] ax_summary_angular.text( bin_num, 0, p2star(p_value)) p_value = sig['ang_zero'][i] ax_summary_angular.text( bin_num, -100, p2star(p_value)) ax_summary_angular.text( bin_num, 140, '%s - %s'%(sig['bin_start'][i], sig['bin_stop'][i]), ) p_value = sig['lin'][i] ax_summary_linear.text( bin_num, 5, p2star(p_value)) ax_summary_angular.set_ylabel('angular velocity (deg/sec)') ax_summary_linear.set_ylabel('velocity (mm/sec)') ax_summary_linear.set_xlabel('bin number') fig_fname = 'fig_summary.png' fig_summary_angular.savefig(fig_fname) print 'saved',fig_fname fig_fname = 'fig_summary.svg' fig_summary_angular.savefig(fig_fname) print 'saved',fig_fname fig_fname = 'fig_summary.pdf' fig_summary_angular.savefig(fig_fname) print 'saved',fig_fname # ----------------------- # plot individual timeseries for gti,gt in enumerate(order): fig_gt = plt.figure('indiv_timeseries_'+gt) n_rows = len(data[gt]['timeseries_vel']) n_cols = 2 # linear and angular save_times = data[gt]['save_times'] ax_lin = None ax_ang = None for i in range(n_rows): angular_vel = data[gt]['timeseries_angular_vel'][i] linear_vel = data[gt]['timeseries_vel'][i] ax_lin = fig_gt.add_subplot( n_rows, 2, i*2+1, sharex=ax_lin, sharey=ax_lin ) ax_ang = fig_gt.add_subplot( n_rows, 2, i*2+2, sharex=ax_ang, sharey=ax_ang ) ax_ang.axhline(0,color='black') ax_lin.plot( save_times, linear_vel ) ax_ang.plot( save_times, angular_vel*R2D ) bag = os.path.basename(data[gt]['bag_fname'][i]) ax_lin.text(50,50,'lin '+bag) ax_ang.text(50,3*R2D,'ang '+bag) ax_lin.set_ylim(0,50) ax_ang.set_ylim(-300,300) plt.figtext(0.05, 0.05, gt) fname = 'individual_%s.pdf'%gt fig_gt.savefig(fname) print 'saved',fname # ----------------------- if 1: return # - John's category stuff ---------------- #pprint.pprint(data) figt = plt.figure('dtheta %s' % (smoothstr), figsize=(16,10)) axt = figt.add_subplot(1,1,1) figv = plt.figure('v %s' % (smoothstr), figsize=(16,10)) axv = figv.add_subplot(1,1,1) for gt in order: xdata = [] ytdata = [] yvdata = [] for xlabel in sorted(data[gt]['chunk']): xloc = int(xlabel[0]) for mean_dtheta,mean_v in data[gt]['chunk'][xlabel]: xdata.append(xloc) ytdata.append(mean_dtheta) yvdata.append(mean_v) # print gt,xdata,ydata axt.plot(xdata,ytdata,'o',color=COLORS[gt],markersize=5,label=gt) axv.plot(xdata,yvdata,'o',color=COLORS[gt],markersize=5,label=gt) for ax in (axt,axv): ax.legend() ax.set_xlim(0,9) ax.set_xticks([int(s[0]) for s in sorted(data["NINE"]['chunk'])]) ax.set_xticklabels([s[2:] for s in sorted(data["NINE"]['chunk'])]) figpath = os.path.join(path,'dtheta_%s.png' % (smoothstr)) figt.savefig(figpath) print "wrote", figpath figpath = os.path.join(path,'v_%s.png' % (smoothstr)) figv.savefig(figpath) print "wrote", figpath