File: glm_lowlevel.py

package info (click to toggle)
nipy 0.1.2%2B20100526-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,992 kB
  • ctags: 13,434
  • sloc: python: 47,720; ansic: 41,334; makefile: 197
file content (61 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
This example routine simulates a number of pure Gaussian white noise 
signals, then fits each one in terms of two regressors: a constant baseline, 
and a linear function of time. The voxelwise t statistics associated 
with the baseline coefficient are then computed. 
"""
print __doc__

import numpy as np
from nipy.neurospin import glm

dimt = 100
dimx = 10
dimy = 11
dimz = 12 

# axis defines the "time direction" 

y = np.random.randn(dimt, dimx*dimy*dimz)
axis = 0

"""
y = random.randn(dimx, dimt, dimy, dimz)
axis = 1
"""

X = np.array([np.ones(dimt), range(dimt)])
X = X.transpose() ## the design matrix X must have dimt lines

#mod = glm.glm(y, X, axis=axis) ## default is spherical model using OLS 
mod = glm.glm(y, X, axis=axis, model='ar1')
#mod = glm.glm(y, X, formula='y~x1+(x1|x2)', axis=axis, model='mfx')

##mod.save('toto')
##mod = glm.load('toto')

# Define a t contrast
tcon = mod.contrast([1,0]) 

# Compute the t-stat
t = tcon.stat()
## t = tcon.stat(baseline=1) to test effects > 1 

# Compute the p-value
p = tcon.pvalue()

# Compute the z-score
z = tcon.zscore()

# Perform a F test without keeping the F stat
p = mod.contrast([[1,0],[1,-1]]).pvalue()

# Perform a conjunction test similarly 
##p = mod.contrast([[1,0],[1,-1]], type='tmin').pvalue()

print np.shape(y)
print np.shape(X)
print np.shape(z)