def run(self): self.logger.info("START FDA...") f_name = self.config.get_param("data_file_name") f_dir = self.config.get_param("data_file_path") in_file = acquisition_helper.get_file_path(f_dir, f_name) filename = os.path.join(f_dir, f_name + ".obci") data = sp.signalParser(filename) fs = int(float(data.getSamplingFrequency())) mgr = read_manager.ReadManager( in_file+'.obci.xml', in_file+'.obci.raw', in_file+'.obci.tag') if self.use_channels is None: self.use_channels = data.getChannelList() if self.ignore_channels is not None: for i in self.ignore_channels: try: self.use_channels.remove(i) except: pass self.montage = self.config.get_param("montage") tmp = self.config.get_param("montage_channels") if len(tmp) > 0: self.montage_channels = tmp.split(';') else: self.montage_channels = [] self.logger.info("USE CHANNELS: "+str(self.use_channels)) self.logger.info("CHANNELS NAMES: "+str(mgr.get_param('channels_names'))) ## Get data and tags data.setMontage(self.montage_channels) Signal = data.getData(self.use_channels) trgTags = data.get_p300_tags(idx=[1,7]) ntrgTags = data.get_not_p300_tags(idx=[1,7]) trgTags = trgTags[trgTags<Signal.shape[1]-fs] ntrgTags = ntrgTags[ntrgTags<Signal.shape[1]-fs] trgTags = trgTags[trgTags>0] ntrgTags = ntrgTags[ntrgTags>0] target, nontarget = data.getTargetNontarget(Signal, trgTags, ntrgTags) ## Get params from file pPer = float(self.config.get_param("p_per")) nRepeat = int(self.config.get_param("n_repeat")) nMin = int(self.config.get_param("n_min")) nMax = int(self.config.get_param("n_max")) avrM_l = map(lambda x: int(x), self.config.get_param("avr_m").split(';')) conN_l = map(lambda x: int(x), self.config.get_param("con_n").split(';')) csp_dt_l = map(lambda x: float(x), self.config.get_param("csp_dt").split(';')) csp_time_l = map(lambda x: float(x), self.config.get_param("csp_time").split(';')) plotFlag = int(self.config.get_param("plot_flag")) debugFlag = int(self.config.get_param("debug_flag")) ## Define buffer buffer = 1.1*fs self.logger.info("Computer buffer len: "+str(buffer)) ## Make montage matrix conf = self.use_channels, self.montage, self.montage_channels self.montage_matrix = self._get_montage_matrix(conf[0], conf[1], conf[2], mgr.get_param('channels_names')) print "self.montage_matrix: ", self.montage_matrix channels = ";".join(self.use_channels) # Test all combination of parameters N = 0 d = {} for avrM in avrM_l: for conN in conN_l: for csp_time in csp_time_l: for dt in csp_dt_l: d[N] = {"avrM":avrM, "conN":conN, "csp_time":[csp_time, csp_time+dt]} print "d[%i] = " %N, d[N] N += 1 ################################# ## CROSS CHECKING P_dict, dVal_dict = {}, {} l = np.zeros(N) for idxN in range(N): KEY = d[idxN].keys() for k in KEY: exec "{0}_tmp = {1}".format(k, d[idxN][k]) in globals(), locals() self.logger.info("Zestaw: {0} / {1}".format(idxN, N)) self.logger.info(str(d[idxN])) csp = csp_time_tmp[0]*fs, csp_time_tmp[1]*fs # Crop signalas to csp_time : csp_time+csp_dt _target = target[:,:,csp[0]:csp[1]] _nontarget = nontarget[:,:,csp[0]:csp[1]] p300 = P300_train(channels, fs, avrM_tmp, conN_tmp, csp_time_tmp, pPer) l[idxN] = p300.valid_kGroups(Signal, _target, _nontarget, 2) P_dict[idxN] = p300.getPWC() dVal_dict[idxN] = p300.getDValDistribution() if debugFlag: p300.saveDist2File("target_%i"%idxN, "nontarget_%i"%idxN) self.logger.info("Test score: " + str(l[idxN])) self.logger.info("Calibration passed") ################################# if debugFlag: distributionDraw = P300_draw(fs) # Plot all d distributions for idx in range(N): dTarget, dNontarget = dVal_dict[idx] distributionDraw.plotDistribution(dTarget, dNontarget) ################################# # Finding best self.logger.info("\n"*5) self.logger.info( "L: " + str(l) ) P, conN, avrM, csp_time = None, None, None, None BEST = -1 arr = np.arange(len(l)) for i in range(5): bestN = int(arr[l==l.min()][0]) print "best_{0}: {1}".format(i, bestN) print "d[bestN]: ", d[bestN] print "l[bestN]: ", l.min() #~ if (l.min() > 100) and (BEST == -1): BEST = bestN if (BEST == -1): BEST = bestN l[bestN] = l.max()-1 P, w, c = P_dict[BEST] dTarget, dNontarget = dVal_dict[BEST] pVal = st.scoreatpercentile(dNontarget, pPer) pdf = dNontarget avrM = d[BEST]["avrM"] conN = d[BEST]["conN"] csp_time = d[BEST]["csp_time"] cfg = {"csp_time":csp_time, "use_channels": ';'.join(self.use_channels), 'avrM':avrM, 'conN':conN, 'pdf':pdf, "P":P, "w":w, "c":c, "montage":self.montage, "montage_channels":';'.join(self.montage_channels), "buffer":buffer } f_name = self.config.get_param("fda_file_name") f_dir = self.config.get_param("fda_file_path") csp_helper.set_csp_config(f_dir, f_name, cfg) if plotFlag: ## Plotting best p300_draw = P300_draw() p300_draw.setCalibration(target, nontarget) p300_draw.setCSP(P) p300_draw.setTimeLine(conN, avrM, csp_time) p300_draw.plotSignal() #~ p300_draw.plotSignal_ds() self.logger.info("FDA classifier -- DONE") if not self.run_offline: self._run_next_scenario()
def run(self): self.logger.info("START FDA...") f_name = self.config.get_param("data_file_name") f_dir = self.config.get_param("data_file_path") in_file = acquisition_helper.get_file_path(f_dir, f_name) filename = os.path.join(f_dir, f_name + ".obci") data = sp.signalParser(filename) fs = int(float(data.getSamplingFrequency())) mgr = read_manager.ReadManager(in_file + '.obci.xml', in_file + '.obci.raw', in_file + '.obci.tag') if self.use_channels is None: self.use_channels = data.getChannelList() if self.ignore_channels is not None: for i in self.ignore_channels: try: self.use_channels.remove(i) except: pass self.montage = self.config.get_param("montage") tmp = self.config.get_param("montage_channels") if len(tmp) > 0: self.montage_channels = tmp.split(';') else: self.montage_channels = [] self.logger.info("USE CHANNELS: " + str(self.use_channels)) self.logger.info("CHANNELS NAMES: " + str(mgr.get_param('channels_names'))) ## Get data and tags data.setMontage(self.montage_channels) Signal = data.getData(self.use_channels) trgTags = data.get_p300_tags() ntrgTags = data.get_not_p300_tags() print "Signal.shape[1]: ", Signal.shape[1] print "last trg: ", trgTags[-1] print "last ntrg: ", ntrgTags[-1] m = max([trgTags[-1], ntrgTags[-1]]) # ?!?!?!?!?!?!?!?! Why blinks are <0 trgTags = trgTags[trgTags > 0] ntrgTags = ntrgTags[ntrgTags > 0] trgTags = trgTags[trgTags < Signal.shape[1] - fs] ntrgTags = ntrgTags[ntrgTags < Signal.shape[1] - fs] print "last trg: ", trgTags[-1] print "last ntrg: ", ntrgTags[-1] target, nontarget = data.getTargetNontarget(Signal, trgTags, ntrgTags) ## Get params from file pPer = float(self.config.get_param("p_per")) nRepeat = int(self.config.get_param("n_repeat")) nMin = int(self.config.get_param("n_min")) nMax = int(self.config.get_param("n_max")) avrM_l = map(lambda x: int(x), self.config.get_param("avr_m").split(';')) conN_l = map(lambda x: int(x), self.config.get_param("con_n").split(';')) csp_dt_l = map(lambda x: float(x), self.config.get_param("csp_dt").split(';')) csp_time_l = map(lambda x: float(x), self.config.get_param("csp_time").split(';')) plotFlag = int(self.config.get_param("plot_flag")) debugFlag = int(self.config.get_param("debug_flag")) ## Define buffer buffer = 1.1 * fs self.logger.info("Computer buffer len: " + str(buffer)) ## Make montage matrix conf = self.use_channels, self.montage, self.montage_channels self.montage_matrix = self._get_montage_matrix( conf[0], conf[1], conf[2], mgr.get_param('channels_names')) print "self.montage_matrix: ", self.montage_matrix channels = ";".join(self.use_channels) # Test all combination of parameters N = 0 d = {} for avrM in avrM_l: for conN in conN_l: for csp_time in csp_time_l: for dt in csp_dt_l: d[N] = { "avrM": avrM, "conN": conN, "csp_time": [csp_time, csp_time + dt] } print "d[%i] = " % N, d[N] N += 1 ################################# ## CROSS CHECKING P_dict, dVal_dict = {}, {} l = np.zeros(N) for idxN in range(N): KEY = d[idxN].keys() for k in KEY: exec "{0}_tmp = {1}".format(k, d[idxN][k]) in globals(), locals() self.logger.info("Zestaw: {0} / {1}".format(idxN, N)) self.logger.info(str(d[idxN])) csp = csp_time_tmp[0] * fs, csp_time_tmp[1] * fs # Crop signalas to csp_time : csp_time+csp_dt _target = target[:, :, csp[0]:csp[1]] _nontarget = nontarget[:, :, csp[0]:csp[1]] p300 = P300_train(channels, fs, avrM_tmp, conN_tmp, csp_time_tmp, pPer) l[idxN] = p300.valid_kGroups(Signal, _target, _nontarget, 2) P_dict[idxN] = p300.getPWC() dVal_dict[idxN] = p300.getDValDistribution() if debugFlag: p300.saveDist2File("target_%i" % idxN, "nontarget_%i" % idxN) self.logger.info("Test score: " + str(l[idxN])) self.logger.info("Calibration passed") ################################# if debugFlag: distributionDraw = P300_draw(fs) # Plot all d distributions for idx in range(N): dTarget, dNontarget = dVal_dict[idx] distributionDraw.plotDistribution(dTarget, dNontarget) ################################# # Finding best self.logger.info("\n" * 5) self.logger.info("L: " + str(l)) P, conN, avrM, csp_time = None, None, None, None BEST = -1 arr = np.arange(len(l)) for i in range(5): bestN = int(arr[l == l.min()][0]) print "best_{0}: {1}".format(i, bestN) print "d[bestN]: ", d[bestN] print "l[bestN]: ", l.min() if (l.min() > 100) and (BEST == -1): BEST = bestN l[bestN] = l.max() - 1 P, w, c = P_dict[BEST] dTarget, dNontarget = dVal_dict[BEST] pVal = st.scoreatpercentile(dNontarget, pPer) pdf = dNontarget avrM = d[BEST]["avrM"] conN = d[BEST]["conN"] csp_time = d[BEST]["csp_time"] cfg = { "csp_time": csp_time, "use_channels": ';'.join(self.use_channels), 'avrM': avrM, 'conN': conN, 'pdf': pdf, "P": P, "w": w, "c": c, "montage": self.montage, "montage_channels": ';'.join(self.montage_channels), "buffer": buffer } f_name = self.config.get_param("fda_file_name") f_dir = self.config.get_param("fda_file_path") csp_helper.set_csp_config(f_dir, f_name, cfg) if plotFlag: ## Plotting best p300_draw = P300_draw() p300_draw.setCalibration(target, nontarget) p300_draw.setCSP(P) p300_draw.setTimeLine(conN, avrM, csp_time) p300_draw.plotSignal() #~ p300_draw.plotSignal_ds() self.logger.info("FDA classifier -- DONE") if not self.run_offline: self._run_next_scenario()
dt = 1.0/Fs t = np.arange(0,T,dt) s = np.sin(2*np.pi*f*t + phi) return s if __name__ == "__main__": fnCalibration = "dawid1.obci" fnClasification = "dawid2.obci" #~ CHANNELS = "PO7,O1,Oz,O2,PO8,PO3,PO4,Pz,Cz".split(',') CHANNELS = "PO7,O1,O2,PO8,PO3,PO4,Pz,Cz".split(',') REF_CHANNEL = ["Pz"] chL = len(CHANNELS) sigPars = signalParser(fnCalibration) refSig = sigPars.extract_channel(REF_CHANNEL) eegSig = sigPars.extract_channel(CHANNELS) eegSigCalibration = eegSig - refSig sigPars = signalParser(fnClasification) refSig = sigPars.extract_channel(REF_CHANNEL) eegSig = sigPars.extract_channel(CHANNELS) eegSigClassification = eegSig - refSig #~ eegSig = eegSig - np.mean(eegSig, axis=1) Fs = 128.0 filtr = Filtr(Fs) filtr.set_lowPass_filter() filtr.set_highPass_filter()
def run(self): LOGGER.info("START FDA...") f_name = self.config.get_param("data_file_name") f_dir = self.config.get_param("data_file_path") in_file = acquisition_helper.get_file_path(f_dir, f_name) filename = os.path.join(f_dir, f_name + ".obci") data = sp.signalParser(filename) fs = int(float(data.getSamplingFrequency())) mgr = read_manager.ReadManager( in_file+'.obci.xml', in_file+'.obci.raw', in_file+'.obci.tag') if self.use_channels is None: self.use_channels = data.getChannelList() if self.ignore_channels is not None: for i in self.ignore_channels: try: self.use_channels.remove(i) except: pass self.montage = self.config.get_param("montage") tmp = self.config.get_param("montage_channels") if len(tmp) > 0: self.montage_channels = tmp.split(';') else: self.montage_channels = [] LOGGER.info("USE CHANNELS: "+str(self.use_channels)) LOGGER.info("CHANNELS NAMES: "+str(mgr.get_param('channels_names'))) ## Get data and tags data.setMontage(self.montage_channels) Signal = data.getData(self.use_channels) trgTags = data.get_p300_tags() ntrgTags = data.get_not_p300_tags() target, nontarget = data.getTargetNontarget(Signal, trgTags, ntrgTags) ## Get params from file pVal = float(self.config.get_param("p_val")) nRepeat = int(self.config.get_param("n_repeat")) nMin = int(self.config.get_param("n_min")) nMax = int(self.config.get_param("n_max")) avrM_l = map(lambda x: int(x), self.config.get_param("avr_m").split(';')) conN_l = map(lambda x: int(x), self.config.get_param("con_n").split(';')) csp_dt_l = map(lambda x: float(x), self.config.get_param("csp_dt").split(';')) csp_time_l = map(lambda x: float(x), self.config.get_param("csp_time").split(';')) ## Define buffer buffer = 1.1*fs LOGGER.info("Computer buffer len: "+str(buffer)) ## Make montage matrix conf = self.use_channels, self.montage, self.montage_channels self.montage_matrix = self._get_montage_matrix(conf[0], conf[1], conf[2], mgr.get_param('channels_names')) print "self.montage_matrix: ", self.montage_matrix channels = ";".join(self.use_channels) # make sure that: # channels -- is in format like "P07;O1;Oz;O2" # fs -- is a number # avrM -- are int # conN -- are int # csp_time -- is a list of two float 0 < x < 1 N = 0 d, P_dict = {}, {} for avrM in avrM_l: for conN in conN_l: for csp_time in csp_time_l: for dt in csp_dt_l: d[N] = {"avrM":avrM, "conN":conN, "csp_time":[csp_time, csp_time+dt]} print "d[%i] = " %N, d[N] N += 1 ################################# ## CROSS CHECKING l = np.zeros(N) for idxN in range(N): KEY = d[idxN].keys() for k in KEY: #~ c = "{0} = {1}".format(k, d[idxN][k]) #~ print c #~ exec(c) exec "{0}_tmp = {1}".format(k, d[idxN][k]) in globals(), locals() p300 = P300_train(channels, fs, avrM_tmp, conN_tmp, csp_time_tmp) l[idxN] = p300.valid_kGroups(Signal, target, nontarget, 2) P_dict[idxN] = p300.getPWC() ################################# # Finding best print "\n"*5 print "L: ", l P, conN, avrM, csp_time = None, None, None, None BEST = -1 arr = np.arange(len(l)) for i in range(5): bestN = int(arr[l==l.max()]) print "best_{0}: {1}".format(i, bestN) print "d[bestN]: ", d[bestN] print "l[bestN]: ", l.max() if (l.max() < 1000) and (BEST == -1): BEST = bestN l[bestN] = l.min()-1 print "best: ", BEST P, w, c = P_dict[BEST] avrM = d[BEST]["avrM"] conN = d[BEST]["conN"] csp_time = d[BEST]["csp_time"] cfg = {"csp_time":csp_time, "use_channels": ';'.join(self.use_channels), 'pVal':pVal, 'avrM':avrM, 'conN':conN, "nRepeat":nRepeat, "P":P, "w":w, "c":c, "montage":self.montage, "montage_channels":';'.join(self.montage_channels), "buffer":buffer } f_name = self.config.get_param("csp_file_name") f_dir = self.config.get_param("csp_file_path") ssvep_csp_helper.set_csp_config(f_dir, f_name, cfg) ## Plotting best p300_draw = P300_draw() p300_draw.setCalibration(target, nontarget, trgTags, ntrgTags) p300_draw.setCSP(P) p300_draw.setTimeLine(conN, avrM, csp_time) p300_draw.plotSignal() p300_draw.plotSignal_ds() LOGGER.info("FDA Calibration complete") if not self.run_offline: self._run_next_scenario()
def run(self): LOGGER.info("START FDA...") f_name = self.config.get_param("data_file_name") f_dir = self.config.get_param("data_file_path") in_file = acquisition_helper.get_file_path(f_dir, f_name) filename = os.path.join(f_dir, f_name + ".obci") data = sp.signalParser(filename) fs = int(float(data.getSamplingFrequency())) mgr = read_manager.ReadManager(in_file + '.obci.xml', in_file + '.obci.raw', in_file + '.obci.tag') if self.use_channels is None: self.use_channels = data.getChannelList() if self.ignore_channels is not None: for i in self.ignore_channels: try: self.use_channels.remove(i) except: pass self.montage = self.config.get_param("montage") tmp = self.config.get_param("montage_channels") if len(tmp) > 0: self.montage_channels = tmp.split(';') else: self.montage_channels = [] LOGGER.info("USE CHANNELS: " + str(self.use_channels)) LOGGER.info("CHANNELS NAMES: " + str(mgr.get_param('channels_names'))) ## Get data and tags data.setMontage(self.montage_channels) Signal = data.getData(self.use_channels) trgTags = data.get_p300_tags() ntrgTags = data.get_not_p300_tags() target, nontarget = data.getTargetNontarget(Signal, trgTags, ntrgTags) ## Get params from file pVal = float(self.config.get_param("p_val")) nRepeat = int(self.config.get_param("n_repeat")) nMin = int(self.config.get_param("n_min")) nMax = int(self.config.get_param("n_max")) avrM_l = map(lambda x: int(x), self.config.get_param("avr_m").split(';')) conN_l = map(lambda x: int(x), self.config.get_param("con_n").split(';')) csp_dt_l = map(lambda x: float(x), self.config.get_param("csp_dt").split(';')) csp_time_l = map(lambda x: float(x), self.config.get_param("csp_time").split(';')) ## Define buffer buffer = 1.1 * fs LOGGER.info("Computer buffer len: " + str(buffer)) ## Make montage matrix conf = self.use_channels, self.montage, self.montage_channels self.montage_matrix = self._get_montage_matrix( conf[0], conf[1], conf[2], mgr.get_param('channels_names')) print "self.montage_matrix: ", self.montage_matrix channels = ";".join(self.use_channels) # make sure that: # channels -- is in format like "P07;O1;Oz;O2" # fs -- is a number # avrM -- are int # conN -- are int # csp_time -- is a list of two float 0 < x < 1 N = 0 d, P_dict = {}, {} for avrM in avrM_l: for conN in conN_l: for csp_time in csp_time_l: for dt in csp_dt_l: d[N] = { "avrM": avrM, "conN": conN, "csp_time": [csp_time, csp_time + dt] } print "d[%i] = " % N, d[N] N += 1 ################################# ## CROSS CHECKING l = np.zeros(N) for idxN in range(N): KEY = d[idxN].keys() for k in KEY: #~ c = "{0} = {1}".format(k, d[idxN][k]) #~ print c #~ exec(c) exec "{0}_tmp = {1}".format(k, d[idxN][k]) in globals(), locals() p300 = P300_train(channels, fs, avrM_tmp, conN_tmp, csp_time_tmp) l[idxN] = p300.valid_kGroups(Signal, target, nontarget, 2) P_dict[idxN] = p300.getPWC() ################################# # Finding best print "\n" * 5 print "L: ", l P, conN, avrM, csp_time = None, None, None, None BEST = -1 arr = np.arange(len(l)) for i in range(5): bestN = int(arr[l == l.max()]) print "best_{0}: {1}".format(i, bestN) print "d[bestN]: ", d[bestN] print "l[bestN]: ", l.max() if (l.max() < 1000) and (BEST == -1): BEST = bestN l[bestN] = l.min() - 1 print "best: ", BEST P, w, c = P_dict[BEST] avrM = d[BEST]["avrM"] conN = d[BEST]["conN"] csp_time = d[BEST]["csp_time"] cfg = { "csp_time": csp_time, "use_channels": ';'.join(self.use_channels), 'pVal': pVal, 'avrM': avrM, 'conN': conN, "nRepeat": nRepeat, "P": P, "w": w, "c": c, "montage": self.montage, "montage_channels": ';'.join(self.montage_channels), "buffer": buffer } f_name = self.config.get_param("csp_file_name") f_dir = self.config.get_param("csp_file_path") ssvep_csp_helper.set_csp_config(f_dir, f_name, cfg) ## Plotting best p300_draw = P300_draw() p300_draw.setCalibration(target, nontarget, trgTags, ntrgTags) p300_draw.setCSP(P) p300_draw.setTimeLine(conN, avrM, csp_time) p300_draw.plotSignal() p300_draw.plotSignal_ds() LOGGER.info("FDA Calibration complete") if not self.run_offline: self._run_next_scenario()
dt = 1.0 / Fs t = np.arange(0, T, dt) s = np.sin(2 * np.pi * f * t + phi) return s if __name__ == "__main__": fnCalibration = "dawid1.obci" fnClasification = "dawid2.obci" # ~ CHANNELS = "PO7,O1,Oz,O2,PO8,PO3,PO4,Pz,Cz".split(',') CHANNELS = "PO7,O1,O2,PO8,PO3,PO4,Pz,Cz".split(",") REF_CHANNEL = ["Pz"] chL = len(CHANNELS) sigPars = signalParser(fnCalibration) refSig = sigPars.extract_channel(REF_CHANNEL) eegSig = sigPars.extract_channel(CHANNELS) eegSigCalibration = eegSig - refSig sigPars = signalParser(fnClasification) refSig = sigPars.extract_channel(REF_CHANNEL) eegSig = sigPars.extract_channel(CHANNELS) eegSigClassification = eegSig - refSig # ~ eegSig = eegSig - np.mean(eegSig, axis=1) Fs = 128.0 filtr = Filtr(Fs) filtr.set_lowPass_filter() filtr.set_highPass_filter()