Exemplo n.º 1
0
def plot_erp_specgrams(
        d,
        samplerate=None,
        NFFT=256,
        freq_range=[0.1, 50],
        fig=None):
    assert d.data.ndim == 3
    assert d.feat_lab is not None

    if fig is None:
        fig = plot.figure()

    tf = trials.trial_specgram(d, samplerate, NFFT)
    tf_erp = np.mean(tf.d, axis=3)
    
    ch_labs = tf.feat_lab[0]

    freqs = np.array([float(x) for x in tf.feat_lab[1]])
    times = np.array([float(x) for x in tf.feat_lab[2]])

    selection = np.logical_and(freqs >= freq_range[0], freqs <= freq_range[1])
    freqs = freqs[selection]
    tf_erp = tf_erp[:,selection,:]
    clim = (-np.max(np.abs(tf_erp)), np.max(np.abs(tf_erp)))

    num_channels = tf_erp.shape[0]
    num_cols = max(1, num_channels/8)
    num_rows = min(num_channels, 8)

    fig.subplots_adjust(hspace=0)

    for channel in range(num_channels):
        s = tf_erp[channel,:,:]

        col = channel / num_rows
        row = channel % num_rows
        ax = plot.subplot(num_rows, num_cols, num_cols*row+col+1)
        im = plot.imshow(
            np.flipud(s), aspect='auto',
            extent=[np.min(times), np.max(times), np.min(freqs), np.max(freqs)],
        )

        plot.ylim(freq_range[0], freq_range[1])
        plot.clim(clim)
        plot.ylabel(ch_labs[channel])

        if row == num_rows-1 or channel == num_channels-1:
            plot.xlabel('Time (s)')
        else:
            ax.get_xaxis().set_visible(False)

    cax = fig.add_axes([0.91, 0.1, 0.01, 0.8])
    fig.colorbar(im, cax=cax)
    return fig
Exemplo n.º 2
0
def plot_erp_specdiffs(
        d,
        samplerate=None,
        NFFT=256,
        freq_range=[0.1, 50],
        classes=[0,1],
        significant_only=False,
        pval=0.05,
        fig=None):
    assert d.data.ndim == 3
    assert len(classes) == 2
    assert d.feat_lab is not None

    if fig is None:
        fig = plot.figure()

    tf = trials.trial_specgram(d, samplerate, NFFT)
    tf_erp = trials.erp(tf)
    diff = np.log(tf_erp.data[...,classes[0]]) - np.log(tf_erp.data[...,classes[1]])

    if significant_only:
        _,ps = scipy.stats.ttest_ind(tf.get_class(classes[0]).data,
                                     tf.get_class(classes[1]).data, axis=3)
        diff[ps > pval] = 0
    
    ch_labs = tf_erp.feat_lab[0]
    freqs = np.array([float(x) for x in tf_erp.feat_lab[1]])
    times = np.array([float(x) for x in tf_erp.feat_lab[2]])

    selection = np.logical_and(freqs >= freq_range[0], freqs <= freq_range[1])
    freqs = freqs[selection]
    diff = diff[:,selection]
    clim = (-np.max(np.abs(diff)), np.max(np.abs(diff)))

    num_channels = d.data.shape[0]
    num_cols = max(1, num_channels/8)
    num_rows = min(num_channels, 8)

    fig.subplots_adjust(hspace=0)

    cdict = {'red': ((0.0, 1.0, 1.0),
                     (0.5, 1.0, 1.0),
                     (1.0, 0.0, 0.0)),
             'green': ((0.0, 0.0, 0.0),
                       (0.5, 1.0, 1.0),
                       (1.0, 0.0, 0.0)),
             'blue': ((0.0, 0.0, 0.0),
                      (0.5, 1.0, 1.0),
                      (1.0, 1.0, 1.0))}

    cmap = matplotlib.colors.LinearSegmentedColormap('polarity',cdict,256)

    for channel in range(num_channels):
        s = diff[channel,:,:]

        col = channel / num_rows
        row = channel % num_rows
        ax = plot.subplot(num_rows, num_cols, num_cols*row+col+1)
        im = plot.imshow(
            s, aspect='auto',
            extent=[np.min(times), np.max(times), np.min(freqs), np.max(freqs)],
            cmap=cmap
        )

        plot.ylim(freq_range[0], freq_range[1])
        plot.clim(clim)
        plot.ylabel(ch_labs[channel])

        ax.xaxis.grid(True, color='w', which='major')
        ax.yaxis.grid(False)

        if row == num_rows-1 or channel == num_channels-1:
            plot.xlabel('Time (s)')
        else:
            [label.set_visible(False) for label in ax.get_xticklabels()]
            [tick.set_visible(False) for tick in ax.get_xticklines()]

    cax = fig.add_axes([0.91, 0.1, 0.01, 0.8])
    fig.colorbar(im, cax=cax)
    return fig