Inheritance diagram for nipy.modalities.fmri.utils:
This module defines some convenience functions of time.
interp : an expresion for a interpolated function of time
step_function : an expression for a step function of time
events : a convenience function to generate sums of events
blocks : a convenience function to generate sums of blocks
convolve_functions : numerically convolve two functions of time
fourier_basis : a convenience function to generate a Fourier basis
Bases: scipy.interpolate.interpolate.interp1d
Wrapper for interp1 to raise TypeError for object array input
We need this because sympy will try to evaluate interpolated functions when constructing expressions involving floats. At least sympy 1.0 only accepts TypeError or AttributeError as indication that the implemented value cannot be sampled with the sympy expression. Therefore, raise a TypeError directly for an input giving an object array (such as a sympy expression), rather than letting interp1d raise a ValueError.
See:
Initialize a 1D linear interpolation class.
Bases: object
Make a convolution kernel from a symbolic function of t
A convolution kernel is a function with extra attributes to allow it to function as a kernel for numerical convolution (see convolve_functions()).
Parameters: | expr : sympy expression
support : 2 sequence
delta : float
|
---|
Convolve sympy expression g with this kernel
Parameters: | g : sympy expr
g_interval : (2,) sequence of floats
name : None or str, optional
**kwargs : keyword args, optional
|
---|---|
Returns: | fg : sympy expr
|
Step function based on a sequence of intervals.
Parameters: | intervals : (S,) sequence of (2,) sequences
amplitudes : (S,) sequence of float, optional
name : None or str, optional
|
---|---|
Returns: | b_of_t : sympy expr
|
Examples
>>> on_off = [[1,2],[3,4]]
>>> tval = np.array([0.4,1.4,2.4,3.4])
>>> b = blocks(on_off)
>>> lam = lambdify_t(b)
>>> lam(tval)
array([ 0., 1., 0., 1.])
>>> b = blocks(on_off, amplitudes=[3,5])
>>> lam = lambdify_t(b)
>>> lam(tval)
array([ 0., 3., 0., 5.])
Expression containing numerical convolution of fn1 with fn2
Parameters: | f : sympy expr
g : sympy expr
f_interval : (2,) sequence of float
g_interval : (2,) sequence of floats
dt : float
fill : None or float
name : None or str, optional
**kwargs : keyword args, optional
|
---|---|
Returns: | fg : sympy expr
|
Examples
>>> from nipy.algorithms.statistics.formula.formulae import Term
>>> t = Term('t')
This is a square wave on [0,1]
>>> f1 = (t > 0) * (t < 1)
The convolution of f1 with itself is a triangular wave on [0, 2], peaking at 1 with height 1
>>> tri = convolve_functions(f1, f1, [0, 2], [0, 2], 1.0e-3, name='conv')
The result is a symbolic function
>>> print(tri)
conv(t)
Get the numerical values for a time vector
>>> ftri = lambdify(t, tri)
>>> x = np.arange(0, 2, 0.2)
>>> y = ftri(x)
The peak is at 1 >>> x[np.argmax(y)] 1.0
Create function of t expression from arbitrary expression expr
Take an arbitrarily complicated expression expr of ‘t’ and make it an expression that is a simple function of t, of form '%s(t)' % name such that when it evaluates (via lambdify) it has the right values.
Parameters: | expr : sympy expression
name : str |
---|---|
Returns: | nexpr: sympy expression : |
Examples
>>> t = Term('t')
>>> expr = t**2 + 3*t
>>> print(expr)
3*t + t**2
>>> newexpr = define('f', expr)
>>> print(newexpr)
f(t)
>>> f = lambdify_t(newexpr)
>>> f(4)
28
>>> 3*4+4**2
28
Return a sum of functions based on a sequence of times.
Parameters: | times : sequence
amplitudes : None or sequence length $N$, optional
f : sympy.Function, optional
g : sympy.Basic, optional
|
---|---|
Returns: | sum_expression : Sympy.Add
|
Examples
We import some sympy stuff so we can test if we’ve got what we expected
>>> from sympy import DiracDelta, Symbol, Function
>>> from nipy.modalities.fmri.utils import T
>>> evs = events([3,6,9])
>>> evs == DiracDelta(-9 + T) + DiracDelta(-6 + T) + DiracDelta(-3 + T)
True
>>> hrf = Function('hrf')
>>> evs = events([3,6,9], f=hrf)
>>> evs == hrf(-9 + T) + hrf(-6 + T) + hrf(-3 + T)
True
>>> evs = events([3,6,9], amplitudes=[2,1,-1])
>>> evs == -DiracDelta(-9 + T) + 2*DiracDelta(-3 + T) + DiracDelta(-6 + T)
True
sin and cos Formula for Fourier drift
The Fourier basis consists of sine and cosine waves of given frequencies.
Parameters: | freq : sequence of float
|
---|---|
Returns: | f : Formula |
Examples
>>> f=fourier_basis([1,2,3])
>>> f.terms
array([cos(2*pi*t), sin(2*pi*t), cos(4*pi*t), sin(4*pi*t), cos(6*pi*t),
sin(6*pi*t)], dtype=object)
>>> f.mean
_b0*cos(2*pi*t) + _b1*sin(2*pi*t) + _b2*cos(4*pi*t) + _b3*sin(4*pi*t) + _b4*cos(6*pi*t) + _b5*sin(6*pi*t)
Generic interpolation function of t given times and values
Imterpolator such that:
f(times[i]) = values[i]
See scipy.interpolate.interp1d for details of interpolation types and other keyword arguments. Default is ‘kind’ is linear, making this function, by default, have the same behavior as linear_interp.
Parameters: | times : array-like
values : array-like
fill : None or float, optional
name : None or str, optional
**kw : keyword args, optional
|
---|---|
Returns: | f : sympy expression
|
Examples
>>> s = interp([0,4,5.],[2.,4,6])
>>> tval = np.array([-0.1,0.1,3.9,4.1,5.1])
>>> res = lambdify_t(s)(tval)
0 outside bounds by default
>>> np.allclose(res, [0, 2.05, 3.95, 4.2, 0])
True
Return sympy function of t expr lambdified as function of t
Parameters: | expr : sympy expr |
---|---|
Returns: | func : callable
|
Linear interpolation function of t given times and values
Imterpolator such that:
f(times[i]) = values[i]
This version of the function enforces the ‘linear’ kind of interpolation (argument to scipy.interpolate.interp1d).
Parameters: | times : array-like
values : array-like
fill : None or float, optional
name : None or str, optional
**kw : keyword args, optional
|
---|---|
Returns: | f : sympy expression
|
Examples
>>> s = linear_interp([0,4,5.],[2.,4,6])
>>> tval = np.array([-0.1,0.1,3.9,4.1,5.1])
>>> res = lambdify_t(s)(tval)
0 outside bounds by default
>>> np.allclose(res, [0, 2.05, 3.95, 4.2, 0])
True
Right-continuous step function of time t
Function of t such that
f(times[i]) = values[i]
Parameters: | times : (N,) sequence
values : (N,) sequence
fill : float
name : str
|
---|---|
Returns: | f_t : sympy expr
|
Examples
>>> s = step_function([0,4,5],[2,4,6])
>>> tval = np.array([-0.1,3.9,4.1,5.1])
>>> lam = lambdify_t(s)
>>> lam(tval)
array([ 0., 2., 4., 6.])