|
| 1 | +import numba as nb |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +@nb.njit(cache=True) |
| 6 | +def _fdr_bh_single_row(ps_row, m): |
| 7 | + """Apply Benjamini-Hochberg correction to a single row.""" |
| 8 | + # Sort the row and get indices |
| 9 | + order = np.argsort(ps_row) |
| 10 | + ps_sorted = ps_row[order] |
| 11 | + |
| 12 | + # BH scale: p_(i) * m / i |
| 13 | + ps_bh = np.empty_like(ps_sorted, dtype=np.float64) |
| 14 | + for i in range(m): |
| 15 | + ps_bh[i] = ps_sorted[i] * (m / (i + 1)) |
| 16 | + |
| 17 | + # Reverse cumulative min |
| 18 | + ps_rev = np.empty_like(ps_bh, dtype=np.float64) |
| 19 | + for i in range(m): |
| 20 | + ps_rev[i] = ps_bh[m - 1 - i] |
| 21 | + |
| 22 | + for j in range(1, m): |
| 23 | + ps_rev[j] = min(ps_rev[j], ps_rev[j - 1]) |
| 24 | + |
| 25 | + # Reverse back |
| 26 | + ps_monotone = np.empty_like(ps_rev, dtype=np.float64) |
| 27 | + for i in range(m): |
| 28 | + ps_monotone[i] = ps_rev[m - 1 - i] |
| 29 | + |
| 30 | + # Unsort back to original order |
| 31 | + ps_adj = np.empty_like(ps_monotone, dtype=np.float64) |
| 32 | + for i in range(m): |
| 33 | + ps_adj[order[i]] = ps_monotone[i] |
| 34 | + |
| 35 | + # Clip to [0, 1] |
| 36 | + for i in range(m): |
| 37 | + ps_adj[i] = max(0.0, min(1.0, ps_adj[i])) |
| 38 | + |
| 39 | + return ps_adj |
| 40 | + |
| 41 | + |
| 42 | +@nb.njit(parallel=True, cache=True) |
| 43 | +def _fdr_bh_parallel(ps, m): |
| 44 | + """Apply Benjamini-Hochberg correction to all rows in parallel.""" |
| 45 | + n_rows = ps.shape[0] |
| 46 | + result = np.empty_like(ps, dtype=np.float64) |
| 47 | + |
| 48 | + for i in nb.prange(n_rows): |
| 49 | + result[i] = _fdr_bh_single_row(ps[i], m) |
| 50 | + |
| 51 | + return result |
| 52 | + |
| 53 | + |
| 54 | +def _fdr_bh_axis1_numba(ps): |
| 55 | + """Benjamini–Hochberg adjusted p-values along axis=1 (rows).""" |
| 56 | + ps = np.asarray(ps, dtype=np.float64) |
| 57 | + if ps.ndim != 2: |
| 58 | + raise ValueError("ps must be 2D (n_rows, n_tests) for axis=1.") |
| 59 | + if not np.issubdtype(ps.dtype, np.number): |
| 60 | + raise ValueError("`ps` must be numeric.") |
| 61 | + if not np.all((ps >= 0) & (ps <= 1)): |
| 62 | + raise ValueError("`ps` must be within [0, 1].") |
| 63 | + |
| 64 | + n_rows, m = ps.shape |
| 65 | + if m <= 1: |
| 66 | + return ps.copy().astype(np.float32) |
| 67 | + |
| 68 | + # Process each row in parallel |
| 69 | + result = _fdr_bh_parallel(ps, m) |
| 70 | + return result.astype(np.float32) |
0 commit comments