Ejemplo n.º 1
0
def plot_avg_psd_encoder_weights(agg, data_dir='/auto/tdrive/mschachter/data', decomp='full_psds'):

    freqs,lags = get_freqs_and_lags()
    bs_agg = AggregateBiosounds.load(os.path.join(data_dir, 'aggregate', 'biosound.h5'))

    wdf,Wsq,Wsq_by_freq,Wsq_by_reg = get_encoder_weights_squared(agg, decomp)

    print 'Wsq_by_freq.shape=',Wsq_by_freq.shape
    Wsq_by_freq,aprop_order = reorder_by_row_sum(Wsq_by_freq)

    figsize = (23, 13)
    fig = plt.figure(figsize=figsize)
    fig.subplots_adjust(top=0.95, bottom=0.02, right=0.99, left=0.05, hspace=0.25, wspace=0.25)
    gs = plt.GridSpec(100, 100)

    pwidth = 45

    # plot the performance boxplots by frequency
    freqs = list(sorted(wdf.freq.unique()))
    ax = plt.subplot(gs[55:95, 62:98])
    plot_lfp_and_spike_perf_boxplot(agg, ax, lfp_decomp=decomp)

    """
    # plot the average encoder effect sizes
    ax = plt.subplot(gs[45:100, :pwidth])
    plt.imshow(Wsq_by_freq, origin='upper', interpolation='nearest', aspect='auto', vmin=0, cmap=magma)
    plt.xticks(range(len(freqs)), ['%d' % int(f) for f in freqs])
    aprop_lbls = [ACOUSTIC_PROP_NAMES[USED_ACOUSTIC_PROPS[k]] for k in aprop_order]
    plt.yticks(range(len(aprop_lbls)), aprop_lbls)
    plt.xlabel('LFP Frequency (Hz)')
    plt.colorbar(label='Normalized Encoder Effect')
    """

    fname = os.path.join(get_this_dir(), 'average_encoder_weights.svg')
    plt.savefig(fname, facecolor='w', edgecolor='none')

    plt.show()
Ejemplo n.º 2
0
def draw_joint_relationships(data_dir='/auto/tdrive/mschachter/data'):

    big3_file = os.path.join(data_dir, 'aggregate', 'acoustic_big3.csv')

    if not os.path.exists(big3_file):
        bs_file = os.path.join(data_dir, 'aggregate', 'biosound.h5')
        bs_agg = AggregateBiosounds.load(bs_file)

        aprops = ['maxAmp', 'sal', 'meanspect']
        Xz, good_indices = bs_agg.remove_duplicates(aprops=aprops, thresh=np.inf)
        stim_types = bs_agg.df.stim_type[good_indices].values

        pdata = {'maxAmp': Xz[:, 0], 'sal': Xz[:, 1], 'meanspect': Xz[:, 2], 'stim_type': stim_types}
        df = pd.DataFrame(pdata)
        df.to_csv(big3_file, header=True, index=False)
    else:
        df = pd.read_csv(big3_file)

    print '# of syllables used: %d' % len(df)

    plt.figure()
    ax = plt.subplot(111, projection='3d')

    maxAmp_rescaled = df.maxAmp
    maxAmp_rescaled -= maxAmp_rescaled.min()
    maxAmp_rescaled = np.log(maxAmp_rescaled + 1e-6)
    maxAmp_rescaled -= maxAmp_rescaled.mean()
    maxAmp_rescaled /= maxAmp_rescaled.std(ddof=1)

    for ct in DECODER_CALL_TYPES:
        i = df.stim_type == ct
        if i.sum() == 0:
            continue

        x = maxAmp_rescaled[i]
        y = df.sal[i].values
        z = df.meanspect[i].values
        c = CALL_TYPE_COLORS[ct]
        ax.scatter(x, y, z, s=49, c=c)

    ax.set_xlabel('Amplitude')
    ax.set_ylabel('Saliency')
    ax.set_zlabel('Mean Frequency')

    leg = custom_legend([CALL_TYPE_COLORS[ct] for ct in DECODER_CALL_TYPES], DECODER_CALL_TYPES)
    plt.legend(handles=leg)

    plt.figure()
    nrows = 1
    ncols = 3

    call_type_order = ['song', 'Te', 'DC', 'Be', 'LT', 'Ne', 'Ag', 'Di', 'Th']

    # plot amplitude vs saliency
    ax = plt.subplot(nrows, ncols, 1)
    for ct in call_type_order:
        i = df.stim_type == ct
        if i.sum() == 0:
            continue
        x = maxAmp_rescaled[i]
        y = df.sal[i].values
        c = CALL_TYPE_COLORS[ct]
        ax.scatter(x, y, s=49, c=c, alpha=0.7)
    plt.xlabel('Maximum Amplitude')
    plt.ylabel('Saliency')
    plt.xlim(-3, 2)

    # plot amplitude vs meanspect
    ax = plt.subplot(nrows, ncols, 2)
    for ct in call_type_order:
        i = df.stim_type == ct
        if i.sum() == 0:
            continue
        x = maxAmp_rescaled[i]
        y = df.meanspect[i].values
        c = CALL_TYPE_COLORS[ct]
        ax.scatter(x, y, s=49, c=c, alpha=0.7)
    plt.xlabel('Maximum Amplitude')
    plt.ylabel('Mean Spectral Freq')
    plt.xlim(-3, 2)

    # plot saliency vs meanspect
    ax = plt.subplot(nrows, ncols, 3)
    for ct in call_type_order:
        i = df.stim_type == ct
        if i.sum() == 0:
            continue
        x = df.sal[i].values
        y = df.meanspect[i].values
        c = CALL_TYPE_COLORS[ct]
        ax.scatter(x, y, s=49, c=c, alpha=0.7)
    plt.xlabel('Saliency')
    plt.ylabel('Mean Spectral Freq')

    plt.show()
Ejemplo n.º 3
0
        plot_spectrogram(sprops['spec_t'][si:ei], sprops['spec_freq'], sprops['spec'][:, si:ei], ax=ax,
                         colormap='SpectroColorMap', colorbar=False)
        plt.title('%d_%d (%s), fund=%0.2f' % (stim_id, stim_order, stim_type, f))

        fi = yes_fund_i[k]
        f = funds[fi]
        stim_id, stim_order, stim_type = stims[fi]
        sprops = get_syllable_props(agg, stim_id, stim_order, data_dir)

        ax = plt.subplot(gs[1, k])
        si = sprops['spec_si']
        ei = sprops['spec_ei']
        plot_spectrogram(sprops['spec_t'][si:ei], sprops['spec_freq'], sprops['spec'][:, si:ei], ax=ax,
                         colormap='SpectroColorMap', colorbar=False)
        plt.title('%d_%d (%s), fund=%0.2f' % (stim_id, stim_order, stim_type, f))

    plt.show()

if __name__ == '__main__':

    set_font()

    agg_file = '/auto/tdrive/mschachter/data/aggregate/biosound.h5'
    agg = AggregateBiosounds.load(agg_file)

    # plot_syllable_comps(agg)
    plot_acoustic_stats(agg)

    # plot_nofund(agg)

Ejemplo n.º 4
0
def plot_avg_pairwise_encoder_weights(agg, data_dir='/auto/tdrive/mschachter/data'):

    freqs,lags = get_freqs_and_lags()
    bs_agg = AggregateBiosounds.load(os.path.join(data_dir, 'aggregate', 'biosound.h5'))

    print agg.df.decomp.unique()
    decomp = 'full_psds+full_cfs'
    i = agg.df.decomp == decomp
    assert i.sum() > 0

    W = list()
    wdata = {'lag':list(), 'xindex':list()}
    nlags = len(lags)

    g = agg.df[i].groupby(['bird', 'block', 'segment', 'hemi'])
    for (bird,block,seg,hemi),gdf in g:
        assert len(gdf) == 1

        wkey = gdf['wkey'].values[0]
        index2electrode = agg.index2electrode[wkey]

        eperf = agg.encoder_perfs[wkey]
        eweights = agg.encoder_weights[wkey]

        # site = '%s_%s_%s_%s' % (bird, block, seg, hemi)

        for k,e1 in enumerate(index2electrode):
            for j in range(k):
                for l,lag in enumerate(lags):
                    w = eweights[k, j, l, :]
                    wdata['lag'].append(int(lag))
                    wdata['xindex'].append(len(W))
                    W.append(w)

    wdf = pd.DataFrame(wdata)
    W = np.array(W)
    W[np.isnan(W)] = 0.
    W2 = W**2

    print 'nlags=%d' % nlags

    W2_by_lag = np.zeros([len(USED_ACOUSTIC_PROPS), nlags])
    for l,lag in enumerate(lags):
        i = wdf.lag == int(lag)
        assert i.sum() > 0
        ii = wdf.xindex[i].values
        W2_by_lag[:, l] = W2[ii, :].mean(axis=0)

    plot = True
    if plot:
        plt.figure()
        # ax = plt.subplot(1, 3, 1)
        absmax = W2_by_lag.max()
        plt.imshow(W2_by_lag, interpolation='nearest', aspect='auto', cmap=magma, vmin=0, vmax=absmax)
        plt.colorbar()
        plt.title('Mean Weights')
        plt.yticks(np.arange(len(USED_ACOUSTIC_PROPS)), USED_ACOUSTIC_PROPS)
        plt.xticks(np.arange(nlags), ['%d' % x for x in lags])

        plt.show()

    return lags,W2_by_lag,None