def getBranchNames(self, lines=None): """Get numbering of ancestral nodes""" lines = self.setupLines(lines) for line in lines: if line.startswith("(1) Branch lengths and substitution pattern"): break else: raise Exception("no branch names found") branches = lines.next().strip().split() names = [branch.split("..") for branch in branches] dists = map(float, lines.next().strip().split()) # add nodes to tree tree = treelib.Tree() for name in set(util.flatten(names)): tree.add(treelib.TreeNode(name)) # link up nodes for (top, bot), dist in zip(names, dists): tree.add_child(tree.nodes[top], tree.nodes[bot]) tree.nodes[bot].dist = dist # find root for node in tree: if node.parent is None: tree.root = node break return tree
def confusionMatrix(parts1, parts2): """ Returns a confusion matrix of two different partitions of the same items. """ confuse = util.Dict(dim=2, default=0) lookup1 = item2part(parts1) lookup2 = item2part(parts2) items1 = set(util.flatten(parts1, 1)) items2 = set(util.flatten(parts2, 1)) sameset = items1 & items2 diffset = items1.symmetric_difference(items2) for item in sameset: confuse[lookup1[item]][lookup2[item]] += 1 return confuse, list(diffset)
def confusionMatrix(parts1, parts2): """Returns a confusion matrix of two different partitions of the same items""" confuse = util.Dict(dim=2, default=0) lookup1 = item2part(parts1) lookup2 = item2part(parts2) items1 = set(util.flatten(parts1, 1)) items2 = set(util.flatten(parts2, 1)) sameset = items1 & items2 diffset = items1.symmetric_difference(items2) for item in sameset: confuse[lookup1[item]][lookup2[item]] += 1 return confuse, list(diffset)
def heatmap(matrix, width=20, height=20, colormap=None, filename=None, rlabels=None, clabels=None, display=True, xdir=1, ydir=1, xmargin=0, ymargin=0, labelPadding=2, labelSpacing=4, mincutoff=None, maxcutoff=None, showVals=False, formatVals=str, valColor=black, clabelsAngle=270, clabelsPadding=None, rlabelsAngle=0, rlabelsPadding=None, colors=None, strokeColors=None, valAnchor="start", close=True): from rasmus import util if display and (not close): raise Exception("must close file if display is used") # determine filename if filename is None: filename = util.tempfile(".", "heatmap", ".svg") temp = True else: temp = False # determine colormap if colors is None: if colormap is None: colormap = rainbowColorMap(util.flatten(matrix)) # determine matrix size and orientation nrows = len(matrix) ncols = len(matrix[0]) if xdir == 1: xstart = xmargin ranchor = "end" coffset = width elif xdir == -1: xstart = xmargin + ncols * width ranchor = "start" coffset = 0 else: raise Exception("xdir must be 1 or -1") if ydir == 1: ystart = ymargin roffset = height canchor = "start" elif ydir == -1: ystart = ymargin + nrows * width roffset = 0 canchor = "end" else: raise Exception("ydir must be 1 or -1") # begin svg infile = util.open_stream(filename, "w") s = svg.Svg(infile) s.beginSvg(ncols * width + 2 * xmargin, nrows * height + 2 * ymargin) # draw matrix for i in xrange(nrows): for j in xrange(ncols): if mincutoff and matrix[i][j] < mincutoff: continue if maxcutoff and matrix[i][j] > maxcutoff: continue if colors: color = colors[i][j] else: color = colormap.get(matrix[i][j]) if strokeColors: strokeColor = strokeColors[i][j] else: strokeColor = color s.rect(xstart + xdir * j * width, ystart + ydir * i * height, xdir * width, ydir * height, strokeColor, color) # draw values if showVals: # find text size fontwidth = 7 / 11.0 textsize = [] for i in xrange(nrows): for j in xrange(ncols): if mincutoff and matrix[i][j] < mincutoff: continue if maxcutoff and matrix[i][j] > maxcutoff: continue strval = formatVals(matrix[i][j]) if len(strval) > 0: textsize.append( min(height, width / (float(len(strval)) * fontwidth))) textsize = min(textsize) if valAnchor == "start": xoffset = 0 elif valAnchor == "middle": xoffset = 0.5 elif valAnchor == "end": xoffset = 1 else: raise Exception("anchor not supported: %s" % valAnchor) yoffset = int(ydir == -1) for i in xrange(nrows): for j in xrange(ncols): if mincutoff and matrix[i][j] < mincutoff: continue if maxcutoff and matrix[i][j] > maxcutoff: continue strval = formatVals(matrix[i][j]) s.text(strval, xstart + xdir * (j + xoffset) * width, ystart + ydir * (i + yoffset) * height + height / 2.0 + textsize / 2.0, textsize, fillColor=valColor, anchor=valAnchor) # draw labels if rlabels is not None: assert len(rlabels) == nrows, \ "number of row labels does not equal number of rows" if rlabelsPadding is None: rlabelsPadding = labelPadding for i in xrange(nrows): x = xstart - xdir * rlabelsPadding y = ystart + roffset + ydir * i * height - labelSpacing / 2. s.text(rlabels[i], x, y, height - labelSpacing, anchor=ranchor, angle=rlabelsAngle) if clabels is not None: assert len(clabels) == ncols, \ "number of col labels does not equal number of cols" if clabelsPadding is None: clabelsPadding = labelPadding for j in xrange(ncols): x = xstart + coffset + xdir * j * width - labelSpacing / 2. y = ystart - ydir * clabelsPadding s.text(clabels[j], x, y, width - labelSpacing, anchor=canchor, angle=clabelsAngle) # end svg if close: s.endSvg() s.close() # display matrix if display: #if temp: os.system("display %s" % filename) #else: # os.spawnl(os.P_NOWAIT, "display", "display", filename) # clean up temp files if temp: os.remove(filename) return s
def heatmap(matrix, width=20, height=20, colormap=None, filename=None, rlabels=None, clabels=None, display=True, xdir=1, ydir=1, xmargin=0, ymargin=0, labelPadding=2, labelSpacing=4, mincutoff=None, maxcutoff=None, showVals=False, formatVals=str, valColor=black, clabelsAngle=270, clabelsPadding=None, rlabelsAngle=0, rlabelsPadding=None): from rasmus import util # determine filename if filename == None: filename = util.tempfile(".", "heatmap", ".svg") temp = True else: temp = False # determine colormap if colormap == None: colormap = rainbowColorMap(util.flatten(matrix)) # determine matrix size and orientation nrows = len(matrix) ncols = len(matrix[0]) if xdir == 1: xstart = xmargin ranchor = "end" coffset = width elif xdir == -1: xstart = xmargin + ncols * width ranchor = "start" coffset = 0 else: raise Exception("xdir must be 1 or -1") if ydir == 1: ystart = ymargin roffset = height canchor = "start" elif ydir == -1: ystart = ymargin + nrows * width roffset = 0 canchor = "end" else: raise Exception("ydir must be 1 or -1") # begin svg infile = util.open_stream(filename, "w") s = svg.Svg(infile) s.beginSvg(ncols*width + 2*xmargin, nrows*height + 2*ymargin) # draw matrix for i in xrange(nrows): for j in xrange(ncols): if mincutoff and matrix[i][j] < mincutoff: continue if maxcutoff and matrix[i][j] > maxcutoff: continue color = colormap.get(matrix[i][j]) s.rect(xstart + xdir*j*width, ystart + ydir*i*height, xdir*width, ydir*height, color, color) # draw values if showVals: # find text size fontwidth = 7/11.0 textsize = [] for i in xrange(nrows): for j in xrange(ncols): if mincutoff and matrix[i][j] < mincutoff: continue if maxcutoff and matrix[i][j] > maxcutoff: continue strval = formatVals(matrix[i][j]) if len(strval) > 0: textsize.append(min(height, width/(float(len(strval)) * fontwidth))) textsize = min(textsize) yoffset = int(ydir == -1) for i in xrange(nrows): for j in xrange(ncols): if mincutoff and matrix[i][j] < mincutoff: continue if maxcutoff and matrix[i][j] > maxcutoff: continue strval = formatVals(matrix[i][j]) s.text(strval, xstart + xdir*j*width, ystart + ydir*(i+yoffset)*height + height/2.0 + textsize/2.0, textsize, fillColor=valColor) # draw labels if rlabels != None: assert len(rlabels) == nrows, \ "number of row labels does not equal number of rows" if rlabelsPadding is None: rlabelsPadding = labelPadding for i in xrange(nrows): x = xstart - xdir*rlabelsPadding y = ystart + roffset + ydir*i*height - labelSpacing/2. s.text(rlabels[i], x, y, height-labelSpacing, anchor=ranchor, angle=rlabelsAngle) if clabels != None: assert len(clabels) == ncols, \ "number of col labels does not equal number of cols" if clabelsPadding is None: clabelsPadding = labelPadding for j in xrange(ncols): x = xstart + coffset + xdir*j*width - labelSpacing/2. y = ystart - ydir*clabelsPadding s.text(clabels[j], x, y, width-labelSpacing, anchor=canchor, angle=clabelsAngle) # end svg s.endSvg() s.close() # display matrix if display: #if temp: os.system("display %s" % filename) #else: # os.spawnl(os.P_NOWAIT, "display", "display", filename) # clean up temp files if temp: os.remove(filename)