-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspm_funcs.py
More file actions
88 lines (69 loc) · 2.07 KB
/
spm_funcs.py
File metadata and controls
88 lines (69 loc) · 2.07 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
""" This module defines SPM-like functions
Run the tests with::
nosetests test_spm_funcs.py
The functions have docstrings according to the numpy docstring standard - see:
https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
"""
# Python 3 compatibility
from __future__ import print_function, division
import numpy as np
from scipy.stats import gamma
import nibabel as nib
def spm_global(vol):
""" Calculate SPM global metric for array `vol`
Parameters
----------
vol : array
Array giving image data, usually 3D.
Returns
-------
g : float
SPM global metric for `vol`
"""
T = np.mean(vol) / 8
return np.mean(vol[vol > T])
def get_spm_globals(fname):
""" Calculate SPM global metrics for volumes in image filename `fname`
Parameters
----------
fname : str
Filename of file containing 4D image
Returns
-------
spm_vals : array
SPM global metric for each 3D volume in the 4D image.
"""
img = nib.load(fname)
data = img.get_data()
spm_vals = []
for i in range(data.shape[-1]):
vol = data[..., i]
spm_vals.append(spm_global(vol))
return spm_vals
def spm_hrf(times):
""" Return values for standard SPM HRF at given `times`
This is the same as SPM's ``spm_hrf.m`` function using the default input
values.
Parameters
----------
times : array
Times at which to sample hemodynamic response function
Returns
-------
values : array
Array of same length as `times` giving HRF samples at corresponding
time post onset (where onset is T==0).
"""
# Gamma only defined for x values > 0
time_gt_0 = times > 0
ok_times = times[time_gt_0]
# Output vector
values = np.zeros(len(times))
# Gamma pdf for the peak
peak_values = gamma.pdf(ok_times, 6)
# Gamma pdf for the undershoot
undershoot_values = gamma.pdf(ok_times, 16)
# Combine them
values[time_gt_0] = peak_values - undershoot_values / 6.
# Divide by sum
return values / np.sum(values)