def plot_randomized_speed_profiles(avgSpeeds, trialTypes):
    # Set Plotting Attributes
    color1 = (0.0, 0.0, 0.0, 0.1)
    color2 = (1.0, 0.6, 0.0, 0.1)
    color1b = (0.0, 0.0, 0.0, 1.0)
    color2b = (1.0, 0.6, 0.0, 1.0)

    traceColors = [color1, color2]
    boldColors = [color1b, color2b]

    # Plot Average Speeds in bins
    plt.figure()
    numTrials = np.size(trialTypes)
    for t in range(0, numTrials):
        if trialTypes[t] == 0:
            plt.plot(avgSpeeds[t, :], color=color1)
        else:
            plt.plot(avgSpeeds[t, :], color=color2)

    stableTrials = np.where(trialTypes == 0)
    unstableTrials = np.where(trialTypes == 1)
    mSt = stats.nanmean(avgSpeeds[stableTrials, :], 1)
    mUn = stats.nanmean(avgSpeeds[unstableTrials, :], 1)
    eSt = stats.nanstd(avgSpeeds[stableTrials, :],
                       1) / np.sqrt(np.size(stableTrials) - 1)
    eUn = stats.nanstd(avgSpeeds[unstableTrials, :],
                       1) / np.sqrt(np.size(unstableTrials) - 1)

    #    eSt = stats.nanstd(avgSpeeds[stableTrials, :], 1)
    #    eUn = stats.nanstd(avgSpeeds[unstableTrials, :], 1)

    mSt = mSt[0]
    mUn = mUn[0]
    eSt = eSt[0]
    eUn = eUn[0]

    plt.plot(mUn, color=color2b, linewidth=7)
    plt.plot(mSt, color=color1b, linewidth=7)

    #    plt.plot(mSt + eSt, color=color1b, linewidth = 0.5)
    #    plt.plot(mSt - eSt, color=color1b, linewidth = 0.5)
    #    plt.plot(mUn + eUn, color=color2b, linewidth = 0.5)
    #    plt.plot(mUn - eUn, color=color2b, linewidth = 0.5)
    #pltutils.fix_font_size()
    plt.xlabel('crossing extent (cm)')
    plt.ylabel('normalized horizontal speed')
    pltutils.fix_font_size()
    plt.axis([0, 39, 0, 3])
def plot_randomized_group_average_speed_profiles(avgProfiles,
                                                 labelx=True,
                                                 labely=True,
                                                 legend=True,
                                                 title=None):
    # Set Plotting Attributes
    color1b = (0.0, 0.0, 0.0, 1.0)
    color2b = (1.0, 0.6, 0.0, 1.0)

    # Unpack average profile structure
    (stAvg, stErr), (unAvg, unErr) = avgProfiles

    # Prepare Bulk Arrays
    stAvg = np.array(stAvg)
    unAvg = np.array(unAvg)
    stErr = np.array(stErr)
    unErr = np.array(unErr)

    # Plot Averge Speed Profiles
    a1 = plt.plot(np.mean(unAvg, 0),
                  color=color2b,
                  linewidth=3,
                  label='unstable')
    a2 = plt.plot(np.mean(stAvg, 0),
                  color=color1b,
                  linewidth=3,
                  label='stable')
    if labelx:
        plt.xlabel('crossing extent (cm)')
    if labely:
        plt.ylabel('normalized horizontal speed')
    pltutils.fix_font_size()
    plt.axis([0, numBins, 0.5, 2.0])
    plt.yticks([0.75, 1.0, 1.25, 1.5, 1.75])
    if legend:
        plt.legend((a2[0], a1[0]), ('stable', 'unstable'), loc='upper left')

    if title is not None:
        ax = plt.gca()
        ax.text(0.5,
                0.9,
                title,
                horizontalalignment='center',
                transform=ax.transAxes)
Exemple #3
0
offsetscale = 2
plt.close('all')
fig = plt.figure('conditions average tip speed across sessions')
ax = fig.gca()
ax.set_color_cycle(group_cycle)
[
    plot_average_tip_speed('conditions', [
        mclesionsham[group_order[i]][s] for s in ([0, 1, 2] if i > 0 else [0])
    ],
                           crop=valid_positions,
                           label='lesion' if group_cycle[i] == 'r' else 'sham',
                           offset=i * 2,
                           scale=46,
                           trial_slices=slice(1, None)) for i in range(0, 14)
]
pltutils.fix_font_size()
handles, labels = ax.get_legend_handles_labels()
plt.legend((handles[0], handles[7]), (labels[0], labels[7]))
plt.title('average nose speed across assay conditions')
tickoffset = 6.5 * offsetscale
ax.set_xticks([tickoffset, scale + tickoffset, scale * 2 + tickoffset])
ax.set_xticklabels(['stable', 'center unstable', 'all released'])
plt.xlabel('')
plt.draw()


######## I.d Nose Speed Distribution Across Conditions Across Groups ##########
def barplot(data, offset, barwidth, color):
    plt.bar(offset,
            np.mean(data),
            width=barwidth,
def plot_comparison_measure(group_order,
                            group_cycle,
                            mclesionsham,
                            bracket_offset=5,
                            bracket_tickheight=1):
    scale = 46
    offsetscale = 2
    plt.close('all')
    fig = plt.figure('stable sessions average trial times', figsize=(4, 7))
    ax = fig.gca()
    ax.set_color_cycle(group_cycle)
    measures = [
        plot_average_trial_times(
            'stable sessions',
            mclesionsham[group_order[i]][1:5],
            label='lesion' if group_cycle[i] == 'r' else 'control',
            offset=i * 2,
            scale=46) for i in range(14)
    ]
    plt.ylabel('time between rewards (s)')
    plt.xlabel('sessions')

    lesionoffset = 3 * offsetscale
    lesioncenter = [scale * i + lesionoffset for i in range(4)]
    lesion_times = [[
        get_average_trial_time(mclesionsham[i][s]) for i in group_order[0:7]
    ] for s in range(1, 5)]
    lesion_mean = [np.mean(x) for x in lesion_times]
    lesion_std = [np.std(x) for x in lesion_times]
    plt.errorbar(lesioncenter,
                 lesion_mean,
                 lesion_std,
                 fmt='--o',
                 color=group_cycle[0],
                 ecolor='k',
                 linewidth=2,
                 capthick=2,
                 markersize=0)

    controloffset = 10 * offsetscale
    controlcenter = [scale * i + controloffset for i in range(4)]
    control_times = [[
        get_average_trial_time(mclesionsham[i][s]) for i in group_order[7:14]
    ] for s in range(1, 5)]
    control_mean = [np.mean(x) for x in control_times]
    control_std = [np.std(x) for x in control_times]
    plt.errorbar(controlcenter,
                 control_mean,
                 control_std,
                 fmt='--o',
                 color=group_cycle[7],
                 ecolor='k',
                 linewidth=2,
                 capthick=2,
                 markersize=0)

    pltutils.fix_font_size()
    handles, labels = ax.get_legend_handles_labels()
    plt.legend((handles[0], handles[7]), (labels[0], labels[7]))

    tickoffset = 6.5 * offsetscale
    xticks = [scale * i + tickoffset for i in range(4)]
    ax.set_xticks(xticks)
    ax.set_xticklabels([1, 2, 3, 4])

    ### BRACKETS (BETWEEN GROUPS) ###
    maxstd = []
    minstd = []
    significance = 0.05
    maxrange = len(measures)
    for i in range(len(xticks)):
        sigtest = stats.ttest_ind(lesion_times[i], control_times[i])[1]
        print sigtest, "groups"
        testlabel = str.format("*",
                               sigtest) if sigtest < significance else 'n.s.'
        maxstd.append(
            get_bracket_y(
                measures,
                range(maxrange) if i < 1 else range(0, 7) + range(8, 14), i))
        minstd.append(
            get_negative_bracket_y(
                measures,
                range(maxrange) if i < 1 else range(0, 7) + range(8, 14), i))

    minstd = min(minstd)
    for i in range(len(xticks)):
        pltutils.hbracket(xticks[i],
                          minstd - bracket_offset,
                          1.4,
                          label=testlabel,
                          tickheight=-10 * bracket_tickheight)
    #################################

    plt.draw()
def plot_randomized_speed_profile_difference_comparison(controls, lesions):
    controlMeans = np.mean(controls, 0)
    controlMeanAll = np.mean(controlMeans)
    controlError = np.std(controlMeans) / np.sqrt(7 - 1)

    lesionMeans = np.mean(lesions, 0)
    lesionMeanAll = np.mean(lesionMeans)
    lesionlError = np.std(lesionMeans) / np.sqrt(6 - 1)

    significance = 0.05
    sigtest = stats.ttest_ind(controlMeans, lesionMeans)[1]
    testlabel = str.format("*", sigtest) if sigtest < significance else 'n.s.'
    print sigtest

    cX = [1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]
    lX = [1.9, 1.9, 1.9, 1.9, 1.9, 1.9]

    plt.plot([0, 3], [0, 0], color=[0.25, 0.25, 0.25, 1], linewidth=1)
    plt.plot(cX, controlMeans, 'bo')
    plt.plot(lX, lesionMeans, 'ro')
    plt.plot(1.9, lesionMeans[5], 'o', color=[1.0, 0.75, 0.75, 1.0])

    #plt.bar(1.2, controlMeanAll, 0.2, color=[1.0,1.0,1.0,0.0])
    plt.errorbar(1.3,
                 controlMeanAll,
                 controlError,
                 marker='s',
                 mfc='blue',
                 ecolor='black',
                 mec='black',
                 ms=1,
                 mew=1,
                 capsize=5,
                 elinewidth=2)
    plt.plot([1.0, 1.4], [controlMeanAll, controlMeanAll],
             color=[0.25, 0.25, 1.0, 1],
             linewidth=2)

    #plt.bar(1.6, lesionMeanAll, 0.2, color=[1.0,1.0,1.0,0.0])
    plt.errorbar(1.7,
                 lesionMeanAll,
                 lesionlError,
                 marker='s',
                 mfc='red',
                 ecolor='black',
                 mec='black',
                 ms=1,
                 mew=1,
                 capsize=5,
                 elinewidth=2)
    plt.plot([1.6, 2.0], [lesionMeanAll, lesionMeanAll],
             color=[1.00, 0.25, 0.25, 1],
             linewidth=2)
    pltutils.hbracket(1.5, 0.17, 4.5, label=testlabel, tickheight=0.01)

    ax = plt.gca()
    ax.set_xticks([1.1, 1.9])
    ax.set_xticklabels(['controls', 'lesions'])
    plt.ylabel('normalized speed difference')

    pltutils.fix_font_size()
    plt.axis([0.75, 2.25, -0.2, 0.2])
def plot_trial_measures(group_cycle,
                        group_order,
                        trial_measures,
                        bracket_offset=0.5,
                        bracket_tickheight=1,
                        ylims=None,
                        xticklabels=None):
    scale = 46
    offsetscale = 2
    num_sessions = len(trial_measures[0])
    ax = plt.gca()
    ax.set_color_cycle(group_cycle)
    measures = [[
        trial_measures[group_order[i]][s]
        for s in (range(num_sessions) if i != 7 else [0])
    ] for i in range(len(trial_measures))]
    [
        plot_epoch_average(
            measures[i],
            label='lesion' if group_cycle[i] == 'r' else 'control',
            offset=i * offsetscale,
            scale=scale) for i in range(len(trial_measures))
    ]
    plt.ylabel('time to cross obstacles (s)')
    plt.xlabel('x')

    offset1 = 3 * offsetscale
    center1 = [scale * i + offset1 for i in range(num_sessions)]
    group1_times = [[np.mean(trial_measures[i][s]) for i in group_order[0:7]]
                    for s in range(num_sessions)]
    group1_mean = [np.mean(x) for x in group1_times]
    group1_std = [np.std(x) for x in group1_times]
    plt.errorbar(center1,
                 group1_mean,
                 group1_std,
                 fmt='o',
                 color=group_cycle[0],
                 ecolor='k',
                 linewidth=2,
                 capthick=2,
                 markersize=0)

    offset2 = 10 * offsetscale
    center2 = [scale * i + offset2 for i in range(num_sessions)]
    group2_times = [[
        np.mean(trial_measures[i][s])
        for i in (group_order[7:14] if s < 1 else group_order[8:14])
    ] for s in range(num_sessions)]
    group2_mean = [np.mean(x) for x in group2_times]
    group2_std = [np.std(x) for x in group2_times]
    plt.errorbar(center2,
                 group2_mean,
                 group2_std,
                 fmt='o',
                 color=group_cycle[7],
                 ecolor='k',
                 linewidth=2,
                 capthick=2,
                 markersize=0)

    pltutils.fix_font_size()
    #handles, labels = ax.get_legend_handles_labels()
    #plt.legend((handles[0],handles[7]),(labels[0],labels[7]))
    tickoffset = 6.5 * offsetscale
    xticks = [scale * i + tickoffset for i in range(num_sessions)]
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticklabels)

    ### BRACKETS (BETWEEN GROUPS) ###
    maxstd = []
    minstd = []
    significance = 0.01
    maxrange = len(measures)
    for i in range(len(xticks)):
        sigtest = stats.ttest_ind(group1_times[i], group2_times[i])[1]
        print sigtest, "groups"
        testlabel = str.format("*",
                               sigtest) if sigtest < significance else 'n.s.'
        maxstd.append(
            get_bracket_y(
                measures,
                range(maxrange) if i < 1 else range(0, 7) + range(8, 14), i))
        minstd.append(
            get_negative_bracket_y(
                measures,
                range(maxrange) if i < 1 else range(0, 7) + range(8, 14), i))

    maxstd = max(maxstd)
    for i in range(len(xticks)):
        pltutils.hbracket(xticks[i],
                          maxstd + bracket_offset,
                          2,
                          label=testlabel,
                          tickheight=bracket_tickheight)
    ##################################

    ### BRACKETS (BETWEEN CONDITIONS) ###
    minstd = min(minstd)
    sigtest = stats.ttest_ind(group1_times[0] + group2_times[0],
                              group1_times[-1] + group2_times[-1])[1]
    print sigtest, "conditions"
    testlabel = str.format("*", sigtest) if sigtest < significance else 'n.s.'
    #pltutils.hbracket(xticks[1],minstd-bracket_offset,5.5,label=testlabel,tickheight=-1.5*bracket_tickheight)
    pltutils.hbracket((xticks[-1] - xticks[0]) / 2 + xticks[0],
                      minstd - bracket_offset,
                      9.5,
                      label=testlabel,
                      tickheight=-1.5 * bracket_tickheight)
    #####################################

    ### SEPARATORS ###
    ylims = plt.ylim() if ylims is None else ylims
    for i in range(len(xticks) - 1):
        separatorxi = (xticks[i] + xticks[i + 1]) / 2
        ax.plot((separatorxi, separatorxi), ylims, 'k--')
    plt.ylim(ylims)
    ##################

    plt.xlabel('')
    plt.draw()