Пример #1
0
    def message(self, msg):
        """Message receive function for show_pairs toggling and reset"""
        vocab = self.vocab_out
        # Send the new labels
        if self.config.show_pairs:
            vocab.include_pairs = True
            if isinstance(vocab, spa.Vocabulary):
                self.data.append(
                    '["reset_legend_and_data", "%s"]' % (
                        '","'.join(vocab.keys + vocab.key_pairs)))
                # if we're starting to show pairs, track pair length
                self.old_pairs_length = len(vocab.key_pairs)

            else:
                self.data.append(
                    '["reset_legend_and_data", "%s"]' % (
                        '","'.join(set(vocab.keys()) | pairs(vocab))))
                # if we're starting to show pairs, track pair length
                self.old_pairs_length = len(pairs(vocab))
        else:
            vocab.include_pairs = False
            if isinstance(vocab, spa.Vocabulary):
                self.data.append('["reset_legend_and_data", "%s"]'
                             % ('","'.join(vocab.keys)))
            else:
                self.data.append('["reset_legend_and_data", "%s"]'
                             % ('","'.join(vocab)))
Пример #2
0
    def gather_data(self, t, x):
        vocab = self.vocab_out

        if isinstance(vocab, spa.Vocabulary):
            length = len(vocab.keys)
        else:
            length = len(vocab)
        if self.old_vocab_length != length:
            self.update_legend(vocab)

        # get the similarity and send it
        key_similarity = np.dot(vocab.vectors, x)
        simi_list = ['{:.2f}'.format(simi) for simi in key_similarity]

        if self.config.show_pairs:

            # briefly there can be no pairs, so catch the error
            try:
                if isinstance(vocab, spa.Vocabulary):
                    pair_similarity = np.dot(vocab.vector_pairs, x)
                else:
                    pair_similarity = (np.dot(vocab.parse(p).v, x) for p in pairs(vocab))
                simi_list += ['{:.2f}'.format(simi) for simi in pair_similarity]
            except TypeError:
                pass

        if(simi_list != []):
            self.data.append(  '["data_msg", %g, %s]'
                             %( t, ",".join(simi_list) )  )
Пример #3
0
    def gather_data(self, t, x):
        vocab = self.vocab_out
        key_similarities = np.dot(vocab.vectors, x)
        over_threshold = key_similarities > 0.01
        if isinstance(vocab, spa.Vocabulary):
            matches = zip(key_similarities[over_threshold],
                          np.array(vocab.keys)[over_threshold])
            if self.config.show_pairs:
                self.vocab_out.include_pairs = True
                pair_similarities = np.dot(vocab.vector_pairs, x)
                over_threshold = pair_similarities > 0.01
                pair_matches = zip(pair_similarities[over_threshold],
                                   np.array(vocab.key_pairs)[over_threshold])
                matches = itertools.chain(matches, pair_matches)
        else:
            matches = zip(
                key_similarities[over_threshold],
                [k for i, k in enumerate(vocab) if over_threshold[i]])
            if self.config.show_pairs:
                pair_similarities = np.array(
                    [np.dot(vocab.parse(p).v, x) for p in pairs(vocab)])
                over_threshold = pair_similarities > 0.01
                pair_matches = zip(
                    pair_similarities[over_threshold],
                    (k
                     for i, k in enumerate(pairs(vocab)) if over_threshold[i]))
                matches = itertools.chain(matches, pair_matches)

        text = ';'.join(
            ['%0.2f%s' % (min(sim, 9.99), key) for sim, key in matches])

        # msg sent as a string due to variable size of pointer names
        msg = '%g %s' % (t, text)
        self.data.append(msg)
        if self.override_target is None:
            return np.zeros(self.vocab_out.dimensions)
        else:
            v = (self.override_target.v - x) * 3
            return v
Пример #4
0
    def update_legend(self, vocab):
        # pass all the missing keys
        legend_update = []
        if isinstance(vocab, spa.Vocabulary):
            legend_update += (vocab.keys[self.old_vocab_length:])
            self.old_vocab_length = len(vocab.keys)
        else:
            legend_update += (list(vocab.keys())[self.old_vocab_length:])
            self.old_vocab_length = len(vocab)
        # and all the missing pairs if we're showing pairs
        if self.config.show_pairs:
            # briefly there can be no pairs, so catch the error
            try:
                key_pairs = list(pairs(vocab))
                legend_update += key_pairs[self.old_pairs_length:]
                self.old_pairs_length = len(key_pairs)
            except TypeError:
                pass

        self.data.append('["update_legend", "%s"]'
                         %('","'.join(legend_update)))
Пример #5
0
def test_pairs():
    v = Vocabulary(64)
    v.populate('A; B; C')
    actual = pairs(v)
    expected = {'A*B', 'A*C', 'B*C'}
    assert actual == expected
Пример #6
0
def test_pairs():
    v = Vocabulary(64)
    v.populate("A; B; C")
    actual = pairs(v)
    expected = {"A*B", "A*C", "B*C"}
    assert actual == expected