Exemple #1
0
def test_autoblend():
    '''
    If weights are not specified explicitly, Blend computes them automatically
    so as to maximize the amount of interaction between the two matrices.

    This is hard to test in general. The strategy used here is to
    blend two copies of the same matrix (so the singular values are
    the same), but with different labels.
    '''

    t1 = ez_matrix('0013', '0421', [1,2,3,4])
    t2 = ez_matrix('2214', '0421', [3,6,9,12]) # one overlapping label, 3x the values
    b = Blend([t1, t2]) # don't specify weights => autoblend.

    eq_(b.label_overlap[0], 1)
    eq_(b.label_overlap[1], 4)

    # This should result in t2 getting weighted 1/3 the weight of t1:
    logging.info(b.weights)
    assert allclose(b.weights, [.75, .25])

    # Test the resulting tensor
    b.build_tensor()
    # -non-overlapping elements
    assert t1['0', '0'] == 1
    assert_almost_equal(b['0', '0'], .75*1) # remember that the original tensors had non-unity values.
    assert t2['4', '1'] == 4*3
    assert_almost_equal(b['4', '1'], .25*4*3)
    # -overlapping element
    assert t1['1', '2'] == 3
    assert t2['1', '2'] == 3*3
    assert_almost_equal(b['1', '2'],  0.75*3 + 0.25*3*3) # just to be explicit...
Exemple #2
0
def test_manual_weights():
    '''
    Specifying weights manually causes the result to be the weighted sum.
    '''
    b = Blend([t1, t2], weights=[.75, .25])
    b.build_tensor()
    expected = dict(zip(zip('aabc', '1211'), (.75, 1.0, .75, .25)))
    assertTensorEqualCompleteDict(b, expected)
Exemple #3
0
def test_manual_weights():
    '''
    Specifying weights manually causes the result to be the weighted sum.
    '''
    b = Blend([t1, t2], weights=[.75,.25])
    b.build_tensor()
    expected = dict(zip(zip('aabc', '1211'), (.75, 1.0, .75, .25)))
    assertTensorEqualCompleteDict(b, expected)
Exemple #4
0
def test_no_overlap():
    '''
    Certain optimizations are possible if there is no overlap.
    '''
    t2 = ez_matrix('dc', '21', [1] * 2)
    b = Blend([t1, t2], weights=[.75, .25])
    b.build_tensor()
    expected = dict(zip(zip('aabdc', '12121'), (.75, .75, .75, .25, .25)))
    assertTensorEqualCompleteDict(b, expected)
Exemple #5
0
def test_no_overlap():
    '''
    Certain optimizations are possible if there is no overlap.
    '''
    t2 = ez_matrix('dc', '21', [1]*2)
    b = Blend([t1, t2], weights=[.75,.25])
    b.build_tensor()
    expected = dict(zip(zip('aabdc', '12121'), (.75, .75, .75, .25, .25)))
    assertTensorEqualCompleteDict(b, expected)
Exemple #6
0
def test_autoblend():
    '''
    If weights are not specified explicitly, Blend computes them automatically
    so as to maximize the amount of interaction between the two matrices.

    This is hard to test in general. The strategy used here is to
    blend two copies of the same matrix (so the singular values are
    the same), but with different labels.
    '''

    t1 = ez_matrix('0013', '0421', [1, 2, 3, 4])
    t2 = ez_matrix('2214', '0421',
                   [3, 6, 9, 12])  # one overlapping label, 3x the values
    b = Blend([t1, t2])  # don't specify weights => autoblend.

    eq_(b.label_overlap[0], 1)
    eq_(b.label_overlap[1], 4)

    # This should result in t2 getting weighted 1/3 the weight of t1:
    logging.info(b.weights)
    assert allclose(b.weights, [.75, .25])

    # Test the resulting tensor
    b.build_tensor()
    # -non-overlapping elements
    assert t1['0', '0'] == 1
    assert_almost_equal(
        b['0', '0'],
        .75 * 1)  # remember that the original tensors had non-unity values.
    assert t2['4', '1'] == 4 * 3
    assert_almost_equal(b['4', '1'], .25 * 4 * 3)
    # -overlapping element
    assert t1['1', '2'] == 3
    assert t2['1', '2'] == 3 * 3
    assert_almost_equal(b['1', '2'],
                        0.75 * 3 + 0.25 * 3 * 3)  # just to be explicit...