def get_rank_based_enrichment(<EOL>self,<EOL>ranked_genes: List[str],<EOL>pval_thresh: float = <NUM_LIT>,<EOL>X_frac: float = <NUM_LIT>,<EOL>X_min: int = <NUM_LIT:5>,<EOL>L: int = None,<EOL>adjust_pval_thresh: bool = True,<EOL>escore_pval_thresh: float = None,<EOL>exact_pval: str = '<STR_LIT>',<EOL>gene_set_ids: List[str] = None,<EOL>table: np.ndarray = None) -> RankBasedGSEResult:
<EOL>X_frac = float(X_frac)<EOL>if table is not None:<EOL><INDENT>if not np.issubdtype(table.dtype, np.longdouble):<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>if L is None:<EOL><INDENT>L = int(len(ranked_genes)/<NUM_LIT>)<EOL><DEDENT>gene_set_coll = self._gene_set_coll<EOL>gene_memberships = self._gene_memberships<EOL>if escore_pval_thresh is None:<EOL><INDENT>logger.warning('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if gene_set_ids is not None:<EOL><INDENT>gs_indices = np.int64([self._gene_set_coll.index(id_)<EOL>for id_ in gene_set_ids])<EOL>gene_sets = [gene_set_coll[id_] for id_ in gene_set_ids]<EOL>gene_set_coll = GeneSetCollection(gene_sets)<EOL>gene_memberships = gene_memberships[:, gs_indices]  <EOL><DEDENT>unknown = <NUM_LIT:0><EOL>L_adj = L<EOL>sel = []<EOL>filtered_genes = []<EOL>logger.debug('<STR_LIT>' % len(ranked_genes))<EOL>for i, g in enumerate(ranked_genes):<EOL><INDENT>try:<EOL><INDENT>idx = self._gene_indices[g]<EOL><DEDENT>except KeyError:<EOL><INDENT>unknown += <NUM_LIT:1><EOL>if i < L:<EOL><INDENT>L_adj -= <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>sel.append(idx)<EOL>filtered_genes.append(g)<EOL><DEDENT><DEDENT>sel = np.int64(sel)<EOL>logger.debug('<STR_LIT>', L_adj)<EOL>gene_memberships = gene_memberships[sel, :]<EOL>N, m = gene_memberships.shape<EOL>if unknown > <NUM_LIT:0>:<EOL><INDENT>logger.warn('<STR_LIT>',<EOL>unknown, len(ranked_genes),<EOL><NUM_LIT:100> * (unknown / float(len(ranked_genes))))<EOL><DEDENT>k_above_L = np.sum(gene_memberships[:L_adj, :], axis=<NUM_LIT:0>, dtype=np.int64)<EOL>k_below_L = np.sum(gene_memberships[L_adj:, :], axis=<NUM_LIT:0>, dtype=np.int64)<EOL>K_vec = k_above_L + k_below_L<EOL>K_max = np.amax(K_vec)<EOL>X = np.amax(<EOL>np.c_[np.tile(X_min, m), np.int64(np.ceil(X_frac * K_vec))],<EOL>axis=<NUM_LIT:1>)<EOL>num_tests = np.sum(K_vec-X >= <NUM_LIT:0>)<EOL>logger.info('<STR_LIT>', num_tests)<EOL>final_pval_thresh = pval_thresh<EOL>if adjust_pval_thresh and num_tests > <NUM_LIT:0>:<EOL><INDENT>final_pval_thresh /= float(num_tests)<EOL>logger.info('<STR_LIT>',<EOL>final_pval_thresh)<EOL><DEDENT>if escore_pval_thresh is None:<EOL><INDENT>escore_pval_thresh = final_pval_thresh<EOL><DEDENT>elif escore_pval_thresh < final_pval_thresh:<EOL><INDENT>logger.warning('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>escore_pval_thresh = final_pval_thresh<EOL><DEDENT>if table is None:<EOL><INDENT>table = np.empty((K_max+<NUM_LIT:1>, N+<NUM_LIT:1>), dtype=np.longdouble)<EOL><DEDENT>else:<EOL><INDENT>if table.shape[<NUM_LIT:0>] < K_max+<NUM_LIT:1> or table.shape[<NUM_LIT:1>] < N+<NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>% (table.shape[<NUM_LIT:0>], table.shape[<NUM_LIT:1>], K_max+<NUM_LIT:1>, N+<NUM_LIT:1>))<EOL><DEDENT><DEDENT>logger.debug('<STR_LIT>',<EOL>len(ranked_genes), X_frac, X_min, L, K_max)<EOL>enriched = []<EOL>num_tests = <NUM_LIT:0>  <EOL>for j in range(m):<EOL><INDENT>X = max(X_min, int(ceil(X_frac * float(K_vec[j]))))<EOL>if K_vec[j] >= X:<EOL><INDENT>num_tests += <NUM_LIT:1><EOL>if k_above_L[j] >= X:<EOL><INDENT>indices = np.uint16(np.nonzero(gene_memberships[:, j])[<NUM_LIT:0>])<EOL>res = xlmhg.get_xlmhg_test_result(<EOL>N, indices, X, L, pval_thresh=final_pval_thresh,<EOL>escore_pval_thresh=escore_pval_thresh,<EOL>exact_pval=exact_pval, table=table)<EOL>if res.pval <= final_pval_thresh:<EOL><INDENT>ind_genes = [ranked_genes[i] for i in indices]<EOL>gse_result = RankBasedGSEResult(<EOL>gene_set_coll[j], N, indices, ind_genes,<EOL>X, L, res.stat, res.cutoff, res.pval,<EOL>escore_pval_thresh=escore_pval_thresh<EOL>)<EOL>enriched.append(gse_result)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>q = len(enriched)<EOL>ignored = m - num_tests<EOL>if ignored > <NUM_LIT:0>:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>ignored, m, <NUM_LIT:100> * (ignored / float(m)))<EOL><DEDENT>logger.info('<STR_LIT>'<EOL>'<STR_LIT>', q, m, final_pval_thresh)<EOL>return enriched<EOL>
Test for gene set enrichment at the top of a ranked list of genes.

        This function uses the XL-mHG test to identify enriched gene sets.

        This function also calculates XL-mHG E-scores for the enriched gene
        sets, using ``escore_pval_thresh`` as the p-value threshold "psi".

        Parameters
        ----------
        ranked_gene_ids : list of str
            The ranked list of gene IDs.
        pval_thresh : float, optional
            The p-value threshold used to determine significance.
            See also ``adjust_pval_thresh``. [0.05]
        X_frac : float, optional
            The min. fraction of genes from a gene set required for enrichment. [0.25]
        X_min : int, optional
            The min. no. of genes from a gene set required for enrichment. [5]
        L : int, optional
            The lowest cutoff to test for enrichment. If ``None``,
            int(0.25*(no. of genes)) will be used. [None]
        adjust_pval_thresh : bool, optional
            Whether to adjust the p-value thershold for multiple testing,
            using the Bonferroni method. [True]
        escore_pval_thresh : float or None, optional
            The "psi" p-value threshold used in calculating E-scores. If
            ``None``, will be set to p-value threshold. [None]
        exact_pval : str
            Choices are: "always", "if_significant", "if_necessary". Parameter
            will be passed to `xlmhg.get_xlmhg_test_result`. ["always"]
        gene_set_ids : list of str or None, optional
            A list of gene set IDs to specify which gene sets should be tested for enrichment. If ``None``, all gene sets will be tested. [None]
        table : 2-dim numpy.ndarray of type numpy.longdouble or None, optional
            The dynamic programming table used by the algorithm for calculating XL-mHG p-values. Passing this avoids memory re-allocation when calling this function repetitively. [None]

        Returns
        -------
        list of `RankBasedGSEResult`
            A list of all significantly enriched gene sets.