Ejemplo n.º 1
0
Archivo: fir.py Proyecto: ofenlab/nipy
# The splines are functions of t (time)
bsp_fns = linBspline(np.arange(0, 10, 2))

# We're going to evaluate at these specific values of time
tt = np.linspace(0, 50, 101)
tvals = tt.view(np.dtype([('t', np.float)]))

# Some inter-stimulus intervals
isis = np.random.uniform(low=0, high=3, size=(4, )) + 10.

# Made into event onset times
e = np.cumsum(isis)

# Make event onsets into functions of time convolved with the spline functions.
event_funcs = [utils.events(e, f=fn) for fn in bsp_fns]

# Put into a formula.
f = Formula(event_funcs)

# The design matrix
X = f.design(tvals, return_float=True)

# Show the design matrix as line plots
plt.plot(X[:, 0])
plt.plot(X[:, 1])
plt.plot(X[:, 2])
plt.xlabel('time (s)')
plt.title('B spline used as bases for an FIR response model')
plt.show()
Ejemplo n.º 2
0
# 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])
d2 = d2.subs(t, t2)

# Formula for run 2 signal in terms of time in run 2 (t2)
f2 = Formula([c12, c22, c32]) + d2
Ejemplo n.º 3
0
from nipy.algorithms.statistics.api import Formula, make_recarray
from nipy.modalities.fmri import utils, hrf

# Inter-stimulus intervals (time between events)
dt = np.random.uniform(low=0, high=2.5, size=(50,))
# Onset times from the ISIs
t = np.cumsum(dt)

# We're going to model the amplitudes ('a') by dt (the time between events)
a = sympy.Symbol('a')
linear = utils.define('linear', utils.events(t, dt, f=hrf.glover))
quadratic = utils.define('quad', utils.events(t, dt, f=hrf.glover, g=a**2))
cubic = utils.define('cubic', utils.events(t, dt, f=hrf.glover, g=a**3))

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

# Evaluate this time-based formula at specific times to make the design matrix
tval = make_recarray(np.linspace(0,100, 1001), 't')
X1 = f1.design(tval, return_float=True)

# Now we make a model where the relationship of time between events and signal
# is an 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([exponential])

# Make a design matrix by passing in time and required parameters
params = make_recarray([(4.5, 3.5)], ('l', '_b0'))
X3 = f3.design(tval, params, return_float=True)
Ejemplo n.º 4
0
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
assert X.shape == (50, 9)
assert c['C'].shape == (2, 9)