timeawarepc 1.2.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ """
2
+ Time-Aware PC Algorithm for estimating Causal Functional Connectivity from Time Series
3
+ """
4
+
5
+ __version__ = "0.0.1"
6
+ __all__ = ["tpc_helpers","tpc","find_cfc","pcalg","pcalg_helpers","simulate_data"]
@@ -0,0 +1,38 @@
1
+ """Convenient wrapper for functions in the library.
2
+ """
3
+ __all__ = ["find_cfc"]
4
+ from timeawarepc.tpc import *
5
+ def find_cfc(data,method_name,alpha=0.05,maxdelay=1,niter=50,thresh=0.25,isgauss=False):
6
+ """Estimate Causal Functional Connectivity (CFC) between nodes from time series.
7
+ This is a wrapper for functions cfc_tpc, cfc_pc, cfc_gc in tpc.py.
8
+ Refer to the individual functions for their details.
9
+
10
+ Args:
11
+ data: (numpy.array) of shape (n,p) with n time-recordings for p nodes
12
+ method_name: (string)
13
+ 'TPC': Implements TPC Algorithm,
14
+ 'PC': PC Algorithm,
15
+ 'GC': Granger Causality.
16
+ alpha: (float) Significance level
17
+ isgauss: (boolean) Arg used for method_name == 'PC' or 'TPC'.
18
+ True: Assume Gaussian Noise distribution,
19
+ False: Distribution free.
20
+ maxdelay: (int) Maximum time-delay of interactions. Arg used for method_name == 'GC' or 'TPC'.
21
+ subsampsize: (int) Bootstrap window width in TPC. Arg used for method_name == 'TPC'.
22
+ niter: (int) Number of bootstrap iterations in TPC. Arg used for method_name == 'TPC'.
23
+ thresh: (float) Bootstrap stability cut-off in TPC. Arg used for method_name == 'TPC'.
24
+
25
+ Returns:
26
+ adjacency: (numpy.array) Adcajency matrix of estimated CFC by chosen method.
27
+ weights: (numpy.array) Connectivity Weights in the CFC
28
+
29
+ """
30
+
31
+ if method_name == 'TPC':
32
+ adjacency, weights = cfc_tpc(data,maxdelay=maxdelay,alpha=alpha,niter=niter,thresh=thresh,isgauss=isgauss)
33
+ elif method_name == 'PC':
34
+ adjacency, weights = cfc_pc(data,alpha,isgauss=isgauss)
35
+ elif method_name == 'GC':
36
+ from timeawarepc.gc import cfc_gc
37
+ adjacency, weights = cfc_gc(data,maxdelay,alpha)
38
+ return adjacency,weights
timeawarepc/gc.py ADDED
@@ -0,0 +1,31 @@
1
+ import numpy as np
2
+ import nitime.analysis as nta
3
+ import nitime.timeseries as ts
4
+ def cfc_gc(data,maxdelay,alpha):
5
+ """Estimate Causal Functional Connectivity using Granger Causality.
6
+
7
+ Args:
8
+ data: (numpy.array) of shape (n,p) with n samples for p nodes
9
+ maxdelay: Maximum time-delay of interactions.
10
+ alpha: (float) Significance level for conditional independence tests
11
+
12
+ Returns:
13
+ adjacency: (numpy.array) Adcajency matrix of shape (p,p) of estimated CFC by Granger Causality.
14
+ weights: (numpy.array) Connectivity Weight matrix of shape (p,p).
15
+
16
+ """
17
+ TR = 1
18
+ thresh = 0
19
+ time_series = ts.TimeSeries(data.T, sampling_interval=TR)
20
+ order=maxdelay
21
+ G = nta.GrangerAnalyzer(time_series, order=order)
22
+ adj_mat = np.zeros((data.shape[1],data.shape[1]))
23
+
24
+ adj_mat=np.mean(np.nan_to_num(G.causality_xy[:, :]),-1)+np.mean(np.nan_to_num(G.causality_yx[:, :]),-1).T
25
+ adjmat1=np.mean(np.nan_to_num(G.causality_xy[:, :]),-1)
26
+ adjmat2=np.mean(np.nan_to_num(G.causality_yx[:, :]),-1)
27
+ adj_mat=adjmat1+adjmat2.T
28
+ weights = adj_mat
29
+ thresh = np.percentile(adj_mat,(1-alpha)*100)
30
+ adjacency=(adj_mat > thresh).astype(int)
31
+ return adjacency, weights