def test_OLS(): regress = regression(method=['OLS'], yrange=[0.0, 100.0], params=[{ 'fit_intercept': True }], ransacparams={})
def test_PLS(): regress = regression(method=['PLS'], yrange=[0.0, 100.0], params=[{ 'n_components': 0, 'scale': False }], ransacparams={})
def test_OMP(): regress = regression(method=['OMP'], yrange=[0.0, 100.0], params=[{ 'fit_intercept': True, 'n_nonzero_coefs': 615, 'CV': True }], ransacparams={})
def test_GP(): regress = regression(method=['GP'], yrange=[0.0, 100.0], params=[{ 'reduce_dim': 'PCA', 'n_components': 0, 'random_start': 1, 'theta0': 1.0, 'thetaL': 0.1, 'thetaU': 100.0 }], ransacparams={})
def test_KRR(): regress = regression(method=['KRR'], yrange=[0.0, 100.0], params=[{ 'alpha': 0, 'kernel': 'linear', 'gamma': 'None', 'degree': 3.0, 'coef0': 1.0, 'kernel_params': 'None' }], ransacparams={})
def test_Lasso(): regress = regression(method=['Lasso'], yrange=[0.0, 100.0], params=[{ 'alpha': 1.0, 'fit_intercept': True, 'max_iter': 1000, 'tol': 0.0001, 'positive': False, 'selection': 'random', 'CV': True }], ransacparams={})
def test_Ridge(): regress = regression(method=['Ridge'], yrange=[0.0, 100.0], params=[{ 'alpha': 1.0, 'copy_X': True, 'fit_intercept': True, 'max_iter': 'None', 'normalize': False, 'solver': 'auto', 'tol': 0.0, 'random_state': '' }], ransacparams={})
def taskGLM(subj, task, taskModel='canonical', nuisModel='24pXaCompCorXVolterra', nproc=8): """ This function runs a task-based GLM on a single subject Will only regress out task-timing, using either a canonical HRF model or FIR model Input parameters: subj : subject number as a string task : fMRI task name (2 runs per task for HCP data) nuisModel : nuisance regression model (to identify input data) nproc : number of processes to use via multiprocessing """ h5f = h5py.File(outputdir + subj + '_glmOutput_data.h5', 'a') run1 = h5f[task + '_RL']['nuisanceReg_resid_' + nuisModel][:].copy() run2 = h5f[task + '_LR']['nuisanceReg_resid_' + nuisModel][:].copy() data = np.hstack((run1, run2)) h5f.close() # Identify number of ROIs nROIs = data.shape[0] # Identify number of TRs nTRs = data.shape[1] # Load regressors for data X = loadTaskTiming(subj, task, taskModel=taskModel, nRegsFIR=25) taskRegs = X['taskRegressors'] # These include the two binary regressors betas, resid = regression.regression(data.T, taskRegs, constant=True) betas = betas.T # Exclude nuisance regressors residual_ts = resid.T h5f = h5py.File(outputdir + subj + '_glmOutput_data.h5', 'a') outname1 = 'taskRegression/' + task + '_' + nuisModel + '_taskReg_resid_' + taskModel outname2 = 'taskRegression/' + task + '_' + nuisModel + '_taskReg_betas_' + taskModel try: h5f.create_dataset(outname1, data=residual_ts) h5f.create_dataset(outname2, data=betas) except: del h5f[outname1], h5f[outname2] h5f.create_dataset(outname1, data=residual_ts) h5f.create_dataset(outname2, data=betas) h5f.close()
def test_LARS(): regress = regression(method=['LARS'], yrange=[0.0, 100.0], params=[{ 'n_nonzero_coefs': 500, 'fit_intercept': True, 'positive': False, 'verbose': False, 'normalize': False, 'precompute': True, 'copy_X': True, 'eps': 2.220445, 'fit_path': True }], ransacparams={})
def test_Lasso_LARS(): regress = regression(method=['Lasso LARS'], yrange=[0.0, 100.0], params=[{ 'alpha': 0.0, 'fit_intercept': True, 'positive': False, 'verbose': False, 'normalize': True, 'copy_X': True, 'precompute': 'Auto', 'max_iter': 500, 'eps': 2.220446, 'fit_path': True }], ransacparams={})
def test_Bayesian_Ridge(): regress = regression(method=['Bayesian Ridge'], yrange=[0.0, 100.0], params=[{ 'n_iter': 300, 'tol': 0.001, 'alpha_1': 0.001, 'alpha_2': 1e-06, 'lambda_1': 1e-06, 'lambda_2': 1e-06, 'compute_score': False, 'fit_intercept': True, 'normalize': False, 'copy_X': True, 'verbose': False }], ransacparams={})
def test_SVR(): regress = regression(method=['SVR'], yrange=[0.0, 100.0], params=[{ 'C': 1.0, 'epsilon': 0.1, 'kernel': 'rbf', 'degree': 0, 'gamma': 'auto', 'coef0': 0.0, 'shrinking': False, 'tol': 0.001, 'cache_size': 200, 'verbose': False, 'max_iter': -1 }], ransacparams={})
def test_Elastic_Net(): regress = regression(method=['Elastic Net'], yrange=[0.0, 100.0], params=[{ 'alpha': 1.0, 'l1_ratio': 0.5, 'fit_intercept': True, 'normalize': False, 'precompute': 'False', 'max_iter': 1000, 'copy_X': True, 'tol': 0.0001, 'warm_start': False, 'positive': False, 'selection': 'cyclic', 'random_state': 'None' }], ransacparams={})
def _nuisanceRegression(subj, run, inputdata, outputdir, model='24pXaCompCorXVolterra', spikeReg=False, zscore=False, nproc=8): """ This function runs nuisance regression on the Glasser Parcels (360) on a single subjects run Will only regress out noise parameters given the model choice (see below for model options) Input parameters: subj : subject number as a string run : task run outputdir: Directory for GLM output, as an h5 file (each run will be contained within each h5) model : model choices for linear regression. Models include: 1. 24pXaCompCorXVolterra [default] Variant from Ciric et al. 2017. Includes (64 regressors total): - Movement parameters (6 directions; x, y, z displacement, and 3 rotations) and their derivatives, and their quadratics (24 regressors) - aCompCor (5 white matter and 5 ventricle components) and their derivatives, and their quadratics (40 regressors) 2. 18p (the legacy default) Includes (18 regressors total): - Movement parameters (6 directions) and their derivatives (12 regressors) - Global signal and its derivative (2 regressors) - White matter signal and its derivative (2 regressors) - Ventricles signal and its derivative (2 regressors) 3. 16pNoGSR (the legacy default, without GSR) Includes (16 regressors total): - Movement parameters (6 directions) and their derivatives (12 regressors) - White matter signal and its derivative (2 regressors) - Ventricles signal and its derivative (2 regressors) 4. 12pXaCompCor (Typical motion regression, but using CompCor (noGSR)) Includes (32 regressors total): - Movement parameters (6 directions) and their derivatives (12 regressors) - aCompCor (5 white matter and 5 ventricle components) and their derivatives (no quadratics; 20 regressors) 5. 36p (State-of-the-art, according to Ciric et al. 2017) Includes (36 regressors total - same as legacy, but with quadratics): - Movement parameters (6 directions) and their derivatives and quadratics (24 regressors) - Global signal and its derivative and both quadratics (4 regressors) - White matter signal and its derivative and both quadratics (4 regressors) - Ventricles signal and its derivative (4 regressors) spikeReg : spike regression (Satterthwaite et al. 2013) [True/False] Note, inclusion of this will add additional set of regressors, which is custom for each subject/run zscore : Normalize data (across time) prior to fitting regression nproc = number of processes to use via multiprocessing """ data = inputdata tMask = np.ones((data.shape[1], )) tMask[:framesToSkip] = 0 # Skip frames data = data[:, framesToSkip:] # Demean each run data = signal.detrend(data, axis=1, type='constant') # Detrend each run data = signal.detrend(data, axis=1, type='linear') tMask = np.asarray(tMask, dtype=bool) nROIs = data.shape[0] # Load nuisance regressors for this data h5f = h5py.File(nuis_reg_dir + subj + '_nuisanceRegressors.h5', 'r') if model == '24pXaCompCorXVolterra': # Motion parameters + derivatives motion_parameters = h5f[run]['motionParams'][:].copy() motion_parameters_deriv = h5f[run]['motionParams_deriv'][:].copy() # WM aCompCor + derivatives aCompCor_WM = h5f[run]['aCompCor_WM'][:].copy() aCompCor_WM_deriv = h5f[run]['aCompCor_WM_deriv'][:].copy() # Ventricles aCompCor + derivatives aCompCor_ventricles = h5f[run]['aCompCor_ventricles'][:].copy() aCompCor_ventricles_deriv = h5f[run][ 'aCompCor_ventricles_deriv'][:].copy() # Create nuisance regressors design matrix nuisanceRegressors = np.hstack( (motion_parameters, motion_parameters_deriv, aCompCor_WM, aCompCor_WM_deriv, aCompCor_ventricles, aCompCor_ventricles_deriv)) quadraticRegressors = nuisanceRegressors**2 nuisanceRegressors = np.hstack( (nuisanceRegressors, quadraticRegressors)) elif model == '18p': # Motion parameters + derivatives motion_parameters = h5f[run]['motionParams'][:].copy() motion_parameters_deriv = h5f[run]['motionParams_deriv'][:].copy() # Global signal + derivatives global_signal = h5f[run]['global_signal'][:].copy() global_signal_deriv = h5f[run]['global_signal_deriv'][:].copy() # white matter signal + derivatives wm_signal = h5f[run]['wm_signal'][:].copy() wm_signal_deriv = h5f[run]['wm_signal_deriv'][:].copy() # ventricle signal + derivatives ventricle_signal = h5f[run]['ventricle_signal'][:].copy() ventricle_signal_deriv = h5f[run]['ventricle_signal_deriv'][:].copy() # Create nuisance regressors design matrix tmp = np.vstack( (global_signal, global_signal_deriv, wm_signal, wm_signal_deriv, ventricle_signal, ventricle_signal_deriv )).T # Need to vstack, since these are 1d arrays nuisanceRegressors = np.hstack( (motion_parameters, motion_parameters_deriv, tmp)) elif model == '16pNoGSR': # Motion parameters + derivatives motion_parameters = h5f[run]['motionParams'][:].copy() motion_parameters_deriv = h5f[run]['motionParams_deriv'][:].copy() # white matter signal + derivatives wm_signal = h5f[run]['wm_signal'][:].copy() wm_signal_deriv = h5f[run]['wm_signal_deriv'][:].copy() # ventricle signal + derivatives ventricle_signal = h5f[run]['ventricle_signal'][:].copy() ventricle_signal_deriv = h5f[run]['ventricle_signal_deriv'][:].copy() # Create nuisance regressors design matrix tmp = np.vstack((wm_signal, wm_signal_deriv, ventricle_signal, ventricle_signal_deriv )).T # Need to vstack, since these are 1d arrays nuisanceRegressors = np.hstack( (motion_parameters, motion_parameters_deriv, tmp)) elif model == '12pXaCompCor': # Motion parameters + derivatives motion_parameters = h5f[run]['motionParams'][:].copy() motion_parameters_deriv = h5f[run]['motionParams_deriv'][:].copy() # WM aCompCor + derivatives aCompCor_WM = h5f[run]['aCompCor_WM'][:].copy() aCompCor_WM_deriv = h5f[run]['aCompCor_WM_deriv'][:].copy() # Ventricles aCompCor + derivatives aCompCor_ventricles = h5f[run]['aCompCor_ventricles'][:].copy() aCompCor_ventricles_deriv = h5f[run][ 'aCompCor_ventricles_deriv'][:].copy() # Create nuisance regressors design matrix nuisanceRegressors = np.hstack( (motion_parameters, motion_parameters_deriv, aCompCor_WM, aCompCor_WM_deriv, aCompCor_ventricles, aCompCor_ventricles_deriv)) elif model == '36p': # Motion parameters + derivatives motion_parameters = h5f[run]['motionParams'][:].copy() motion_parameters_deriv = h5f[run]['motionParams_deriv'][:].copy() # Global signal + derivatives global_signal = h5f[run]['global_signal'][:].copy() global_signal_deriv = h5f[run]['global_signal_deriv'][:].copy() # white matter signal + derivatives wm_signal = h5f[run]['wm_signal'][:].copy() wm_signal_deriv = h5f[run]['wm_signal_deriv'][:].copy() # ventricle signal + derivatives ventricle_signal = h5f[run]['ventricle_signal'][:].copy() ventricle_signal_deriv = h5f[run]['ventricle_signal_deriv'][:].copy() # Create nuisance regressors design matrix tmp = np.vstack( (global_signal, global_signal_deriv, wm_signal, wm_signal_deriv, ventricle_signal, ventricle_signal_deriv )).T # Need to vstack, since these are 1d arrays nuisanceRegressors = np.hstack( (motion_parameters, motion_parameters_deriv, tmp)) quadraticRegressors = nuisanceRegressors**2 nuisanceRegressors = np.hstack( (nuisanceRegressors, quadraticRegressors)) if spikeReg: # Obtain motion spikes try: motion_spikes = h5f[run]['motionSpikes'][:].copy() nuisanceRegressors = np.hstack((nuisanceRegressors, motion_spikes)) except: print 'Spike regression option was chosen... but no motion spikes for subj', subj, '| run', run, '!' # Update the model name - to keep track of different model types for output naming model = model + '_spikeReg' if zscore: model = model + '_zscore' h5f.close() # Skip first 5 frames of nuisanceRegressors, too nuisanceRegressors = nuisanceRegressors[framesToSkip:, :].copy() betas, resid = regression.regression(data.T, nuisanceRegressors, constant=True) betas = betas.T # Exclude nuisance regressors residual_ts = resid.T if zscore: residual_ts = stats.zscore(residual_ts, axis=1) outname1 = run + '/nuisanceReg_resid_' + model outname2 = run + '/nuisanceReg_betas_' + model outputfilename = outputdir + subj + '_glmOutput_data.h5' h5f = h5py.File(outputfilename, 'a') try: h5f.create_dataset(outname1, data=residual_ts) h5f.create_dataset(outname2, data=betas) except: del h5f[outname1], h5f[outname2] h5f.create_dataset(outname1, data=residual_ts) h5f.create_dataset(outname2, data=betas) h5f.close()
def taskGLM_onRest(subj, taskModel='canonical', nuisModel='24pXaCompCorXVolterra', nproc=8): """ This function runs a task-based GLM on a single subject Will only regress out task-timing, using either a canonical HRF model or FIR model Input parameters: subj : subject number as a string task : fMRI task name (2 runs per task for HCP data) nuisModel : nuisance regression model (to identify input data) nproc : number of processes to use via multiprocessing """ h5f = h5py.File(outputdir + subj + '_glmOutput_data.h5', 'a') restname = 'rfMRI_REST' run1 = h5f[restname + '1_RL']['nuisanceReg_resid_' + nuisModel][:].copy() run2 = h5f[restname + '1_LR']['nuisanceReg_resid_' + nuisModel][:].copy() run3 = h5f[restname + '2_RL']['nuisanceReg_resid_' + nuisModel][:].copy() run4 = h5f[restname + '2_LR']['nuisanceReg_resid_' + nuisModel][:].copy() data = np.hstack((run1, run2, run3, run4)) h5f.close() # Identify number of ROIs nROIs = data.shape[0] # Identify number of TRs nTRs = data.shape[1] # Load regressors for data, for each task, and trcount = 0 data_resids = [] for task in taskNames: X = loadTaskTiming(subj, task, taskModel=taskModel, nRegsFIR=25) taskRegs = X['taskRegressors'] trstart = trcount trend = trstart + taskRegs.shape[0] betas, resid = regression.regression(data[:, trstart:trend].T, taskRegs, constant=True) betas = betas.T # Exclude nuisance regressors residual_ts = resid.T data_resids.extend(residual_ts.T) trcount = trend # Append the rest of the resting-state time series, just in case # trstart = trcount # data_resids.extend(data[:,trstart:].T) data_resids = np.asarray(data_resids) residual_ts = data_resids.T h5f = h5py.File(outputdir + subj + '_glmOutput_data.h5', 'a') outname1 = 'taskRegression/' + restname + '_' + nuisModel + '_taskReg_resid_' + taskModel try: h5f.create_dataset(outname1, data=residual_ts) except: del h5f[outname1] h5f.create_dataset(outname1, data=residual_ts) h5f.close() return residual_ts