예제 #1
0
 def __init__(self, fn, channels, to_freq, montage_channels, montage, idx=1, csp_time=[0, 0.3], a_time=0.5):
     self.data = sp.signalParser(fn)
     self.tags = self.data.get_p300_tags(idx=idx, samples=False)
     signal = self.data.prep_signal(to_freq, channels, montage_channels, montage)
     signal2 = np.zeros(signal.shape)
     self.fs = to_freq
     signal2 = np.zeros(signal.shape)
     self.wrong_tags = self.data.get_p300_tags(idx=idx, rest=True, samples=False)
     artifacts_data = np.zeros([len(channels), self.fs * a_time, len(self.tags)])
     for i, p in enumerate(self.tags):
         artifacts_data[..., i] = signal[
             :, p * self.data.sampling_frequency : (p + a_time) * self.data.sampling_frequency
         ]
     self.a_features, self.bands = artifactsCalibration(artifacts_data, self.data.sampling_frequency)
     signal2 = np.zeros(signal.shape)
     self.fs = to_freq
     self.channels = channels
     self.idx = idx
     b, a = ss.butter(3, 2 * 1.0 / self.fs, btype="high")
     b_l, a_l = ss.butter(3, 20.0 * 2 / self.fs, btype="low")
     for e in xrange(len(self.channels)):
         tmp = filtfilt(b, a, signal[e, :])
         signal2[e, :] = filtfilt(b_l, a_l, tmp)
     self.signal_original = signal2
     self.t1, self.t2 = self.show_mean(csp_time, "Cz", dont_plot=False)
     P, vals = self.train_csp(signal2, [self.t1, self.t2])
     self.P = P
     self.signal = np.dot(P[:, 0], signal2)
예제 #2
0
 def __init__(self,
              fn,
              channels,
              to_freq,
              montage_channels,
              montage,
              idx=1,
              csp_time=[0, 0.3],
              a_time=0.5):
     self.data = sp.signalParser(fn)
     self.tags = self.data.get_p300_tags(idx=idx, samples=False)
     signal = self.data.prep_signal(to_freq, channels, montage_channels,
                                    montage)
     signal2 = np.zeros(signal.shape)
     self.fs = to_freq
     signal2 = np.zeros(signal.shape)
     self.wrong_tags = self.data.get_p300_tags(idx=idx,
                                               rest=True,
                                               samples=False)
     artifacts_data = np.zeros(
         [len(channels), self.fs * a_time,
          len(self.tags)])
     for i, p in enumerate(self.tags):
         artifacts_data[...,
                        i] = signal[:, p *
                                    self.data.sampling_frequency:(p +
                                                                  a_time) *
                                    self.data.sampling_frequency]
     self.a_features, self.bands = artifactsCalibration(
         artifacts_data, self.data.sampling_frequency)
     signal2 = np.zeros(signal.shape)
     self.fs = to_freq
     self.channels = channels
     self.idx = idx
     b, a = ss.butter(3, 2 * 1.0 / self.fs, btype='high')
     b_l, a_l = ss.butter(3, 20.0 * 2 / self.fs, btype='low')
     for e in xrange(len(self.channels)):
         tmp = filtfilt(b, a, signal[e, :])
         signal2[e, :] = filtfilt(b_l, a_l, tmp)
     self.signal_original = signal2
     self.t1, self.t2 = self.show_mean(csp_time, 'Cz', dont_plot=False)
     P, vals = self.train_csp(signal2, [self.t1, self.t2])
     self.P = P
     self.signal = np.dot(P[:, 0], signal2)
def run(in_file, use_channels, ignore_channels, montage, montage_channels, plot_stats=True,
	context=ctx.get_dummy_context('UgmBlinkingConnection')):
	logger = context['logger']
	mgr = read_manager.ReadManager(
		in_file+'.obci.xml',
		in_file+'.obci.raw',
		in_file+'.obci.tag')
        to_frequency = int(float(mgr.get_param("sampling_frequency")))
	if use_channels is None:
		use_channels = mgr.get_param('channels_names')
	if ignore_channels is not None:
		for i in ignore_channels:
			try:
				use_channels.remove(i)
			except:
				pass

	exp_info = mgr.get_tags('experimentInfo')[0]['desc']
        to_signal = float(exp_info['target_time'])
        freqs = [int(i) for i in exp_info['all_freqs'].split(';')]
	dec_count = 4


        data = sp.signalParser(in_file+'.obci')
        train_tags = data.get_train_tags(trial_separator_name='diodes', screen=True)
	logger.info("Run csp with: use_channels: "+str(use_channels)+" motage: "+str(montage)+" montage_channels: "+str(montage_channels))
        q = csp.modCSP(in_file+'.obci', freqs, use_channels, montage, montage_channels)
        q.start_CSP(to_signal, to_frequency, baseline = False, filt='cheby', method = 'regular', train_tags = train_tags)#liczenie CSP
        time = pylab.linspace(1, to_signal, 7)
        t1, t2 = q.time_frequency_selection(to_frequency, train_tags, time=time, frequency_no=dec_count, plt=False)

        logger.info("Got times t1: "+str(t1)+ " and t2: "+str(t2))
        value, mu, sigma, means, stds, out_top, out_bottom = q.count_stats(to_signal, to_frequency, train_tags, plt=plot_stats)#Liczenie statystyk
        logger.info("For freqs: "+str(freqs))
        logger.info("Got means: "+str(means))
        
        pairs = zip(means, freqs)
        pairs.sort(reverse=True)
        logger.info("Sorted: "+str(pairs))
	best = [i[1] for i in pairs]
        best_means = [i[0] for i in pairs]
        logger.info("Best freqs: "+str(best))
        
        logger.info("Finished CSP with stats:")
        logger.info(str(value) + " / " + str(mu) + " / " + str(sigma))
        logger.info("And q:")
        logger.info(str(q))
        
	tmp = best[:dec_count]
        best_freqs = [tmp[0],0,tmp[1],0,
		      0,tmp[2],0,tmp[3]]
	best_freqs = [str(i) for i in best_freqs]

        logger.info("freqs:"+str(freqs))
        d = {'value': value,
             'mu': mu,
             'sigma': sigma,
             'q' : q,
	     'freqs':';'.join(best_freqs),
	     'all_freqs':';'.join([str(i) for i in best]),
	     'all_means':';'.join([str(i) for i in best_means]),
	     'dec_count':dec_count,
	     'buffer':t2,
	     'use_channels':';'.join(use_channels),
	     'montage':montage,
	     'montage_channels':';'.join(montage_channels),
	     'out_top':out_top,
	     'out_bottom':out_bottom
             }
	return d
예제 #4
0
def run(in_file,
        use_channels,
        ignore_channels,
        montage,
        montage_channels,
        dec_count,
        plot_stats=True,
        context=ctx.get_dummy_context('UgmBlinkingConnection')):
    logger = context['logger']
    mgr = read_manager.ReadManager(in_file + '.obci.xml',
                                   in_file + '.obci.raw',
                                   in_file + '.obci.tag')
    to_frequency = int(float(mgr.get_param("sampling_frequency")))
    if use_channels is None:
        use_channels = mgr.get_param('channels_names')
    if ignore_channels is not None:
        for i in ignore_channels:
            try:
                use_channels.remove(i)
            except:
                pass

    exp_info = mgr.get_tags('experimentInfo')[0]['desc']
    to_signal = float(exp_info['target_time'])
    freqs = [int(i) for i in exp_info['all_freqs'].split(';')]

    data = sp.signalParser(in_file + '.obci')
    train_tags = data.get_train_tags(trial_separator_name='diodes',
                                     screen=True)
    logger.info("Run csp with: use_channels: " + str(use_channels) +
                " motage: " + str(montage) + " montage_channels: " +
                str(montage_channels))
    q = csp.modCSP(in_file + '.obci', freqs, use_channels, montage,
                   montage_channels)
    q.start_CSP(to_signal,
                to_frequency,
                baseline=False,
                filt='cheby',
                method='regular',
                train_tags=train_tags)  #liczenie CSP
    time = pylab.linspace(1, to_signal, 7)
    t1, t2 = q.time_frequency_selection(to_frequency,
                                        train_tags,
                                        time=time,
                                        frequency_no=dec_count,
                                        plt=False)

    logger.info("Got times t1: " + str(t1) + " and t2: " + str(t2))
    logger.info("Set buffer time:" + str(t2))
    value, mu, sigma, means, stds, out_top, out_bottom = q.count_stats(
        to_signal, to_frequency, train_tags,
        plt=plot_stats)  #Liczenie statystyk
    logger.info("For freqs: " + str(freqs))
    logger.info("Got means: " + str(means))

    pairs = zip(means, freqs)
    pairs.sort(reverse=True)
    logger.info("Sorted: " + str(pairs))
    best = [i[1] for i in pairs]
    best_means = [i[0] for i in pairs]
    logger.info("Best freqs: " + str(best))
    logger.info("dec_count: " + str(dec_count))
    logger.info("freqs: " + str(best[:dec_count]))
    logger.info("Finished CSP with stats:")
    logger.info(str(value) + " / " + str(mu) + " / " + str(sigma))
    logger.info("And q:")
    logger.info(str(q))

    d = {
        'value': value,
        'mu': mu,
        'sigma': sigma,
        'q': q,
        'freqs': ';'.join([str(i) for i in best[:dec_count]]),
        'all_freqs': ';'.join([str(i) for i in best]),
        'all_means': ';'.join([str(i) for i in best_means]),
        'dec_count': dec_count,
        'buffer': t2,
        'use_channels': ';'.join(use_channels),
        'montage': montage,
        'montage_channels': ';'.join(montage_channels),
        'out_top': out_top,
        'out_bottom': out_bottom
    }
    return d