Ejemplo n.º 1
0
def plot_behavioral_index_as_frac_of_courtship(
    exp,
    bnames,
    colors,
    show_points=True
    ):
    """
    Parameters
    ----------
    bnames : list of string
        Should always be in the following order ['tap', 'ori', 'sci']
    """
    fig, ax = plt.subplots()

    num_behaviors = len(bnames)
    num_groups = len(exp.order)

    at = get_paired_ixs(num_groups, num_behaviors).tolist()

    for i, name in enumerate(bnames):
        index = exp.get_behavioral_index_as_fraction_of_courtship(
            name,
            'courtship_gt'
            )

        cp.boxplot(index, order=exp.order, colors=colors, at=at[i], ax=ax,
            widths=0.5, zorder=0)
        if show_points:
            cp.striphist(
                index,
                order=exp.order,
                markerfacecolors=['k'] * num_groups,
                markeredgecolors=['k'] * num_groups,
                at=at[i],
                ax=ax,
                zorder=1,
                s=10
                )

    ax = despine(ax)
    # ax.set_xlim(0, 9)
    ax.set_ylim(0, 1)
    ax.set_xticks(np.mean(at, axis=1))
    ax.set_xticklabels(['tap', 'ori', 'sci'])
    ax.set_yticks([0, 0.5, 1])

    fig.set_size_inches(4,3)
    return fig, ax
Ejemplo n.º 2
0
def plot_transition_probas_as_boxes_01(
    exp,
    bnames,
    colors,
    show_points=True
    ):
    """
    Parameters
    ----------
    bnames : list of string ['tap', 'ori', 'sci']
        These should be valid behavior names.

    Returns
    -------
    fig, ax : figure & axes handles
    transition_names : list of string
    transition_data : dict of dict
        Each key is a transition name, each value is a second dictionary where
        keys are group names and values are arrays of transitions probas for
        each individual in the group.
    """
    fig, ax = plt.subplots()
    transition_names = [
        'tap->tap', 'tap->ori', 'tap->sci',
        'ori->tap', 'ori->ori', 'ori->sci',
        'sci->tap', 'sci->ori', 'sci->sci'
    ]
    transition_data = {tn: [] for tn in transition_names}
    all_data = {tn: {} for tn in transition_names}

    num_transitions = len(transition_names)
    num_groups = len(exp.order)

    at = get_paired_ixs(num_groups, num_transitions).T.tolist()

    for i, group_name in enumerate(exp.order):
        trans_mat = markov.get_transition_matrix(exp, group_name, bnames)
        j = 0
        for rr in xrange(trans_mat.shape[0]):
            for cc in xrange(trans_mat.shape[1]):
                trans_arr = pd.Series(trans_mat[rr, cc, :])
                transition_data[transition_names[j]] = trans_arr.dropna().values
                all_data[transition_names[j]][group_name] = trans_arr.dropna().values
                j += 1

        cp.boxplot(
            data=transition_data,
            order=transition_names,
            colors=[colors[i]]*9,
            at=at[i],
            ax=ax,
            zorder=0
        )

        if show_points:
            cp.striphist(
                data=transition_data,
                order=transition_names,
                at=at[i],
                ax=ax,
                markerfacecolors=['k']*9,
                markeredgecolors=['k']*9,
                zorder=1,
                s=10,
                clip_on=False
            )

    ax = despine(ax)
    # ax.set_xlim(0, 27)
    ax.set_xticks(np.mean(at, axis=0))
    ax.set_xticklabels(transition_names)

    ax.set_ylim(-0.1, 1)
    ax.set_yticks([0, 0.5, 1])
    fig.set_size_inches(12, 3)
    return fig, ax, transition_names, all_data
Ejemplo n.º 3
0
def plot_cl(
    exp,
    colors,
    bname='courtship_gt',
    include_nonbehavors=False,
    ax=None
    ):
    """Plots the courtship latency as a boxplot overlayed with a binned strip
    chart.

    Parameters
    ----------
    exp : FixedCourtshipTrackingExperiment
        Experiment to plot CL for.

    colors : list
        Each item should be a valid matplotlib color. Colors of dots in strip
        chart. These should be ordered as in `exp.order`.

    bname : string (optional, default='courtship_gt')
        Name of behavior to calculate latency for. This name should represent
        courtship if you want to plot the courtship latency.

    include_nonbehavors : bool (optional, default=False)
        Whether or not to include flies that did not engage in courtship in the
        calculated latency.

    ax : matplotlib.Axes handle or None (optional, default=None)
        Axes handle to plot onto.

    Returns
    -------
    fig, ax : matplotlib.Figure and matplotlib.Axes handles.
    """
    if ax is None:
        fig, ax = plt.subplots()
    else:
        fig = plt.gcf()

    cl = exp.get_behavioral_latencies(
        bname,
        include_nonbehavors=include_nonbehavors
    )

    cp.boxplot(
        cl,
        exp.order,
        colors=['lightgray']*len(exp.order),
        ax=ax,
        zorder=0,
        showfliers=False,
        widths=0.25
    )

    cp.striphist(
        cl,
        order=exp.order,
        ax=ax,
        zorder=1,
        spread=0.25,
        marker='o',
        markerfacecolors=colors,
        markeredgecolors=colors,
        num_bins=30,
        clip_on=True,
        s=10
    )

    ax = despine(ax)
    ax.set_xticklabels([])
    ax.set_yticks([0, 300, 600])
    ax.set_ylim(0, 600)
    fig.set_size_inches(4,3)
    return fig, ax
Ejemplo n.º 4
0
def plot_dists_peak_ratios(
    exp,
    bname='courtship_gt',
    colors=['k', 'steelblue'],
    show_outliers=True,
    ax=None,
    spread=0.25,
    s=20
    ):
    """Creates a boxplot showing the ratio of the average male-female
    distance (centroid-to-centroid) when the fly is on the front half of the
    female to when the fly is on the rear half of the female.

    $$ DPR = \frac{\tilde{d_{\text{front}}}}{\tilde{d_{\text{rear}}}} $$

    Parameters
    ----------
    exp : canal.objects.experiment.FixedCourtshipExperiment

    bname : string
        Must be a valid behavior name in each male in the Experiment.

    colors : list of valid matplotlib colors

    show_outliers : bool, optional (default=True)
        Whether or not to show outliers.

    Returns
    -------
    fig, ax
    """
    if ax is None:
        fig, ax = plt.subplots()
    else:
        fig = plt.gcf()

    ratios = exp.get_behavioral_distances_peak_ratio(bname)
    if not show_outliers:
        ratios = remove_outliers(ratios)

    cp.boxplot(
        ratios,
        colors=['lightgray'] * len(exp.order),
        order=exp.order,
        ax=ax,
        zorder=0,
        showfliers=False,
        widths=0.5
    )

    cp.striphist(
        ratios,
        order=exp.order,
        ax=ax,
        zorder=1,
        spread=spread,
        marker='o',
        markerfacecolors=colors,
        markeredgecolors=colors,
        alpha=0.75,
        num_bins=30,
        s=s
    )

    ax.set_xticklabels([])

    ax.set_ylim(0, 3)
    ax.set_yticks([0, 1, 2, 3])
    ax.set_yticklabels([])

    ax.hlines(1, 0, len(exp.order) + 1, linestyle='--', color='k', alpha=0.5)
    ax=despine(ax)
    fig.set_size_inches(3,4)
    return fig, ax