Exemple #1
0
def test_design():
    # Check that you get the design matrix we expect
    t1 = F.Term("x")
    t2 = F.Term('y')

    n = F.make_recarray([2,4,5], 'x')
    yield assert_almost_equal, t1.formula.design(n)['x'], n['x']

    f = t1.formula + t2.formula
    n = F.make_recarray([(2,3),(4,5),(5,6)], 'xy')

    yield assert_almost_equal, f.design(n)['x'], n['x']
    yield assert_almost_equal, f.design(n)['y'], n['y']

    f = t1.formula + t2.formula + F.I + t1.formula * t2.formula
    yield assert_almost_equal, f.design(n)['x'], n['x']
    yield assert_almost_equal, f.design(n)['y'], n['y']
    yield assert_almost_equal, f.design(n)['1'], 1
    yield assert_almost_equal, f.design(n)['x*y'], n['x']*n['y']

    ny = ML.rec_drop_fields(n, 'y')
    yield assert_raises, ValueError, f.design, ny
    n = np.array([(2,3,'a'),(4,5,'b'),(5,6,'a')], np.dtype([('x', np.float),
                                                            ('y', np.float),
                                                            ('f', 'S1')]))
    f = F.Factor('f', ['a','b'])
    ff = t1.formula * f + F.I
    yield assert_almost_equal, ff.design(n)['f_a*x'], n['x']*[1,0,1]
    yield assert_almost_equal, ff.design(n)['f_b*x'], n['x']*[0,1,0]
    yield assert_almost_equal, ff.design(n)['1'], 1
Exemple #2
0
def test_nonlin1():
    # Fit an exponential curve, with the exponent stratified by a factor
    # with a common intercept and multiplicative factor in front of the
    # exponential
    x = F.Term('x')
    fac = F.Factor('f', 'ab')

    
    f = F.Formula([sympy.exp(fac.stratify(x).mean)]) + F.I

    params = F.getparams(f.mean)
    yield assert_equal, set([str(p) for p in params]), set(['_x0', '_x1', '_b0', '_b1'])
    
    test1 = set(['1',
                 'exp(_x0*f_a + _x1*f_b)',
                 '_b0*f_a*exp(_x0*f_a + _x1*f_b)',
                 '_b0*f_b*exp(_x0*f_a + _x1*f_b)'])
    test2 = set(['1',
                 'exp(_x0*f_a + _x1*f_b)',
                 '_b1*f_a*exp(_x0*f_a + _x1*f_b)',
                 '_b1*f_b*exp(_x0*f_a + _x1*f_b)'])
    yield assert_true, test1 or test2

    n = F.make_recarray([(2,3,'a'),(4,5,'b'),(5,6,'a')], 'xyf', ['d','d','S1'])
    p = F.make_recarray([1,2,3,4], ['_x0', '_x1', '_b0', '_b1'])
    A = f.design(n, p)
    print A, A.dtype
Exemple #3
0
def test_make_recarray():
    m = F.make_recarray([[3, 4], [4, 6], [7, 9]], "wv", [np.float, np.int])

    yield assert_equal, m.dtype.names, ["w", "v"]

    m2 = F.make_recarray(m, "xy")
    yield assert_equal, m2.dtype.names, ["x", "y"]
Exemple #4
0
def test_contrast1():
    x = F.Term('x')

    yield assert_equal, x, x+x

    y = F.Term('y')
    z = F.Term('z')

    f = F.Formula([x,y])

    arr = F.make_recarray([[3,5,4],[8,21,-1],[4,6,-2]], 'xyz')

    D, C = f.design(arr, contrasts={'x':x.formula,
                                    'diff':F.Formula([x-y]),
                                    'sum':F.Formula([x+y]),
                                    'both':F.Formula([x-y,x+y])})
    yield assert_almost_equal, C['x'], np.array([1,0])
    yield assert_almost_equal, C['diff'], np.array([1,-1])
    yield assert_almost_equal, C['sum'], np.array([1,1])
    yield assert_almost_equal, C['both'], np.array([[1,-1],[1,1]])

    f = F.Formula([x,y,z])
    arr = F.make_recarray([[3,5,4],[8,21,-1],[4,6,-2]], 'xyz')

    D, C = f.design(arr, contrasts={'x':x.formula,
                                    'diff':F.Formula([x-y]),
                                    'sum':F.Formula([x+y]),
                                    'both':F.Formula([x-y,x+y])})
    yield assert_almost_equal, C['x'], np.array([1,0,0])
    yield assert_almost_equal, C['diff'], np.array([1,-1,0])
    yield assert_almost_equal, C['sum'], np.array([1,1,0])
    yield assert_almost_equal, C['both'], np.array([[1,-1,0],[1,1,0]])
Exemple #5
0
def test_natural_spline():

    xt=F.Term('x')

    ns=F.natural_spline(xt, knots=[2,6,9])
    xx= F.make_recarray(np.linspace(0,10,101), 'x')
    dd=ns.design(xx, return_float=True)
    xx = xx['x']
    yield assert_almost_equal, dd[:,0], xx
    yield assert_almost_equal, dd[:,1], xx**2
    yield assert_almost_equal, dd[:,2], xx**3
    yield assert_almost_equal, dd[:,3], (xx-2)**3*np.greater_equal(xx,2)
    yield assert_almost_equal, dd[:,4], (xx-6)**3*np.greater_equal(xx,6)
    yield assert_almost_equal, dd[:,5], (xx-9)**3*np.greater_equal(xx,9)

    ns=F.natural_spline(xt, knots=[2,9,6], intercept=True)
    xx= F.make_recarray(np.linspace(0,10,101), 'x')
    dd=ns.design(xx, return_float=True)
    xx = xx['x']
    yield assert_almost_equal, dd[:,0], 1
    yield assert_almost_equal, dd[:,1], xx
    yield assert_almost_equal, dd[:,2], xx**2
    yield assert_almost_equal, dd[:,3], xx**3
    yield assert_almost_equal, dd[:,4], (xx-2)**3*np.greater_equal(xx,2)
    yield assert_almost_equal, dd[:,5], (xx-9)**3*np.greater_equal(xx,9)
    yield assert_almost_equal, dd[:,6], (xx-6)**3*np.greater_equal(xx,6)
Exemple #6
0
def test_nonlin2():
    dz = F.make_recarray([2, 3, 4], "z")
    z = F.Term("z")
    t = sympy.Symbol("th")
    p = F.make_recarray([3], ["tt"])
    f = F.Formula([sympy.exp(t * z)])
    yield assert_raises, ValueError, f.design, dz, p
Exemple #7
0
def test_design():
    # Check that you get the design matrix we expect
    t1 = F.Term("x")
    t2 = F.Term("y")

    n = F.make_recarray([2, 4, 5], "x")
    yield assert_almost_equal, t1.formula.design(n)["x"], n["x"]

    f = t1.formula + t2.formula
    n = F.make_recarray([(2, 3), (4, 5), (5, 6)], "xy")

    yield assert_almost_equal, f.design(n)["x"], n["x"]
    yield assert_almost_equal, f.design(n)["y"], n["y"]

    f = t1.formula + t2.formula + F.I + t1.formula * t2.formula
    yield assert_almost_equal, f.design(n)["x"], n["x"]
    yield assert_almost_equal, f.design(n)["y"], n["y"]
    yield assert_almost_equal, f.design(n)["1"], 1
    yield assert_almost_equal, f.design(n)["x*y"], n["x"] * n["y"]
    # drop x field, check that design raises error
    ny = np.recarray(n.shape, dtype=[("x", n.dtype["x"])])
    ny["x"] = n["x"]
    yield assert_raises, ValueError, f.design, ny
    n = np.array([(2, 3, "a"), (4, 5, "b"), (5, 6, "a")], np.dtype([("x", np.float), ("y", np.float), ("f", "S1")]))
    f = F.Factor("f", ["a", "b"])
    ff = t1.formula * f + F.I
    yield assert_almost_equal, ff.design(n)["f_a*x"], n["x"] * [1, 0, 1]
    yield assert_almost_equal, ff.design(n)["f_b*x"], n["x"] * [0, 1, 0]
    yield assert_almost_equal, ff.design(n)["1"], 1
Exemple #8
0
def test_make_recarray():
    m = F.make_recarray([[3,4],[4,6],[7,9]], 'wv', [np.float, np.int])

    yield assert_equal, m.dtype.names, ['w', 'v']

    m2 = F.make_recarray(m, 'xy')
    yield assert_equal, m2.dtype.names, ['x', 'y']
Exemple #9
0
def test_nonlin2():
    dz = F.make_recarray([2,3,4],'z')
    z = F.Term('z')
    t = sympy.Symbol('th')

    p = F.make_recarray([3], ['tt'])
    f = F.Formula([sympy.exp(t*z)])
    yield assert_raises, ValueError, f.design, dz, p
Exemple #10
0
def test_nonlin1():
    # Fit an exponential curve, with the exponent stratified by a factor
    # with a common intercept and multiplicative factor in front of the
    # exponential
    x = F.Term("x")
    fac = F.Factor("f", "ab")
    f = F.Formula([sympy.exp(fac.stratify(x).mean)]) + F.I
    params = F.getparams(f.mean)
    yield assert_equal, set([str(p) for p in params]), set(["_x0", "_x1", "_b0", "_b1"])
    test1 = set(["1", "exp(_x0*f_a + _x1*f_b)", "_b0*f_a*exp(_x0*f_a + _x1*f_b)", "_b0*f_b*exp(_x0*f_a + _x1*f_b)"])
    test2 = set(["1", "exp(_x0*f_a + _x1*f_b)", "_b1*f_a*exp(_x0*f_a + _x1*f_b)", "_b1*f_b*exp(_x0*f_a + _x1*f_b)"])
    yield assert_true, test1 or test2
    n = F.make_recarray([(2, 3, "a"), (4, 5, "b"), (5, 6, "a")], "xyf", ["d", "d", "S1"])
    p = F.make_recarray([1, 2, 3, 4], ["_x0", "_x1", "_b0", "_b1"])
    A = f.design(n, p)
    print A, A.dtype
Exemple #11
0
def build_dmtx(form, frametimes):
    """
    This is a work arount to control the order of the regressor 
    in the design matrix construction

    Parameters
    ----------
    form: formula.Formula instance,
          the formula that describes the design matrix
    frametimes: array of shape (nb_time_samples),
                the time sampling grid

    Returns
    -------     
    X: array of shape (nrows,nb_time_samples)
       the resulting matrix
    """
    # fixme : workaround to control matrix columns order
    t = formula.make_recarray(frametimes, 't')
    X = []
    for ft in form.terms:
        lf = formula.Formula([ft])
        X.append(lf.design(t, return_float=True))
    X = np.array(X)
    return X 
Exemple #12
0
def test_alias():
    x = F.Term('x')
    f = F.aliased_function('f', lambda x: 2*x)
    g = F.aliased_function('g', lambda x: np.sqrt(x))
    ff = F.Formula([f(x), g(x)**2])
    n = F.make_recarray([2,4,5], 'x')
    yield assert_almost_equal(ff.design(n)['f(x)'], n['x']*2)
    yield assert_almost_equal(ff.design(n)['g(x)**2'], n['x'])
Exemple #13
0
def test_return_float():
    x = F.Term("x")
    f = F.Formula([x, x ** 2])
    xx = F.make_recarray(np.linspace(0, 10, 11), "x")
    dtype = f.design(xx).dtype
    yield assert_equal, set(dtype.names), set(["x", "x**2"])
    dtype = f.design(xx, return_float=True).dtype
    yield assert_equal, dtype, np.float
Exemple #14
0
def test_alias():
    x = F.Term("x")
    f = F.aliased_function("f", lambda x: 2 * x)
    g = F.aliased_function("g", lambda x: np.sqrt(x))
    ff = F.Formula([f(x), g(x) ** 2])
    n = F.make_recarray([2, 4, 5], "x")
    yield assert_almost_equal(ff.design(n)["f(x)"], n["x"] * 2)
    yield assert_almost_equal(ff.design(n)["g(x)**2"], n["x"])
Exemple #15
0
def test_random_effects():
    subj = F.make_recarray([2, 2, 2, 3, 3], "s")
    subj_factor = F.Factor("s", [2, 3])

    c = F.RandomEffects(subj_factor.terms, sigma=np.array([[4, 1], [1, 6]]))
    C = c.cov(subj)
    yield assert_almost_equal, C, [[4, 4, 4, 1, 1], [4, 4, 4, 1, 1], [4, 4, 4, 1, 1], [1, 1, 1, 6, 6], [1, 1, 1, 6, 6]]

    a = sympy.Symbol("a")
    b = sympy.Symbol("b")
    c = F.RandomEffects(subj_factor.terms, sigma=np.array([[a, 0], [0, b]]))
    C = c.cov(subj)
    t = np.equal(C, [[a, a, a, 0, 0], [a, a, a, 0, 0], [a, a, a, 0, 0], [0, 0, 0, b, b], [0, 0, 0, b, b]])
    yield assert_true, np.alltrue(t)
Exemple #16
0
def test_contrast1():
    x = F.Term("x")
    yield assert_equal, x, x + x
    y = F.Term("y")
    z = F.Term("z")
    f = F.Formula([x, y])
    arr = F.make_recarray([[3, 5, 4], [8, 21, -1], [4, 6, -2]], "xyz")
    D, C = f.design(
        arr,
        contrasts={
            "x": x.formula,
            "diff": F.Formula([x - y]),
            "sum": F.Formula([x + y]),
            "both": F.Formula([x - y, x + y]),
        },
    )
    yield assert_almost_equal, C["x"], np.array([1, 0])
    yield assert_almost_equal, C["diff"], np.array([1, -1])
    yield assert_almost_equal, C["sum"], np.array([1, 1])
    yield assert_almost_equal, C["both"], np.array([[1, -1], [1, 1]])

    f = F.Formula([x, y, z])
    arr = F.make_recarray([[3, 5, 4], [8, 21, -1], [4, 6, -2]], "xyz")
    D, C = f.design(
        arr,
        contrasts={
            "x": x.formula,
            "diff": F.Formula([x - y]),
            "sum": F.Formula([x + y]),
            "both": F.Formula([x - y, x + y]),
        },
    )
    yield assert_almost_equal, C["x"], np.array([1, 0, 0])
    yield assert_almost_equal, C["diff"], np.array([1, -1, 0])
    yield assert_almost_equal, C["sum"], np.array([1, 1, 0])
    yield assert_almost_equal, C["both"], np.array([[1, -1, 0], [1, 1, 0]])
import numpy as np
import nipy.testing as niptest
import sympy
from nipy.modalities.fmri import formula, utils, hrf
from nipy.modalities.fmri.fmristat import hrf as delay

c1 = utils.events([3,7,10], f=hrf.glover) # Symbolic function of time
c2 = utils.events([1,3,9], f=hrf.glover) # Symbolic function of time
c3 = utils.events([3,4,6], f=delay.spectral[0])
d = utils.fourier_basis([3,5,7]) # Formula

f = formula.Formula([c1,c2,c3]) + d
contrast = formula.Formula([c1-c2, c1-c3])

t = formula.make_recarray(np.linspace(0,20,50), 't')

X, c = f.design(t, return_float=True, contrasts={'C':contrast})
preC = contrast.design(t, return_float=True)

C = np.dot(np.linalg.pinv(X), preC).T
niptest.assert_almost_equal(C, c['C'])

print C

Exemple #18
0
    Fcontrasts['sentenceF'] = formula.Formula([termdict['sentence%d' % j] for j in range(nhrf)])
    Fcontrasts['interactionF'] = formula.Formula([termdict['interaction%d' % j] for j in range(nhrf)])

    Fcontrasts['overall2'] = Fcontrasts['averageF'] + Fcontrasts['speakerF'] + Fcontrasts['sentenceF'] + Fcontrasts['interactionF']

    return f, Tcontrasts, Fcontrasts


# block and event protocols
block, bTcons, bFcons = protocol(descriptions['block'], 'block', *delay.spectral)
event, eTcons, eFcons = protocol(descriptions['event'], 'event', *delay.spectral)

# Now create the design matrices and contrasts
# The 0 indicates that it will be these columns
# convolved with the first HRF
t = formula.make_recarray(time_vector, 't')
X = {}
c = {}
D = {}
for f, cons, design_type in [(block, bTcons, 'block'), (event, eTcons, 'event')]:
    X[design_type], c[design_type] = f.design(t, contrasts=cons)
    D[design_type] = f.design(t, return_float=False)

    
def test_altprotocol():
    block, bT, bF = protocol(descriptions['block'], 'block', *delay.spectral)
    event, eT, eF = protocol(descriptions['event'], 'event', *delay.spectral)

    blocka, baT, baF = altprotocol(altdescr['block'], 'block', *delay.spectral)
    eventa, eaT, eaF = altprotocol(altdescr['event'], 'event', *delay.spectral)
from nipy.modalities.fmri import utils, formula, hrf

dt = np.random.uniform(low=0, high=2.5, size=(50,))
t = np.cumsum(dt)


a = sympy.Symbol('a')
linear = formula.define('linear', utils.events(t, dt, f=hrf.glover))
quadratic = formula.define('quad', utils.events(t, dt, f=hrf.glover, g=a**2))
cubic = formula.define('cubic', utils.events(t, dt, f=hrf.glover, g=a**3))

f1 = formula.Formula([linear, quadratic, cubic])

# Evaluate them

tval = formula.make_recarray(np.linspace(0,100, 1001), 't')
X1 = f1.design(tval, return_float=True)

# Let's make it exponential with a time constant tau

l = sympy.Symbol('l')
exponential = utils.events(t, dt, f=hrf.glover, g=sympy.exp(-l*a))
f3 = formula.Formula([exponential])

params = formula.make_recarray([(4.5,3.5)], ('l', '_b0'))
X3 = f3.design(tval, params, return_float=True)

# the columns or d/d_b0 and d/dl

tt = tval.view(np.float)
v1 = np.sum([hrf.glovert(tt - s)*np.exp(-4.5*a) for s,a  in zip(t, dt)], 0)
Exemple #20
0
from nipy.modalities.fmri import utils, formula, hrf

dt = np.random.uniform(low=0, high=2.5, size=(50,))
t = np.cumsum(dt)


a = sympy.Symbol("a")
linear = formula.define("linear", utils.events(t, dt, f=hrf.glover))
quadratic = formula.define("quad", utils.events(t, dt, f=hrf.glover, g=a ** 2))
cubic = formula.define("cubic", utils.events(t, dt, f=hrf.glover, g=a ** 3))

f1 = formula.Formula([linear, quadratic, cubic])

# Evaluate them

tval = formula.make_recarray(np.linspace(0, 100, 1001), "t")
X1 = f1.design(tval, return_float=True)

# Let's make it exponential with a time constant tau

l = sympy.Symbol("l")
exponential = utils.events(t, dt, f=hrf.glover, g=sympy.exp(-l * a))
f3 = formula.Formula([exponential])

params = formula.make_recarray([(4.5, 3.5)], ("l", "_b0"))
X3 = f3.design(tval, params, return_float=True)

# the columns or d/d_b0 and d/dl

tt = tval.view(np.float)
v1 = np.sum([hrf.glovert(tt - s) * np.exp(-4.5 * a) for s, a in zip(t, dt)], 0)
Exemple #21
0
def run_model(subj, run):
    """
    Single subject fitting of FIAC model
    """
    #----------------------------------------------------------------------
    # Set initial parameters of the FIAC dataset
    #----------------------------------------------------------------------
    # Number of volumes in the fMRI data
    nvol = 191
    # The TR of the experiment
    TR = 2.5 
    # The time of the first volume
    Tstart = 0.0
    # The array of times corresponding to each 
    # volume in the fMRI data
    volume_times = np.arange(nvol)*TR + Tstart
    # This recarray of times has one column named 't'
    # It is used in the function design.event_design
    # to create the design matrices.
    volume_times_rec = formula.make_recarray(volume_times, 't')
    # Get a path description dictionary that contains all the path data
    # relevant to this subject/run
    path_info = futil.path_info(subj,run)

    #----------------------------------------------------------------------
    # Experimental design
    #----------------------------------------------------------------------

    # Load the experimental description from disk.  We have utilities in futil
    # that reformat the original FIAC-supplied format into something where the
    # factorial structure of the design is more explicit.  This has already
    # been run once, and get_experiment_initial() will simply load the
    # newly-formatted design description files (.csv) into record arrays.
    experiment, initial = futil.get_experiment_initial(path_info)
    
    # Create design matrices for the "initial" and "experiment" factors,
    # saving the default contrasts. 

    # The function event_design will create
    # design matrices, which in the case of "experiment"
    # will have num_columns =
    # (# levels of speaker) * (# levels of sentence) * len(delay.spectral) =
    #      2 * 2 * 2 = 8
    # For "initial", there will be
    # (# levels of initial) * len([hrf.glover]) = 1 * 1 = 1

    # Here, delay.spectral is a sequence of 2 symbolic HRFs that 
    # are described in 
    # 
    # Liao, C.H., Worsley, K.J., Poline, J-B., Aston, J.A.D., Duncan, G.H.,
    #    Evans, A.C. (2002). \'Estimating the delay of the response in fMRI
    #    data.\' NeuroImage, 16:593-606.

    # The contrasts, cons_exper,
    # is a dictionary with keys: ['constant_0', 'constant_1', 'speaker_0', 
    # 'speaker_1',
    # 'sentence_0', 'sentence_1', 'sentence:speaker_0', 'sentence:speaker_1']
    # representing the four default contrasts: constant, main effects + 
    # interactions,
    # each convolved with 2 HRFs in delay.spectral. Its values
    # are matrices with 8 columns.

    # XXX use the hrf __repr__ for naming contrasts

    X_exper, cons_exper = design.event_design(experiment, volume_times_rec,
                                              hrfs=delay.spectral)

    # The contrasts for 'initial' are ignored 
    # as they are "uninteresting" and are included
    # in the model as confounds.

    X_initial, _ = design.event_design(initial, volume_times_rec,
                                       hrfs=[hrf.glover]) 

    # In addition to factors, there is typically a "drift" term
    # In this case, the drift is a natural cubic spline with
    # a not at the midpoint (volume_times.mean())

    vt = volume_times # shorthand
    drift = np.array( [vt**i for i in range(4)] +
                      [(vt-vt.mean())**3 * (np.greater(vt, vt.mean()))] )
    for i in range(drift.shape[0]):
        drift[i] /= drift[i].max()

    # We transpose the drift so that its shape is (nvol,5) so that it will have
    # the same number of rows as X_initial and X_exper.
    drift = drift.T

    # There are helper functions to create these drifts: design.fourier_basis,
    # design.natural_spline.  Therefore, the above is equivalent (except for
    # the normalization by max for numerical stability) to
    #
    # >>> drift = design.natural_spline(t, [volume_times.mean()])

    # Stack all the designs, keeping the new contrasts which has the same keys
    # as cons_exper, but its values are arrays with 15 columns, with the
    # non-zero entries matching the columns of X corresponding to X_exper
    X, cons = design.stack_designs((X_exper, cons_exper),
                                   (X_initial, {}),
                                   (drift, {}))

    # Sanity check: delete any non-estimable contrasts
    # XXX - this seems to be broken right now, it's producing bogus warnings.
    ## for k in cons.keys():
    ##     if not isestimable(X, cons[k]):
    ##         del(cons[k])
    ##         warnings.warn("contrast %s not estimable for this run" % k)

    # The default contrasts are all t-statistics.  We may want to output
    # F-statistics for 'speaker', 'sentence', 'speaker:sentence' based on the
    # two coefficients, one for each HRF in delay.spectral

    cons['speaker'] = np.vstack([cons['speaker_0'], cons['speaker_1']])
    cons['sentence'] = np.vstack([cons['sentence_0'], cons['sentence_1']])
    cons['sentence:speaker'] = np.vstack([cons['sentence:speaker_0'], 
                                          cons['sentence:speaker_1']])

    #----------------------------------------------------------------------
    # Data loading
    #----------------------------------------------------------------------
    
    # Load in the fMRI data, saving it as an array
    # It is transposed to have time as the first dimension,
    # i.e. fmri[t] gives the t-th volume.

    fmri, anat = futil.get_fmri_anat(path_info)
    fmri = np.transpose(fmri, [3,0,1,2])

    nvol, volshape = fmri.shape[0], fmri.shape[1:] 
    nslice, sliceshape = volshape[0], volshape[1:]

    #----------------------------------------------------------------------
    # Model fit
    #----------------------------------------------------------------------

    # The model is a two-stage model, the first stage being an OLS (ordinary
    # least squares) fit, whose residuals are used to estimate an AR(1)
    # parameter for each voxel.

    m = OLSModel(X)
    ar1 = np.zeros(volshape)

    # Fit the model, storing an estimate of an AR(1) parameter at each voxel
    for s in range(nslice):
        d = np.array(fmri[:,s])
        flatd = d.reshape((d.shape[0], -1))
        result = m.fit(flatd)
        ar1[s] = ((result.resid[1:] * result.resid[:-1]).sum(0) /
                  (result.resid**2).sum(0)).reshape(sliceshape)

    # We round ar1 to nearest one-hundredth
    # and group voxels by their rounded ar1 value,
    # fitting an AR(1) model to each batch of voxels.

    # XXX smooth here?
    # ar1 = smooth(ar1, 8.0)

    ar1 *= 100
    ar1 = ar1.astype(np.int) / 100.

    # We split the contrasts into F-tests and t-tests.
    # XXX helper function should do this
    
    fcons = {}; tcons = {}
    for n, v in cons.items():
        v = np.squeeze(v)
        if v.ndim == 1:
            tcons[n] = v
        else:
            fcons[n] = v

    # Setup a dictionary to hold all the output
    # XXX ideally these would be memmap'ed Image instances

    output = {}
    for n in tcons:
        tempdict = {}
        for v in ['sd', 't', 'effect']:
            tempdict[v] = np.memmap(NamedTemporaryFile(prefix='%s%s.nii' \
                                    % (n,v)), dtype=np.float, 
                                    shape=volshape, mode='w+')
        output[n] = tempdict
    
    for n in fcons:
        output[n] = np.memmap(NamedTemporaryFile(prefix='%s%s.nii' \
                                    % (n,v)), dtype=np.float, 
                                    shape=volshape, mode='w+')

    # Loop over the unique values of ar1

    for val in np.unique(ar1):
        armask = np.equal(ar1, val)
        m = ARModel(X, val)
        d = fmri[:,armask]
        results = m.fit(d)

        # Output the results for each contrast

        for n in tcons:
            resT = results.Tcontrast(tcons[n])
            output[n]['sd'][armask] = resT.sd
            output[n]['t'][armask] = resT.t
            output[n]['effect'][armask] = resT.effect

        for n in fcons:
            output[n][armask] = results.Fcontrast(fcons[n]).F

    # Dump output to disk
    odir = futil.output_dir(path_info,tcons,fcons)

    for n in tcons:
        for v in ['t', 'sd', 'effect']:
            im = api.Image(output[n][v], anat.coordmap.copy())
            save_image(im, pjoin(odir, n, '%s.nii' % v))

    for n in fcons:
        im = api.Image(output[n], anat.coordmap.copy())
        save_image(im, pjoin(odir, n, "F.nii"))
Exemple #22
0
# is an example from
# "Applied Linear Statistical Models" that can be found
# The number of Days in a hospital stay are recorded
# based on the Duration of dialysis treatment
# and the Weight of the patient. These
# two variables are described categorically
# as Duration (1 or 2), Weight (1, 2 or 3)
#
# You can find another copy of the data at
#
# http://www-stat.stanford.edu/~jtaylo/courses/stats191/data/kidney.table

D = []
for row in StringIO(data):
    D.append(map(float, row.split()))
D = F.make_recarray(D, ["Days", "Duration", "Weight", "ID"])

# Create the categorical regressors, known as Factors

f1 = F.Factor("Duration", [1, 2])
f2 = F.Factor("Weight", [1, 2, 3])

twoway = f1 * f2

# The columns of X are 0-1 indicator columns,
# return_float = False yields a recarray
# with interpretable names


def test_names():
    # Check that the design column names are what we expect
Exemple #23
0
def test_intercept():

    dz = F.make_recarray([2,3,4],'z')
    v = F.I.design(dz, return_float=False)
    yield assert_equal, v.dtype.names, ['intercept']
Exemple #24
0
# is an example from 
# "Applied Linear Statistical Models" that can be found
# The number of Days in a hospital stay are recorded
# based on the Duration of dialysis treatment
# and the Weight of the patient. These
# two variables are described categorically
# as Duration (1 or 2), Weight (1, 2 or 3)
#
# You can find another copy of the data at
#
# http://www-stat.stanford.edu/~jtaylo/courses/stats191/data/kidney.table

D = []
for row in StringIO(data):
    D.append(map(float, row.split()))
D = F.make_recarray(D, ['Days', 'Duration', 'Weight', 'ID'])

# Create the categorical regressors, known as Factors

f1 = F.Factor('Duration', [1,2])
f2 = F.Factor('Weight', [1,2,3])

twoway = f1 * f2

# The columns of X are 0-1 indicator columns,
# return_float = False yields a recarray 
# with interpretable names

def test_names():
    # Check that the design column names are what we expect
    X = twoway.design(D, return_float=False)
Exemple #25
0
    Fcontrasts["overall2"] = (
        Fcontrasts["averageF"] + Fcontrasts["speakerF"] + Fcontrasts["sentenceF"] + Fcontrasts["interactionF"]
    )

    return f, Tcontrasts, Fcontrasts


# block and event protocols
block, bTcons, bFcons = protocol(StringIO(descriptions["block"]), "block", *delay.spectral)
event, eTcons, eFcons = protocol(StringIO(descriptions["event"]), "event", *delay.spectral)

# Now create the design matrices and contrasts
# The 0 indicates that it will be these columns
# convolved with the first HRF
t = formula.make_recarray(np.arange(191) * 2.5 + 1.25, "t")
X = {}
c = {}
fmristat = {}
D = {}
for f, cons, design_type in [(block, bTcons, "block"), (event, eTcons, "event")]:
    X[design_type], c[design_type] = f.design(t, contrasts=cons)
    D[design_type] = f.design(t, return_float=False)
    fstat = np.array([float(x) for x in designs[design_type].strip().split("\t")])
    fmristat[design_type] = fstat.reshape((191, fstat.shape[0] / 191)).T


def test_altprotocol():
    block, bT, bF = protocol(StringIO(descriptions["block"]), "block", *delay.spectral)
    event, eT, eF = protocol(StringIO(descriptions["event"]), "event", *delay.spectral)