-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
146 lines (118 loc) · 4.69 KB
/
Copy pathdemo.py
File metadata and controls
146 lines (118 loc) · 4.69 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
Demo Script for Functional Connectivity Analyzer
This script demonstrates the basic functionality of the Functional Connectivity Analyzer
using synthetic fMRI data.
"""
import numpy as np
import matplotlib.pyplot as plt
from connectivity_analyzer import FunctionalConnectivityAnalyzer
from utils import create_synthetic_data, create_roi_labels, print_analysis_summary
def main():
"""Run the demo."""
print("🚀 FUNCTIONAL CONNECTIVITY ANALYZER DEMO")
print("=" * 50)
# Create synthetic fMRI data
print("\n📊 Creating synthetic fMRI data...")
n_timepoints = 150
n_rois = 8
sampling_rate = 1.0
time_series, true_connectivity = create_synthetic_data(
n_timepoints=n_timepoints,
n_rois=n_rois,
sampling_rate=sampling_rate,
noise_level=0.1,
connectivity_strength=0.6
)
roi_labels = create_roi_labels(n_rois, atlas_type="custom")
print(f" ✅ Created time series: {time_series.shape}")
print(f" ✅ Number of ROIs: {n_rois}")
print(f" ✅ Time points: {n_timepoints}")
# Initialize analyzer
print("\n🔧 Initializing analyzer...")
analyzer = FunctionalConnectivityAnalyzer(
sampling_rate=sampling_rate,
detrend=True,
normalize=True,
verbose=True
)
# Set data
analyzer.roi_signals = time_series
analyzer.roi_labels = roi_labels
# Compute connectivity metrics
print("\n🧮 Computing connectivity metrics...")
methods = ['coherence', 'plv', 'pearson_correlation']
for method in methods:
print(f" Computing {method}...")
matrix = analyzer.compute_connectivity(
method=method,
frequency_band=(0.01, 0.1) # Slow-5 frequency band
)
print(f" ✅ {method} computed successfully")
# Visualize results
print("\n📈 Creating visualizations...")
for method in methods:
matrix = analyzer.connectivity_matrices[method]
# Plot connectivity matrix
analyzer.plot_connectivity_matrix(
matrix,
roi_labels=roi_labels,
title=f"{method.upper()} Connectivity Matrix"
)
# Plot network graph
analyzer.plot_network_graph(
matrix,
roi_labels=roi_labels,
threshold=0.3
)
# Network analysis
print("\n🌐 Analyzing network properties...")
for method in methods:
matrix = analyzer.connectivity_matrices[method]
network_props = analyzer.analyze_network_properties(matrix, threshold=0.3)
print(f"\n {method.upper()} Network Properties:")
print(f" • Nodes: {network_props['n_nodes']}")
print(f" • Edges: {network_props['n_edges']}")
print(f" • Density: {network_props['density']:.3f}")
print(f" • Clustering: {network_props['average_clustering']:.3f}")
print(f" • Modularity: {network_props['modularity']:.3f}")
# Statistical analysis
print("\n📊 Performing statistical analysis...")
for method in methods:
matrix = analyzer.connectivity_matrices[method]
stats_results = analyzer.statistical_analysis(
matrix,
method='fdr',
threshold=0.05
)
n_significant = np.sum(stats_results['significant_connections'])
print(f" {method}: {n_significant} significant connections")
# Save results
print("\n💾 Saving results...")
analyzer.save_results(
output_dir="demo_results",
prefix="demo"
)
# Print summary
print("\n📋 Analysis Summary:")
summary = analyzer.get_summary()
print_analysis_summary(summary)
# Compare with true connectivity
print("\n🔍 Comparing with true connectivity...")
for method in methods:
matrix = analyzer.connectivity_matrices[method]
from utils import compute_matrix_similarity
similarity = compute_matrix_similarity(matrix, true_connectivity, method='correlation')
print(f" {method} similarity: {similarity:.3f}")
print("\n" + "=" * 50)
print("🎉 DEMO COMPLETED SUCCESSFULLY!")
print("=" * 50)
print("\n📁 Results saved in 'demo_results/' directory")
print("📊 Check the generated plots for visualizations")
print("📄 CSV files contain the connectivity matrices")
print("\n🔗 Next steps:")
print(" • Try the examples/ directory for more advanced usage")
print(" • Run tests with: python -m tests.test_connectivity_analyzer")
print(" • Check the README.md for detailed documentation")
if __name__ == "__main__":
main()