Repository navigation
Expand file tree
/
Copy pathconn_utils.py
More file actions
58 lines (43 loc) · 1.73 KB
/
Copy pathconn_utils.py
File metadata and controls
58 lines (43 loc) · 1.73 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
from functools import partial
import numpy as np
import jax
import jax.tree_util
import jax.numpy as jnp
@partial(jax.jit, static_argnames=['w'])
def get_windows(x, w):
ws = x.shape[1] - w + 1
f = jnp.r_[:ws]
slc = lambda f: jax.lax.dynamic_slice(x, (0, f), (x.shape[0], w))
wd = jax.vmap(slc)(f)
return wd
@partial(jax.jit, static_argnames=['set_negative_corr_to_zero'])
def get_ut_fc(x, set_negative_corr_to_zero=True):
fc = jnp.corrcoef(x, rowvar=True)
fc = jax.lax.cond(set_negative_corr_to_zero, lambda fc: fc * (fc > 0), lambda fc: fc, fc)
ut_fc = fc[jnp.triu_indices(fc.shape[0], k=1)]
return ut_fc
def get_fcd(x, w, set_negative_corr_to_zero=True):
n_overlap = w - 1
k = n_overlap//(w-n_overlap) + 1
wd = get_windows(x, w)
ut_fcs = jax.vmap(get_ut_fc, (0, None))(wd, set_negative_corr_to_zero)
fcd = jnp.corrcoef(ut_fcs, rowvar=True)
ut_fcd = fcd[jnp.triu_indices(fcd.shape[0], k=k)]
return fcd, ut_fcd
def compute_fc(data, axis=0):
triu_indices = jnp.triu_indices(data.shape[axis], k=1)
fc_array = jnp.corrcoef(data)
fc_triu = fc_array[triu_indices]
return fc_array, fc_triu.mean()
def compute_fcd(ts, window_length=20, overlap=19):
n_samples, n_regions = ts.shape
window_steps_size = window_length - overlap
n_windows = int(np.floor((n_samples - window_length) / window_steps_size + 1))
Isupdiag = np.triu_indices(n_regions, 1)
FC_t = np.zeros((int(n_regions*(n_regions-1)/2),n_windows))
for i in range(n_windows):
FCtemp = np.corrcoef(ts[window_steps_size*i:window_length+window_steps_size*i,:].T)
FCtemp = np.nan_to_num(FCtemp, nan=0)
FC_t[:,i] = FCtemp[Isupdiag]
FCD = np.corrcoef(FC_t.T)
return FCD