示例#1
0
def test_compute_I_div():
    R = numpy.array([[1,2],[3,4]],dtype=float)
    M = numpy.array([[1,1],[0,1]])
    (I,J,K,L) = (2,2,3,1)
    
    F = numpy.array([[1,2,3],[4,5,6]])
    S = numpy.array([[7],[8],[9]])
    G = numpy.array([[10],[11]])
    #R_predicted = numpy.array([[500,550],[1220,1342]],dtype=float)    
    
    nmtf = NMTF(R,M,K,L)
    nmtf.F = F
    nmtf.S = S
    nmtf.G = G
    
    expected_I_div = sum([
        1.0*math.log(1.0/500.0) - 1.0 + 500.0,
        2.0*math.log(2.0/550.0) - 2.0 + 550.0,
        4.0*math.log(4.0/1342.0) - 4.0 + 1342.0
    ])
    
    nmtf = NMTF(R,M,K,L)
    nmtf.F = F
    nmtf.S = S
    nmtf.G = G
    
    I_div = nmtf.compute_I_div()    
    assert I_div == expected_I_div
示例#2
0
def test_compute_I_div():
    R = numpy.array([[1, 2], [3, 4]], dtype=float)
    M = numpy.array([[1, 1], [0, 1]])
    (I, J, K, L) = (2, 2, 3, 1)

    F = numpy.array([[1, 2, 3], [4, 5, 6]])
    S = numpy.array([[7], [8], [9]])
    G = numpy.array([[10], [11]])
    #R_predicted = numpy.array([[500,550],[1220,1342]],dtype=float)

    nmtf = NMTF(R, M, K, L)
    nmtf.F = F
    nmtf.S = S
    nmtf.G = G

    expected_I_div = sum([
        1.0 * math.log(1.0 / 500.0) - 1.0 + 500.0,
        2.0 * math.log(2.0 / 550.0) - 2.0 + 550.0,
        4.0 * math.log(4.0 / 1342.0) - 4.0 + 1342.0
    ])

    nmtf = NMTF(R, M, K, L)
    nmtf.F = F
    nmtf.S = S
    nmtf.G = G

    I_div = nmtf.compute_I_div()
    assert I_div == expected_I_div
示例#3
0
def test_run():
    ###### Test updating F, G, S in that order
    # Test whether a single iteration gives the correct behaviour, updating F, G, S in that order
    R = numpy.array([[1,2],[3,4]],dtype='f')
    M = numpy.array([[1,1],[0,1]])
    K = 3
    L = 1
    
    F = numpy.array([[1,2,3],[4,5,6]],dtype='f')
    S = numpy.array([[7],[8],[9]],dtype='f')
    G = numpy.array([[10],[11]],dtype='f')
    #FSG = numpy.array([[500,550],[1220,1342]],dtype='f')
    #FS = numpy.array([[50],[122]],dtype='f')
    #SG = numpy.array([[70,77],[80,88],[90,99]],dtype='f')
    
    nmtf = NMTF(R,M,K,L)
    
    # Check we get an Exception if W, H are undefined
    with pytest.raises(AssertionError) as error:
        nmtf.run(0)
    assert str(error.value) == "F, S and G have not been initialised - please run NMTF.initialise() first."       
    
    nmtf.F = numpy.copy(F)
    nmtf.S = numpy.copy(S)
    nmtf.G = numpy.copy(G) 
    
    nmtf.run(1)
示例#4
0
def test_predict():
    R = numpy.array([[1, 2], [3, 4]], dtype=float)
    M = numpy.array([[1, 1], [0, 1]])
    (I, J, K, L) = (2, 2, 3, 1)

    F = numpy.array([[1, 2, 3], [4, 5, 6]])
    S = numpy.array([[7], [8], [9]])
    G = numpy.array([[10], [11]])
    #R_predicted = numpy.array([[500,550],[1220,1342]],dtype=float)

    nmtf = NMTF(R, M, K, L)
    nmtf.F = F
    nmtf.S = S
    nmtf.G = G
    performances = nmtf.predict(M)

    MSE = (499 * 499 + 548 * 548 + 1338 * 1338) / 3.0
    R2 = 1 - (499**2 + 548**2 + 1338**2) / (
        (4.0 / 3.0)**2 + (1.0 / 3.0)**2 + (5.0 / 3.0)**2)  #mean=7.0/3.0
    #mean_real=7.0/3.0,mean_pred=2392.0/3.0 -> diff_real=[-4.0/3.0,-1.0/3.0,5.0/3.0],diff_pred=[-892.0/3.0,-742.0/3.0,1634.0/3.0]
    Rp = ((-4.0 / 3.0 * -892.0 / 3.0) + (-1.0 / 3.0 * -742.0 / 3.0) +
          (5.0 / 3.0 * 1634.0 / 3.0)) / (math.sqrt((-4.0 / 3.0)**2 +
                                                   (-1.0 / 3.0)**2 +
                                                   (5.0 / 3.0)**2) *
                                         math.sqrt((-892.0 / 3.0)**2 +
                                                   (-742.0 / 3.0)**2 +
                                                   (1634.0 / 3.0)**2))

    assert performances['MSE'] == MSE
    assert abs(performances['R^2'] - R2) < 0.000000001
    assert abs(performances['Rp'] - Rp) < 0.000000001
示例#5
0
def test_run():
    ###### Test updating F, G, S in that order
    # Test whether a single iteration gives the correct behaviour, updating F, G, S in that order
    R = numpy.array([[1, 2], [3, 4]], dtype='f')
    M = numpy.array([[1, 1], [0, 1]])
    K = 3
    L = 1

    F = numpy.array([[1, 2, 3], [4, 5, 6]], dtype='f')
    S = numpy.array([[7], [8], [9]], dtype='f')
    G = numpy.array([[10], [11]], dtype='f')
    #FSG = numpy.array([[500,550],[1220,1342]],dtype='f')
    #FS = numpy.array([[50],[122]],dtype='f')
    #SG = numpy.array([[70,77],[80,88],[90,99]],dtype='f')

    nmtf = NMTF(R, M, K, L)

    # Check we get an Exception if W, H are undefined
    with pytest.raises(AssertionError) as error:
        nmtf.run(0)
    assert str(
        error.value
    ) == "F, S and G have not been initialised - please run NMTF.initialise() first."

    nmtf.F = numpy.copy(F)
    nmtf.S = numpy.copy(S)
    nmtf.G = numpy.copy(G)

    nmtf.run(1)
示例#6
0
def test_predict():
    R = numpy.array([[1,2],[3,4]],dtype=float)
    M = numpy.array([[1,1],[0,1]])
    (I,J,K,L) = (2,2,3,1)
    
    F = numpy.array([[1,2,3],[4,5,6]])
    S = numpy.array([[7],[8],[9]])
    G = numpy.array([[10],[11]])
    #R_predicted = numpy.array([[500,550],[1220,1342]],dtype=float)    
    
    nmtf = NMTF(R,M,K,L)
    nmtf.F = F
    nmtf.S = S
    nmtf.G = G
    performances = nmtf.predict(M)
    
    MSE = ( 499*499 + 548*548 + 1338*1338 ) / 3.0
    R2 = 1 - (499**2 + 548**2 + 1338**2)/((4.0/3.0)**2 + (1.0/3.0)**2 + (5.0/3.0)**2) #mean=7.0/3.0
    #mean_real=7.0/3.0,mean_pred=2392.0/3.0 -> diff_real=[-4.0/3.0,-1.0/3.0,5.0/3.0],diff_pred=[-892.0/3.0,-742.0/3.0,1634.0/3.0]
    Rp = ((-4.0/3.0*-892.0/3.0)+(-1.0/3.0*-742.0/3.0)+(5.0/3.0*1634.0/3.0)) / (math.sqrt((-4.0/3.0)**2+(-1.0/3.0)**2+(5.0/3.0)**2) * math.sqrt((-892.0/3.0)**2+(-742.0/3.0)**2+(1634.0/3.0)**2))
      
    assert performances['MSE'] == MSE
    assert abs(performances['R^2'] - R2) < 0.000000001
    assert abs(performances['Rp'] - Rp) < 0.000000001