Exemplo n.º 1
0
def test_baseline():

    d = Dataset(micromed_file)
    ev = [ev['start'] for ev in d.read_markers()][1]
    chans = d.header['chan_name'][:2]
    data = d.read_data(events=ev, pre=1, post=1, chan=chans)

    time_interval = (-.5, -.1)
    out = apply_baseline(data, time=time_interval)
    mout = math(select(out, time=time_interval), operator_name='mean', axis='time')
    assert_array_almost_equal(mout(trial=0), array([1, 1]))

    out = apply_baseline(data, time=time_interval, baseline='zscore')
    mout = math(select(out, time=time_interval), operator_name='mean', axis='time')
    assert_array_almost_equal(mout(trial=0), array([0, 0]))

    out = apply_baseline(data, time=time_interval, baseline='percent')
    mout = math(select(out, time=time_interval), operator_name='mean', axis='time')
    assert_array_almost_equal(mout(trial=0), array([0, 0]))

    freq_interval = (10, 15)
    freq = frequency(data)
    out = apply_baseline(freq, freq=freq_interval, baseline='dB')
    mout = math(select(out, freq=freq_interval), operator_name='mean', axis='freq')
    assert_array_almost_equal(mout(trial=0), array([0, 0]))

    out = apply_baseline(freq, freq=freq_interval, baseline='normchange')
    assert out.data[0].max() <= 1
    assert out.data[0].min() >= -1
Exemplo n.º 2
0
def test_select_interval_invert():
    data1 = select(data, time=(0.2, 0.5), invert=True)
    assert data1.number_of('time')[0] == data.number_of('time')[0] - 76
    assert data1.data[0].shape[1] == data.number_of('time')[0] - 76
    assert data1.data[-1].shape[1] == data.number_of('time')[0] - 76

    data2 = select(data1, time=(0.2, 0.5), invert=True)
    assert data1.number_of('trial') == data2.number_of('trial')
Exemplo n.º 3
0
def test_select_trial():

    data1 = select(data, trial=(1, 2))
    assert data1.number_of('trial') == 2

    data1 = select(data, trial=(0, 0))
    assert_array_equal(data1.data[0], data1.data[1])
    assert len(data1.axis['chan']) == 2
    assert data1.number_of('trial') == 2
Exemplo n.º 4
0
def correct_baseline(freq_A, freq_B, frequency):
    move = select(freq_A, freq=frequency)
    rest = select(freq_B, freq=frequency)

    merged = merge_datasets(move, rest)
    merged = concatenate(merged, 'time')
    baseline = math(merged, operator_name='mean', axis='time')

    move.data[0] /= baseline.data[0][:, None, :]
    rest.data[0] /= baseline.data[0][:, None, :]
    return move, rest
Exemplo n.º 5
0
def test_select_oneside_interval_both():
    data1 = select(data, time=(None, None))

    assert len(data1.axis['time'][0]) == len(data.axis['time'][0])
    assert data1.data[0].shape[1] == data.data[0].shape[1]
    assert data1.axis['time'][0][0] == data.axis['time'][0][0]
    assert data1.axis['time'][0][-1] == data.axis['time'][0][-1]
Exemplo n.º 6
0
def test_select_oneside_interval_1():
    data1 = select(data, time=(0.5, None))

    assert len(data1.axis['time'][0]) * 2 == len(data.axis['time'][0])
    assert data1.data[0].shape[1] * 2 == data.data[0].shape[1]
    assert data1.axis['time'][0][0] >= 0.5
    assert data1.axis['time'][0][-1] == data.axis['time'][0][-1]
Exemplo n.º 7
0
def test_select_oneside_interval_0():
    data1 = select(data, time=(None, 0.5))

    assert len(data1.axis['time'][0]) * 2 == len(data.axis['time'][0])
    assert data1.data[0].shape[1] * 2 == data.data[0].shape[1]
    assert data1.axis['time'][0][0] == 0
    assert data1.axis['time'][0][-1] < .5
Exemplo n.º 8
0
def apply_common_baseline(tf, time, baseline):
    """Concatenate all the baseline periods for all the conditions. Keep it
    separate for each channel and each frequency"""

    tf_time = select(tf, time=time)

    X = moveaxis(tf_time.data[0], 1, 2)
    X = X.reshape((X.shape[0], X.shape[1], -1))

    bline_mean = nanmean(X, axis=-1)
    bline_std = nanstd(X, axis=-1)

    if len(tf.list_of_axes) == 4:
        bline_m = bline_mean[:, None, :, None]
        bline_sd = bline_std[:, None, :, None]
    else:
        bline_m = bline_mean[:, None, :]
        bline_sd = bline_std[:, None, :]

    if baseline == 'zscore':
        tf.data[0] = (tf.data[0] - bline_m) / bline_sd
    elif baseline == 'dB':
        tf.data[0] = 10 * log10(tf.data[0] / bline_m)

    return tf
Exemplo n.º 9
0
def select_significant_channels(data, onsets, threshold=0.0005):
    assert NotImplementedError
    """Select channels that show a significant difference between the period
    before onset and the period after the onset.

    Parameters
    ----------
    data : instance of wonambi.data
        continuous data (already converted to z-score or dB)
    onsets : array
        array of
    threshold : float
        p-value to consider it significant

    Returns
    -------
    list of str
        list of significant array
    """
    PRESTIM = 1
    POSTSTIM = 1
    v_pre = []
    v_post = []
    for on in onsets:
        d = select(data, time=(on - PRESTIM, on))
        v_pre.append(d(trial=0, trial_axis='trial000000').mean(axis=1))
        d = select(data, time=(on, on + POSTSTIM))
        v_post.append(d(trial=0, trial_axis='trial000000').mean(axis=1))

    v_pre = array(v_pre)
    v_post = array(v_post)

    artifact = isnan(v_pre[:, 0]) | isnan(v_post[:, 0])
    res = ttest_rel(v_post[~artifact, :], v_pre[~artifact, :], axis=0)

    i_significant = (res.pvalue <= threshold)
    significant_chan = data.chan[0][i_significant]

    out = []
    for i in where(i_significant)[0]:
        out.append(f'{data.chan[0][i]:<6}:{res.pvalue[i]: .3f}')
    lg.info('; '.join(out))

    return significant_chan
Exemplo n.º 10
0
def compute_quick_spectrogram(ieeg_file, freq):
    with ieeg_file.open('rb') as f:
        data = load(f)

    # TODO: from parameters
    reref = 'average'
    method = 'spectrogram'
    duration = 2
    data = montage(data, ref_to_avg=True, method=reref)
    tf = timefrequency(data, method=method, duration=duration)

    dat0 = math(select(tf, freq=freq), operator_name='mean', axis='freq')
    return dat0
Exemplo n.º 11
0
def estimate_ieeg_prf(ieeg_file, method, freq=(60, 80)):
    with ieeg_file.open('rb') as f:
        data = load(f)

    stimuli = data.attr['stimuli']

    data = select(data, freq=freq)
    data = math(data, operator_name='mean', axis='time')
    data = math(data, operator_name='mean', axis='freq')
    data = concatenate(data, 'trial')

    compute_prf(ieeg_file, data.data[0], data.chan[0], stimuli, method)

    return replace_extension(ieeg_file, 'prf.tsv')
Exemplo n.º 12
0
def test_select_typeerror():
    with raises(TypeError):
        select(data, trial=1)

    with raises(TypeError):
        select(chan='chan01')

    with raises(TypeError):
        select(data, time=1, chan=('chan01', ))
Exemplo n.º 13
0
def apply_baseline_to_continuous(parameters, tf, onsets):

    v = []
    for on in onsets:
        x = select(tf, time=on + parameters['spectrum']['baseline']['time'])
        v.append(x.data[0])

    A = concatenate(v, axis=1)
    bline_m = nanmean(A, axis=1)[:, :, None]
    bline_sd = nanstd(A, axis=1)[:, :, None]

    if parameters['spectrum']['baseline']['type'] == 'zscore':
        tf.data[0] = (tf.data[0] - bline_m) / bline_sd
    elif parameters['spectrum']['baseline']['type'] == 'dB':
        tf.data[0] = 10 * log10(tf.data[0] / bline_m)

    return tf
Exemplo n.º 14
0
def find_activity_per_finger(subject, run, event_type='cues'):
    """
    TODO
    ----
    this function could be rewritten using preexisting code
    """
    data, events = load('data', subject, run, event_type=event_type)
    tf = compute_timefreq(data, mean=True)
    tf = get_chantime(tf)
    best_time = find_max_point(tf)[1]

    tf = compute_timefreq(data, mean=False)
    tf = get_chan(tf, time=best_time)

    values = {}
    for f in FINGERS:
        i_finger = create_bool(events, f)
        tf_finger = select(tf, trial_axis=i_finger)
        tf_finger_chan = math(tf_finger, operator_name='mean', axis='trial_axis')
        values[f] = tf_finger_chan(trial=0)

    v = c_[values['thumb'], values['index'], values['middle'], values['ring'], values['little']]
    return v, tf.chan[0]
Exemplo n.º 15
0
def timeseries_ieeg(parameters):

    ieeg_dir = parameters['paths']['output'] / 'workflow' / 'ieeg'
    subject = parameters['plot']['subject']
    freq = parameters['ieeg']['ecog_compare']['frequency_bands'][-1]

    ieeg_subj_dir = ieeg_dir / f'_subject_{subject}'

    ieeg_compare_file = next(
        ieeg_subj_dir.glob(
            f'_frequency_{freq[0]}.{freq[1]}/ecog_compare/sub-*_compare.tsv'))
    ieeg0_file = next(ieeg_subj_dir.rglob('*_task-motoractive_*_ieeg.pkl'))
    ieeg1_file = next(ieeg_subj_dir.rglob('*_task-motorbaseline_*_ieeg.pkl'))

    subj_dir = parameters['paths']['input'] / f'sub-{subject}'
    events_ieeg_file = next(subj_dir.glob('ses-*/ieeg/*_events.tsv'))

    dat0 = compute_quick_spectrogram(ieeg0_file, freq)
    dat1 = compute_quick_spectrogram(ieeg1_file, freq)

    dat = dat0._copy()
    dat.data = concat([dat0.data, dat1.data])
    dat.axis['time'] = concat([dat0.time, dat1.time])
    dat.axis['chan'] = concat([dat0.chan, dat1.chan])

    dat = concatenate(dat, axis='time')

    ieeg_compare = read_tsv(ieeg_compare_file)

    chans = ieeg_compare['channel'][ieeg_compare['measure'] > 10]
    x0 = math(select(dat, chan=chans), operator_name='mean', axis='chan')

    t = dat.time[0]
    x = x0(trial=0)

    i_t = argsort(t)

    events = read_tsv(events_ieeg_file)
    i_start = where(events['trial_type'] == 'rest')[0][0]
    offset = events['onset'][i_start]
    events['onset'] -= offset

    traces = [
        go.Scatter(x=t[i_t] - offset, y=x[i_t], line=dict(color='black', )),
    ]

    layout = merge(
        LAYOUT,
        dict(
            height=100,
            width=450,
            xaxis=dict(
                tick0=0,
                dtick=30,
                range=(0, 330),
            ),
            yaxis=dict(
                dtick=0.1,
                range=(0, 0.4),
            ),
            shapes=event_shapes(events),
        ),
    )

    fig = go.Figure(data=traces, layout=layout)

    return fig
Exemplo n.º 16
0
def merge(freq, method, frequency):

    freq = select(freq, freq=frequency)

    if method == '1a':
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='mean', axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        # only one value
        out = Data(freq.data[0][:, None], freq.s_freq, chan=freq.chan[0], time=(0, ))

    elif method == '1b':
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='dB')
        freq = math(freq, operator_name='mean', axis='freq')
        freq = math(freq, operator_name='mean', axis='time')
        # only one value
        out = Data(freq.data[0][:, None], freq.s_freq, chan=freq.chan[0], time=(0, ))

    elif method == '1c':
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        freq = math(freq, operator_name='dB')
        freq = math(freq, operator_name='mean', axis='time')
        # only one value
        out = Data(freq.data[0][:, None], freq.s_freq, chan=freq.chan[0], time=(0, ))

    elif method == '1d':
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        freq = math(freq, operator_name='mean', axis='time')
        freq = math(freq, operator_name='dB')
        # only one value
        out = Data(freq.data[0][:, None], freq.s_freq, chan=freq.chan[0], time=(0, ))

    elif method == '2a':
        freq = math(freq, operator_name='mean', axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        # one value per trial
        out = concatenate(freq, axis='trial')

    elif method == '2b':
        freq = math(freq, operator_name='dB')
        freq = math(freq, operator_name='mean', axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        # one value per trial
        out = concatenate(freq, axis='trial')

    elif method == '2c':
        freq = math(freq, operator_name='mean', axis='time')
        freq = math(freq, operator_name='dB')
        freq = math(freq, operator_name='mean', axis='freq')
        # one value per trial
        out = concatenate(freq, axis='trial')

    elif method == '2d':
        freq = math(freq, operator_name='mean', axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        freq = math(freq, operator_name='dB')
        # one value per trial
        out = concatenate(freq, axis='trial')

    elif method == '3a':
        freq = concatenate(freq, axis='time')
        # values per time point
        out = math(freq, operator_name='mean', axis='freq')

    elif method == '3b':
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='dB')
        # values per time point
        out = math(freq, operator_name='mean', axis='freq')

    elif method == '3c':
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='mean', axis='freq')
        # values per time point
        out = math(freq, operator_name='dB')

    elif method == 'dh2012':
        # identical to 3b, but use log instead of dB
        freq = concatenate(freq, axis='time')
        freq = math(freq, operator_name='log')
        # values per time point
        out = math(freq, operator_name='mean', axis='freq')

    return out
Exemplo n.º 17
0
def test_select_trials_and_string_invert():
    data1 = select(data, trial=(1, 4), chan=('chan01', 'chan02'), invert=True)
    assert len(data1.axis['chan']) == data.number_of('trial') - 2
    assert len(data1.axis['chan'][0]) == data.number_of('chan')[0] - 2
Exemplo n.º 18
0
def test_select_interval():
    data1 = select(data, time=(0.2, 0.5))

    assert data1.axis['time'][0].shape[0] == 76
    assert data1.data[0].shape[1] == 76
    assert data1.data[-1].shape[1] == 76
Exemplo n.º 19
0
def test_select_trials_and_string():
    data1 = select(data, trial=(1, 4), chan=('chan01', 'chan02'))
    assert len(data1.axis['chan']) == 2
    assert len(data1.axis['chan'][0]) == 2
Exemplo n.º 20
0
def test_select_trials():
    data1 = select(data, trial=(1, 4))
    assert_array_equal(data.data[1], data1.data[0])
Exemplo n.º 21
0
def test_select_empty_selection():
    data1 = select(data, chan=[])
    assert len(data1.axis['chan'][0]) == 0
    assert data1.data[0].shape[0] == 0
Exemplo n.º 22
0
def test_select_string_selection():
    data1 = select(data, chan=['chan02'])
    assert data1.axis['chan'][0][0] == 'chan02'
    assert data1.data[0].shape[0] == 1
Exemplo n.º 23
0
def test_select_interval_not_in_data():
    data1 = select(data, time=(10.2, 10.5))

    assert len(data1.axis['time'][0]) == 0
    assert data1.data[0].shape[1] == 0
    assert data1.data[-1].shape[1] == 0