Example #1
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
Example #2
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')
Example #3
0
def _reject_channels(d, elec_names, cond, minimalduration):
    markers = d.read_markers()
    block_beg = []
    block_end = []
    for mrk in markers:
        if mrk['name'] in cond:
            dur = (mrk['end'] - mrk['start'])
            if dur >= minimalduration:

                block_beg.append(mrk['start'])
                block_end.append(mrk['end'])

    data = d.read_data(chan=elec_names, begtime=block_beg, endtime=block_end)
    data = concatenate(data, 'time')

    clean_labels = reject_channels(data, 3)
    return clean_labels
def test_concatenate_trial():
    data1 = concatenate(data, axis='trial')
    assert data1.number_of('trial') == 1
    assert data1.list_of_axes == ('chan', 'time', 'trial_axis')
def test_concatenate_axis():
    data1 = concatenate(data, axis='time')
    assert data1.number_of(
        'time')[0] == data.number_of('time')[0] * data.number_of('trial')
Example #6
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
Example #7
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