1 #copyright ben lipkowitz, 2009. distributed under the GNU GPL version 2 or later
4 # cat processes.yaml | sed 's/!.*$//' > processes_notags.yaml #strip tags which confuse yaml
5 # python taxonomy-graph.py |dot -Grankdir=LR -Tjpg -o 'foo.jpg'
12 depthcolors
= {} #keeping track of colors per each level
14 def random_color(depth
):
15 '''A colorvalue may be "h,s,v" (hue, saturation, brightness) floating
16 point numbers between 0 and 1, or an X11 color name such as white black
17 red green blue yellow magenta cyan or burlywood, or a "#rrggbb" (red,
18 green, blue, 2 hex characters each) value.'''
19 hue
= random
.uniform(0,1)
20 saturation
= random
.uniform(0, 0.3)
22 returnstring
= '"%f,%f,%f"' % (hue
, saturation
, brightness
)
23 # set color to previous color at this depth (if exists)
24 if depthcolors
.has_key(depth
):
25 returnstring
= (depthcolors
[depth
])[0]
27 # alt functionality: check if there's a color too similar already at this depth.
31 #linnaeus = yaml.load(open('processes_notags.yaml'))['abrasive jet']
32 linnaeus
= yaml
.load(open('trans-tech.yaml'))
34 taxonomy
= graph
.digraph()
35 node_id
=0 #we need numerical nodes because some terms show up multiple times, like thermal, mechanical, chemical
37 def walk(treebeard
, color
, parent_node
, depth
):
40 if hasattr(treebeard
, 'keys'):
41 children
= treebeard
.keys()
42 for child
in children
:
44 taxonomy
.add_node(node_id
, [('label', child
), ('shape', 'box'),
45 ('fontsize', '24'), ('color', color
), ('style', 'filled')])
46 taxonomy
.add_edge(parent_node
, node_id
)
47 mycolor
= random_color(depth
)
48 if (depthcolors
.has_key(depth
)): depthcolors
[depth
].append(mycolor
)
49 else: depthcolors
[depth
] = [color
]
50 #if hasattr(child, 'keys'):
51 walk(treebeard
[child
], mycolor
, node_id
, depth
+1)
54 taxonomy
.add_node(node_id
, [('label', 'root')])
55 walk(linnaeus
, 'yellow', node_id
, 0)
57 print graph
.readwrite
.write_dot_graph(taxonomy
, False)