Example #1
0
    def test_sparse_dcm_rd(self):
        A = np.array([[0, 1, 1, 0, 0], [1, 0, 1, 0, 0], [0, 0, 0, 1, 0],
                      [1, 1, 0, 0, 1], [0, 0, 1, 0, 0]])

        sA = scipy.sparse.csr_matrix(A)

        sol, step, diff = iterative_solver(sA,
                                           max_steps=300,
                                           eps=0.01,
                                           method='dcm_rd')
        # output convergence
        # print('steps = {}'.format(step))
        # print('diff = {}'.format(diff))
        # epectation degree vs actual degree
        d = sample.scalability_classes(A, method='dcm_rd')
        expected_out_it = sample.expected_out_degree(sol, 'dcm_rd', d)
        expected_in_it = sample.expected_in_degree(sol, 'dcm_rd', d)
        expected_k = np.concatenate((expected_out_it, expected_in_it))
        k_out = sample.out_degree(sA)
        k_in = sample.in_degree(sA)
        k = np.concatenate((k_out, k_in))
        # debug check
        # print(d)
        # print(k)
        # print(expected_k)
        # print(np.linalg.norm(k- expected_k))

        self.assertTrue(np.allclose(expected_k, k, atol=1e-02, rtol=1e-02))
Example #2
0
    def test_dcm_rd(self):
        A = np.array([[0, 1, 1, 0, 0], [1, 0, 1, 0, 0], [0, 0, 0, 1, 0],
                      [1, 1, 0, 0, 1], [0, 0, 1, 0, 0]])

        right_d = {(2, 2): [0, 1], (1, 3): [2], (3, 1): [3], (1, 1): [4]}

        k_out = sample.out_degree(A)
        k_in = sample.in_degree(A)
        k = np.concatenate((k_out, k_in))

        d = sample.scalability_classes(A, 'dcm_rd')

        self.assertTrue(
            np.alltrue(
                sample.rd2full_dcm_rd(np.array([2, 1, 3, 1, 2, 3, 1, 1]), d) ==
                k))
def tester(A, verbose=False):
    t = time.time()
    tic = time.time()
    k_out = sample.out_degree(A)
    k_in = sample.in_degree(A)
    s_out = sample.out_strength(A)
    s_in = sample.in_strength(A)
    k = np.concatenate((k_out, k_in, s_out, s_in))
    toc = time.time() - tic
    if len(k) < 30:
        print("\n\nExperiment matrix: \n {}".format(A))
        print("\n degree sequence:\n{}".format(k))
    print('\ndegree sequence computation time = {}'.format(toc))
    # dianati iterative function for dcm
    tic = time.time()
    n = len(k_in)
    par, x0 = sample.setup(A, method='decm')
    print('initial guess = {}'.format(x0))
    sol, step, diff = sample.iterative_solver(A,
                                              max_steps=300,
                                              eps=1e-4,
                                              method='decm',
                                              verbose=verbose)
    toc = time.time() - tic
    print('\nsolver exectution time = {}'.format(toc))
    # test results: degree reconstruction
    tic = time.time()
    k_in_sol = sample.expected_in_degree_decm(sol)
    k_out_sol = sample.expected_out_degree_decm(sol)
    s_in_sol = sample.expected_in_strength_decm(sol)
    s_out_sol = sample.expected_out_strength_decm(sol)
    k_sol = np.concatenate((k_out_sol, k_in_sol, s_out_sol, s_in_sol))
    toc = time.time() - tic
    print('\n number of steps = {}'.format(step))
    print('\nsolution = {}'.format(sol))
    print('\ndegree reconstruction time = {}'.format(toc))

    print('\n{}reconstruction error = {}{}'.format(bcolors.WARNING,
                                                   np.linalg.norm(k - k_sol),
                                                   bcolors.ENDC))
    if len(k) < 30:
        print("\n Original degree sequence:\n{}".format(k))
        print("\n Reconstructed degree sequence\n{}".format(k_sol))
    toc = time.time() - t
    print('\ntotal test time = {}'.format(toc))
Example #4
0
    def test_array_dcm(self):
        A = np.array([[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 1, 0]])

        sol, step, diff = iterative_solver(A, max_steps=300, eps=0.01)
        # output convergence
        # print('steps = {}'.format(step))
        # print('diff = {}'.format(diff))
        # epectation degree vs actual degree
        expected_out_it = sample.expected_out_degree(sol, 'dcm')
        expected_in_it = sample.expected_in_degree(sol, 'dcm')
        expected_k = np.concatenate((expected_out_it, expected_in_it))
        k_out = sample.out_degree(A)
        k_in = sample.in_degree(A)
        k = np.concatenate((k_out, k_in))
        # debug check
        # print(k)
        # print(expected_k)
        # print(np.linalg.norm(k- expected_k))

        self.assertTrue(np.allclose(expected_k, k, atol=1e-02, rtol=1e-02))
def tester(A, verbose=False):
    t = time.time()
    tic = time.time()
    k_out = sample.out_degree(A)
    k_in = sample.in_degree(A)
    k = np.concatenate((k_out, k_in))
    toc = time.time() - tic
    if len(k) < 12:
        print("\n\nExperiment matrix: \n {}".format(A))
        print("\n degree sequence:\n{}".format(k))
    print('\ndegree sequence computation time = {}'.format(toc))
    # dianati iterative function for dcm
    tic = time.time()
    n = len(k_in)
    x0 = np.array([ki / np.sqrt(n) for ki in k])  # initialial point
    sol, step, diff = sample.iterative_solver(A,
                                              max_steps=300,
                                              eps=0.01,
                                              method='dcm')
    toc = time.time() - tic
    print('\nsolver exectution time = {}'.format(toc))
    # test results: degree reconstruction
    tic = time.time()
    k_in_sol = sample.expected_in_degree_dcm(sol)
    k_out_sol = sample.expected_out_degree_dcm(sol)
    k_sol = np.concatenate((k_out_sol, k_in_sol))
    toc = time.time() - tic
    print('\n number of steps = {}'.format(step))
    print('\nsolution = {}'.format(sol))
    print('\ndegree reconstruction time = {}'.format(toc))
    print('\n{}reconstruction error = {}{}'.format(bcolors.WARNING,
                                                   np.linalg.norm(k - k_sol),
                                                   bcolors.ENDC))
    if len(k) < 12:
        print("\n Original degree sequence:\n{}".format(k))
        print("\n Reconstructed degree sequence\n{}".format(k_sol))
    toc = time.time() - t
    print('\ntotal test time = {}'.format(toc))
    def test_array_inin(self):
        A = np.array([[0, 0, 1, 0, 1],
                [1, 0, 0, 1, 0],
                [1, 1, 0, 0, 0],
                [0, 1, 1, 0, 1],
                [1, 0, 0, 1, 0]])
        
        A_nx = nx.convert_matrix.from_numpy_array(A, create_using=nx.DiGraph())
        k = sample.in_degree(A)

        knn_sample = sample.nearest_neighbour_degree_inin(A)
        # knn_nx = nx.degree_assortativity_coefficient(A_nx)
        knn_nx = nx.k_nearest_neighbors(A_nx, source='in', target='in')

        # debug check
        """
        print(k)
        print('\n')
        print(knn_sample)
        print(knn_nx)
        """

        self.assertTrue(knn_sample == knn_nx)
Example #7
0
G = nx.read_graphml(db_path + week)
# A = nx.to_numpy_array(G, dtype=int)
A = nx.to_scipy_sparse_matrix(G) 

# iterative solver
tic = time.time()
sol, step, diff = sample.iterative_solver(A, eps=1e-04, method='dcm_rd')
t = time.time() - tic

# test solution
d = sample.scalability_classes(A, 'dcm_rd')
expected_out_it = sample.expected_out_degree(sol, 'dcm_rd', d)
expected_in_it = sample.expected_in_degree(sol, 'dcm_rd', d)
expected_k = np.concatenate((expected_out_it, expected_in_it))
k_out = sample.out_degree(A)
k_in = sample.in_degree(A)
k = np.concatenate((k_out, k_in))
error = max(abs(k - expected_k))
result = np.allclose(expected_k, k, atol=1e-02, rtol=1e-02)

# output
print('experiment success: {}'.format(result))
if not result:
    print('error = {}'.format(error))
print('diff = {}'.format(diff) )
print('steps = {}'.format(step) )
print('time = {}'.format(t) )

# save results
csvfile = 'df_btc_dcmrd.csv'
# if the csv file already exists, do not write the header