def nistats_confound_glm(nifti_file, confounds_file, which_confounds): import pandas as pd import nibabel as nb import numpy as np from nistats.regression import OLSModel from scipy.stats import zscore from nilearn.image import math_img from nilearn.plotting import plot_stat_map import matplotlib.pyplot as plt infile = nb.load(nifti_file) mean_img = math_img('np.mean(infile, axis=-1)', infile=infile) in_data = infile.get_data().astype(np.float32) confounds_table = pd.read_table(confounds_file)[which_confounds] # we assume the confounds nor the nifti_file need temporal filtering cfz = confounds_table.apply(zscore) cfz['intercept'] = np.ones(infile.shape[-1]) om = OLSModel(np.array(cfz)) om_rr = om.fit(in_data.reshape((-1,infile.shape[-1])).T) resid_img = nb.Nifti1Image(om_rr.resid.T.reshape(infile.shape).astype(np.float32), affine=infile.affine, header=infile.header) cleaned_img = math_img('(resid_img + mean_img[...,np.newaxis]).astype(np.float32)', resid_img=resid_img, mean_img=mean_img) output_nifti = nifti_file.replace('.nii.gz', '_nuis.nii.gz') cleaned_img.to_filename(output_nifti) output_pdf = confounds_file.replace('.tsv', '_sd-diff.pdf') f = plt.figure(figsize=(24,6)) plot_stat_map(math_img('(infile.std(axis=-1)-cleaned_img.std(axis=-1))/infile.std(axis=-1)', infile=infile, cleaned_img=cleaned_img), bg_img=mean_img, figure=f, cut_coords=(0,0,0), threshold=0.125, vmax=1, cmap='viridis', output_file=output_pdf) return output_pdf, output_nifti
def test_residuals(): Xintercept = X.copy() # If design matrix contains an intercept, the # mean of the residuals should be 0 (short of # some numerical rounding errors) Xintercept[:, 0] = 1 model = OLSModel(design=Xintercept) results = model.fit(Y) assert_almost_equal(results.residuals.mean(), 0) assert len(results.whitened_residuals) == 40
def test_predicted_r_square(): Xshort = X.copy()[:10, :] Yshort = Y.copy()[:10] # Signal of 10 elements should be completely # predicted by 10 predictors (short of some numerical # rounding errors) model = OLSModel(design=Xshort) results = model.fit(Yshort) assert_almost_equal(results.residuals.sum(), 0) assert_array_almost_equal(results.predicted, Yshort) assert_almost_equal(results.r_square, 1.0)
def test_resid_rename_warnings_ols(): model = OLSModel(design=X) results_ols = model.fit(Y) with pytest.warns(FutureWarning, match="'df_resid'"): assert_array_equal(results_ols.df_resid, results_ols.df_residuals) with pytest.warns(FutureWarning, match="'resid'"): assert_array_equal(results_ols.resid, results_ols.residuals) with pytest.warns(FutureWarning, match="'wY'"): assert_array_equal(results_ols.wY, results_ols.whitened_Y) with pytest.warns(FutureWarning, match="'wdesign'"): assert_array_equal(results_ols.wdesign, results_ols.whitened_design) with pytest.warns(FutureWarning, match="'wdesign'"): assert_array_equal(model.wdesign, model.whitened_design)
def test_OLS_degenerate(): Xd = X.copy() Xd[:, 0] = Xd[:, 1] + Xd[:, 2] model = OLSModel(design=Xd) results = model.fit(Y) assert results.df_residuals == 31
def test_OLS(): model = OLSModel(design=X) results = model.fit(Y) assert results.df_residuals == 30 assert results.residuals.shape[0] == 40 assert results.predicted.shape[0] == 40
""" import numpy as np # In fact we're testing methods defined in model from nistats.regression import OLSModel from nose.tools import assert_true, assert_equal, assert_raises from nose import SkipTest from numpy.testing import (assert_array_almost_equal, assert_array_equal) N = 10 X = np.c_[np.linspace(-1, 1, N), np.ones((N, ))] Y = np.r_[range(5), range(1, 6)] MODEL = OLSModel(X) RESULTS = MODEL.fit(Y) """ R script :: X = cbind(0:9 * 2/9 -1, 1) Y = as.matrix(c(0:4, 1:5)) results = lm(Y ~ X-1) print(results) print(summary(results)) gives:: Call: lm(formula = Y ~ X - 1)
import numpy as np # In fact we're testing methods defined in model from nistats.regression import OLSModel from nose.tools import assert_true, assert_equal, assert_raises from nose import SkipTest from numpy.testing import (assert_array_almost_equal, assert_array_equal) N = 10 X = np.c_[np.linspace(- 1, 1, N), np.ones((N,))] Y = np.r_[range(5), range(1, 6)] MODEL = OLSModel(X) RESULTS = MODEL.fit(Y) """ R script :: X = cbind(0:9 * 2/9 -1, 1) Y = as.matrix(c(0:4, 1:5)) results = lm(Y ~ X-1) print(results) print(summary(results)) gives:: Call:
def test_OLS_degenerate(): Xd = X.copy() Xd[:, 0] = Xd[:, 1] + Xd[:, 2] model = OLSModel(design=Xd) results = model.fit(Y) assert_equal(results.df_resid, 31)
def test_OLS(): model = OLSModel(design=X) results = model.fit(Y) assert_equal(results.df_resid, 30)