2012-09-08 05:10:31 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import pygraphviz as pgv
|
|
|
|
import json
|
2012-09-11 00:41:29 +00:00
|
|
|
import argparse
|
2012-09-08 05:10:31 +00:00
|
|
|
|
2012-09-08 15:49:04 +00:00
|
|
|
# These are the attributes to use for color-coding the graphs
|
|
|
|
|
|
|
|
# Three gender colours, so that we encapsulate masculine, feminine, and a catch-all for other identities
|
|
|
|
# If you'd rather explicitly colour-code other identities, just add them :)
|
2012-09-08 05:10:31 +00:00
|
|
|
gender_colors = {
|
|
|
|
'm': 'blue',
|
|
|
|
'f': 'pink',
|
|
|
|
'o': 'green',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-11 00:41:29 +00:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description='Generate graphs of polyamorous family networks from data files')
|
|
|
|
parser.add_argument('--input', '-i', default='network.json', help='Input file in json format')
|
|
|
|
parser.add_argument('--output', '-o', default='network.png', help='Output image file. Format auto-detected from file extension')
|
2012-09-12 14:20:34 +00:00
|
|
|
parser.add_argument('--gender', '-g', action='store_true', help='Indicate gender in output graph')
|
|
|
|
parser.add_argument('--relationships', '-r', action='store_true', help='Indicate relationship details (std risk, relationship type) in output graph')
|
2012-09-11 00:41:29 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2012-09-08 05:10:31 +00:00
|
|
|
def main():
|
2012-09-11 00:41:29 +00:00
|
|
|
settings = parse_args()
|
|
|
|
|
2012-09-08 15:49:04 +00:00
|
|
|
try:
|
2012-09-11 00:41:29 +00:00
|
|
|
data = json.load(open(settings.input, 'r'))
|
2012-09-08 15:49:04 +00:00
|
|
|
except Exception as e:
|
|
|
|
print "Error parsing data file: " + str(e)
|
|
|
|
return
|
2012-09-08 05:10:31 +00:00
|
|
|
|
|
|
|
graph = pgv.AGraph()
|
|
|
|
graph.graph_attr['overlap'] = 'prism'
|
2012-09-08 15:49:04 +00:00
|
|
|
graph.node_attr['style'] = 'filled'
|
|
|
|
graph.edge_attr['arrowsize'] = 0.5
|
2012-09-08 05:10:31 +00:00
|
|
|
|
|
|
|
# Add nodes to graph
|
2012-09-11 00:56:38 +00:00
|
|
|
gender_color = 'white'
|
|
|
|
|
2012-09-11 00:32:33 +00:00
|
|
|
for user in data['members']:
|
2012-09-11 00:56:38 +00:00
|
|
|
if settings.gender:
|
|
|
|
gender_color = gender_colors[user['gender']]
|
|
|
|
|
|
|
|
graph.add_node(user['name'], fillcolor=gender_color)
|
2012-09-08 05:10:31 +00:00
|
|
|
|
|
|
|
# Add edges to graph
|
2013-05-29 22:44:38 +00:00
|
|
|
edge_style = 'solid'
|
2012-09-11 00:56:38 +00:00
|
|
|
|
2013-05-29 22:44:38 +00:00
|
|
|
for (member1,member2,rel,edge) in data['relationships']:
|
|
|
|
if settings.relationships:
|
|
|
|
edge_style = edge
|
2012-09-11 00:56:38 +00:00
|
|
|
relationship_color = relationship_colors[rel]
|
|
|
|
|
2013-05-29 22:44:38 +00:00
|
|
|
graph.add_edge(member1, member2, style=edge_style, color='black')
|
2012-09-08 05:10:31 +00:00
|
|
|
|
|
|
|
graph.layout()
|
2012-09-11 00:41:29 +00:00
|
|
|
graph.draw(settings.output)
|
2012-09-08 05:10:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': main()
|