def test_modelgen1(): tempdir = mkdtemp() filename1 = os.path.join(tempdir, "test1.nii") filename2 = os.path.join(tempdir, "test2.nii") Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1) Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2) s = SpecifyModel() s.inputs.input_units = "scans" set_output_units = lambda: setattr(s.inputs, "output_units", "scans") yield assert_raises, TraitError, set_output_units s.inputs.functional_runs = [filename1, filename2] s.inputs.time_repetition = 6 s.inputs.high_pass_filter_cutoff = 128.0 info = [ Bunch( conditions=["cond1"], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None, ), Bunch( conditions=["cond1"], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None, ), ] s.inputs.subject_info = info res = s.run() yield assert_equal, len(res.outputs.session_info), 2 yield assert_equal, len(res.outputs.session_info[0]["regress"]), 0 yield assert_equal, len(res.outputs.session_info[0]["cond"]), 1 yield assert_almost_equal, np.array(res.outputs.session_info[0]["cond"][0]["onset"]), np.array([12, 300, 600, 1080]) info = [ Bunch(conditions=["cond1"], onsets=[[2]], durations=[[1]]), Bunch(conditions=["cond1"], onsets=[[3]], durations=[[1]]), ] s.inputs.subject_info = deepcopy(info) res = s.run() yield assert_almost_equal, np.array(res.outputs.session_info[0]["cond"][0]["duration"]), np.array([6.0]) yield assert_almost_equal, np.array(res.outputs.session_info[1]["cond"][0]["duration"]), np.array([6.0]) info = [ Bunch(conditions=["cond1", "cond2"], onsets=[[2, 3], [2]], durations=[[1, 1], [1]]), Bunch(conditions=["cond1", "cond2"], onsets=[[2, 3], [2, 4]], durations=[[1, 1], [1, 1]]), ] s.inputs.subject_info = deepcopy(info) s.inputs.input_units = "scans" res = s.run() yield assert_almost_equal, np.array(res.outputs.session_info[0]["cond"][0]["duration"]), np.array([6.0, 6.0]) yield assert_almost_equal, np.array(res.outputs.session_info[0]["cond"][1]["duration"]), np.array([6.0]) yield assert_almost_equal, np.array(res.outputs.session_info[1]["cond"][1]["duration"]), np.array([6.0, 6.0]) rmtree(tempdir)
def test_modelgen1(tmpdir): tempdir = str(tmpdir) filename1 = os.path.join(tempdir, 'test1.nii') filename2 = os.path.join(tempdir, 'test2.nii') Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1) Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2) s = SpecifyModel() s.inputs.input_units = 'scans' set_output_units = lambda: setattr(s.inputs, 'output_units', 'scans') with pytest.raises(TraitError): set_output_units() s.inputs.functional_runs = [filename1, filename2] s.inputs.time_repetition = 6 s.inputs.high_pass_filter_cutoff = 128. info = [Bunch(conditions=['cond1'], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None), Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None)] s.inputs.subject_info = info res = s.run() assert len(res.outputs.session_info) == 2 assert len(res.outputs.session_info[0]['regress']) == 0 assert len(res.outputs.session_info[0]['cond']) == 1 npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([12, 300, 600, 1080])) info = [Bunch(conditions=['cond1'], onsets=[[2]], durations=[[1]]), Bunch(conditions=['cond1'], onsets=[[3]], durations=[[1]])] s.inputs.subject_info = deepcopy(info) res = s.run() npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([6.])) npt.assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][0]['duration']), np.array([6.])) info = [Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2]], durations=[[1, 1], [1]]), Bunch(conditions=['cond1', 'cond2'], onsets=[[2, 3], [2, 4]], durations=[[1, 1], [1, 1]])] s.inputs.subject_info = deepcopy(info) s.inputs.input_units = 'scans' res = s.run() npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][0]['duration']), np.array([6., 6.])) npt.assert_almost_equal(np.array(res.outputs.session_info[0]['cond'][1]['duration']), np.array([6., ])) npt.assert_almost_equal(np.array(res.outputs.session_info[1]['cond'][1]['duration']), np.array([6., 6.]))
def test_modelgen1(): tempdir = mkdtemp() filename1 = os.path.join(tempdir, 'test1.nii') filename2 = os.path.join(tempdir, 'test2.nii') Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1) Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2) s = SpecifyModel() s.inputs.input_units = 'scans' set_output_units = lambda: setattr(s.inputs, 'output_units', 'scans') yield assert_raises, TraitError, set_output_units s.inputs.functional_runs = [filename1, filename2] s.inputs.time_repetition = 6 s.inputs.high_pass_filter_cutoff = 128. info = [ Bunch(conditions=['cond1'], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None), Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None) ] s.inputs.subject_info = info res = s.run() yield assert_equal, len(res.outputs.session_info), 2 yield assert_equal, len(res.outputs.session_info[0]['regress']), 0 yield assert_equal, len(res.outputs.session_info[0]['cond']), 1 yield assert_almost_equal, np.array( res.outputs.session_info[0]['cond'][0]['onset']), np.array( [12, 300, 600, 1080]) rmtree(tempdir)
def test_modelgen1(): tempdir = mkdtemp() filename1 = os.path.join(tempdir, 'test1.nii') filename2 = os.path.join(tempdir, 'test2.nii') Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1) Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2) s = SpecifyModel() s.inputs.input_units = 'scans' set_output_units = lambda: setattr(s.inputs, 'output_units', 'scans') yield assert_raises, TraitError, set_output_units s.inputs.functional_runs = [filename1, filename2] s.inputs.time_repetition = 6 s.inputs.high_pass_filter_cutoff = 128. info = [Bunch(conditions=['cond1'], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None), Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None)] s.inputs.subject_info = info res = s.run() yield assert_equal, len(res.outputs.session_info), 2 yield assert_equal, len(res.outputs.session_info[0]['regress']), 0 yield assert_equal, len(res.outputs.session_info[0]['cond']), 1 yield assert_almost_equal, np.array(res.outputs.session_info[0]['cond'][0]['onset']), np.array([12, 300, 600, 1080]) rmtree(tempdir)
def runglmperun(self, subject, trtimeinsec): s = SpecifyModel() # loop on all runs and models within each run modelfiles = subject._modelfiles for model in modelfiles: # Make directory results to store the results of the model results_dir = os.path.join(subject._path, 'model', model[0], 'results', model[1]) dir_util.mkpath(results_dir) os.chdir(results_dir) s.inputs.event_files = model[2] s.inputs.input_units = 'secs' s.inputs.functional_runs = os.path.join(subject._path, 'BOLD', model[1], 'bold_mcf_hp.nii.gz') # use nibable to get the tr of from the .nii file s.inputs.time_repetition = trtimeinsec s.inputs.high_pass_filter_cutoff = 128. # find par file that has motion motionfiles = glob( os.path.join(subject._path, 'BOLD', model[1], "*.par")) s.inputs.realignment_parameters = motionfiles #info = [Bunch(conditions=['cond1'], onsets=[[2, 50, 100, 180]], durations=[[1]]), Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]])] #s.inputs.subject_info = None res = s.run() res.runtime.cwd print ">>>> preparing evs for model " + model[ 1] + "and run " + model[0] sessionInfo = res.outputs.session_info level1design = Level1Design() level1design.inputs.interscan_interval = trtimeinsec level1design.inputs.bases = {'dgamma': {'derivs': False}} level1design.inputs.session_info = sessionInfo level1design.inputs.model_serial_correlations = True #TODO: add contrasts to level 1 design so that I have just condition vs rest for each ev #TODO: Look into changign this to FILM instead of FEAT - this also has the option of setting output directory # http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FEAT/UserGuide#Contrasts #http://nipy.org/nipype/interfaces/generated/nipype.interfaces.fsl.model.html#filmgls resLevel = level1design.run() featModel = FEATModel() featModel.inputs.fsf_file = resLevel.outputs.fsf_files featModel.inputs.ev_files = resLevel.outputs.ev_files resFeat = featModel.run() print ">>>> creating fsf design files for " + model[ 1] + "and run " + model[0] # TODO: give mask here glm = fsl.GLM(in_file=s.inputs.functional_runs[0], design=resFeat.outputs.design_file, output_type='NIFTI') print ">>>> running glm for " + model[1] + "and run " + model[0] resGlm = glm.run() print ">>>> finished running glm for " + model[ 1] + "and run " + model[0]
def test_modelgen1(tmpdir): filename1 = tmpdir.join("test1.nii").strpath filename2 = tmpdir.join("test2.nii").strpath Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename1) Nifti1Image(np.random.rand(10, 10, 10, 200), np.eye(4)).to_filename(filename2) s = SpecifyModel() s.inputs.input_units = "scans" set_output_units = lambda: setattr(s.inputs, "output_units", "scans") with pytest.raises(TraitError): set_output_units() s.inputs.functional_runs = [filename1, filename2] s.inputs.time_repetition = 6 s.inputs.high_pass_filter_cutoff = 128.0 info = [ Bunch( conditions=["cond1"], onsets=[[2, 50, 100, 180]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None, ), Bunch( conditions=["cond1"], onsets=[[30, 40, 100, 150]], durations=[[1]], amplitudes=None, pmod=None, regressors=None, regressor_names=None, tmod=None, ), ] s.inputs.subject_info = info res = s.run() assert len(res.outputs.session_info) == 2 assert len(res.outputs.session_info[0]["regress"]) == 0 assert len(res.outputs.session_info[0]["cond"]) == 1 npt.assert_almost_equal( np.array(res.outputs.session_info[0]["cond"][0]["onset"]), np.array([12, 300, 600, 1080]), ) info = [ Bunch(conditions=["cond1"], onsets=[[2]], durations=[[1]]), Bunch(conditions=["cond1"], onsets=[[3]], durations=[[1]]), ] s.inputs.subject_info = deepcopy(info) res = s.run() npt.assert_almost_equal( np.array(res.outputs.session_info[0]["cond"][0]["duration"]), np.array([6.0])) npt.assert_almost_equal( np.array(res.outputs.session_info[1]["cond"][0]["duration"]), np.array([6.0])) info = [ Bunch(conditions=["cond1", "cond2"], onsets=[[2, 3], [2]], durations=[[1, 1], [1]]), Bunch( conditions=["cond1", "cond2"], onsets=[[2, 3], [2, 4]], durations=[[1, 1], [1, 1]], ), ] s.inputs.subject_info = deepcopy(info) s.inputs.input_units = "scans" res = s.run() npt.assert_almost_equal( np.array(res.outputs.session_info[0]["cond"][0]["duration"]), np.array([6.0, 6.0]), ) npt.assert_almost_equal( np.array(res.outputs.session_info[0]["cond"][1]["duration"]), np.array([6.0])) npt.assert_almost_equal( np.array(res.outputs.session_info[1]["cond"][1]["duration"]), np.array([6.0, 6.0]), )
struct=[['subject_id']], evs=[['subject_id', 'func_scans']]) datasource.inputs.sort_filelist = False results = datasource.run() print results.outputs cont1 = ['Bundling-Control', 'T', ['Bundling', 'Control'], [1, -1]] s = SpecifyModel() s.inputs.input_units = 'secs' s.inputs.functional_runs = results.outputs.func s.inputs.time_repetition = 2 s.inputs.high_pass_filter_cutoff = 128. s.inputs.event_files = results.outputs.evs model = s.run() level1design = Level1Design() level1design.inputs.interscan_interval = 2.5 level1design.inputs.bases = {'dgamma': {'derivs': False}} level1design.inputs.model_serial_correlations = False level1design.inputs.session_info = model.outputs.session_info level1design.inputs.contrasts = [cont1] l1d = level1design.run() print l1d.outputs.ev_files modelgen = FEATModel() modelgen.inputs.ev_files = l1d.outputs.ev_files modelgen.inputs.fsf_file = l1d.outputs.fsf_files model = modelgen.run()