various methods to parse pages from thingiverse
[skdb.git] / taxonomy-graph.py
blob29c2871be3bc01f76e0be782a018ebadc433e326
1 #copyright ben lipkowitz, 2009. distributed under the GNU GPL version 2 or later
3 #usage:
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'
7 import os
8 import yaml
9 import graph
10 import random
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)
21 brightness = 0.9
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]
26 return returnstring
27 # alt functionality: check if there's a color too similar already at this depth.
28 # similarity means ?
29 return returnstring
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):
38 global node_id
39 children = []
40 if hasattr(treebeard, 'keys'):
41 children = treebeard.keys()
42 for child in children:
43 node_id += 1
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)