Skip to content

Add highest dct order #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions nipy/modalities/fmri/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _cosine_drift(period_cut, frametimes):
Returns
-------
cdrift: array of shape(n_scans, n_drifts)
cosin drifts plus a constant regressor at cdrift[:,0]
cosine drifts plus a constant regressor at cdrift[:,-1]

Ref: http://en.wikipedia.org/wiki/Discrete_cosine_transform DCT-II
"""
Expand All @@ -85,13 +85,13 @@ def _cosine_drift(period_cut, frametimes):

dt = frametimes[1] - frametimes[0] # frametimes.max() should be (len_tim-1)*dt
order = int(np.floor(2*len_tim*hfcut*dt)) # s.t. hfcut = 1/(2*dt) yields len_tim
cdrift = np.zeros((len_tim, order))
cdrift = np.zeros((len_tim, order + 1))
nfct = np.sqrt(2.0/len_tim)

for k in range(1, order):
for k in range(1, order + 1):
cdrift[:,k-1] = nfct * np.cos((np.pi/len_tim)*(n_times + .5)*k)

cdrift[:,order-1] = 1. # or 1./sqrt(len_tim) to normalize
cdrift[:, -1] = 1. # or 1./sqrt(len_tim) to normalize
return cdrift


Expand Down
10 changes: 5 additions & 5 deletions nipy/modalities/fmri/realfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def dct_ii_basis(volume_times, order=None, normcols=False):
Returns
-------
dct_basis : array
Shape ``(len(volume_times), order)`` array with DCT-II basis up to
Shape ``(len(volume_times), order + 1)`` array with DCT-II basis up to
order `order`.

Raises
Expand All @@ -43,11 +43,11 @@ def dct_ii_basis(volume_times, order=None, normcols=False):
raise ValueError("DCT basis assumes continuous regular sampling")
n = np.arange(N)
cycle = np.pi * (n + 0.5) / N
dct_basis = np.zeros((N, order))
for k in range(0, order):
dct_basis = np.zeros((N, order + 1))
for k in range(0, order + 1):
dct_basis[:, k] = np.cos(cycle * k)
if normcols: # Set column lengths to 1
lengths = np.ones(order) * np.sqrt(N / 2.)
lengths = np.ones(order + 1) * np.sqrt(N / 2.)
lengths[0:1] = np.sqrt(N) # Allow order=0
dct_basis /= lengths
return dct_basis
Expand Down Expand Up @@ -80,6 +80,6 @@ def dct_ii_cut_basis(volume_times, cut_period):
# Always return constant column
if order == 0:
return np.ones((N, 1))
basis = np.ones((N, order))
basis = np.ones((N, order + 1))
basis[:, :-1] = dct_ii_basis(volume_times, order, normcols=True)[:, 1:]
return basis
6 changes: 3 additions & 3 deletions nipy/modalities/fmri/tests/test_dmtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_cosine_drift():
tim = np.arange(20)
P = 10 # period is half the time, gives us an order 4
nipy_drifts = _cosine_drift(P, tim) #
assert_almost_equal(spm_drifts[:,1:], nipy_drifts[:,:-1])
assert_almost_equal(spm_drifts[:,1:], nipy_drifts[:, :-2])
# nipy_drifts is placing the constant at the end [:,:-1]


Expand Down Expand Up @@ -205,7 +205,7 @@ def test_dmtx2():
hrf_model = 'Canonical'
X, names= dmtx_light(frametimes, paradigm, hrf_model=hrf_model,
drift_model='cosine', hfcut=63)
assert_equal(len(names), 7) # was 8 with old cosine
assert_equal(len(names), 8)

def test_dmtx3():
# idem test_dmtx1 with a different drift term
Expand Down Expand Up @@ -416,7 +416,7 @@ def test_dmtx20():
drift_model='cosine')

# check that the drifts are not constant
assert_true(np.all(np.diff(X[:, -2]) != 0))
assert_true(np.any(np.diff(X[:, -2]) != 0))


def test_fir_block():
Expand Down
11 changes: 6 additions & 5 deletions nipy/modalities/fmri/tests/test_realfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_dct_ii_basis():
spm_fname = pjoin(HERE, 'dct_{0}.txt'.format(N))
spm_mtx = np.loadtxt(spm_fname)
vol_times = np.arange(N) * 15. + 3.2
our_dct = dct_ii_basis(vol_times)
# :-1 is needed because one extra drift was added
our_dct = dct_ii_basis(vol_times)[:, :-1]
# Check dot products of columns
sq_col_lengths = np.ones(N) * N / 2.
sq_col_lengths[0] = N
Expand All @@ -32,13 +33,13 @@ def test_dct_ii_basis():
col_lengths = np.sqrt(sq_col_lengths)
assert_almost_equal(our_dct / col_lengths, spm_mtx)
# Normalize length
our_normed_dct = dct_ii_basis(vol_times, normcols=True)
our_normed_dct = dct_ii_basis(vol_times, normcols=True)[:, :-1]
assert_almost_equal(our_normed_dct, spm_mtx)
assert_almost_equal(our_normed_dct.T.dot(our_normed_dct), np.eye(N))
for i in range(N):
assert_almost_equal(dct_ii_basis(vol_times, i) / col_lengths[:i],
assert_almost_equal(dct_ii_basis(vol_times, i)[:, :-1] / col_lengths[:i],
spm_mtx[:, :i])
assert_almost_equal(dct_ii_basis(vol_times, i, True),
assert_almost_equal(dct_ii_basis(vol_times, i, True)[:, :-1],
spm_mtx[:, :i])
vol_times[0] += 0.1
assert_raises(ValueError, dct_ii_basis, vol_times)
Expand All @@ -55,6 +56,6 @@ def test_dct_ii_cut_basis():
if order == 0:
assert_array_equal(dct_vals, np.ones((N, 1)))
continue
dct_expected = np.ones((N, order))
dct_expected = np.ones((N, order + 1))
dct_expected[:, :-1] = dct_ii_basis(times, order, normcols=True)[:, 1:]
assert_array_equal(dct_vals, dct_expected)