DHH20070627-02

InfoInfo
Search:    

Go to the [WWW]original post
Go back to the DHH Archive

Python script to generate Graphviz file

# ncd-dcb-graph.py
#
# Use Graphviz to plot a network diagram of
# NCDs below some threshold
# using randomly chosen sample of entries from the
# Dictionary of Canadian Biography volume 1
#
# wjt
# http://digitalhistoryhacks.blogspot.com
#
# 26 jun 2007

threshold = 0.77

infile = open('ncd-dcb.txt', 'r')
rows = infile.readlines()
infile.close()

bios = []
links = []

for r in rows:
    row = r.split()
    if float(row[2]) < threshold:
        bios.append(row[0])
        bios.append(row[1])
        links.append(row)
        # print r,

print list(set(bios))
print links

graph_file = open('ncd-dcb-graph.txt', 'w')
graph_file.write('digraph G {\n')
graph_file.write('\tgraph [overlap=scale];\n')
graph_file.write('\tnode [color=steelblue3];\n')

bios = list(set(bios))
for b in bios:
    graph_file.write('\t' + b + ' [color=lightblue2, style=filled];\n')

for l in links:
    graph_file.write('\t' + l[0] + ' -> ' + l[1] + ' [arrowhead=both];\n')

graph_file.write('}')
graph_file.close()

Graphviz file

digraph G {
        graph [overlap=scale];
        node [color=steelblue3];
        34438 [color=lightblue2, style=filled];
        34652 [color=lightblue2, style=filled];
        34693 [color=lightblue2, style=filled];
        34167 [color=lightblue2, style=filled];
        34635 [color=lightblue2, style=filled];
        34678 [color=lightblue2, style=filled];
        34611 [color=lightblue2, style=filled];
        34631 [color=lightblue2, style=filled];
        34675 [color=lightblue2, style=filled];
        34397 [color=lightblue2, style=filled];
        34195 [color=lightblue2, style=filled];
        34638 [color=lightblue2, style=filled];
        34208 [color=lightblue2, style=filled];
        34443 [color=lightblue2, style=filled];
        34155 [color=lightblue2, style=filled];
        34445 [color=lightblue2, style=filled];
        34461 [color=lightblue2, style=filled];
        34170 [color=lightblue2, style=filled];
        34281 [color=lightblue2, style=filled];
        34268 [color=lightblue2, style=filled];
        34404 [color=lightblue2, style=filled];
        34265 [color=lightblue2, style=filled];
        34340 [color=lightblue2, style=filled];
        34174 [color=lightblue2, style=filled];
        34403 [color=lightblue2, style=filled];
        34626 [color=lightblue2, style=filled];
        34588 [color=lightblue2, style=filled];
        34541 [color=lightblue2, style=filled];
        34540 [color=lightblue2, style=filled];
        34604 [color=lightblue2, style=filled];
        34520 [color=lightblue2, style=filled];
        34708 [color=lightblue2, style=filled];
        34576 [color=lightblue2, style=filled];
        34538 [color=lightblue2, style=filled];
        34599 [color=lightblue2, style=filled];
        34457 [color=lightblue2, style=filled];
        34140 [color=lightblue2, style=filled];
        34475 [color=lightblue2, style=filled];
        34594 [color=lightblue2, style=filled];
        34595 [color=lightblue2, style=filled];
        34471 [color=lightblue2, style=filled];
        34470 [color=lightblue2, style=filled];
        34155 -> 34174 [arrowhead=both];
        34155 -> 34170 [arrowhead=both];
        34631 -> 34268 [arrowhead=both];
        34470 -> 34155 [arrowhead=both];
        34445 -> 34155 [arrowhead=both];
        34708 -> 34268 [arrowhead=both];
        34652 -> 34693 [arrowhead=both];
        34445 -> 34170 [arrowhead=both];
        34631 -> 34708 [arrowhead=both];
        34520 -> 34538 [arrowhead=both];
        34281 -> 34635 [arrowhead=both];
        34576 -> 34397 [arrowhead=both];
        34155 -> 34140 [arrowhead=both];
        34170 -> 34174 [arrowhead=both];
        34445 -> 34470 [arrowhead=both];
        34595 -> 34611 [arrowhead=both];
        34626 -> 34678 [arrowhead=both];
        34445 -> 34140 [arrowhead=both];
        34445 -> 34404 [arrowhead=both];
        34265 -> 34404 [arrowhead=both];
        34170 -> 34167 [arrowhead=both];
        34404 -> 34140 [arrowhead=both];
        34445 -> 34174 [arrowhead=both];
        34470 -> 34170 [arrowhead=both];
        34540 -> 34155 [arrowhead=both];
        34576 -> 34438 [arrowhead=both];
        34611 -> 34155 [arrowhead=both];
        34604 -> 34208 [arrowhead=both];
        34540 -> 34167 [arrowhead=both];
        34470 -> 34174 [arrowhead=both];
        34443 -> 34208 [arrowhead=both];
        34594 -> 34195 [arrowhead=both];
        34461 -> 34541 [arrowhead=both];
        34443 -> 34604 [arrowhead=both];
        34265 -> 34155 [arrowhead=both];
        34265 -> 34457 [arrowhead=both];
        34471 -> 34208 [arrowhead=both];
        34443 -> 34595 [arrowhead=both];
        34693 -> 34340 [arrowhead=both];
        34626 -> 34541 [arrowhead=both];
        34604 -> 34588 [arrowhead=both];
        34265 -> 34638 [arrowhead=both];
        34170 -> 34140 [arrowhead=both];
        34445 -> 34541 [arrowhead=both];
        34470 -> 34541 [arrowhead=both];
        34155 -> 34167 [arrowhead=both];
        34443 -> 34599 [arrowhead=both];
        34457 -> 34635 [arrowhead=both];
        34675 -> 34340 [arrowhead=both];
        34541 -> 34140 [arrowhead=both];
        34576 -> 34631 [arrowhead=both];
        34167 -> 34195 [arrowhead=both];
        34638 -> 34445 [arrowhead=both];
        34540 -> 34470 [arrowhead=both];
        34470 -> 34140 [arrowhead=both];
        34631 -> 34403 [arrowhead=both];
        34626 -> 34404 [arrowhead=both];
        34404 -> 34174 [arrowhead=both];
        34588 -> 34195 [arrowhead=both];
        34595 -> 34599 [arrowhead=both];
        34445 -> 34167 [arrowhead=both];
        34638 -> 34170 [arrowhead=both];
        34520 -> 34678 [arrowhead=both];
        34471 -> 34475 [arrowhead=both];
        34611 -> 34174 [arrowhead=both];
}
This is a Wiki Spot wiki. Wiki Spot is a 501(c)3 non-profit organization that helps communities collaborate via wikis.