예제 #1
0
파일: test_base.py 프로젝트: harmsm/gpgraph
def test_constructor():

    # This should work fine
    G = GenotypePhenotypeGraph()
    assert type(G) is GenotypePhenotypeGraph

    assert G.gpm is None
    with pytest.raises(ValueError):
        G.model(1., 1.)
예제 #2
0
def test_pos(test_data):

    # requires G
    with pytest.raises(TypeError):
        gpgraph.pyplot.pos.flattened()

    # pass dumb argument
    with pytest.raises(ValueError):
        gpgraph.pyplot.pos.flattened("stupid")

    for d in test_data:

        gpm = GenotypePhenotypeMap(genotypes=d["genotypes"],
                                   wildtype=d["wildtype"],
                                   phenotypes=d["phenotypes"])

        G = GenotypePhenotypeGraph()

        # test check for loaded dataset.
        with pytest.raises(ValueError):
            gpgraph.pyplot.pos.flattened(G)

        # Load in data
        G.add_gpm(gpm)

        # Should work
        ret = gpgraph.pyplot.pos.flattened(G)

        # Check basic sanity of returns
        assert type(ret) is dict
        for k in ret:
            G.nodes[k]
            assert type(ret[k]) is list
            assert len(ret[k]) == 2

        # Check for bad scale values
        with pytest.raises(ValueError):
            gpgraph.pyplot.pos.flattened(G, scale=0)
        with pytest.raises(ValueError):
            gpgraph.pyplot.pos.flattened(G, scale=-1)

        # these should work
        ret = gpgraph.pyplot.pos.flattened(G, scale=10.0)
        assert type(ret) is dict

        ret = gpgraph.pyplot.pos.flattened(G, scale=22.3)
        assert type(ret) is dict

        # make sure it takes vertical parameter. not really testing if it has
        # effect (test for graphs are annoying... )
        ret = gpgraph.pyplot.pos.flattened(G, scale=1, vertical=True)
        assert type(ret) is dict
예제 #3
0
def test_read_json(gpgraph_test):
    """Test reading from json"""
    read_gpm = GenotypePhenotypeGraph.read_json("data/test_data.json")
    # Test instance was created
    assert isinstance(read_gpm, GenotypePhenotypeGraph)
    # Test elements align
    np.testing.assert_array_equal(gpgraph_test.nodes, read_gpm.nodes)
예제 #4
0
파일: test_base.py 프로젝트: harmsm/gpgraph
def test_add_model(test_data):

    for d in test_data:

        gpm = GenotypePhenotypeMap(genotypes=d["genotypes"],
                                   wildtype=d["wildtype"],
                                   phenotypes=d["phenotypes"],
                                   bad_pheno=d["phenotypes"])

        # Give negative phenotypes (which models except ratio will not like)
        gpm.data.loc[:, "bad_pheno"] = gpm.data.loc[:, "bad_pheno"] * -1

        G = GenotypePhenotypeGraph()

        # Make sure model checks properly for adding gpm first
        with pytest.raises(ValueError):
            G.add_model()

        # Use basic hamming connectivity model
        G.add_gpm(gpm, "hamming", 1)

        # Try calculating ratio and sswm models
        for m in ["ratio", "sswm"]:
            G.add_model(column="phenotypes", model=m)

        # Try calculating moran and mcclandish with multiple population sizes
        for m in ["moran", "mcclandish"]:
            for N in [1, 3, 10, 30, 100, 300, 1000, 3000, 10000]:
                G.add_model(column="phenotypes", model=m, population_size=N)

        # Pass in some crappy data
        with pytest.raises(ValueError):
            G.add_model(column="bad_pheno", model="sswm")

        # should work for ratio
        G.add_model(column="bad_pheno", model="ratio")

        # Pass in string data
        with pytest.raises(ValueError):
            G.add_model(column="genotypes", model="sswm")

        # don't pass in required arg
        with pytest.raises(ValueError):
            G.add_model(column="phenotypes", model="mcclandish")

        # pass in nonsensical version of required arg
        with pytest.raises(ValueError):
            G.add_model(column="phenotypes",
                        model="mcclandish",
                        population_size=-5)

        # pass in undefined model
        with pytest.raises(ValueError):
            G.add_model(column="phenotypes", model="not_a_real_model")

        # pass in unrecognized arg to a model
        with pytest.raises(ValueError):
            G.add_model(column="phenotypes", model="sswm", population_size=10)

        # pass in incompatible model
        def bad_model(v1):
            return 5

        with pytest.raises(ValueError):
            G.add_model(column="phenotypes", model=bad_model)

        # okay model, only float args
        def okay_model(v1, v2):
            return 5

        G.add_model(column="phenotypes", model=okay_model)

        # okay model, float and custom kwarg
        def okay_model(v1, v2, needed_kwarg):
            return 5

        G.add_model(column="phenotypes", model=okay_model, needed_kwarg=5)

        # check node property setting for this model where we know the answer
        for e in G.edges:
            assert G.edges[e]["prob"] == 5

        # pass in no model (should set all prob to 1)
        G.add_model(column="phenotypes")
        for e in G.edges:
            assert G.edges[e]["prob"] == 1

        # pass in nothing (all prob should be 1)
        G.add_model()
        for e in G.edges:
            assert G.edges[e]["prob"] == 1

        # pass in no data with model. prob should all be 0 for sswm because
        # fitness is same all genotypes.
        G.add_model(model="sswm")
        for e in G.edges:
            assert G.edges[e]["prob"] == 0
예제 #5
0
파일: test_base.py 프로젝트: harmsm/gpgraph
def test_add_gpm(test_data):

    # Check hamming = 1
    G = GenotypePhenotypeGraph()
    gpm = GenotypePhenotypeMap(genotypes=["SG", "PF", "SF", "PG"])
    G.add_gpm(gpm, "hamming", cutoff=1)

    new_edges = list(G.edges)
    new_edges.sort()
    hamming_edges = [(0, 2), (2, 0), (0, 3), (3, 0), (1, 2), (2, 1), (1, 3),
                     (3, 1)]
    hamming_edges.sort()
    for i in range(len(new_edges)):
        assert np.array_equal(hamming_edges[i], new_edges[i])

    # Check hamming = 2
    G = GenotypePhenotypeGraph()
    gpm = GenotypePhenotypeMap(genotypes=["SG", "PF", "SF", "PG"])
    G.add_gpm(gpm, "hamming", cutoff=2)

    new_edges = list(G.edges)
    new_edges.sort()
    hamming_edges = [(0, 1), (1, 0), (0, 2), (2, 0), (0, 3), (3, 0), (1, 2),
                     (2, 1), (1, 3), (3, 1), (2, 3), (3, 2)]
    hamming_edges.sort()
    for i in range(len(new_edges)):
        assert np.array_equal(hamming_edges[i], new_edges[i])

    # Check amino acid codon = 1
    G = GenotypePhenotypeGraph()
    gpm = GenotypePhenotypeMap(genotypes=["SG", "PF", "SF", "PG"])
    G.add_gpm(gpm, "codon", cutoff=1)

    new_edges = list(G.edges)
    new_edges.sort()
    codon_edges = [(0, 3), (3, 0), (1, 2), (2, 1)]
    codon_edges.sort()
    for i in range(len(new_edges)):
        assert np.array_equal(codon_edges[i], new_edges[i])

    for d in test_data:

        gpm = GenotypePhenotypeMap(genotypes=d["genotypes"],
                                   wildtype=d["wildtype"],
                                   phenotypes=d["phenotypes"])

        G = GenotypePhenotypeGraph()
        G.add_gpm(gpm)

        assert type(G.gpm) is GenotypePhenotypeMap
        assert len(G.nodes) == len(gpm.genotypes)
        for i in range(len(G.nodes)):
            G.nodes[i]["genotypes"] == gpm.genotypes[i]
            G.nodes[i]["phenotypes"] == gpm.phenotypes[i]
예제 #6
0
def test_read_csv(gpgraph_test):
    """Test reading from csv"""
    read_gpm = GenotypePhenotypeGraph.read_csv("data/test_data_external.csv",
                                               wildtype='RDDWKAQ')
    # Test instance was created
    assert isinstance(read_gpm, GenotypePhenotypeGraph)
예제 #7
0
def gpgraph_test(gpmap_base):
    gpgraph_test = GenotypePhenotypeGraph(gpmap_base)

    return gpgraph_test