Skip to content

Commit dc1fb39

Browse files
committed
Merge pull request #200 from matthew-brett/fmri-utils-fixes
RF+TST: Fmri utils fixes Tests for names for step functions, blocks Ability to names blocks functions
2 parents 1d4a94f + 4aac157 commit dc1fb39

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

nipy/modalities/fmri/tests/test_utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
""" Testing fmri utils
44
55
"""
6+
import re
67

78
import numpy as np
89

@@ -24,7 +25,7 @@
2425
)
2526
from .. import hrf
2627

27-
from nose.tools import assert_equal, raises
28+
from nose.tools import assert_equal, raises, assert_false
2829

2930
from numpy.testing import (assert_array_equal, assert_array_almost_equal,
3031
assert_almost_equal)
@@ -99,6 +100,11 @@ def test_step_function():
99100
s = step_function([0,4,5],[4,2,1])
100101
lam = lambdify(t, s)
101102
assert_array_equal(lam(tval), [0, 4, 4, 2, 2, 1])
103+
# Name default
104+
assert_false(re.match(r'step\d+\(t\)$', str(s)) is None)
105+
# Name reloaded
106+
s = step_function([0,4,5],[4,2,1], name='goodie_goodie_yum_yum')
107+
assert_equal(str(s), 'goodie_goodie_yum_yum(t)')
102108

103109

104110
def test_blocks():
@@ -110,6 +116,15 @@ def test_blocks():
110116
b = blocks(on_off, amplitudes=[3,5])
111117
lam = lambdify(t, b)
112118
assert_array_equal(lam(tval), [0, 3, 0, 5])
119+
# Check what happens with names
120+
# Default is from step function
121+
assert_false(re.match(r'step\d+\(t\)$', str(b)) is None)
122+
# Can pass in another
123+
b = blocks(on_off, name='funky_chicken')
124+
assert_equal(str(b), 'funky_chicken(t)')
125+
126+
127+
113128

114129

115130
def numerical_convolve(func1, func2, interval, dt, padding_f=0.1):

nipy/modalities/fmri/utils.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def lambdify_t(expr):
4949

5050
def define(name, expr):
5151
""" Create function of t expression from arbitrary expression `expr`
52-
52+
5353
Take an arbitrarily complicated expression `expr` of 't' and make it
5454
an expression that is a simple function of t, of form ``'%s(t)' %
5555
name`` such that when it evaluates (via ``lambdify``) it has the
@@ -123,7 +123,7 @@ def interp(times, values, fill=0, name=None, **kw):
123123
""" Generic interpolation function of t given `times` and `values`
124124
125125
Imterpolator such that:
126-
126+
127127
f(times[i]) = values[i]
128128
129129
if t < times[0]:
@@ -146,7 +146,7 @@ def interp(times, values, fill=0, name=None, **kw):
146146
Name of symbolic expression to use. If None, a default is used.
147147
**kw : keyword args, optional
148148
passed to ``interp1d``
149-
149+
150150
Returns
151151
-------
152152
f : sympy expression
@@ -179,7 +179,7 @@ def linear_interp(times, values, fill=0, name=None, **kw):
179179
""" Linear interpolation function of t given `times` and `values`
180180
181181
Imterpolator such that:
182-
182+
183183
f(times[i]) = values[i]
184184
185185
if t < times[0]:
@@ -200,10 +200,10 @@ def linear_interp(times, values, fill=0, name=None, **kw):
200200
Name of symbolic expression to use. If None, a default is used.
201201
**kw : keyword args, optional
202202
passed to ``interp1d``
203-
203+
204204
Returns
205205
-------
206-
f : sympy expression
206+
f : sympy expression
207207
A Function of t.
208208
209209
Examples
@@ -265,7 +265,7 @@ def step_function(times, values, name=None, fill=0):
265265
if name is None:
266266
name = 'step%d' % step_function.counter
267267
step_function.counter += 1
268-
268+
269269
def _imp(x):
270270
x = np.asarray(x)
271271
f = np.zeros(x.shape) + fill
@@ -333,7 +333,7 @@ def events(times, amplitudes=None, f=DiracDelta, g=Symbol('a')):
333333
return e
334334

335335

336-
def blocks(intervals, amplitudes=None):
336+
def blocks(intervals, amplitudes=None, name=None):
337337
""" Step function based on a sequence of intervals.
338338
339339
Parameters
@@ -343,6 +343,9 @@ def blocks(intervals, amplitudes=None):
343343
sequences of length 2, giving 'on' and 'off' times of block
344344
amplitudes : (S,) sequence of float, optional
345345
Optional amplitudes for each block. Defaults to 1.
346+
name : None or str, optional
347+
Name of the convolved function in the resulting expression.
348+
Defaults to one created by ``utils.interp``.
346349
347350
Returns
348351
-------
@@ -372,12 +375,12 @@ def blocks(intervals, amplitudes=None):
372375
v += [a, 0]
373376
t.append(np.inf)
374377
v.append(0)
375-
return step_function(t, v)
378+
return step_function(t, v, name=name)
376379

377380

378381
def convolve_functions(fn1, fn2, interval, dt, padding_f=0.1, name=None):
379382
""" Expression containing numerical convolution of `fn1` with `fn2`
380-
383+
381384
Parameters
382385
----------
383386
fn1 : sympy expr

0 commit comments

Comments
 (0)