예제 #1
0
파일: utils.py 프로젝트: bgauthie/unicog
def analyze_average(data,
                    onsets,
                    sampling_interval,
                    len_et=12,
                    offset=-2,
                    y_label="Bold signal",
                    time_unit='s'):

    # Times series initialization
    ts = timeseries.TimeSeries(data,
                               sampling_interval=sampling_interval,
                               time_unit=time_unit)

    # Events initialization
    events = timeseries.Events(onsets, time_unit=time_unit)

    # Timeseries analysis
    #len_et = numbre of TR what you want to see
    #offset = number of offset including in len_et
    analyzer = analysis.EventRelatedAnalyzer(ts,
                                             events,
                                             len_et=len_et,
                                             offset=offset)

    return analyzer
예제 #2
0
def plot_event(signal_filt, trg_ts, std_ts, kernel, infile):
    """Plot peri-stimulus timecourse of each event type as well as the 
    canonical pupil response function"""
    outfile = pupil_utils.get_outfile(infile, '_PSTCplot.png')
    plt.ioff()
    all_events = std_ts.data + (trg_ts.data * 2)
    all_events_ts = ts.TimeSeries(all_events, sampling_rate=30., time_unit='s')
    all_era = nta.EventRelatedAnalyzer(signal_filt,
                                       all_events_ts,
                                       len_et=75,
                                       correct_baseline=True)
    fig, ax = plt.subplots()
    viz.plot_tseries(all_era.eta, yerror=all_era.ets, fig=fig)
    ax.plot((all_era.eta.time * (10**-12)), kernel)
    ax.legend(['Standard', 'Target', 'Pupil IRF'])
    fig.savefig(outfile)
    plt.close(fig)
예제 #3
0
def plot_event(signal_filt,
               con_ts,
               incon_ts,
               neut_ts,
               kernel,
               infile,
               plot_kernel=True):
    """Plot peri-stimulus timecourse of each event type as well as the 
    canonical pupil response function"""
    outfile = pupil_utils.get_proc_outfile(infile, '_PSTCplot.png')
    plt.ioff()
    all_events = con_ts.data + (incon_ts.data * 2) + (neut_ts.data * 3)
    all_events_ts = ts.TimeSeries(all_events, sampling_rate=30., time_unit='s')
    all_era = nta.EventRelatedAnalyzer(signal_filt,
                                       all_events_ts,
                                       len_et=90,
                                       correct_baseline=False)
    fig, ax = plt.subplots()
    viz.plot_tseries(all_era.eta, yerror=all_era.ets, fig=fig)
    if plot_kernel:
        ax.plot((all_era.eta.time * (10**-12)), kernel)
    ax.legend(['Congruent', 'Incongruent', 'Neutral'])
    fig.savefig(outfile)
    plt.close(fig)
예제 #4
0
condition = behavioral['name']
onset = behavioral['onset']
import pandas as pd
events = pd.DataFrame({'onset': onset, 'trial_type': condition})

# from nilearn.image import index_img
# func_img=index_img(func_img, onset)
# data=func_img.get_data()
#------------------------------------------------------------------------
# Create the mask image
#------------------------------------------------------------------------
#------------------------------------------------------------------------
TR = 1.
len_et = 494
n_jobs = -1

t1 = ts.TimeSeries(func_img, sampling_interval=TR)

t2 = ts.TimeSeries(onset, sampling_interval=TR)

E = nta.EventRelatedAnalyzer(t1, t2, len_et, n_jobs)

# fig01=viz.plot_tseries(E.eta, ylabel='BOLD(% signal'
#                                    'change)', yerror=E.ets)

fig02 = viz.plot_tseries(E.FIR, ylabel='BOLD (% signal change)')

fig03 = viz.plot_tseries(E.xcorr_eta, ylabel='BOLD (% signal change)')

plt.show()
예제 #5
0
Note that the time-representation will not change if we now convert the
time-unit into ms. The only thing this accomplishes is to use this time-unit in
subsequent visualization of the resulting time-series

"""

stim_time_series.time_unit = 'ms'

"""

Next, we initialize an EventRelatedAnalyzer:

"""

event_related = tsa.EventRelatedAnalyzer(stim_time_series,
                                         spike_ev,
                                         len_et=200,
                                         offset=-200)

"""

The actual STA gets calculated in this line (the call to 'event_related.eta')
and the result gets input directly into the plotting function:

"""

fig01 = viz.plot_tseries(event_related.eta, ylabel='Amplitude (dB SPL)')

"""

We prettify the plot a bit by adding a dashed line at the mean of the stimulus
예제 #6
0
def test_EventRelatedAnalyzer():
    cycles = 10
    l = 1024
    unit = 2 * np.pi / l
    t = np.arange(0, 2 * np.pi + unit, unit)
    signal = np.sin(cycles * t)
    events = np.zeros(t.shape)
    # Zero crossings:
    idx = np.where(np.abs(signal) < 0.03)[0]
    # An event occurs at the beginning of every cycle:
    events[idx[:-2:2]] = 1
    # and another kind of event at the end of each cycle:
    events[idx[1:-1:2]] = 2

    T_signal = ts.TimeSeries(signal, sampling_rate=1)
    T_events = ts.TimeSeries(events, sampling_rate=1)
    for correct_baseline in [True, False]:
        ETA = nta.EventRelatedAnalyzer(T_signal,
                                       T_events,
                                       l / (cycles * 2),
                                       correct_baseline=correct_baseline).eta
        # This should hold
        npt.assert_almost_equal(ETA.data[0], signal[:ETA.data.shape[-1]], 3)
        npt.assert_almost_equal(ETA.data[1], -1 * signal[:ETA.data.shape[-1]],
                                3)

    # Same should be true for the FIR analysis:
    FIR = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).FIR
    npt.assert_almost_equal(FIR.data[0], signal[:FIR.data.shape[-1]], 3)
    npt.assert_almost_equal(FIR.data[1], -1 * signal[:FIR.data.shape[-1]], 3)

    # Same should be true for
    XCORR = nta.EventRelatedAnalyzer(T_signal, T_events,
                                     l / (cycles * 2)).xcorr_eta
    npt.assert_almost_equal(XCORR.data[0], signal[:XCORR.data.shape[-1]], 3)
    npt.assert_almost_equal(XCORR.data[1], -1 * signal[:XCORR.data.shape[-1]],
                            3)

    # More dimensions:
    T_signal = ts.TimeSeries(np.vstack([signal, signal]), sampling_rate=1)
    T_events = ts.TimeSeries(np.vstack([events, events]), sampling_rate=1)
    ETA = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).eta

    # The events input and the time-series input have different dimensions:
    T_events = ts.TimeSeries(events, sampling_rate=1)
    ETA = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).eta
    npt.assert_almost_equal(ETA.data[0][0], signal[:ETA.data.shape[-1]], 3)
    npt.assert_almost_equal(ETA.data[1][1], -1 * signal[:ETA.data.shape[-1]],
                            3)

    # Input is an Events object, instead of a time-series:
    ts1 = ts.TimeSeries(np.arange(100), sampling_rate=1)
    ev = ts.Events([10, 20, 30])
    et = nta.EventRelatedAnalyzer(ts1, ev, 5)

    # The five points comprising the average of the three sequences:
    npt.assert_equal(et.eta.data, [20., 21., 22., 23., 24.])

    ts2 = ts.TimeSeries(np.arange(200).reshape(2, 100), sampling_rate=1)
    ev = ts.Events([10, 20, 30])
    et = nta.EventRelatedAnalyzer(ts2, ev, 5)

    npt.assert_equal(
        et.eta.data,
        [[20., 21., 22., 23., 24.], [120., 121., 122., 123., 124.]])

    # The event-triggered SEM should be approximately zero:
    for correct_baseline in [True, False]:
        EA = nta.EventRelatedAnalyzer(T_signal,
                                      T_events,
                                      l / (cycles * 2),
                                      correct_baseline=correct_baseline)

        npt.assert_almost_equal(EA.ets.data[0],
                                np.zeros_like(EA.ets.data[0]),
                                decimal=2)
    # Test the et_data method:
    npt.assert_almost_equal(EA.et_data[0][0].data[0],
                            signal[:ETA.data.shape[-1]])

    # Test that providing the analyzer with an array, instead of an Events or a
    # TimeSeries object throws an error:
    with pytest.raises(ValueError) as e_info:
        nta.EventRelatedAnalyzer(ts2, events, 10)

    # This is not yet implemented, so this should simply throw an error, for
    # now:
    with pytest.raises(NotImplementedError) as e_info:
        nta.EventRelatedAnalyzer.FIR_estimate(EA)