forked from macshine/coupling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coupling.py
57 lines (40 loc) · 1.32 KB
/
coupling.py
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
import numpy as np
import pandas as pd
def coupling(data,window):
"""
creates a functional coupling metric from 'data'
data: should be organized in 'time x nodes' matrix
smooth: smoothing parameter for dynamic coupling score
"""
#define variables
[tr,nodes] = data.shape
der = tr-1
td = np.zeros((der,nodes))
td_std = np.zeros((der,nodes))
data_std = np.zeros(nodes)
mtd = np.zeros((der,nodes,nodes))
sma = np.zeros((der,nodes*nodes))
#calculate temporal derivative
for i in range(0,nodes):
for t in range(0,der):
td[t,i] = data[t+1,i] - data[t,i]
#standardize data
for i in range(0,nodes):
data_std[i] = np.std(td[:,i])
td_std = td / data_std
#functional coupling score
for t in range(0,der):
for i in range(0,nodes):
for j in range(0,nodes):
mtd[t,i,j] = td_std[t,i] * td_std[t,j]
#temporal smoothing
temp = np.reshape(mtd,[der,nodes*nodes])
sma = pd.rolling_mean(temp,window)
sma = np.reshape(sma,[der,nodes,nodes])
return (mtd, sma)
#input the variables 'd' (data) and 's' (smooth)
#I've made 'd' random for now, but this could just as easily be real data
d = np.random.rand(200,5)
s = 9
#run the script
coupling(d,s)