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

import sympy
from nipy.modalities.fmri import formula, utils, hrf

# hrf 

h1 = sympy.Function('hrf1')
h2 = sympy.Function('hrf2')
t = formula.Term('t')

# Session 1

t1 = formula.Term('t1')
c11 = utils.events([3,7,10], f=h1); c11 = c11.subs(t, t1)
c21 = utils.events([1,3,9], f=h1); c21 = c21.subs(t, t1)
c31 = utils.events([2,4,8], f=h1); c31 = c31.subs(t, t1)
d1 = utils.fourier_basis([0.3,0.5,0.7]); d1 = d1.subs(t, t1)
tval1 = np.linspace(0,20,101)

f1 = formula.Formula([c11,c21,c31]) + d1
f1 = f1.subs('hrf1', hrf.glover)

# Session 2

t2 = formula.Term('t2')
c12 = utils.events([3.3,7,10], f=h2); c12 = c12.subs(t, t2)
c22 = utils.events([1,3.2,9], f=h2); c22 = c22.subs(t, t2)
c32 = utils.events([2,4.2,8], f=h2); c32 = c32.subs(t, t2)
d2 = utils.fourier_basis([0.3,0.5,0.7]); d2 = d2.subs(t, t2)
tval2 = np.linspace(0,10,51)

f2 = formula.Formula([c12,c22,c32]) + d2
# hrf

h1 = sympy.Function("hrf1")
h2 = sympy.Function("hrf2")
t = formula.Term("t")

# Session 1

t1 = formula.Term("t1")
c11 = utils.events([3, 7, 10], f=h1)
c11 = c11.subs(t, t1)
c21 = utils.events([1, 3, 9], f=h1)
c21 = c21.subs(t, t1)
c31 = utils.events([2, 4, 8], f=h1)
c31 = c31.subs(t, t1)
d1 = utils.fourier_basis([0.3, 0.5, 0.7])
d1 = d1.subs(t, t1)
tval1 = np.linspace(0, 20, 101)

f1 = formula.Formula([c11, c21, c31]) + d1
f1 = f1.subs("hrf1", hrf.glover)

# Session 2

t2 = formula.Term("t2")
c12 = utils.events([3.3, 7, 10], f=h2)
c12 = c12.subs(t, t2)
c22 = utils.events([1, 3.2, 9], f=h2)
c22 = c22.subs(t, t2)
c32 = utils.events([2, 4.2, 8], f=h2)
c32 = c32.subs(t, t2)
# terms of 't', but we'll want models in terms of 't1' and 't2'.  We need 't'
# here so we can substitute.
t = Term('t')

# run 1
t1 = Term('t1')  # Time within run 1
c11 = utils.events([3, 7, 10], f=h1)  # Condition 1, run 1
# The events utility returns a formula in terms of 't' - general time
c11 = c11.subs(t, t1)  # Now make it in terms of time in run 1
# Same for conditions 2 and 3
c21 = utils.events([1, 3, 9], f=h1)
c21 = c21.subs(t, t1)
c31 = utils.events([2, 4, 8], f=h1)
c31 = c31.subs(t, t1)
# Add also a Fourier basis set for drift with frequencies 0.3, 0.5, 0.7
d1 = utils.fourier_basis([0.3, 0.5, 0.7])
d1 = d1.subs(t, t1)

# Here's our formula for run 1 signal terms of time in run 1 (t1)
f1 = Formula([c11, c21, c31]) + d1

# run 2
t2 = Term('t2')  # Time within run 2
# Conditions 1 through 3 in run 2
c12 = utils.events([3.3, 7, 10], f=h2)
c12 = c12.subs(t, t2)
c22 = utils.events([1, 3.2, 9], f=h2)
c22 = c22.subs(t, t2)
c32 = utils.events([2, 4.2, 8], f=h2)
c32 = c32.subs(t, t2)
d2 = utils.fourier_basis([0.3, 0.5, 0.7])
Exemple #5
0
import numpy as np

from nipy.algorithms.statistics.api import Formula, make_recarray
from nipy.modalities.fmri import utils, hrf
from nipy.modalities.fmri.fmristat import hrf as delay

# We take event onsets, and a specified HRF model, and make symbolic functions
# of time
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])  # Symbolic function of time

# We can also use a Fourier basis for some other onsets - again making symbolic
# functions of time
d = utils.fourier_basis([3, 5, 7])  # Formula

# Make a formula for all four sets of onsets
f = Formula([c1, c2, c3]) + d

# A contrast is a formula expressed on the elements of the design formula
contrast = Formula([c1 - c2, c1 - c3])

# Instantiate actual values of time at which to create the design matrix rows
t = make_recarray(np.linspace(0, 20, 50), 't')

# Make the design matrix, and get contrast matrices for the design
X, c = f.design(t, return_float=True, contrasts={'C': contrast})

# c is a dictionary, containing a 2 by 9 matrix - the F contrast matrix for our
# contrast of interest