Ejemplo n.º 1
0
def bci_competition_chain(mode, avg_size):
    if mode == 'B':
        if avg_size == 5:
            ch = [
                my_tools.ReadSignal(files=get_files('bci_competition', 'B')),
                my_tools.Montage(montage_type='no_montage'),
                my_tools.LeaveChannels(channels=['C3']),
                my_tools.Filter(
                    **{
                        'wp': 0.3,
                        'ws': 0.1,
                        'gpass': 3.0,
                        'gstop': 30.0,
                        'ftype': 'cheby2',
                        'unit': 'hz'
                    }),
                my_tools.Filter(
                    **{
                        'wp': 15.0,
                        'ws': 25.0,
                        'gpass': 1.0,
                        'gstop': 60.0,
                        'ftype': 'ellip',
                        'unit': 'hz'
                    }),
                my_tools.Segment(classes=('target', 'non-target'),
                                 start_offset=0.15,
                                 duration=0.6),
                my_tools.Average(bin_selectors=[
                    lambda mgr: mgr['name'] == 'target',
                    lambda mgr: mgr['name'] == 'non-target'
                ],
                                 bin_names=['target', 'non-target'],
                                 size=avg_size,
                                 baseline=0.2,
                                 strategy='random'),
                my_tools.Downsample(factor=7),
                my_tools.Normalize(norm=2),
                my_tools.PrepareTrainSet(),
                my_tools.SVM(C=2.0,
                             Cmode='classProb',
                             kernel=ker.Polynomial(2))
            ]
            return ch
        elif avg_size == 10:
            ch = [
                my_tools.ReadSignal(files=get_files('bci_competition', 'B')),
                my_tools.Montage(montage_type='no_montage'),
                my_tools.LeaveChannels(channels=['Cz']),
                my_tools.Filter(
                    **{
                        'wp': 0.3,
                        'ws': 0.1,
                        'gpass': 3.0,
                        'gstop': 30.0,
                        'ftype': 'cheby2',
                        'unit': 'hz'
                    }),
                my_tools.Filter(
                    **{
                        'wp': 15.0,
                        'ws': 25.0,
                        'gpass': 1.0,
                        'gstop': 60.0,
                        'ftype': 'ellip',
                        'unit': 'hz'
                    }),
                my_tools.Segment(classes=('target', 'non-target'),
                                 start_offset=-0.2,
                                 duration=0.6),
                my_tools.Average(bin_selectors=[
                    lambda mgr: mgr['name'] == 'target',
                    lambda mgr: mgr['name'] == 'non-target'
                ],
                                 bin_names=['target', 'non-target'],
                                 size=avg_size,
                                 baseline=0.0,
                                 strategy='random'),
                my_tools.Downsample(factor=9),
                my_tools.Normalize(norm=2),
                my_tools.PrepareTrainSet(),
                my_tools.SVM(C=0.1,
                             Cmode='classProb',
                             kernel=ker.Polynomial(3))
            ]
            return ch
        elif avg_size == 15:
            ch = [
                my_tools.ReadSignal(files=get_files('bci_competition', 'B')),
                my_tools.Montage(montage_type='no_montage'),
                my_tools.LeaveChannels(channels=['Cz']),
                my_tools.Filter(
                    **{
                        'wp': 0.3,
                        'ws': 0.1,
                        'gpass': 3.0,
                        'gstop': 30.0,
                        'ftype': 'cheby2',
                        'unit': 'hz'
                    }),
                my_tools.Filter(
                    **{
                        'wp': 15.0,
                        'ws': 25.0,
                        'gpass': 1.0,
                        'gstop': 60.0,
                        'ftype': 'ellip',
                        'unit': 'hz'
                    }),
                my_tools.Segment(classes=('target', 'non-target'),
                                 start_offset=0.1,
                                 duration=0.6),
                my_tools.Average(bin_selectors=[
                    lambda mgr: mgr['name'] == 'target',
                    lambda mgr: mgr['name'] == 'non-target'
                ],
                                 bin_names=['target', 'non-target'],
                                 size=avg_size,
                                 baseline=0.0,
                                 strategy='random'),
                my_tools.Downsample(factor=9),
                my_tools.Normalize(norm=2),
                my_tools.PrepareTrainSet(),
                my_tools.SVM(C=12.0, Cmode='classProb', kernel=ker.Linear())
            ]
            return ch
Ejemplo n.º 2
0
def run_plot(person1, mode1, person2, mode2, avg_sizes, channels):
    chain1 = p300_sample_data.get_chain(person1, mode1, 5)
    chain2 = p300_sample_data.get_chain(person2, mode2, 5)
    x = 1 # for now
    y = 1 # for now
    assert(len(channels) == 1) # for now


    mgrs1 = None
    mgrs2 = None
    # Filter
    for ch in [chain1[0], chain1[3], chain1[4]]:
        mgrs1 = ch.process(mgrs1)
    for ch in [chain2[0], chain2[3], chain2[4]]:
        mgrs2 = ch.process(mgrs2)

    # Segment
    segmenter = my_tools.Segment(classes=('target','non-target'), start_offset=-0.1, duration=0.6)
    mgrs1 = segmenter.process(mgrs1)
    mgrs2 = segmenter.process(mgrs2)


    averager = my_tools.Average(bin_selectors=[lambda mgr: mgr['name'] == 'target',
                                        lambda mgr: mgr['name'] == 'non-target'],
                                bin_names=['target', 'non-target'], size=5,
                                baseline=0.1, strategy='random')

    samp = float(mgrs1[0].get_param('sampling_frequency'))
    for i, avg_size in enumerate(avg_sizes):
        # Ommit reading-in : normalize, prepare, classify
        averager.size = avg_size
        avgs1 = averager.process(mgrs1)
        avgs2 = averager.process(mgrs2)

        plotter = erp_plot.Plotter(x, y, avg_size)
        plt_id = 0
        for i_ch_name in channels:
            plt_id += 1
            
            avgs = [] 
            for avg in avgs1:
                if avg.get_param('epoch_name') == 'target':
                    avg.name = '1 - target'
                    avgs.append(avg)
                    break
            for avg in avgs1:
                if avg.get_param('epoch_name') == 'non-target':
                    avg.name = '1 - non-target'
                    avgs.append(avg)
                    break
            for avg in avgs2:
                if avg.get_param('epoch_name') == 'target':
                    avg.name = '2 - target'
                    avgs.append(avg)
                    break
            for avg in avgs2:
                if avg.get_param('epoch_name') == 'non-target':
                    avg.name = '2 - non-target'
                    avgs.append(avg)
                    break


            for j, avg in enumerate(avgs):
                ch = avg.get_channel_samples(i_ch_name)*0.0715
                plotter.add_plot(ch, 
                                 avg.name+" - "+i_ch_name, 
                                 [i*(1/samp)*1000 for i in range(-0.1*samp-1, len(ch)-(0.1*samp))],
                                 plt_id, {'linewidth':2.0})
            
        plotter.prepare_to_show()
    erp_plot.show()