def test_run(Simulator, algebra, seed): rng = np.random.RandomState(seed) vocab = spa.Vocabulary(32, pointer_gen=rng, algebra=algebra) vocab.populate('A; B') with spa.Network(seed=seed, vocabs=VocabularyMap([vocab])) as model: model.superpos = spa.Superposition(2, vocab=32) def inputA(t): if 0 <= t < 0.1: return 'A' else: return 'B' model.input = spa.Transcode(inputA, output_vocab=vocab) model.input >> model.superpos.inputs[0] spa.sym.A >> model.superpos.inputs[1] with model: p = nengo.Probe(model.superpos.output, synapse=0.03) with Simulator(model) as sim: sim.run(0.2) error = rmse(vocab.parse("(B+A).normalized()").v, sim.data[p][-1]) assert error < 0.1 error = rmse(vocab.parse("(A+A).normalized()").v, sim.data[p][100]) assert error < 0.2
def test_vocab_config(): with spa.Network() as model: with spa.Network() as model.shared_vocabs: pass with spa.Network(vocabs=VocabularyMap()) as model.non_shared_vocabs: pass assert model.shared_vocabs.vocabs is model.vocabs assert model.non_shared_vocabs.vocabs is not model.vocabs
def test_spa_vocab(): # create a model without a vocab and check that it is empty model = spa.Network() assert len(model.vocabs) == 0 # create a model with a vocab and check that it's filled va = spa.Vocabulary(16) va.populate("PANTS") vb = spa.Vocabulary(32) vb.populate("SHOES") model = spa.Network(vocabs=VocabularyMap([va, vb])) assert list(model.vocabs[16].keys()) == ["PANTS"] assert list(model.vocabs[32].keys()) == ["SHOES"] # warning on vocabs with duplicate dimensions vc = spa.Vocabulary(16) vc.populate("SOCKS") with pytest.warns(UserWarning): model = spa.Network(vocabs=VocabularyMap([va, vb, vc])) assert list(model.vocabs[16].keys()) == ["SOCKS"] assert list(model.vocabs[32].keys()) == ["SHOES"]
def test_vocabulary_map_param(): class Test(object): vocab_map = VocabularyMapParam('vocab_map', readonly=False) obj = Test() vm = VocabularyMap() v16 = Vocabulary(16) v32 = Vocabulary(32) obj.vocab_map = vm assert obj.vocab_map is vm obj.vocab_map = [v16, v32] assert obj.vocab_map[16] is v16 assert obj.vocab_map[32] is v32 with pytest.raises(ValidationError): obj.vocab_map = 'incompatible'
def __init__(self, label=None, seed=None, add_to_container=None, vocabs=None): super(Network, self).__init__(label, seed, add_to_container) self.config.configures(Network) if vocabs is None: vocabs = Config.default(Network, 'vocabs') if vocabs is None and len(Network.context) > 0: vocabs = self._master_vocabs.get(Network.context[0], None) if vocabs is None: if seed is not None: rng = np.random.RandomState(seed) else: rng = None vocabs = VocabularyMap(rng=rng) if len(Network.context) > 0: self.__class__._master_vocabs[Network.context[0]] = vocabs self.vocabs = vocabs self.config[Network].vocabs = vocabs self._stimuli = None
class Test(object): vocabs = VocabularyMap([v16]) vocab = VocabularyOrDimParam('vocab', readonly=False)
def test_vocabulary_set(rng): v8 = Vocabulary(8) v16 = Vocabulary(16) v32 = Vocabulary(32) vs = VocabularyMap([v8, v16], rng=rng) # Behaviour common to set and dict assert len(vs) == 2 assert 8 in vs assert 16 in vs assert 32 not in vs assert v8 in vs assert v16 in vs assert v32 not in vs assert Vocabulary(8) not in vs # dict behaviour assert vs[8] is v8 assert vs[16] is v16 del vs[8] assert 8 not in vs # set behaviour vs.add(v32) assert vs[32] is v32 with pytest.warns(UserWarning): vs.add(v32) vs.discard(32) assert 32 not in vs vs.discard(v16) assert 16 not in vs # creating new vocabs if non existent vs.add(v8) assert vs.get_or_create(8) is v8 new = vs.get_or_create(16) assert vs[16] is new assert new.dimensions == 16 assert new.pointer_gen.rng is rng