Пример #1
0
def test_ar_modeling():
    # Compare against standard routines
    rng = np.random.RandomState(20110903)
    N = 10
    Y = rng.normal(size=(N,1)) * 10 + 100
    X = np.c_[np.linspace(-1,1,N), np.ones((N,))]
    my_model = OLSModel(X)
    results = my_model.fit(Y)
    # fmristat wrapper
    rhos = estimateAR(results.resid, my_model.design, order=2)
    assert_equal(rhos.shape, (2,))
    assert_true(np.all(np.abs(rhos <= 1)))
    # standard routine
    rhos2 = ar_bias_correct(results, 2)
    assert_array_almost_equal(rhos, rhos2, 8)
    # Make 2D and 3D Y
    Y = rng.normal(size=(N,4)) * 10 + 100
    results = my_model.fit(Y)
    rhos = estimateAR(results.resid, my_model.design, order=2)
    assert_equal(rhos.shape, (2,4))
    assert_true(np.all(np.abs(rhos <= 1)))
    rhos2 = ar_bias_correct(results, 2)
    assert_array_almost_equal(rhos, rhos2, 8)
    # 3D
    results.resid = np.reshape(results.resid, (N,2,2))
    rhos = estimateAR(results.resid, my_model.design, order=2)
    assert_equal(rhos.shape, (2,2,2))
    assert_true(np.all(np.abs(rhos <= 1)))
    rhos2 = ar_bias_correct(results, 2)
    assert_array_almost_equal(rhos, rhos2, 8)
Пример #2
0
def test_ar_modeling():
    # Compare against standard routines
    rng = np.random.RandomState(20110903)
    N = 10
    Y = rng.normal(size=(N, 1)) * 10 + 100
    X = np.c_[np.linspace(-1, 1, N), np.ones((N, ))]
    my_model = OLSModel(X)
    results = my_model.fit(Y)
    # fmristat wrapper
    rhos = estimateAR(results.resid, my_model.design, order=2)
    assert_equal(rhos.shape, (2, ))
    assert_true(np.all(np.abs(rhos <= 1)))
    # standard routine
    rhos2 = ar_bias_correct(results, 2)
    assert_array_almost_equal(rhos, rhos2, 8)
    # Make 2D and 3D Y
    Y = rng.normal(size=(N, 4)) * 10 + 100
    results = my_model.fit(Y)
    rhos = estimateAR(results.resid, my_model.design, order=2)
    assert_equal(rhos.shape, (2, 4))
    assert_true(np.all(np.abs(rhos <= 1)))
    rhos2 = ar_bias_correct(results, 2)
    assert_array_almost_equal(rhos, rhos2, 8)
    # 3D
    results.resid = np.reshape(results.resid, (N, 2, 2))
    rhos = estimateAR(results.resid, my_model.design, order=2)
    assert_equal(rhos.shape, (2, 2, 2))
    assert_true(np.all(np.abs(rhos <= 1)))
    rhos2 = ar_bias_correct(results, 2)
    assert_array_almost_equal(rhos, rhos2, 8)
Пример #3
0
def test_altprotocol():
    block, bT, bF = protocol(descriptions['block'], 'block', *delay.spectral)
    event, eT, eF = protocol(descriptions['event'], 'event', *delay.spectral)

    blocka, baT, baF = altprotocol(altdescr['block'], 'block', *delay.spectral)
    eventa, eaT, eaF = altprotocol(altdescr['event'], 'event', *delay.spectral)

    for c in bT.keys():
        baf = baT[c]
        if not isinstance(baf, formulae.Formula):
            baf = formulae.Formula([baf])

        bf = bT[c]
        if not isinstance(bf, formulae.Formula):
            bf = formulae.Formula([bf])

    X = baf.design(t, return_float=True)
    Y = bf.design(t, return_float=True)
    if X.ndim == 1:
        X.shape = (X.shape[0], 1)
    m = OLSModel(X)
    r = m.fit(Y)
    remaining = (r.resid**2).sum() / (Y**2).sum()
    assert_almost_equal(remaining, 0)

    for c in bF.keys():
        baf = baF[c]
        if not isinstance(baf, formulae.Formula):
            baf = formulae.Formula([baf])

        bf = bF[c]
        if not isinstance(bf, formulae.Formula):
            bf = formulae.Formula([bf])

    X = baf.design(t, return_float=True)
    Y = bf.design(t, return_float=True)
    if X.ndim == 1:
        X.shape = (X.shape[0], 1)
    m = OLSModel(X)
    r = m.fit(Y)
    remaining = (r.resid**2).sum() / (Y**2).sum()
    assert_almost_equal(remaining, 0)
Пример #4
0
    def fit(self, Y, model='ar1', steps=100):
        """GLM fitting of a dataset using 'ols' regression or the two-pass

        Parameters
        ----------
        Y : array of shape(n_time_points, n_samples)
            the fMRI data
        model : {'ar1', 'ols'}, optional
            the temporal variance model. Defaults to 'ar1'
        steps : int, optional
            Maximum number of discrete steps for the AR(1) coef histogram
        """
        if model not in ['ar1', 'ols']:
            raise ValueError('Unknown model')

        if Y.ndim == 1:
            Y = Y[:, np.newaxis]

        if Y.shape[0] != self.X.shape[0]:
            raise ValueError('Response and predictors are inconsistent')

        # fit the OLS model
        ols_result = OLSModel(self.X).fit(Y)

        # compute and discretize the AR1 coefs
        ar1 = ((ols_result.resid[1:] * ols_result.resid[:-1]).sum(0) /
               (ols_result.resid**2).sum(0))
        ar1 = (ar1 * steps).astype(np.int) * 1. / steps

        # Fit the AR model acccording to current AR(1) estimates
        if model == 'ar1':
            self.results_ = {}
            self.labels_ = ar1
            # fit the model
            for val in np.unique(self.labels_):
                m = ARModel(self.X, val)
                self.results_[val] = m.fit(Y[:, self.labels_ == val])
        else:
            self.labels_ = np.zeros(Y.shape[1])
            self.results_ = {0.0: ols_result}
Пример #5
0
def test_altprotocol():
    block, bT, bF = protocol(descriptions['block'], 'block', *delay.spectral)
    event, eT, eF = protocol(descriptions['event'], 'event', *delay.spectral)

    blocka, baT, baF = altprotocol(altdescr['block'], 'block', *delay.spectral)
    eventa, eaT, eaF = altprotocol(altdescr['event'], 'event', *delay.spectral)

    for c in bT.keys():
        baf = baT[c]
        if not isinstance(baf, formulae.Formula):
            baf = formulae.Formula([baf])

        bf = bT[c]
        if not isinstance(bf, formulae.Formula):
            bf = formulae.Formula([bf])

    X = baf.design(t, return_float=True)
    Y = bf.design(t, return_float=True)
    if X.ndim == 1:
        X.shape = (X.shape[0], 1)
    m = OLSModel(X)
    r = m.fit(Y)
    remaining = (r.resid**2).sum() / (Y**2).sum()
    assert_almost_equal(remaining, 0)

    for c in bF.keys():
        baf = baF[c]
        if not isinstance(baf, formulae.Formula):
            baf = formulae.Formula([baf])

        bf = bF[c]
        if not isinstance(bf, formulae.Formula):
            bf = formulae.Formula([bf])

    X = baf.design(t, return_float=True)
    Y = bf.design(t, return_float=True)
    if X.ndim == 1:
        X.shape = (X.shape[0], 1)
    m = OLSModel(X)
    r = m.fit(Y)
    remaining = (r.resid**2).sum() / (Y**2).sum()
    assert_almost_equal(remaining, 0)
Пример #6
0
    tempdict = {}
    for v in ['sd', 't', 'effect']:
        tempdict[v] = np.zeros(mask_array.sum())
    output[contrast_id] = tempdict


########################################
# Perform a GLM analysis
########################################

print 'Fitting a GLM (this takes time)...'
fmri_image = load(data_path)
Y = fmri_image.get_data()[mask_array]
X = design_matrix.matrix

m = OLSModel(X)
# Fit the model, storing an estimate of an AR(1) parameter at each voxel
result = m.fit(Y.T)
ar1 = ((result.resid[1:] * result.resid[:-1]).sum(0) /
          (result.resid ** 2).sum(0))
ar1 *= 100
ar1 = ar1.astype(np.int) / 100.


for val in np.unique(ar1):
    armask = np.equal(ar1, val)
    m = ARModel(X, val)
    d = Y[armask]
    results = m.fit(d.T)
    
    # Output the results for each contrast
Пример #7
0

# Module globals
FIMG = load_image(funcfile)
# Put time on first axis
FIMG = rollimg(FIMG, 't')
FDATA = FIMG.get_data()
FIL = FmriImageList.from_image(FIMG)

# I think it makes more sense to use FDATA instead of FIL for GLM
# purposes -- reduces some noticeable overhead in creating the
# array from FmriImageList

# create a design matrix, model and contrast matrix
DESIGN = noise((FDATA.shape[0], 3))
MODEL = OLSModel(DESIGN)
CMATRIX = np.array([[1, 0, 0], [0, 1, 0]])


# two prototypical functions in a GLM analysis
def fit(input):
    return MODEL.fit(input).resid


def contrast(results):
    return results.Fcontrast(CMATRIX)


# generators
def result_generator(datag):
    for i, fdata in datag:
Пример #8
0
import nipy
from nipy.algorithms.statistics.models.model import Model, \
    LikelihoodModel, \
    LikelihoodModelResults
from random import seed
from random import random
from sklearn.metrics import mean_squared_error
import numpy as np

from numpy.random import standard_normal as stan
from nipy.algorithms.statistics.models.regression import OLSModel

x = np.hstack((stan((30, 1)), stan((30, 1)), stan((30, 1))))
beta = np.array([3.25, 1.5, 7.0])
y = np.dot(x, beta) + stan(30)
model = OLSModel(x).fit(y)
confidence_intervals = model.conf_int(cols=(1, 2))
print(confidence_intervals)
FIM = LikelihoodModel.information
rsults = LikelihoodModel.information(FIM, theta=x, nuisance=None)
'''seems this nipy doesn't work
    or I read the documentation wrong. Medical or Mathematical package?
'''


class FisherMatrix:
    def __init__(self):
        self.pvalue = False

    def info_matrix(self, theta_estimate):
        FIMatrix = LikelihoodModel.information(theta_estimate, nuisance=None)