complexeva 0.1.7__tar.gz → 0.1.9__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.
Files changed (31) hide show
  1. {complexeva-0.1.7 → complexeva-0.1.9}/PKG-INFO +1 -1
  2. complexeva-0.1.9/complexeva/measures/__init__.py +5 -0
  3. complexeva-0.1.9/complexeva/measures/dimension_1/__init__.py +14 -0
  4. complexeva-0.1.9/complexeva/measures/dimension_1/compression_complexity.py +25 -0
  5. complexeva-0.1.9/complexeva/measures/dimension_1/kolmogorov_complexity.py +24 -0
  6. complexeva-0.1.9/complexeva/measures/dimension_1/lyapunov_exponent.py +44 -0
  7. complexeva-0.1.9/complexeva/signal/co_activation.py +100 -0
  8. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva.egg-info/PKG-INFO +1 -1
  9. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva.egg-info/SOURCES.txt +5 -0
  10. {complexeva-0.1.7 → complexeva-0.1.9}/setup.py +1 -1
  11. complexeva-0.1.7/complexeva/measures/__init__.py +0 -6
  12. {complexeva-0.1.7 → complexeva-0.1.9}/README.md +0 -0
  13. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/__init__.py +0 -0
  14. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/dummies/__init__.py +0 -0
  15. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/dummies/matrix.py +0 -0
  16. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/dummies/networks.py +0 -0
  17. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/measures/dimension_1/higuchi.py +0 -0
  18. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/measures/dimension_1/lempel_ziv.py +0 -0
  19. {complexeva-0.1.7/complexeva/measures/dimension_1 → complexeva-0.1.9/complexeva/measures/dimension_2}/__init__.py +0 -0
  20. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/measures/dimension_2/cbb_matrix.py +0 -0
  21. {complexeva-0.1.7/complexeva/measures/dimension_2 → complexeva-0.1.9/complexeva/measures/network_space}/__init__.py +0 -0
  22. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/measures/network_space/cbb_networkx.py +0 -0
  23. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/measures/validate.py +0 -0
  24. {complexeva-0.1.7/complexeva/measures/network_space → complexeva-0.1.9/complexeva/signal}/__init__.py +0 -0
  25. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/utils/__init__.py +0 -0
  26. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/utils/convert_image_matrix.py +0 -0
  27. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/utils/convert_matrix_network.py +0 -0
  28. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva/utils/device.py +0 -0
  29. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva.egg-info/dependency_links.txt +0 -0
  30. {complexeva-0.1.7 → complexeva-0.1.9}/complexeva.egg-info/top_level.txt +0 -0
  31. {complexeva-0.1.7 → complexeva-0.1.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: complexeva
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Home-page:
5
5
  Author: Don Yin
6
6
  Author-email: don_yin@outlook.com
@@ -0,0 +1,5 @@
1
+ from .dimension_1 import *
2
+ from .dimension_2.cbb_matrix import FractalHandlerMatrix
3
+ from .network_space.cbb_networkx import FractalHandlerNetworkX
4
+
5
+ __all__ = ["FractalHandlerMatrix", "FractalHandlerNetworkX", *dimension_1.__all__]
@@ -0,0 +1,14 @@
1
+ from .compression_complexity import compression_complexity
2
+ from .lyapunov_exponent import lyapunov_exponent
3
+ from .kolmogorov_complexity import kolmogorov_complexity
4
+ from .higuchi import hfd_matlab_equivalent, hfd_pyeeg
5
+ from .lempel_ziv import lempel_ziv
6
+
7
+ __all__ = [
8
+ "compression_complexity",
9
+ "lyapunov_exponent",
10
+ "kolmogorov_complexity",
11
+ "hfd_matlab_equivalent",
12
+ "hfd_pyeeg",
13
+ "lempel_ziv",
14
+ ]
@@ -0,0 +1,25 @@
1
+ import zlib
2
+ import numpy as np
3
+
4
+
5
+ def compression_complexity(data):
6
+ """
7
+ Calculate compression complexity using zlib compression.
8
+
9
+ Args:
10
+ data: numpy array or list of numerical values
11
+
12
+ Returns:
13
+ float: compression complexity score
14
+ """
15
+ # Convert numerical data to bytes
16
+ if isinstance(data, np.ndarray):
17
+ data = data.tobytes()
18
+ elif isinstance(data, list):
19
+ data = np.array(data).tobytes()
20
+ else:
21
+ raise ValueError("Input must be a numpy array or list")
22
+
23
+ # Calculate compression ratio
24
+ compressed = zlib.compress(data)
25
+ return len(compressed) / len(data) # Normalized complexity score
@@ -0,0 +1,24 @@
1
+ import zlib
2
+ import numpy as np
3
+
4
+
5
+ def kolmogorov_complexity(data):
6
+ """
7
+ Calculate an approximation of Kolmogorov complexity using zlib compression.
8
+
9
+ Args:
10
+ data: numpy array or list of numerical values
11
+
12
+ Returns:
13
+ int: approximated Kolmogorov complexity (compressed length)
14
+ """
15
+ # Convert numerical data to bytes
16
+ if isinstance(data, np.ndarray):
17
+ data = data.tobytes()
18
+ elif isinstance(data, list):
19
+ data = np.array(data).tobytes()
20
+ else:
21
+ raise ValueError("Input must be a numpy array or list")
22
+
23
+ # Calculate compressed length as approximation of Kolmogorov complexity
24
+ return len(zlib.compress(data))
@@ -0,0 +1,44 @@
1
+ import numpy as np
2
+ from scipy.spatial.distance import pdist, squareform
3
+
4
+
5
+ def lyapunov_exponent(data, tau=1, m=2, dt=1):
6
+ """
7
+ Calculate the largest Lyapunov exponent for a time series.
8
+
9
+ Args:
10
+ data: numpy array or list of numerical values
11
+ tau: time delay (default=1)
12
+ m: embedding dimension (default=2)
13
+ dt: time step (default=1)
14
+
15
+ Returns:
16
+ float: largest Lyapunov exponent
17
+ """
18
+ # Convert to numpy array if needed
19
+ x = np.array(data)
20
+ N = len(x)
21
+
22
+ # Create time-delay embedding
23
+ Y = np.zeros((N - (m - 1) * tau, m))
24
+ for i in range(m):
25
+ Y[:, i] = x[i * tau : N - (m - 1) * tau + i * tau]
26
+
27
+ # Calculate pairwise distances
28
+ D = squareform(pdist(Y))
29
+ np.fill_diagonal(D, np.inf) # Exclude self-matches
30
+
31
+ # Find nearest neighbors
32
+ nearest = np.argmin(D, axis=1)
33
+
34
+ # Calculate divergence
35
+ div = np.zeros(N - (m - 1) * tau - 1)
36
+ for i in range(len(div)):
37
+ div[i] = np.abs(x[i + 1] - x[nearest[i] + 1])
38
+
39
+ # Calculate Lyapunov exponent
40
+ positive_div = div[div > 0] # Avoid log(0)
41
+ if len(positive_div) == 0:
42
+ return 0.0
43
+
44
+ return np.mean(np.log(positive_div)) / dt
@@ -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")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: complexeva
3
- Version: 0.1.7
3
+ Version: 0.1.9
4
4
  Home-page:
5
5
  Author: Don Yin
6
6
  Author-email: don_yin@outlook.com
@@ -11,12 +11,17 @@ complexeva/dummies/networks.py
11
11
  complexeva/measures/__init__.py
12
12
  complexeva/measures/validate.py
13
13
  complexeva/measures/dimension_1/__init__.py
14
+ complexeva/measures/dimension_1/compression_complexity.py
14
15
  complexeva/measures/dimension_1/higuchi.py
16
+ complexeva/measures/dimension_1/kolmogorov_complexity.py
15
17
  complexeva/measures/dimension_1/lempel_ziv.py
18
+ complexeva/measures/dimension_1/lyapunov_exponent.py
16
19
  complexeva/measures/dimension_2/__init__.py
17
20
  complexeva/measures/dimension_2/cbb_matrix.py
18
21
  complexeva/measures/network_space/__init__.py
19
22
  complexeva/measures/network_space/cbb_networkx.py
23
+ complexeva/signal/__init__.py
24
+ complexeva/signal/co_activation.py
20
25
  complexeva/utils/__init__.py
21
26
  complexeva/utils/convert_image_matrix.py
22
27
  complexeva/utils/convert_matrix_network.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="complexeva",
5
- version="0.1.7",
5
+ version="0.1.9",
6
6
  packages=find_packages(),
7
7
  install_requires=[],
8
8
  author="Don Yin",
@@ -1,6 +0,0 @@
1
- from .dimension_1.lempel_ziv import lempel_ziv
2
- from .dimension_1.higuchi import hfd_matlab_equivalent, hfd_pyeeg
3
- from .dimension_2.cbb_matrix import FractalHandlerMatrix
4
- from .network_space.cbb_networkx import FractalHandlerNetworkX
5
-
6
- __all__ = ["lempel_ziv", "hfd_matlab_equivalent", "hfd_pyeeg", "FractalHandlerMatrix", "FractalHandlerNetworkX"]
File without changes
File without changes