Ejemplo n.º 1
0
def test_CbGiGiGaD_traversal():
    """
    Test path counts and degree-weighted path counts for the CbGiGiGaD
    metapath between bupropion and nicotine dependence. These values are not
    intended to correspond to the values from the entire Hetionet v1.0. Hence,
    the expected values are generated using hetio.pathtools.
    """
    graph = get_graph('bupropion-subgraph')
    compound = 'DB01156'  # Bupropion
    disease = 'DOID:0050742'  # nicotine dependence
    metapath = graph.metagraph.metapath_from_abbrev('CbGiGiGaD')
    paths = hetio.pathtools.paths_between(
        graph,
        source=('Compound', compound),
        target=('Disease', disease),
        metapath=metapath,
        duplicates=False,
    )
    hetio_dwpc = hetio.pathtools.DWPC(paths, damping_exponent=0.4)

    rows, cols, pc_matrix = dwpc(graph, metapath, damping=0)
    rows, cols, dwpc_matrix = dwpc(graph, metapath, damping=0.4)
    i = rows.index(compound)
    j = cols.index(disease)

    assert pc_matrix[i, j] == len(paths)
    assert dwpc_matrix[i, j] == pytest.approx(hetio_dwpc)
Ejemplo n.º 2
0
def test_path_traversal(metapath, hetmat, tmpdir):
    """
    Test PC (path count) and DWPC (degree-weighted path count) computation
    on the random subgraph of Hetionet v1.0. Evaluates max path count
    compound-disease pair where errors are most likely to appear.
    """
    # Read graph
    graph = get_graph('random-subgraph')
    graph_or_hetmat = graph
    if hetmat:
        graph_or_hetmat = get_graph('random-subgraph', hetmat=hetmat, directory=tmpdir)
    metapath = graph.metagraph.metapath_from_abbrev(metapath)

    # Matrix computations
    rows, cols, pc_matrix = dwpc(graph_or_hetmat, metapath, damping=0)
    rows, cols, dwpc_matrix = dwpc(graph_or_hetmat, metapath, damping=0.4)

    # Find compound-disease pair with the max path count
    i, j = numpy.unravel_index(pc_matrix.argmax(), pc_matrix.shape)
    compound = rows[i]
    disease = cols[j]

    # hetnetpy.pathtools computations
    paths = hetnetpy.pathtools.paths_between(
        graph,
        source=('Compound', compound),
        target=('Disease', disease),
        metapath=metapath,
        duplicates=False,
    )
    hetnetpy_dwpc = hetnetpy.pathtools.DWPC(paths, damping_exponent=0.4)

    # Check matrix values match hetnetpy.pathtools
    assert pc_matrix[i, j] == len(paths)
    assert dwpc_matrix[i, j] == pytest.approx(hetnetpy_dwpc)
Ejemplo n.º 3
0
def test_CbGpPWpGaD_traversal():
    """
    Test path counts and degree-weighted path counts for the CbGpPWpGaD
    metapath between bupropion and nicotine dependence. Expected values from
    the network traversal methods at https://git.io/vHBh2.
    """
    graph = get_graph('bupropion-subgraph')
    compound = 'DB01156'  # Bupropion
    disease = 'DOID:0050742'  # nicotine dependence
    metapath = graph.metagraph.metapath_from_abbrev('CbGpPWpGaD')
    rows, cols, pc_matrix = dwpc(graph, metapath, damping=0)
    rows, cols, dwpc_matrix = dwpc(graph, metapath, damping=0.4)
    i = rows.index(compound)
    j = cols.index(disease)
    assert pc_matrix[i, j] == 142
    assert dwpc_matrix[i, j] == pytest.approx(0.03287590886921623)
Ejemplo n.º 4
0
def test_dtype(metapath, dtype, dwwc_method):
    graph = get_graph('disease-gene-example')
    metapath = graph.metagraph.metapath_from_abbrev(metapath)
    rows, cols, dwpc_matrix = dwpc(graph,
                                   metapath,
                                   dtype=dtype,
                                   dwwc_method=dwwc_method)
    assert dwpc_matrix.dtype == dtype
Ejemplo n.º 5
0
def test_disjoint_dwpc(metapath, exp_row, exp_col, exp_data, shape):
    graph = get_graph('random-subgraph')
    metapath = graph.metagraph.metapath_from_abbrev(metapath)

    row, col, dwpc_matrix = dwpc(graph, metapath)

    # expected = numpy.array(expected, dtype=numpy.float64)
    expected = sparse.coo_matrix((exp_data, (exp_row, exp_col)), shape=shape)
    assert abs(dwpc_matrix - expected).max() == pytest.approx(0, abs=1e-7)
Ejemplo n.º 6
0
def test_dwpc(metapath, expected, dense_threshold):
    if expected is not None:
        expected = numpy.array(expected, dtype=numpy.float64)

    graph = get_graph('disease-gene-example')
    metapath = graph.metagraph.metapath_from_abbrev(metapath)
    if expected is None:
        with pytest.raises(Exception):
            dwpc(graph, metapath, damping=0.5, dense_threshold=dense_threshold)
    else:
        row, col, dwpc_matrix = dwpc(graph,
                                     metapath,
                                     damping=0.5,
                                     dense_threshold=dense_threshold)
        assert abs(expected - dwpc_matrix).max() == pytest.approx(0, abs=1e-7)
        if dense_threshold == 1:
            assert sparse.issparse(dwpc_matrix)
        else:
            assert not sparse.issparse(dwpc_matrix)
Ejemplo n.º 7
0
def test_dwpc_approx(metapath, relative):
    graph = get_graph('random-subgraph')
    metapath = graph.metagraph.metapath_from_abbrev(metapath)
    rows, cols, dwpc_matrix = dwpc(graph, metapath)
    rows, cols, dwpc_approx = _dwpc_approx(graph, metapath)
    rows, cols, dwwc_matrix = dwwc(graph, metapath)
    if relative == 'equal':
        assert abs(
            (dwpc_approx - dwpc_matrix)).max() == pytest.approx(0, abs=1e-7)
    else:
        assert numpy.sum((dwpc_approx - dwpc_matrix)) >= 0
    assert abs((dwwc_matrix - dwpc_approx)).max() >= 0