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_and_design(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_design_matrix0(): # Test design matrix creation when no experimental paradigm is provided tr = 1.0 frame_times = np.linspace(0, 127 * tr, 128) _, X, names = check_design_matrix( make_first_level_design_matrix(frame_times, drift_model='polynomial', drift_order=3)) assert len(names) == 4 x = np.linspace(-0.5, .5, 128) assert_almost_equal(X[:, 0], x)
def test_csv_io(): # test the csv io on design matrices tr = 1.0 frame_times = np.linspace(0, 127 * tr, 128) events = modulated_event_paradigm() DM = make_first_level_design_matrix( frame_times, events, hrf_model='glover', drift_model='polynomial', drift_order=3, ) path = 'design_matrix.csv' with InTemporaryDirectory(): DM.to_csv(path) DM2 = pd.read_csv(path, index_col=0) _, matrix, names = check_design_matrix(DM) _, matrix_, names_ = check_design_matrix(DM2) assert_almost_equal(matrix, matrix_) assert names == names_
def test_design_matrix0d(): # test design matrix creation when regressors are provided manually tr = 1.0 frame_times = np.linspace(0, 127 * tr, 128) ax = np.random.randn(128, 4) _, X, names = check_design_matrix( make_first_level_design_matrix(frame_times, drift_model='polynomial', drift_order=3, add_regs=ax)) assert len(names) == 8 assert X.shape[1] == 8
def test_spm_2(): # Check that the nistats design matrix is close enough to the SPM one # (it cannot be identical, because the hrf shape is different) frame_times = np.linspace(0, 99, 100) conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2'] onsets = [30, 50, 70, 10, 30, 80, 30, 40, 60] durations = 10 * np.ones(9) events = pd.DataFrame({ 'trial_type': conditions, 'onset': onsets, 'duration': durations }) X1 = make_first_level_design_matrix(frame_times, events, drift_model=None) spm_design_matrix = DESIGN_MATRIX['arr_1'] _, matrix, _ = check_design_matrix(X1) assert (((spm_design_matrix - matrix)**2).sum() / (spm_design_matrix**2).sum() < .1)
def design_matrix_light(frame_times, events=None, hrf_model='glover', drift_model='cosine', high_pass=.01, drift_order=1, fir_delays=None, add_regs=None, add_reg_names=None, min_onset=-24, path=None): """ Same as make_first_level_design_matrix, but only returns the computed matrix and associated name. """ fir_delays = fir_delays if fir_delays else [0] dmtx = make_first_level_design_matrix(frame_times, events, hrf_model, drift_model, high_pass, drift_order, fir_delays, add_regs, add_reg_names, min_onset) _, matrix, names = check_design_matrix(dmtx) return matrix, names
def test_design_matrix0c(): # test design matrix creation when regressors are provided manually tr = 1.0 frame_times = np.linspace(0, 127 * tr, 128) ax = np.random.randn(128, 4) _, X, names = check_design_matrix( make_first_level_design_matrix(frame_times, drift_model='polynomial', drift_order=3, add_regs=ax)) assert_almost_equal(X[:, 0], ax[:, 0]) ax = np.random.randn(127, 4) with pytest.raises( AssertionError, match="Incorrect specification of additional regressors:."): make_first_level_design_matrix(frame_times, add_regs=ax) ax = np.random.randn(128, 4) with pytest.raises( ValueError, match="Incorrect number of additional regressor names."): make_first_level_design_matrix(frame_times, add_regs=ax, add_reg_names='')