def test_synchronize(test_data): # Should work even without gpm G = gpmap.GenotypePhenotypeGraph() assert G.synchronize() is None for d in test_data: # Make map G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) # Delete a node using DiGraph method and make sure it's really gone by # calling the super nodes property. (The GenotypePhenotypeGraph.nodes # property will call sync_nodes implicitly). super(gpmap.GenotypePhenotypeGraph,G).remove_node(0) assert len(super(gpmap.GenotypePhenotypeGraph,G).nodes) == len(d["genotype"]) - 1 # Should be back because gpm.data always wins G.synchronize() assert len(G.nodes) == len(d["genotype"]) # Delete a genotype directly from gpm.data mask = np.arange(1,len(G.gpm.data),dtype=int) G.gpm._data = G.gpm.data.loc[mask,:] # Should now lose a node. G.synchronize() assert len(G.nodes) == len(d["genotype"]) - 1 for d in test_data: # Make map G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) num_neighbors = len(G.gpm.neighbors) # Delete an edge using DiGraph method and make sure it's really gone by # calling the super edges property. (The GenotypePhenotypeGraph.edges # property will call sync_nodes implicitly). super(gpmap.GenotypePhenotypeGraph,G).remove_edge(*list(G.edges)[0]) assert len(super(gpmap.GenotypePhenotypeGraph,G).edges) == num_neighbors - 1 # Should be back because gpm.data always wins G.synchronize() assert len(G.edges) == num_neighbors # Delete an edge directly from gpm.neighbors mask = np.arange(len(G.gpm.neighbors)-1,dtype=int) G.gpm._neighbors = G.gpm.neighbors.loc[mask,:] # Should now lose an edge. G.synchronize() assert len(G.edges) == num_neighbors - 1
def test_nodes_getter(test_data): # Make sure getter works. note it runs sync_nodes # Should work even without gpm G = gpmap.GenotypePhenotypeGraph() assert len(G.nodes) == 0 for d in test_data: # Make map G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) # Delete a node using DiGraph method. It should not delete from node_list # because node_list getter runs sync_nodes() super(gpmap.GenotypePhenotypeGraph,G).remove_node(0) assert len(G.nodes) == len(d["genotype"]) # Delete a genotype directly from gpm.data old_data = G.gpm._data.copy() mask = np.arange(len(G.gpm.data)-1,dtype=int) G.gpm._data = G.gpm.data.loc[mask,:] # Should now lose a node. assert len(G.nodes) == len(d["genotype"]) - 1 # Put node back in via data frame. nodes should update. G.gpm._data = old_data assert len(G.nodes) == len(d["genotype"])
def test_add_remove_node_cmap(test_data): """ Test both add_node_cmap and remove_node_cmap methods. """ G = gpmap.GenotypePhenotypeGraph() with pytest.raises(RuntimeError): G.add_node_cmap("test") for d in test_data: G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) with pytest.raises(KeyError): G.add_node_cmap(data_column="not_a_column") with pytest.raises(ValueError): G.add_node_cmap(data_column="phenotype",cmap="not_a_cmap") G.add_node_cmap(data_column="phenotype",cmap="plasma") node_options = copy.deepcopy(G.node_options) assert type(node_options["node_color"]) is tuple assert node_options["node_color"][0] == "_gpm" assert node_options["node_color"][1] == "phenotype" assert type(node_options["node_color"][2]) is type(matplotlib.cm.get_cmap("plasma")) assert node_options["node_color"][3] == np.min(d["phenotype"]) assert node_options["node_color"][4] == np.max(d["phenotype"]) # Now pass in cmap as cm object G = gpmap.GenotypePhenotypeGraph() G.add_gpm(gpm) cmap = matplotlib.cm.get_cmap("plasma") G.add_node_cmap(data_column="phenotype",cmap=cmap) node_options = copy.deepcopy(G.node_options) assert type(node_options["node_color"]) is tuple assert node_options["node_color"][0] == "_gpm" assert node_options["node_color"][1] == "phenotype" assert type(node_options["node_color"][2]) is type(matplotlib.cm.get_cmap("plasma")) assert node_options["node_color"][3] == np.min(d["phenotype"]) assert node_options["node_color"][4] == np.max(d["phenotype"]) G.add_node_cmap(data_column="phenotype",cmap=cmap,vmin=5,vmax=10) node_options = copy.deepcopy(G.node_options) assert type(node_options["node_color"]) is tuple assert node_options["node_color"][0] == "_gpm" assert node_options["node_color"][1] == "phenotype" assert type(node_options["node_color"][2]) is type(matplotlib.cm.get_cmap("plasma")) assert node_options["node_color"][3] == 5 assert node_options["node_color"][4] == 10 G.remove_node_cmap() removed_options = ["vmin","vmax","cmap"] for r in removed_options: with pytest.raises(KeyError): G.node_options[r] assert G.node_options["node_color"] == "gray"
def test_edge_weight_column_getter(): gpm = gpmap.GenotypePhenotypeMap(genotype=["00","11"]) G = gpmap.GenotypePhenotypeGraph(gpm) G.edge_weight_column = "weight" assert G.edge_weight_column == "weight" G.edge_weight_column = None assert G.edge_weight_column == None gpm = gpmap.GenotypePhenotypeMap(genotype=["00","01"]) gpm.get_neighbors() gpm.neighbors.loc[:,"test"] = np.ones(len(gpm.neighbors)) G = gpmap.GenotypePhenotypeGraph(gpm,edge_weight_column="test") assert G.edge_weight_column == "test"
def test_edges_getter(test_data): # Should work even without gpm G = gpmap.GenotypePhenotypeGraph() assert len(G.edges) == 0 for d in test_data: # Make map G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) new_gpm_neighbors = gpm.neighbors.copy() num_edges = len(G.gpm.neighbors) # Delete a edge using DiGraph method. It should not delete from edges # because edges getter runs sync_edges() e = list(super(gpmap.GenotypePhenotypeGraph,G).edges)[0] super(gpmap.GenotypePhenotypeGraph,G).remove_edge(*e) assert len(G.edges) == num_edges # Delete a genotype directly from gpm.data mask = np.arange(len(G.gpm.neighbors)-1,dtype=int) G.gpm._neighbors = G.gpm.neighbors.loc[mask,:] # Should now lose a edge. assert len(G.edges) == num_edges - 1 # Add full gpm, which will effectively add one genotype back in G.gpm._neighbors = new_gpm_neighbors.copy() assert len(G.edges) == num_edges # Set weight G.edge_weight_column = "weight" for e in G.edges: assert G.edges[e[0],e[1]]["weight"] == 1 # Remove weight G.edge_weight_column = None for e in G.edges: with pytest.raises(KeyError): G.edges[e[0],e[1]]["weight"]
def test_add_remove_edge_labels(test_data): # Throw error because no gpmap G = gpmap.GenotypePhenotypeGraph() with pytest.raises(RuntimeError): G.add_edge_labels("test") for d in test_data: # Make map G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) assert G.gpm is gpm # Throw error data column is not real with pytest.raises(KeyError): G.add_edge_labels(data_column="not_a_column") # This should work G.add_edge_labels(data_column="weight") # This should not work with pytest.raises(ValueError): G.add_edge_labels(data_column="weight",fmt="{:d}") G.add_edge_labels(data_column="weight") expected = ("_gpm","weight","{:.3f}") assert np.array_equal(expected,G.edge_label_options["edge_labels"]) # Check remove_edge_sizemap G.remove_edge_labels() with pytest.raises(KeyError): G.edge_label_options["edge_labels"] # Make sure fmt pass works G.add_edge_labels(data_column="weight",fmt="{}") expected = ("_gpm","weight","{}") assert np.array_equal(expected,G.edge_label_options["edge_labels"])
def test_draw_nodes(test_data): for d in test_data: gpm = GenotypePhenotypeMap(genotype=d["genotype"], phenotype=d["phenotype"]) G = gpmap.GenotypePhenotypeGraph() G.add_gpm(gpm) fig, ax = plt.subplots(figsize=(10, 10)) # Make sure it draws something. nodes = draw_nodes(G, flattened(G), ax) ax.plot # <- will throw attribute error if this is not right len(nodes)
def test__nx_wrapper(test_data): gpm = GenotypePhenotypeMap(genotype=["AA", "AB", "BA", "BB"]) G = gpmap.GenotypePhenotypeGraph() G.add_gpm(gpm) pos = flattened(G) fig, ax = plt.subplots(figsize=(10, 10)) # Should throw warning even though bad arg. with pytest.warns(UserWarning): ret = _nx_wrapper(nx.draw_networkx_nodes, G=G, pos=pos, ax=ax, not_a_real_argument=10)
def test_edge_weight_column_setter(test_data): G = gpmap.GenotypePhenotypeGraph() with pytest.raises(RuntimeError): G.edge_weight_column = "weight" for d in test_data: gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G.add_gpm(gpm) with pytest.raises(KeyError): G.edge_weight_column = "stupid" with pytest.raises(TypeError): G.edge_weight_column = "edge" # should work G.edge_weight_column = "weight" for g in G.edges: print(g) G.edges[g[0],g[1]]["weight"]
def test_add_remove_edge_sizemap(test_data): """ Test add_edge_sizemap and remove_edge_sizemap methods. """ # Throw error because no gpmap G = gpmap.GenotypePhenotypeGraph() with pytest.raises(RuntimeError): G.add_edge_sizemap("test") for d in test_data: # Make map G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G.add_gpm(gpm) assert G.gpm is gpm # Throw error data column is not real with pytest.raises(KeyError): G.add_edge_sizemap(data_column="not_a_column") # Throw error because vmin > vmax with pytest.raises(ValueError): G.add_edge_sizemap(data_column="weight",vmin=1000,vmax=10) # Throw error because size is < 0 with pytest.raises(ValueError): G.add_edge_sizemap(data_column="weight",size_min=-2) # Throw error because size_max < size_min with pytest.raises(ValueError): G.add_edge_sizemap(data_column="weight",size_max=2,size_min=10) # This should work G.gpm.neighbors.loc[:,"test"] = np.random.random(len(gpm.neighbors)) G.add_edge_sizemap(data_column="test") # Make sure min/max doing what we think for defaults mn = np.min(G.gpm.neighbors.loc[:,"test"]) mx = np.max(G.gpm.neighbors.loc[:,"test"]) expected = ("_gpm","test",mn,mx,0.1,20) assert np.array_equal(expected,G.edge_options["width"]) # Check remove_edge_sizemap G.remove_edge_sizemap() assert G.edge_options["width"] == G._default_edge_width # Make sure it takes in various phenotype, vmin, vmax, size_min, # size_max and does right stuff with them. new_phenos = [np.ones(len(gpm.neighbors)), np.random.random(len(gpm.neighbors))-0.5] for i in range(len(new_phenos)): G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G.add_gpm(gpm) G.gpm.neighbors.loc[:,"test"] = new_phenos[i] vmin = np.random.random() vmax = vmin + 10 sizemin = np.random.random() sizemax = sizemin + 10 G.add_edge_sizemap(data_column="test",vmin=vmin,vmax=vmax, size_min=sizemin,size_max=sizemax) expected = ("_gpm","test",vmin,vmax,sizemin,sizemax) assert np.array_equal(expected,G.edge_options["width"])
def test_add_remove_edge_cmap(test_data): """ Test both add_edge_cmap and remove_edge_cmap methods. """ G = gpmap.GenotypePhenotypeGraph() with pytest.raises(RuntimeError): G.add_edge_cmap("test") for d in test_data: # Test basic construction/error checking G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) G.add_gpm(gpm) with pytest.raises(KeyError): G.add_edge_cmap(data_column="not_a_column") with pytest.raises(ValueError): G.add_edge_cmap(data_column="weight",cmap="not_a_cmap") G.add_edge_cmap(data_column="weight",cmap="plasma") # Now pass in cmap as cm object G = gpmap.GenotypePhenotypeGraph() G.add_gpm(gpm) cmap = matplotlib.cm.get_cmap("plasma") G.add_edge_cmap(data_column="weight",cmap=cmap) # Now see if it works edge_options = copy.deepcopy(G.edge_options) assert type(edge_options["edge_color"]) is tuple assert edge_options["edge_color"][0] == "_gpm" assert edge_options["edge_color"][1] == "weight" assert type(edge_options["edge_color"][2]) is type(matplotlib.cm.get_cmap("plasma")) assert edge_options["edge_color"][3] == np.min([1]) assert edge_options["edge_color"][4] == np.min([1]) # Now pass in more interesting edge values G = gpmap.GenotypePhenotypeGraph() gpm = gpmap.GenotypePhenotypeMap(d["genotype"], phenotype=d["phenotype"]) gpm.get_neighbors() gpm.neighbors.loc[:,"flux"] = np.random.random(len(gpm.neighbors)) G.add_gpm(gpm) G.add_edge_cmap(data_column="flux",cmap="plasma") edge_options = copy.deepcopy(G.edge_options) assert type(edge_options["edge_color"]) is tuple assert edge_options["edge_color"][0] == "_gpm" assert edge_options["edge_color"][1] == "flux" assert type(edge_options["edge_color"][2]) is type(matplotlib.cm.get_cmap("plasma")) assert edge_options["edge_color"][3] == np.min(gpm.neighbors.loc[:,"flux"]) assert edge_options["edge_color"][4] == np.max(gpm.neighbors.loc[:,"flux"]) G.add_edge_cmap(data_column="flux",cmap=cmap,vmin=5,vmax=10) edge_options = copy.deepcopy(G.edge_options) assert type(edge_options["edge_color"]) is tuple assert edge_options["edge_color"][0] == "_gpm" assert edge_options["edge_color"][1] == "flux" assert type(edge_options["edge_color"][2]) is type(matplotlib.cm.get_cmap("plasma")) assert edge_options["edge_color"][3] == 5 assert edge_options["edge_color"][4] == 10 # Now test removal G.remove_edge_cmap() removed_options = ["edge_vmin","edge_vmax","edge_cmap"] for r in removed_options: with pytest.raises(KeyError): G.edge_options[r] assert G.edge_options["edge_color"] == "black"
def test_pyplot_plot(test_data): d = test_data[0] # Feed in GenotypePhenotypeMap gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm) assert isinstance(G, gpmap.GenotypePhenotypeGraph) assert isinstance(fig, matplotlib.figure.Figure) assert isinstance(ax, matplotlib.axes.Axes) plt.close() # Feed in GenotypePhenotypeGraph gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G_in = gpmap.GenotypePhenotypeGraph() G_in.add_gpm(gpm) G, fig, ax = gpmap.plot(G_in) assert G is G assert gpm is G.gpm assert isinstance(G, gpmap.GenotypePhenotypeGraph) assert isinstance(fig, matplotlib.figure.Figure) assert isinstance(ax, matplotlib.axes.Axes) plt.close() # Feed in bad stuff bad_args = ["test", gpmap.GenotypePhenotypeGraph, (1, 23), 14] for b in bad_args: with pytest.raises(TypeError): gpmap.plot(b) # Feed in all combinations of plot modes... bool_args = [ "plot_nodes", "plot_edges", "plot_node_labels", "plot_edge_labels" ] for i in range(len(bool_args)): for c in itertools.combinations(bool_args, i + 1): bool_kwargs = dict([(k, True) for k in bool_args]) for k in c: bool_kwargs[k] = False gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, **bool_kwargs) assert isinstance(G, gpmap.GenotypePhenotypeGraph) assert isinstance(fig, matplotlib.figure.Figure) assert isinstance(ax, matplotlib.axes.Axes) plt.close() # Check passing node options gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, node_options={"node_size": 5}) plt.close() assert G.node_options["node_size"] == 5 with pytest.raises(TypeError): gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, node_options="not_right_type") # Check passing edge options gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, edge_options={"style": "--"}) plt.close() assert G.edge_options["style"] == "--" with pytest.raises(TypeError): gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, edge_options="not_right_type") # Check passing node label options gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, node_label_options={"font_size": 14}) plt.close() assert G.node_label_options["font_size"] == 14 with pytest.raises(TypeError): gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, node_label_options="not_right_type") # Check passing edge label options gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, edge_label_options={"font_size": 14}) plt.close() assert G.edge_label_options["font_size"] == 14 with pytest.raises(TypeError): gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, edge_label_options="not_right_type") # Test figsize setting gpm = gpmap.GenotypePhenotypeMap(d["genotype"]) G, fig, ax = gpmap.plot(gpm, figsize=(2, 2)) assert np.array_equal(fig.get_size_inches(), (2, 2)) plt.close() bad_fig_size = [(1, 2, 3), (1, ), "stupid", "RA", 5] for b in bad_fig_size: with pytest.raises(ValueError): G, fig, ax = gpmap.plot(gpm, figsize=b) # test ax pass G, fig, ax = gpmap.plot(gpm) G, fig, ax2 = gpmap.plot(gpm, ax=ax) assert ax is ax2 with pytest.raises(TypeError): G, fig, ax = gpmap.plot(gpm, ax="stupid") plt.close() plt.close()