Ejemplo n.º 1
0
def downsample(mgr, factor):
    assert (factor >= 1)

    ch_num = len(mgr.get_samples())
    ch_len = len(mgr.get_samples()[0])

    # To be determined ...
    ret_ch_len = 0
    i = 0
    left_inds = []

    # Determine ret_ch_len - a size of returned channel
    while i < ch_len:
        left_inds.append(i)
        ret_ch_len += 1
        i += factor

    new_samples = array([zeros(ret_ch_len) for i in range(ch_num)])
    for i in range(ch_num):
        for j, ind in enumerate(left_inds):
            new_samples[i, j] = mgr.get_samples()[i, ind]

    info_source = copy.deepcopy(mgr.info_source)
    info_source.get_params()['number_of_samples'] = str(ret_ch_len * ch_num)
    info_source.get_params()['sampling_frequency'] = str(
        float(mgr.get_param('sampling_frequency')) / factor)

    tags_source = copy.deepcopy(mgr.tags_source)
    samples_source = read_data_source.MemoryDataSource(new_samples)
    return read_manager.ReadManager(info_source, samples_source, tags_source)
Ejemplo n.º 2
0
def get_fake_manager(number_of_channels, channel_len):
    """
    Return read manager with samples:
    [
       [1 2 3 ... channel_len],
       [10 20 30 ... 10*channel_len]
       ...
       [10^number_of_channels ... ]
    ]
    """
    new_samples = zeros((number_of_channels, channel_len))
    start_i = 1
    for ch in range(number_of_channels):
        new_samples[ch, :] = range(start_i, start_i*(channel_len+1), start_i)
        start_i = 10*start_i
    
    new_tags = []

    new_params = {}
    new_params['channels_names'] = ['CH'+str(i) for i in range(number_of_channels)]
    new_params['channels_numbers'] = [str(i) for i in range(number_of_channels)]
    new_params['channels_gains'] = [str(1+i/10.0) for i in range(number_of_channels)]
    new_params['channels_offsets'] = [str(i/10.0) for i in range(number_of_channels)]
    new_params['number_of_channels'] = str(number_of_channels)
    new_params['number_of_samples'] = str(channel_len*number_of_channels)
    new_params['sampling_frequency'] = str(128.0)

    info_source = read_info_source.MemoryInfoSource(new_params)
    tags_source = read_tags_source.MemoryTagsSource(new_tags)
    samples_source = read_data_source.MemoryDataSource(new_samples)
    return read_manager.ReadManager(info_source, samples_source, tags_source)
Ejemplo n.º 3
0
 def process(self, files=None):
     if not self.files is None:
         files = self.files
     mgrs = []
     for f in files:
         mgrs.append(
             read_manager.ReadManager(f['info'], f['data'], f['tags']))
     return mgrs
Ejemplo n.º 4
0
def montage_csa(mgr):
    new_samples = get_montage(
        mgr.get_samples(),
        get_montage_matrix_csa(int(mgr.get_param('number_of_channels'))))
    info_source = copy.deepcopy(mgr.info_source)
    tags_source = copy.deepcopy(mgr.tags_source)
    samples_source = read_data_source.MemoryDataSource(new_samples)
    return read_manager.ReadManager(info_source, samples_source, tags_source)
Ejemplo n.º 5
0
def exclude_channels(mgr, channels):
    new_params = copy.deepcopy(mgr.get_params())
    samples = mgr.get_samples()
    new_tags = copy.deepcopy(mgr.get_tags())

    ex_channels_inds = [
        new_params['channels_names'].index(ch) for ch in channels
    ]
    assert (-1 not in ex_channels_inds)

    new_samples = zeros((int(new_params['number_of_channels']) - len(channels),
                         len(samples[0])))
    # Define new samples and params list values
    keys = [
        'channels_names', 'channels_numbers', 'channels_gains',
        'channels_offsets'
    ]
    keys_to_remove = []
    for k in keys:
        try:
            #Exclude from keys those keys that are missing in mgr
            mgr.get_params()[k]
        except KeyError:
            keys_to_remove.append(k)
            continue
        new_params[k] = []

    for k in keys_to_remove:
        keys.remove(k)
    new_ind = 0
    for ch_ind, ch in enumerate(samples):
        if ch_ind in ex_channels_inds:
            continue
        else:
            new_samples[new_ind, :] = ch
            for k in keys:
                new_params[k].append(mgr.get_params()[k][ch_ind])

            new_ind += 1

    # Define other new new_params
    new_params['number_of_channels'] = str(
        int(new_params['number_of_channels']) - len(channels))
    new_params['number_of_samples'] = str(int(new_params['number_of_samples']) - \
                                              len(channels)*len(samples[0]))

    info_source = read_info_source.MemoryInfoSource(new_params)
    tags_source = read_tags_source.MemoryTagsSource(new_tags)
    samples_source = read_data_source.MemoryDataSource(new_samples)
    return read_manager.ReadManager(info_source, samples_source, tags_source)
Ejemplo n.º 6
0
def montage_ears(mgr, l_ear_channel, r_ear_channel):
    left_index = mgr.get_param('channels_names').index(l_ear_channel)
    right_index = mgr.get_param('channels_names').index(r_ear_channel)
    if left_index < 0 or right_index < 0:
        raise Exception("Montage - couldn`t find ears channels: " +
                        str(l_ear_channel) + ", " + str(r_ear_channel))

    new_samples = get_montage(
        mgr.get_samples(),
        get_montage_matrix_ears(int(mgr.get_param('number_of_channels')),
                                left_index, right_index))
    info_source = copy.deepcopy(mgr.info_source)
    tags_source = copy.deepcopy(mgr.tags_source)
    samples_source = read_data_source.MemoryDataSource(new_samples)
    return read_manager.ReadManager(info_source, samples_source, tags_source)
Ejemplo n.º 7
0
def run():
    dr2 = '/media/windows/wiedza/bci/EKSPERYMENTY_DANE/p300_10_12_2010/squares/'
    f2_name = 'p300_128hz_laptop_training_6x6_square_CATDOGFISHWATERBOWL_longer_8trials2'
    f2 = {
        'info': os.path.join(dr2, f2_name+'_10HZ.obci.xml'),
        'data': os.path.join(dr2, f2_name+'_10HZ.obci.bin'),
        'tags':os.path.join(dr2, f2_name+'.obci.arts_free.svarog.tags')
       }
    
    """dr2 = '/media/windows/wiedza/bci/EKSPERYMENTY_DANE/p300_10_12_2010/numbered_squares/'
    f2_name = 'p300_128hz_laptop_training_6x6_squareNUMBERS_CATDOGFISHWATERBOWL_longer_8trials'
    f2 = {
        'info': os.path.join(dr2, f2_name+'.obci.filtered.xml'),
        'data': os.path.join(dr2, f2_name+'_10HZ.obci.bin'),
        'tags':os.path.join(dr2, f2_name+'.obci.arts_free.svarog.tags')
       }"""

    mgr = read_manager.ReadManager(f2['info'], f2['data'], f2['tags'])
    train_data_ch, train_labels = prepare.get_train_set_from_mgr(mgr, num_per_avg=10, start_samples_to_norm=0, downsample_level=5) 
    class MY_SVM(object):
        def __init__(self, C):
            self.s = svm.SVM(C=C)
        def process(self, data):
            return self.s.stratifiedCV(data, 5).getBalancedSuccessRate()
        def __repr__(self):
            return "SVM: C - "+str(self.s.C)

    class MY_STD(object):
        def process(self, data):
            #p = prepare.Standardizer()
            #p.train(data)
            data.normalize(2)
            return data

    ch = my_chain.Chain(
        my_chain.ChainElement(MY_STD, {}),
        my_chain.ChainElement(MY_SVM,
                              {'C':[0.01, 0.1, 0.5, 1, 10]})
        )

    l = Labels([str(i) for i in train_labels])
    for i, train_data in enumerate(train_data_ch):
        train_data = train_data_ch[11]
        data = VectorDataSet(train_data, L=l)
        cs, res = ch.process(data)
        print("CSS: "+str(cs))
        print("RES: "+str(res))
        break
Ejemplo n.º 8
0
def filter(mgr,
           wp,
           ws,
           gpass,
           gstop,
           analog=0,
           ftype='ellip',
           output='ba',
           unit='radians',
           use_filtfilt=False):
    if unit == 'radians':
        b, a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output)
    elif unit == 'hz':
        sampling = float(mgr.get_param('sampling_frequency'))
        try:
            wp = wp / sampling
            ws = ws / sampling
        except TypeError:
            wp = [i / sampling for i in wp]
            ws = [i / sampling for i in ws]

        b, a = signal.iirdesign(wp, ws, gpass, gstop, analog, ftype, output)
    if use_filtfilt:
        import filtfilt
        #samples_source = read_data_source.MemoryDataSource(mgr.get_samples(), False)
        for i in range(int(mgr.get_param('number_of_channels'))):
            print("FILT FILT CHANNEL " + str(i))
            mgr.get_samples()[i, :] = filtfilt.filtfilt(
                b, a,
                mgr.get_samples()[i])
        samples_source = read_data_source.MemoryDataSource(
            mgr.get_samples(), False)
    else:
        print("FILTER CHANNELs")
        filtered = signal.lfilter(b, a, mgr.get_samples())
        print("FILTER CHANNELs finished")
        samples_source = read_data_source.MemoryDataSource(filtered, True)

    info_source = copy.deepcopy(mgr.info_source)
    tags_source = copy.deepcopy(mgr.tags_source)
    new_mgr = read_manager.ReadManager(info_source, samples_source,
                                       tags_source)
    return new_mgr
Ejemplo n.º 9
0
def fix_file(f, from_sample, num_of_samples, sample_value):
    mgr = read_manager.ReadManager(f['info'], f['data'], None)
    wr = data_file_proxy.DataFileWriteProxy(f['data'] + '.fixed.raw')
    for i, s in enumerate(mgr.iter_samples()):
        for v in s:
            wr.data_received(v)
        if i > from_sample:
            print("BREAK")
            break

    ch_no = int(mgr.get_param('number_of_channels'))
    for i in range(num_of_samples):
        for j in range(ch_no):
            wr.data_received(sample_value)
    print("FILLED")

    for i, s in enumerate(mgr.iter_samples()):
        if i > from_sample:
            for v in s:
                wr.data_received(v)

    wr.finish_saving()
Ejemplo n.º 10
0
def trigger_to_tag(p_new_tag_file_path,
                   p_exp_file_path,
                   p_info_file_path,
                   p_data_file_path,
                   p_ExpTagClass,
                   p_ignore_first=0,
                   p_ignore_last=0,
                   p_tag_len=1.0,
                   p_min_trig_len=0.0,
                   p_ignore_from_sample_number=None):
    """
    >>> trigger_to_tag('test.tags', '/media/windows/wiedza/bci/EKSPERYMENTY_DANE/kamil_pilot_25_11_2010/kamil_001.txt', '/media/windows/wiedza/bci/EKSPERYMENTY_DANE/kamil_pilot_25_11_2010/kamil_001.obci.svarog.info', '/media/windows/wiedza/bci/EKSPERYMENTY_DANE/kamil_pilot_25_11_2010/kamil_001.obci.dat', KamilExpTag, 2, 1, 1.0, 0.5)

    >>> import os

    #>>> os.remove('test.tags')
    """
    l_mgr = read_manager.ReadManager(p_info_file_path, p_data_file_path, None)

    l_exp_tags = get_experiment_tags_from(p_exp_file_path, p_ExpTagClass)
    l_tags = get_tags_from_trigger(l_mgr, l_exp_tags, p_ignore_first,
                                   p_ignore_last, p_tag_len, p_min_trig_len,
                                   p_ignore_from_sample_number)
    create_tags_file(l_tags, p_new_tag_file_path)
Ejemplo n.º 11
0
    plot([avg_diff] * len(tss_diff))
    title("Received_trigger_ts-Sent_trigger_ts")
    legend(("Rcv_ts-Sent_ts", "avg(Rcv_ts-Sent_ts)"))
    show()


if __name__ == "__main__":

    dr = '/home/mati/bci_dev/openbci_repo/trunk/temp/'
    f_name = 'synchro_test_02_12_2010_22ch_1024hz_c++_usb'
    f = {
        'info': os.path.join(dr, f_name + '.obci.svarog.info'),
        'data': os.path.join(dr, f_name + '.obci.dat'),
        'tags': os.path.join(dr, f_name + '.obci.svarog.tags')
    }
    read_manager = read_manager.ReadManager(f['info'], f['data'], f['tags'])

    asci_trig = ''  #os.path.join(dr, f_name+'.asci_trigger')
    if os.path.isfile(asci_trig):
        sent_vals, sent_tss = trigger_change.get_trigger_sent_timestamps(
            None, asci_trig)
    else:
        sent_vals, sent_tss = trigger_change.get_trigger_sent_timestamps(
            read_manager, None)

    rcv_vals, rcv_tss, rcv_len = trigger_change.get_trigger_received_timestamps(
        read_manager)

    LOGGER.info("*******************************************")
    LOGGER.info("Red sent vals: ")
    LOGGER.info(sent_vals)
Ejemplo n.º 12
0
def montage_custom(mgr, montage_matrix):
    new_samples = get_montage(mgr.get_samples(), montage_matrix)
    info_source = copy.deepcopy(mgr.info_source)
    tags_source = copy.deepcopy(mgr.tags_source)
    samples_source = read_data_source.MemoryDataSource(new_samples)
    return read_manager.ReadManager(info_source, samples_source, tags_source)