def process_lmat(infile, mat, filename=None): """Processes an lmat so that it returns as an imat""" if filename: util.tic("reading '%s'" % filename) rows, cols, vals = matrixlib.transpose(list(matrixlib.iter_lmat(infile))) # determine labels rowlabels = util.unique(rows) collabels = util.unique(cols) nrows = len(rowlabels) ncols = len(collabels) nnz = len(vals) # determine order if mat.order is not None: order = util.read_strings(mat.order) rowlookup = util.list2lookup(order) collookup = util.list2lookup(order) rowlabels.sort(key=lambda x: rowlookup[x]) collabels.sort(key=lambda x: collookup[x]) else: rowlookup = util.list2lookup(rowlabels) collookup = util.list2lookup(collabels) mat.rowlabels = rowlabels mat.collabels = collabels # iterate with an imat, then post process def func(): ilmat = itertools.izip(rows, cols, vals) imat = matrixlib.ilmat2imat(ilmat, rowlabels, collabels) for entry in imat: yield entry # also store entries by label for i, j, v in itertools.izip(mat.rows, mat.cols, mat.vals): mat[rowlabels[i]][collabels[j]] = v if filename: util.toc() return nrows, ncols, nnz, func()
def load_matrix(nrows, ncols, nnz, imat, mat, loadvals=False, minval=-util.INF, sample=False, rowsample=False, colsample=False, filename=None): """Load matrix from an index matrix iterator""" mat.setup(nrows, ncols, nnz, rowsample=rowsample, colsample=colsample) rows, cols, vals = (mat.rows, mat.cols, mat.vals) # clear matrix rows[:] = [] cols[:] = [] vals[:] = [] if filename: util.tic("reading '%s'" % filename) util.log("%s: %d nrows, %d ncols, %d non-zeros" % (filename, nrows, ncols, nnz)) try: for i, j, v in imat: # filtering: 1. random sample sample # 2. row/col filtering # 3. value cutoff if (sample and random.random() > sample) or \ i not in mat.rshow or j not in mat.cshow or \ v < minval: continue rows.append(i) cols.append(j) vals.append(v) if loadvals: mat[i][j] = v except Exception, e: if filename: util.toc() raise e
#!/usr/bin/env python-i # a very large hierarchical clustering of all human and dog genes # print """ NOTE: use Ctrl+right drag and Shift+right drag to zoom the x- and y-axis independently """ # import necessary libraries from summon import sumtree, util, treelib # read tree in parent tree format util.tic("read tree") tree = treelib.Tree() tree.read_parent_tree("dog-human-genes.ptree", "dog-human-genes.label") util.toc() # read colormap colormap = treelib.read_tree_color_map("dog-human.colormap") # Provide a customization of the node labels. # To view node labels, press "L" def nodelabel(node): if isinstance(node.name, str): if node.name.startswith("ENS"): return "" return node.name
win = summon.Window("11_fractal") def fractal(depth, offset): if depth == 0: return group() branches = 5 shrink = .38 step = 2 * pi / 10 c = 1 - depth / 7.0 c2 = 1 - (depth - 1) / 7.0 vis = [] for i in xrange(branches): angle = i * 2 * pi / branches + offset vis.append(group( lines(color(1, c, 0), 0,0, color(1, c2, 0), cos(angle), sin(angle)), translate(cos(angle), sin(angle), scale(shrink, shrink, fractal(depth - 1, offset + step))))) return group(*vis) util.tic("draw") win.add_group(fractal(6, 0)) win.home() util.toc()