def main(): # parse the command line if (len(argv) != 1): usage("give me no arguments") # read the tree forest = read_howde_tree_file(stdin, keepFileExtension=True) assert (len(forest) != 0), "input has no tree" # find the deepest node maxDepth = None for tree in forest: tree.compute_depth() preOrder = tree.pre_order() treeMaxDepth = max([node.depth for node in preOrder]) if (maxDepth == None) or (treeMaxDepth > maxDepth): maxDepth = treeMaxDepth print("max depth is %d" % maxDepth)
def main(): global debug # parse the command line nameTemplate = None debug = {} for arg in argv[1:]: if ("=" in arg): argVal = arg.split("=", 1)[1] if ("{node}" in arg): nameTemplate = arg elif (arg == "--debug"): debug["debug"] = True elif (arg.startswith("--debug=")): for name in argVal.split(","): debug[name] = True else: usage("unrecognized option: %s" % arg) if (nameTemplate == None): usage("you must give me a template for the new node names") # read the tree forest = read_howde_tree_file(stdin, keepFileExtension=True, debug=("parse" in debug)) assert (len(forest) != 0), "input has no tree" # collect nodes by depth depthToNodes = {} for tree in forest: tree.compute_depth() preOrder = tree.pre_order() for node in preOrder: if (node.depth not in depthToNodes): depthToNodes[node.depth] = [] depthToNodes[node.depth] += [node] # assign new names nodeNumber = 0 for depth in depthToNodes: for node in depthToNodes[depth]: if (len(node.children) == 0): continue nodeNumber += 1 node.name = nameTemplate.replace("{node}", str(nodeNumber)) # output the tree topology, with the new names for tree in forest: tree.list_pre_order()
def main(): # parse the command line showWhat = "pre order" fileSpec = None for arg in argv[1:]: if ("=" in arg): argVal = arg.split("=", 1)[1] if (arg in ["--show=preorder", "--show=pre"]): showWhat = "pre order" elif (arg in ["--show=postorder", "--show=post"]): showWhat = "post order" elif (arg == "--show=leafgroups"): showWhat = "leaf groups" elif (arg == "--show=height"): showWhat = "height etc" elif (arg.startswith("--show:subtree=")) or ( arg.startswith("--subtree=")): showWhat = "subtree" nodeName = argVal elif (arg.startswith("--filespec=")): if ("{name}" not in argVal): usage("filespec MUST contain {name}\n(in \"%s\"" % arg) fileSpec = argVal elif (arg.startswith("--")): usage("unrecognized option: %s" % arg) else: usage("unrecognized option: %s" % arg) # process the tree forest = read_howde_tree_file(stdin) assert (len(forest) != 0), "input has no tree" for tree in forest: if (showWhat == "pre order"): tree.list_pre_order() elif (showWhat == "post order"): tree.list_post_order() elif (showWhat == "leaf groups"): tree.list_leaf_groups() elif (showWhat == "height etc"): tree.compute_height_etc() tree.list_height_etc() elif (showWhat == "subtree"): nameToNode = tree.build_dict() assert (nodeName in nameToNode), \ "unknown node: \"%s\"" % nodeName subtree = nameToNode[nodeName] subtree.list_pre_order(fileSpec=fileSpec) else: assert (False), \ "internal error: unknown operation \"%s\"" % showWhat
def main(): # parse the command line leavesOnly = False reportParent = False for arg in argv[1:]: if ("=" in arg): argVal = arg.split("=", 1)[1] if (arg in ["--leafonly", "--leavesonly", "--onlyleaves"]): leavesOnly = True elif (arg in ["--withparent", "--withparents", "--parent", "--parents"]): reportParent = True elif (arg in [ "--withoutparent", "--withoutparents", "--noparent", "--noparents" ]): reportParent = False else: usage("unrecognized option: %s" % arg) # read the tree forest = read_howde_tree_file(stdin, keepFileExtension=True) assert (len(forest) != 0), "input has no tree" # report the siblings if (len(forest) > 1): if (reportParent): print("forest ", end="") print(" ".join([root.name for root in forest])) for root in forest: preOrder = root.pre_order() for node in preOrder: if (node.children == []): continue if (leavesOnly): isAllLeaves = True for child in node.children: if (child.children != []): isAllLeaves = False break if (not isAllLeaves): continue if (reportParent): print(node.name, "", end="") print(" ".join([child.name for child in node.children]))
def main(): # parse the command line if (len(argv) > 1): usage("give me no arguments") # read the tree forest = read_howde_tree_file(stdin,keepFileExtension=True) assert (len(forest) != 0), "input has no tree" # collect the stats stats = [] for tree in forest: tree.compute_depth() preOrder = tree.pre_order() numLeaves = 0 for node in preOrder: if (len(node.children) == 0): numLeaves += 1 treeDepth = max([node.depth for node in preOrder]) stats += [(len(preOrder),numLeaves,treeDepth,tree.name)] # report the stats stats.sort() stats.reverse() print "# %d tree(s)" % len(forest) for (treeSize,numLeaves,treeDepth,name) in stats: print "%s treesize=%d leaves=%d depth=%s" \ % (name,treeSize,numLeaves,treeDepth)
def main(): # parse the command line reportTable = False fileSpec = None for arg in argv[1:]: if ("=" in arg): argVal = arg.split("=", 1)[1] if (arg == "--table"): reportTable = True elif (arg.startswith("--")): usage("unrecognized option: %s" % arg) else: usage("unrecognized option: %s" % arg) # read the tree forest = read_howde_tree_file(stdin, keepTags=True) assert (len(forest) != 0), "input has no tree" # compute the stats if (reportTable): print "#node\tbits\tall\tsome\tpresent\tabsent\tunresolved" sumPresent = sumAbsent = 0 for root in forest: preOrder = root.pre_order() for node in preOrder: assert (len(node.tags) == 3) node.bfSize = int(node.tags[0]) node.allOnes = int(node.tags[1]) node.someOnes = int(node.tags[2]) node.present = node.allOnes node.unresolved = node.someOnes parent = node.parent if (parent == None): node.absent = node.bfSize - (node.allOnes + node.someOnes) else: assert (node.bfSize == parent.bfSize) node.absent = parent.unresolved - (node.allOnes + node.someOnes) sumPresent += node.present sumAbsent += node.absent if (reportTable): print "%s\t%d\t%d\t%d\t%d\t%d\t%d" \ % (node.name, node.bfSize,node.allOnes,node.someOnes, node.present,node.absent,node.unresolved) denom = float(sumPresent + sumAbsent) output = "present/absent = %d/%d = %.1f%%/%.1f%%" \ % (sumPresent,sumAbsent,100*sumPresent/denom,100*sumAbsent/denom) if (reportTable): print >> stderr, output else: print output
def main(): global dc global tree, nameToNode, preOrder, depthToNames global debug # set drawing controls dc.lineThickness = 0.75 dc.branchThickness = 0.75 dc.lineColor = svg_color((0, 0, 0)) # (black) dc.leafFillColor = svg_color((.75, .90, .98)) # (light blue) dc.nodeFillColor = svg_color((.95, .95, .95)) # (light gray) dc.branchColor = svg_color((0, 0, 0)) # (black) dc.borderTop = 2 dc.borderLeft = 2 dc.nodeWidth = 160 dc.nodeHeight = 57 dc.nodeSepWidth = 5 dc.nodeSepHeight = 21 dc.nameFont = "Courier New" dc.nameFontSize = 11 (dc.nameCapsHgt, dc.nameDescHgt) = (16, 6) # (relative to 16 pt font) dc.nameFontLineHgt = 19.2 # (relative to 16 pt font) # parse the command line bitVectorTemplate = None svgFilename = "sbt_tree_to_svg.svg" debug = {} for arg in argv[1:]: if ("=" in arg): argVal = arg.split("=", 1)[1] if (arg.startswith("--bitvectors=")) or (arg.startswith("--bits=")): bitVectorTemplate = argVal if (not "{leaf}" in bitVectorTemplate): usage("(in %s) template fails to contain {leaf}" % arg) elif (arg.startswith("--node:width=")) or ( arg.startswith("--node:W=")): dc.nodeWidth = float(argVal) elif (arg.startswith("--node:height=")) or ( arg.startswith("--node:H=")): dc.nodeHeight = float(argVal) elif (arg.startswith("--node:sepwidth=")) or ( arg.startswith("--node:SW=")): dc.nodeSepWidth = float(argVal) elif (arg.startswith("--node:sepheight=")) or ( arg.startswith("--node:SH=")): dc.nodeSepHeight = float(argVal) elif (arg.startswith("--node:font=")) or (arg.startswith("--node:F=")): dc.nameFontSize = float(argVal) elif (arg.startswith("--out=")): svgFilename = argVal if (not svgFilename.endswith(".svg")): svgFilename = svgFilename + ".svg" elif (arg == "--debug"): debug["debug"] = True elif (arg.startswith("--debug=")): for name in argVal.split(","): debug[name] = True elif (arg.startswith("--")): usage("unrecognized option: %s" % arg) else: usage("unrecognized option: %s" % arg) # read the tree tree = read_howde_tree_file(stdin) nameToNode = tree.build_dict() preOrder = tree.pre_order() for name in nameToNode: node = nameToNode[name] if ((node.left == None) != (node.right == None)): if (node.left != None): assert ( False), "node \"%s\" has a left child but no right" % name else: assert ( False), "node \"%s\" has a right child but no left" % name depthToNames = {} for (depth, name) in preOrder: if (depth not in depthToNames): depthToNames[depth] = [] depthToNames[depth] += [name] if ("depth" in debug): for depth in depthToNames: print("depth %s: %s" % (depth, ",".join(depthToNames[depth])), file=stderr) if (bitVectorTemplate != None): read_leaf_bits(bitVectorTemplate) if (bitVectorTemplate != None): compute_all_some() # layout the tree layout_tree() # draw the tree svg = draw_tree() svg.save(svgFilename)
def main(): global dc, orientation, showNodesAs, showBitsAs, bitGroupSizes global tree, nameToNode, preOrder, depthToNames global debug # set drawing controls dc.lineThickness = 0.75 dc.branchThickness = 0.75 dc.bitOutlineThickness = 0.75 dc.lineColor = svg_color((0, 0, 0)) # (black) dc.leafFillColor = svg_color((.75, .90, .98)) # (light blue) dc.nodeFillColor = svg_color((.95, .95, .95)) # (light gray) dc.branchColor = svg_color((0, 0, 0)) # (black) dc.bitOutlineColor = svg_color((.5, .5, .5)) # (gray) dc.bit1Color = svg_color((0, 0, 0)) # (black) dc.bit0Color = svg_color((1, 1, 1)) # (white) dc.bitXColor = svg_color((.75, .75, .75)) # (gray) dc.borderTop = 2 dc.borderLeft = 2 dc.borderBottom = dc.borderTop dc.borderRight = dc.borderLeft dc.nodeWdt = 160 dc.nodeHgt = 57 dc.nodeSepWdt = 5 dc.nodeSepHgt = 21 dc.bitWdt = 10 dc.bitHgt = dc.bitWdt dc.bitGroupSepWdt = 3 dc.bitPrefixSkip = 1.4 * dc.bitWdt dc.nameFont = "Courier New" dc.nameFontSize = 11 (dc.nameCapsHgt, dc.nameDescHgt) = (16, 6) # (relative to 16 pt font) dc.nameFontLineHgt = 19.2 # (relative to 16 pt font) dc.nameFontCharWdt = 12.8 # (relative to 16 pt font) # parse the command line bitVectorTemplate = None svgFilename = "howde_tree_to_svg.svg" orientation = "T2B" bitGroupSizes = None showNodesAs = "union" showBitsAs = "squares" debug = {} for arg in argv[1:]: if ("=" in arg): argVal = arg.split("=", 1)[1] if (arg in ["--bitvectors", "--bits="]): bitVectorTemplate = "{leaf}" elif (arg.startswith("--bitvectors=")) or (arg.startswith("--bits=")): bitVectorTemplate = argVal if (not "{leaf}" in bitVectorTemplate): usage("(in %s) template fails to contain {leaf}" % arg) elif (arg in ["--group=none", "--chunk=none"]): bitGroupSizes = [] elif (arg.startswith("--group=")) or (arg.startswith("--chunk=")): bitGroupSizes = list(map(int, argVal.split(","))) elif (arg == "--nodes:union"): showNodesAs = "union" elif (arg == "--nodes:allsome"): showNodesAs = "all/some" elif (arg == "--nodes:determined") or (arg == "--nodes:det"): showNodesAs = "determined/how" elif (arg == "--nodes:determined,active") or (arg == "--nodes:det,active") \ or (arg == "--nodes:determined,informative") or (arg == "--nodes:det,informative"): showNodesAs = "determined/how/active" elif (arg == "--bits:squares") or (arg == "--bits:square"): showBitsAs = "squares" elif (arg == "--bits:binary"): showBitsAs = "text" elif (arg in ["--top-to-bottom", "--toptobottom", "--top2bottom"]): orientation = "T2B" elif (arg in ["--left-to-right", "--lefttoright", "--left2right"]): orientation = "L2R" elif (arg.startswith("--out=")): svgFilename = argVal if (not svgFilename.endswith(".svg")): svgFilename = svgFilename + ".svg" elif (arg.startswith("--node:width=")) or ( arg.startswith("--node:W=")): dc.nodeWdt = float(argVal) elif (arg.startswith("--node:height=")) or ( arg.startswith("--node:H=")): dc.nodeHgt = float(argVal) elif (arg.startswith("--node:sepwidth=")) or ( arg.startswith("--node:SW=")): dc.nodeSepWdt = float(argVal) elif (arg.startswith("--node:sepheight=")) or ( arg.startswith("--node:SH=")): dc.nodeSepHgt = float(argVal) elif (arg.startswith("--node:font=")) or (arg.startswith("--node:F=")): dc.nameFontSize = float(argVal) elif (arg == "--debug"): debug["debug"] = True elif (arg.startswith("--debug=")): for name in argVal.split(","): debug[name] = True elif (arg.startswith("--")): usage("unrecognized option: %s" % arg) else: usage("unrecognized option: %s" % arg) if (bitGroupSizes == None): bitGroupSizes = [5] elif (bitGroupSizes == []): bitGroupSizes = None if (bitGroupSizes != None) and (len(bitGroupSizes) > 1): usage("multiple groups aren't implemented yet (sorry)") # adjust the layout parameters if (dc.nameFontSize < 16): scale = dc.nameFontSize / 16.0 dc.nameCapsHgt *= scale dc.nameDescHgt *= scale dc.nameFontLineHgt *= scale dc.nameFontCharWdt *= scale if (orientation == "L2R"): (dc.nodeSepWdt, dc.nodeSepHgt) = (dc.nodeSepHgt, dc.nodeSepWdt) # read the tree forest = read_howde_tree_file(stdin, debug="treeparse" in debug) assert (len(forest) != 0), "input has no tree" assert (len(forest) == 1), "input is a forest (not supported yet)" tree = forest[0] tree.compute_depth() nameToNode = tree.build_dict() preOrder = tree.pre_order() for name in nameToNode: node = nameToNode[name] if (len(node.children) == 1): assert (False), "node \"%s\" has exactly one child" % name depthToNames = {} for node in preOrder: if (node.depth not in depthToNames): depthToNames[node.depth] = [] depthToNames[node.depth] += [node.name] if ("depth" in debug): for depth in depthToNames: print("depth %s: %s" % (depth, ",".join(depthToNames[depth])), file=stderr) if (bitVectorTemplate != None): read_leaf_bits(bitVectorTemplate) node = find_a_leaf() dc.nodeWdt = drawn_bits_width(node.numBits) + 3 if (bitVectorTemplate != None): if (showNodesAs == "all/some"): compute_all_some() dc.nodeHgt = 5 * dc.nameFontLineHgt + 3 elif (showNodesAs == "determined/how"): compute_determined_how(withActive=False) dc.nodeHgt = 6 * dc.nameFontLineHgt + 3 elif (showNodesAs == "determined/how/active"): compute_determined_how(withActive=True) dc.nodeHgt = 6 * dc.nameFontLineHgt + 3 else: # if (showNodesAs == "union"): compute_union() dc.nodeHgt = 3 * dc.nameFontLineHgt + 3 # layout the tree (width, height) = layout_tree() # draw the tree svg = draw_tree(width, height) svg.save(svgFilename)