def get_clustermeans(X, labels, nlabels): """ finds the cluster means of each of the nlabels clusters for each subject in the confiles Parameters: ----------- labels: array with labeled clusters nlabels: number of clusters confiles: links to the confiles of various subjects, for each of the files, the clustermask for a specifically labeled cluster gets applied and then of the selected values, the mean is computed Returns: -------- clustermeans: array of shape (len(confiles), nlabels) with the clustermeans for each subject for each cluster """ confiles, _ = get_subject_data(X) clustermeans = np.zeros([len(confiles), nlabels]) for sub, conf in enumerate(confiles): data = nib.load(conf).get_data() mean_val = np.zeros(nlabels) for clusteridx in range(1,nlabels+1): #for each cluster, find the average value of voxel intensity from data idx = np.where(labels == clusteridx) mean_val[clusteridx-1] = np.mean(data[idx]) clustermeans[sub,:] = mean_val return clustermeans
def predict(self,X): """ predicts from the linear regression model Parameters: ----------- X: test samples used for prediction. Assumes same structure as above """ if self.model_ is not None: # get the confiles + clustermeans of the given subjects clustermeans = get_clustermeans(X, self.labels_, self.nlabels_) _, pdata = get_subject_data(X) features = np.hstack((pdata.lsas_pre[:, None], pdata.classtype[:, None] - 2)) # make new matrix (first behvars, then clustermeans) X_new = np.hstack((features, clustermeans)) prediction = self.model_.predict(X_new[:,self.varidx_]) return prediction else: raise Exception('no model')
def setup_spm(subjects, y): """ runs the SPM analysis workflow within a LOO crossvalidation Parameters: ----------- subjects: vector with a subset of range(subject_num) select_conf: list of confiles from all subjects in the subjects index vector Returns: -------- analdirs: the list of paths to the directories where the SPM output is saved """ metawf = pe.Workflow(name='fitloo') np.random.seed(int(time.time()*100000)) metawf.base_dir = os.path.join(tempspmdir, '%f_%d' % (time.time(), np.random.randint(10000))) count = 0 _, pdata = get_subject_data(subjects) max_folds = np.min(np.histogram(pdata.classtype, bins=2)[0]) #print subjects, pdata.classtype, max_folds for trainidx, testidx in cv.StratifiedKFold(pdata.classtype, max_folds): # workflow count += 1 analname='anal%02d' % count wf = do_spm(subjects[trainidx], y[trainidx], analname=analname, run_workflow=False) metawf.add_nodes([wf]) #print count #print metawf._graph.nodes() metawf.run(plugin='PBS', plugin_args={'qsub_args': '-o /dev/null -e /dev/null', 'max_tries': 5, 'retry_timeout': 1}) #metawf.run(plugin='MultiProc', plugin_args={'n_procs': 24}) #metawf.run() return os.path.join(metawf.base_dir, metawf.name)
def fit(self,X,y): """ fits the model using linear regression Parameters: ----------- X: the design matrix; assumes first column is the subject id (number between 0 and n) according to which, SPM 2lvl is done and clusters and clustermeans are computed; remaining columns are beh data y: target scores for each subject """ _, pdata = get_subject_data(X) features = np.hstack((pdata.lsas_pre[:, None], pdata.classtype[:, None] - 2)) model, varidx, labels, nlabels = _fit(X, y, features) self.labels_ = labels self.nlabels_ = nlabels self.model_ = model self.varidx_ = varidx return self
#from nipype.utils.config import config #config.enable_debug_mode() import nipype.pipeline.engine as pe from spm_2lvl import do_spm #spm workflow --> give directory + confiles from feature_selection import determine_model_all from cluster_tools import get_clustermeans from cfutils import get_subjects, get_subject_data # <codecell> X = get_subjects() _, pdata = get_subject_data(X) X = pdata.subject y = pdata.lsas_pre - pdata.lsas_post dcsidx = np.nonzero(pdata.classtype==2)[0] pcbidx = np.nonzero(pdata.classtype==3)[0] # <codecell> #wf = do_spm(X, y, analname='all_subjects', run_workflow=False) #wf.base_dir = os.path.realpath('..') #wf.run() # <markdowncell> # ###get cluster coordinates
import os import numpy as np from cfutils import get_subjects, get_subject_data X = get_subjects() _, pdata = get_subject_data(X) X = pdata.subject y = pdata.lsas_pre - pdata.lsas_post lgroup,_ = get_subject_data(X[y<=np.median(y)]) hgroup,_ = get_subject_data(X[y>np.median(y)]) import nipype.interfaces.spm as spm from nipype.caching import Memory os.makedirs('/mindhive/scratch/satra/sadfigures/nipype_mem') mem = Memory('/mindhive/scratch/satra/sadfigures') designer = mem.cache(spm.OneSampleTTestDesign) estimator = mem.cache(spm.EstimateModel) cestimator = mem.cache(spm.EstimateContrast) ldesres = designer(in_files = lgroup) lestres = estimator(spm_mat_file=ldesres.outputs.spm_mat_file, estimation_method={'Classical':None}) lcestres = cestimator(spm_mat_file=lestres.outputs.spm_mat_file, beta_images=lestres.outputs.beta_images, residual_image=lestres.outputs.residual_image, group_contrast=True, contrasts=[('LGroup', 'T', ['mean'], [1])])
def do_spm(subjects, y, analdir=None, analname=None, run_workflow=True): """ workflow function that does all spm analysis (2 sample t-test, estimate model, estimate contrasts, threshold image) Parameters ---------- subjects : list of subjects on whom to do regression y : dependent variable corresponding to the list of subjects """ confiles, pdata = get_subject_data(subjects) # MAKE COVARIATES # containes lsas_delta and lsas_pre for group2/3 covariates_group2 = [[],[]] covariates_group3 = [[],[]] # initialize the classtype lists (contains subjects nii files) classtype2 = [] classtype3 = [] # list of subject ids belonging to respective group group2 = [] group3 = [] for sidx, con_file in enumerate(confiles): # add subject files to lists if pdata.classtype[sidx] == 2: # add .nii file classtype2.append(con_file) # add subject id group2.append(sidx) # add lsas_delta score for the subject covariates_group2[0].append(y[sidx]) # and at the same time fill up the matrix of the other group with zeros from the beginning covariates_group3[0].insert(0,0) # do the same for lsas_pre scores covariates_group2[1].append(pdata.lsas_pre[sidx]) covariates_group3[1].insert(0,0) elif pdata.classtype[sidx] == 3: # add .nii file classtype3.append(con_file) # add subject id group3.append(sidx) # add lsas_delta and lsas_pre to group3's covariates covariates_group3[0].append(y[sidx]) covariates_group3[1].append(pdata.lsas_pre[sidx]) # fill up the rest of group2's covariates with zeros for n in range(len(group3)): covariates_group2[0].append(0) covariates_group2[1].append(0) # AT THIS POINT: covariates_group2 has 2 columns (lsas_delta and lsas_pre) # with values in the first half of the vector and zeros in the later half # and covariats_group3 looks the same only that the first half are zeros and the 2nd half contain values # TWO SAMPLE T-TEST ttester = pe.Node(interface = spm.TwoSampleTTestDesign(), name = 'ttestdes') # group1: classtype 2, group2: classtype 3 ttester.inputs.group1_files = classtype2 ttester.inputs.group2_files = classtype3 ttester.inputs.covariates = [dict(vector=covariates_group2[0], name='Group2LSAS_delta'), dict(vector=covariates_group3[0], name='Group3LSAS_delta'), dict(vector=covariates_group2[1], name='Group2LSAS_pre'), dict(vector=covariates_group3[1], name='Group3LSAS_pre')] # ESTIMATE MODEL estimator = pe.Node(interface = spm.EstimateModel(), name = 'ttestest') estimator.inputs.estimation_method = {'Classical':1} # ESTIMATE CONTRAST conest = pe.Node(interface = spm.EstimateContrast(), name = 'conest') conest.inputs.group_contrast = True conest.group_contrast = True con1 = ('Group2>Group3','T',['Group_{1}','Group_{2}'],[1,-1]) con2 = ('Group3>Group2','T',['Group_{1}','Group_{2}'],[-1,1]) con3 = ('Group2LSAS_delta>Group3LSAS_delta','T',['Group2LSAS_delta','Group3LSAS_delta'],[1,-1]) con4 = ('Group2LSAS_delta<Group3LSAS_delta','T',['Group2LSAS_delta','Group3LSAS_delta'],[-1,1]) con5 = ('LSAS Delta Response','T',['Group2LSAS_delta','Group3LSAS_delta'],[.5,.5]) con5 = ('LSAS Delta Response','T',['Group2LSAS_delta','Group3LSAS_delta'],[.5,.5]) conest.inputs.contrasts = [con5] # THRESHOLD thresh = pe.Node(interface = spm.Threshold(), name = 'thresh') thresh.inputs.contrast_index = 1 thresh.inputs.height_threshold = 0.001 thresh.inputs.use_fwe_correction = False thresh.inputs.use_topo_fdr = True thresh.inputs.extent_fdr_p_threshold = 0.05 # WORKFLOW if analname is None: analname = 'some_name' workflow = pe.Workflow(name = analname) workflow.connect(ttester,'spm_mat_file',estimator,'spm_mat_file') workflow.connect(estimator,'spm_mat_file',conest,'spm_mat_file') workflow.connect(estimator,'residual_image',conest,'residual_image') workflow.connect(estimator,'beta_images',conest,'beta_images') workflow.connect(conest,'spm_mat_file',thresh,'spm_mat_file') workflow.connect(conest,'spmT_images',thresh,'stat_image') if run_workflow: if analdir is None: analdir = mkdtemp() workflow.base_dir = analdir workflow.write_graph() workflow.run() return workflow