def test_first_level_models_with_no_signal_scaling(): """ test to ensure that the FirstLevelModel works correctly with a signal_scaling==False. In particular, that derived theta are correct for a constant design matrix with a single valued fmri image """ shapes, rk = [(3, 1, 1, 2)], 1 fmri_data = list() design_matrices = list() design_matrices.append( pd.DataFrame(np.ones((shapes[0][-1], rk)), columns=list('abcdefghijklmnopqrstuvwxyz')[:rk])) first_level_model = FirstLevelModel(mask_img=False, noise_model='ols', signal_scaling=False) fmri_data.append(Nifti1Image(np.zeros((1, 1, 1, 2)) + 6, np.eye(4))) first_level_model.fit(fmri_data, design_matrices=design_matrices) # trivial test of signal_scaling value assert first_level_model.signal_scaling is False # assert that our design matrix has one constant assert first_level_model.design_matrices_[0].equals( pd.DataFrame([1.0, 1.0], columns=['a'])) # assert that we only have one theta as there is only on voxel in our image assert first_level_model.results_[0][0].theta.shape == (1, 1) # assert that the theta is equal to the one voxel value assert_almost_equal(first_level_model.results_[0][0].theta[0, 0], 6.0, 2)
def do_fmri_model(slabel, runs, funcfiles, rpfiles, timings, outputdir): tr = 1.810 # repetition time #n_scans = nibabel.load(r).shape[3] #frame_times = np.arange(n_scans) * tr fmri_glm = FirstLevelModel( t_r=1.810, hrf_model='spm', #noise_model='ar1', noise_model='ols', mask='mask_ICV.nii', drift_model='polynomial', drift_order=3, smoothing_fwhm=None, period_cut=128, verbose=0, #minimize_memory=True, memory=CACHE) events_list = [] confounds_list = [] for run, img, rp in zip(runs, funcfiles, rpfiles): print(f'Adding run {run} : {img}') onsets = timings.time[timings.block == run] / 1000.0 conds = timings.condphase[timings.block == run] durations = timings.duration[timings.block == run] durations[timings.phase != 'GreyScreen'] = 0.0 events = pd.DataFrame({ 'trial_type': conds, 'onset': onsets, 'duration': durations }) events.to_csv('example.csv') events_list.append(events) if len(events.trial_type.unique()) != 16: print("Did not find 16 types of events:") print(events.trial_type.unique()) if not op.isfile(rp): print(f"Could not find {rp}") motions = pd.DataFrame(np.loadtxt(rp)) confounds_list.append(motions) #hv_confounds = pd.DataFrame(high_variance_confounds(img, percentile=1)) #confounds_list.append(pd.concat([motions, hv_confounds])) print("Fitting...") fmri_glm.fit(funcfiles, events=events_list, confounds=confounds_list) # save design matrices for ix, dmtx in enumerate(fmri_glm.design_matrices_): dmtx.to_csv(outputdir + f'design_matrix{runs[ix]}.csv') print(" done") return fmri_glm
def test_explicit_fixed_effects(): """ tests the fixed effects performed manually/explicitly""" with InTemporaryDirectory(): shapes, rk = ((7, 8, 7, 15), (7, 8, 7, 16)), 3 mask, fmri_data, design_matrices = _write_fake_fmri_data(shapes, rk) contrast = np.eye(rk)[1] # session 1 multi_session_model = FirstLevelModel(mask_img=mask).fit( fmri_data[0], design_matrices=design_matrices[:1]) dic1 = multi_session_model.compute_contrast(contrast, output_type='all') # session 2 multi_session_model.fit(fmri_data[1], design_matrices=design_matrices[1:]) dic2 = multi_session_model.compute_contrast(contrast, output_type='all') # fixed effects model multi_session_model.fit(fmri_data, design_matrices=design_matrices) fixed_fx_dic = multi_session_model.compute_contrast(contrast, output_type='all') # manual version contrasts = [dic1['effect_size'], dic2['effect_size']] variance = [dic1['effect_variance'], dic2['effect_variance']] ( fixed_fx_contrast, fixed_fx_variance, fixed_fx_stat, ) = compute_fixed_effects(contrasts, variance, mask) assert_almost_equal(fixed_fx_contrast.get_data(), fixed_fx_dic['effect_size'].get_data()) assert_almost_equal(fixed_fx_variance.get_data(), fixed_fx_dic['effect_variance'].get_data()) assert_almost_equal(fixed_fx_stat.get_data(), fixed_fx_dic['stat'].get_data()) # test without mask variable ( fixed_fx_contrast, fixed_fx_variance, fixed_fx_stat, ) = compute_fixed_effects(contrasts, variance) assert_almost_equal(fixed_fx_contrast.get_data(), fixed_fx_dic['effect_size'].get_data()) assert_almost_equal(fixed_fx_variance.get_data(), fixed_fx_dic['effect_variance'].get_data()) assert_almost_equal(fixed_fx_stat.get_data(), fixed_fx_dic['stat'].get_data()) # ensure that using unbalanced effects size and variance images # raises an error with pytest.raises(ValueError): compute_fixed_effects(contrasts * 2, variance, mask) del mask, multi_session_model
def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['type'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout(os.path.join(derivatives, 'spynoza')) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['type'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] mask = derivatives_layout.get(subject=subject, session=session, type='mask', return_type='file')[0] mask = image.math_img('(im > .5).astype(int)', im=mask) print(mask) row = bold.iloc[0] results_dir = os.path.join(derivatives, 'modelfitting_av', 'glm2', 'sub-{}'.format(row['subject'])) os.makedirs(results_dir, exist_ok=True) av_bold_fn = os.path.join( results_dir, 'sub-{}_ses-{}_bold_average.nii.gz'.format(row['subject'], row['session'])) av_bold = average_over_runs(bold.path.tolist(), output_filename=av_bold_fn) av_bold = image.math_img('(av_bold / av_bold.mean(-1)[..., np.newaxis])', av_bold=av_bold) av_bold.to_filename(av_bold_fn) model = FirstLevelModel(t_r=4, mask=mask, drift_model=None) paradigm = pd.read_table(events.iloc[0]['path']) model.fit(av_bold, paradigm) left_right = model.compute_contrast('eye_L - eye_R', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'], ))) left_right = model.compute_contrast('eye_L - eye_R', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_psc.nii.gz'.format( row['subject'], row['session'])))
def fit_first_level(imgs, confounds, dmtxs, mask, atlas, dim): mem = Memory(location=expanduser('cache')) masker, masker_geom = make_masker(atlas, dim, mask) model = FirstLevelModel(mask=masker, t_r=2., memory=mem, memory_level=1, period_cut=128, signal_scaling=False) model.fit(imgs, confounds=confounds, design_matrices=dmtxs) return model
def test_first_level_model_residuals(): shapes, rk = [(10, 10, 10, 100)], 3 mask, fmri_data, design_matrices = _generate_fake_fmri_data(shapes, rk) for i in range(len(design_matrices)): design_matrices[i].iloc[:, 0] = 1 model = FirstLevelModel(mask=mask, minimize_memory=False, noise_model='ols') model.fit(fmri_data, design_matrices=design_matrices) residuals = model.residuals[0] mean_residuals = model.masker_.transform(residuals).mean(0) assert_array_almost_equal(mean_residuals, 0)
def test_first_level_model_design_creation(): # Test processing of FMRI inputs with InTemporaryDirectory(): shapes = ((7, 8, 9, 10),) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3) model = model.fit(func_img, events) frame1, X1, names1 = check_design_matrix(model.design_matrices_[0]) # check design computation is identical n_scans = func_img.get_data().shape[3] start_time = slice_time_ref * t_r end_time = (n_scans - 1 + slice_time_ref) * t_r frame_times = np.linspace(start_time, end_time, n_scans) design = make_first_level_design_matrix(frame_times, events, drift_model='polynomial', drift_order=3) frame2, X2, names2 = check_design_matrix(design) assert_array_equal(frame1, frame2) assert_array_equal(X1, X2) assert_array_equal(names1, names2) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del FUNCFILE, mask, model, func_img
def report_flm_fiac(): # pragma: no cover data = nistats_datasets.fetch_fiac_first_level() fmri_img = [data['func1'], data['func2']] from nilearn.image import mean_img mean_img_ = mean_img(fmri_img[0]) design_files = [data['design_matrix1'], data['design_matrix2']] design_matrices = [pd.DataFrame(np.load(df)['X']) for df in design_files] fmri_glm = FirstLevelModel(mask_img=data['mask'], minimize_memory=True) fmri_glm = fmri_glm.fit(fmri_img, design_matrices=design_matrices) n_columns = design_matrices[0].shape[1] contrasts = { 'SStSSp_minus_DStDSp': _pad_vector([1, 0, 0, -1], n_columns), 'DStDSp_minus_SStSSp': _pad_vector([-1, 0, 0, 1], n_columns), 'DSt_minus_SSt': _pad_vector([-1, -1, 1, 1], n_columns), 'DSp_minus_SSp': _pad_vector([-1, 1, -1, 1], n_columns), 'DSt_minus_SSt_for_DSp': _pad_vector([0, -1, 0, 1], n_columns), 'DSp_minus_SSp_for_DSt': _pad_vector([0, 0, -1, 1], n_columns), 'Deactivation': _pad_vector([-1, -1, -1, -1, 4], n_columns), 'Effects_of_interest': np.eye(n_columns)[:5] } report = make_glm_report( fmri_glm, contrasts, bg_img=mean_img_, height_control='fdr', ) output_filename = 'generated_report_flm_fiac.html' output_filepath = os.path.join(REPORTS_DIR, output_filename) report.save_as_html(output_filepath) report.get_iframe()
def process_subject(inputpath, subjid, dtx_mat, outputpath): subjglm = op.join(outputpath, "cache", "glm_{}".format(subjid)) subjid = str(subjid) if op.isfile(subjglm): print('Loading already saved model {}'.format(subjglm)) fmri_glm = load(subjglm) # the model has already been estimated else: # else, we create and estimate it print('Creating model for subject %s' % subjid) imgs = sorted( glob.glob(op.join(inputpath, subjid, "func", "res*_medn_afw.nii"))) if len(imgs) != 9: print("WARNING: %s does not have 9 sessions. We skip it." % subjid) return fmri_glm = FirstLevelModel( t_r=2.0, hrf_model='spm', # mask='mask_ICV.nii', noise_model='ar1', period_cut=128.0, smoothing_fwhm=0, minimize_memory=True, # memory='/mnt/ephemeral/cache', memory=None, verbose=2, n_jobs=1) # creating and estimating the model fmri_glm = fmri_glm.fit(imgs, design_matrices=dtx_mat) # saving it as a pickle object dump(fmri_glm, subjglm) # creating the maps for each individual predictor # this assumes the same predictors for each session print('Computing contrasts for subject %s', subjid) contrasts = {} con_names = [i for i in dtx_mat[0].columns] ncon = len(con_names) con = np.eye(ncon) for i, name in enumerate(con_names): contrasts[name] = con[i, :] for name, val in contrasts.items(): z_map = fmri_glm.compute_contrast(val, output_type='z_score') eff_map = fmri_glm.compute_contrast(val, output_type='effect_size') #std_map = fmri_glm.compute_contrast(val, output_type='stddev') nib.save(z_map, op.join(outputpath, '%s_%s_zmap.nii.gz' % (name, subjid))) nib.save(eff_map, op.join(outputpath, '%s_%s_effsize.nii.gz' % (name, subjid))) display = None display = plot_glass_brain(z_map, display_mode='lzry', threshold=3.1, colorbar=True, title=name) display.savefig( op.join(outputpath, '%s_%s_glassbrain.png' % (name, subjid))) display.close()
def test_first_level_model_design_creation(): # Test processing of FMRI inputs with InTemporaryDirectory(): shapes = ((7, 8, 9, 10), ) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask, drift_model='polynomial', drift_order=3) model = model.fit(func_img, events) frame1, X1, names1 = check_design_matrix(model.design_matrices_[0]) # check design computation is identical n_scans = get_data(func_img).shape[3] start_time = slice_time_ref * t_r end_time = (n_scans - 1 + slice_time_ref) * t_r frame_times = np.linspace(start_time, end_time, n_scans) design = make_first_level_design_matrix(frame_times, events, drift_model='polynomial', drift_order=3) frame2, X2, names2 = check_design_matrix(design) assert_array_equal(frame1, frame2) assert_array_equal(X1, X2) assert_array_equal(names1, names2) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del FUNCFILE, mask, model, func_img
def test_first_level_model_design_creation(): # Test processing of FMRI inputs with InTemporaryDirectory(): shapes = ((7, 8, 9, 10),) mask, FUNCFILE, _ = write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 1.0 slice_time_ref = 0. paradigm = basic_paradigm() model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3) model = model.fit(func_img, paradigm) frame1, X1, names1 = check_design_matrix(model.design_matrices_[0]) # check design computation is identical n_scans = func_img.get_data().shape[3] start_time = slice_time_ref * t_r end_time = (n_scans - 1 + slice_time_ref) * t_r frame_times = np.linspace(start_time, end_time, n_scans) design = make_design_matrix(frame_times, paradigm, drift_model='polynomial', drift_order=3) frame2, X2, names2 = check_design_matrix(design) assert_array_equal(frame1, frame2) assert_array_equal(X1, X2) assert_array_equal(names1, names2)
def test_first_level_model_contrast_computation(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10), ) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() # Ordinary Least Squares case model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask, drift_model='polynomial', drift_order=3, minimize_memory=False) c1, c2, cnull = np.eye(7)[0], np.eye(7)[1], np.zeros(7) # asking for contrast before model fit gives error with pytest.raises(ValueError): model.compute_contrast(c1) # fit model model = model.fit([func_img, func_img], [events, events]) # smoke test for different contrasts in fixed effects model.compute_contrast([c1, c2]) # smoke test for same contrast in fixed effects model.compute_contrast([c2, c2]) # smoke test for contrast that will be repeated model.compute_contrast(c2) model.compute_contrast(c2, 'F') model.compute_contrast(c2, 't', 'z_score') model.compute_contrast(c2, 't', 'stat') model.compute_contrast(c2, 't', 'p_value') model.compute_contrast(c2, None, 'effect_size') model.compute_contrast(c2, None, 'effect_variance') # formula should work (passing varible name directly) model.compute_contrast('c0') model.compute_contrast('c1') model.compute_contrast('c2') # smoke test for one null contrast in group model.compute_contrast([c2, cnull]) # only passing null contrasts should give back a value error with pytest.raises(ValueError): model.compute_contrast(cnull) with pytest.raises(ValueError): model.compute_contrast([cnull, cnull]) # passing wrong parameters with pytest.raises(ValueError): model.compute_contrast([]) with pytest.raises(ValueError): model.compute_contrast([c1, []]) with pytest.raises(ValueError): model.compute_contrast(c1, '', '') with pytest.raises(ValueError): model.compute_contrast(c1, '', []) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del func_img, FUNCFILE, model
def test_first_level_glm_computation_with_memory_caching(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10),) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # initialize FirstLevelModel with memory option enabled t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() # Ordinary Least Squares case model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3, memory='nilearn_cache', memory_level=1, minimize_memory=False) model.fit(func_img, events) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del mask, func_img, FUNCFILE, model
def test_first_level_glm_computation_with_memory_caching(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10), ) mask, FUNCFILE, _ = write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # initialize FirstLevelModel with memory option enabled t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() # Ordinary Least Squares case model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3, memory='nilearn_cache', memory_level=1, minimize_memory=False) model.fit(func_img, events)
def fit_subject(sub, space): funcs = sorted( glob( f'../derivatives/fmriprep/{sub}/ses-*/func/*task-flocBLOCKED*space-{space}*desc-preproc_bold.nii.gz' )) masks = [f.replace('preproc_bold', 'brain_mask') for f in funcs] mask = masking.intersect_masks(masks, threshold=0.9) conf_files = [ f.split('space')[0] + 'desc-confounds_regressors.tsv' for f in funcs ] ccols = ['trans_x', 'trans_y', 'trans_z', 'rot_x', 'rot_y', 'rot_z'] confs = [pd.read_csv(c, sep='\t').loc[:, ccols] for c in conf_files] events = [ f"../{sub}/ses{f.split('ses')[1].split('func')[0]}/func/{op.basename(f).split('desc')[0]}events.tsv" for f in conf_files ] flm = FirstLevelModel(t_r=0.7, slice_time_ref=0.5, drift_model='cosine', high_pass=0.01, mask_img=mask, smoothing_fwhm=3.5, noise_model='ols', verbose=True) flm.fit(funcs, events, confs) con_defs = [('face', '4*face - object - character - body - place'), ('place', '4*place - object - face - character - body'), ('body', '4*body - object - face - character - place'), ('character', '4*character - object - face - place - body')] for name, df in con_defs: roi = flm.compute_contrast(df) f_out = f'../derivatives/floc/{sub}/rois/{sub}_task-flocBLOCKED_space-{space}_desc-{name}_zscore.nii.gz' if not op.isdir(op.dirname(f_out)): os.makedirs(op.dirname(f_out)) roi.to_filename(f_out)
def test_first_level_model_predictions_r_square(): shapes, rk = [(10, 10, 10, 25)], 3 mask, fmri_data, design_matrices = _generate_fake_fmri_data(shapes, rk) for i in range(len(design_matrices)): design_matrices[i].iloc[:, 0] = 1 model = FirstLevelModel(mask=mask, signal_scaling=False, minimize_memory=False, noise_model='ols') model.fit(fmri_data, design_matrices=design_matrices) pred = model.predicted[0] data = fmri_data[0] r_square_3d = model.r_square[0] y_predicted = model.masker_.transform(pred) y_measured = model.masker_.transform(data) assert_almost_equal(np.mean(y_predicted - y_measured), 0) r_square_2d = model.masker_.transform(r_square_3d) assert_array_less(0., r_square_2d)
def test_first_level_models_with_no_signal_scaling(): """ test to ensure that the FirstLevelModel works correctly with a signal_scaling==False. In particular, that derived theta are correct for a constant design matrix with a single valued fmri image """ shapes, rk = [(3, 1, 1, 2)], 1 fmri_data = list() design_matrices = list() design_matrices.append(pd.DataFrame(np.ones((shapes[0][-1], rk)), columns=list('abcdefghijklmnopqrstuvwxyz')[:rk])) first_level_model = FirstLevelModel(mask=False, noise_model='ols', signal_scaling=False) fmri_data.append(Nifti1Image(np.zeros((1, 1, 1, 2)) + 6, np.eye(4))) first_level_model.fit(fmri_data, design_matrices=design_matrices) # trivial test of signal_scaling value assert_true(first_level_model.signal_scaling is False) # assert that our design matrix has one constant assert_true(first_level_model.design_matrices_[0].equals( pd.DataFrame([1.0, 1.0], columns=['a']))) # assert that we only have one theta as there is only on voxel in our image assert_true(first_level_model.results_[0][0].theta.shape == (1, 1)) # assert that the theta is equal to the one voxel value assert_almost_equal(first_level_model.results_[0][0].theta[0, 0], 6.0, 2)
def test_first_level_model_contrast_computation(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10),) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() # Ordinary Least Squares case model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3, minimize_memory=False) c1, c2, cnull = np.eye(7)[0], np.eye(7)[1], np.zeros(7) # asking for contrast before model fit gives error assert_raises(ValueError, model.compute_contrast, c1) # fit model model = model.fit([func_img, func_img], [events, events]) # smoke test for different contrasts in fixed effects model.compute_contrast([c1, c2]) # smoke test for same contrast in fixed effects model.compute_contrast([c2, c2]) # smoke test for contrast that will be repeated model.compute_contrast(c2) model.compute_contrast(c2, 'F') model.compute_contrast(c2, 't', 'z_score') model.compute_contrast(c2, 't', 'stat') model.compute_contrast(c2, 't', 'p_value') model.compute_contrast(c2, None, 'effect_size') model.compute_contrast(c2, None, 'effect_variance') # formula should work (passing varible name directly) model.compute_contrast('c0') model.compute_contrast('c1') model.compute_contrast('c2') # smoke test for one null contrast in group model.compute_contrast([c2, cnull]) # only passing null contrasts should give back a value error assert_raises(ValueError, model.compute_contrast, cnull) assert_raises(ValueError, model.compute_contrast, [cnull, cnull]) # passing wrong parameters assert_raises(ValueError, model.compute_contrast, []) assert_raises(ValueError, model.compute_contrast, [c1, []]) assert_raises(ValueError, model.compute_contrast, c1, '', '') assert_raises(ValueError, model.compute_contrast, c1, '', []) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del func_img, FUNCFILE, model
def test_first_level_model_contrast_computation(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10), ) mask, FUNCFILE, _ = write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 1.0 slice_time_ref = 0. paradigm = basic_paradigm() # ols case model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3, minimize_memory=False) c1, c2, cnull = np.eye(7)[0], np.eye(7)[1], np.zeros(7) # asking for contrast before model fit gives error assert_raises(ValueError, model.compute_contrast, c1) # fit model model = model.fit([func_img, func_img], [paradigm, paradigm]) # smoke test for different contrasts in fixed effects model.compute_contrast([c1, c2]) # smoke test for same contrast in fixed effects model.compute_contrast([c2, c2]) # smoke test for contrast that will be repeated model.compute_contrast(c2) model.compute_contrast(c2, 'F') model.compute_contrast(c2, 't', 'z_score') model.compute_contrast(c2, 't', 'stat') model.compute_contrast(c2, 't', 'p_value') model.compute_contrast(c2, None, 'effect_size') model.compute_contrast(c2, None, 'effect_variance') # formula should work (passing varible name directly) model.compute_contrast('c0') model.compute_contrast('c1') model.compute_contrast('c2') # smoke test for one null contrast in group model.compute_contrast([c2, cnull]) # only passing null contrasts should give back a value error assert_raises(ValueError, model.compute_contrast, cnull) assert_raises(ValueError, model.compute_contrast, [cnull, cnull]) # passing wrong parameters assert_raises(ValueError, model.compute_contrast, []) assert_raises(ValueError, model.compute_contrast, [c1, []]) assert_raises(ValueError, model.compute_contrast, c1, '', '') assert_raises(ValueError, model.compute_contrast, c1, '', [])
def test_first_level_model_glm_computation(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10),) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() # Ordinary Least Squares case model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask, drift_model='polynomial', drift_order=3, minimize_memory=False) model = model.fit(func_img, events) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del mask, FUNCFILE, func_img, model
def report_flm_adhd_dmn(): # pragma: no cover t_r = 2. slice_time_ref = 0. n_scans = 176 pcc_coords = (0, -53, 26) adhd_dataset = nilearn.datasets.fetch_adhd(n_subjects=1) seed_masker = NiftiSpheresMasker([pcc_coords], radius=10, detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2., memory='nilearn_cache', memory_level=1, verbose=0) seed_time_series = seed_masker.fit_transform(adhd_dataset.func[0]) frametimes = np.linspace(0, (n_scans - 1) * t_r, n_scans) design_matrix = make_first_level_design_matrix(frametimes, hrf_model='spm', add_regs=seed_time_series, add_reg_names=["pcc_seed"]) dmn_contrast = np.array([1] + [0] * (design_matrix.shape[1] - 1)) contrasts = {'seed_based_glm': dmn_contrast} first_level_model = FirstLevelModel(t_r=t_r, slice_time_ref=slice_time_ref) first_level_model = first_level_model.fit(run_imgs=adhd_dataset.func[0], design_matrices=design_matrix) report = make_glm_report( first_level_model, contrasts=contrasts, title='ADHD DMN Report', cluster_threshold=15, height_control='bonferroni', min_distance=8., plot_type='glass', report_dims=(1200, 'a'), ) output_filename = 'generated_report_flm_adhd_dmn.html' output_filepath = os.path.join(REPORTS_DIR, output_filename) report.save_as_html(output_filepath) report.get_iframe()
def test_first_level_model_glm_computation(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10),) mask, FUNCFILE, _ = write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 1.0 slice_time_ref = 0. paradigm = basic_paradigm() # ols case model = FirstLevelModel(t_r, slice_time_ref, mask=mask, drift_model='polynomial', drift_order=3, minimize_memory=False) model = model.fit(func_img, paradigm) labels1 = model.labels_[0] results1 = model.results_[0] labels2, results2 = run_glm( model.masker_.transform(func_img), model.design_matrices_[0].as_matrix(), 'ar1')
def test_first_level_model_glm_computation(): with InTemporaryDirectory(): shapes = ((7, 8, 9, 10), ) mask, FUNCFILE, _ = _write_fake_fmri_data(shapes) FUNCFILE = FUNCFILE[0] func_img = load(FUNCFILE) # basic test based on basic_paradigm and glover hrf t_r = 10.0 slice_time_ref = 0. events = basic_paradigm() # Ordinary Least Squares case model = FirstLevelModel(t_r, slice_time_ref, mask_img=mask, drift_model='polynomial', drift_order=3, minimize_memory=False) model = model.fit(func_img, events) # Delete objects attached to files to avoid WindowsError when deleting # temporary directory (in Windows) del mask, FUNCFILE, func_img, model
memory='nilearn_cache', memory_level=1, verbose=0) seed_time_series = seed_masker.fit_transform(adhd_dataset.func[0]) frametimes = np.linspace(0, (n_scans - 1) * t_r, n_scans) design_matrix = make_first_level_design_matrix(frametimes, hrf_model='spm', add_regs=seed_time_series, add_reg_names=["pcc_seed"]) dmn_contrast = np.array([1] + [0]*(design_matrix.shape[1]-1)) contrasts = {'seed_based_glm': dmn_contrast} ######################################################################### # Perform first level analysis # ---------------------------- # Setup and fit GLM first_level_model = FirstLevelModel(t_r=t_r, slice_time_ref=slice_time_ref) first_level_model = first_level_model.fit(run_imgs=adhd_dataset.func[0], design_matrices=design_matrix) ######################################################################### # contrast estimation print('Contrast seed_based_glm computed.') z_map = first_level_model.compute_contrast(contrasts['seed_based_glm'], output_type='z_score') # Saving snapshots of the contrasts filename = 'dmn_z_map.png' display = plotting.plot_stat_map(z_map, threshold=3.0, title='Seed based GLM', cut_coords=pcc_coords) display.add_markers(marker_coords=[pcc_coords], marker_color='g',marker_size=300) display.savefig(filename) print("Save z-map in '{0}'.".format(filename))
def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['type'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout(os.path.join(derivatives, 'spynoza')) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['type'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] mask = derivatives_layout.get(subject=subject, session=session, type='mask', return_type='file')[0] mask = image.math_img('(im > .5).astype(int)', im=mask) print(mask) df = events.merge(bold, on=['subject', 'session', 'run'], suffixes=('_events', '_bold')) models = [] for ix, row in df.iterrows(): results_dir = os.path.join(derivatives, 'modelfitting', 'sub-{}'.format(row['subject'])) if 'session' in row: results_dir = os.path.join(results_dir, 'ses-{}'.format(row['session'])) os.makedirs(results_dir, exist_ok=True) print('Fitting {}'.format(row['path_bold'])) model = FirstLevelModel(t_r=4, mask=mask) paradigm = pd.read_table(row['path_events']) paradigm_short = paradigm.copy() paradigm_short['duration'] = 1 paradigm_short['trial_type'] = paradigm_short['trial_type'].map( lambda x: '{}_instant'.format(x)) paradigm = pd.concat((paradigm, paradigm_short)) model.fit(row['path_bold'], paradigm) left_right = model.compute_contrast('eye_L - eye_R', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) left_right = model.compute_contrast('eye_L - eye_R', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_psc.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_l_instant = model.compute_contrast('eye_L_instant', output_type='z_score') eye_l_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_l_instant_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_l_instant = model.compute_contrast('eye_L_instant', output_type='effect_size') eye_l_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_l_instant_effect_size.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_r_instant = model.compute_contrast('eye_R_instant', output_type='z_score') eye_r_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_r_instant_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) eye_r_instant = model.compute_contrast('eye_R_instant', output_type='effect_size') eye_r_instant.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_eye_R_instant_effect_size.nii.gz'.format( row['subject'], row['session'], row['run']))) models.append(model) second_level_model = SecondLevelModel(mask=mask) second_level_model.fit(models) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='effect_size') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_effect_size.nii.gz'.format( row['subject'], row['session'])))
# -------------------------------------- data = datasets.fetch_fiac_first_level() fmri_img = [data['func1'], data['func2']] mean_img_ = mean_img(fmri_img[0]) design_files = [data['design_matrix1'], data['design_matrix2']] design_matrices = [pd.DataFrame(np.load(df)['X']) for df in design_files] ######################################################################### # GLM estimation # ---------------------------------- # GLM specification fmri_glm = FirstLevelModel(mask=data['mask'], minimize_memory=True) ######################################################################### # GLM fitting fmri_glm = fmri_glm.fit(fmri_img, design_matrices=design_matrices) ######################################################################### # compute fixed effects of the two runs and compute related images n_columns = design_matrices[0].shape[1] def pad_vector(contrast_, n_columns): return np.hstack((contrast_, np.zeros(n_columns - len(contrast_)))) contrasts = { 'SStSSp_minus_DStDSp': pad_vector([1, 0, 0, -1], n_columns), 'DStDSp_minus_SStSSp': pad_vector([-1, 0, 0, 1], n_columns), 'DSt_minus_SSt': pad_vector([-1, -1, 1, 1], n_columns), 'DSp_minus_SSp': pad_vector([-1, 1, -1, 1], n_columns),
def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['type'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout( os.path.join(derivatives, 'spynoza_mc_mutualinfo')) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['type'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] confounds = derivatives_df[(derivatives_df['type'] == 'confounds') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] print(derivatives_df.type.unique()) mask = derivatives_layout.get(subject=subject, session=session, type='mask', return_type='file')[0] df = events.merge(bold, on=['subject', 'session', 'run'], suffixes=('_events', '_bold')) confounds = confounds.rename(columns={'path': 'confounds'}) df = df.merge(confounds[['subject', 'session', 'run', 'confounds']]) models = [] for ix, row in df.iterrows(): results_dir = os.path.join(derivatives, 'modelfitting', 'glm4', 'sub-{}'.format(row['subject'])) if 'session' in row: results_dir = os.path.join(results_dir, 'ses-{}'.format(row['session'])) os.makedirs(results_dir, exist_ok=True) confounds = pd.read_table(row.confounds).fillna(method='bfill') print('Fitting {}'.format(row['path_bold'])) model = FirstLevelModel(t_r=4, mask=mask) paradigm = pd.read_table(row['path_events']) model.fit(row['path_bold'], paradigm, confounds=confounds) left_right = model.compute_contrast('eye_L - eye_R', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'], row['run']))) left_right = model.compute_contrast('eye_L - eye_R', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_run-{}_left_over_right_psc.nii.gz'.format( row['subject'], row['session'], row['run']))) models.append(model) second_level_model = SecondLevelModel(mask=mask) second_level_model.fit(models) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='effect_size') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_effect_size.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye_L - eye_R', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session'])))
t_r = 2.4 events_file = data['events'] import pandas as pd events= pd.read_table(events_file) ############################################################################### # Running a basic model # --------------------- # # First specify a linear model. # the fit() model creates the design matrix and the beta maps. # from nistats.first_level_model import FirstLevelModel first_level_model = FirstLevelModel(t_r) first_level_model = first_level_model.fit(fmri_img, events=events) design_matrix = first_level_model.design_matrices_[0] ######################################################################### # Let us take a look at the design matrix: it has 10 main columns corresponding to 10 experimental conditions, followed by 3 columns describing low-frequency signals (drifts) and a constant regressor. from nistats.reporting import plot_design_matrix plot_design_matrix(design_matrix) import matplotlib.pyplot as plt plt.show() ######################################################################### # Specification of the contrasts. # # For this, let's create a function that, given the design matrix, # generates the corresponding contrasts. This will be useful to # repeat contrast specification when we change the design matrix.
def main(subject, sourcedata, derivatives): source_layout = BIDSLayout(sourcedata, validate=False, derivatives=False) fmriprep_layout = BIDSLayout(op.join(derivatives, 'fmriprep'), validate=False) bold = fmriprep_layout.get( subject=subject, suffix='bold', description='preproc', extension='nii.gz', ) bold = sorted([e for e in bold if 'MNI' in e.filename], key=lambda x: x.run) reg = re.compile('.*_space-(?P<space>.+)_desc.*') fmriprep_layout_df = fmriprep_layout.to_df() fmriprep_layout_df = fmriprep_layout_df[~fmriprep_layout_df.subject.isnull( )] fmriprep_layout_df['subject'] = fmriprep_layout_df.subject.astype(int) fmriprep_layout_df = fmriprep_layout_df[np.in1d( fmriprep_layout_df.suffix, ['bold', 'regressors', 'mask'])] fmriprep_layout_df = fmriprep_layout_df[np.in1d( fmriprep_layout_df.extension, ['nii.gz', 'tsv'])] fmriprep_layout_df['space'] = fmriprep_layout_df.path.apply( lambda path: reg.match(path).group(1) if reg.match(path) else None) fmriprep_layout_df = fmriprep_layout_df.set_index( ['subject', 'run', 'suffix', 'space']) events_df = source_layout.to_df() events_df = events_df[events_df.suffix == 'events'] events_df['subject'] = events_df['subject'].astype(int) events_df = events_df.set_index(['subject', 'run']) tr = source_layout.get_tr(bold[0].path) for b in bold: run = b.entities['run'] print(run) confounds_ = fmriprep_layout_df.loc[(subject, run, 'regressors'), 'path'].iloc[0] confounds_ = pd.read_csv(confounds_, sep='\t') confounds_ = confounds_[to_include].fillna(method='bfill') events_ = events_df.loc[(subject, run), 'path'] events_ = pd.read_csv(events_, sep='\t') events_['trial_type'] = events_['trial_type'].apply( lambda x: 'stim2' if x.startswith('stim2') else x) model = FirstLevelModel(tr, drift_model=None, n_jobs=5, smoothing_fwhm=4.0) pca = PCA(n_components=7) confounds_ -= confounds_.mean(0) confounds_ /= confounds_.std(0) confounds_pca = pca.fit_transform(confounds_[to_include]) events_['onset'] += tr model.fit(b.path, events_, confounds_pca) base_dir = op.join(derivatives, 'glm_stim1', f'sub-{subject}', 'func') if not op.exists(base_dir): os.makedirs(base_dir) # PE ims = [] for stim in 5, 7, 10, 14, 20, 28: im = model.compute_contrast(f'stim1-{stim}', output_type='effect_size') ims.append(im) ims = image.concat_imgs(ims) ims.to_filename( op.join(base_dir, f'sub-{subject}_run-{run}_desc-stims1_pe.nii.gz')) # zmap ims = [] for stim in 5, 7, 10, 14, 20, 28: im = model.compute_contrast(f'stim1-{stim}', output_type='z_score') ims.append(im) ims = image.concat_imgs(ims) ims.to_filename( op.join(base_dir, f'sub-{subject}_run-{run}_desc-stims1_zmap.nii.gz'))
# * t_r=7(s) is the time of repetition of acquisitions # * noise_model='ar1' specifies the noise covariance model: a lag-1 dependence # * standardize=False means that we do not want to rescale the time series to mean 0, variance 1 # * hrf_model='spm' means that we rely on the SPM "canonical hrf" model (without time or dispersion derivatives) # * drift_model='cosine' means that we model the signal drifts as slow oscillating time functions # * period_cut=160(s) defines the cutoff frequency (its inverse actually). fmri_glm = FirstLevelModel(t_r=7, noise_model='ar1', standardize=False, hrf_model='spm', drift_model='cosine', period_cut=160) ############################################################################### # Now that we have specified the model, we can run it on the fMRI image fmri_glm = fmri_glm.fit(fmri_img, events) ############################################################################### # One can inspect the design matrix (rows represent time, and # columns contain the predictors). design_matrix = fmri_glm.design_matrices_[0] ############################################################################### # Formally, we have taken the first design matrix, because the model is # implictily meant to for multiple runs. from nistats.reporting import plot_design_matrix plot_design_matrix(design_matrix) import matplotlib.pyplot as plt plt.show() ###############################################################################
masks = [f.replace('preproc_bold', 'brain_mask') for f in funcs] events = sorted(glob(f'../{sub}/ses-*/func/*task-face*events.tsv')) cols = ['rot_x', 'rot_y', 'rot_z', 'trans_x', 'trans_y', 'trans_z'] confs = [pd.read_csv(c, sep='\t').loc[:, cols] for c in confs] events = [pd.read_csv(e, sep='\t').drop('trial_type', axis=1).rename({'expression': 'trial_type'}, axis=1) for e in events] events = [e.loc[~e['trial_type'].isna(), :] for e in events] print(events) mask = masking.intersect_masks(masks, threshold=1) flm = FirstLevelModel( t_r=0.7, slice_time_ref=0.5, hrf_model='glover', drift_model='cosine', high_pass=0.01, noise_model='ols', mask_img=mask, verbose=True, smoothing_fwhm=4, n_jobs=10 ) flm.fit(funcs, confounds=confs, events=events) con = flm.compute_contrast('smiling - neutral') #roi = image.resample_to_img(roi, con, interpolation='nearest') con.to_filename(f'../derivatives/{sub}_smilingGTneutral.nii.gz') #R.append(con) #R = image.concat_imgs(R) #R.to_filename('../derivatives/maleGTfemale.nii.gz')
design_files = [data['design_matrix1'], data['design_matrix2']] import pandas as pd import numpy as np design_matrices = [pd.DataFrame(np.load(df)['X']) for df in design_files] ######################################################################### # GLM estimation # ---------------------------------- # GLM specification. Note that the mask was provided in the dataset. So we use it. from nistats.first_level_model import FirstLevelModel fmri_glm = FirstLevelModel(mask_img=data['mask'], minimize_memory=True) ######################################################################### # Let's fit the GLM. fmri_glm = fmri_glm.fit(fmri_img, design_matrices=design_matrices) ######################################################################### # Compute fixed effects of the two runs and compute related images. # For this, we first define the contrasts as we would do for a single session. n_columns = design_matrices[0].shape[1] def pad_vector(contrast_, n_columns): """A small routine to append zeros in contrast vectors""" return np.hstack((contrast_, np.zeros(n_columns - len(contrast_)))) ######################################################################### # Contrast specification
# spanning the two conditions. contrasts = { 'faces-scrambled': basic_contrasts['faces'] - basic_contrasts['scrambled'], 'scrambled-faces': -basic_contrasts['faces'] + basic_contrasts['scrambled'], 'effects_of_interest': np.vstack((basic_contrasts['faces'], basic_contrasts['scrambled'])) } ######################################################################### # Fit the GLM -- 2 sessions. # Imports for GLM, the sepcify, then fit. from nistats.first_level_model import FirstLevelModel print('Fitting a GLM') fmri_glm = FirstLevelModel() fmri_glm = fmri_glm.fit(fmri_img, design_matrices=design_matrices) ######################################################################### # Compute contrast-related statistical maps (in z-scale), and plot them print('Computing contrasts') from nilearn import plotting # Iterate on contrasts for contrast_id, contrast_val in contrasts.items(): print("\tcontrast id: %s" % contrast_id) # compute the contrasts z_map = fmri_glm.compute_contrast( contrast_val, output_type='z_score') # plot the contrasts as soon as they're generated # the display is overlayed on the mean fMRI image # a threshold of 3.0 is used. More sophisticated choices are possible.
import numpy as np import matplotlib.pyplot as plt import nibabel from nistats.design_matrix import make_design_matrix, plot_design_matrix tr = 2.5 n_scans = nibabel.load(func_file).get_data().shape[-1] frametimes = np.arange(0, n_scans * tr, tr) design_matrix = make_design_matrix(frametimes, paradigm) plot_design_matrix(design_matrix) plt.tight_layout() # Fit GLM print('Fitting a GLM') from nistats.first_level_model import FirstLevelModel fmri_glm = FirstLevelModel(tr) fmri_glm = fmri_glm.fit(func_file, design_matrices=design_matrix) # Specify the contrasts contrasts = {} n_columns = len(design_matrix.columns) for n, name in enumerate(design_matrix.columns[:3]): contrasts[name] = np.zeros((n_columns,)) contrasts[name][n] = 1 contrasts['[motor audio] left - right'] = \ contrasts['motor_audio_left'] - contrasts['motor_audio_right'] # Compute contrast maps from nilearn import plotting for contrast_id, contrast_val in contrasts.items(): z_map = fmri_glm.compute_contrast( contrast_val, contrast_name=contrast_id, output_type='z_score')
slice_time_ref = 0.5 # Prepare data data = datasets.fetch_localizer_first_level() paradigm_file = data.paradigm paradigm = pd.read_csv(paradigm_file, sep=' ', header=None, index_col=None) paradigm.columns = ['session', 'trial_type', 'onset'] fmri_img = data.epi_img ######################################################################### # Perform first level analysis # ---------------------------- # Setup and fit GLM first_level_model = FirstLevelModel(t_r, slice_time_ref, hrf_model='glover + derivative') first_level_model = first_level_model.fit(fmri_img, paradigm) ######################################################################### # Estimate contrasts # ------------------ # Specify the contrasts design_matrix = first_level_model.design_matrices_[0] contrast_matrix = np.eye(design_matrix.shape[1]) contrasts = dict([(column, contrast_matrix[i]) for i, column in enumerate(design_matrix.columns)]) contrasts["audio"] = contrasts["clicDaudio"] + contrasts["clicGaudio"] +\ contrasts["calculaudio"] + contrasts["phraseaudio"] contrasts["video"] = contrasts["clicDvideo"] + contrasts["clicGvideo"] + \ contrasts["calculvideo"] + contrasts["phrasevideo"] contrasts["computation"] = contrasts["calculaudio"] + contrasts["calculvideo"]
# make design matrix of PC components print("Computing the PCA out of the CSF and the WM...") pca = PCA(n_components=2) pca.fit(dirty_data) frametimes = np.linspace(0, (n_scans - 1) * tr, n_scans) print("Defining the cleaning design matrix..") design_matrix = make_design_matrix(frametimes, hrf_model='spm', add_regs=pca.components_, add_reg_names=[["csfwm_pc1", "csfwm_pc2"]]) # fit a first GLM to clean the data print("Fitting the first GLM to clean the data...") cleaner = FirstLevelModel(t_r=tr, slice_time_ref=0.5, noise_model='ar1', standardize=False) cleaner.fit(run_imgs=fmri_img, design_matrices=design_matrix) dirty_fmri_img = cleaner.results_.predict(design_matrix) print("Clean the data...") fmri_img -= dirty_fmri_img ######################################################################### # Cleaning the data # extract the seed print("Extracting the average seed time serie...") seed_masker = NiftiSpheresMasker([pcc_coords], radius=10, detrend=True, standardize=True, low_pass=0.1, high_pass=0.01, t_r=2., memory='nilearn_cache', memory_level=1, verbose=1) seed_time_series = seed_masker.fit_transform(func_fname)
class Model(object): def __init__(self, img, events, t_r, regressors=None, mask=None, standardize=False, signal_scaling=0, event_index=None, first_level_kws=None): """Class for building a first-level model for the purpose of single- trial modelling. This is intended for single-trial modelling and not as a general purpose GLM class. To prevent unwanted aggregation, it only accepts one image/regressor/event/mask, rather than multiple. This is enforced and will raise a ValueError if inputs are a list of length > 1. Parameters ---------- img : niimg-like One 4D functional image. events : pandas.core.DataFrame DataFrame with 'events', 'onsets' and 'trial_type' columns. Other columns can be included as parametric modulators. regressors : pandas.core.DataFrame DataFrame with shape (number of volumes/timepoints in img, number of regressors). Each column is a separate regressor. mask : niimg-like Mask to restrict analysis, by default None which will compute a whole-brain mask. event_index : int, optional Index of single event in `events`. All other events will be labelled as 'other'. This is to isolate a single event for LSS modelling. """ self.img = img self.mask = mask regressors = _check_file(regressors) if regressors is not None: self.regressors = regressors.values self.reg_names = regressors.columns.values else: self.regressors = regressors self.reg_names = None self.events = _check_file(events) # for LSS modelling self.event_index = event_index if self.event_index is not None: ev = events.copy() idx = ev.index.isin([event_index]) ev.loc[~idx, 'trial_type'] = 'other' # to be used to index contrast during parameter extraction self._event_of_interest = ev.loc[idx, 'trial_type'].values[0] self.events = ev self.t_r = t_r self.frame_times = _compute_frame_times(self.img, self.t_r) if first_level_kws is not None: self.glm = FirstLevelModel(t_r=self.t_r, mask_img=mask, standardize=standardize, signal_scaling=signal_scaling, **first_level_kws) else: self.glm = FirstLevelModel(t_r=self.t_r, mask_img=mask, standardize=standardize, signal_scaling=signal_scaling) self.design = None def add_design_matrix(self, hrf_model, drift_model='cosine', high_pass=.01): self.design = make_first_level_design_matrix( frame_times=self.frame_times, events=self.events, hrf_model=hrf_model, drift_model=drift_model, high_pass=high_pass, add_regs=self.regressors) return self def fit(self): self.glm.fit(self.img, design_matrices=self.design) return self def extract_params(self, param_type, contrast_ix=0): design = self.glm.design_matrices_[0] contrast = np.zeros(design.shape[1]) contrast[contrast_ix] = 1 if param_type == 'beta': param_img = self.glm.compute_contrast(contrast, output_type='effect_size') reg_sd = np.std(design.iloc[:, contrast_ix]) param_img = math_img("img * {}".format(reg_sd), img=param_img) elif param_type == 't': param_img = self.glm.compute_contrast(contrast, stat_type='t', output_type='stat') else: param_img = self.glm.compute_contrast(contrast, output_type=param_type) return param_img
def main(sourcedata, derivatives, subject, session, tmp_dir): sourcedata_layout = BIDSLayout(sourcedata) sourcedata_df = sourcedata_layout.as_data_frame() events = sourcedata_df[(sourcedata_df['suffix'] == 'events') & (sourcedata_df['subject'] == subject) & (sourcedata_df['session'] == session)] derivatives_layout = BIDSLayout(os.path.join(derivatives), validate=False) derivatives_df = derivatives_layout.as_data_frame() bold = derivatives_df[(derivatives_df['suffix'] == 'preproc') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] confounds = derivatives_df[(derivatives_df['suffix'] == 'confounds') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] compcor = derivatives_df[(derivatives_df['suffix'] == 'compcor') & (derivatives_df['subject'] == subject) & (derivatives_df['session'] == session)] mask = derivatives_layout.get(subject=subject, session=session, suffix='mask', return_type='file')[0] df = events.merge(bold, on=['subject', 'session', 'run'], suffixes=('_events', '_bold')) confounds = confounds.rename(columns={'path': 'confounds'}) df = df.merge(confounds[['subject', 'session', 'run', 'confounds']]) compcor = compcor.rename(columns={'path': 'compcor'}) df = df.merge(compcor[['subject', 'session', 'run', 'compcor']]) df.sort_values('run', inplace=True) print(df.iloc[0]) models = [] for ix, row in df.iterrows(): results_dir = os.path.join(derivatives, 'modelfitting', 'glm8', 'sub-{}'.format(row['subject'])) if 'session' in row: results_dir = os.path.join(results_dir, 'ses-{}'.format(row['session'])) results_dir = op.join(results_dir, 'func') os.makedirs(results_dir, exist_ok=True) confounds = pd.read_table(row.confounds).fillna(method='bfill') compcor = pd.read_table(row.compcor).fillna(method='bfill') confounds = pd.concat((confounds, compcor), 1) confounds -= confounds.mean() confounds /= confounds.std() pca = decomposition.PCA(n_components=6) confounds_trans = pd.DataFrame( pca.fit_transform(confounds), columns=['pca_{}'.format(i) for i in range(6)]) print('Fitting {}'.format(row['path_bold'])) model = FirstLevelModel(t_r=4, signal_scaling=False, subject_label=int(row['run']), mask_img=mask) paradigm = pd.read_table(row['path_events']) paradigm_ = paradigm.copy() paradigm['trial_type'] = 'stimulation' paradigm['modulation'] = 1 paradigm_['modulation'] = paradigm_.trial_type.map({ 'eye_L': 1, 'eye_R': -1 }) paradigm_['trial_type'] = 'eye' paradigm = pd.concat((paradigm, paradigm_), ignore_index=True) model.fit(row['path_bold'], paradigm, confounds=confounds_trans) row['run'] = int(row['run']) row = dict(row) left_right = model.compute_contrast('eye', output_type='z_score') left_right.to_filename( os.path.join( results_dir, 'sub-{subject}_ses-{session}_task-{task_events}_run-{run:02d}_left_over_right_zmap.nii.gz' .format(**row))) left_right = model.compute_contrast('eye', output_type='effect_size') left_right.to_filename( os.path.join( results_dir, 'sub-{subject}_ses-{session}_task-{task_events}_run-{run:02d}_left_over_right_psc.nii.gz' .format(**row))) stimulation = model.compute_contrast('stimulation', output_type='effect_size') stimulation.to_filename( os.path.join( results_dir, 'sub-{subject}_ses-{session}_task-{task_events}_run-{run:02d}_stimulation_psc.nii.gz' .format(**row))) stimulation = model.compute_contrast('stimulation', output_type='z_score') stimulation.to_filename( os.path.join( results_dir, 'sub-{subject}_ses-{session}_task-{task_events}_run-{run:02d}_stimulation_zmap.nii.gz' .format(**row))) models.append(model) second_level_model = SecondLevelModel(mask_img=mask) second_level_model.fit(models) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_zmap.nii.gz'.format( row['subject'], row['session']))) left_right_group = second_level_model.compute_contrast( first_level_contrast='eye', output_type='effect_size') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_left_over_right_effect_size.nii.gz'.format( row['subject'], row['session']))) stimulation_group = second_level_model.compute_contrast( first_level_contrast='stimulation', output_type='z_score') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_stimulation_zmap.nii.gz'.format( row['subject'], row['session']))) stimulation_group = second_level_model.compute_contrast( first_level_contrast='stimulation', output_type='effect_size') left_right_group.to_filename( os.path.join( results_dir, 'sub-{}_ses-{}_stimulation_effect_size.nii.gz'.format( row['subject'], row['session'])))
design_files = [data['design_matrix1'], data['design_matrix2']] import pandas as pd import numpy as np design_matrices = [pd.DataFrame(np.load(df)['X']) for df in design_files] ######################################################################### # GLM estimation # ---------------------------------- # GLM specification. Note that the mask was provided in the dataset. So we use it. from nistats.first_level_model import FirstLevelModel fmri_glm = FirstLevelModel(mask=data['mask'], minimize_memory=True) ######################################################################### # GLM fitting fmri_glm = fmri_glm.fit(fmri_img, design_matrices=design_matrices) ######################################################################### # Compute fixed effects of the two runs and compute related images # For this, we first define the contrasts as we would do for a single session n_columns = design_matrices[0].shape[1] def pad_vector(contrast_, n_columns): """A small routine to append zeros in contrast vectors""" return np.hstack((contrast_, np.zeros(n_columns - len(contrast_)))) ######################################################################### # Contrast specification contrasts = {'SStSSp_minus_DStDSp': pad_vector([1, 0, 0, -1], n_columns), 'DStDSp_minus_SStSSp': pad_vector([-1, 0, 0, 1], n_columns),