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
def plot_transition_probas( exp, bnames, colors, from_behavior='tap' ): data = get_transition_data_as_dict(exp, bnames) if from_behavior == 'tap': order = ['tap->tap', 'tap->ori', 'tap->sci'] elif from_behavior == 'ori': order = ['ori->tap', 'ori->ori', 'ori->sci'] elif from_behavior == 'sci': order = ['sci->tap', 'sci->ori', 'sci->sci'] else: raise AttributeError('`from_behavior` must one of [\'tap\', \'ori\', ' + '\'sci\']') num_groups = len(exp.order) num_transitions = len(order) at = get_paired_ixs(num_groups, num_transitions).tolist() fig, ax = plt.subplots() for i, transition_name in enumerate(order): cp.boxplot( data[transition_name], order=exp.order, at=at[i], colors=colors, ax=ax, widths=0.5 ) # ax.set_xlim(0, 9) ax.set_ylim(-0.1, 1.1) ax.set_xticks(np.mean(at, axis=1)) ax.set_xticklabels(order) ax.set_yticks([0, 0.5, 1]) return fig, ax
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
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
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
def plot_sideways_velocity_ratios( exp, colors, behavior_name='courtship_gt', num_bins=50, ax=None, **kwargs ): """ Returns ------- fig : ax : v_side_to_front : v_side_to_rear : """ if ax is None: fig, ax = plt.subplots() else: fig = plt.gcf() vels = exp.get_binned_abs_sideways_velocities( behavior_name, num_bins=num_bins ) thetas = np.linspace(-np.pi, np.pi, num_bins) rear_quadrant_ixs = (thetas > 3*np.pi/4) + (thetas < -3*np.pi/4) front_quadrant_ixs = (thetas < np.pi/4) * (thetas > -np.pi/4) left_quadrant_ixs = (thetas < -np.pi/4) * (thetas > -3*np.pi/4) right_quadrant_ixs = (thetas > np.pi/4) * (thetas < 3*np.pi/4) side_to_front = {} side_to_rear = {} for group_name in exp.order: v = vels[group_name] v_side = np.nanmean(v[:, left_quadrant_ixs + right_quadrant_ixs], axis=1) v_front = np.nanmean(v[:, front_quadrant_ixs], axis=1) v_rear = np.nanmean(v[:, rear_quadrant_ixs], axis=1) v_side_to_front = v_side/v_front v_side_to_rear = v_side/v_rear side_to_front[group_name] = v_side_to_front side_to_rear[group_name] = v_side_to_rear # plot cp.boxplot( clean_dataset(side_to_front), order=exp.order, colors=colors, ax=ax, at=[1,2], zorder=1, **kwargs ) cp.boxplot( clean_dataset(side_to_rear), order=exp.order, colors=colors, ax=ax, at=[4,5], zorder=1, **kwargs ) ax.hlines(1, 0, 6, linestyle='--', color='k', zorder=0) ax = cp.format_categorical_axes(ax) ax.set_xlim(0, 6) ax.set_xticks([1,2, 4,5]) ax.set_xticklabels([]) ax.set_ylim(0, 6) ax.set_yticks([0, 3, 6]) return fig, ax, clean_dataset(side_to_front), clean_dataset(side_to_rear)