@@ -39,6 +39,7 @@ class LSSBetaSeriesInputSpec(BaseInterfaceInputSpec):
3939 fir_delays = traits .Either (None , traits .List (traits .Int ),
4040 desc = "FIR delays (in scans)" ,
4141 default = None , usedefault = True )
42+ return_tstat = traits .Bool (desc = "use the T-statistic instead of the raw beta estimates" )
4243
4344
4445class LSSBetaSeriesOutputSpec (TraitedSpec ):
@@ -99,15 +100,20 @@ def _run_interface(self, runtime):
99100 delay_ttype = trial_type + '_delay_{}' .format (delay )
100101 new_delay_ttype = delay_ttype .replace ('_delay_{}' .format (delay ),
101102 'Delay{}Vol' .format (delay ))
102- beta_map = model .compute_contrast (
103- delay_ttype , output_type = 'effect_size' )
103+ beta_map = _calc_beta_map (model ,
104+ delay_ttype ,
105+ self .inputs .hrf_model ,
106+ self .inputs .return_tstat )
104107 if new_delay_ttype in beta_maps :
105108 beta_maps [new_delay_ttype ].append (beta_map )
106109 else :
107110 beta_maps [new_delay_ttype ] = [beta_map ]
108111 else :
109112 # calculate the beta map
110- beta_map = _calc_beta_map (model , trial_type , self .inputs .hrf_model )
113+ beta_map = _calc_beta_map (model ,
114+ trial_type ,
115+ self .inputs .hrf_model ,
116+ self .inputs .return_tstat )
111117 design_matrix_collector [trial_idx ] = model .design_matrices_ [0 ]
112118 # assign beta map to appropriate list
113119 if trial_type in beta_maps :
@@ -168,6 +174,7 @@ class LSABetaSeriesInputSpec(BaseInterfaceInputSpec):
168174 smoothing_kernel = traits .Either (None , traits .Float (),
169175 desc = "full wide half max smoothing kernel" )
170176 high_pass = traits .Float (0.0078125 , desc = "the high pass filter (Hz)" )
177+ return_tstat = traits .Bool (desc = "use the T-statistic instead of the raw beta estimates" )
171178
172179
173180class LSABetaSeriesOutputSpec (TraitedSpec ):
@@ -219,7 +226,10 @@ def _run_interface(self, runtime):
219226 t_type = lsa_df .loc [i_trial , 'original_trial_type' ]
220227
221228 # calculate the beta map
222- beta_map = _calc_beta_map (model , t_name , self .inputs .hrf_model )
229+ beta_map = _calc_beta_map (model ,
230+ t_name ,
231+ self .inputs .hrf_model ,
232+ self .inputs .return_tstat )
223233
224234 # assign beta map to appropriate list
225235 if t_type in beta_maps :
@@ -352,7 +362,7 @@ def _select_confounds(confounds_file, selected_confounds):
352362 return desired_confounds
353363
354364
355- def _calc_beta_map (model , trial_type , hrf_model ):
365+ def _calc_beta_map (model , trial_type , hrf_model , tstat ):
356366 """
357367 Calculates the beta estimates for every voxel from
358368 a nistats model
@@ -361,6 +371,12 @@ def _calc_beta_map(model, trial_type, hrf_model):
361371 ----------
362372 model : nistats.first_level_model.FirstLevelModel
363373 a fit model of the first level results
374+ trial_type : str
375+ the trial to create the beta estimate
376+ hrf_model : str
377+ the hemondynamic response function used to fit the model
378+ tstat : bool
379+ return the t-statistic for the betas instead of the raw estimates
364380
365381 Returns
366382 -------
@@ -369,32 +385,68 @@ def _calc_beta_map(model, trial_type, hrf_model):
369385 """
370386 import numpy as np
371387
388+ # make it so we do not divide by zero
389+ TINY = 1e-50
390+ raw_beta_map = _estimate_map (model , trial_type , hrf_model , 'effect_size' )
391+ if tstat :
392+ var_map = _estimate_map (model , trial_type , hrf_model , 'effect_variance' )
393+ tstat_array = raw_beta_map .get_fdata () / np .sqrt (np .maximum (var_map .get_fdata (), TINY ))
394+ return nib .Nifti2Image (tstat_array , raw_beta_map .affine , raw_beta_map .header )
395+ else :
396+ return raw_beta_map
397+
398+
399+ def _estimate_map (model , trial_type , hrf_model , output_type ):
400+ """
401+ Calculates model output for every voxel from
402+ a nistats model
403+
404+ Parameters
405+ ----------
406+ model : nistats.first_level_model.FirstLevelModel
407+ a fit model of the first level results
408+ trial_type : str
409+ the trial to create the beta estimate
410+ hrf_model : str
411+ the hemondynamic response function used to fit the model
412+ output_type : str
413+ Type of the output map.
414+ Can be ‘z_score’, ‘stat’, ‘p_value’, ‘effect_size’, or ‘effect_variance’
415+
416+ Returns
417+ -------
418+ map_img : nibabel.nifti2.Nifti2Image
419+ nifti image containing voxelwise output_type estimates
420+ """
421+ import numpy as np
422+
372423 # calculate the beta map
373- beta_map_list = []
374- beta_map_base = model .compute_contrast (trial_type , output_type = 'effect_size' )
375- beta_map_list .append (beta_map_base .get_fdata ())
376- beta_sign = np .where (beta_map_list [0 ] < 0 , - 1 , 1 )
424+ map_list = []
425+ map_base = model .compute_contrast (trial_type , output_type = output_type )
426+ map_list .append (map_base .get_fdata ())
427+ sign = np .where (map_list [0 ] < 0 , - 1 , 1 )
377428 if 'derivative' in hrf_model :
378429 td_contrast = '_' .join ([trial_type , 'derivative' ])
379- beta_map_list .append (
430+ map_list .append (
380431 model .compute_contrast (
381- td_contrast , output_type = 'effect_size' ).get_fdata ())
432+ td_contrast , output_type = output_type ).get_fdata ())
382433 if 'dispersion' in hrf_model :
383434 dd_contrast = '_' .join ([trial_type , 'dispersion' ])
384- beta_map_list .append (
435+ map_list .append (
385436 model .compute_contrast (
386- dd_contrast , output_type = 'effect_size' ).get_fdata ())
437+ dd_contrast , output_type = output_type ).get_fdata ())
387438
388- if len (beta_map_list ) == 1 :
389- beta_map = beta_map_base
439+ if len (map_list ) == 1 :
440+ map_img = map_base
390441 else :
391- beta_map_array = beta_sign * \
442+ map_array = sign * \
392443 np .sqrt (
393444 np .sum (
394- np .array ([np .power (c , 2 ) for c in beta_map_list ]), axis = 0 ))
395- beta_map = nib .Nifti2Image (
396- beta_map_array ,
397- beta_map_base .affine ,
398- beta_map_base .header )
445+ np .array ([np .power (c , 2 ) for c in map_list ]), axis = 0 ))
446+
447+ map_img = nib .Nifti2Image (
448+ map_array ,
449+ map_base .affine ,
450+ map_base .header )
399451
400- return beta_map
452+ return map_img
0 commit comments