def group_analysis(design, contrast): """ Compute group analysis effect, t, sd for `design` and `contrast` Saves to disk in 'group' analysis directory Parameters ---------- design : {'block', 'event'} contrast : str contrast name """ array = np.array # shorthand # Directory where output will be written odir = futil.ensure_dir(futil.DATADIR, 'group', design, contrast) # Which subjects have this (contrast, design) pair? subj_con_dirs = futil.subj_des_con_dirs(design, contrast) if len(subj_con_dirs) == 0: raise ValueError('No subjects for %s, %s' % (design, contrast)) # Assemble effects and sds into 4D arrays sds = [] Ys = [] for s in subj_con_dirs: sd_img = load_image(pjoin(s, "sd.nii")) effect_img = load_image(pjoin(s, "effect.nii")) sds.append(sd_img.get_data()) Ys.append(effect_img.get_data()) sd = array(sds) Y = array(Ys) # This function estimates the ratio of the fixed effects variance # (sum(1/sd**2, 0)) to the estimated random effects variance # (sum(1/(sd+rvar)**2, 0)) where rvar is the random effects variance. # The EM algorithm used is described in: # # Worsley, K.J., Liao, C., Aston, J., Petre, V., Duncan, G.H., # Morales, F., Evans, A.C. (2002). \'A general statistical # analysis for fMRI data\'. NeuroImage, 15:1-15 varest = onesample.estimate_varatio(Y, sd) random_var = varest['random'] # XXX - if we have a smoother, use # random_var = varest['fixed'] * smooth(varest['ratio']) # Having estimated the random effects variance (and possibly smoothed it), # the corresponding estimate of the effect and its variance is computed and # saved. # This is the coordmap we will use coordmap = futil.load_image_ds105("fiac_00", "wanatomical.nii").coordmap adjusted_var = sd**2 + random_var adjusted_sd = np.sqrt(adjusted_var) results = onesample.estimate_mean(Y, adjusted_sd) for n in ['effect', 'sd', 't']: im = api.Image(results[n], copy(coordmap)) save_image(im, pjoin(odir, "%s.nii" % n))
def group_analysis(design, contrast): """ Compute group analysis effect, t, sd for `design` and `contrast` Saves to disk in 'group' analysis directory Parameters ---------- design : {'block', 'event'} contrast : str contrast name """ array = np.array # shorthand # Directory where output will be written odir = futil.ensure_dir(futil.DATADIR, 'group', design, contrast) # Which subjects have this (contrast, design) pair? subj_con_dirs = futil.subj_des_con_dirs(design, contrast) if len(subj_con_dirs) == 0: raise ValueError('No subjects for %s, %s' % (design, contrast)) # Assemble effects and sds into 4D arrays sds = [] Ys = [] for s in subj_con_dirs: sd_img = load_image(pjoin(s, "sd.nii")) effect_img = load_image(pjoin(s, "effect.nii")) sds.append(sd_img.get_data()) Ys.append(effect_img.get_data()) sd = array(sds) Y = array(Ys) # This function estimates the ratio of the fixed effects variance # (sum(1/sd**2, 0)) to the estimated random effects variance # (sum(1/(sd+rvar)**2, 0)) where rvar is the random effects variance. # The EM algorithm used is described in: # # Worsley, K.J., Liao, C., Aston, J., Petre, V., Duncan, G.H., # Morales, F., Evans, A.C. (2002). \'A general statistical # analysis for fMRI data\'. NeuroImage, 15:1-15 varest = onesample.estimate_varatio(Y, sd) random_var = varest['random'] # XXX - if we have a smoother, use # random_var = varest['fixed'] * smooth(varest['ratio']) # Having estimated the random effects variance (and possibly smoothed it), # the corresponding estimate of the effect and its variance is computed and # saved. # This is the coordmap we will use coordmap = futil.load_image_ds105("fiac_00","wanatomical.nii").coordmap adjusted_var = sd**2 + random_var adjusted_sd = np.sqrt(adjusted_var) results = onesample.estimate_mean(Y, adjusted_sd) for n in ['effect', 'sd', 't']: im = api.Image(results[n], copy(coordmap)) save_image(im, pjoin(odir, "%s.nii" % n))
def view_thresholdedT(design, contrast, threshold, inequality=np.greater): """ A mayavi isosurface view of thresholded t-statistics Parameters ---------- design : {'standard'} contrast : str threshold : float inequality : {np.greater, np.less}, optional """ maska = np.asarray(MASK) tmap = np.array(load_image_ds105('group', design, contrast, 't.nii')) test = inequality(tmap, threshold) tval = np.zeros(tmap.shape) tval[test] = tmap[test] # XXX make the array axes agree with mayavi2 avganata = np.array(AVGANAT) avganat_iso = mlab.contour3d(avganata * maska, opacity=0.3, contours=[3600], color=(0.8,0.8,0.8)) avganat_iso.actor.property.backface_culling = True avganat_iso.actor.property.ambient = 0.3 tval_iso = mlab.contour3d(tval * MASK, color=(0.8,0.3,0.3), contours=[threshold]) return avganat_iso, tval_iso
def fixed_effects(subj, design): """ Fixed effects (within subject) for OpenfMRI ds105 model Finds run by run estimated model results, creates fixed effects results image per subject. Parameters ---------- subj : int subject number 1..6 inclusive design : {'standard'} design type """ # First, find all the effect and standard deviation images # for the subject and this design type path_dict = futil.path_info_design(subj, design) rootdir = path_dict['rootdir'] # The output directory fixdir = pjoin(rootdir, "fixed") # Fetch results images from run estimations results = futil.results_table(path_dict) # Get our hands on the relevant coordmap to save our results coordmap = futil.load_image_ds105("_%02d" % subj, "wanatomical.nii").coordmap # Compute the "fixed" effects for each type of contrast for con in results: fixed_effect = 0 fixed_var = 0 for effect, sd in results[con]: effect = load_image(effect).get_data() sd = load_image(sd).get_data() var = sd**2 # The optimal, in terms of minimum variance, combination of the # effects has weights 1 / var # # XXX regions with 0 variance are set to 0 # XXX do we want this or np.nan? ivar = np.nan_to_num(1. / var) fixed_effect += effect * ivar fixed_var += ivar # Now, compute the fixed effects variance and t statistic fixed_sd = np.sqrt(fixed_var) isd = np.nan_to_num(1. / fixed_sd) fixed_t = fixed_effect * isd # Save the results odir = futil.ensure_dir(fixdir, con) for a, n in zip([fixed_effect, fixed_sd, fixed_t], ['effect', 'sd', 't']): im = api.Image(a, copy(coordmap)) save_image(im, pjoin(odir, '%s.nii' % n))
def fixed_effects(subj, design): """ Fixed effects (within subject) for OpenfMRI ds105 model Finds run by run estimated model results, creates fixed effects results image per subject. Parameters ---------- subj : int subject number 1..6 inclusive design : {'standard'} design type """ # First, find all the effect and standard deviation images # for the subject and this design type path_dict = futil.path_info_design(subj, design) rootdir = path_dict['rootdir'] # The output directory fixdir = pjoin(rootdir, "fixed") # Fetch results images from run estimations results = futil.results_table(path_dict) # Get our hands on the relevant coordmap to save our results coordmap = futil.load_image_ds105("_%02d" % subj, "wanatomical.nii").coordmap # Compute the "fixed" effects for each type of contrast for con in results: fixed_effect = 0 fixed_var = 0 for effect, sd in results[con]: effect = load_image(effect).get_data() sd = load_image(sd).get_data() var = sd ** 2 # The optimal, in terms of minimum variance, combination of the # effects has weights 1 / var # # XXX regions with 0 variance are set to 0 # XXX do we want this or np.nan? ivar = np.nan_to_num(1. / var) fixed_effect += effect * ivar fixed_var += ivar # Now, compute the fixed effects variance and t statistic fixed_sd = np.sqrt(fixed_var) isd = np.nan_to_num(1. / fixed_sd) fixed_t = fixed_effect * isd # Save the results odir = futil.ensure_dir(fixdir, con) for a, n in zip([fixed_effect, fixed_sd, fixed_t], ['effect', 'sd', 't']): im = api.Image(a, copy(coordmap)) save_image(im, pjoin(odir, '%s.nii' % n))
#----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- SUBJECTS = tuple(range(1,7)) RUNS = tuple(range(1, 13)) DESIGNS = ('standard',) CONTRASTS = ('speaker_0', 'speaker_1', 'sentence_0', 'sentence_1', 'sentence:speaker_0', 'sentence:speaker_1') # XXX: this mask was copied by hand from one of the subjects # we should have a function to create this mask from the ds105 data GROUP_MASK = futil.load_image_ds105('group', 'mask.nii.gz') TINY_MASK = np.zeros(GROUP_MASK.shape, np.bool) TINY_MASK[30:32,40:42,30:32] = 1 #----------------------------------------------------------------------------- # Public functions #----------------------------------------------------------------------------- # For group analysis def run_model(subj, run): """ Single subject fitting of FIAC model """ #---------------------------------------------------------------------- # Set initial parameters of the FIAC dataset
import ds105_util as futil reload(futil) # while developing interactively #----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- SUBJECTS = tuple(range(1, 7)) RUNS = tuple(range(1, 13)) DESIGNS = ('standard', ) CONTRASTS = ('speaker_0', 'speaker_1', 'sentence_0', 'sentence_1', 'sentence:speaker_0', 'sentence:speaker_1') # XXX: this mask was copied by hand from one of the subjects # we should have a function to create this mask from the ds105 data GROUP_MASK = futil.load_image_ds105('group', 'mask.nii.gz') TINY_MASK = np.zeros(GROUP_MASK.shape, np.bool) TINY_MASK[30:32, 40:42, 30:32] = 1 #----------------------------------------------------------------------------- # Public functions #----------------------------------------------------------------------------- # For group analysis def run_model(subj, run): """ Single subject fitting of OpenfMRI ds105 model """ #----------------------------------------------------------------------