Exemplo n.º 1
0
def test_set_edge_label_options(test_data):

    G = base.GenotypePhenotypeGraph()

    # Get available options
    avail_options = copy.deepcopy(G.edge_label_options)

    # Set individually to 1.
    for a in avail_options:
        G.set_edge_label_options(**{a:1})
        assert G.edge_label_options[a] == 1

    # Set all at once and make sure they match
    G.set_edge_label_options(**avail_options)
    for a in avail_options:
        assert G.edge_label_options[a] == avail_options[a]

    # pass bad options
    bad_options = ["not_a_key"]
    for b in bad_options:
        with pytest.raises(KeyError):
            G.set_edge_label_options(**{b:1})

    # test exotic edge_label setting
    d = test_data[0]
    gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
Exemplo n.º 2
0
def test_gpm_getter():

    G = base.GenotypePhenotypeGraph()
    assert G.gpm is None

    gpm = gpmap.GenotypePhenotypeMap(["AA"])
    G.add_gpm(gpm)
    assert G.gpm is gpm
Exemplo n.º 3
0
def test_constructor(test_data):

    G = base.GenotypePhenotypeGraph()
    assert isinstance(G,nx.DiGraph)
    assert G.gpm is None

    attributes = [G.node_options,G.edge_options,
                  G.node_label_options,G.edge_label_options]
    for a in attributes:
        assert type(a) is dict
        assert len(a) > 0

    with pytest.raises(KeyError):
        G.node_options["nodelist"]
    with pytest.raises(KeyError):
        G.node_options["edgelist"]

    assert G.node_options["node_size"] == G._default_node_size
    assert G.edge_options["node_size"] == G._default_node_size
    assert G.edge_options["arrows"] == False

    # Test bad gpm inputs
    bad_inputs = ["stupid",1,[],(1,),1.1]
    for b in bad_inputs:
        with pytest.raises(TypeError):
            G = base.GenotypePhenotypeGraph(gpm=b)

    # Send in good genotype phenotype map. does not have weights.
    d = test_data[0]
    gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
    G = base.GenotypePhenotypeGraph(gpm=gpm)
    G.gpm.data
    G.gpm.neighbors

    # Pass bad edge weight columns
    gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
    with pytest.raises(ValueError):
        G = base.GenotypePhenotypeGraph(gpm=gpm,edge_weight_column="not_good")

    gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
    with pytest.raises(ValueError):
        G = base.GenotypePhenotypeGraph(gpm=gpm,edge_weight_column=1.1)

    # Should work and have weighted edges
    gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
    G = base.GenotypePhenotypeGraph(gpm=gpm)
    for g in G.edges:
        G.edges[g[0],g[1]]["weight"]
Exemplo n.º 4
0
def test_set_node_label_options():

    G = base.GenotypePhenotypeGraph()

    # Get available options
    avail_options = copy.deepcopy(G.node_label_options)

    # Set individually to 1.
    for a in avail_options:
        G.set_node_label_options(**{a:1})
        assert G.node_label_options[a] == 1

    # Set all at once and make sure they match
    G.set_node_label_options(**avail_options)
    for a in avail_options:
        assert G.node_label_options[a] == avail_options[a]

    # pass bad options
    bad_options = ["not_a_key"]
    for b in bad_options:
        with pytest.raises(KeyError):
            G.set_node_label_options(**{b:1})
Exemplo n.º 5
0
def test_edge_label_options_getter():

    G = base.GenotypePhenotypeGraph()
    assert type(G.edge_label_options) is dict
Exemplo n.º 6
0
def test_node_options_getter():

    G = base.GenotypePhenotypeGraph()
    assert type(G.node_options) is dict
Exemplo n.º 7
0
def test_add_gpm(test_data):

    # Check for bad value checking
    G = base.GenotypePhenotypeGraph()
    bad_values = ["test",[],1.3,(1,2),base.GenotypePhenotypeGraph()]
    for b in bad_values:
        with pytest.raises(TypeError):
            G.add_gpm(b)

    for d in test_data:

        G = base.GenotypePhenotypeGraph()
        assert G.gpm is None

        # Build with bad edge_weight_column, no neighbors
        gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
        with pytest.raises(ValueError):
            G.add_gpm(gpm,edge_weight_column="not_yet")

        # Build with bad edge_weight_column, pre-built neighbors
        G = base.GenotypePhenotypeGraph()
        gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
        gpm.get_neighbors()
        gpm.neighbors.loc[:,"now_here"] = np.ones(len(gpm.neighbors))
        with pytest.raises(ValueError):
            G.add_gpm(gpm,edge_weight_column="not_yet")

        # Add with good edge_weight_column, pre-built neighbors
        G = base.GenotypePhenotypeGraph()
        gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
        gpm.get_neighbors()
        gpm.neighbors.loc[:,"now_here"] = np.ones(len(gpm.neighbors))
        ret = G.add_gpm(gpm,edge_weight_column="now_here")
        assert ret.edge_weight_column == "now_here"

        # No prebuilt neighbors
        G = base.GenotypePhenotypeGraph()
        gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
        ret = G.add_gpm(gpm)

        # Test return of self
        assert G is ret

        # Make sure neighbors and neighbor weight construted correctly
        assert G.edge_weight_column == "weight"
        assert G.gpm.neighbors is not None
        G.gpm.neighbors.weight

        # Make sure gpm is now attached (as pointer, not copy)
        assert G.gpm is gpm

        # Make sure data loaded into nodes as expected
        assert len(G.nodes) == len(G.gpm.data)
        keys = ["genotype","binary","n_mutations","name"]
        for i in range(len(G.nodes)):
            for k in keys:
                assert G.nodes[i][k] == G.gpm.data.iloc[i][k]

        # Make sure neighbors were generated. (Do not check that the neighbors
        # are right. For this, check the test_gpm.py tests.)
        assert G.gpm.neighbors is not None

        # Make sure edges match neighbors
        assert len(G.gpm.neighbors) == len(G.edges)
        for i in range(len(G.gpm.neighbors)):
            edge = G.gpm.neighbors.edge[i]
            G.edges[edge]

        # Build neighbors differently -- make sure this still worked without
        # generating neighbors on the fly.
        G = base.GenotypePhenotypeGraph()
        gpm = gpmap.GenotypePhenotypeMap(d["genotype"])
        gpm.get_neighbors("hamming",cutoff=3)
        pregen_edges = np.copy(gpm.neighbors.edge)
        G.add_gpm(gpm)
        assert len(pregen_edges) == len(G.gpm.neighbors.edge)
        for i in range(len(G.gpm.neighbors)):
            edge = G.gpm.neighbors.edge[i]
            G.edges[edge]