scirs2-metrics 0.3.2

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Synaptic systems and plasticity mechanisms
//!
//! This module contains synaptic connections, plasticity rules, and
//! learning mechanisms for neuromorphic computing.

#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]

use super::core::{NetworkTopology, SynapseType};
use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

/// Synaptic connections between neurons
#[derive(Debug)]
pub struct SynapticConnections<F: Float> {
    /// Connection matrix
    pub connections: HashMap<(usize, usize), Synapse<F>>,
    /// Synaptic delays
    pub delays: HashMap<(usize, usize), Duration>,
    /// Connection topology
    pub topology: super::core::ConnectionTopology,
}

/// Individual synapse
#[derive(Debug)]
pub struct Synapse<F: Float> {
    /// Synaptic weight
    pub weight: F,
    /// Presynaptic neuron ID
    pub pre_neuron: usize,
    /// Postsynaptic neuron ID
    pub post_neuron: usize,
    /// Synaptic type
    pub synapse_type: SynapseType,
    /// Plasticity state
    pub plasticity_state: PlasticityState<F>,
    /// Short-term dynamics
    pub short_term_dynamics: ShortTermDynamics<F>,
}

/// Plasticity state of synapse
#[derive(Debug, Clone)]
pub struct PlasticityState<F: Float> {
    /// Long-term potentiation level
    pub ltp_level: F,
    /// Long-term depression level
    pub ltd_level: F,
    /// Meta-plasticity threshold
    pub meta_threshold: F,
    /// Eligibility trace
    pub eligibility_trace: F,
    /// Last spike timing difference
    pub last_spike_diff: Duration,
}

/// Short-term synaptic dynamics
#[derive(Debug)]
pub struct ShortTermDynamics<F: Float> {
    /// Facilitation variable
    pub facilitation: F,
    /// Depression variable
    pub depression: F,
    /// Utilization of synaptic efficacy
    pub utilization: F,
    /// Recovery time constants
    pub tau_facilitation: Duration,
    pub tau_depression: Duration,
}

/// Synaptic plasticity manager
#[derive(Debug)]
pub struct SynapticPlasticityManager<F: Float> {
    /// STDP windows
    pub stdp_windows: HashMap<String, STDPWindow<F>>,
    /// Homeostatic controllers
    pub homeostatic_controllers: Vec<HomeostaticController<F>>,
    /// Metaplasticity state
    pub metaplasticity_state: MetaplasticityState<F>,
    /// Learning rate scheduler
    pub learning_scheduler: LearningRateScheduler<F>,
}

/// STDP (Spike-Timing-Dependent Plasticity) window
#[derive(Debug, Clone)]
pub struct STDPWindow<F: Float> {
    /// Time window for LTP
    pub ltp_window: Duration,
    /// Time window for LTD
    pub ltd_window: Duration,
    /// LTP amplitude function
    pub ltp_amplitude: Vec<(Duration, F)>,
    /// LTD amplitude function
    pub ltd_amplitude: Vec<(Duration, F)>,
    /// STDP curve parameters
    pub curve_parameters: STDPCurveParameters<F>,
}

/// STDP curve parameters
#[derive(Debug, Clone)]
pub struct STDPCurveParameters<F: Float> {
    /// LTP amplitude
    pub a_ltp: F,
    /// LTD amplitude
    pub a_ltd: F,
    /// LTP time constant
    pub tau_ltp: Duration,
    /// LTD time constant
    pub tau_ltd: Duration,
    /// Asymmetry parameter
    pub asymmetry: F,
}

/// Homeostatic controller for maintaining network stability
#[derive(Debug)]
pub struct HomeostaticController<F: Float> {
    /// Target firing rate
    pub target_rate: F,
    /// Current firing rate
    pub current_rate: F,
    /// Scaling factor
    pub scaling_factor: F,
    /// Time constant for adaptation
    pub time_constant: Duration,
    /// Controlled neurons
    pub controlled_neurons: Vec<usize>,
    /// Control mode
    pub control_mode: HomeostaticMode,
}

/// Homeostatic control modes
#[derive(Debug, Clone)]
pub enum HomeostaticMode {
    /// Synaptic scaling
    SynapticScaling,
    /// Intrinsic excitability
    IntrinsicExcitability,
    /// Threshold adaptation
    ThresholdAdaptation,
    /// Combined approach
    Combined,
}

/// Metaplasticity state
#[derive(Debug)]
pub struct MetaplasticityState<F: Float> {
    /// Activity history
    pub activity_history: VecDeque<F>,
    /// Threshold modulation
    pub threshold_modulation: F,
    /// Learning rate modulation
    pub learning_rate_modulation: F,
    /// State variables
    pub state_variables: HashMap<String, F>,
}

/// Learning rate scheduler
#[derive(Debug)]
pub struct LearningRateScheduler<F: Float> {
    /// Base learning rate
    pub base_rate: F,
    /// Current learning rate
    pub current_rate: F,
    /// Scheduling policy
    pub policy: SchedulingPolicy<F>,
    /// Performance metrics
    pub performance_metrics: VecDeque<F>,
}

/// Learning rate scheduling policies
#[derive(Debug, Clone)]
pub enum SchedulingPolicy<F: Float> {
    /// Constant rate
    Constant,
    /// Exponential decay
    ExponentialDecay { decay_rate: F },
    /// Step decay
    StepDecay { step_size: usize, gamma: F },
    /// Performance-based
    PerformanceBased { patience: usize, factor: F },
    /// Adaptive (based on gradient)
    Adaptive { momentum: F },
}

impl<F: Float> SynapticConnections<F> {
    /// Create new synaptic connections from topology
    pub fn new(topology: &NetworkTopology) -> Self {
        let connections = HashMap::new();
        let delays = HashMap::new();
        let topology_data = super::core::ConnectionTopology {
            adjacency_matrix: scirs2_core::ndarray::Array2::from_elem((0, 0), false),
            weight_matrix: scirs2_core::ndarray::Array2::zeros((0, 0)),
            small_world: super::core::SmallWorldProperties {
                clustering_coefficient: 0.0,
                average_path_length: 0.0,
                small_world_index: 0.0,
            },
        };

        Self {
            connections,
            delays,
            topology: topology_data,
        }
    }

    /// Add a new synaptic connection
    pub fn add_connection(
        &mut self,
        pre_neuron: usize,
        post_neuron: usize,
        weight: F,
        synapse_type: SynapseType,
        delay: Duration,
    ) -> crate::error::Result<()> {
        let synapse = Synapse::new(pre_neuron, post_neuron, weight, synapse_type);
        self.connections.insert((pre_neuron, post_neuron), synapse);
        self.delays.insert((pre_neuron, post_neuron), delay);
        Ok(())
    }

    /// Get synaptic weight between neurons
    pub fn get_weight(&self, pre_neuron: usize, post_neuron: usize) -> Option<F> {
        self.connections
            .get(&(pre_neuron, post_neuron))
            .map(|s| s.weight)
    }

    /// Update synaptic weight
    pub fn update_weight(
        &mut self,
        pre_neuron: usize,
        post_neuron: usize,
        new_weight: F,
    ) -> crate::error::Result<()> {
        if let Some(synapse) = self.connections.get_mut(&(pre_neuron, post_neuron)) {
            synapse.weight = new_weight;
            Ok(())
        } else {
            Err(crate::error::MetricsError::InvalidInput(
                "Synapse not found".to_string(),
            ))
        }
    }

    /// Apply STDP to all connections
    pub fn apply_stdp(
        &mut self,
        spike_times: &HashMap<usize, Instant>,
        stdp_window: &STDPWindow<F>,
    ) -> crate::error::Result<()> {
        for ((pre_id, post_id), synapse) in self.connections.iter_mut() {
            if let (Some(&pre_time), Some(&post_time)) =
                (spike_times.get(pre_id), spike_times.get(post_id))
            {
                let time_diff = if post_time > pre_time {
                    post_time.duration_since(pre_time)
                } else {
                    pre_time.duration_since(post_time)
                };

                let weight_change =
                    stdp_window.calculate_weight_change(time_diff, post_time > pre_time);
                synapse.weight = synapse.weight + weight_change;
            }
        }
        Ok(())
    }
}

impl<F: Float> Synapse<F> {
    /// Create a new synapse
    pub fn new(
        pre_neuron: usize,
        post_neuron: usize,
        weight: F,
        synapse_type: SynapseType,
    ) -> Self {
        Self {
            weight,
            pre_neuron,
            post_neuron,
            synapse_type,
            plasticity_state: PlasticityState::new(),
            short_term_dynamics: ShortTermDynamics::new(),
        }
    }

    /// Update synapse state
    pub fn update(&mut self, dt: Duration) -> crate::error::Result<()> {
        self.short_term_dynamics.update(dt);
        self.plasticity_state.update(dt);
        Ok(())
    }

    /// Get effective synaptic strength
    pub fn get_effective_strength(&self) -> F {
        self.weight * self.short_term_dynamics.get_current_strength()
    }
}

impl<F: Float> PlasticityState<F> {
    /// Create new plasticity state
    pub fn new() -> Self {
        Self {
            ltp_level: F::zero(),
            ltd_level: F::zero(),
            meta_threshold: F::one(),
            eligibility_trace: F::zero(),
            last_spike_diff: Duration::from_secs(0),
        }
    }

    /// Update plasticity state
    pub fn update(&mut self, dt: Duration) {
        // Decay eligibility trace
        let decay_factor = F::from((-dt.as_secs_f64() / 0.1).exp()).expect("Operation failed");
        self.eligibility_trace = self.eligibility_trace * decay_factor;

        // Update LTP/LTD levels
        self.ltp_level = self.ltp_level * decay_factor;
        self.ltd_level = self.ltd_level * decay_factor;
    }

    /// Apply spike-timing dependent plasticity
    pub fn apply_stdp(&mut self, spike_time_diff: Duration, is_ltp: bool) {
        if is_ltp {
            self.ltp_level =
                self.ltp_level + F::from(0.1).expect("Failed to convert constant to float");
        } else {
            self.ltd_level =
                self.ltd_level + F::from(0.05).expect("Failed to convert constant to float");
        }
        self.last_spike_diff = spike_time_diff;
    }
}

impl<F: Float> ShortTermDynamics<F> {
    /// Create new short-term dynamics
    pub fn new() -> Self {
        Self {
            facilitation: F::one(),
            depression: F::one(),
            utilization: F::from(0.5).expect("Failed to convert constant to float"),
            tau_facilitation: Duration::from_millis(100),
            tau_depression: Duration::from_millis(500),
        }
    }

    /// Update short-term dynamics
    pub fn update(&mut self, dt: Duration) {
        // Facilitation decay
        let f_decay = F::from((-dt.as_secs_f64() / self.tau_facilitation.as_secs_f64()).exp())
            .expect("Operation failed");
        self.facilitation = self.facilitation * f_decay + (F::one() - f_decay);

        // Depression recovery
        let d_decay = F::from((-dt.as_secs_f64() / self.tau_depression.as_secs_f64()).exp())
            .expect("Operation failed");
        self.depression = self.depression * d_decay + (F::one() - d_decay);
    }

    /// Get current synaptic strength
    pub fn get_current_strength(&self) -> F {
        self.facilitation * self.depression * self.utilization
    }

    /// Apply presynaptic spike
    pub fn apply_presynaptic_spike(&mut self) {
        self.facilitation = self.facilitation + self.utilization * (F::one() - self.facilitation);
        self.depression = self.depression * (F::one() - self.utilization);
    }
}

impl<F: Float> STDPWindow<F> {
    /// Create new STDP window
    pub fn new(
        ltp_window: Duration,
        ltd_window: Duration,
        curve_params: STDPCurveParameters<F>,
    ) -> Self {
        Self {
            ltp_window,
            ltd_window,
            ltp_amplitude: Vec::new(),
            ltd_amplitude: Vec::new(),
            curve_parameters: curve_params,
        }
    }

    /// Calculate weight change based on spike timing
    pub fn calculate_weight_change(&self, time_diff: Duration, is_ltp: bool) -> F {
        if is_ltp && time_diff <= self.ltp_window {
            // LTP case
            let tau = self.curve_parameters.tau_ltp.as_secs_f64();
            let amplitude = self.curve_parameters.a_ltp;
            amplitude * F::from((-time_diff.as_secs_f64() / tau).exp()).expect("Operation failed")
        } else if !is_ltp && time_diff <= self.ltd_window {
            // LTD case
            let tau = self.curve_parameters.tau_ltd.as_secs_f64();
            let amplitude = self.curve_parameters.a_ltd;
            -amplitude * F::from((-time_diff.as_secs_f64() / tau).exp()).expect("Operation failed")
        } else {
            F::zero()
        }
    }
}

impl<F: Float> STDPCurveParameters<F> {
    /// Create default STDP parameters
    pub fn default() -> Self {
        Self {
            a_ltp: F::from(0.1).expect("Failed to convert constant to float"),
            a_ltd: F::from(0.05).expect("Failed to convert constant to float"),
            tau_ltp: Duration::from_millis(20),
            tau_ltd: Duration::from_millis(20),
            asymmetry: F::one(),
        }
    }
}

impl<F: Float + std::iter::Sum> SynapticPlasticityManager<F> {
    /// Create new plasticity manager
    pub fn new() -> Self {
        let mut stdp_windows = HashMap::new();
        stdp_windows.insert(
            "default".to_string(),
            STDPWindow::new(
                Duration::from_millis(40),
                Duration::from_millis(40),
                STDPCurveParameters::default(),
            ),
        );

        Self {
            stdp_windows,
            homeostatic_controllers: Vec::new(),
            metaplasticity_state: MetaplasticityState::new(),
            learning_scheduler: LearningRateScheduler::new(),
        }
    }

    /// Add homeostatic controller
    pub fn add_homeostatic_controller(&mut self, controller: HomeostaticController<F>) {
        self.homeostatic_controllers.push(controller);
    }

    /// Update all plasticity mechanisms
    pub fn update(&mut self, dt: Duration, network_activity: &[F]) -> crate::error::Result<()> {
        // Update homeostatic controllers
        for controller in &mut self.homeostatic_controllers {
            controller.update(dt, network_activity)?;
        }

        // Update metaplasticity
        self.metaplasticity_state.update(network_activity);

        // Update learning rate scheduler
        self.learning_scheduler.update(dt)?;

        Ok(())
    }
}

impl<F: Float + std::iter::Sum> HomeostaticController<F> {
    /// Create new homeostatic controller
    pub fn new(target_rate: F, neurons: Vec<usize>, mode: HomeostaticMode) -> Self {
        Self {
            target_rate,
            current_rate: F::zero(),
            scaling_factor: F::one(),
            time_constant: Duration::from_secs(10),
            controlled_neurons: neurons,
            control_mode: mode,
        }
    }

    /// Update homeostatic control
    pub fn update(&mut self, dt: Duration, activity: &[F]) -> crate::error::Result<()> {
        // Calculate current firing rate
        self.current_rate = activity.iter().cloned().sum::<F>()
            / F::from(activity.len()).expect("Operation failed");

        // Update scaling factor based on difference from target
        let error = self.target_rate - self.current_rate;
        let adaptation_rate =
            F::from(dt.as_secs_f64() / self.time_constant.as_secs_f64()).expect("Operation failed");

        match self.control_mode {
            HomeostaticMode::SynapticScaling => {
                self.scaling_factor = self.scaling_factor + adaptation_rate * error;
            }
            HomeostaticMode::IntrinsicExcitability => {
                // Adjust intrinsic excitability
                self.scaling_factor = self.scaling_factor
                    + adaptation_rate
                        * error
                        * F::from(0.1).expect("Failed to convert constant to float");
            }
            _ => {
                // Default scaling
                self.scaling_factor = self.scaling_factor + adaptation_rate * error;
            }
        }

        Ok(())
    }

    /// Get current scaling factor
    pub fn get_scaling_factor(&self) -> F {
        self.scaling_factor
    }
}

impl<F: Float + std::iter::Sum> MetaplasticityState<F> {
    /// Create new metaplasticity state
    pub fn new() -> Self {
        Self {
            activity_history: VecDeque::new(),
            threshold_modulation: F::one(),
            learning_rate_modulation: F::one(),
            state_variables: HashMap::new(),
        }
    }

    /// Update metaplasticity state
    pub fn update(&mut self, network_activity: &[F]) {
        let avg_activity = network_activity.iter().cloned().sum::<F>()
            / F::from(network_activity.len()).expect("Operation failed");
        self.activity_history.push_back(avg_activity);

        // Keep bounded
        if self.activity_history.len() > 1000 {
            self.activity_history.pop_front();
        }

        // Update modulation factors based on activity history
        if self.activity_history.len() > 10 {
            let recent_avg = self
                .activity_history
                .iter()
                .rev()
                .take(10)
                .cloned()
                .sum::<F>()
                / F::from(10).expect("Failed to convert constant to float");
            self.threshold_modulation = F::one()
                + (recent_avg - F::from(0.5).expect("Failed to convert constant to float"))
                    * F::from(0.1).expect("Failed to convert constant to float");
            self.learning_rate_modulation = F::one()
                + (recent_avg - F::from(0.5).expect("Failed to convert constant to float"))
                    * F::from(0.05).expect("Failed to convert constant to float");
        }
    }
}

impl<F: Float> LearningRateScheduler<F> {
    /// Create new learning rate scheduler
    pub fn new() -> Self {
        Self {
            base_rate: F::from(0.01).expect("Failed to convert constant to float"),
            current_rate: F::from(0.01).expect("Failed to convert constant to float"),
            policy: SchedulingPolicy::Constant,
            performance_metrics: VecDeque::new(),
        }
    }

    /// Update learning rate
    pub fn update(&mut self, dt: Duration) -> crate::error::Result<()> {
        match &self.policy {
            SchedulingPolicy::Constant => {
                // No change
            }
            SchedulingPolicy::ExponentialDecay { decay_rate } => {
                let decay_factor = F::from(
                    (-decay_rate.to_f64().expect("Failed to convert to float") * dt.as_secs_f64())
                        .exp(),
                )
                .expect("Operation failed");
                self.current_rate = self.current_rate * decay_factor;
            }
            SchedulingPolicy::PerformanceBased { patience, factor } => {
                if self.performance_metrics.len() > *patience {
                    let recent = self
                        .performance_metrics
                        .iter()
                        .rev()
                        .take(*patience)
                        .cloned()
                        .collect::<Vec<_>>();
                    let is_plateau = recent.windows(2).all(|w| {
                        (w[1] - w[0]).abs()
                            < F::from(0.001).expect("Failed to convert constant to float")
                    });
                    if is_plateau {
                        self.current_rate = self.current_rate * *factor;
                    }
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Add performance metric
    pub fn add_performance_metric(&mut self, metric: F) {
        self.performance_metrics.push_back(metric);
        if self.performance_metrics.len() > 100 {
            self.performance_metrics.pop_front();
        }
    }

    /// Get current learning rate
    pub fn get_current_rate(&self) -> F {
        self.current_rate
    }
}