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')
Example #2
0
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_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_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')
Example #6
0
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)