Example #1
0
    def setup(self, nrows=None, ncols=None, nnz=None,
              rowsample=False, colsample=False):
        # set dimensions
        if nrows is not None:
            self.nrows = nrows
        if ncols is not None:
            self.ncols = ncols
        if nnz is not None:
            self.nnz = nnz

        # set permutations
        if self.rperm is None:
            self.rperm = range(self.nrows)
        if self.cperm is None:
            self.cperm = range(self.ncols)

        # set inverse permutations
        self.rinv = util.invperm(self.rperm)
        self.cinv = util.invperm(self.cperm)
    
        # setup row/col sampling
        if rowsample is not False:
            self.rshow = set(random.sample(range(self.nrows), 
                                           int(self.nrows * rowsample)))
        else:
            self.rshow = set(range(self.nrows))

        if colsample is not False:
            self.cshow = random.sample(range(self.ncols),
                                       int(self.ncols * colsample))
        else:
            self.cshow = set(range(self.ncols))
Example #2
0
def set_perm_from_part(mat, rows=True, cols=True):
    
    # setup default permutation from cluster ids
    if rows and mat.rpart:
        mat.rperm = part2perm(mat.rpart)
        mat.rinv = util.invperm(mat.rperm)
    if cols and mat.cpart:
        mat.cperm = part2perm(mat.cpart)
        mat.cinv = util.invperm(mat.cperm)
Example #3
0
    def __init__(self, mat=None, on_click=None, 
                 bgcolor=(0,0,0), drawzeros=False, style="points",
                 show_labels=False, show_label_windows=False,
                 winsize=(400,400), title="summatrix",
                 rtree=None, ctree=None,
                 use_tree_lens=(False, False),
                 show_tree_windows=None):
        self.win = None
        self.mat = mat
        self.bgcolor = bgcolor
        self.drawzeros = drawzeros
        self.style = style
        self.winsize = winsize[:]
        self.title = title
        self.part_lines = None
        self.part_lines_visible = True
        self.first_open = True
        if on_click != None:
            self.on_click = on_click

        # labels
        self.show_labels = show_labels
        self.show_label_windows = show_label_windows        
        self.label_windows = [None, None]

        # trees
        if show_tree_windows != False and (rtree != None or ctree != None):
            self.show_tree_windows = True
        else:
            self.show_tree_windows = False
        self.tree_windows = [None, None]
        self.rtree = rtree
        self.ctree = ctree
        self.use_tree_lens = use_tree_lens

        # setup perm based on trees
        if self.rtree:
            leaves = self.rtree.leaf_names()
            if self.mat.rowlabels == None:
                self.mat.rperm = map(int, leaves)
            else:
                lookup = util.list2lookup(self.mat.rowlabels)
                self.mat.rperm = util.mget(lookup, leaves)

        if self.ctree:
            leaves = self.ctree.leaf_names()
            if self.mat.collabels == None:
                self.mat.cperm = map(int, leaves)
            else:
                lookup = util.list2lookup(self.mat.collabels)
                self.mat.cperm = util.mget(lookup, leaves)

            
        # set inverse permutations
        self.mat.rinv = util.invperm(self.mat.rperm)
        self.mat.cinv = util.invperm(self.mat.cperm)
Example #4
0
    def submatrix(self, rows=None, cols=None):
        """Returns a submatrix"""
        
        mat = Matrix()
        
        if rows == None:
            rows = range(mat.nrows)
        
        if cols == None:
            cols = range(mat.ncols)
                
        lookuprows = util.list2lookup(rows)
        lookupcols = util.list2lookup(cols)
        
        # get subset of data
        rows2, cols2, vals2 = self.rows, self.cols, self.vals
        rows3, cols3, vals3 = mat.rows, mat.cols, mat.vals
                
        for i in xrange(len(rows2)):            
            r = rows2[i]
            c = cols2[i]
            v = vals2[i]
            
            if r not in lookuprows or c not in lookupcols:
                continue

            r2 = lookuprows[r]
            c2 = lookupcols[r]
            rows3.append(r2)
            cols3.append(c2)
            vals3.append(v)
            mat[r2][c2] = v
        
        # get subset of permutation        
        ind = [self.rinv[i] for i in rows]
        lookup = util.list2lookup(util.sort(ind))
        mat.rinv = util.mget(lookup, ind)
        mat.rperm = util.invperm(mat.rinv)
        
        ind = [self.cinv[i] for i in cols]
        lookup = util.list2lookup(util.sort(ind))
        mat.cinv = util.mget(lookup, ind)
        mat.cperm = util.invperm(mat.cinv)
        
        # get subset of partition
        if mat.rpart != None:
            mat.rpart = util.mget(self.rpart, rows)
        if mat.cpart != None:
            mat.cpart = util.mget(self.cpart, cols)
        
        mat.setup(len(rows), len(cols), len(rows3))
        return mat