def _estimate_maximal_stats(self, ts, ppos, pneg, dp):
     n = int(1.0/dp)
     nt, nf = ts.shape[1:]
     max_t = np.empty((n, nt, nf))
     min_t = np.empty((n, nt, nf))
     for t in xrange(nt):
         for f in xrange(nf):
             edges, _ = su.map_t(ts[:,t,f], pneg[:,t,f], dp)
             min_t[:,t,f] = edges
             edges, _ = su.map_t(-ts[:,t,f], ppos[:,t,f], dp)
             max_t[:,t,f] = -edges
     return min_t, max_t
def test_map_t():
    n = 2000
    
    p = np.sort(np.random.randint(1, high=n, size=100)).astype('d')
    # want to find some sampling of p scores with large
    # gaps between values
    while(np.diff(p).max() < 5):
        p = np.sort(np.random.randint(1, high=n, size=100)).astype('d')
    p /= float(n)
    # unsort p
    np.random.shuffle(p)
    # test the neg tail, so reverse the sign of the isf function
    t = -st.norm.isf(p)
    edges, pvals = su.map_t(t, p, 1.0/n)
    # choose an alpha that is not exactly on a bin edge
    alpha = p[5] + .25/n
    k = int( alpha * n )
    thresh = edges[k]
    m = t <= thresh

    yield assert_true, p[m].max() < alpha, 'alpha mapped incorrectly'

    t *= -1
    edges *= -1
    k = int( alpha*n + .5)
    thresh = edges[k]
    m = t >= thresh

    yield assert_true, p[m].max() < alpha, 'alpha mapped incorrectly'
def test_map_t_real_data():
    import os
    pth = os.path.join(os.path.dirname(__file__), 's_beamtf1_avg.mat')
    if not os.path.exists(pth):
        assert False, 'did not find data file'
        return
    mdict, mbeam, dof = su.split_combo_tfstats_matfile(pth)
    p_corr_pos = mdict['p val pos (corr)']
    p_corr_neg = mdict['p val neg (corr)']
    tt = mdict['T test']
    n = 2048
    nt, nf = tt.s.shape[1:]
    max_t_maps = np.empty((n, nt, nf))
    min_t_maps = np.empty((n, nt, nf))
    for t in xrange(nt):
        for f in xrange(nf):
            edges, _ = su.map_t(tt.s[:,t,f], p_corr_neg.s[:,t,f], 1.0/n)
            min_t_maps[:,t,f] = edges
            edges, _ = su.map_t(-tt.s[:,t,f], p_corr_pos.s[:,t,f], 1.0/n)
            max_t_maps[:,t,f] = -edges


    min_t_maps = np.sort(min_t_maps, axis=0)
    alpha = 0.05
    while not (p_corr_neg.s <= alpha).any():
        alpha += 0.05
    print 'testing negative tail at significance level', alpha
    # highest k index satisfying t <= tc
    k_mn = int(alpha * n)
    tc = min_t_maps[k_mn]
    m = tt.s <= tc
    yield assert_false, (p_corr_neg.s[m] > alpha).any()

    max_t_maps = np.sort(max_t_maps, axis=0)
    alpha = 0.05
    while not (p_corr_pos.s <= alpha).any():
        alpha += 0.05
    print 'testing positive tail at significance level', alpha        
    # lowest k index in max_t_maps satisfying significant t >= tc
    k_mx = int((1-alpha) * n + 0.5)
    tc = max_t_maps[k_mx]
    m = tt.s >= tc
    print m.sum()
    yield assert_false, (p_corr_pos.s[m] > alpha).any()
Example #4
0
null = np.sort(np.random.normal(size=(100,), loc=4, scale=1.2))
# create test scores from our experiment
# (but for ease of demonstration, clip the values to be within the null dist)
t_scores = np.clip(
    np.random.normal(size=(20,), loc=5, scale=0.8),
    null.min(), null.max()*.99
    )

# Score the t_scores based on the empirical null distribution
index = su.index(t_scores, null)
p_table = np.linspace(1,0,len(null)+1,endpoint=True)
p_scores = np.take(p_table, index)

# Now, try to recover the null distribution, based on these few
# T-score and P-score pairs -- use the negative edges
edges, pbins = su.map_t(-t_scores, p_scores, 1/100.)

edges = -edges[::-1]

p_recovered = np.array(
    [ ( edges > t_scores[i] ).sum() for i in xrange(len(t_scores))]
    )
assert (np.round(p_scores*100) == p_recovered).all()

probability = np.arange(100)/100.

si = np.argsort(p_scores)
print np.round(p_scores[si]*100), t_scores[si]
pp.plot(probability, null)
pp.plot(probability, edges, 'r')