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()
#!/usr/bin/env python-i # use summatrix to visualize a dense matrix # # load summon matrix library import summon from summon import matrix, util # read in matrix (stored in Index Format 'imat') mat = matrix.Matrix() matrix.open_matrix("data.mat", mat, format="dmat") # set colormap mat.colormap = summon.PosNegColorMap() # read row and column labels mat.rowlabels = util.read_strings("data.rlabels") mat.collabels = util.read_strings("data.clabels") # create and show matrix viewer viewer = matrix.MatrixViewer(mat, title="unclustered data", show_labels=True, bgcolor=(1,1,1), style="quads", winsize=(400, 600)) viewer.show()