4
4
import numpy as np
5
5
6
6
from ..design import event_design , block_design
7
- from ..utils import events , lambdify_t
7
+ from ..utils import (events , lambdify_t , T , convolve_functions ,
8
+ blocks )
8
9
from ..hrf import glover
9
10
from nipy .algorithms .statistics .formula import make_recarray
10
11
@@ -26,95 +27,134 @@ def test_event_design():
26
27
# Test event design helper function
27
28
# An event design with one event type
28
29
onsets = np .array ([0 , 20 , 40 , 60 ])
30
+ durations = np .array ([2 , 3 , 4 , 5 ])
31
+ offsets = onsets + durations
29
32
c_fac = np .array ([1 , 1 , 1 , 1 ]) # constant factor (one level)
30
33
fac_1 = np .array ([0 , 1 , 0 , 1 ]) # factor 1, two levels
31
34
fac_2 = np .array ([0 , 0 , 1 , 1 ]) # factor 2, two levels
32
35
t = np .arange (0 , 100 , 2 )
33
- # Event spec with no event factor -> single column design, no contrasts
34
- event_spec_0 = make_recarray (onsets , ('time' ,))
35
- X_0 , contrasts_0 = event_design (event_spec_0 , t )
36
- exp_x_0 = lambdify_t (events (onsets , f = glover ))(t )
37
- assert_almost_equal (X_0 , exp_x_0 )
38
- assert_dict_almost_equal (contrasts_0 , dict (constant_0 = 1 ))
39
- X_0 , contrasts_0 = event_design (event_spec_0 , t , level_contrasts = True )
40
- assert_almost_equal (X_0 , exp_x_0 )
41
- assert_dict_almost_equal (contrasts_0 , dict (constant_0 = 1 , _event__1_0 = 1 ))
42
- # Event spec with single factor, but only one level
43
- event_spec_1c = make_recarray (zip (onsets , c_fac ), ('time' , 'smt' ))
44
- X_1c , contrasts_1c = event_design (event_spec_1c , t )
45
- assert_almost_equal (X_1c , exp_x_0 )
46
- assert_dict_almost_equal (contrasts_1c , dict (constant_0 = 1 ))
47
- X_1c , contrasts_1c = event_design (event_spec_1c , t , level_contrasts = True )
48
- assert_dict_almost_equal (contrasts_1c , dict (constant_0 = 1 , smt_1_0 = 1 ))
49
- # Event spec with single factor, two levels
50
- event_spec_1d = make_recarray (zip (onsets , fac_1 ), ('time' , 'smt' ))
51
- exp_x_0 = lambdify_t (events (onsets [fac_1 == 0 ], f = glover ))(t )
52
- exp_x_1 = lambdify_t (events (onsets [fac_1 == 1 ], f = glover ))(t )
53
- X_1d , contrasts_1d = event_design (event_spec_1d , t )
54
- assert_almost_equal (X_1d , np .c_ [exp_x_0 , exp_x_1 ])
55
- assert_dict_almost_equal (contrasts_1d ,
56
- dict (constant_0 = [1 , 1 ], smt_0 = [1 , - 1 ]))
57
- X_1d , contrasts_1d = event_design (event_spec_1d , t , level_contrasts = True )
58
- assert_dict_almost_equal (contrasts_1d ,
59
- dict (constant_0 = 1 ,
60
- smt_0 = [1 , - 1 ], # main effect
61
- smt_0_0 = [1 , 0 ], # level 0, hrf 0
62
- smt_1_0 = [0 , 1 ])) # level 1, hrf 0
63
- # Event spec with two factors, one with two levels, another with one
64
- event_spec_2dc = make_recarray (zip (onsets , fac_1 , c_fac ),
65
- ('time' , 'smt' , 'smte' ))
66
- X_2dc , contrasts_2dc = event_design (event_spec_2dc , t )
67
- assert_almost_equal (X_2dc , np .c_ [exp_x_0 , exp_x_1 ])
68
- assert_dict_almost_equal (contrasts_2dc ,
69
- {'constant_0' : [1 , 1 ],
70
- 'smt_0' : [1 , - 1 ], # main effect
71
- 'smt:smte_0' : [1 , - 1 ], # interaction
72
- })
73
- X_2dc , contrasts_2dc = event_design (event_spec_2dc , t , level_contrasts = True )
74
- assert_dict_almost_equal (contrasts_2dc ,
75
- {'constant_0' : [1 , 1 ],
76
- 'smt_0' : [1 , - 1 ], # main effect
77
- 'smt:smte_0' : [1 , - 1 ], # interaction
78
- 'smt_0*smte_1_0' : [1 , 0 ], # smt 0, smte 0, hrf 0
79
- 'smt_1*smte_1_0' : [0 , 1 ], # smt 1, smte 0, hrf 0
80
- })
81
- # Event spec with two factors, both with two levels
82
- event_spec_2dd = make_recarray (zip (onsets , fac_1 , fac_2 ),
83
- ('time' , 'smt' , 'smte' ))
84
- exp_x_0 = lambdify_t (events (onsets [(fac_1 == 0 ) & (fac_2 == 0 )], f = glover ))(t )
85
- exp_x_1 = lambdify_t (events (onsets [(fac_1 == 0 ) & (fac_2 == 1 )], f = glover ))(t )
86
- exp_x_2 = lambdify_t (events (onsets [(fac_1 == 1 ) & (fac_2 == 0 )], f = glover ))(t )
87
- exp_x_3 = lambdify_t (events (onsets [(fac_1 == 1 ) & (fac_2 == 1 )], f = glover ))(t )
88
- X_2dd , contrasts_2dd = event_design (event_spec_2dd , t )
89
- assert_almost_equal (X_2dd , np .c_ [exp_x_0 , exp_x_1 , exp_x_2 , exp_x_3 ])
90
- exp_cons = {'constant_0' : [1 , 1 , 1 , 1 ],
91
- 'smt_0' : [1 , 1 , - 1 , - 1 ], # main effect fac_1
92
- 'smte_0' : [1 , - 1 , 1 , - 1 ], # main effect fac_2
93
- 'smt:smte_0' : [1 , - 1 , - 1 , 1 ], # interaction
94
- }
95
- assert_dict_almost_equal (contrasts_2dd , exp_cons )
96
- X_2dd , contrasts_2dd = event_design (event_spec_2dd , t , level_contrasts = True )
97
- level_cons = exp_cons .copy ()
98
- level_cons .update ({
99
- 'smt_0*smte_0_0' : [1 , 0 , 0 , 0 ], # smt 0, smte 0, hrf 0
100
- 'smt_0*smte_1_0' : [0 , 1 , 0 , 0 ], # smt 0, smte 1, hrf 0
101
- 'smt_1*smte_0_0' : [0 , 0 , 1 , 0 ], # smt 1, smte 0, hrf 0
102
- 'smt_1*smte_1_0' : [0 , 0 , 0 , 1 ], # smt 1, smte 1, hrf 0
103
- })
104
- assert_dict_almost_equal (contrasts_2dd , level_cons )
105
- # Test max order >> 2, no error
106
- X_2dd , contrasts_2dd = event_design (event_spec_2dd , t , order = 100 )
107
- assert_almost_equal (X_2dd , np .c_ [exp_x_0 , exp_x_1 , exp_x_2 , exp_x_3 ])
108
- assert_dict_almost_equal (contrasts_2dd , exp_cons )
109
- # Test max order = 1
110
- X_2dd , contrasts_2dd = event_design (event_spec_2dd , t , order = 1 )
111
- assert_almost_equal (X_2dd , np .c_ [exp_x_0 , exp_x_1 , exp_x_2 , exp_x_3 ])
112
- # No interaction
113
- assert_dict_almost_equal (contrasts_2dd ,
114
- {'constant_0' : [1 , 1 , 1 , 1 ],
115
- 'smt_0' : [1 , 1 , - 1 , - 1 ], # main effect fac_1
116
- 'smte_0' : [1 , - 1 , 1 , - 1 ], # main effect fac_2
117
- })
118
- # Test field called "time" is necessary
119
- event_spec_1d = make_recarray (zip (onsets , fac_1 ), ('brighteyes' , 'smt' ))
120
- assert_raises (ValueError , event_design , event_spec_1d , t )
36
+
37
+ def mk_ev_spec (factors , names ):
38
+ names = ('time' ,) + tuple (names )
39
+ if len (factors ) == 0 :
40
+ return make_recarray (onsets , names )
41
+ return make_recarray (zip (onsets , * factors ), names )
42
+
43
+ def mk_blk_spec (factors , names ):
44
+ names = ('start' , 'end' ) + tuple (names )
45
+ return make_recarray (zip (onsets , offsets , * factors ), names )
46
+
47
+ def mk_ev_tc (ev_inds ):
48
+ # Make real time course for given event onsets
49
+ return lambdify_t (events (onsets [ev_inds ], f = glover ))(t )
50
+
51
+ def mk_blk_tc (ev_inds ):
52
+ # Make real time course for block onset / offsets
53
+ B = blocks (zip (onsets [ev_inds ], offsets [ev_inds ]))
54
+ term = convolve_functions (B , glover (T ),
55
+ (- 5 , 70 ), # step func support
56
+ (0 , 30. ), # conv kernel support
57
+ 0.02 ) # dt
58
+ return lambdify_t (term )(t )
59
+
60
+ for d_maker , spec_maker , tc_maker , null_name in (
61
+ (event_design , mk_ev_spec , mk_ev_tc , '_event_' ),
62
+ (block_design , mk_blk_spec , mk_blk_tc , '_block_' )):
63
+ # Event spec with no event factor -> single column design, no contrasts
64
+ spec_0 = spec_maker ((), ())
65
+ X_0 , contrasts_0 = d_maker (spec_0 , t )
66
+ exp_x_0 = tc_maker (onsets == onsets )
67
+ assert_almost_equal (X_0 , exp_x_0 )
68
+ assert_dict_almost_equal (contrasts_0 , dict (constant_0 = 1 ))
69
+ X_0 , contrasts_0 = d_maker (spec_0 , t , level_contrasts = True )
70
+ assert_almost_equal (X_0 , exp_x_0 )
71
+ assert_dict_almost_equal (contrasts_0 ,
72
+ {'constant_0' : 1 ,
73
+ null_name + '_1_0' : 1 })
74
+ # Event spec with single factor, but only one level
75
+ spec_1c = spec_maker ((c_fac ,), ('smt' ,))
76
+ X_1c , contrasts_1c = d_maker (spec_1c , t )
77
+ assert_almost_equal (X_1c , exp_x_0 )
78
+ assert_dict_almost_equal (contrasts_1c , dict (constant_0 = 1 ))
79
+ X_1c , contrasts_1c = d_maker (spec_1c , t , level_contrasts = True )
80
+ assert_dict_almost_equal (contrasts_1c , dict (constant_0 = 1 , smt_1_0 = 1 ))
81
+ # Event spec with single factor, two levels
82
+ spec_1d = spec_maker ((fac_1 ,), ('smt' ,))
83
+ exp_x_0 = tc_maker (fac_1 == 0 )
84
+ exp_x_1 = tc_maker (fac_1 == 1 )
85
+ X_1d , contrasts_1d = d_maker (spec_1d , t )
86
+ assert_almost_equal (X_1d , np .c_ [exp_x_0 , exp_x_1 ])
87
+ assert_dict_almost_equal (contrasts_1d ,
88
+ dict (constant_0 = [1 , 1 ], smt_0 = [1 , - 1 ]))
89
+ X_1d , contrasts_1d = d_maker (spec_1d , t , level_contrasts = True )
90
+ assert_dict_almost_equal (contrasts_1d ,
91
+ dict (constant_0 = 1 ,
92
+ smt_0 = [1 , - 1 ], # main effect
93
+ smt_0_0 = [1 , 0 ], # level 0, hrf 0
94
+ smt_1_0 = [0 , 1 ])) # level 1, hrf 0
95
+ # Event spec with two factors, one with two levels, another with one
96
+ spec_2dc = spec_maker ((fac_1 , c_fac ), ('smt' , 'smte' ))
97
+ X_2dc , contrasts_2dc = d_maker (spec_2dc , t )
98
+ assert_almost_equal (X_2dc , np .c_ [exp_x_0 , exp_x_1 ])
99
+ assert_dict_almost_equal (contrasts_2dc ,
100
+ {'constant_0' : [1 , 1 ],
101
+ 'smt_0' : [1 , - 1 ], # main effect
102
+ 'smt:smte_0' : [1 , - 1 ], # interaction
103
+ })
104
+ X_2dc , contrasts_2dc = d_maker (spec_2dc , t , level_contrasts = True )
105
+ assert_dict_almost_equal (contrasts_2dc ,
106
+ {'constant_0' : [1 , 1 ],
107
+ 'smt_0' : [1 , - 1 ], # main effect
108
+ 'smt:smte_0' : [1 , - 1 ], # interaction
109
+ 'smt_0*smte_1_0' : [1 , 0 ], # smt 0, smte 0, hrf 0
110
+ 'smt_1*smte_1_0' : [0 , 1 ], # smt 1, smte 0, hrf 0
111
+ })
112
+ # Event spec with two factors, both with two levels
113
+ spec_2dd = spec_maker ((fac_1 , fac_2 ), ('smt' , 'smte' ))
114
+ exp_x_0 = tc_maker ((fac_1 == 0 ) & (fac_2 == 0 ))
115
+ exp_x_1 = tc_maker ((fac_1 == 0 ) & (fac_2 == 1 ))
116
+ exp_x_2 = tc_maker ((fac_1 == 1 ) & (fac_2 == 0 ))
117
+ exp_x_3 = tc_maker ((fac_1 == 1 ) & (fac_2 == 1 ))
118
+ X_2dd , contrasts_2dd = d_maker (spec_2dd , t )
119
+ assert_almost_equal (X_2dd , np .c_ [exp_x_0 , exp_x_1 , exp_x_2 , exp_x_3 ])
120
+ exp_cons = {'constant_0' : [1 , 1 , 1 , 1 ],
121
+ 'smt_0' : [1 , 1 , - 1 , - 1 ], # main effect fac_1
122
+ 'smte_0' : [1 , - 1 , 1 , - 1 ], # main effect fac_2
123
+ 'smt:smte_0' : [1 , - 1 , - 1 , 1 ], # interaction
124
+ }
125
+ assert_dict_almost_equal (contrasts_2dd , exp_cons )
126
+ X_2dd , contrasts_2dd = d_maker (spec_2dd , t , level_contrasts = True )
127
+ level_cons = exp_cons .copy ()
128
+ level_cons .update ({
129
+ 'smt_0*smte_0_0' : [1 , 0 , 0 , 0 ], # smt 0, smte 0, hrf 0
130
+ 'smt_0*smte_1_0' : [0 , 1 , 0 , 0 ], # smt 0, smte 1, hrf 0
131
+ 'smt_1*smte_0_0' : [0 , 0 , 1 , 0 ], # smt 1, smte 0, hrf 0
132
+ 'smt_1*smte_1_0' : [0 , 0 , 0 , 1 ], # smt 1, smte 1, hrf 0
133
+ })
134
+ assert_dict_almost_equal (contrasts_2dd , level_cons )
135
+ # Test max order >> 2, no error
136
+ X_2dd , contrasts_2dd = d_maker (spec_2dd , t , order = 100 )
137
+ assert_almost_equal (X_2dd , np .c_ [exp_x_0 , exp_x_1 , exp_x_2 , exp_x_3 ])
138
+ assert_dict_almost_equal (contrasts_2dd , exp_cons )
139
+ # Test max order = 1
140
+ X_2dd , contrasts_2dd = d_maker (spec_2dd , t , order = 1 )
141
+ assert_almost_equal (X_2dd , np .c_ [exp_x_0 , exp_x_1 , exp_x_2 , exp_x_3 ])
142
+ # No interaction
143
+ assert_dict_almost_equal (contrasts_2dd ,
144
+ {'constant_0' : [1 , 1 , 1 , 1 ],
145
+ 'smt_0' : [1 , 1 , - 1 , - 1 ], # main effect fac_1
146
+ 'smte_0' : [1 , - 1 , 1 , - 1 ], # main effect fac_2
147
+ })
148
+ # events : test field called "time" is necessary
149
+ spec_1d = make_recarray (zip (onsets , fac_1 ), ('brighteyes' , 'smt' ))
150
+ assert_raises (ValueError , event_design , spec_1d , t )
151
+ # blocks : test fields called "start" and "end" are necessary
152
+ spec_1d = make_recarray (zip (onsets , offsets , fac_1 ),
153
+ ('mister' , 'brighteyes' , 'smt' ))
154
+ assert_raises (ValueError , block_design , spec_1d , t )
155
+ spec_1d = make_recarray (zip (onsets , offsets , fac_1 ),
156
+ ('start' , 'brighteyes' , 'smt' ))
157
+ assert_raises (ValueError , block_design , spec_1d , t )
158
+ spec_1d = make_recarray (zip (onsets , offsets , fac_1 ),
159
+ ('mister' , 'end' , 'smt' ))
160
+ assert_raises (ValueError , block_design , spec_1d , t )
0 commit comments