File: glm_lowlevel.py

package info (click to toggle)
nipy 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,352 kB
  • sloc: python: 39,115; ansic: 30,931; makefile: 210; sh: 93
file content (49 lines) | stat: -rwxr-xr-x 1,147 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
#!/usr/bin/env python3
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
__doc__ = """
This example 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.modalities.fmri.glm import GeneralLinearModel

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

# axis defines the "time direction"
y = np.random.randn(dimt, dimx * dimy * dimz)
axis = 0

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

mod = GeneralLinearModel(X)
mod.fit(y)

# 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.p_value()

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

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

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