This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Graph generators take 2 #26
Open
mzargham
wants to merge
14
commits into
master
Choose a base branch
from
graph-generators-take-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a6e537b
copied graph generators to infra
mzargham 5a8013a
applied black to graph generators
mzargham 7f6a018
matching variable convensions
mzargham aac3efb
factored out helper functions
mzargham 7cf686f
refactored functions with helpers
mzargham 3be7d78
refactored node and edge types into helper
mzargham d6cf679
added error handling for inputs
mzargham e05f8b5
ran black to format post edits
mzargham 2d24882
value error use 'in'
mzargham cd31f9e
updated method names and added tests; tests fail
mzargham d4b715a
fixed the set types method
mzargham 3d6c01a
added more tests; tests passed
mzargham bbdd973
Add test runner hook to graph_generators_test
teamdandelion 22aed74
Refactor unit tests and helper methods
teamdandelion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
This Module provides functions for generating simple networks | ||
""" | ||
import networkx as nx | ||
|
||
|
||
def bidirectional(graph): | ||
""" | ||
Returns a bidirectional copy of the input graph. | ||
|
||
For every edge in the input graph, the bidirectional graph | ||
has a corresponding reversed edge. | ||
|
||
Does not mutate the input graph. | ||
""" | ||
output = nx.MultiDiGraph() | ||
for e in graph.edges: | ||
output.add_edge(e[0], e[1]) | ||
output.add_edge(e[1], e[0]) | ||
return output | ||
|
||
|
||
def reverse(graph): | ||
""" | ||
Returns a reversed direction copy of the input graph. | ||
|
||
For every edge in the input graph, there will be a reversed edge in the | ||
resultant graph. | ||
|
||
Does not mutate the input graph. | ||
""" | ||
output = nx.MultiDiGraph() | ||
for e in graph.edges: | ||
output.add_edge(e[1], e[0]) | ||
return output | ||
|
||
|
||
def set_types(graph, node_types="vanilla", edge_types="vanilla"): | ||
""" maps edge types and node types onto graph""" | ||
if type(node_types) == dict: | ||
for n in graph.nodes: | ||
try: | ||
graph.nodes[n]["type"] = node_types[n] | ||
except: | ||
raise ValueError( | ||
"if node_types is dictionary, must be keyed to the nodes of the graph, " | ||
+ str(node_types) | ||
+ " provided" | ||
) | ||
elif type(node_types) == str: | ||
nx.set_node_attributes(graph, node_types, "type") | ||
else: | ||
raise ValueError( | ||
"node_types must be string or dictionary keyed to the nodes of the graph, " | ||
+ str(node_types) | ||
+ " provided" | ||
) | ||
|
||
if type(edge_types) == dict: | ||
for e in graph.edges: | ||
try: | ||
graph.edges[e]["type"] = edge_types[e] | ||
except: | ||
raise ValueError( | ||
"edges_types must be string or dictionary keyed to the nodes of the graph, " | ||
+ str(edge_types) | ||
+ " provided" | ||
) | ||
elif type(edge_types) == str: | ||
nx.set_edge_attributes(graph, edge_types, "type") | ||
else: | ||
raise ValueError( | ||
"if edges_types is dictionary, must keyed to the edges of the graph, " | ||
+ str(edge_types) | ||
+ " provided" | ||
) | ||
|
||
return graph | ||
|
||
|
||
def line_graph_gen( | ||
num_nodes, bidir=False, node_type_name="vanilla", edge_type_name="vanilla" | ||
): | ||
"""returns a line graph of length num_nodes""" | ||
|
||
if not (type(num_nodes) == int) or (num_nodes < 1): | ||
raise ValueError( | ||
"num_nodes must be positive integer, " + str(num_nodes) + " provided" | ||
) | ||
|
||
if not (type(bidir) == bool): | ||
raise ValueError("bidir must be boolean, " + str(bidir) + " provided") | ||
|
||
graph = nx.path_graph(num_nodes, create_using=nx.MultiDiGraph) | ||
if bidir: | ||
graph = bidirectional(graph) | ||
|
||
graph = set_types(graph, edge_types=node_type_name, node_types=edge_type_name) | ||
|
||
return graph | ||
|
||
|
||
def star_graph_gen( | ||
num_nodes, kind="sink", node_type_name="vanilla", edge_type_name="vanilla" | ||
): | ||
"""returns a star graph of length num_nodes""" | ||
|
||
if not (type(num_nodes) == int) or (num_nodes < 1): | ||
raise ValueError( | ||
"num_nodes must be positive integer, " + str(num_nodes) + " provided" | ||
) | ||
|
||
if kind not in ("source", "sink", "bidir"): | ||
raise ValueError( | ||
'kind must be "sink", "source" or "bidir", ' + str(kind) + " provided" | ||
) | ||
|
||
graph = nx.MultiDiGraph(nx.star_graph(num_nodes - 1)) | ||
|
||
for n in range(1, num_nodes): | ||
if kind == "sink": | ||
graph.remove_edge(0, n) | ||
elif kind == "source": | ||
graph.remove_edge(n, 0) | ||
|
||
graph = set_types(graph, edge_types=node_type_name, node_types=edge_type_name) | ||
|
||
return graph | ||
|
||
|
||
def circle_graph_gen( | ||
num_nodes, bidir=False, node_type_name="vanilla", edge_type_name="vanilla" | ||
): | ||
"""returns a cycle graph of length num_nodes""" | ||
|
||
if not (type(num_nodes) == int) or (num_nodes < 1): | ||
raise ValueError( | ||
"num_nodes must be positive integer, " + str(num_nodes) + " provided" | ||
) | ||
|
||
if not (type(bidir) == bool): | ||
raise ValueError("bidir must be boolean, " + str(bidir) + " provided") | ||
|
||
graph = nx.cycle_graph(num_nodes, create_using=nx.MultiDiGraph) | ||
|
||
if bidir: | ||
graph = bidirectional(graph) | ||
|
||
graph = set_types(graph, edge_types=node_type_name, node_types=edge_type_name) | ||
|
||
return graph | ||
|
||
|
||
def tree_graph_gen( | ||
rate, height, kind="sink", node_type_name="vanilla", edge_type_name="vanilla" | ||
): | ||
"""returns a tree graph of depth height and splitting rate rate""" | ||
|
||
if not (type(rate) == int) or (rate < 1): | ||
raise ValueError("rate must be positive integer, " + str(rate) + " provided") | ||
|
||
if not (type(height) == int) or (height < 1): | ||
raise ValueError( | ||
"height must be positive integer, " + str(height) + " provided" | ||
) | ||
|
||
if kind not in ("source", "sink", "bidir"): | ||
raise ValueError( | ||
'kind must be "sink", "source" or "bidir", ' + str(kind) + " provided" | ||
) | ||
|
||
graph = nx.balanced_tree(rate, height, create_using=nx.MultiDiGraph) | ||
|
||
if kind == "sink": | ||
graph = reverse(graph) | ||
elif kind == "bidir": | ||
graph = bidirectional(graph) | ||
|
||
graph = set_types(graph, edge_types=node_type_name, node_types=edge_type_name) | ||
|
||
return graph |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this API (or the way that types are handled in this PR generally). When node_types is provided as a dict, there's no guarantee that every node is present in the dict, so I expect that often times nodes will go without any type at all. So there's no guarantee that types are consistently present. Also, AFAIK we don't have any code yet that actually consumes node types.
Can we strip out the node and edge types concept from this PR and merge without them? Then, once we actually need node types, we can put them back in, but we will know a little more at that time based on what the actual usage is.