complexeva 0.1.7__tar.gz → 0.1.8__tar.gz
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.
- {complexeva-0.1.7 → complexeva-0.1.8}/PKG-INFO +1 -1
- complexeva-0.1.8/complexeva/signal/__init__.py +0 -0
- complexeva-0.1.8/complexeva/signal/co_activation.py +100 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva.egg-info/PKG-INFO +1 -1
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva.egg-info/SOURCES.txt +2 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/setup.py +1 -1
- {complexeva-0.1.7 → complexeva-0.1.8}/README.md +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/dummies/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/dummies/matrix.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/dummies/networks.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/dimension_1/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/dimension_1/higuchi.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/dimension_1/lempel_ziv.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/dimension_2/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/dimension_2/cbb_matrix.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/network_space/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/network_space/cbb_networkx.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/measures/validate.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/utils/__init__.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/utils/convert_image_matrix.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/utils/convert_matrix_network.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva/utils/device.py +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva.egg-info/dependency_links.txt +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/complexeva.egg-info/top_level.txt +0 -0
- {complexeva-0.1.7 → complexeva-0.1.8}/setup.cfg +0 -0
|
File without changes
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/Users/donyin/miniconda3/envs/rotation-1/bin/python
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
- now we have two 1d series as signals make dummy data using numpy
|
|
5
|
+
- use either a sliding window or regression method to produce a new signal that represents the co-activation between the two signals
|
|
6
|
+
- in the sliding_window method, the window_size parameter is used to determine the size of the window used to calculate the co-activation
|
|
7
|
+
- it moves one step at a time and calculates the correlation between the two signals in the window
|
|
8
|
+
- in the regression method, the two signals are used as independent and dependent variables in a linear regression model
|
|
9
|
+
- the resulting signal is the predicted dependent variable
|
|
10
|
+
- if we use the sliding_window method, the window_size parameter must be an integer greater than 0
|
|
11
|
+
- if we use the regression method, we must plot the two signal and the resulting signal
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
from sklearn.linear_model import LinearRegression
|
|
16
|
+
import matplotlib.pyplot as plt
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def get_co_activation(
|
|
20
|
+
signal_1: np.ndarray, signal_2: np.ndarray, method: str, window_size: int = None, save_plot_as=None
|
|
21
|
+
) -> np.ndarray:
|
|
22
|
+
"""
|
|
23
|
+
Get a measure of co-activation between two 1D signals.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
signal_1: First 1D time series signal
|
|
27
|
+
signal_2: Second 1D time series signal
|
|
28
|
+
method: Either 'sliding_window' or 'regression'
|
|
29
|
+
window_size: Size of the window for 'sliding_window' method (must be an integer greater than 0)
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
np.ndarray: A new signal representing the co-activation between the two signals
|
|
33
|
+
"""
|
|
34
|
+
match method:
|
|
35
|
+
case "sliding_window":
|
|
36
|
+
if window_size is None or window_size <= 0:
|
|
37
|
+
raise ValueError("window_size must be an integer greater than 0 for sliding_window method")
|
|
38
|
+
correlations = []
|
|
39
|
+
num_windows = len(signal_1) - window_size + 1
|
|
40
|
+
for i in range(num_windows):
|
|
41
|
+
window_signal_1 = signal_1[i : i + window_size]
|
|
42
|
+
window_signal_2 = signal_2[i : i + window_size]
|
|
43
|
+
corr = np.corrcoef(window_signal_1, window_signal_2)[0, 1]
|
|
44
|
+
correlations.append(corr)
|
|
45
|
+
|
|
46
|
+
# Plot signals and correlations
|
|
47
|
+
correlations = np.array(correlations)
|
|
48
|
+
if save_plot_as:
|
|
49
|
+
plt.figure(figsize=(10, 5))
|
|
50
|
+
plt.plot(signal_1, label="Signal 1", alpha=0.7)
|
|
51
|
+
plt.plot(signal_2, label="Signal 2", alpha=0.7)
|
|
52
|
+
plt.plot(
|
|
53
|
+
np.arange(window_size // 2, len(correlations) + window_size // 2),
|
|
54
|
+
correlations,
|
|
55
|
+
label="Sliding Window Correlation",
|
|
56
|
+
linestyle="--",
|
|
57
|
+
)
|
|
58
|
+
plt.legend()
|
|
59
|
+
plt.title(f"Sliding Window Method Co-Activation (window size={window_size})")
|
|
60
|
+
plt.savefig(save_plot_as, dpi=320)
|
|
61
|
+
plt.close()
|
|
62
|
+
|
|
63
|
+
return correlations
|
|
64
|
+
|
|
65
|
+
case "regression":
|
|
66
|
+
# Reshape signals for regression
|
|
67
|
+
X = signal_1.reshape(-1, 1)
|
|
68
|
+
y = signal_2
|
|
69
|
+
|
|
70
|
+
# Fit linear regression
|
|
71
|
+
model = LinearRegression()
|
|
72
|
+
model.fit(X, y)
|
|
73
|
+
|
|
74
|
+
# Predict y
|
|
75
|
+
y_pred = model.predict(X)
|
|
76
|
+
|
|
77
|
+
# Plot signals
|
|
78
|
+
if save_plot_as:
|
|
79
|
+
plt.figure(figsize=(10, 5))
|
|
80
|
+
plt.plot(signal_1, label="Signal 1 (IV)")
|
|
81
|
+
plt.plot(signal_2, label="Signal 2 (DV)")
|
|
82
|
+
plt.plot(y_pred, label="Predicted DV", linestyle="--")
|
|
83
|
+
plt.legend()
|
|
84
|
+
plt.title("Regression Method Co-Activation")
|
|
85
|
+
plt.savefig(save_plot_as, dpi=320)
|
|
86
|
+
plt.close()
|
|
87
|
+
|
|
88
|
+
return y_pred
|
|
89
|
+
|
|
90
|
+
case _:
|
|
91
|
+
raise ValueError("method must be 'sliding_window' or 'regression'")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
if __name__ == "__main__":
|
|
95
|
+
t = np.linspace(0, 10, 1000)
|
|
96
|
+
signal1 = np.sin(2 * np.pi * 0.5 * t) + 0.2 * np.random.randn(1000)
|
|
97
|
+
signal2 = 0.8 * np.sin(2 * np.pi * 0.5 * t + np.pi / 4) + 0.2 * np.random.randn(1000)
|
|
98
|
+
|
|
99
|
+
get_co_activation(signal1, signal2, method="sliding_window", window_size=32, save_plot_as="coactivation_sliding_window.png")
|
|
100
|
+
get_co_activation(signal1, signal2, method="regression", save_plot_as="coactivation_regression.png")
|
|
@@ -17,6 +17,8 @@ complexeva/measures/dimension_2/__init__.py
|
|
|
17
17
|
complexeva/measures/dimension_2/cbb_matrix.py
|
|
18
18
|
complexeva/measures/network_space/__init__.py
|
|
19
19
|
complexeva/measures/network_space/cbb_networkx.py
|
|
20
|
+
complexeva/signal/__init__.py
|
|
21
|
+
complexeva/signal/co_activation.py
|
|
20
22
|
complexeva/utils/__init__.py
|
|
21
23
|
complexeva/utils/convert_image_matrix.py
|
|
22
24
|
complexeva/utils/convert_matrix_network.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|