summaryrefslogtreecommitdiff
path: root/examples/example_diffusion.py
blob: dc2d05ed01543b9900bffefcfcce4354807facd9 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
"""
Example demonstrating diffusion processing of transient events.

This script shows how to use the new event_processor module to analyze
diffusion characteristics of detected events, similar to the approach
in heehun_diffusion_processing.py.
"""

import numpy as np
import matplotlib.pyplot as plt

# Import transivent functions
from transivent import (
    process_file,
    get_final_events,
    process_events_for_diffusion,
    extract_event_waveforms,
    calculate_msd_parallel,
    calculate_acf,
    fit_diffusion_linear,
    plot_diffusion_comparison,
    detect_events,
    calculate_initial_background,
    estimate_noise,
    analyze_thresholds,
)

# Configure logging
from transivent import configure_logging
configure_logging("INFO")


def create_synthetic_dataset(
    n_events: int = 50,
    event_duration: float = 100e-6,  # 100 microseconds
    sampling_interval: float = 1e-6,  # 1 microsecond
    total_duration: float = 0.1,  # 100 milliseconds
    diffusion_coeff: float = 1e-12,  # m²/s
    noise_level: float = 0.01,  # Lower noise for better detection
    signal_amplitude: float = 2.0,  # Higher amplitude for better detection
):
    """
    Create synthetic data with diffusion-like events.
    
    Parameters
    ----------
    n_events : int
        Number of events to generate
    event_duration : float
        Duration of each event in seconds
    sampling_interval : float
        Sampling interval in seconds
    total_duration : float
        Total duration of the signal in seconds
    diffusion_coeff : float
        Diffusion coefficient for event dynamics
    noise_level : float
        Noise level in the signal
    signal_amplitude : float
        Amplitude of the events
        
    Returns
    -------
    t : np.ndarray
        Time array
    x : np.ndarray
        Signal array
    """
    n_points = int(total_duration / sampling_interval)
    t = np.linspace(0, total_duration, n_points)
    x = np.random.normal(0, noise_level, n_points)
    
    # Generate random event times
    event_starts = np.random.uniform(0, total_duration - event_duration, n_events)
    
    for start_time in event_starts:
        start_idx = int(start_time / sampling_interval)
        end_idx = int((start_time + event_duration) / sampling_interval)
        
        if end_idx >= n_points:
            end_idx = n_points - 1
        
        # Create a diffusion-like signal for this event
        event_length = end_idx - start_idx
        event_t = np.linspace(0, event_duration, event_length)
        
        # Simulate Brownian motion
        np.random.seed(int(start_time * 1e6))  # Seed for reproducibility
        steps = np.random.normal(0, np.sqrt(2 * diffusion_coeff * sampling_interval), event_length)
        event_signal = signal_amplitude * (1 + np.cumsum(steps))
        
        # Add to main signal
        x[start_idx:end_idx] += event_signal
    
    return t, x


def analyze_single_dataset(name: str, t: np.ndarray, x: np.ndarray, sampling_interval: float):
    """
    Analyze a single dataset for diffusion characteristics.
    
    Parameters
    ----------
    name : str
        Name of the dataset
    t : np.ndarray
        Time array
    x : np.ndarray
        Signal array
    sampling_interval : float
        Sampling interval in seconds
        
    Returns
    -------
    dict
        Results containing events and diffusion analysis
    """
    print(f"\n=== Analyzing {name} ===")
    
    # Step 1: Detect events using transivent
    # First calculate background
    smooth_n = int(10e-6 / sampling_interval)  # 10 microsecond smoothing window
    if smooth_n % 2 == 0:
        smooth_n += 1
    
    bg_initial = calculate_initial_background(t, x, smooth_n)
    global_noise = estimate_noise(x, bg_initial)
    
    # Detect events
    events, _ = detect_events(
        t, x, bg_initial,
        snr_threshold=3.0,
        min_event_len=int(10e-6 / sampling_interval),
        min_event_amp=5.0 * global_noise,
        widen_frac=0.5,
        global_noise=global_noise,
        signal_polarity=1,  # Positive events
    )
    
    print(f"Detected {len(events)} events")
    
    # Step 2: Process events for diffusion analysis
    results = process_events_for_diffusion(
        name=name,
        sampling_interval=sampling_interval,
        data_path="",
        t=t,
        x=x,
        events=events,
        max_lag=500,
        n_jobs=-1,
    )
    
    print(f"Processed {results['event_count']} events for diffusion")
    if results['event_count'] > 0:
        print(f"Mean diffusion coefficient: {results['statistics']['mean_diffusion']:.3e} ± "
              f"{results['statistics']['std_diffusion']:.3e} m²/s")
    
    return results


def main():
    """Main function demonstrating diffusion analysis."""
    print("Transivent Diffusion Processing Example")
    print("=" * 40)
    
    # Create synthetic datasets with different diffusion characteristics
    datasets = {
        "Ferritin (slow diffusion)": {
            "diffusion_coeff": 5e-13,  # Slower diffusion
            "signal_amplitude": 1.0,
            "n_events": 40,
        },
        "Catalase (medium diffusion)": {
            "diffusion_coeff": 1e-12,  # Medium diffusion
            "signal_amplitude": 0.8,
            "n_events": 35,
        },
        "BSA (fast diffusion)": {
            "diffusion_coeff": 2e-12,  # Faster diffusion
            "signal_amplitude": 0.6,
            "n_events": 45,
        },
    }
    
    sampling_interval = 1e-6  # 1 microsecond
    
    all_results = {}
    
    # Analyze each dataset
    for name, params in datasets.items():
        # Generate synthetic data
        t, x = create_synthetic_dataset(
            n_events=params["n_events"],
            diffusion_coeff=params["diffusion_coeff"],
            signal_amplitude=params["signal_amplitude"],
            sampling_interval=sampling_interval,
        )
        
        # Analyze for diffusion
        results = analyze_single_dataset(name, t, x, sampling_interval)
        all_results[name] = results
    
    # Create comparison plot
    if any(len(results["diffusion_coeffs"]) > 0 for results in all_results.values()):
        print("\n=== Creating comparison plot ===")
        fig = plot_diffusion_comparison(
            all_results,
            show_ellipses=True,
            colors=["#1f77b4", "gold", "green"],
            markers=["o", "^", "s"],
        )
        
        # Save the plot
        import os
        os.makedirs("analysis_output", exist_ok=True)
        fig.savefig("analysis_output/diffusion_comparison.png", dpi=150, bbox_inches="tight")
        print("Saved plot to: analysis_output/diffusion_comparison.png")
        
        # Show plot
        plt.show()
    else:
        print("\nNo events detected in any dataset. Check detection parameters.")


if __name__ == "__main__":
    main()