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 analyse(self, blink, data): """Fired as often as defined in hashtable configuration: # Define from which moment in time (ago) we want to get samples (in seconds) 'ANALYSIS_BUFFER_FROM': # Define how many samples we wish to analyse every tick (in seconds) 'ANALYSIS_BUFFER_COUNT': # Define a tick duration (in seconds). 'ANALYSIS_BUFFER_EVERY': # To SUMP UP - above default values (0.5, 0.4, 0.25) define that # every 0.25s we will get buffer of length 0.4s starting from a sample # that we got 0.5s ago. # Some more typical example would be for values (0.5, 0.5 0.25). # In that case, every 0.25 we would get buffer of samples from 0.5s ago till now. data format is determined by another hashtable configuration: # possible values are: 'PROTOBUF_SAMPLES', 'NUMPY_CHANNELS' # it indicates format of buffered data returned to analysis # NUMPY_CHANNELS is a numpy 2D array with data divided by channels # PROTOBUF_SAMPLES is a list of protobuf Sample() objects 'ANALYSIS_BUFFER_RET_FORMAT' """ self.logger.debug("Got data to analyse... after: " + str(time.time() - self.last_time)) self.logger.debug("first and last value: " + str(data[0][0]) + " - " + str(data[0][-1])) self.logger.debug("DATA SIZE: " + str(data.shape)) self.logger.debug("BLINK index / ts / real_ts: " + str(blink.index) + " / " + str(blink.timestamp) + " / " + str(time.time())) self.last_time = time.time() #Wszystko dalej powinno się robić dla każdego nowego sygnału signal = np.dot(self.montage_matrix.T, data) self.logger.debug("AFTER MONTAGE SIGNAL SIZE: " + str(signal.shape)) tmp_sig = np.zeros(signal.shape) for e in xrange(len(self.montage_matrix.T)): tmp = filtfilt(self.b, self.a, signal[e, :]) tmp_sig[e, :] = filtfilt(self.b_l, self.a_l, tmp) if artifactsClasifier(tmp_sig, self.a_features, self.bands, self.fs): #2 Montujemy CSP #sig = np.dot(self.q.P[:, 0], tmp_sig) #3 Klasyfikacja: indeks pola albo -1, gdy nie ma detekcji ix = self.analyze.analyze(tmp_sig, blink.index, tr=self.treshold) if ix >= 0: self.send_func(ix) else: self.logger.info("Got -1 ind- no decision") else: self.logger.info("Got -1 ind- no decision")
def analyse(self, blink, data): """Fired as often as defined in hashtable configuration: # Define from which moment in time (ago) we want to get samples (in seconds) 'ANALYSIS_BUFFER_FROM': # Define how many samples we wish to analyse every tick (in seconds) 'ANALYSIS_BUFFER_COUNT': # Define a tick duration (in seconds). 'ANALYSIS_BUFFER_EVERY': # To SUMP UP - above default values (0.5, 0.4, 0.25) define that # every 0.25s we will get buffer of length 0.4s starting from a sample # that we got 0.5s ago. # Some more typical example would be for values (0.5, 0.5 0.25). # In that case, every 0.25 we would get buffer of samples from 0.5s ago till now. data format is determined by another hashtable configuration: # possible values are: 'PROTOBUF_SAMPLES', 'NUMPY_CHANNELS' # it indicates format of buffered data returned to analysis # NUMPY_CHANNELS is a numpy 2D array with data divided by channels # PROTOBUF_SAMPLES is a list of protobuf Sample() objects 'ANALYSIS_BUFFER_RET_FORMAT' """ self.logger.debug("Got data to analyse... after: "+str(time.time()-self.last_time)) self.logger.debug("first and last value: "+str(data[0][0])+" - "+str(data[0][-1])) self.logger.debug("DATA SIZE: "+str(data.shape)) self.logger.debug("BLINK index / ts / real_ts: "+str(blink.index)+" / "+str(blink.timestamp)+" / "+str(time.time())) self.last_time = time.time() #Wszystko dalej powinno się robić dla każdego nowego sygnału signal = np.dot(self.montage_matrix.T, data) self.logger.debug("AFTER MONTAGE SIGNAL SIZE: "+str(signal.shape)) tmp_sig = np.zeros(signal.shape) for e in xrange(len(self.montage_matrix.T)): tmp = filtfilt(self.b,self.a, signal[e, :]) tmp_sig[e, :] = filtfilt(self.b_l, self.a_l, tmp) if artifactsClasifier(tmp_sig, self.a_features, self.bands, self.fs): #2 Montujemy CSP #sig = np.dot(self.q.P[:, 0], tmp_sig) #3 Klasyfikacja: indeks pola albo -1, gdy nie ma detekcji ix = self.analyze.analyze(tmp_sig, blink.index, tr=self.treshold) if ix >= 0: self.send_func(ix) else: self.logger.info("Got -1 ind- no decision") else: self.logger.info("Got -1 ind- no decision")
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)