Esempio n. 1
0
def test_score(model_name,parameter):

    label2id = {'Other': 0, 'Cause-Effect(e1,e2)': 1, 'Cause-Effect(e2,e1)': 2, 'Component-Whole(e1,e2)': 3,
                'Component-Whole(e2,e1)': 4, 'Content-Container(e1,e2)': 5, 'Content-Container(e2,e1)': 6, 
                'Entity-Destination(e1,e2)': 7, 'Entity-Destination(e2,e1)': 8, 'Entity-Origin(e1,e2)': 9, 
                'Entity-Origin(e2,e1)': 10, 'Instrument-Agency(e2,e1)': 11, 'Instrument-Agency(e1,e2)': 12, 
                'Member-Collection(e1,e2)': 13, 'Member-Collection(e2,e1)': 14, 'Message-Topic(e1,e2)': 15, 
                'Message-Topic(e2,e1)': 16, 'Product-Producer(e1,e2)': 17, 'Product-Producer(e2,e1)': 18}

    #加载预训练语料
    vocab_file = 'dataset/vocab/vocab.pkl'
    vocab = Vocab(vocab_file, load=True)
    resoult_file=open('resoult/resoult.txt','w')

    #加载预处理测试集
    test_batch = DataLoader('dataset/sem/test_file.json',parameter, vocab, train=False)
    test_emb_file = './dataset/vocab/embedding.npy'
    test_emb_matrix = np.load(test_emb_file)
    parameter['vocab_size'] = vocab.size

    #加载模型
    trainer = GCNTrainer(parameter,test_emb_matrix)
    trainer.load(model_name)
    id2label = dict([(v,k) for k,v in label2id.items()])
    predictions = []
    for i, batch in enumerate(test_batch):
        preds = trainer.predict(batch)
        predictions += preds
    predictions = [id2label[p] for p in predictions]

    #结果写入文件
    counter=8001
    for resoult in predictions:
        if counter == 10718:
            break
        resoult_file.writelines(str(counter)+"\t"+resoult+'\n')
        counter+=1
    resoult_file.close()

    #计算分数
    os.system('perl ./resoult/semeval2010_task8_scorer-v1.2.pl ./resoult/resoult.txt ./resoult/test_key.txt > ./resoult/score.txt')
    f=open("resoult/score.txt")

    resoult1=""
    line=f.readline()
    i=0
    while line:
        if i==143:
            resoult1=line
        if i==147:
            break
        line=f.readline()
        i+=1
    f.close()
    return line,resoult1
Esempio n. 2
0
    def __init__(self, addresses, frequency, window):
        """Initialization - super() and ready() calls are required..."""
        super(SampleAnalysis, self).__init__(addresses=addresses, type=peers.ANALYSIS)

        self.eog_analyser = EogAnalyser(frequency, 0)
        high_pass = HighpassFilter(frequency, 0.1)
        self.data_loader = DataLoader([high_pass], 2, frequency)

        self.data_loader.set_window(window)
        self.plotter = SignalPlotter(self.data_loader.prepare_timeline())

        self.ready()
        LOGGER.info("Sample analysis init finished!")
Esempio n. 3
0
    def start_train(self):
        #加载预训练语料
        vocab_file = 'dataset/vocab/vocab.pkl'
        self.vocab = Vocab(vocab_file, load=True)
        self.parameter['vocab_size'] = self.vocab.size
        emb_file = './dataset/vocab/embedding.npy'
        self.emb_matrix = np.load(emb_file)

        #加载训练集
        self.train_batch = DataLoader('dataset/sem/train_file.json',
                                      self.parameter,
                                      self.vocab,
                                      train=True)
        self.trainer = GCNTrainer(self.parameter, emb_matrix=self.emb_matrix)
        self.current_lr = self.parameter['lr']
        self.log_name = "log/" + str(time.strftime("%d_%I_%M")) + ".log"
        log = open(self.log_name, 'w+')
        log.write(str(self.parameter))
        log.close()
Esempio n. 4
0
            type = "float",
            default = 256.0,
            help = "Data registration frequency.")

    # parse command-line arguments, exits when fatal error
    options, args = parser.parse_args()

    if not options.input_file:
        print "Input file required; type -h for help."
        sys.exit(-1)

    file_loader  = FileLoader(options)
    eog_analyser = EogAnalyser(options.frequency, 0)
    high_pass    = HighpassFilter(options.frequency, 0.1)
    data_loader  = DataLoader([high_pass],
            2,
            options.frequency)

    data_loader.set_window(64)
    data_loader.set_leftover(4)
    plotter      = SignalPlotter(data_loader.prepare_timeline())
    tracker      = EyeTracker()
    tracker.run()

    while True:
        s = file_loader.get_sample()
        if s is None:
            break
        a = numpy.array(s)
        data_loader.load_pack(a)
        signal = data_loader.get_signal_set()
Esempio n. 5
0
class SampleAnalysis(ConfiguredMultiplexerServer):
    """A class responsible for handling signal message and making proper decision.
    The class inherits from generic class for convinience - all technical stuff
    is being done in this super-class"""
    def __init__(self, addresses, frequency, window, leftover):
        """Initialization - super() and ready() calls are required..."""
        super(SampleAnalysis, self).__init__(addresses=addresses,
                                          type=peers.ANALYSIS)

        self.eog_analyser = EogAnalyser(frequency, 0)
        high_pass         = HighpassFilter(frequency, 0.1)
        self.data_loader       = DataLoader([high_pass],
                2,
                frequency)

        self.data_loader.set_window(window)
        self.data_loader.set_leftover(leftover)
        self.plotter      = SignalPlotter(self.data_loader.prepare_timeline())
        self.tracker      = EyeTracker()
        self.tracker.run()

        self.ready()
        LOGGER.info("Sample analysis init finished!")

    def handle_message(self, mxmsg):
        """The only required function in the class
        that will be fired every time message is received"""
        if mxmsg.type == types.AMPLIFIER_SIGNAL_MESSAGE:
            # Got proper message, let`s unpack it ...

            # Messages are transmitted in bunches so lets define SampleVector
            # in order to unpack bunch of Sample messages ...
            l_vect = variables_pb2.SampleVector()
            l_vect.ParseFromString(mxmsg.message)

            # Now we have message unpacked, lets iterate over every sample ...
            for s in l_vect.samples:
                 # Every sample has two fields:
                # timestamp - system clock time of a moment of Sample`s creation
                # channels - a list of values - one for every channel
                #LOGGER.debug("Got sample with timestamp: "+str(s.timestamp))

                # One can copy samples to numpy array ...

                a = numpy.array(s.channels) # w tym miejscu mamy w tablicy a "paczke" próbek (domyślnie 4próbki ) ze wszystkich zadeklarowanych kanalow
                self.data_loader.load_pack(a)
                signal = self.data_loader.get_signal_set()

                if signal:
                    self.eog_analyser.load_signal_set(signal)
                    decisions =  self.eog_analyser.get_decisions(window = 10,
                            window_seconds = False,
                            jump = 1000,
                            min_sec_diff = 1.5)
                    for direction, time in decisions:
                        tracker.move_square(direction)                            
                    

            # Having a new bunch of values one can fire some magic analysis and
            # generate decision ....

            # Below we have quite simple decision-maker - it generates a random
            # decision every ~100 samples-bunch
########## TU NA PODSTAWIE ANALLIZY PODEJMUJEMY DECYZJE I MOZEMY JA PRZEKAZAC DO RESZTY SYSTEMU OBCI ######################
########## W TYM PRZYKLADZIE JEST TO LOSOWA DECYZJA ##############
########## W TYM CWICZENIU WYSTARCZY JESLI WYPISZECIE DECYZJE NA EKRAN ###########
            if random.random() > 0.99:
                # Here we send DECISION message somewhere-to-the-system ...
                # It's up to scenario's configuration how the decision will be used ...
                # Eg. it might be used by LOGIC module to push some button in speller.
                self.conn.send_message(message = str(random.randint(0,7)),
                                       type = types.DECISION_MESSAGE,
                                       flush=True)
        else:
            LOGGER.warning("Got unrecognised message type: "+str(mxmsg.type))

        # Tell the system 'I`ll not respond to this message, I`m just receiving'
        self.no_response()