Esempio n. 1
0
def test_tensors():
    m = Message(tensors, vectors)
    t = m.tensors()
    assert t == TensorMessage(tensors)
    t = m.tensors(keys=['a'])
    assert t == TensorMessage({'a': tensors['a']})
    t = m.tensors(keys=['a','c'])
    assert t == TensorMessage({'a': tensors['a'], 'c': torch.Tensor(vectors['c'])})
Esempio n. 2
0
def test_TensorMessage_eq():

    a = [1,2,3]
    b = [4, 5, 6]

    # Test init
    email = TensorMessage({'a': a, 'b':b})
    gmail = TensorMessage(email)
Esempio n. 3
0
def test_append():

    t = tensors
    v = vectors
    m1 = Message(t, v)
    m2 = Message(t, v)
    m3 = Message(t)
    m4 = TensorMessage(t)
    m5 = Message(pd.DataFrame(v))
    m6 = pd.DataFrame(v)

    m0 = Message()
    assert(len(m0) == 0)
    m = m0.append(Message(t))
    assert m == Message(t)
    m = m0.append(Message(v))
    assert m == Message(v)
    m = m0.append(Message(t,v))
    assert m == Message(t,v)

    m = m1.append(m2)
    assert len(m) == 6
    assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}, {'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])})
    m = m3.append(t)
    assert len(m) == 6
    assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])})
    m = m3.append(m3)
    assert len(m) == 6
    assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])})
    m = m3.append(m4)
    assert len(m) == 6
    assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])})

    m = m4.append(t)
    assert len(m) == 6
    assert m == TensorMessage({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])})
    m = m4.append(m3)
    assert len(m) == 6
    assert m == TensorMessage({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])})
    m = m4.append(m4)
    assert len(m) == 6
    assert m == TensorMessage({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])})

    m = m5.append(v)
    assert len(m) == 6
    assert m == Message({'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])})
    m = m5.append(m5)
    assert len(m) == 6
    assert m == Message({'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])})
    m = m5.append(m6)
    assert len(m) == 6
    assert m == Message({'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])})

    # Test type conversions on appending to TensorMessage
    m = m4.append({'a': np.array([42]), 'b': np.array([24])})
    assert len(m) == 4
    assert m == TensorMessage({'a': torch.Tensor([1,2,3,42]), 'b': torch.Tensor([4,5,6,24])})
Esempio n. 4
0
def test_join():

    t = tensors
    v = vectors
    t2 = {'d': torch.Tensor([13,14,15])}
    v2 = {'e': np.array([16,17,18])}
    m1 = Message(t,v)
    m2 = Message(t)
    m2_t = TensorMessage(t)
    m3 = Message(v)
    m4 = Message(t2,v2)
    m5 = Message(t2)
    m5_t = TensorMessage(t2)
    m6 = Message(v2)
    m7 = Message(t,v2)
    m8 = Message(t2, v)

    # Test if a tensor message can be merged into a message and vice versa
    assert m2.merge(m3) == m1
    assert m3.merge(m2) == m1
    assert m3.merge(m2_t) == m1
    assert m3.merge(t) == m1

    # Test if the tensors in messages can be merged
    assert m2.merge(t2) == Message({**t, **t2})
    assert m2.merge(m5) == Message({**t, **t2})
    assert m2.merge(m5_t) == Message({**t, **t2})
    assert m2_t.merge(t2) == TensorMessage({**t, **t2})
    assert m2_t.merge(m5) == TensorMessage({**t, **t2})
    assert m2_t.merge(m5_t) == TensorMessage({**t, **t2})

    # Test if the dataframes in messages can be merged
    assert m3.merge(m6) == Message({**v, **v2})
    assert m6.merge(m3) == Message({**v, **v2})
    assert m3.merge(v2) == Message({**v, **v2})
Esempio n. 5
0
def test_TensorMessage_set_get_del():

    a = [1, 2, 3]
    b = [4, 5, 6]
    email = TensorMessage({'a': a, 'b': b})
    replacement = {'a': torch.Tensor([42]), 'b': torch.Tensor([43])}
    gmail = TensorMessage({
        'a': torch.Tensor([42, 2, 3]),
        'b': torch.Tensor([43, 5, 6])
    })
    yahoomail = TensorMessage({
        'a': torch.Tensor([2, 3]),
        'b': torch.Tensor([5, 6])
    })
    assert email != gmail
    email[0] = replacement
    assert email == gmail
    assert len(email) == 3
    email['a'] = torch.Tensor([9, 9, 9])
    assert torch.equal(email['a'], torch.Tensor([9, 9, 9]))
    assert gmail != yahoomail
    del gmail[0]
    assert len(gmail) == 2
    assert gmail == yahoomail
    # Test column deletions
    email = TensorMessage({'a': a, 'b': b})
    assert set(email.columns) == set(['a', 'b'])
    del email['a']
    assert set(email.columns) == set(['b'])
    # Test that out of bounds requests raise errors
    try:
        email[3]
        assert False
    except IndexError:
        assert True
    try:
        email[3:5]
        assert False
    except IndexError:
        assert True

    # Test length adjustment if all columns are deleted
    zohomail = TensorMessage({'a': a, 'b': b})
    assert len(zohomail) == 3
    del zohomail['a']
    assert len(zohomail) == 3
    del zohomail['b']
    assert len(zohomail) == 0
Esempio n. 6
0
def test_TensorMessage_permute():
    a = [1,2,3]
    b = [4, 5, 6]
    email = TensorMessage({'a': a, 'b':b})
    gmail = email.permute([2,1,0])
    assert gmail == TensorMessage({'a':[3,2,1], 'b':[6,5,4]})
    gmail = email.permute([0,0,0])
    assert gmail == TensorMessage({'a':[1,1,1], 'b':[4,4,4]})
Esempio n. 7
0
def test_Message_iter():

    m = Message(tensors, vectors)
    l = len(m)
    for x,i in zip(m, count()):
        assert type(x) is Message
        if i > l:
            assert False
    assert i == l - 1

    t = TensorMessage(tensors)
    l = len(t)
    for x,i in zip(t, count()):
        assert type(x) is TensorMessage
        if i > l:
            assert False
    assert i == l - 1
Esempio n. 8
0
def test_TensorMessage():

    a = [1,2,3]
    b = [4, 5, 6]

    # Test init
    empty = TensorMessage()
    assert len(empty) == 0
    assert empty.keys() == {}.keys()
    email = TensorMessage({'a': a, 'b':b})

    #   Test error cases
    # TODO: test error cases

    # Test length
    assert len(email) == 3
    # Test getitem
    x = email[2]
    assert set(x.keys()) == set(['a','b'])
    assert (x['a'] == torch.Tensor([3])).all()
    assert (x['b'] == torch.Tensor([6])).all()
    x = email[0:2]
    assert set(x.keys()) == set(['a','b'])
    assert (x['a'] == torch.Tensor([1,2])).all()
    assert (x['b'] == torch.Tensor([4,5])).all()

    # Test for length 1 init
    gmail = TensorMessage({'a':1, 'b': 80})
    assert len(gmail) == 1
    y = gmail[0]
    assert set(y.keys()) == set(['a','b'])
    assert (y['a'] == torch.Tensor([1])).all()
    assert (y['b'] == torch.Tensor([80])).all()

    # Test extend
    yahoomail = email.append(gmail)
    assert len(yahoomail) == 4
    z = yahoomail[0:4]
    assert set(z.keys()) == set(['a','b'])
    assert (z['a'] == torch.Tensor([1,2,3,1])).all()
    assert (z['b'] == torch.Tensor([4, 5, 6, 80])).all()
Esempio n. 9
0
def test_Message():
    """ Test init, getitem, and len methopl. """

    def attribute_test(message, length = 3):
        assert len(message) == length
        assert message[0].tensors() == {
            'a': torch.Tensor([1]),
            'b': torch.Tensor([4]),
        }
        assert message[0].dataframe().equals(pd.DataFrame({
            'c': np.array([7]),
            'd': np.array([10]),
        }))

        assert message[0] == Message({'a': torch.Tensor([1]),'b': torch.Tensor([4])}, pd.DataFrame({'c': np.array([7]),'d': np.array([10]),}))

        assert message[1:3].tensors() == {
            'a': torch.Tensor([2,3]),
            'b': torch.Tensor([5,6]),
        }
        assert message[1:3].dataframe().equals(pd.DataFrame({
            'c': np.array([8,9]),
            'd': np.array([11,12]),
        }))

        assert (message['a'] == torch.Tensor([1,2,3])).all()
        assert message[['a','c']] == Message({'a': torch.Tensor([1,2,3]), 'c': np.array([7,8,9])})

        assert message[1:3] == Message({'a': torch.Tensor([2,3]),'b': torch.Tensor([5,6])}, pd.DataFrame({'c': np.array([8,9]),'d': np.array([11,12])}))

        # Test length
        assert len(message) == length
        # Test __getitem__

    # Init empty message
    m = Message()
    assert len(m) == 0
    # Init message from tensor_dict / TensorMessage and dict of arrays / dataframe using positional arguments.
    tensor_message = TensorMessage(tensors)
    tensor_as_message = Message(tensors = tensors)
    df = pd.DataFrame(vectors)
    df_as_message = Message(df = vectors)

    # Try every combination
    tensor_options = [tensors, tensor_message, tensor_as_message]
    vector_options = [vectors, df, df_as_message]

    for t, v in product(tensor_options, vector_options):
        m = Message(t, v)
        attribute_test(m)
        m = Message(tensors = t, df = v)
        attribute_test(m)

    # Test one sided Messages
    for t in tensor_options:
        m = Message(t, None)
        assert len(m) == 3
        assert m == Message(tensors)
    for v in vector_options:
        m = Message(None, v)
        assert len(m) == 3
        assert m == Message(vectors)

    # Init message from a single dict
    everything = {**tensors, **vectors}
    m = Message(everything)
    attribute_test(m)