Example #1
0
def test_load_and_save_W():

    print "\n-- 'load_W', 'save_W'"
    print "Example where file does not exist:"
    filename1 = 'notExistingFile.csv'
    with pytest.raises(IOError):
        W, n = load_W(join(data_directory, filename1))     # files still without zero indexing

    print "\nTorus, zeroindexing=False"
    filename2 = 'Torus_W.csv'
    print "File directory:\n  ", join(data_directory, filename2)
    W, n = load_W(join(data_directory, filename2), zeroindexing=False)
    print "W:\n", W
    print "Dense:\n", W.todense()
    print "Shape: ", W.shape

    print "\nTorus, n=9"
    W, n = load_W(join(data_directory, filename2), zeroindexing=False, n=9)
    print "W:\n", W
    print "Dense:\n", W.todense()
    print "Shape: ", W.shape

    print "\nSave same data with n=9 and load without specifying n=9 during load"
    filename3 = 'Torus_W3.gz'
    save_W(join(data_directory, filename3), W, saveWeights=True)      # gzip is possible
    W, n = load_W(join(data_directory, filename3))                    # newly written files use zeroindexing=True
    print "Dense:\n", W.todense()
    print "Shape: ", W.shape

    print "\nLoad data with float"
    W, n = load_W(join(data_directory, 'Torus_W3.gz'), usefloat=True)     # use float for weights other than integers
    print "W:\n", W
    print "Shape: ", W.shape

    print "\nSave data with float works, load without specifying float"
    filename4 = 'Torus_W4.csv'
    save_W(join(data_directory, filename4), W, saveWeights=True)
    W, n = load_W(join(data_directory, filename4), usefloat=True)
    print "W:\n", W

    print "\n2 columns, directed, space as delimiter"
    filename5 = 'test_load_and_save_W.txt'
    W, n = load_W(join(data_directory, filename5), delimiter=None, zeroindexing=False)
    print "W\n", W

    print "\n2 columns, directed, space as delimiter, doubleUndirected"
    W, n = load_W(join(data_directory, filename5), delimiter=None, zeroindexing=False, doubleUndirected=True)
    print "W\n", W

    print "\n3 columns, directed, space as delimiter, doubleUndirected"
    W, n = load_W(join(data_directory, 'Torus_W6.csv'), usefloat=True, doubleUndirected=True)
    print "W\n", W

    print "\nSave the same without weights, and load again"
    filename7 = 'Torus_W7.csv'
    save_W(join(data_directory, filename7), W, delimiter=' ')
    W, n = load_W(join(data_directory, filename7), delimiter=None)
    print "W\n", W
Example #2
0
def test_eps_convergence_linbp_Torus():
    print "\n-- 'eps_convergence_linbp' for Torus ---"
    W, n = load_W(join(data_directory, 'Torus_W.csv'), zeroindexing=False)
    print 'W dense:\n', W.todense()
    print 'W:\n', W
    Hc, k, _ = load_H(join(data_directory, 'Torus_H.csv'), zeroindexing=False)
    print "H\n", Hc
    print

    # Simple spectral = 0.658
    start = time.time()
    eps = eps_convergence_linbp(Hc, W)
    end = time.time()-start
    print "Eps:", eps
    print "Time needed:", end

    # Echo spectral = 0.488
    start = time.time()
    eps = eps_convergence_linbp(Hc, W, echo=True)
    end = time.time()-start
    print "Eps:", eps
    print "Time needed:", end
Example #3
0
def test_eps_convergence_linbp_Torus():
    print "\n-- 'eps_convergence_linbp' for Torus ---"
    W, n = load_W(join(data_directory, 'Torus_W.csv'), zeroindexing=False)
    print 'W dense:\n', W.todense()
    print 'W:\n', W
    Hc, k, _ = load_H(join(data_directory, 'Torus_H.csv'), zeroindexing=False)
    print "H\n", Hc
    print

    # Simple spectral = 0.658
    start = time.time()
    eps = eps_convergence_linbp(Hc, W)
    end = time.time() - start
    print "Eps:", eps
    print "Time needed:", end

    # Echo spectral = 0.488
    start = time.time()
    eps = eps_convergence_linbp(Hc, W, echo=True)
    end = time.time() - start
    print "Eps:", eps
    print "Time needed:", end
Example #4
0
def test_linBP_undirected_Torus():
    print "\n-- 'linBP_undirected', 'eps_convergence_linbp' with Torus --"

    # -- Load W, create X and P
    W, n = load_W(join(data_directory, 'Torus_W.csv'), zeroindexing=False, delimiter=',')
    X = np.array(
    [[1, 0, 0],
     [0, 1, 0],
     [0, 0, 1],
     [0, 0, 0],
     [0, 0, 0],
     [0, 0, 0],
     [0, 0, 0],
     [0, 0, 0]])
    H = np.array(
    [[0.1, 0.8, 0.1],
     [0.8, 0.1, 0.1],
     [0.1, 0.1, 0.8]])
    print "W (dense):\n", W.todense()
    print "X:\n", X
    print "H:\n", H
    Hc = to_centering_beliefs(H)
    print "Hc:\n", Hc
    print

    # -- Other parameters
    eps = 0.4
    numMaxIt = 20

    eps_max = eps_convergence_linbp(Hc, W)
    print "eps_max: ", eps_max
    print "eps: ", eps
    Hc2 = Hc*eps
    print "P*eps:\n", Hc2
    print

    # --- linBP
    listF = linBP_undirected(X, W, Hc2, numIt=numMaxIt, debug=2)

    # --- Display BP results
    print "linBP results:"
    print "last two F:"
    print listF[-2]
    print listF[-1]
    print "\nValues for node 6 (zero indexing):"
    print listF[:, 6, :]

    # --- Visualize BP results
    filename = join(fig_directory, 'temp.pdf')
    print "\nVisualize values for node 3 (zero indexing):"
    node = 3
    plt.plot(listF[:, node, :], lw=2)
    plt.xlabel('# iterations')
    plt.ylabel('belief')
    plt.xlim(0, numMaxIt)

    print filename

    plt.savefig(filename, dpi=None, facecolor='w', edgecolor='w',
                orientation='portrait', papertype='letter', format='pdf',
                transparent=True, bbox_inches='tight', pad_inches=0.1,
                frameon=None)
    os.system("chmod 744 " + filename)  # first change permissions in order to open PDF
    os.system("open " + filename)       # open PDF
Example #5
0
                                               exponent=exponent,
                                               directed=True,
                                               debug=False)
            X0 = from_dictionary_beliefs(Xd)
            time_graph = time.time() - start

            save_W(join(data_directory, '{}_{}_W.csv'.format(filename, n)),
                   W,
                   saveWeights=False)
            save_X(join(data_directory, '{}_{}_X.csv'.format(filename, n)), X0)
            save_tuple(n, 'graph', time_graph)

        else:
            W, _ = load_W(join(data_directory,
                               '{}_{}_W.csv'.format(filename, n)),
                          skiprows=1,
                          zeroindexing=True,
                          n=None,
                          doubleUndirected=False)
            X0, _, _ = load_X(join(data_directory,
                                   '{}_{}_X.csv'.format(filename, n)),
                              n=None,
                              k=None,
                              skiprows=1,
                              zeroindexing=True)

        # -- Repeat loop
        for i in range(repeat):
            print("\n  repeat: {}".format(i))

            X2, ind = replace_fraction_of_rows(X0,
                                               1 - f,
Example #6
0
def test_load_and_save_W():

    print "\n-- 'load_W', 'save_W'"
    print "Example where file does not exist:"
    filename1 = 'notExistingFile.csv'
    with pytest.raises(IOError):
        W, n = load_W(join(data_directory,
                           filename1))  # files still without zero indexing

    print "\nTorus, zeroindexing=False"
    filename2 = 'Torus_W.csv'
    print "File directory:\n  ", join(data_directory, filename2)
    W, n = load_W(join(data_directory, filename2), zeroindexing=False)
    print "W:\n", W
    print "Dense:\n", W.todense()
    print "Shape: ", W.shape

    print "\nTorus, n=9"
    W, n = load_W(join(data_directory, filename2), zeroindexing=False, n=9)
    print "W:\n", W
    print "Dense:\n", W.todense()
    print "Shape: ", W.shape

    print "\nSave same data with n=9 and load without specifying n=9 during load"
    filename3 = 'Torus_W3.gz'
    save_W(join(data_directory, filename3), W,
           saveWeights=True)  # gzip is possible
    W, n = load_W(join(data_directory,
                       filename3))  # newly written files use zeroindexing=True
    print "Dense:\n", W.todense()
    print "Shape: ", W.shape

    print "\nLoad data with float"
    W, n = load_W(join(data_directory, 'Torus_W3.gz'),
                  usefloat=True)  # use float for weights other than integers
    print "W:\n", W
    print "Shape: ", W.shape

    print "\nSave data with float works, load without specifying float"
    filename4 = 'Torus_W4.csv'
    save_W(join(data_directory, filename4), W, saveWeights=True)
    W, n = load_W(join(data_directory, filename4), usefloat=True)
    print "W:\n", W

    print "\n2 columns, directed, space as delimiter"
    filename5 = 'test_load_and_save_W.txt'
    W, n = load_W(join(data_directory, filename5),
                  delimiter=None,
                  zeroindexing=False)
    print "W\n", W

    print "\n2 columns, directed, space as delimiter, doubleUndirected"
    W, n = load_W(join(data_directory, filename5),
                  delimiter=None,
                  zeroindexing=False,
                  doubleUndirected=True)
    print "W\n", W

    print "\n3 columns, directed, space as delimiter, doubleUndirected"
    W, n = load_W(join(data_directory, 'Torus_W6.csv'),
                  usefloat=True,
                  doubleUndirected=True)
    print "W\n", W

    print "\nSave the same without weights, and load again"
    filename7 = 'Torus_W7.csv'
    save_W(join(data_directory, filename7), W, delimiter=' ')
    W, n = load_W(join(data_directory, filename7), delimiter=None)
    print "W\n", W