def test_array_rdcm(self): A = np.array([[0, 1, 1, 0], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 1, 0]]) sol, step, diff = iterative_solver(A, method='rdcm', max_steps=300, eps=0.01) # output convergence # print('steps = {}'.format(step)) # print('diff = {}'.format(diff)) # epectation degree vs actual degree expected_out_nr_it = sample.expected_non_reciprocated_out_degree_rdcm( sol) expected_in_nr_it = sample.expected_non_reciprocated_in_degree_rdcm( sol) expected_k_r_it = sample.expected_reciprocated_degree_rdcm(sol) expected_k = np.concatenate( (expected_out_nr_it, expected_in_nr_it, expected_k_r_it)) k_out_nr = sample.non_reciprocated_out_degree(A) k_in_nr = sample.non_reciprocated_in_degree(A) k_r = sample.reciprocated_degree(A) k = np.concatenate((k_out_nr, k_in_nr, k_r)) # 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 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))
def test_sparse_dcm(self): A = np.array([[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 1, 0]]) sA = scipy.sparse.csr_matrix(A) # debug check # sk_out = sample.out_degree(sA) # sk_in = sample.in_degree(sA) # sk = np.concatenate((sk_out, sk_in)) ## print(sk) sol, step, diff = iterative_solver(sA, max_steps = 300, eps = 0.01) # sample matrix sample.ensemble_sampler(sol=sol, m=1, method='dcm') # load matrix AA = scipy.sparse.load_npz('dcm_graph_0.npz') # debug check """ 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)) _out = sample.out_degree(AA) k_in = sample.in_degree(AA) k = np.concatenate((k_out, k_in)) print(k) print(expected_k) print(np.linalg.norm(k- expected_k)) """ # remove sampled graph os.remove('dcm_graph_0.npz') self.assertTrue(sA.size == AA.size)
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))
def test_array_cm(self): A = np.array([[0, 1, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [0, 1, 0, 0]]) sol, step, diff = iterative_solver(A, max_steps=300, eps=0.01, method='cm') # output convergence # print('steps = {}'.format(step)) # print('diff = {}'.format(diff)) # epectation degree vs actual degree expected_k = sample.expected_degree(sol, 'cm') k = sample.out_degree(A) # 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))
# week to test # week = '2011-01-17.graphml' # week = '2012-01-16.graphml' # week = '2013-01-14.graphml' # week = '2014-01-13.graphml' week = sys.argv[1] print(db_path + week) # load the graph 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))
def test_dcm_rd_dyads_emseble_empirical(self): A = np.array([[0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) # solve the netrec problem sol, step, diff = sample.iterative_solver(A, max_steps = 300, eps = 0.01, method='dcm_rd') d = sample.scalability_classes(A, 'dcm_rd') sol_full = sample.rd2full(sol, d, 'dcm_rd') sol = sol_full # find the analytical expectation # dyads dyads_fun_expectation = sample.expected_dyads(sol, method='dcm') # singles singles_fun_expectation = sample.expected_dyads(sol, method='dcm', t='singles') # zeros zeros_fun_expectation = sample.expected_dyads(sol, method='dcm', t='zeros') # sample 100 networks (dcm method by default) s_dir = 'tmp' sample.ensemble_sampler(sol=sol, m=1000, method='dcm', sample_dir=s_dir) # count empirical dyads for each sampled network files = os.listdir(s_dir) dyads_l = [] singles_l = [] zeros_l = [] for f in files: fA = scipy.sparse.load_npz(s_dir + '/' + f) dyads = sample.dyads_count(fA) dyads_l.append(dyads) singles = sample.singles_count(fA) singles_l.append(singles) zeros = sample.zeros_count(fA) zeros_l.append(zeros) # compute empirical average dyads_empirical_expectation = np.average(dyads_l) singles_empirical_expectation = np.average(singles_l) zeros_empirical_expectation = np.average(zeros_l) # debug """ print('Empirical test DCM REDUCED') print('dyads') print('analytical: {}'.format(dyads_fun_expectation)) print('Empirical : {}'.format(dyads_empirical_expectation)) print('singles') print(singles_fun_expectation) print(singles_empirical_expectation) print('zeros') print(zeros_fun_expectation) print(zeros_empirical_expectation) """ # remove ensemble directory files = os.listdir(s_dir) for f in files: os.remove(s_dir + '/' + f) os.rmdir(s_dir) # testing self.assertTrue(np.allclose(dyads_fun_expectation, dyads_empirical_expectation, atol=1e-01, rtol=1e-01)) self.assertTrue(np.allclose(singles_fun_expectation, singles_empirical_expectation, atol=1e-01, rtol=1e-01)) self.assertTrue(np.allclose(zeros_fun_expectation, zeros_empirical_expectation, atol=1e-01, rtol=1e-01))
# btc database absolute path # db_path = '~/Datasets/NET-btc530k-heur_2s-week/' db_path = '/mnt/hdd_data/imt/NET-btc530k-heur_2s-week/' # week to test # week = '2011-01-17.graphml' # week = '2012-01-16.graphml' # week = '2013-01-14.graphml' week = '2014-01-13.graphml' # load the graph 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) t = time.time() - tic # test solution expected_out_it = utils.expected_out_degree(sol) expected_in_it = utils.expected_in_degree(sol) expected_k = np.concatenate((expected_out_it, expected_in_it)) k_out = utils.out_degree(A) k_in = utils.in_degree(A) k = np.concatenate((k_out, k_in)) # result = np.allclose(expected_k, k, atol=1e-02, rtol=1e-02) print('experiment success: {}'.format(result)) if not result: print('error = {}'.format(max(abs(k - expected_k)))) # print('diff = {}'.format(diff))