Example #1
0
def test_param_mask_deprecation_SecondLevelModel():
    """ Tests whether use of deprecated keyword parameter `mask`
    raises the correct warning & transfers its value to
    replacement parameter `mask_img` correctly.
    """
    deprecation_msg = (
        'The parameter "mask" will be removed in next release of Nistats. '
        'Please use the parameter "mask_img" instead.')
    mask_filepath = '~/masks/mask_01.nii.gz'
    with warnings.catch_warnings(record=True) as raised_warnings:
        slm1 = SecondLevelModel(mask=mask_filepath)
        slm2 = SecondLevelModel(mask_img=mask_filepath)
        slm3 = SecondLevelModel(mask_filepath)
    assert slm1.mask_img == mask_filepath
    assert slm2.mask_img == mask_filepath
    assert slm3.mask_img == mask_filepath

    with assert_raises(AttributeError):
        slm1.mask == mask_filepath
    with assert_raises(AttributeError):
        slm2.mask == mask_filepath
    with assert_raises(AttributeError):
        slm3.mask == mask_filepath

    raised_param_deprecation_warnings = [
        raised_warning_ for raised_warning_ in raised_warnings
        if str(raised_warning_.message).startswith('The parameter')
    ]

    assert len(raised_param_deprecation_warnings) == 1
    for param_warning_ in raised_param_deprecation_warnings:
        assert str(param_warning_.message) == deprecation_msg
        assert param_warning_.category is DeprecationWarning
Example #2
0
def oneSample_ttest(map_list, timeseries, coord):
    print("hOLII")
    template = ld.load_timserie_mask()
    print("alooooo")
    design_matrix = pd.DataFrame([1] * len(map_list), columns=['intercept'])
    print("Hasta aqui si")
    nifti_masker = NiftiMasker(standardize=True,
                               mask_strategy='epi',
                               memory="nilearn_cache",
                               memory_level=2,
                               smoothing_fwhm=8)
    print("me iamgino que hasta aqui llega.")
    ts = ants.matrix_to_timeseries(template, timeseries[0])
    print("Esto es nuevo")
    nifti_masker.fit(ts)
    print("No estoy seguro de donde ha reventado")
    zf = np.asmatrix(map_list[0].transpose())
    imgUsar = nifti_masker.inverse_transform(zf)
    print("No estoy seguro de donde ha reventado 2")
    second_level_model = SecondLevelModel().fit(pd.DataFrame(zf),
                                                design_matrix=design_matrix)
    print("Creo que peta aqui")
    z_map = second_level_model.compute_contrast(output_type='z_score')

    return z_map
Example #3
0
def report_slm_oasis():  # pragma: no cover
    n_subjects = 5  # more subjects requires more memory
    oasis_dataset = nilearn.datasets.fetch_oasis_vbm(n_subjects=n_subjects)
    # Resample the images, since this mask has a different resolution
    mask_img = resample_to_img(
        nilearn.datasets.fetch_icbm152_brain_gm_mask(),
        oasis_dataset.gray_matter_maps[0],
        interpolation='nearest',
    )
    design_matrix = _make_design_matrix_slm_oasis(oasis_dataset, n_subjects)
    second_level_model = SecondLevelModel(smoothing_fwhm=2.0, mask=mask_img)
    second_level_model.fit(oasis_dataset.gray_matter_maps,
                           design_matrix=design_matrix)

    contrast = [[1, 0, 0], [0, 1, 0]]
    report = make_glm_report(
        model=second_level_model,
        contrasts=contrast,
        bg_img=nilearn.datasets.fetch_icbm152_2009()['t1'],
        height_control=None,
    )
    output_filename = 'generated_report_slm_oasis.html'
    output_filepath = os.path.join(REPORTS_DIR, output_filename)
    report.save_as_html(output_filepath)
    report.get_iframe()
Example #4
0
def create_one_sample_t_test(name, maps, output_dir, smoothing_fwhm=6.0):
    if not op.isdir(output_dir):
        op.mkdir(output_dir)
    model = SecondLevelModel(smoothing_fwhm=smoothing_fwhm)
    design_matrix = pd.DataFrame([1] * len(maps), columns=['intercept'])
    model = model.fit(maps, design_matrix=design_matrix)
    z_map = model.compute_contrast(output_type='z_score')
    nib.save(z_map, op.join(output_dir, "{}_group_zmap.nii.gz".format(name)))
def compute_group_z_map(second_level_input, n_sub, output_pathway):
    # Model the effect of conditions (sample 1 vs sample 2).
    condition_effect = np.hstack(([1] * n_sub, [- 1] * n_sub))

    # Model the subject effect:
    # each subject is observed in sample 1 and sample 2.
    subject_effect = np.vstack((np.eye(n_sub), np.eye(n_sub)))
    subjects = ['S%02d' % i for i in range(1, n_sub + 1)]

    # We then assemble those in a design matrix and...
    design_matrix = pd.DataFrame(
        np.hstack((condition_effect[:, np.newaxis], subject_effect)),
        columns=['Story vs. Math'] + subjects)

    # ... plot the design_matrix.
    plot_design_matrix(design_matrix, output_file=
                       os.path.join(output_pathway,
                                    'design_matrix_story_math.png'))

    # Specify the analysis model and fit it
    second_level_model = SecondLevelModel().fit(second_level_input,
                                                design_matrix=design_matrix)

    # Estimate the contrast
    z_map = second_level_model.compute_contrast('Story vs. Math',
                                                output_type='z_score')

    # Report of the GLM
    report = make_glm_report(second_level_model,
                             contrasts='Story vs. Math',
                             title='Group-Level HCP900 Story vs.Math Report',
                             cluster_threshold=5,
                             height_control='fdr',
                             min_distance=8.,
                             plot_type='glass',
    )

    report.save_as_html(os.path.join(output_pathway, 'report.html'))

    # Save contrast nifti-file
    z_map.to_filename(os.path.join(output_pathway,
                                   'group_hcplang900_story_math.nii.gz'))

    # Plot contrast
    threshold = 3.1  # correponds to  p < .001, uncorrected
    display = plotting.plot_glass_brain(z_map, threshold=threshold,
                                        colorbar=True,
                                        plot_abs=False,
                                        title='Story vs. Math (unc p<0.001)',
                                        output_file=os.path.join(
                                        output_pathway,
                                        'group_hcplang900_story_math'))

    return z_map
def fit_second_level(models1, contrasts, atlas, dim, split, write_dir):
    mem = Memory(location=expanduser('cache'))
    model = SecondLevelModel(n_jobs=1, memory=mem, memory_level=1)
    model.fit(models1)

    for contrast in contrasts:
        for output_type in ['z_score', 'effect_size']:
            img = model.compute_contrast(first_level_contrast=contrast,
                                         output_type=output_type)
            img.to_filename(
                join(
                    write_dir, f'{contrast}_{output_type}_{atlas}'
                    f'_{dim}_{split}.nii.gz'))
Example #7
0
def test_second_level_model_glm_computation():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # ols case
        model = SecondLevelModel(mask=mask)
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4)
        model = model.fit(Y, design_matrix=X)
        labels1 = model.labels_
        results1 = model.results_
        labels2, results2 = run_glm(model.masker_.transform(Y), X, 'ols')
        assert_almost_equal(labels1, labels2, decimal=1)
        assert_equal(len(results1), len(results2))
def one_sample_ttest(filenames, name):
    design_matrix = pd.DataFrame([1] * len(filenames), columns=['intercept'])
    second_level_model = SecondLevelModel().fit(filenames,
                                                design_matrix=design_matrix)
    z_map = second_level_model.compute_contrast(output_type='z_score')
    nib.save(zmap, name + '.nii')
    thmap, threshold1 = map_threshold(z_map,
                                      level=.001,
                                      height_control='fpr',
                                      cluster_threshold=10)
    display = plot_glass_brain(thmap,
                               display_mode='lzry',
                               threshold=0,
                               colorbar=True)
    display.savefig(name + '.png')
    display.close()
Example #9
0
def test_slm_reporting():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1), )
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        model = SecondLevelModel()
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        c1 = np.eye(len(model.design_matrix_.columns))[0]
        report_slm = glmr.make_glm_report(model, c1)
        # catches & raises UnicodeEncodeError in HTMLDocument.get_iframe()
        report_iframe = report_slm.get_iframe()
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del Y, FUNCFILE, func_img, model
def do_contrast(con, ROOTDIR, MODEL):
    """ Input: con is the name of the contrast """
    confile = f'{con}_con.nii.gz'
    spmfile = f'{con}_zmap.nii.gz'
    cmaps = glob.glob(op.join(ROOTDIR, 'sub*', MODEL, confile))
    smaps = glob.glob(op.join(ROOTDIR, 'sub*', MODEL, spmfile))
    cmaps.sort()
    smaps.sort()

    fig, axes = plt.subplots(nrows=5, ncols=4)
    for cidx, tmap in enumerate(smaps):
        plotting.plot_glass_brain(tmap,
                                  colorbar=True,
                                  threshold=3.1,
                                  title=f'{cidx:02d}',
                                  axes=axes[int(cidx / 4),
                                            int(cidx % 4)],
                                  plot_abs=False,
                                  display_mode='z')
    fig.suptitle(f'contrast {con}')
    #pdf.savefig(fig)

    second_level_input = cmaps
    design_matrix = pd.DataFrame([1] * len(second_level_input),
                                 columns=['intercept'])

    second_level_model = SecondLevelModel(smoothing_fwhm=8.0)
    second_level_model = second_level_model.fit(second_level_input,
                                                design_matrix=design_matrix)

    z_map = second_level_model.compute_contrast(output_type='z_score')
    nib.save(z_map, f'group_{con}.nii.gz')
    p_val = 0.001
    p001_unc = norm.isf(p_val)
    display = plotting.plot_glass_brain(
        z_map,
        threshold=p001_unc,
        colorbar=True,
        display_mode='lzry',
        plot_abs=False,
        title=f'group contrasts {con} (unc p<0.001)')
    display.savefig(f'group_{con}.png')
    #pdf.savefig()
    display.close()
Example #11
0
def create_one_sample_t_test(name, maps, output_dir, smoothing_fwhm=8.0):
    if not op.isdir(output_dir):
        op.mkdir(output_dir)

    model = SecondLevelModel(smoothing_fwhm=smoothing_fwhm)
    design_matrix = pd.DataFrame([1] * len(maps), columns=['intercept'])
    model = model.fit(maps, design_matrix=design_matrix)
    z_map = model.compute_contrast(output_type='z_score')
    nibabel.save(z_map, op.join(output_dir,
                                "{}_group_zmap.nii.gz".format(name)))

    p_val = 0.001
    z_th = norm.isf(p_val)
    z_th = 3.1
    display = plotting.plot_glass_brain(z_map,
                                        threshold=z_th,
                                        colorbar=True,
                                        plot_abs=False,
                                        display_mode='lzry',
                                        title=name)
    display.savefig(op.join(output_dir, "{}_group_zmap".format(name)))
def test_high_level_glm_with_paths():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask)
        # asking for contrast before model fit gives error
        assert_raises(ValueError, model.compute_contrast, [])
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        c1 = np.eye(len(model.design_matrix_.columns))[0]
        z_image = model.compute_contrast(c1, output_type='z_score')
        assert_true(isinstance(z_image, Nifti1Image))
        assert_array_equal(z_image.affine, load(mask).affine)
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del Y, FUNCFILE, func_img, model
Example #13
0
def test_high_level_glm_with_paths():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1), )
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask)
        # asking for contrast before model fit gives error
        assert_raises(ValueError, model.compute_contrast, [])
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        c1 = np.eye(len(model.design_matrix_.columns))[0]
        z_image = model.compute_contrast(c1, output_type='z_score')
        assert_true(isinstance(z_image, Nifti1Image))
        assert_array_equal(z_image.affine, load(mask).affine)
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del Y, FUNCFILE, func_img, model
Example #14
0
def test_make_headings_with_contrasts_title_none():
    model = SecondLevelModel()
    test_input = ({
        'contrast_0': [0, 0, 1],
        'contrast_1': [0, 1, 1],
    }, None, model)
    expected_output = (
        'Report: Second Level Model for contrast_0, contrast_1',
        'Statistical Report for contrast_0, contrast_1',
        'Second Level Model',
    )
    actual_output = glmr._make_headings(*test_input)
    assert actual_output == expected_output
def test_second_level_model_glm_computation():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask)
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])

        model = model.fit(Y, design_matrix=X)
        model.compute_contrast()
        labels1 = model.labels_
        results1 = model.results_

        labels2, results2 = run_glm(
            model.masker_.transform(Y), X.values, 'ols')
        assert_almost_equal(labels1, labels2, decimal=1)
        assert_equal(len(results1), len(results2))
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del func_img, FUNCFILE, model, X, Y
Example #16
0
def test_second_level_model_glm_computation():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1), )
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask)
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])

        model = model.fit(Y, design_matrix=X)
        model.compute_contrast()
        labels1 = model.labels_
        results1 = model.results_

        labels2, results2 = run_glm(model.masker_.transform(Y), X.values,
                                    'ols')
        assert_almost_equal(labels1, labels2, decimal=1)
        assert_equal(len(results1), len(results2))
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del func_img, FUNCFILE, model, X, Y
Example #17
0
def create_one_sample_t_test(name,
                             maps,
                             output_dir,
                             smoothing_fwhm=None,
                             vmax=None,
                             design_matrix=None,
                             p_val=0.001,
                             fdr=0.01,
                             loc=0,
                             scale=1,
                             fwhm=6):
    """ Do a one sample t-test over the maps.
    """
    print('##### ', name, ' #####')
    model = SecondLevelModel(smoothing_fwhm=smoothing_fwhm, n_jobs=-1)
    design_matrix = design_matrix if (
        design_matrix is not None) else pd.DataFrame([1] * len(maps),
                                                     columns=['intercept'])
    model = model.fit(maps, design_matrix=design_matrix)
    z_map = model.compute_contrast(output_type='z_score')
    p_val = p_val
    z_th = norm.isf(p_val, loc=loc, scale=scale)  # 3.09

    # apply fdr to zmap
    thresholded_zmap, th = map_threshold(stat_img=z_map,
                                         alpha=fdr,
                                         height_control='fdr',
                                         cluster_threshold=0,
                                         two_sided=False)
    print(z_th, th)
    # effect size-map
    eff_map = model.compute_contrast(output_type='effect_size')

    thr = np.abs(thresholded_zmap.get_data())

    return z_map, eff_map, new_img_like(eff_map, (thr > z_th)), (thr > z_th)
def compute_second_level(model1, model2, FWHM=None):
    """
    Compute the group-level significance of the paired difference between two
    models
    """
    # # compute and save the difference of two models
    compute_model_difference(model1, model2,
                             FWHM)  # compute with the FirstLevelAnalysis code

    # redefine the mask, without smoothing
    masker = compute_global_masker(rootdir)

    # use those different files as input for group-level analysis
    second_level_input = get_fmri_files(
        os.path.join(rootdir, first_dir, "diff"), f'{model1}-{model2}')

    # prepare second level analysis (one sample t-test)
    design_matrix = pd.DataFrame([1] * len(second_level_input),
                                 columns=['intercept'])
    second_level_model = SecondLevelModel(masker)
    second_level_model = second_level_model.fit(second_level_input,
                                                design_matrix=design_matrix)

    # estimation the contrast
    z_map = second_level_model.compute_contrast(output_type='z_score')
    # save to disk
    nib.save(
        z_map,
        os.path.join(rootdir, second_dir, f"GroupLevel_{model1}-{model2}"))

    # Get the map of positive values only
    z_val = masker.transform(z_map)
    z_val_pos = [val if val > 0 else 0 for val in z_val[0]]
    z_map_pos = masker.inverse_transform(z_val_pos)

    return z_map, z_map_pos
Example #19
0
def test_make_headings_with_contrasts_title_custom():
    model = SecondLevelModel()
    test_input = (
        {
            'contrast_0': [0, 0, 1],
            'contrast_1': [0, 1, 1],
        },
        'Custom Title for report',
        model,
    )
    expected_output = (
        'Custom Title for report',
        'Custom Title for report',
        'Second Level Model',
    )
    actual_output = glmr._make_headings(*test_input)
    assert actual_output == expected_output
Example #20
0
def test_second_level_model_contrast_computation_with_memory_caching():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1), )
        mask, FUNCFILE, _ = write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask=mask, memory='nilearn_cache')
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        ncol = len(model.design_matrix_.columns)
        c1 = np.eye(ncol)[0, :]
        # test memory caching for compute_contrast
        model.compute_contrast(c1, output_type='z_score')
        # or simply pass nothing
        model.compute_contrast()
Example #21
0
def test_create_second_level_design():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        first_level_input = _first_level_dataframe()
        first_level_input['effects_map_path'] = [FUNCFILE] * 4
        confounds = [['01', 0.1], ['02', 0.75]]
        confounds = pd.DataFrame(confounds, columns=['subject_id', 'f1'])
        design = create_second_level_design(first_level_input, confounds)
        expected_design = np.array([[1, 0, 1, 0, 0.1], [0, 1, 1, 0, 0.1],
                                    [1, 0, 0, 1, 0.75], [0, 1, 0, 1, 0.75]])
        assert_array_equal(design, expected_design)
        assert_true(len(design.columns) == 2 + 2 + 1)
        assert_true(len(design) == 2 + 2)
        model = SecondLevelModel(mask=mask).fit(first_level_input,
                                                confounds=confounds)
        design = model.design_matrix_
        assert_array_equal(design, expected_design)
        assert_true(len(design.columns) == 2 + 2 + 1)
        assert_true(len(design) == 2 + 2)
def test_second_level_model_contrast_computation_with_memory_caching():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask, memory='nilearn_cache')
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        ncol = len(model.design_matrix_.columns)
        c1 = np.eye(ncol)[0, :]
        # test memory caching for compute_contrast
        model.compute_contrast(c1, output_type='z_score')
        # or simply pass nothing
        model.compute_contrast()
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del func_img, FUNCFILE, model, X, Y
def anova(db, masker):
    """perform a big ANOVA of brain activation with three factors:
    acquisition, subject, contrast"""
    df = db[(db.acquisition == 'ap') | (db.acquisition == 'pa')]

    # make the design matrix
    subject_dmtx, subject_ = design(df.subject)
    contrast_dmtx, contrast_ = design(df.contrast)
    acq_dmtx, acq_ = design(df.acquisition)
    dmtx = np.hstack(
        (subject_dmtx[:, :-1], contrast_dmtx[:, :-1], acq_dmtx[:, :-1],
         np.ones((len(df), 1))))
    labels = np.hstack(
        (subject_[:-1], contrast_[:-1], acq_[:-1], ['intercept']))
    design_matrix = pd.DataFrame(dmtx, columns=labels)
    _, singular, _ = np.linalg.svd(design_matrix.values, 0)
    dof_subject = len(subject_) - 1
    dof_contrast = len(contrast_) - 1
    dof_acq = len(acq_) - 1

    # fit the model
    from nistats.second_level_model import SecondLevelModel
    second_level_model = SecondLevelModel(mask=masker.mask_img_)
    second_level_model = second_level_model.fit(list(df.path.values),
                                                design_matrix=design_matrix)
    subject_map = second_level_model.compute_contrast(np.eye(
        len(labels))[:dof_subject],
                                                      output_type='z_score')
    contrast_map = second_level_model.compute_contrast(np.eye(
        len(labels))[dof_subject:dof_subject + dof_contrast],
                                                       output_type='z_score')
    acq_map = second_level_model.compute_contrast(np.eye(
        len(labels))[-1 - dof_acq:-1],
                                                  output_type='z_score')
    subject_map = math_img('img * (img > -8.2095)', img=subject_map)
    contrast_map = math_img('img * (img > -8.2095)', img=contrast_map)
    acq_map = math_img('img * (img > -8.2095)', img=acq_map)
    return design_matrix, subject_map, contrast_map, acq_map
Example #24
0
def test_second_level_model_contrast_computation():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1), )
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask)
        # asking for contrast before model fit gives error
        assert_raises(ValueError, model.compute_contrast, 'intercept')
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        ncol = len(model.design_matrix_.columns)
        c1, cnull = np.eye(ncol)[0, :], np.zeros(ncol)
        # smoke test for different contrasts in fixed effects
        model.compute_contrast(c1)
        model.compute_contrast(c1, output_type='z_score')
        model.compute_contrast(c1, output_type='stat')
        model.compute_contrast(c1, output_type='p_value')
        model.compute_contrast(c1, output_type='effect_size')
        model.compute_contrast(c1, output_type='effect_variance')
        # formula should work (passing variable name directly)
        model.compute_contrast('intercept')
        # or simply pass nothing
        model.compute_contrast()
        # passing null contrast should give back a value error
        assert_raises(ValueError, model.compute_contrast, cnull)
        # passing wrong parameters
        assert_raises(ValueError, model.compute_contrast, [])
        assert_raises(ValueError, model.compute_contrast, c1, None, '')
        assert_raises(ValueError, model.compute_contrast, c1, None, [])
        assert_raises(ValueError, model.compute_contrast, c1, None, None, '')
        # check that passing no explicit contrast when the dsign
        # matrix has morr than one columns raises an error
        X = pd.DataFrame(np.random.rand(4, 2), columns=['r1', 'r2'])
        model = model.fit(Y, design_matrix=X)
        assert_raises(ValueError, model.compute_contrast, None)
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del func_img, FUNCFILE, model, X, Y
Example #25
0
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'])))
Example #26
0
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'])))
Example #27
0
#########################################################################
# Get the set of individual statstical maps (contrast estimates)
cmap_filenames = localizer_dataset.cmaps

#########################################################################
# Perform the second level analysis
# ----------------------------------
#
# First define a design matrix for the model. As the model is trivial (one-sample test), the design matrix is just one column with ones.
import pandas as pd
design_matrix = pd.DataFrame([1] * n_samples, columns=['intercept'])

#########################################################################
# Specify and estimate the model
from nistats.second_level_model import SecondLevelModel
second_level_model = SecondLevelModel().fit(
    cmap_filenames, design_matrix=design_matrix)

#########################################################################
# Compute the only possible contrast: the one-sample test. Since there
# is only one possible contrast, we don't need to specify it in detail
z_map = second_level_model.compute_contrast(output_type='z_score')

#########################################################################
# Threshold the resulting map:
# false positive rate < .001, cluster size > 10 voxels
from nistats.thresholding import map_threshold
thresholded_map1, threshold1 = map_threshold(
    z_map, alpha=.001, height_control='fpr', cluster_threshold=10)

#########################################################################
# Now use FDR <.05, (False Discovery Rate) no cluster-level threshold
Example #28
0
design_matrix = pd.DataFrame(np.vstack((age, sex, intercept)).T,
                             columns=['age', 'sex', 'intercept'])

#############################################################################
# Plot the design matrix
from nistats.reporting import plot_design_matrix
ax = plot_design_matrix(design_matrix)
ax.set_title('Second level design matrix', fontsize=12)
ax.set_ylabel('maps')

##########################################################################
# Specify and fit the second-level model when loading the data, we
# smooth a little bit to improve statistical behavior

from nistats.second_level_model import SecondLevelModel
second_level_model = SecondLevelModel(smoothing_fwhm=2.0, mask=mask_img)
second_level_model.fit(gray_matter_map_filenames,
                       design_matrix=design_matrix)

##########################################################################
# Estimate the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = second_level_model.compute_contrast(second_level_contrast=[1, 0, 0],
                                            output_type='z_score')

###########################################################################
# We threshold the second level contrast at uncorrected p < 0.001 and plot it.
# First compute the threshold.
from nistats.thresholding import map_threshold
_, threshold = map_threshold(
    z_map, level=.05, height_control='fdr')
Example #29
0
fig.suptitle('subjects z_map language network (unc p<0.001)')
plotting.show()

#########################################################################
# Second level model estimation
# -----------------------------
# We just have to provide the list of fitted FirstLevelModel objects
# to the SecondLevelModel object for estimation. We can do this because
# all subjects share a similar design matrix (same variables reflected in
# column names)
from nistats.second_level_model import SecondLevelModel
second_level_input = models

#########################################################################
# Note that we apply a smoothing of 8mm.
second_level_model = SecondLevelModel(smoothing_fwhm=8.0)
second_level_model = second_level_model.fit(second_level_input)

#########################################################################
# Computing contrasts at the second level is as simple as at the first level
# Since we are not providing confounders we are performing an one-sample test
# at the second level with the images determined by the specified first level
# contrast.
zmap = second_level_model.compute_contrast(
    first_level_contrast='language-string')

#########################################################################
# The group level contrast reveals a left lateralized fronto-temporal
# language network
plotting.plot_glass_brain(zmap, colorbar=True, threshold=p001_unc,
                          title='Group language network (unc p<0.001)',
def run_sig_tests(data_fnames, mask=None):
    cmap = "Wistia"
    second_level_model = SecondLevelModel(smoothing_fwhm=5.0, mask=mask)
    if isinstance(data_fnames, dict):
        test_type = 'z'
        data_fnames_num = sorted(data_fnames['number'])
        data_fnames_fri = sorted(data_fnames['friend'])
        fname_atts = get_all_bids_atts(data_fnames_num[0])
        fname_atts['task'] = 'diff'
        fname_atts['test'] = 'pairedt'
        # create paired t-test design matrix
        pair_mat = np.zeros(
            (2 * len(data_fnames_num), len(data_fnames_num) + 1), dtype=int)
        labs = []
        for i in range(len(data_fnames_num)):
            l = 's' + str(i)
            labs = labs + [l]
            a = [0] * len(data_fnames_num)
            a[i] = 1
            pair_mat[:, i] = a + a
        pair_mat[:, len(data_fnames_num)] = [1] * len(
            data_fnames_num) + [-1] * len(data_fnames_fri)
        design_matrix = pd.DataFrame(pair_mat, columns=labs + ['diff'])
        if fname_atts['pred'] == 'deg':
            data_fnames = data_fnames_num + data_fnames_fri
            cmap = "winter"
        elif fname_atts['pred'] == 'dist':
            data_fnames = data_fnames_fri + data_fnames_num
            cmap = "cool"
        print(data_fnames)
        con_name = 'diff'
    else:
        fname_atts = get_all_bids_atts(data_fnames[0])
        # if fname_atts['val'] == 'R2':
        #     test_type = 'ks'
        #     fname_atts['test'] = 'ksfit'
        # else:
        test_type = 'z'
        fname_atts['test'] = 'singlesamplet'
        design_matrix = pd.DataFrame([1] * len(data_fnames),
                                     columns=['intercept'])
        con_name = 'intercept'
    # setup file names
    del fname_atts['sub']
    fname_atts['correction'] = "none"
    # show data files and design matrix
    print("Running significance testing on: ")
    print(data_fnames)
    if test_type == 'z':
        print("Using design matrix: ")
        print(design_matrix)
        out_stat = "z"
        # save z map
        second_level_model = second_level_model.fit(
            data_fnames, design_matrix=design_matrix)
        z_map = second_level_model.compute_contrast(con_name,
                                                    output_type='z_score')
        out_map = z_map
        # save p map
        p_val = second_level_model.compute_contrast(con_name,
                                                    output_type='p_value')
        refnii = p_val
    elif test_type == 'ks':
        out_stat = "D"
        d_map, p_map, all_data = run_R2_sig_tests(data_fnames, mask_nii=mask)
        refnii = nib.load(data_fnames[0])
        out_map = nib.Nifti1Image(d_map, refnii.affine, refnii.header)
        p_val = nib.Nifti1Image(p_map, refnii.affine, refnii.header)
    # save R2 mean
    if fname_atts['val'] == 'R2':
        all_data = load_all(data_fnames)
        mean_data = np.mean(all_data, axis=3)
        fname_atts['val2'] = 'mean'
        out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
        save_nii(mean_data, refnii, out_fname)
    # save stat map
    fname_atts['val2'] = out_stat
    out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
    nib.save(out_map, out_fname)
    print(out_fname + ' saved.')
    # save p map
    fname_atts['val2'] = "p"
    out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
    nib.save(p_val, out_fname)
    print(out_fname + ' saved.')
    # FDR correction
    if mask is None:
        test_indices = np.argwhere(np.ones(
            refnii.get_data().flatten().shape)).flatten()
    else:
        test_indices = np.argwhere(mask.get_data().flatten()).flatten()
    p_arr = p_val.get_data().flatten()
    p_arr2 = np.take(p_arr, test_indices)
    sigs2, fdr_val2, a2, b2 = multipletests(p_arr2,
                                            alpha=.05,
                                            method='fdr_bh',
                                            is_sorted=False,
                                            returnsorted=False)
    pfdr2 = deepcopy(1 - fdr_val2)
    pfdr_arr = np.zeros(p_arr.shape)
    np.put(pfdr_arr, test_indices, pfdr2)
    pfdr_3d2 = pfdr_arr.reshape(p_val.shape)
    pfdr_map = nib.Nifti1Image(pfdr_3d2, refnii.affine, refnii.header)
    fname_atts['val2'] = "p"
    fname_atts['correction'] = "fdr"
    fname_atts['dir'] = "rev"
    out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
    nib.save(pfdr_map, out_fname)
    print(out_fname + ' saved.')
    # plot maps
    thresholded_map2, threshold2 = map_threshold(
        out_map, level=.05, height_control='fdr')  #, two_sided=False)
    display = plot_stat_map(out_map, title='raw stat')
    print('The FDR=.05 threshold is %.3g' % threshold2)
    if not math.isinf(threshold2):
        #     thresholded_map2, threshold2 = map_threshold(
        #     out_map, level=.001, height_control=None)#, two_sided=False)
        #     fname_atts['val2'] = out_stat
        #     fname_atts['thresh'] = "p01"
        #     fname_atts['plot'] = "statmap"
        #     fname_atts['correction'] = "none"
        #     out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
        #     display = plot_stat_map(thresholded_map2, title='z map, p < 0.001', threshold = threshold2)
        #     display.savefig(out_fname)
        # else:
        fname_atts['val2'] = out_stat
        fname_atts['thresh'] = "p05"
        fname_atts['plot'] = "statmap"
        fname_atts['correction'] = "fdr"
        out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
        display = plot_stat_map(thresholded_map2,
                                cut_coords=display.cut_coords,
                                title='Thresholded ' + out_stat +
                                ' map, expected fdr = .05, ' + out_stat +
                                ' = ' + str(threshold2),
                                threshold=threshold2)
        display.savefig(out_fname)
        fname_atts['plot'] = "glassbrain"
        out_fname = os.path.join(out_dir, make_bids_str(fname_atts))
        display = plot_glass_brain(thresholded_map2,
                                   cut_coords=display.cut_coords,
                                   title='Thresholded ' + out_stat +
                                   ' map, expected fdr = .05, z = ' +
                                   str(threshold2),
                                   threshold=threshold2)
        display.savefig(out_fname)
def test_second_level_model_contrast_computation():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # ols case
        model = SecondLevelModel(mask=mask)
        # asking for contrast before model fit gives error
        assert_raises(ValueError, model.compute_contrast, 'intercept')
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        ncol = len(model.design_matrix_.columns)
        c1, cnull = np.eye(ncol)[0, :], np.zeros(ncol)
        # smoke test for different contrasts in fixed effects
        model.compute_contrast(c1)
        model.compute_contrast(c1, output_type='z_score')
        model.compute_contrast(c1, output_type='stat')
        model.compute_contrast(c1, output_type='p_value')
        model.compute_contrast(c1, output_type='effect_size')
        model.compute_contrast(c1, output_type='effect_variance')
        # formula should work (passing variable name directly)
        model.compute_contrast('intercept')
        # or simply pass nothing
        model.compute_contrast()
        # passing null contrast should give back a value error
        assert_raises(ValueError, model.compute_contrast, cnull)
        # passing wrong parameters
        assert_raises(ValueError, model.compute_contrast, [])
        assert_raises(ValueError, model.compute_contrast, c1, None, '')
        assert_raises(ValueError, model.compute_contrast, c1, None, [])
        assert_raises(ValueError, model.compute_contrast, c1, None, None, '')
plt.show()

############################################################################
# Estimate second level model
# ---------------------------
# We define the input maps and the design matrix for the second level model
# and fit it.
import pandas as pd
second_level_input = data['cmaps']
design_matrix = pd.DataFrame([1] * len(second_level_input),
                             columns=['intercept'])

############################################################################
# Model specification and fit
from nistats.second_level_model import SecondLevelModel
second_level_model = SecondLevelModel(smoothing_fwhm=8.0)
second_level_model = second_level_model.fit(second_level_input,
                                            design_matrix=design_matrix)

##########################################################################
# To estimate the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = second_level_model.compute_contrast(output_type='z_score')

###########################################################################
# We threshold the second level contrast at uncorrected p < 0.001 and plot
from scipy.stats import norm
p_val = 0.001
p001_unc = norm.isf(p_val)
display = plotting.plot_glass_brain(
    z_map, threshold=p001_unc, colorbar=True, display_mode='z', plot_abs=False,
print("Actual number of subjects after quality check: %d" % n_samples)

############################################################################
# Estimate second level model
# ---------------------------
# We define the input maps and the design matrix for the second level model
# and fit it.
import pandas as pd
design_matrix = pd.DataFrame(
    np.hstack((tested_var, np.ones_like(tested_var))),
    columns=['fluency', 'intercept'])

###########################################################################
# Fit of the second-level model
from nistats.second_level_model import SecondLevelModel
model = SecondLevelModel(smoothing_fwhm=5.0)
model.fit(contrast_map_filenames, design_matrix=design_matrix)

##########################################################################
# To estimate the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = model.compute_contrast('fluency', output_type='z_score')

###########################################################################
# We compute the fdr-corrected p = 0.05 threshold for these data
from nistats.thresholding import map_threshold
_, threshold = map_threshold(z_map, alpha=.05, height_control='fdr')

###########################################################################
# Let us plot the second level contrast at the computed thresholds
from nilearn import plotting
Example #34
0
tested_var = tested_var[mask_quality_check].astype(float).reshape((-1, 1))
print("Actual number of subjects after quality check: %d" % n_samples)

############################################################################
# Estimate second level model
# ---------------------------
# We define the input maps and the design matrix for the second level model
# and fit it.
import pandas as pd
design_matrix = pd.DataFrame(np.hstack((tested_var, np.ones_like(tested_var))),
                             columns=['fluency', 'intercept'])

###########################################################################
# Fit of the second-level model
from nistats.second_level_model import SecondLevelModel
model = SecondLevelModel(smoothing_fwhm=5.0)
model.fit(contrast_map_filenames, design_matrix=design_matrix)

##########################################################################
# To estimate the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = model.compute_contrast('fluency', output_type='z_score')

###########################################################################
# We compute the fdr-corrected p = 0.05 threshold for these data
from nistats.thresholding import map_threshold
_, threshold = map_threshold(z_map, alpha=.05, height_control='fdr')

###########################################################################
# Let us plot the second level contrast at the computed thresholds
from nilearn import plotting
# Assemble those in a design matrix
design_matrix = pd.DataFrame(np.hstack(
    (condition_effect[:, np.newaxis], subject_effect)),
                             columns=['vertical vs horizontal'] + subjects)

############################################################################
# plot the design_matrix
from nistats.reporting import plot_design_matrix

plot_design_matrix(design_matrix)

############################################################################
# formally specify the analysis model and fit it
from nistats.second_level_model import SecondLevelModel

second_level_model = SecondLevelModel().fit(second_level_input,
                                            design_matrix=design_matrix)

##########################################################################
# Estimating the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = second_level_model.compute_contrast('vertical vs horizontal',
                                            output_type='z_score')

###########################################################################
# We threshold the second level contrast and plot it
threshold = 3.1  # correponds to  p < .001, uncorrected
display = plotting.plot_glass_brain(
    z_map,
    threshold=threshold,
    colorbar=True,
    plot_abs=False,
############################################################################
# Assemble those in a design matrix
design_matrix = pd.DataFrame(
    np.hstack((condition_effect[:, np.newaxis], subject_effect)),
    columns=['vertical vs horizontal'] + subjects)

############################################################################
# plot the design_matrix
from nistats.reporting import plot_design_matrix
plot_design_matrix(design_matrix)

############################################################################
# formally specify the analysis model and fit it
from nistats.second_level_model import SecondLevelModel
second_level_model = SecondLevelModel().fit(
    second_level_input, design_matrix=design_matrix)

##########################################################################
# Estimating the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = second_level_model.compute_contrast('vertical vs horizontal',
                                            output_type='z_score')

###########################################################################
# We threshold the second level contrast and plot it
threshold = 3.1  # correponds to  p < .001, uncorrected
display = plotting.plot_glass_brain(
    z_map, threshold=threshold, colorbar=True, plot_abs=False,
    title='vertical vs horizontal checkerboard (unc p<0.001')

###########################################################################
def test_second_level_model_contrast_computation():
    with InTemporaryDirectory():
        shapes = ((7, 8, 9, 1),)
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        # Ordinary Least Squares case
        model = SecondLevelModel(mask_img=mask)
        # asking for contrast before model fit gives error
        assert_raises(ValueError, model.compute_contrast, 'intercept')
        # fit model
        Y = [func_img] * 4
        X = pd.DataFrame([[1]] * 4, columns=['intercept'])
        model = model.fit(Y, design_matrix=X)
        ncol = len(model.design_matrix_.columns)
        c1, cnull = np.eye(ncol)[0, :], np.zeros(ncol)
        # smoke test for different contrasts in fixed effects
        model.compute_contrast(c1)
        model.compute_contrast(c1, output_type='z_score')
        model.compute_contrast(c1, output_type='stat')
        model.compute_contrast(c1, output_type='p_value')
        model.compute_contrast(c1, output_type='effect_size')
        model.compute_contrast(c1, output_type='effect_variance')
        # formula should work (passing variable name directly)
        model.compute_contrast('intercept')
        # or simply pass nothing
        model.compute_contrast()
        # passing null contrast should give back a value error
        assert_raises(ValueError, model.compute_contrast, cnull)
        # passing wrong parameters
        assert_raises(ValueError, model.compute_contrast, [])
        assert_raises(ValueError, model.compute_contrast, c1, None, '')
        assert_raises(ValueError, model.compute_contrast, c1, None, [])
        assert_raises(ValueError, model.compute_contrast, c1, None, None, '')
        # check that passing no explicit contrast when the dsign
        # matrix has morr than one columns raises an error
        X = pd.DataFrame(np.random.rand(4, 2), columns=['r1', 'r2'])
        model = model.fit(Y, design_matrix=X)
        assert_raises(ValueError, model.compute_contrast, None)
        # Delete objects attached to files to avoid WindowsError when deleting
        # temporary directory (in Windows)
        del func_img, FUNCFILE, model, X, Y
Example #38
0
design_matrix = pd.DataFrame(np.vstack((age, sex, intercept)).T,
                             columns=['age', 'sex', 'intercept'])

#############################################################################
# Plot the design matrix
from nistats.reporting import plot_design_matrix
ax = plot_design_matrix(design_matrix)
ax.set_title('Second level design matrix', fontsize=12)
ax.set_ylabel('maps')

##########################################################################
# Specify and fit the second-level model when loading the data, we
# smooth a little bit to improve statistical behavior

from nistats.second_level_model import SecondLevelModel
second_level_model = SecondLevelModel(smoothing_fwhm=2.0, mask=mask_img)
second_level_model.fit(gray_matter_map_filenames, design_matrix=design_matrix)

##########################################################################
# Estimate the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = second_level_model.compute_contrast(second_level_contrast=[1, 0, 0],
                                            output_type='z_score')

###########################################################################
# We threshold the second level contrast at uncorrected p < 0.001 and plot it.
# First compute the threshold.
from nistats.thresholding import map_threshold
_, threshold = map_threshold(z_map, level=.05, height_control='fdr')
print('The FDR=.05-corrected threshold is: %.3g' % threshold)
Example #39
0
def test_fmri_inputs():
    # Test processing of FMRI inputs
    with InTemporaryDirectory():
        # prepare fake data
        p, q = 80, 10
        X = np.random.randn(p, q)
        shapes = ((7, 8, 9, 10), )
        mask, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]
        func_img = load(FUNCFILE)
        T = func_img.shape[-1]
        des = pd.DataFrame(np.ones((T, 1)), columns=['a'])
        des_fname = 'design.csv'
        des.to_csv(des_fname)

        # prepare correct input first level models
        flm = FirstLevelModel(subject_label='01').fit(FUNCFILE,
                                                      design_matrices=des)
        flms = [flm, flm, flm]
        # prepare correct input dataframe and lists
        shapes = ((7, 8, 9, 1), )
        _, FUNCFILE, _ = _write_fake_fmri_data(shapes)
        FUNCFILE = FUNCFILE[0]

        dfcols = ['subject_label', 'map_name', 'effects_map_path']
        dfrows = [['01', 'a', FUNCFILE], ['02', 'a', FUNCFILE],
                  ['03', 'a', FUNCFILE]]
        niidf = pd.DataFrame(dfrows, columns=dfcols)
        niimgs = [FUNCFILE, FUNCFILE, FUNCFILE]
        niimg_4d = concat_imgs(niimgs)
        confounds = pd.DataFrame([['01', 1], ['02', 2], ['03', 3]],
                                 columns=['subject_label', 'conf1'])
        sdes = pd.DataFrame(X[:3, :3], columns=['intercept', 'b', 'c'])

        # smoke tests with correct input
        # First level models as input
        SecondLevelModel(mask_img=mask).fit(flms)
        SecondLevelModel().fit(flms)
        # Note : the following one creates a singular design matrix
        SecondLevelModel().fit(flms, confounds)
        SecondLevelModel().fit(flms, None, sdes)
        # dataframes as input
        SecondLevelModel().fit(niidf)
        SecondLevelModel().fit(niidf, confounds)
        SecondLevelModel().fit(niidf, confounds, sdes)
        SecondLevelModel().fit(niidf, None, sdes)
        # niimgs as input
        SecondLevelModel().fit(niimgs, None, sdes)
        # 4d niimg as input
        SecondLevelModel().fit(niimg_4d, None, sdes)

        # test wrong input errors
        # test first level model requirements
        assert_raises(ValueError, SecondLevelModel().fit, flm)
        assert_raises(ValueError, SecondLevelModel().fit, [flm])
        # test dataframe requirements
        assert_raises(ValueError,
                      SecondLevelModel().fit, niidf['subject_label'])
        # test niimgs requirements
        assert_raises(ValueError, SecondLevelModel().fit, niimgs)
        assert_raises(ValueError,
                      SecondLevelModel().fit, niimgs + [[]], confounds)
        # test first_level_conditions, confounds, and design
        assert_raises(ValueError, SecondLevelModel().fit, flms, ['', []])
        assert_raises(ValueError, SecondLevelModel().fit, flms, [])
        assert_raises(ValueError,
                      SecondLevelModel().fit, flms, confounds['conf1'])
        assert_raises(ValueError, SecondLevelModel().fit, flms, None, [])
import matplotlib.pyplot as plt

############################################################################
# Estimate second level model
# ---------------------------
# We define the input maps and the design matrix for the second level model
# and fit it.
import pandas as pd
second_level_input = data['cmaps']
design_matrix = pd.DataFrame([1] * len(second_level_input),
                             columns=['intercept'])

############################################################################
# Model specification and fit
from nistats.second_level_model import SecondLevelModel
second_level_model = SecondLevelModel(smoothing_fwhm=8.0)
second_level_model = second_level_model.fit(second_level_input,
                                            design_matrix=design_matrix)

##########################################################################
# To estimate the contrast is very simple. We can just provide the column
# name of the design matrix.
z_map = second_level_model.compute_contrast(output_type='z_score')

###########################################################################
# We threshold the second level contrast at uncorrected p < 0.001 and plot
from scipy.stats import norm
p_val = 0.001
p001_uncorrected = norm.isf(p_val)

from nistats.thresholding import cluster_level_inference