scirs2_spatial/neuromorphic/algorithms/
spiking_clustering.rs

1//! Spiking Neural Network Clustering
2//!
3//! This module implements clustering algorithms based on spiking neural networks (SNNs).
4//! These algorithms use spike-timing dependent plasticity (STDP) and competitive learning
5//! to discover patterns in spatial data through biologically-inspired neural dynamics.
6
7use crate::error::{SpatialError, SpatialResult};
8use scirs2_core::ndarray::{Array1, ArrayView2};
9use scirs2_core::random::Rng;
10use std::collections::HashMap;
11
12// Import core neuromorphic components
13use super::super::core::{SpikeEvent, SpikingNeuron, Synapse};
14
15/// Spiking neural network clusterer
16///
17/// This clusterer uses a network of spiking neurons with STDP learning to perform
18/// unsupervised clustering of spatial data. Input points are encoded as spike trains
19/// and presented to the network, which learns to respond selectively to different
20/// input patterns through competitive dynamics.
21///
22/// # Features
23/// - Rate coding for spatial data encoding
24/// - STDP learning for adaptive weights
25/// - Lateral inhibition for competitive dynamics
26/// - Configurable network architecture
27/// - Spike timing analysis
28///
29/// # Example
30/// ```rust
31/// use scirs2_core::ndarray::Array2;
32/// use scirs2_spatial::neuromorphic::algorithms::SpikingNeuralClusterer;
33///
34/// let points = Array2::from_shape_vec((4, 2), vec![
35///     0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0
36/// ]).unwrap();
37///
38/// let mut clusterer = SpikingNeuralClusterer::new(2)
39///     .with_spike_threshold(0.8)
40///     .with_stdp_learning(true)
41///     .with_lateral_inhibition(true);
42///
43/// let (assignments, spike_events) = clusterer.fit(&points.view()).unwrap();
44/// println!("Cluster assignments: {:?}", assignments);
45/// ```
46#[derive(Debug, Clone)]
47pub struct SpikingNeuralClusterer {
48    /// Network of spiking neurons
49    neurons: Vec<SpikingNeuron>,
50    /// Synaptic connections
51    synapses: Vec<Synapse>,
52    /// Number of clusters (output neurons)
53    num_clusters: usize,
54    /// Spike threshold
55    spike_threshold: f64,
56    /// Enable STDP learning
57    stdp_learning: bool,
58    /// Enable lateral inhibition
59    lateral_inhibition: bool,
60    /// Simulation time step
61    dt: f64,
62    /// Current simulation time
63    current_time: f64,
64    /// Spike history
65    spike_history: Vec<SpikeEvent>,
66    /// Number of training epochs
67    max_epochs: usize,
68    /// Simulation duration per data point
69    simulation_duration: f64,
70}
71
72impl SpikingNeuralClusterer {
73    /// Create new spiking neural clusterer
74    ///
75    /// # Arguments
76    /// * `num_clusters` - Number of clusters to discover
77    ///
78    /// # Returns
79    /// A new `SpikingNeuralClusterer` with default parameters
80    pub fn new(num_clusters: usize) -> Self {
81        Self {
82            neurons: Vec::new(),
83            synapses: Vec::new(),
84            num_clusters,
85            spike_threshold: 1.0,
86            stdp_learning: true,
87            lateral_inhibition: true,
88            dt: 0.1,
89            current_time: 0.0,
90            spike_history: Vec::new(),
91            max_epochs: 100,
92            simulation_duration: 10.0,
93        }
94    }
95
96    /// Configure spike threshold
97    ///
98    /// # Arguments
99    /// * `threshold` - Spike threshold for neurons
100    pub fn with_spike_threshold(mut self, threshold: f64) -> Self {
101        self.spike_threshold = threshold;
102        self
103    }
104
105    /// Enable/disable STDP learning
106    ///
107    /// # Arguments
108    /// * `enabled` - Whether to enable STDP learning
109    pub fn with_stdp_learning(mut self, enabled: bool) -> Self {
110        self.stdp_learning = enabled;
111        self
112    }
113
114    /// Enable/disable lateral inhibition
115    ///
116    /// # Arguments
117    /// * `enabled` - Whether to enable lateral inhibition
118    pub fn with_lateral_inhibition(mut self, enabled: bool) -> Self {
119        self.lateral_inhibition = enabled;
120        self
121    }
122
123    /// Configure training parameters
124    ///
125    /// # Arguments
126    /// * `max_epochs` - Maximum number of training epochs
127    /// * `simulation_duration` - Duration to simulate per data point
128    pub fn with_training_params(mut self, max_epochs: usize, simulation_duration: f64) -> Self {
129        self.max_epochs = max_epochs;
130        self.simulation_duration = simulation_duration;
131        self
132    }
133
134    /// Configure simulation time step
135    ///
136    /// # Arguments
137    /// * `dt` - Time step for simulation
138    pub fn with_time_step(mut self, dt: f64) -> Self {
139        self.dt = dt;
140        self
141    }
142
143    /// Fit clustering to spatial data
144    ///
145    /// Trains the spiking neural network on the provided spatial data using
146    /// STDP learning and competitive dynamics to discover cluster structure.
147    ///
148    /// # Arguments
149    /// * `points` - Input points to cluster (n_points × n_dims)
150    ///
151    /// # Returns
152    /// Tuple of (cluster assignments, spike events) where assignments
153    /// maps each point to its cluster and spike_events contains the
154    /// complete spike timing history.
155    pub fn fit(
156        &mut self,
157        points: &ArrayView2<'_, f64>,
158    ) -> SpatialResult<(Array1<usize>, Vec<SpikeEvent>)> {
159        let (n_points, n_dims) = points.dim();
160
161        if n_points == 0 || n_dims == 0 {
162            return Err(SpatialError::InvalidInput(
163                "Input data cannot be empty".to_string(),
164            ));
165        }
166
167        // Initialize neural network
168        self.initialize_network(n_dims)?;
169
170        // Present data points as spike trains
171        let mut assignments = Array1::zeros(n_points);
172
173        for epoch in 0..self.max_epochs {
174            self.current_time = epoch as f64 * 100.0;
175
176            for (point_idx, point) in points.outer_iter().enumerate() {
177                // Encode spatial point as spike train
178                let spike_train = self.encode_point_as_spikes(&point.to_owned())?;
179
180                // Process spike train through network
181                let winning_neuron = self.process_spike_train(&spike_train)?;
182                assignments[point_idx] = winning_neuron;
183
184                // Apply learning if enabled
185                if self.stdp_learning {
186                    self.apply_stdp_learning(&spike_train)?;
187                }
188            }
189
190            // Apply lateral inhibition
191            if self.lateral_inhibition {
192                self.apply_lateral_inhibition()?;
193            }
194        }
195
196        Ok((assignments, self.spike_history.clone()))
197    }
198
199    /// Initialize spiking neural network
200    ///
201    /// Creates the network topology with input neurons, output neurons,
202    /// and synaptic connections between them.
203    fn initialize_network(&mut self, input_dims: usize) -> SpatialResult<()> {
204        self.neurons.clear();
205        self.synapses.clear();
206        self.spike_history.clear();
207
208        // Create input neurons (one per dimension)
209        for i in 0..input_dims {
210            let position = vec![i as f64];
211            let mut neuron = SpikingNeuron::new(position);
212            neuron.set_threshold(self.spike_threshold);
213            self.neurons.push(neuron);
214        }
215
216        // Create output neurons (cluster centers)
217        let mut rng = scirs2_core::random::rng();
218        for _i in 0..self.num_clusters {
219            let position = (0..input_dims).map(|_| rng.gen_range(0.0..1.0)).collect();
220            let mut neuron = SpikingNeuron::new(position);
221            neuron.set_threshold(self.spike_threshold);
222            self.neurons.push(neuron);
223        }
224
225        // Create synaptic connections (input to output)
226        for i in 0..input_dims {
227            for j in 0..self.num_clusters {
228                let output_idx = input_dims + j;
229                let weight = rng.gen_range(0.0..0.5);
230                let synapse = Synapse::new(i, output_idx, weight);
231                self.synapses.push(synapse);
232            }
233        }
234
235        // Create lateral inhibitory connections between output neurons
236        if self.lateral_inhibition {
237            for i in 0..self.num_clusters {
238                for j in 0..self.num_clusters {
239                    if i != j {
240                        let neuron_i = input_dims + i;
241                        let neuron_j = input_dims + j;
242                        let synapse = Synapse::new(neuron_i, neuron_j, -0.5);
243                        self.synapses.push(synapse);
244                    }
245                }
246            }
247        }
248
249        Ok(())
250    }
251
252    /// Encode spatial point as spike train
253    ///
254    /// Converts a spatial data point into a spike train using rate coding,
255    /// where the firing rate of each input neuron is proportional to the
256    /// corresponding coordinate value.
257    fn encode_point_as_spikes(&self, point: &Array1<f64>) -> SpatialResult<Vec<SpikeEvent>> {
258        let mut spike_train = Vec::new();
259
260        // Rate coding: spike frequency proportional to coordinate value
261        for (dim, &coord) in point.iter().enumerate() {
262            // Normalize coordinate to [0, 1] and scale to spike rate
263            let normalized_coord = (coord + 10.0) / 20.0; // Assume data in [-10, 10]
264            let spike_rate = normalized_coord.clamp(0.0, 1.0) * 50.0; // Max 50 Hz
265
266            // Generate Poisson spike train
267            let num_spikes = (spike_rate * 1.0) as usize; // 1 second duration
268            for spike_idx in 0..num_spikes {
269                let timestamp =
270                    self.current_time + (spike_idx as f64) * (1.0 / spike_rate.max(1.0));
271                let spike = SpikeEvent::new(dim, timestamp, 1.0, point.to_vec());
272                spike_train.push(spike);
273            }
274        }
275
276        // Sort spikes by timestamp
277        spike_train.sort_by(|a, b| a.timestamp().partial_cmp(&b.timestamp()).unwrap());
278
279        Ok(spike_train)
280    }
281
282    /// Process spike train through network
283    ///
284    /// Simulates the network dynamics when presented with a spike train,
285    /// determining which output neuron responds most strongly.
286    fn process_spike_train(&mut self, spike_train: &[SpikeEvent]) -> SpatialResult<usize> {
287        let input_dims = self.neurons.len() - self.num_clusters;
288        let mut neuron_spike_counts = vec![0; self.num_clusters];
289
290        // Simulate network for duration of spike train
291        let mut t = self.current_time;
292        let mut spike_idx = 0;
293
294        while t < self.current_time + self.simulation_duration {
295            // Apply input spikes
296            let mut input_currents = vec![0.0; self.neurons.len()];
297
298            while spike_idx < spike_train.len() && spike_train[spike_idx].timestamp() <= t {
299                let spike = &spike_train[spike_idx];
300                if spike.neuron_id() < input_dims {
301                    input_currents[spike.neuron_id()] += spike.amplitude();
302                }
303                spike_idx += 1;
304            }
305
306            // Calculate synaptic currents
307            for synapse in &self.synapses {
308                if synapse.pre_neuron() < self.neurons.len()
309                    && synapse.post_neuron() < self.neurons.len()
310                {
311                    let pre_current = input_currents[synapse.pre_neuron()];
312                    let synaptic_current = synapse.synaptic_current(pre_current);
313                    input_currents[synapse.post_neuron()] += synaptic_current;
314                }
315            }
316
317            // Update neurons and check for spikes
318            for (neuron_idx, neuron) in self.neurons.iter_mut().enumerate() {
319                let spiked = neuron.update(self.dt, input_currents[neuron_idx]);
320
321                if spiked && neuron_idx >= input_dims {
322                    let cluster_idx = neuron_idx - input_dims;
323                    neuron_spike_counts[cluster_idx] += 1;
324
325                    // Record spike event
326                    let spike_event =
327                        SpikeEvent::new(neuron_idx, t, 1.0, neuron.position().to_vec());
328                    self.spike_history.push(spike_event);
329                }
330            }
331
332            t += self.dt;
333        }
334
335        // Find winning neuron (cluster with most spikes)
336        let winning_cluster = neuron_spike_counts
337            .iter()
338            .enumerate()
339            .max_by(|(_, a), (_, b)| a.cmp(b))
340            .map(|(idx, _)| idx)
341            .unwrap_or(0);
342
343        Ok(winning_cluster)
344    }
345
346    /// Apply STDP learning to synapses
347    ///
348    /// Updates synaptic weights based on the relative timing of pre- and
349    /// post-synaptic spikes using the STDP learning rule.
350    fn apply_stdp_learning(&mut self, spike_train: &[SpikeEvent]) -> SpatialResult<()> {
351        // Create spike timing map
352        let mut spike_times: HashMap<usize, Vec<f64>> = HashMap::new();
353        for spike in spike_train {
354            spike_times
355                .entry(spike.neuron_id())
356                .or_default()
357                .push(spike.timestamp());
358        }
359
360        // Add output neuron spikes from history
361        for spike in &self.spike_history {
362            spike_times
363                .entry(spike.neuron_id())
364                .or_default()
365                .push(spike.timestamp());
366        }
367
368        // Update synaptic weights using STDP
369        let empty_spikes = Vec::new();
370        for synapse in &mut self.synapses {
371            let pre_spikes = spike_times
372                .get(&synapse.pre_neuron())
373                .unwrap_or(&empty_spikes);
374            let post_spikes = spike_times
375                .get(&synapse.post_neuron())
376                .unwrap_or(&empty_spikes);
377
378            // Check for coincident spikes
379            for &pre_time in pre_spikes {
380                for &post_time in post_spikes {
381                    let dt = post_time - pre_time;
382                    if dt.abs() < 50.0 {
383                        // Within STDP window
384                        let current_weight = synapse.weight();
385                        if dt > 0.0 {
386                            // Potentiation
387                            let delta_w = synapse.stdp_rate() * (-dt / synapse.stdp_tau()).exp();
388                            synapse.set_weight(current_weight + delta_w);
389                        } else {
390                            // Depression
391                            let delta_w = synapse.stdp_rate() * (dt / synapse.stdp_tau()).exp();
392                            synapse.set_weight(current_weight - delta_w);
393                        }
394                    }
395                }
396            }
397        }
398
399        Ok(())
400    }
401
402    /// Apply lateral inhibition between output neurons
403    ///
404    /// Strengthens inhibitory connections between neurons based on their
405    /// relative activity levels to promote competition.
406    fn apply_lateral_inhibition(&mut self) -> SpatialResult<()> {
407        let input_dims = self.neurons.len() - self.num_clusters;
408
409        // Strengthen inhibitory connections between active neurons
410        for i in 0..self.num_clusters {
411            for j in 0..self.num_clusters {
412                if i != j {
413                    let neuron_i_idx = input_dims + i;
414                    let neuron_j_idx = input_dims + j;
415
416                    // Find inhibitory synapse
417                    for synapse in &mut self.synapses {
418                        if synapse.pre_neuron() == neuron_i_idx
419                            && synapse.post_neuron() == neuron_j_idx
420                        {
421                            // Strengthen inhibition based on activity
422                            let activity_i = self.neurons[neuron_i_idx].membrane_potential();
423                            let activity_j = self.neurons[neuron_j_idx].membrane_potential();
424
425                            if activity_i > activity_j {
426                                let current_weight = synapse.weight();
427                                synapse.set_weight(current_weight - 0.01); // Strengthen inhibition
428                            }
429                        }
430                    }
431                }
432            }
433        }
434
435        Ok(())
436    }
437
438    /// Get number of clusters
439    pub fn num_clusters(&self) -> usize {
440        self.num_clusters
441    }
442
443    /// Get spike threshold
444    pub fn spike_threshold(&self) -> f64 {
445        self.spike_threshold
446    }
447
448    /// Check if STDP learning is enabled
449    pub fn is_stdp_enabled(&self) -> bool {
450        self.stdp_learning
451    }
452
453    /// Check if lateral inhibition is enabled
454    pub fn is_lateral_inhibition_enabled(&self) -> bool {
455        self.lateral_inhibition
456    }
457
458    /// Get current spike history
459    pub fn spike_history(&self) -> &[SpikeEvent] {
460        &self.spike_history
461    }
462
463    /// Get network statistics
464    pub fn network_stats(&self) -> NetworkStats {
465        NetworkStats {
466            num_neurons: self.neurons.len(),
467            num_synapses: self.synapses.len(),
468            num_spikes: self.spike_history.len(),
469            average_weight: if self.synapses.is_empty() {
470                0.0
471            } else {
472                self.synapses.iter().map(|s| s.weight()).sum::<f64>() / self.synapses.len() as f64
473            },
474        }
475    }
476
477    /// Reset the network to initial state
478    pub fn reset(&mut self) {
479        for neuron in &mut self.neurons {
480            neuron.reset();
481        }
482        for synapse in &mut self.synapses {
483            synapse.reset_spike_history();
484        }
485        self.spike_history.clear();
486        self.current_time = 0.0;
487    }
488}
489
490/// Network statistics for analysis
491#[derive(Debug, Clone)]
492pub struct NetworkStats {
493    /// Total number of neurons
494    pub num_neurons: usize,
495    /// Total number of synapses
496    pub num_synapses: usize,
497    /// Total number of spikes recorded
498    pub num_spikes: usize,
499    /// Average synaptic weight
500    pub average_weight: f64,
501}
502
503#[cfg(test)]
504mod tests {
505    use super::*;
506    use scirs2_core::ndarray::Array2;
507
508    #[test]
509    fn test_spiking_clusterer_creation() {
510        let clusterer = SpikingNeuralClusterer::new(3);
511        assert_eq!(clusterer.num_clusters(), 3);
512        assert_eq!(clusterer.spike_threshold(), 1.0);
513        assert!(clusterer.is_stdp_enabled());
514        assert!(clusterer.is_lateral_inhibition_enabled());
515    }
516
517    #[test]
518    fn test_clusterer_configuration() {
519        let clusterer = SpikingNeuralClusterer::new(2)
520            .with_spike_threshold(0.8)
521            .with_stdp_learning(false)
522            .with_lateral_inhibition(false)
523            .with_training_params(50, 5.0);
524
525        assert_eq!(clusterer.spike_threshold(), 0.8);
526        assert!(!clusterer.is_stdp_enabled());
527        assert!(!clusterer.is_lateral_inhibition_enabled());
528        assert_eq!(clusterer.max_epochs, 50);
529        assert_eq!(clusterer.simulation_duration, 5.0);
530    }
531
532    #[test]
533    fn test_simple_clustering() {
534        let points =
535            Array2::from_shape_vec((4, 2), vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]).unwrap();
536
537        let mut clusterer = SpikingNeuralClusterer::new(2).with_training_params(5, 1.0); // Reduced for test speed
538
539        let result = clusterer.fit(&points.view());
540        assert!(result.is_ok());
541
542        let (assignments, spike_events) = result.unwrap();
543        assert_eq!(assignments.len(), 4);
544
545        // Should have recorded some spike events
546        assert!(!spike_events.is_empty());
547    }
548
549    #[test]
550    fn test_empty_input() {
551        let points = Array2::zeros((0, 2));
552        let mut clusterer = SpikingNeuralClusterer::new(2);
553
554        let result = clusterer.fit(&points.view());
555        assert!(result.is_err());
556    }
557
558    #[test]
559    fn test_network_initialization() {
560        let mut clusterer = SpikingNeuralClusterer::new(2);
561        clusterer.initialize_network(3).unwrap();
562
563        let stats = clusterer.network_stats();
564        assert_eq!(stats.num_neurons, 5); // 3 input + 2 output
565
566        // Should have input-to-output connections
567        let expected_connections = 3 * 2; // input_dims * num_clusters
568                                          // Plus lateral inhibition connections: num_clusters * (num_clusters - 1)
569        let lateral_connections = 2;
570        assert_eq!(
571            stats.num_synapses,
572            expected_connections + lateral_connections
573        );
574    }
575
576    #[test]
577    fn test_spike_encoding() {
578        let clusterer = SpikingNeuralClusterer::new(2);
579        let point = Array1::from_vec(vec![1.0, -1.0]);
580
581        let spike_train = clusterer.encode_point_as_spikes(&point).unwrap();
582
583        // Should generate spikes for each dimension
584        assert!(!spike_train.is_empty());
585
586        // Spikes should be sorted by timestamp
587        for i in 1..spike_train.len() {
588            assert!(spike_train[i - 1].timestamp() <= spike_train[i].timestamp());
589        }
590    }
591
592    #[test]
593    fn test_network_reset() {
594        let mut clusterer = SpikingNeuralClusterer::new(2);
595        clusterer.initialize_network(2).unwrap();
596
597        // Add some activity
598        clusterer
599            .spike_history
600            .push(SpikeEvent::new(0, 1.0, 1.0, vec![0.0, 0.0]));
601        clusterer.current_time = 100.0;
602
603        // Reset should clear history and time
604        clusterer.reset();
605        assert!(clusterer.spike_history().is_empty());
606        assert_eq!(clusterer.current_time, 0.0);
607    }
608
609    #[test]
610    fn test_network_stats() {
611        let mut clusterer = SpikingNeuralClusterer::new(2);
612        clusterer.initialize_network(3).unwrap();
613
614        let stats = clusterer.network_stats();
615        assert_eq!(stats.num_neurons, 5);
616        assert!(stats.num_synapses > 0);
617        assert_eq!(stats.num_spikes, 0); // No activity yet
618        assert!(stats.average_weight.is_finite()); // Allow negative weights for inhibitory synapses
619    }
620}