/
test_first_level.py
1553 lines (1324 loc) · 57.5 KB
/
test_first_level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import unittest.mock
import warnings
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
from nibabel import Nifti1Image, load
from nibabel.tmpdirs import InTemporaryDirectory
from nilearn._utils.data_gen import (
_add_metadata_to_bids_dataset,
basic_paradigm,
create_fake_bids_dataset,
generate_fake_fmri_data_and_design,
write_fake_fmri_data_and_design,
)
from nilearn.glm.contrasts import compute_fixed_effects
from nilearn.glm.first_level import (
FirstLevelModel,
first_level_from_bids,
mean_scaling,
run_glm,
)
from nilearn.glm.first_level.design_matrix import (
check_design_matrix,
make_first_level_design_matrix,
)
from nilearn.glm.first_level.first_level import _yule_walker
from nilearn.glm.regression import ARModel, OLSModel
from nilearn.image import get_data
from nilearn.interfaces.bids import get_bids_files
from nilearn.maskers import NiftiMasker
from numpy.testing import (
assert_almost_equal,
assert_array_almost_equal,
assert_array_equal,
assert_array_less,
)
from sklearn.cluster import KMeans
BASEDIR = os.path.dirname(os.path.abspath(__file__))
FUNCFILE = os.path.join(BASEDIR, 'functional.nii.gz')
def test_high_level_glm_one_session():
shapes, rk = [(7, 8, 9, 15)], 3
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes, rk)
# Give an unfitted NiftiMasker as mask_img and check that we get an error
masker = NiftiMasker(mask)
with pytest.raises(ValueError,
match="It seems that NiftiMasker has not been fitted."):
FirstLevelModel(mask_img=masker).fit(
fmri_data[0], design_matrices=design_matrices[0])
# Give a fitted NiftiMasker with a None mask_img_ attribute
# and check that the masker parameters are overridden by the
# FirstLevelModel parameters
masker.fit()
masker.mask_img_ = None
with pytest.warns(UserWarning,
match="Parameter memory of the masker overridden"):
FirstLevelModel(mask_img=masker).fit(
fmri_data[0], design_matrices=design_matrices[0])
# Give a fitted NiftiMasker
masker = NiftiMasker(mask)
masker.fit()
single_session_model = FirstLevelModel(mask_img=masker).fit(
fmri_data[0], design_matrices=design_matrices[0])
assert single_session_model.masker_ == masker
# Call with verbose (improve coverage)
single_session_model = FirstLevelModel(mask_img=None,
verbose=1).fit(
fmri_data[0], design_matrices=design_matrices[0])
single_session_model = FirstLevelModel(mask_img=None).fit(
fmri_data[0], design_matrices=design_matrices[0])
assert isinstance(single_session_model.masker_.mask_img_,
Nifti1Image)
single_session_model = FirstLevelModel(mask_img=mask).fit(
fmri_data[0], design_matrices=design_matrices[0])
z1 = single_session_model.compute_contrast(np.eye(rk)[:1])
assert isinstance(z1, Nifti1Image)
def test_explicit_fixed_effects():
"""Test the fixed effects performed manually/explicitly."""
with InTemporaryDirectory():
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 16)), 3
mask, fmri_data, design_matrices =\
write_fake_fmri_data_and_design(shapes, rk)
contrast = np.eye(rk)[1]
# session 1
multi_session_model = FirstLevelModel(mask_img=mask).fit(
fmri_data[0], design_matrices=design_matrices[:1])
dic1 = multi_session_model.compute_contrast(
contrast, output_type='all')
# session 2
multi_session_model.fit(
fmri_data[1], design_matrices=design_matrices[1:])
dic2 = multi_session_model.compute_contrast(
contrast, output_type='all')
# fixed effects model
multi_session_model.fit(
fmri_data, design_matrices=design_matrices)
fixed_fx_dic = multi_session_model.compute_contrast(
contrast, output_type='all')
# manual version
contrasts = [dic1['effect_size'], dic2['effect_size']]
variance = [dic1['effect_variance'], dic2['effect_variance']]
(
fixed_fx_contrast,
fixed_fx_variance,
fixed_fx_stat,
) = compute_fixed_effects(contrasts, variance, mask)
assert_almost_equal(
get_data(fixed_fx_contrast),
get_data(fixed_fx_dic['effect_size']))
assert_almost_equal(
get_data(fixed_fx_variance),
get_data(fixed_fx_dic['effect_variance']))
assert_almost_equal(
get_data(fixed_fx_stat), get_data(fixed_fx_dic['stat']))
# test without mask variable
(
fixed_fx_contrast,
fixed_fx_variance,
fixed_fx_stat,
) = compute_fixed_effects(contrasts, variance)
assert_almost_equal(
get_data(fixed_fx_contrast),
get_data(fixed_fx_dic['effect_size']))
assert_almost_equal(
get_data(fixed_fx_variance),
get_data(fixed_fx_dic['effect_variance']))
assert_almost_equal(
get_data(fixed_fx_stat), get_data(fixed_fx_dic['stat']))
# ensure that using unbalanced effects size and variance images
# raises an error
with pytest.raises(ValueError):
compute_fixed_effects(contrasts * 2, variance, mask)
del mask, multi_session_model
def test_high_level_glm_with_data():
with InTemporaryDirectory():
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 16)), 3
mask, fmri_data, design_matrices =\
write_fake_fmri_data_and_design(shapes, rk)
multi_session_model = FirstLevelModel(mask_img=None).fit(
fmri_data, design_matrices=design_matrices)
n_voxels = get_data(multi_session_model.masker_.mask_img_).sum()
z_image = multi_session_model.compute_contrast(np.eye(rk)[1])
assert np.sum(get_data(z_image) != 0) == n_voxels
assert get_data(z_image).std() < 3.
# with mask
multi_session_model = FirstLevelModel(mask_img=mask).fit(
fmri_data, design_matrices=design_matrices)
z_image = multi_session_model.compute_contrast(
np.eye(rk)[:2], output_type='z_score')
p_value = multi_session_model.compute_contrast(
np.eye(rk)[:2], output_type='p_value')
stat_image = multi_session_model.compute_contrast(
np.eye(rk)[:2], output_type='stat')
effect_image = multi_session_model.compute_contrast(
np.eye(rk)[:2], output_type='effect_size')
variance_image = multi_session_model.compute_contrast(
np.eye(rk)[:2], output_type='effect_variance')
assert_array_equal(get_data(z_image) == 0., get_data(load(mask)) == 0.)
assert (get_data(variance_image)[get_data(load(mask)) > 0] > .001
).all()
all_images = multi_session_model.compute_contrast(
np.eye(rk)[:2], output_type='all')
assert_array_equal(get_data(all_images['z_score']), get_data(z_image))
assert_array_equal(get_data(all_images['p_value']), get_data(p_value))
assert_array_equal(get_data(all_images['stat']), get_data(stat_image))
assert_array_equal(get_data(all_images['effect_size']),
get_data(effect_image))
assert_array_equal(get_data(all_images['effect_variance']),
get_data(variance_image))
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del (all_images,
design_matrices,
effect_image,
fmri_data,
mask,
multi_session_model,
n_voxels,
p_value,
rk,
shapes,
stat_image,
variance_image,
z_image)
def test_high_level_glm_with_paths():
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 14)), 3
with InTemporaryDirectory():
mask_file, fmri_files, design_files =\
write_fake_fmri_data_and_design(shapes, rk)
multi_session_model = FirstLevelModel(mask_img=None).fit(
fmri_files, design_matrices=design_files)
z_image = multi_session_model.compute_contrast(np.eye(rk)[1])
assert_array_equal(z_image.affine, load(mask_file).affine)
assert get_data(z_image).std() < 3.
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del z_image, fmri_files, multi_session_model
def test_high_level_glm_null_contrasts():
# test that contrast computation is resilient to 0 values.
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 19)), 3
mask, fmri_data, design_matrices = \
generate_fake_fmri_data_and_design(shapes, rk)
multi_session_model = FirstLevelModel(mask_img=None).fit(
fmri_data, design_matrices=design_matrices)
single_session_model = FirstLevelModel(mask_img=None).fit(
fmri_data[0], design_matrices=design_matrices[0])
z1 = multi_session_model.compute_contrast([np.eye(rk)[:1],
np.zeros((1, rk))],
output_type='stat')
z2 = single_session_model.compute_contrast(np.eye(rk)[:1],
output_type='stat')
np.testing.assert_almost_equal(get_data(z1), get_data(z2))
def test_high_level_glm_different_design_matrices():
# test that one can estimate a contrast when design matrices are different
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 19)), 3
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes, rk)
# add a column to the second design matrix
design_matrices[1]['new'] = np.ones((19, 1))
# Fit a glm with two sessions and design matrices
multi_session_model = FirstLevelModel(mask_img=mask).fit(
fmri_data, design_matrices=design_matrices)
z_joint = multi_session_model.compute_contrast(
[np.eye(rk)[:1], np.eye(rk + 1)[:1]], output_type='effect_size')
assert z_joint.shape == (7, 8, 7)
# compare the estimated effects to seprarately-fitted models
model1 = FirstLevelModel(mask_img=mask).fit(
fmri_data[0], design_matrices=design_matrices[0])
z1 = model1.compute_contrast(np.eye(rk)[:1], output_type='effect_size')
model2 = FirstLevelModel(mask_img=mask).fit(
fmri_data[1], design_matrices=design_matrices[1])
z2 = model2.compute_contrast(np.eye(rk + 1)[:1],
output_type='effect_size')
assert_almost_equal(get_data(z1) + get_data(z2),
2 * get_data(z_joint))
def test_high_level_glm_different_design_matrices_formulas():
# test that one can estimate a contrast when design matrices are different
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 19)), 3
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes, rk)
# make column names identical
design_matrices[1].columns = design_matrices[0].columns
# add a column to the second design matrix
design_matrices[1]['new'] = np.ones((19, 1))
# Fit a glm with two sessions and design matrices
multi_session_model = FirstLevelModel(mask_img=mask).fit(
fmri_data, design_matrices=design_matrices)
# Compute contrast with formulas
cols_formula = tuple(design_matrices[0].columns[:2])
formula = "%s-%s" % cols_formula
with pytest.warns(UserWarning, match='One contrast given, '
'assuming it for all 2 runs'):
multi_session_model.compute_contrast(formula,
output_type='effect_size')
def test_compute_contrast_num_contrasts():
shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 19), (7, 8, 7, 13)), 3
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes, rk)
# Fit a glm with 3 sessions and design matrices
multi_session_model = FirstLevelModel(mask_img=mask).fit(
fmri_data, design_matrices=design_matrices)
# raise when n_contrast != n_runs | 1
with pytest.raises(ValueError):
multi_session_model.compute_contrast([np.eye(rk)[1]] * 2)
multi_session_model.compute_contrast([np.eye(rk)[1]] * 3)
with pytest.warns(UserWarning, match='One contrast given, '
'assuming it for all 3 runs'):
multi_session_model.compute_contrast([np.eye(rk)[1]])
def test_run_glm():
rng = np.random.RandomState(42)
n, p, q = 33, 80, 10
X, Y = rng.standard_normal(size=(p, q)), rng.standard_normal(size=(p, n))
# Ordinary Least Squares case
labels, results = run_glm(Y, X, 'ols')
assert_array_equal(labels, np.zeros(n))
assert list(results.keys()) == [0.0]
assert results[0.0].theta.shape == (q, n)
assert_almost_equal(results[0.0].theta.mean(), 0, 1)
assert_almost_equal(results[0.0].theta.var(), 1. / p, 1)
assert type(results[labels[0]].model) == OLSModel
# ar(1) case
labels, results = run_glm(Y, X, 'ar1')
assert len(labels) == n
assert len(results.keys()) > 1
tmp = sum([val.theta.shape[1] for val in results.values()])
assert tmp == n
assert results[labels[0]].model.order == 1
assert type(results[labels[0]].model) == ARModel
# ar(3) case
labels_ar3, results_ar3 = run_glm(Y, X, 'ar3', bins=10)
assert len(labels_ar3) == n
assert len(results_ar3.keys()) > 1
tmp = sum([val.theta.shape[1] for val in results_ar3.values()])
assert tmp == n
assert type(results_ar3[labels_ar3[0]].model) == ARModel
assert results_ar3[labels_ar3[0]].model.order == 3
assert len(results_ar3[labels_ar3[0]].model.rho) == 3
# Check correct errors are thrown for nonsense noise model requests
with pytest.raises(ValueError):
run_glm(Y, X, 'ar0')
with pytest.raises(ValueError):
run_glm(Y, X, 'arfoo')
with pytest.raises(ValueError):
run_glm(Y, X, 'arr3')
with pytest.raises(ValueError):
run_glm(Y, X, 'ar1.2')
with pytest.raises(ValueError):
run_glm(Y, X, 'ar')
with pytest.raises(ValueError):
run_glm(Y, X, '3ar')
def test_glm_AR_estimates():
"""Test that Yule-Walker AR fits are correct."""
n, p, q = 1, 500, 2
X_orig = np.random.RandomState(2).randn(p, q)
Y_orig = np.random.RandomState(2).randn(p, n)
for ar_vals in [[-0.2], [-0.2, -0.5], [-0.2, -0.5, -0.7, -0.3]]:
ar_order = len(ar_vals)
ar_arg = 'ar' + str(ar_order)
X = X_orig.copy()
Y = Y_orig.copy()
for idx in range(1, len(Y)):
for lag in range(ar_order):
Y[idx] += ar_vals[lag] * Y[idx - 1 - lag]
# Test using run_glm
labels, results = run_glm(Y, X, ar_arg, bins=100)
assert len(labels) == n
for lab in results.keys():
ar_estimate = lab.split("_")
for lag in range(ar_order):
assert_almost_equal(float(ar_estimate[lag]),
ar_vals[lag], decimal=1)
# Test using _yule_walker
yw = _yule_walker(Y.T, ar_order)
assert_almost_equal(yw[0], ar_vals, decimal=1)
with pytest.raises(TypeError):
_yule_walker(Y_orig, 1.2)
with pytest.raises(ValueError):
_yule_walker(Y_orig, 0)
with pytest.raises(ValueError):
_yule_walker(Y_orig, -2)
with pytest.raises(TypeError, match='at least 1 dim'):
_yule_walker(np.array(0.), 2)
@pytest.mark.parametrize("random_state", [3, np.random.RandomState(42)])
def test_glm_random_state(random_state):
rng = np.random.RandomState(42)
n, p, q = 33, 80, 10
X, Y = rng.standard_normal(size=(p, q)), rng.standard_normal(size=(p, n))
with unittest.mock.patch.object(
KMeans,
"__init__",
autospec=True,
side_effect=KMeans.__init__,
) as spy_kmeans:
run_glm(Y, X, 'ar3', random_state=random_state)
spy_kmeans.assert_called_once_with(
unittest.mock.ANY,
n_clusters=unittest.mock.ANY,
random_state=random_state)
def test_scaling():
"""Test the scaling function."""
rng = np.random.RandomState(42)
shape = (400, 10)
u = rng.standard_normal(size=shape)
mean = 100 * rng.uniform(size=shape[1]) + 1
Y = u + mean
Y_, mean_ = mean_scaling(Y)
assert_almost_equal(Y_.mean(0), 0, 5)
assert_almost_equal(mean_, mean, 0)
assert Y.std() > 1
def test_fmri_inputs():
# Test processing of FMRI inputs
with InTemporaryDirectory():
shapes = ((7, 8, 9, 10),)
mask, FUNCFILE, _ = write_fake_fmri_data_and_design(shapes)
FUNCFILE = FUNCFILE[0]
func_img = load(FUNCFILE)
T = func_img.shape[-1]
conf = pd.DataFrame([0, 0])
des = pd.DataFrame(np.ones((T, 1)), columns=[''])
des_fname = 'design.csv'
des.to_csv(des_fname)
events = basic_paradigm()
for fi in func_img, FUNCFILE:
for d in des, des_fname:
FirstLevelModel().fit(fi, design_matrices=d)
FirstLevelModel(mask_img=None).fit([fi], design_matrices=d)
FirstLevelModel(mask_img=mask).fit(fi, design_matrices=[d])
FirstLevelModel(mask_img=mask).fit([fi], design_matrices=[d])
with pytest.warns(UserWarning, match="If design matrices "
"are supplied"):
# test with confounds
FirstLevelModel(mask_img=mask).fit([fi],
design_matrices=[d],
confounds=conf)
# Provide t_r, confounds, and events but no design matrix
FirstLevelModel(mask_img=mask, t_r=2.0).fit(
fi,
confounds=pd.DataFrame([0] * 10, columns=['conf']),
events=events)
# Same, but check that an error is raised if there is a
# mismatch in the dimensions of the inputs
with pytest.raises(ValueError,
match="Rows in confounds does not match"):
FirstLevelModel(mask_img=mask, t_r=2.0).fit(
fi, confounds=conf, events=events)
# test with confounds as numpy array
FirstLevelModel(mask_img=mask).fit([fi], design_matrices=[d],
confounds=conf.values)
FirstLevelModel(mask_img=mask).fit([fi, fi],
design_matrices=[d, d])
FirstLevelModel(mask_img=None).fit((fi, fi),
design_matrices=(d, d))
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None).fit([fi, fi], d)
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None).fit(fi, [d, d])
# At least paradigms or design have to be given
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None).fit(fi)
# If paradigms are given then both tr and slice time ref were
# required
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None).fit(fi, d)
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None, t_r=1.0).fit(fi, d)
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None,
slice_time_ref=0.).fit(fi, d)
# confounds rows do not match n_scans
with pytest.raises(ValueError):
FirstLevelModel(mask_img=None).fit(fi, d, conf)
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del fi, func_img, mask, d, des, FUNCFILE, _
def test_first_level_design_creation():
# Test processing of FMRI inputs
with InTemporaryDirectory():
shapes = ((7, 8, 9, 10),)
mask, FUNCFILE, _ = write_fake_fmri_data_and_design(shapes)
FUNCFILE = FUNCFILE[0]
func_img = load(FUNCFILE)
# basic test based on basic_paradigm and glover hrf
t_r = 10.0
slice_time_ref = 0.
events = basic_paradigm()
model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask,
drift_model='polynomial', drift_order=3)
model = model.fit(func_img, events)
frame1, X1, names1 = check_design_matrix(model.design_matrices_[0])
# check design computation is identical
n_scans = get_data(func_img).shape[3]
start_time = slice_time_ref * t_r
end_time = (n_scans - 1 + slice_time_ref) * t_r
frame_times = np.linspace(start_time, end_time, n_scans)
design = make_first_level_design_matrix(frame_times, events,
drift_model='polynomial',
drift_order=3)
frame2, X2, names2 = check_design_matrix(design)
assert_array_equal(frame1, frame2)
assert_array_equal(X1, X2)
assert_array_equal(names1, names2)
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del FUNCFILE, mask, model, func_img
def test_first_level_glm_computation():
with InTemporaryDirectory():
shapes = ((7, 8, 9, 10),)
mask, FUNCFILE, _ = write_fake_fmri_data_and_design(shapes)
FUNCFILE = FUNCFILE[0]
func_img = load(FUNCFILE)
# basic test based on basic_paradigm and glover hrf
t_r = 10.0
slice_time_ref = 0.
events = basic_paradigm()
# Ordinary Least Squares case
model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask,
drift_model='polynomial', drift_order=3,
minimize_memory=False)
model = model.fit(func_img, events)
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del mask, FUNCFILE, func_img, model
def test_first_level_glm_computation_with_memory_caching():
with InTemporaryDirectory():
shapes = ((7, 8, 9, 10),)
mask, FUNCFILE, _ = write_fake_fmri_data_and_design(shapes)
FUNCFILE = FUNCFILE[0]
func_img = load(FUNCFILE)
# initialize FirstLevelModel with memory option enabled
t_r = 10.0
slice_time_ref = 0.
events = basic_paradigm()
# Ordinary Least Squares case
model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask,
drift_model='polynomial', drift_order=3,
memory='nilearn_cache', memory_level=1,
minimize_memory=False)
model.fit(func_img, events)
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del mask, func_img, FUNCFILE, model
def test_first_level_from_bids_set_repetition_time_warnings(tmp_path):
"""Raise a warning when there is no bold.json file in the derivatives
and no TR value is passed as argument.
create_fake_bids_dataset does not add JSON files in derivatives,
so the TR value will be inferred from the raw.
"""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=10,
n_ses=1,
tasks=['main'],
n_runs=[1])
t_r = None
warning_msg = "No bold.json .* BIDS"
with pytest.warns(UserWarning, match=warning_msg):
models, *_ = first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
img_filters=[('desc', 'preproc')],
t_r=t_r,
slice_time_ref=None,
verbose=1
)
# If no t_r is provided it is inferred from the raw dataset
# create_fake_bids_dataset generates a dataset
# with bold data with TR=1.5 secs
expected_t_r = 1.5
assert models[0].t_r == expected_t_r
@pytest.mark.parametrize('t_r, error_type, error_msg',
[('not a number', TypeError, "must be a float"),
(-1, ValueError, "positive")])
def test_first_level_from_bids_set_repetition_time_errors(tmp_path,
t_r,
error_type,
error_msg):
"""Throw errors for impossible values of TR."""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=['main'],
n_runs=[1])
with pytest.raises(error_type, match=error_msg):
first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
img_filters=[('desc', 'preproc')],
slice_time_ref=None,
t_r=t_r
)
def test_first_level_from_bids_set_slice_timing_ref_warnings(tmp_path):
"""Check that a warning is raised when slice_time_ref is not provided \
and cannot be inferred from the dataset.
In this case the model should be created with a slice_time_ref of 0.0.
"""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=10,
n_ses=1,
tasks=['main'],
n_runs=[1])
slice_time_ref = None
warning_msg = "not provided and cannot be inferred"
with pytest.warns(UserWarning, match=warning_msg):
models, *_ = first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
img_filters=[('desc', 'preproc')],
slice_time_ref=slice_time_ref
)
expected_slice_time_ref = 0.0
assert models[0].slice_time_ref == expected_slice_time_ref
@pytest.mark.parametrize('slice_time_ref, error_type, error_msg',
[('not a number', TypeError, "must be a float"),
(2, ValueError, "between 0 and 1")])
def test_first_level_from_bids_set_slice_timing_ref_errors(
tmp_path,
slice_time_ref,
error_type,
error_msg):
"""Throw errors for impossible values of slice_time_ref."""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=['main'],
n_runs=[1])
with pytest.raises(error_type, match=error_msg):
first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
img_filters=[('desc', 'preproc')],
slice_time_ref=slice_time_ref)
def test_first_level_from_bids_get_metadata_from_derivatives(tmp_path):
"""No warning should be thrown given derivatives have metadata.
The model created should use the values found in the derivatives.
"""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=10,
n_ses=1,
tasks=['main'],
n_runs=[1])
RepetitionTime = 6.0
StartTime = 2.0
_add_metadata_to_bids_dataset(
bids_path=tmp_path / bids_path,
metadata={"RepetitionTime": RepetitionTime,
"StartTime": StartTime})
with warnings.catch_warnings():
warnings.simplefilter("error")
models, *_ = first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
img_filters=[('desc', 'preproc')],
slice_time_ref=None,)
assert models[0].t_r == RepetitionTime
assert models[0].slice_time_ref == StartTime / RepetitionTime
def test_first_level_from_bids_get_RepetitionTime_from_derivatives(tmp_path):
"""Only RepetitionTime is provided in derivatives.
Warning about missing StarTime time in derivatives.
slice_time_ref cannot be inferred: defaults to 0.
"""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=10,
n_ses=1,
tasks=['main'],
n_runs=[1])
RepetitionTime = 6.0
_add_metadata_to_bids_dataset(
bids_path=tmp_path / bids_path,
metadata={"RepetitionTime": RepetitionTime})
with pytest.warns(UserWarning,
match="StartTime' not found in file"):
models, *_ = first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
slice_time_ref=None,
img_filters=[('desc', 'preproc')])
assert models[0].t_r == 6.0
assert models[0].slice_time_ref == 0.
def test_first_level_from_bids_get_StartTime_from_derivatives(tmp_path):
"""Only StartTime is provided in derivatives.
Warning about missing repetition time in derivatives,
but RepetitionTime is still read from raw dataset.
"""
bids_path = create_fake_bids_dataset(base_dir=tmp_path,
n_sub=10,
n_ses=1,
tasks=['main'],
n_runs=[1])
StartTime = 1.0
_add_metadata_to_bids_dataset(
bids_path=tmp_path / bids_path,
metadata={"StartTime": StartTime})
with pytest.warns(UserWarning,
match="RepetitionTime' not found in file"):
models, *_ = first_level_from_bids(
dataset_path=str(tmp_path / bids_path),
task_label='main',
space_label='MNI',
img_filters=[('desc', 'preproc')],
slice_time_ref=None)
# create_fake_bids_dataset generates a dataset
# with bold data with TR=1.5 secs
assert models[0].t_r == 1.5
assert models[0].slice_time_ref == StartTime / 1.5
def test_first_level_contrast_computation():
with InTemporaryDirectory():
shapes = ((7, 8, 9, 10),)
mask, FUNCFILE, _ = write_fake_fmri_data_and_design(shapes)
FUNCFILE = FUNCFILE[0]
func_img = load(FUNCFILE)
# basic test based on basic_paradigm and glover hrf
t_r = 10.0
slice_time_ref = 0.
events = basic_paradigm()
# Ordinary Least Squares case
model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask,
drift_model='polynomial', drift_order=3,
minimize_memory=False)
c1, c2, cnull = np.eye(7)[0], np.eye(7)[1], np.zeros(7)
# asking for contrast before model fit gives error
with pytest.raises(ValueError):
model.compute_contrast(c1)
# fit model
model = model.fit([func_img, func_img], [events, events])
# Check that an error is raised for invalid contrast_def
with pytest.raises(ValueError,
match="contrast_def must be an "
"array or str or list"):
model.compute_contrast(37)
# smoke test for different contrasts in fixed effects
model.compute_contrast([c1, c2])
# smoke test for same contrast in fixed effects
model.compute_contrast([c2, c2])
# smoke test for contrast that will be repeated
model.compute_contrast(c2)
model.compute_contrast(c2, 'F')
model.compute_contrast(c2, 't', 'z_score')
model.compute_contrast(c2, 't', 'stat')
model.compute_contrast(c2, 't', 'p_value')
model.compute_contrast(c2, None, 'effect_size')
model.compute_contrast(c2, None, 'effect_variance')
# formula should work (passing variable name directly)
model.compute_contrast('c0')
model.compute_contrast('c1')
model.compute_contrast('c2')
# smoke test for one null contrast in group
model.compute_contrast([c2, cnull])
# only passing null contrasts should give back a value error
with pytest.raises(ValueError):
model.compute_contrast(cnull)
with pytest.raises(ValueError):
model.compute_contrast([cnull, cnull])
# passing wrong parameters
with pytest.raises(ValueError):
model.compute_contrast([])
with pytest.raises(ValueError):
model.compute_contrast([c1, []])
with pytest.raises(ValueError):
model.compute_contrast(c1, '', '')
with pytest.raises(ValueError):
model.compute_contrast(c1, '', [])
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del func_img, FUNCFILE, model
def test_first_level_with_scaling():
shapes, rk = [(3, 1, 1, 2)], 1
fmri_data = list()
fmri_data.append(Nifti1Image(np.zeros((1, 1, 1, 2)) + 6, np.eye(4)))
design_matrices = list()
design_matrices.append(
pd.DataFrame(
np.ones((shapes[0][-1], rk)),
columns=list('abcdefghijklmnopqrstuvwxyz')[:rk])
)
fmri_glm = FirstLevelModel(
mask_img=False, noise_model='ols', signal_scaling=0,
minimize_memory=True
)
assert fmri_glm.signal_scaling == 0
assert not fmri_glm.standardize
with pytest.warns(DeprecationWarning,
match="Deprecated. `scaling_axis` will be removed"):
assert fmri_glm.scaling_axis == 0
glm_parameters = fmri_glm.get_params()
test_glm = FirstLevelModel(**glm_parameters)
fmri_glm = fmri_glm.fit(fmri_data, design_matrices=design_matrices)
test_glm = test_glm.fit(fmri_data, design_matrices=design_matrices)
assert glm_parameters['signal_scaling'] == 0
def test_first_level_with_no_signal_scaling():
"""Test to ensure that the FirstLevelModel works correctly
with a signal_scaling==False.
In particular, that derived theta are correct for a
constant design matrix with a single valued fmri image
"""
shapes, rk = [(3, 1, 1, 2)], 1
fmri_data = list()
design_matrices = list()
design_matrices.append(pd.DataFrame(np.ones((shapes[0][-1], rk)),
columns=list(
'abcdefghijklmnopqrstuvwxyz')[:rk])
)
# Check error with invalid signal_scaling values
with pytest.raises(ValueError,
match="signal_scaling must be"):
FirstLevelModel(mask_img=False, noise_model='ols',
signal_scaling="foo")
first_level = FirstLevelModel(mask_img=False, noise_model='ols',
signal_scaling=False)
fmri_data.append(Nifti1Image(np.zeros((1, 1, 1, 2)) + 6, np.eye(4)))
first_level.fit(fmri_data, design_matrices=design_matrices)
# trivial test of signal_scaling value
assert first_level.signal_scaling is False
# assert that our design matrix has one constant
assert first_level.design_matrices_[0].equals(
pd.DataFrame([1.0, 1.0], columns=['a']))
# assert that we only have one theta as there is only on voxel in our image
assert first_level.results_[0][0].theta.shape == (1, 1)
# assert that the theta is equal to the one voxel value
assert_almost_equal(first_level.results_[0][0].theta[0, 0], 6.0, 2)
def test_first_level_residuals():
shapes, rk = [(10, 10, 10, 100)], 3
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes, rk)
for i in range(len(design_matrices)):
design_matrices[i][design_matrices[i].columns[0]] = 1
# Check that voxelwise model attributes cannot be
# accessed if minimize_memory is set to True
model = FirstLevelModel(mask_img=mask, minimize_memory=True,
noise_model='ols')
model.fit(fmri_data, design_matrices=design_matrices)
with pytest.raises(ValueError,
match="To access voxelwise attributes"):
residuals = model.residuals[0]
model = FirstLevelModel(mask_img=mask, minimize_memory=False,
noise_model='ols')
# Check that trying to access residuals without fitting
# raises an error
with pytest.raises(ValueError,
match="The model has not been fit yet"):
residuals = model.residuals[0]
model.fit(fmri_data, design_matrices=design_matrices)
# For coverage
with pytest.raises(ValueError,
match="attribute must be one of"):
model._get_voxelwise_model_attribute("foo", True)
residuals = model.residuals[0]
mean_residuals = model.masker_.transform(residuals).mean(0)
assert_array_almost_equal(mean_residuals, 0)
@pytest.mark.parametrize("shapes", [
[(10, 10, 10, 25)],
[(10, 10, 10, 25), (10, 10, 10, 100)],
])
def test_get_voxelwise_attributes_should_return_as_many_as_design_matrices(
shapes):
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes)
for i in range(len(design_matrices)):
design_matrices[i][design_matrices[i].columns[0]] = 1
model = FirstLevelModel(mask_img=mask, minimize_memory=False,
noise_model='ols')
model.fit(fmri_data, design_matrices=design_matrices)
# Check that length of outputs is the same as the number of design matrices
assert len(model._get_voxelwise_model_attribute("residuals", True)) == \
len(shapes)
def test_first_level_predictions_r_square():
shapes, rk = [(10, 10, 10, 25)], 3
mask, fmri_data, design_matrices =\
generate_fake_fmri_data_and_design(shapes, rk)
for i in range(len(design_matrices)):
design_matrices[i][design_matrices[i].columns[0]] = 1
model = FirstLevelModel(mask_img=mask,
signal_scaling=False,
minimize_memory=False,
noise_model='ols')
model.fit(fmri_data, design_matrices=design_matrices)
pred = model.predicted[0]
data = fmri_data[0]
r_square_3d = model.r_square[0]
y_predicted = model.masker_.transform(pred)
y_measured = model.masker_.transform(data)
assert_almost_equal(np.mean(y_predicted - y_measured), 0)
r_square_2d = model.masker_.transform(r_square_3d)
assert_array_less(0., r_square_2d)
@pytest.mark.parametrize("hrf_model", [
"spm",
"spm + derivative",
"glover",
lambda tr, ov: np.ones(int(tr * ov))
])
@pytest.mark.parametrize("spaces", [
False,
True
])
def test_first_level_hrf_model(hrf_model, spaces):
"""Ensure that FirstLevelModel runs without raising errors
for different values of hrf_model.
In particular, one checks that it runs
without raising errors when given a custom response function.
When :meth:`~nilearn.glm.first_level.FirstLevelModel.compute_contrast`