Skip to content

Commit 002fafc

Browse files
committed
Merge branch 'glm_null_contrasts_rerebased' into main-master
* glm_null_contrasts_rerebased: Removed useless contrast in test_glm.test_high_level_glm_null_contrasts Added test in test_glm.test_high_level_glm_null_contrasts and fixed a glitch in test_glm.generate_fake_fmri_data Syntax change in FMRILinearModel.contrast implementation FMRILinearModel handles the case where contrasts are null for some fmri sessions
2 parents f332367 + eb58b46 commit 002fafc

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

nipy/modalities/fmri/glm.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
import numpy as np
3434

35+
from warnings import warn
36+
3537
import scipy.stats as sps
3638

3739
from nibabel import load, Nifti1Image
@@ -561,8 +563,11 @@ def contrast(self, contrasts, con_id='', contrast_type=None, output_z=True,
561563
'contrasts must be a sequence of %d session contrasts' %
562564
len(self.glms))
563565

566+
contrast_ = None
564567
for i, (glm, con) in enumerate(zip(self.glms, contrasts)):
565-
if i == 0:
568+
if np.all(con == 0):
569+
warn('Contrast for session %d is null' % i)
570+
elif contrast_ is None:
566571
contrast_ = glm.contrast(con, contrast_type)
567572
else:
568573
contrast_ = contrast_ + glm.contrast(con, contrast_type)

nipy/modalities/fmri/tests/test_glm.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ def write_fake_fmri_data(shapes, rk=3, affine=np.eye(4)):
3434

3535

3636
def generate_fake_fmri_data(shapes, rk=3, affine=np.eye(4)):
37-
fmri_data, design_matrices= []
37+
fmri_data = []
38+
design_matrices = []
3839
for i, shape in enumerate(shapes):
3940
data = 100 + np.random.randn(*shape)
4041
data[0] -= 10
4142
fmri_data.append(Nifti1Image(data, affine))
4243
design_matrices.append(np.random.randn(shape[3], rk))
43-
mask = Nifti1Image((np.random.rand(*shape[:3]) > .5).astype(np.int8),
44+
mask = Nifti1Image((np.random.rand(*shape[:3]) > .5).astype(np.int8),
4445
affine)
45-
return mask, fmri_data, design_matrices
46+
return mask, fmri_data, design_matrices
4647

4748

4849
def test_high_level_glm_with_paths():
@@ -105,6 +106,21 @@ def test_high_level_glm_contrasts():
105106
z1.get_data(), z2.get_data())).all())
106107

107108

109+
def test_high_level_glm_null_contrasts():
110+
shapes, rk = ((5, 6, 7, 20), (5, 6, 7, 19)), 3
111+
mask, fmri_data, design_matrices = generate_fake_fmri_data(shapes, rk)
112+
113+
multi_session_model = FMRILinearModel(
114+
fmri_data, design_matrices, mask=None)
115+
multi_session_model.fit()
116+
single_session_model = FMRILinearModel(
117+
fmri_data[:1], design_matrices[:1], mask=None)
118+
single_session_model.fit()
119+
z1, = multi_session_model.contrast([np.eye(rk)[:1], np.zeros((1, rk))])
120+
z2, = single_session_model.contrast([np.eye(rk)[:1]])
121+
np.testing.assert_almost_equal(z1.get_data(), z2.get_data())
122+
123+
108124
def ols_glm(n=100, p=80, q=10):
109125
X, Y = np.random.randn(p, q), np.random.randn(p, n)
110126
glm = GeneralLinearModel(X)

0 commit comments

Comments
 (0)