Exemple #1
0
def test_readonly(rng):
    v1 = Vocabulary(32, pointer_gen=rng)
    v1.populate('A;B;C')

    v1.readonly = True

    with pytest.raises(ValueError):
        v1.parse('D')
Exemple #2
0
def test_populate(rng):
    v = Vocabulary(64, pointer_gen=rng)

    v.populate('')
    v.populate(' \r\n\t')
    assert len(v) == 0

    v.populate('A')
    assert 'A' in v

    v.populate('B; C')
    assert 'B' in v
    assert 'C' in v

    v.populate('D.unitary()')
    assert 'D' in v
    np.testing.assert_almost_equal(np.linalg.norm(v['D'].v), 1.)
    np.testing.assert_almost_equal(np.linalg.norm((v['D'] * v['D']).v), 1.)

    v.populate('E = A + 2 * B')
    assert np.allclose(v['E'].v, v.parse('A + 2 * B').v)
    assert np.linalg.norm(v['E'].v) > 2.

    v.populate('F = (A + 2 * B).normalized()')
    assert np.allclose(v['F'].v, v.parse('A + 2 * B').normalized().v)
    np.testing.assert_almost_equal(np.linalg.norm(v['F'].v), 1.)

    v.populate('G = A; H')
    assert np.allclose(v['G'].v, v['A'].v)
    assert 'H' in v

    # Assigning non-existing pointer
    with pytest.raises(NameError):
        v.populate('I = J')

    # Redefining
    with pytest.raises(ValidationError):
        v.populate('H = A')

    # Calling non existing function
    with pytest.raises(AttributeError):
        v.populate('I = H.invalid()')

    # invalid names: lowercase, unicode
    with pytest.raises(SpaParseError):
        v.populate('x = A')
    with pytest.raises(SpaParseError):
        v.populate(u'Aα = A')
Exemple #3
0
def test_populate(rng):
    v = Vocabulary(64, pointer_gen=rng)

    v.populate("")
    v.populate(" \r\n\t")
    assert len(v) == 0

    v.populate("A")
    assert "A" in v

    v.populate("B; C")
    assert "B" in v
    assert "C" in v

    v.populate("D.unitary()")
    assert "D" in v
    np.testing.assert_almost_equal(np.linalg.norm(v["D"].v), 1.0)
    np.testing.assert_almost_equal(np.linalg.norm((v["D"] * v["D"]).v), 1.0)

    v.populate("E = A + 2 * B")
    assert np.allclose(v["E"].v, v.parse("A + 2 * B").v)
    assert np.linalg.norm(v["E"].v) > 2.0

    v.populate("F = (A + 2 * B).normalized()")
    assert np.allclose(v["F"].v, v.parse("A + 2 * B").normalized().v)
    np.testing.assert_almost_equal(np.linalg.norm(v["F"].v), 1.0)

    v.populate("G = A; H")
    assert np.allclose(v["G"].v, v["A"].v)
    assert "H" in v

    # Assigning non-existing pointer
    with pytest.raises(NameError):
        v.populate("I = J")

    # Redefining
    with pytest.raises(ValidationError):
        v.populate("H = A")

    # Calling non existing function
    with pytest.raises(AttributeError):
        v.populate("I = H.invalid()")

    # invalid names: lowercase, unicode
    with pytest.raises(SpaParseError):
        v.populate("x = A")
    with pytest.raises(SpaParseError):
        v.populate(u"Aα = A")
Exemple #4
0
def test_parse_n(rng):
    v = Vocabulary(64, pointer_gen=rng)
    v.populate('A; B; C')
    A = v.parse('A')
    B = v.parse('B')

    parsed = v.parse_n('A', 'A*B', 'A+B', '3')
    assert np.allclose(parsed[0].v, A.v)
    assert np.allclose(parsed[1].v, (A * B).v)
    assert np.allclose(parsed[2].v, (A + B).v)
    # FIXME should give an exception?
    assert np.allclose(parsed[3].v, 3 * v['Identity'].v)
Exemple #5
0
def test_parse_n(rng):
    v = Vocabulary(64, pointer_gen=rng)
    v.populate("A; B; C")
    A = v.parse("A")
    B = v.parse("B")

    parsed = v.parse_n("A", "A*B", "A+B", "3")
    assert np.allclose(parsed[0].v, A.v)
    assert np.allclose(parsed[1].v, (A * B).v)
    assert np.allclose(parsed[2].v, (A + B).v)
    # FIXME should give an exception?
    assert np.allclose(parsed[3].v, 3 * v["Identity"].v)
Exemple #6
0
def test_transform(recwarn, rng, solver):
    v1 = Vocabulary(32, strict=False, pointer_gen=rng)
    v2 = Vocabulary(64, strict=False, pointer_gen=rng)
    v1.populate('A; B; C')
    v2.populate('A; B; C')
    A = v1['A']
    B = v1['B']
    C = v1['C']

    # Test transform from v1 to v2 (full vocbulary)
    # Expected: np.dot(t, A.v) ~= v2.parse('A')
    # Expected: np.dot(t, B.v) ~= v2.parse('B')
    # Expected: np.dot(t, C.v) ~= v2.parse('C')
    t = v1.transform_to(v2, solver=solver)

    assert v2.parse('A').compare(np.dot(t, A.v)) > 0.85
    assert v2.parse('C+B').compare(np.dot(t, C.v + B.v)) > 0.85

    # Test transform from v1 to v2 (only 'A' and 'B')
    t = v1.transform_to(v2, keys=['A', 'B'], solver=solver)

    assert v2.parse('A').compare(np.dot(t, A.v)) > 0.85
    assert v2.parse('B').compare(np.dot(t, C.v + B.v)) > 0.85

    # Test warns on missing keys
    v1.populate('D')
    D = v1['D']
    with pytest.warns(NengoWarning):
        v1.transform_to(v2, solver=solver)

    # Test populating missing keys
    t = v1.transform_to(v2, populate=True, solver=solver)
    assert v2.parse('D').compare(np.dot(t, D.v)) > 0.85

    # Test ignores missing keys in source vocab
    v2.populate('E')
    v1.transform_to(v2, populate=True, solver=solver)
    assert 'E' not in v1
def test_am_spa_keys_as_expressions(Simulator, plt, seed, rng):
    """Provide semantic pointer expressions as input and output keys."""
    d = 64

    vocab_in = Vocabulary(d, pointer_gen=rng)
    vocab_out = Vocabulary(d, pointer_gen=rng)

    vocab_in.populate("A; B")
    vocab_out.populate("C; D")

    in_keys = ["A", "A*B"]
    out_keys = ["C*D", "C+D"]
    mapping = dict(zip(in_keys, out_keys))

    with spa.Network(seed=seed) as m:
        m.am = ThresholdingAssocMem(threshold=0.3,
                                    input_vocab=vocab_in,
                                    output_vocab=vocab_out,
                                    mapping=mapping)

        m.inp = spa.Transcode(lambda t: "A" if t < 0.1 else "A*B",
                              output_vocab=vocab_in)
        m.inp >> m.am

        in_p = nengo.Probe(m.am.input)
        out_p = nengo.Probe(m.am.output, synapse=0.03)

    with nengo.Simulator(m) as sim:
        sim.run(0.2)

    # Specify t ranges
    t = sim.trange()
    t_item1 = (t > 0.075) & (t < 0.1)
    t_item2 = (t > 0.175) & (t < 0.2)

    # Modify vocabularies (for plotting purposes)
    vocab_in.add("AxB", vocab_in.parse(in_keys[1]).v)
    vocab_out.add("CxD", vocab_out.parse(out_keys[0]).v)

    plt.subplot(2, 1, 1)
    plt.plot(t, similarity(sim.data[in_p], vocab_in))
    plt.ylabel("Input: " + ", ".join(in_keys))
    plt.legend(vocab_in.keys(), loc="best")
    plt.ylim(top=1.1)
    plt.subplot(2, 1, 2)
    for t_item, c, k in zip([t_item1, t_item2], ["b", "g"], out_keys):
        plt.plot(
            t,
            similarity(sim.data[out_p], [vocab_out.parse(k).v],
                       normalize=True),
            label=k,
            c=c,
        )
        plt.plot(t[t_item], np.ones(t.shape)[t_item] * 0.9, c=c, lw=2)
    plt.ylabel("Output: " + ", ".join(out_keys))
    plt.legend(loc="best")

    assert (np.mean(
        similarity(sim.data[out_p][t_item1],
                   vocab_out.parse(out_keys[0]).v,
                   normalize=True)) > 0.9)
    assert (np.mean(
        similarity(sim.data[out_p][t_item2],
                   vocab_out.parse(out_keys[1]).v,
                   normalize=True)) > 0.9)
Exemple #8
0
def test_special_sps(name, sp):
    v = Vocabulary(16)
    assert name in v
    assert np.allclose(v[name].v, sp(16).v)
    assert np.allclose(v.parse(name).v, sp(16).v)
Exemple #9
0
def test_pointer_names():
    v = Vocabulary(16)
    v.populate('A; B')

    assert v['A'].name == 'A'
    assert v.parse('A*B').name == '(A)*(B)'
Exemple #10
0
def test_vocabulary_tracking(rng):
    v = Vocabulary(32, pointer_gen=rng)
    v.populate('A')

    assert v['A'].vocab is v
    assert v.parse('2 * A').vocab is v
Exemple #11
0
def test_capital(rng):
    v = Vocabulary(16, pointer_gen=rng)
    with pytest.raises(SpaParseError):
        v.parse('a')
    with pytest.raises(SpaParseError):
        v.parse('A+B+C+a')
Exemple #12
0
def test_parse(rng):
    v = Vocabulary(64, pointer_gen=rng)
    v.populate('A; B; C')
    A = v.parse('A')
    B = v.parse('B')
    C = v.parse('C')
    assert np.allclose((A * B).v, v.parse('A * B').v)
    assert np.allclose((A * ~B).v, v.parse('A * ~B').v)
    assert np.allclose((A + B).v, v.parse('A + B').v)
    assert np.allclose((A - (B * C) * 3 + ~C).v, v.parse('A-(B*C)*3+~C').v)

    assert np.allclose(v.parse('0').v, np.zeros(64))
    assert np.allclose(v.parse('1').v, np.eye(64)[0])
    assert np.allclose(v.parse('1.7').v, np.eye(64)[0] * 1.7)

    with pytest.raises(SyntaxError):
        v.parse('A((')
    with pytest.raises(SpaParseError):
        v.parse('"hello"')
    with pytest.raises(SpaParseError):
        v.parse('"hello"')
Exemple #13
0
def test_pointer_names():
    v = Vocabulary(16)
    v.populate("A; B")

    assert v["A"].name == "A"
    assert v.parse("A*B").name == "(A)*(B)"
Exemple #14
0
def test_vocabulary_tracking(rng):
    v = Vocabulary(32, pointer_gen=rng)
    v.populate("A")

    assert v["A"].vocab is v
    assert v.parse("2 * A").vocab is v
Exemple #15
0
def test_parse(rng):
    v = Vocabulary(64, pointer_gen=rng)
    v.populate("A; B; C")
    A = v.parse("A")
    B = v.parse("B")
    C = v.parse("C")
    assert np.allclose((A * B).v, v.parse("A * B").v)
    assert np.allclose((A * ~B).v, v.parse("A * ~B").v)
    assert np.allclose((A + B).v, v.parse("A + B").v)
    assert np.allclose((A - (B * C) * 3 + ~C).v, v.parse("A-(B*C)*3+~C").v)

    assert np.allclose(v.parse("0").v, np.zeros(64))
    assert np.allclose(v.parse("1").v, np.eye(64)[0])
    assert np.allclose(v.parse("1.7").v, np.eye(64)[0] * 1.7)

    with pytest.raises(SyntaxError):
        v.parse("A((")
    with pytest.raises(SpaParseError):
        v.parse('"hello"')
    with pytest.raises(SpaParseError):
        v.parse('"hello"')