def test_montage_bipolar_02():
    data_wrongorder = create_data(n_trial=2, attr=[
        'chan',
    ])
    data_wrongorder.axis['chan'][1] = data_wrongorder.chan[0][-1::-1]

    with raises(ValueError):
        montage(data_wrongorder, bipolar=100)
def test_montage_bipolar_03():
    data_wrongorder = create_data(attr=[
        'chan',
    ])

    # you should get an error if chan is not first
    data_wrongorder.axis = OrderedDict([
        ('time', data_wrongorder.axis['time']),
        ('chan', data_wrongorder.axis['chan']),
    ])

    with raises(ValueError):
        montage(data_wrongorder, bipolar=100)
示例#3
0
文件: ieeg.py 项目: gpiantoni/fima
def hide_artifacts(parameters, data):

    data_comparison = montage(data, ref_to_avg=True, method='median')

    count_samples = []

    bad_smp = int(data.s_freq * parameters['read']['artifacts']['window'] / 2)

    for i_trl in range(data.number_of('trial')):
        i_bad = abs(data_comparison.data[i_trl]) > parameters['read']['artifacts']['threshold']

        # before
        padded_bad = i_bad.copy()
        for i_roll in range(1, bad_smp):
            padded_bad |= pad(i_bad, ((0, 0), (i_roll, 0)), mode='constant', constant_values=False)[:, :-i_roll]

        # after
        for i_roll in range(bad_smp):
            padded_bad |= pad(i_bad, ((0, 0), (0, i_roll)), mode='constant', constant_values=False)[:, i_roll:]

        data.data[i_trl][padded_bad] = NaN

        count_samples.append(padded_bad.sum(axis=1))

    bad_smp_per_chan = sum(count_samples, axis=0) / data.s_freq

    return data, bad_smp_per_chan
示例#4
0
文件: ieeg.py 项目: gpiantoni/fima
def read_data(parameters, filename, event_onsets, opts, continuous=False):

    d = BIDSEEG(filename)
    chans = d.channels[
        (d.channels['status'] == 'good')
        & (d.channels['type'] == 'ECOG')
        ]

    if continuous:
        data = d.read_data(
            begtime=event_onsets[0] - opts['pre'],
            endtime=event_onsets[-1] + opts['post'],
            chan=list(chans['name']))
    else:
        data = d.read_data(
            events=event_onsets,
            pre=opts['pre'],
            post=opts['post'],
            chan=list(chans['name']))

    if parameters['artifacts']['remove']:
        data, bad_smp_per_chan = hide_artifacts(parameters, data)
        lg.info(f'{filename.stem} bad points {mean(bad_smp_per_chan):.3f}s, s.d. {std(bad_smp_per_chan):.3f}s [{min(bad_smp_per_chan):.3f}-{max(bad_smp_per_chan):.3f}s]')

    data = montage(data, ref_to_avg=True)

    return data
示例#5
0
def preprocess_ecog(ieeg_file, reref, duration, offset, output_dir):
    """

    Parameters
    ----------
    reref : str
        'average' or 'regression'
    duration : int
        length of the segments
    offset : bool
        remove one sample for whole duration

    TODO
    ----
    labels_in_roi = find_labels_in_regions(electrodes, regions)
    clean_roi_labels = [label for label in clean_labels if label in labels_in_roi]
    data = select(data, chan=clean_roi_labels)
    """
    with ieeg_file.open('rb') as f:
        data = load(f)

    data = montage(data, ref_to_avg=True, method=reref)
    data = make_segments(data, duration, offset)

    output_file = output_dir / replace_extension(ieeg_file.name, 'proc.pkl')
    with output_file.open('wb') as f:
        dump(data, f)

    return output_file
def test_montage_bipolar_00():
    bipol = montage(data, bipolar=100)

    assert len(bipol.chan[0]) == 28
    assert_array_equal(
        bipol(trial=0, chan='chan00-chan01'),
        data(trial=0, chan='chan00') - data(trial=0, chan='chan01'))
def test_montage_06():
    reref = montage(data, ref_to_avg=True)

    dat1 = reref(trial=0)
    assert_array_almost_equal(sum(dat1, axis=0),
                              zeros((dat1.shape[1])),
                              decimal=4)
def test_montage_03():
    CHAN = ('chan01', 'chan02')
    reref = montage(data, ref_chan=CHAN)
    dat1 = reref(chan=CHAN)
    assert_array_almost_equal(sum(dat1[0], axis=0),
                              zeros((dat1[0].shape[1])),
                              decimal=6)
示例#9
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
示例#10
0
def plot_raw_overview(filename):
    event_type = 'all'

    if filename.name.startswith('sub-drouwen'):
        CHANS = [f'IH0{x + 1}' for x in range(8)]
    elif filename.name.startswith('sub-itens'):
        CHANS = [f'C0{x + 1}' for x in range(8)]
    elif filename.name.startswith('sub-lemmer'):
        CHANS = [f'IH{x + 1}' for x in range(8)]
    elif filename.name.startswith('sub-som705'):
        CHANS = [f'GA0{x + 1}' for x in range(8)]  # a bit random
    elif filename.name.startswith('sub-ommen'):
        CHANS = ['chan1',
                 'chan2']  # I dont 'understand why I cannot use 'chan64'
    elif filename.name.startswith('sub-vledder') or filename.name.startswith(
            'sub-ommen'):
        CHANS = ['chan1', 'chan64']
    elif '_acq-blackrock_' in filename.name:
        CHANS = ['chan1', 'chan128']
    else:
        print('you need to specify reference channel for this test')
        return None, None

    d = Dataset(filename, bids=True)
    event_names, event_onsets = select_events(d, event_type)

    is_ecog = d.dataset.task.channels.tsv['type'] == 'ECOG'
    is_seeg = d.dataset.task.channels.tsv['type'] == 'SEEG'
    chans = array(d.header['chan_name'])[is_ecog | is_seeg]
    data = d.read_data(begtime=event_onsets[0],
                       endtime=event_onsets[-1],
                       chan=list(chans))
    data.data[0][isnan(data.data[0])] = 0  # ignore nan

    data = montage(data, ref_chan=CHANS)
    freq = frequency(data, taper='hann', duration=2, overlap=0.5)

    hist = make_histogram(data, max=250, step=10)
    divs = []
    fig = plot_hist(hist)
    divs.append(to_div(fig))

    bad_chans = None

    if AUTOMATIC:
        from sklearn.covariance import EllipticEnvelope

        algorithm = EllipticEnvelope(
            contamination=P['data_quality']['histogram']['contamination'])
        prediction = algorithm.fit(hist.data[0]).predict(hist.data[0])
        new_bad_chans = data.chan[0][prediction == -1]
        print('bad channels with histogram / elliptic envelope: ' +
              ', '.join(new_bad_chans))
        bad_chans = set(new_bad_chans)

        fig = plot_outliers(hist.chan[0],
                            algorithm.dist_,
                            prediction,
                            yaxis_title='distance',
                            yaxis_type='log')
        divs.append(to_div(fig))

    fig = plot_freq(freq)
    divs.append(to_div(fig))

    if AUTOMATIC:
        from sklearn.neighbors import LocalOutlierFactor

        algorithm = LocalOutlierFactor(
            n_neighbors=P['data_quality']['spectrum']['n_neighbors'])
        prediction = algorithm.fit_predict(freq.data[0])

        new_bad_chans = data.chan[0][prediction == -1]
        print('bad channels with spectrum / local outlier factor: ' +
              ', '.join(new_bad_chans))
        bad_chans |= set(new_bad_chans)
        fig = plot_outliers(freq.chan[0],
                            algorithm.negative_outlier_factor_,
                            prediction,
                            yaxis_title='distance',
                            yaxis_type='linear')
        divs.append(to_div(fig))

        # we use again the reference channel. Ref channel was handpicked but it might have a weird spectrum
        bad_chans -= set(CHANS)

    return bad_chans, divs
def test_montage_bipolar_01():
    data_nochan = create_data()

    with raises(ValueError):
        montage(data_nochan, bipolar=100)
def test_montage_regression():
    reref = montage(data, ref_to_avg=True, method='regression')
    assert_array_almost_equal(reref.data[0].mean(axis=0), 0)
def test_montage_05():
    with raises(TypeError):
        montage(data, ref_chan=['chan00'], ref_to_avg=True)
def test_montage_04():
    reref = montage(data, ref_chan=[])
    assert_array_equal(data.data[0], reref.data[0])
def test_montage_02():
    reref = montage(data, ref_chan=['chan00'])
    dat1 = reref(chan='chan00')
    assert_array_equal(dat1[0], zeros(dat1[0].shape))
def test_montage_01():
    with raises(TypeError):
        montage(data, ref_chan='chan00')