Returns a TermDocMatrix which is identical to self except the metadata values are now identical to the
         categories present and term-doc-matrix is now the metadata matrix.

        :return: TermDocMatrix
779439
def use_categories_as_metadata_and_replace_terms(self):
        '''
        
        '''
        new_metadata_factory = CSRMatrixFactory()
        for i, category_idx in enumerate(self.get_category_ids()):
            new_metadata_factory[i, category_idx] = 1
        new_metadata = new_metadata_factory.get_csr_matrix()
        new_tdm = self._make_new_term_doc_matrix(self._mX,
                                                 new_metadata,
                                                 self._y,
                                                 self._metadata_idx_store,
                                                 self._category_idx_store,
                                                 copy(self._category_idx_store),
                                                 self._y == self._y)
        return new_tdm
473071
def compact(self, term_doc_matrix):
		'''
		
		'''
		domain_mat = CombineDocsIntoDomains(term_doc_matrix).get_new_term_doc_mat(self.doc_domains)
		domain_count = (domain_mat > 0).sum(axis=0)
		valid_term_mask = (self.max_domain_count >= domain_count) \
		                  & (domain_count >= self.min_domain_count)
		indices_to_compact = np.arange(term_doc_matrix.get_num_terms())[~valid_term_mask.A1]
		return term_doc_matrix.remove_terms_by_indices(indices_to_compact)
682616
def term_matrix(idlist, subject_category, taxon, **kwargs):
    
    results = search_associations(objects=idlist,
                                  subject_taxon=taxon,
                                  subject_category=subject_category,
                                  select_fields=[M.SUBJECT, M.OBJECT_CLOSURE],
                                  facet_fields=[],
                                  rows=-1,
                                  include_raw=True,
                                  **kwargs)
    docs = results['raw'].docs

    subjects_per_term = {}
    smap = {}
    for d in docs:
        smap[d[M.SUBJECT]] = 1
        for c in d[M.OBJECT_CLOSURE]:
            if c in idlist:
                if c not in subjects_per_term:
                    subjects_per_term[c] = []
                subjects_per_term[c].append(d[M.SUBJECT])
    pop_n = len(smap.keys())

    cells = []
    for cx in idlist:
        csubjs = set(subjects_per_term[cx])
        for dx in idlist:
            dsubjs = set(subjects_per_term[dx])
            a = len(csubjs.intersection(dsubjs))
            b = len(csubjs) - a
            c = len(dsubjs) - a
            d = pop_n - len(dsubjs) - b
            ctable = [[a, b], [c, d]]

            _, p_under = sp.stats.fisher_exact(ctable, 'less')
            _, p_over = sp.stats.fisher_exact(ctable, 'greater')

            cells.append({'c':cx, 'd':dx,
                          'nc':len(csubjs),
                          'nd':len(dsubjs),
                          'n':a,
                          'p_l':p_under,
                          'p_g':p_over
            })
    return cells
737133
def _normalizeImpurityMatrix(matrix):
    
    newMatrix = list()
    for line in matrix:
        total = sum(line)
        if total != 0:
            newMatrix.append([i / total for i in line])
        else:
            newMatrix.append(line)
    return newMatrix
351242
def _build_from_category_spacy_doc_iter(self, category_doc_iter):
        '''
        
        '''
        term_idx_store = IndexStore()
        category_idx_store = IndexStore()
        metadata_idx_store = IndexStore()
        X, mX, y = self._get_features_and_labels_from_documents_and_indexes \
            (category_doc_iter,
             category_idx_store,
             term_idx_store,
             metadata_idx_store)
        return TermDocMatrix(X,
                             mX,
                             y,
                             term_idx_store=term_idx_store,
                             category_idx_store=category_idx_store,
                             metadata_idx_store=metadata_idx_store)
289174
def _terms(self):
        
        res = []
        for sign, terms in self.terms.items():
            for ID, lon in terms.items():
                res.append(self.T(ID, sign))
        return res
273006
function getTotalMatrix() {
    var m = this._cfg.totalMatrix;
    if (!m) {
      m = [1, 0, 0, 0, 1, 0, 0, 0, 1];
      var parent = this._cfg.parent;
      if (parent) {
        var pm = parent.getTotalMatrix();
        multiple(m, pm);
      }

      multiple(m, this.attr('matrix'));
      this._cfg.totalMatrix = m;
    }
    return m;
  }
123679
def add_doc_names_as_metadata(self, doc_names):
        '''
        
        '''
        if len(doc_names) != self.get_num_docs():
            raise Exception("The parameter doc_names contains %s elements. "
                            "It should have %s elements, one per document." % (len(doc_names), self.get_num_docs()))

        doc_names_counter = collections.Counter(np.array(doc_names))
        metafact = CSRMatrixFactory()
        metaidxstore = IndexStore()
        doc_id_uses = collections.Counter()
        for i in range(self.get_num_docs()):
            doc_id = doc_names[i]
            if doc_names_counter[doc_id] > 1:
                doc_id_uses[doc_id] += 1
                doc_name_idx = metaidxstore.getidx('%s (%s)' % (doc_id, doc_id_uses[doc_id]))
            else:
                doc_name_idx = metaidxstore.getidx(doc_id)
            metafact[i, i] = doc_name_idx
        return self.add_metadata(metafact.get_csr_matrix(), metaidxstore)
1246469
def get_matrix(self):
        
        matrix = Matrix()
        cairo.cairo_pattern_get_matrix(self._pointer, matrix._pointer)
        self._check_status()
        return matrix
447971
def get_doc_indices(self):
		'''
		
		'''
		if self._document_category_df is None:
			return pd.np.array([])
		categories_d = {d: i for i, d in enumerate(self.get_categories())}
		return self._document_category_df.category.apply(categories_d.get).values
1131997
def get_rubric_metadata(self):
        
        # Implemented from template for osid.resource.ResourceForm.get_group_metadata_template
        metadata = dict(self._mdata['rubric'])
        metadata.update({'existing_id_values': self._my_map['rubricId']})
        return Metadata(**metadata)
473852
def similarity_matrix(self, x_subjects=None, y_subjects=None, symmetric=False):
        
        if x_subjects is None:
            x_subjects = []
        if y_subjects is None:
            y_subjects = []

        xset = set(x_subjects)
        yset = set(y_subjects)
        zset = xset.union(yset)

        # first built map of gene->termClosure.
        # this could be calculated ahead of time for all g,
        # but this may be space-expensive. TODO: benchmark
        gmap={}
        for z in zset:
            gmap[z] = self.inferred_types(z)
        ilist = []
        for x in x_subjects:
            for y in y_subjects:
                if not symmetric or x<y:
                    shared = gmap[x].intersection(gmap[y])
                    union = gmap[x].union(gmap[y])
                    j = 0
                    if len(union)>0:
                        j = len(shared) / len(union)
                    ilist.append({'x':x,'y':y,'shared':shared, 'c':len(shared), 'j':j})
        return self.intersectionlist_to_matrix(ilist, x_subjects, y_subjects)
1151768
def _set_sitematrix(self):
        
        data = self._load_response('sitematrix')

        self.params.update({'title': self.COMMONS})

        matrix = data.get('sitematrix')
        if matrix:
            self.data['sites'] = self._sitelist(matrix)
            self.data['random'] = random.choice(self.data['sites'])
377988
def _q_matrix(self):
        
        return np.array([
            [self.q[0], -self.q[1], -self.q[2], -self.q[3]],
            [self.q[1],  self.q[0], -self.q[3],  self.q[2]],
            [self.q[2],  self.q[3],  self.q[0], -self.q[1]],
            [self.q[3], -self.q[2],  self.q[1],  self.q[0]]])
645563
def sample_correlations(self):
        
        C = np.corrcoef(self.X.T)
        corr_matrix = ExpMatrix(genes=self.samples, samples=self.samples, X=C)
        return corr_matrix
239100
def U(self):
        
        u = self.call("U")
        if u is not None:
            mat_name = u.getClass().getSimpleName()
            if mat_name == "RowMatrix":
                return RowMatrix(u)
            elif mat_name == "IndexedRowMatrix":
                return IndexedRowMatrix(u)
            else:
                raise TypeError("Expected RowMatrix/IndexedRowMatrix got %s" % mat_name)
1013993
def _param_to_matrix(self):
        
        for item in self._params:
            self.__dict__[item] = matrix(self.__dict__[item], tc='d')