Ejemplo n.º 1
0
    def __init__(self, addresses):
        super(LogicVenturesExperiment, self).__init__(addresses=addresses,
                                                      type=peers.CLIENT)
        #initialise game engin for current user
        user_id = self.get_param('user_id')
        self.logger.info("Starting maze for user: "******"Unknown session name - abort")
        #initialise server to receive wii analysis messages from experiment server via udpServer
        srv = UdpServer(engine, self.get_param('internal_ip'))
        self.set_param('internal_port', str(srv.socket.getsockname()[1]))
        thread.start_new_thread(srv.run, ())

        self.ready()
        engine.run()

        self.logger.info("Gui closed. Wait for finish saving..")
        acquisition_helper.finish_saving(addresses, ['wii'])
        self.logger.info("Finish saving ended! Exiting app ...")
        sys.exit(0)
Ejemplo n.º 2
0
    def __init__(self, addresses):

        super(Downsampling, self).__init__(addresses=addresses,
                                           type=peers.CLIENT)
        self.new_fs = int(float(self.config.get_param("new_sampling_freq")))
        file_name = self.config.get_param("file_name")
        self.file_path = self.config.get_param("file_path")

        if not file_name:
            _file = acquisition_helper.get_file_path(self.file_path, file_name)

            try:
                files = [f for f in listdir(_file) if isfile(join(_file, f))]
            except OSError:
                self.logger.error(
                    "No such file or directory: {}".format(_file))
                sys.exit(1)

            self.old_file = [f.split(".")[0] for f in files if "raw" in f]
            self.logger.info(
                "Prepered to resampling {} files from {}... \n files list: {}".
                format(len(self.old_file), self.file_path, self.old_file))

        else:
            self.old_file = [file_name]
            self.logger.info(
                "Prepered to resampling {} file from {}...".format(
                    self.old_file[0], self.file_path))
        self.ready()
Ejemplo n.º 3
0
    def make_new_file(self, old_file):

        in_file = acquisition_helper.get_file_path(self.file_path, old_file)

        try:
            manager = read_manager.ReadManager(in_file+".obci.xml",
                                               in_file+".obci.raw",
                                               in_file+".obci.tag")
        except IOError:
            self.logger.error("No such file or directory: {}".format(in_file))
            sys.exit(1)

        fs = int(float(manager.get_param("sampling_frequency")))
        data = manager.get_samples()
        self.logger.info("START resampling: file: {}, old_fs = {} Hz, new_fs = {} Hz...".format(old_file, fs, self.new_fs))
        
        if not fs % self.new_fs == 0:
            self.logger.error("WRONG new sampling frequency!!\n You can choose sampling frequency, which is the divisor of old sampling frequency")
            sys.exit(0)
        else:
            sample_step = fs/self.new_fs

        new_data = np.array([data[:,i] for i in range(0, data.shape[1], sample_step)]).T
        manager.set_samples(new_data, manager.get_param("channels_names"))
        manager.set_param("sampling_frequency", str(self.new_fs))
        self.logger.info("FINISH resampling: file: {}, old_fs = {} Hz, new_fs = {} Hz".format(old_file, fs, self.new_fs))
        self.logger.info("START saving data to {}...".format(in_file + "_resampling_to_"+str(self.new_fs)))
        manager.save_to_file(self.file_path, in_file + "_resampling_to_"+str(self.new_fs))
        self.logger.info("FINISH saving new data")
Ejemplo n.º 4
0
    def _init_save_file_name(self):

        file_name = open(get_file_path(self.file_with_name_path, self.file_with_name),'r').read()
        full_save_file_name = file_name + self.file_name_addition
        full_config_name = file_name + self.config_name_addition
        self.config.set_param('save_file_name', full_save_file_name)
        self.config.set_param('config_file_name', full_config_name)
Ejemplo n.º 5
0
def get_csp_config(path, name):
    file_name = acquisition_helper.get_file_path(path, name)
    csp_file = file_name+'.csp'
    f = open(csp_file, 'r')
    d = pickle.load(f)
    f.close()
    LOGGER.info("Got csp config:")
    LOGGER.info(str(d))
    return d
Ejemplo n.º 6
0
def get_csp_config(path, name):
    file_name = acquisition_helper.get_file_path(path, name)
    csp_file = file_name + '.csp'
    f = open(csp_file, 'r')
    d = pickle.load(f)
    f.close()
    LOGGER.info("Got csp config:")
    LOGGER.info(str(d))
    return d
Ejemplo n.º 7
0
    def _init_save_file_name(self):

        file_name = open(
            get_file_path(self.file_with_name_path, self.file_with_name),
            'r').read()
        full_save_file_name = file_name + self.file_name_addition
        full_config_name = file_name + self.config_name_addition
        self.config.set_param('save_file_name', full_save_file_name)
        self.config.set_param('config_file_name', full_config_name)
Ejemplo n.º 8
0
    def _init_files(self):
        self.f_data = get_file_path(self.config.get_param('data_file_dir'), 
                                    self.config.get_param('data_file_name')+'.obci.raw')

        i_dir = self.config.get_param('info_file_dir')
        if len(i_dir) == 0:
            i_dir = self.config.get_param('data_file_dir')
        i_name = self.config.get_param('info_file_name')
        if len(i_name) == 0:
            i_name = self.config.get_param('data_file_name')
        self.f_info = get_file_path(i_dir, i_name+'.obci.xml')

        t_dir = self.config.get_param('tags_file_dir')
        if len(t_dir) == 0:
            t_dir = self.config.get_param('data_file_dir')
        t_name = self.config.get_param('tags_file_name')
        if len(t_name) == 0:
            t_name = self.config.get_param('data_file_name')
        self.f_tags = get_file_path(t_dir, t_name+'.obci.tag')
Ejemplo n.º 9
0
    def _init_files(self):
        self.f_data = get_file_path(
            self.config.get_param('data_file_dir'),
            self.config.get_param('data_file_name') + '.obci.raw')

        i_dir = self.config.get_param('info_file_dir')
        if len(i_dir) == 0:
            i_dir = self.config.get_param('data_file_dir')
        i_name = self.config.get_param('info_file_name')
        if len(i_name) == 0:
            i_name = self.config.get_param('data_file_name')
        self.f_info = get_file_path(i_dir, i_name + '.obci.xml')

        t_dir = self.config.get_param('tags_file_dir')
        if len(t_dir) == 0:
            t_dir = self.config.get_param('data_file_dir')
        t_name = self.config.get_param('tags_file_name')
        if len(t_name) == 0:
            t_name = self.config.get_param('data_file_name')
        self.f_tags = get_file_path(t_dir, t_name + '.obci.tag')
    def __init__(self, addresses):
        super(LogicVenturesCognitive, self).__init__(addresses=addresses,
                                                     type=peers.CLIENT)
        self.ready()

        user_id = self.get_param('user_id')
        self.logger.info("Starting cognitive maze for user: "+str(user_id))

        tag_name = self.get_param('save_file_name')
        tag_dir = acquisition_helper.get_file_path(self.get_param('save_file_path'), '')
        session_name = self.get_param('session_name')
        session_type = 'experiment' if session_name == 'ventures_game' else 'training'
        maze.MazeGame(user_id, tag_dir=tag_dir, tag_name=tag_name, session_type=session_type).run()
        sys.exit(0)
Ejemplo n.º 11
0
def calculate(file_path, file_name, show=True):
    file_name = acquisition_helper.get_file_path(file_path, file_name)
    w = WBBReadManager(file_name+'.obci.xml', file_name+'.obci.raw', file_name+'.obci.tag')
    w.get_x()
    w.get_y()
    w.mgr.set_param('sampling_frequency', estimate_fs(w.mgr.get_channel_samples('TSS')))
    w = wii_filter_signal(w, 20, 2, use_filtfilt=True)
    w = wii_downsample_signal(w, factor=2, pre_filter=False, use_filtfilt=False)
    smart_tags = wii_cut_fragments(w, start_tag_name='ss_start', end_tags_names=['ss_stop'])
    x = smart_tags[0].get_channel_samples('x')
    y = smart_tags[0].get_channel_samples('y')
    xa, ya = np.std(x)*5, np.mean(y)
    xb, yb = np.mean(x), np.std(y)*5
    xc, yc = np.mean(x), np.mean(y)
    if show:
        show_results(x, y, xa, ya, xb, yb, xc, yc)
    return xa, ya, xb, yb, xc, yc 
Ejemplo n.º 12
0
    def run(self):
        f_name = self.config.get_param("data_file_name")
        f_dir = self.config.get_param("data_file_path")
        context = ctx.get_new_context()
        context['logger'] = self.logger
        cfg = logic_ssvep_csp_fast_analysis.run(
            acquisition_helper.get_file_path(f_dir, f_name), self.use_channels,
            self.ignore_channels, self.montage, self.montage_channels,
            self.mode in ['offline', 'manual'], True, context)

        maybe_buffer = self.config.get_param('buffer_len')
        if len(maybe_buffer) > 0:
            self.logger.info("Overwrite csp buffer len:" + str(cfg['buffer']) +
                             "with from config:" + str(maybe_buffer))
            cfg['buffer'] = float(maybe_buffer)

        self.finish(cfg)
    def __init__(self, addresses):
        super(LogicVenturesCognitive, self).__init__(addresses=addresses,
                                                     type=peers.CLIENT)
        self.ready()

        user_id = self.get_param('user_id')
        self.logger.info("Starting cognitive maze for user: " + str(user_id))

        tag_name = self.get_param('save_file_name')
        tag_dir = acquisition_helper.get_file_path(
            self.get_param('save_file_path'), '')
        session_name = self.get_param('session_name')
        session_type = 'experiment' if session_name == 'ventures_game' else 'training'
        maze.MazeGame(user_id,
                      tag_dir=tag_dir,
                      tag_name=tag_name,
                      session_type=session_type).run()
        sys.exit(0)
    def run(self):
        f_name = self.config.get_param("data_file_name")
        f_dir = self.config.get_param("data_file_path")
        context = ctx.get_new_context()
        context['logger'] = self.logger
        cfg = logic_ssvep_csp_fast_analysis.run(
            acquisition_helper.get_file_path(f_dir, f_name),
            self.use_channels,
            self.ignore_channels,
            self.montage,
            self.montage_channels,
            self.mode in ['offline', 'manual'],
            True,
            context)

        maybe_buffer = self.config.get_param('buffer_len')
        if len(maybe_buffer) > 0:
            self.logger.info("Overwrite csp buffer len:"+str(cfg['buffer'])+"with from config:"+str(maybe_buffer))
            cfg['buffer'] = float(maybe_buffer)

        self.finish(cfg)
Ejemplo n.º 15
0
    def make_new_file(self, old_file):

        in_file = acquisition_helper.get_file_path(self.file_path, old_file)

        try:
            manager = read_manager.ReadManager(in_file + ".obci.xml",
                                               in_file + ".obci.raw",
                                               in_file + ".obci.tag")
        except IOError:
            self.logger.error("No such file or directory: {}".format(in_file))
            sys.exit(1)

        fs = int(float(manager.get_param("sampling_frequency")))
        data = manager.get_samples()
        self.logger.info(
            "START resampling: file: {}, old_fs = {} Hz, new_fs = {} Hz...".
            format(old_file, fs, self.new_fs))

        if not fs % self.new_fs == 0:
            self.logger.error(
                "WRONG new sampling frequency!!\n You can choose sampling frequency, which is the divisor of old sampling frequency"
            )
            sys.exit(0)
        else:
            sample_step = fs / self.new_fs

        new_data = np.array(
            [data[:, i] for i in range(0, data.shape[1], sample_step)]).T
        manager.set_samples(new_data, manager.get_param("channels_names"))
        manager.set_param("sampling_frequency", str(self.new_fs))
        self.logger.info(
            "FINISH resampling: file: {}, old_fs = {} Hz, new_fs = {} Hz".
            format(old_file, fs, self.new_fs))
        self.logger.info(
            "START saving data to {}...".format(in_file + "_resampling_to_" +
                                                str(self.new_fs)))
        manager.save_to_file(self.file_path,
                             in_file + "_resampling_to_" + str(self.new_fs))
        self.logger.info("FINISH saving new data")
Ejemplo n.º 16
0
    def __init__(self, addresses):

        super(Downsampling, self).__init__(addresses=addresses, type=peers.CLIENT)
        self.new_fs = int(float(self.config.get_param("new_sampling_freq")))
        file_name = self.config.get_param("file_name")
        self.file_path = self.config.get_param("file_path")

        if not file_name:
            _file = acquisition_helper.get_file_path(self.file_path, file_name)

            try:
                files = [f for f in listdir(_file) if isfile(join(_file, f))]
            except OSError:
                self.logger.error("No such file or directory: {}".format(_file))
                sys.exit(1)

            self.old_file = [f.split(".")[0] for f in files if "raw" in f]
            self.logger.info("Prepered to resampling {} files from {}... \n files list: {}".format(len(self.old_file), self.file_path, self.old_file))

        else:
            self.old_file = [file_name]
            self.logger.info("Prepered to resampling {} file from {}...".format(self.old_file[0], self.file_path))
        self.ready()
Ejemplo n.º 17
0
def calculate(file_path, file_name, show=True):
    file_name = acquisition_helper.get_file_path(file_path, file_name)
    w = WBBReadManager(file_name + '.obci.xml', file_name + '.obci.raw',
                       file_name + '.obci.tag')
    w.get_x()
    w.get_y()
    w.mgr.set_param('sampling_frequency',
                    estimate_fs(w.mgr.get_channel_samples('TSS')))
    w = wii_filter_signal(w, 20, 2, use_filtfilt=True)
    w = wii_downsample_signal(w,
                              factor=2,
                              pre_filter=False,
                              use_filtfilt=False)
    smart_tags = wii_cut_fragments(w,
                                   start_tag_name='ss_start',
                                   end_tags_names=['ss_stop'])
    x = smart_tags[0].get_channel_samples('x')
    y = smart_tags[0].get_channel_samples('y')
    xa, ya = np.std(x) * 5, np.mean(y)
    xb, yb = np.mean(x), np.std(y) * 5
    xc, yc = np.mean(x), np.mean(y)
    if show:
        show_results(x, y, xa, ya, xb, yb, xc, yc)
    return xa, ya, xb, yb, xc, yc
    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()
Ejemplo n.º 19
0
 def __init__(self, file_name='Enter.wav'):
     dir_name = os.path.join(settings.MAIN_DIR, 'utils/tagger_gui/sounds')
 	sound_file = acquisition_helper.get_file_path(dir_name, file_name)
     self.sound = pygame.mixer.Sound(sound_file)
Ejemplo n.º 20
0
def set_csp_config(path, name, config):
    csp_file = acquisition_helper.get_file_path(path, name) + '.csp'
    f = open(csp_file, 'w')
    pickle.dump(config, f)
    f.close()
Ejemplo n.º 21
0
def get_database_file_name(file_name):
    dir_name = os.path.join(settings.MAIN_DIR, 'exps/ventures/data')
    return acquisition_helper.get_file_path(dir_name, file_name)
Ejemplo n.º 22
0
def set_csp_config(path, name, config):
    csp_file = acquisition_helper.get_file_path(path, name)+'.csp'
    f = open(csp_file, 'w')
    pickle.dump(config, f)
    f.close()
Ejemplo n.º 23
0
    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()
Ejemplo n.º 24
0
    def run(self):
        self.logger.info("START CSP...")
        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)
        mgr = read_manager.ReadManager(in_file + ".obci.xml", in_file + ".obci.raw", in_file + ".obci.tag")
        fs = int(float(mgr.get_param("sampling_frequency")))
        if self.use_channels is None:
            self.use_channels = mgr.get_param("channels_names")
        if self.ignore_channels is not None:
            for i in self.ignore_channels:
                try:
                    self.use_channels.remove(i)
                except:
                    pass

        self.logger.info("USE CHANNELS: " + str(self.use_channels))
        csp_time = [0.0, 0.7]
        data = p300.p300_train(
            in_file + ".obci", self.use_channels, fs, self.montage_channels, self.montage, idx=1, csp_time=csp_time
        )  # idx oznacza indeks na który
        new_tags = data.wyr()
        not_idx_tags = data.data.get_p300_tags(idx=1, rest=True, samples=False)  # Tutaj idx musi być -idx z linijki 11
        # Tagi pozostałych przypadków
        mean, left, right = data.get_mean(new_tags, m_time=csp_time, plot_mean=True)
        buffer_start = int(-csp_time[0] * fs)
        buffer_len = len(mean)
        self.logger.info("Computer buffer start / len: " + str(buffer_start) + " / " + str(buffer_len))
        mean[:left] = 0
        mean[right:] = 0
        t_vec = data.show_mean_CSP(csp_time, new_tags)
        plt.plot(t_vec, mean, "r-")
        plt.show()
        sr = 12  # Maksymalna liczba odcinków do uśrednienia; gdzieś do parametryzacji
        # targets = np.zeros([sr, len(new_tags)])
        # non_targets = np.zeros([sr, len(not_idx_tags)])
        # mu = np.zeros(sr)
        # sigma = np.zeros(sr)
        # for i in xrange(1, sr + 1):
        # x = data.get_n_mean(i, new_tags, csp_time, 0.05)
        # targets[i - 1, :], non_targets[i - 1, :], mu[i - 1], sigma[i - 1] = x
        cl, mu, sigma, mean, left, right = data.prep_classifier(sr, P_vectors=2, reg=1, mean_time=csp_time)
        q = data
        cfg = {
            "mu": mu,
            "sigma": sigma,
            "mean": mean,
            #'targets':targets,
            #'non_targets':non_targets,
            "q": q,
            "buffer_len": buffer_len,
            "buffer_start": buffer_start,
            "use_channels": ";".join(self.use_channels),
            "montage": self.montage,
            "montage_channels": ";".join(self.montage_channels),
            "left": left,
            "right": right,
            "cl": cl,
            "a_features": data.a_features,
            "bands": data.bands,
        }

        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)
        self.logger.info("CSP DONE")
        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()
Ejemplo n.º 26
0
    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()
Ejemplo n.º 27
0
    def run(self):
        self.logger.info("START CSP...")
        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)
        mgr = read_manager.ReadManager(in_file + '.obci.xml',
                                       in_file + '.obci.raw',
                                       in_file + '.obci.tag')
        fs = int(float(mgr.get_param("sampling_frequency")))
        if self.use_channels is None:
            self.use_channels = mgr.get_param('channels_names')
        if self.ignore_channels is not None:
            for i in self.ignore_channels:
                try:
                    self.use_channels.remove(i)
                except:
                    pass

        self.logger.info("USE CHANNELS: " + str(self.use_channels))
        csp_time = [0.0, 0.7]
        data = p300.p300_train(in_file + '.obci',
                               self.use_channels,
                               fs,
                               self.montage_channels,
                               self.montage,
                               idx=1,
                               csp_time=csp_time)  #idx oznacza indeks na który
        new_tags = data.wyr()
        not_idx_tags = data.data.get_p300_tags(
            idx=1, rest=True,
            samples=False)  #Tutaj idx musi być -idx z linijki 11
        #Tagi pozostałych przypadków
        mean, left, right = data.get_mean(new_tags,
                                          m_time=csp_time,
                                          plot_mean=True)
        buffer_start = int(-csp_time[0] * fs)
        buffer_len = len(mean)
        self.logger.info("Computer buffer start / len: " + str(buffer_start) +
                         " / " + str(buffer_len))
        mean[:left] = 0
        mean[right:] = 0
        t_vec = data.show_mean_CSP(csp_time, new_tags)
        plt.plot(t_vec, mean, 'r-')
        plt.show()
        sr = 12  #Maksymalna liczba odcinków do uśrednienia; gdzieś do parametryzacji
        #targets = np.zeros([sr, len(new_tags)])
        #non_targets = np.zeros([sr, len(not_idx_tags)])
        #mu = np.zeros(sr)
        #sigma = np.zeros(sr)
        #for i in xrange(1, sr + 1):
        #x = data.get_n_mean(i, new_tags, csp_time, 0.05)
        #targets[i - 1, :], non_targets[i - 1, :], mu[i - 1], sigma[i - 1] = x
        cl, mu, sigma, mean, left, right = data.prep_classifier(
            sr, P_vectors=2, reg=1, mean_time=csp_time)
        q = data
        cfg = {
            'mu': mu,
            'sigma': sigma,
            'mean': mean,
            #'targets':targets,
            #'non_targets':non_targets,
            'q': q,
            'buffer_len': buffer_len,
            'buffer_start': buffer_start,
            'use_channels': ';'.join(self.use_channels),
            'montage': self.montage,
            'montage_channels': ';'.join(self.montage_channels),
            'left': left,
            'right': right,
            'cl': cl,
            'a_features': data.a_features,
            'bands': data.bands
        }

        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)
        self.logger.info("CSP DONE")
        if not self.run_offline:
            self._run_next_scenario()