Example #1
0
def test_closure_small():
    """ closure on small graph. """
    A = np.asarray([[0.0, 0.1, 0.0, 0.2], [0.0, 0.0, 0.3, 0.0],
                    [0.0, 0.0, 0.0, 0.0], [0.0, 0.3, 0.0, 0.0]])
    # ultrametric
    source = 0
    proxs, paths = clo.closuress(A, source)
    proxs2, paths2 = clo.cclosuress(A, source, retpaths=1)
    assert np.allclose(proxs, proxs2)
    for p1, p2 in zip(paths, paths2):
        assert np.all(p1 == p2)
    # metric
    proxs, paths = clo.closuress(A, source, kind='metric')
    proxs2, paths2 = clo.cclosuress(A, source, retpaths=1, kind='metric')
    assert np.allclose(proxs, proxs2)
    for p1, p2 in zip(paths, paths2):
        assert np.all(p1 == p2)
Example #2
0
def test_closure_big():
    """ closure on large graph + speed test. """
    np.random.seed(100)
    N = 500
    thresh = 0.1
    A = sp.rand(N, N, thresh, 'csr')
    A = np.asarray(A.todense())
    source = 0
    tic = time()
    proxs, _ = clo.closuress(A, source)
    toc = time()
    py_time = toc - tic
    tic = time()
    proxs2, _ = clo.cclosuress(A, source)
    toc = time()
    cy_time = toc - tic
    assert np.allclose(proxs, proxs2)
    assert py_time > cy_time, \
        'python: {:.2g} s, cython: {:.2g} s.'.format(py_time, cy_time)
Example #3
0
def test_closureap():
    """ Correctedness of all-pairs parallel closure. """
    np.random.seed(100)
    dt = DirTree('test', (2, 5, 10), root='test_parallel')
    N = 100
    thresh = 0.1
    A = sp.rand(N, N, thresh, 'csr')
    nnz = A.getnnz()
    sparsity = float(nnz) / N**2
    print 'Number of nnz = {}, sparsity = {:g}'.format(nnz, sparsity)
    A = np.asarray(A.todense())
    clo.closureap(A, dt)
    coords = np.asarray(fromdirtree(dt, N), dtype=coo_dtype)
    coo = (coords['weight'], (coords['row'], coords['col']))
    B = np.asarray(sp.coo_matrix(coo, shape=(N, N)).todense())
    rows = []
    for row in xrange(N):
        r, _ = clo.cclosuress(A, row)
        rows.append(r)
    C = np.asarray(rows)
    assert np.allclose(B, C)
    # cleanup
    for logpath in glob('closure-*.log'):
        os.remove(logpath)