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/// ]).expect("Operation failed");
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()).expect("Operation failed");
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| {
278            a.timestamp()
279                .partial_cmp(&b.timestamp())
280                .expect("Operation failed")
281        });
282
283        Ok(spike_train)
284    }
285
286    /// Process spike train through network
287    ///
288    /// Simulates the network dynamics when presented with a spike train,
289    /// determining which output neuron responds most strongly.
290    fn process_spike_train(&mut self, spike_train: &[SpikeEvent]) -> SpatialResult<usize> {
291        let input_dims = self.neurons.len() - self.num_clusters;
292        let mut neuron_spike_counts = vec![0; self.num_clusters];
293
294        // Simulate network for duration of spike train
295        let mut t = self.current_time;
296        let mut spike_idx = 0;
297
298        while t < self.current_time + self.simulation_duration {
299            // Apply input spikes
300            let mut input_currents = vec![0.0; self.neurons.len()];
301
302            while spike_idx < spike_train.len() && spike_train[spike_idx].timestamp() <= t {
303                let spike = &spike_train[spike_idx];
304                if spike.neuron_id() < input_dims {
305                    input_currents[spike.neuron_id()] += spike.amplitude();
306                }
307                spike_idx += 1;
308            }
309
310            // Calculate synaptic currents
311            for synapse in &self.synapses {
312                if synapse.pre_neuron() < self.neurons.len()
313                    && synapse.post_neuron() < self.neurons.len()
314                {
315                    let pre_current = input_currents[synapse.pre_neuron()];
316                    let synaptic_current = synapse.synaptic_current(pre_current);
317                    input_currents[synapse.post_neuron()] += synaptic_current;
318                }
319            }
320
321            // Update neurons and check for spikes
322            for (neuron_idx, neuron) in self.neurons.iter_mut().enumerate() {
323                let spiked = neuron.update(self.dt, input_currents[neuron_idx]);
324
325                if spiked && neuron_idx >= input_dims {
326                    let cluster_idx = neuron_idx - input_dims;
327                    neuron_spike_counts[cluster_idx] += 1;
328
329                    // Record spike event
330                    let spike_event =
331                        SpikeEvent::new(neuron_idx, t, 1.0, neuron.position().to_vec());
332                    self.spike_history.push(spike_event);
333                }
334            }
335
336            t += self.dt;
337        }
338
339        // Find winning neuron (cluster with most spikes)
340        let winning_cluster = neuron_spike_counts
341            .iter()
342            .enumerate()
343            .max_by(|(_, a), (_, b)| a.cmp(b))
344            .map(|(idx, _)| idx)
345            .unwrap_or(0);
346
347        Ok(winning_cluster)
348    }
349
350    /// Apply STDP learning to synapses
351    ///
352    /// Updates synaptic weights based on the relative timing of pre- and
353    /// post-synaptic spikes using the STDP learning rule.
354    fn apply_stdp_learning(&mut self, spike_train: &[SpikeEvent]) -> SpatialResult<()> {
355        // Create spike timing map
356        let mut spike_times: HashMap<usize, Vec<f64>> = HashMap::new();
357        for spike in spike_train {
358            spike_times
359                .entry(spike.neuron_id())
360                .or_default()
361                .push(spike.timestamp());
362        }
363
364        // Add output neuron spikes from history
365        for spike in &self.spike_history {
366            spike_times
367                .entry(spike.neuron_id())
368                .or_default()
369                .push(spike.timestamp());
370        }
371
372        // Update synaptic weights using STDP
373        let empty_spikes = Vec::new();
374        for synapse in &mut self.synapses {
375            let pre_spikes = spike_times
376                .get(&synapse.pre_neuron())
377                .unwrap_or(&empty_spikes);
378            let post_spikes = spike_times
379                .get(&synapse.post_neuron())
380                .unwrap_or(&empty_spikes);
381
382            // Check for coincident spikes
383            for &pre_time in pre_spikes {
384                for &post_time in post_spikes {
385                    let dt = post_time - pre_time;
386                    if dt.abs() < 50.0 {
387                        // Within STDP window
388                        let current_weight = synapse.weight();
389                        if dt > 0.0 {
390                            // Potentiation
391                            let delta_w = synapse.stdp_rate() * (-dt / synapse.stdp_tau()).exp();
392                            synapse.set_weight(current_weight + delta_w);
393                        } else {
394                            // Depression
395                            let delta_w = synapse.stdp_rate() * (dt / synapse.stdp_tau()).exp();
396                            synapse.set_weight(current_weight - delta_w);
397                        }
398                    }
399                }
400            }
401        }
402
403        Ok(())
404    }
405
406    /// Apply lateral inhibition between output neurons
407    ///
408    /// Strengthens inhibitory connections between neurons based on their
409    /// relative activity levels to promote competition.
410    fn apply_lateral_inhibition(&mut self) -> SpatialResult<()> {
411        let input_dims = self.neurons.len() - self.num_clusters;
412
413        // Strengthen inhibitory connections between active neurons
414        for i in 0..self.num_clusters {
415            for j in 0..self.num_clusters {
416                if i != j {
417                    let neuron_i_idx = input_dims + i;
418                    let neuron_j_idx = input_dims + j;
419
420                    // Find inhibitory synapse
421                    for synapse in &mut self.synapses {
422                        if synapse.pre_neuron() == neuron_i_idx
423                            && synapse.post_neuron() == neuron_j_idx
424                        {
425                            // Strengthen inhibition based on activity
426                            let activity_i = self.neurons[neuron_i_idx].membrane_potential();
427                            let activity_j = self.neurons[neuron_j_idx].membrane_potential();
428
429                            if activity_i > activity_j {
430                                let current_weight = synapse.weight();
431                                synapse.set_weight(current_weight - 0.01); // Strengthen inhibition
432                            }
433                        }
434                    }
435                }
436            }
437        }
438
439        Ok(())
440    }
441
442    /// Get number of clusters
443    pub fn num_clusters(&self) -> usize {
444        self.num_clusters
445    }
446
447    /// Get spike threshold
448    pub fn spike_threshold(&self) -> f64 {
449        self.spike_threshold
450    }
451
452    /// Check if STDP learning is enabled
453    pub fn is_stdp_enabled(&self) -> bool {
454        self.stdp_learning
455    }
456
457    /// Check if lateral inhibition is enabled
458    pub fn is_lateral_inhibition_enabled(&self) -> bool {
459        self.lateral_inhibition
460    }
461
462    /// Get current spike history
463    pub fn spike_history(&self) -> &[SpikeEvent] {
464        &self.spike_history
465    }
466
467    /// Get network statistics
468    pub fn network_stats(&self) -> NetworkStats {
469        NetworkStats {
470            num_neurons: self.neurons.len(),
471            num_synapses: self.synapses.len(),
472            num_spikes: self.spike_history.len(),
473            average_weight: if self.synapses.is_empty() {
474                0.0
475            } else {
476                self.synapses.iter().map(|s| s.weight()).sum::<f64>() / self.synapses.len() as f64
477            },
478        }
479    }
480
481    /// Reset the network to initial state
482    pub fn reset(&mut self) {
483        for neuron in &mut self.neurons {
484            neuron.reset();
485        }
486        for synapse in &mut self.synapses {
487            synapse.reset_spike_history();
488        }
489        self.spike_history.clear();
490        self.current_time = 0.0;
491    }
492}
493
494/// Network statistics for analysis
495#[derive(Debug, Clone)]
496pub struct NetworkStats {
497    /// Total number of neurons
498    pub num_neurons: usize,
499    /// Total number of synapses
500    pub num_synapses: usize,
501    /// Total number of spikes recorded
502    pub num_spikes: usize,
503    /// Average synaptic weight
504    pub average_weight: f64,
505}
506
507#[cfg(test)]
508mod tests {
509    use super::*;
510    use scirs2_core::ndarray::Array2;
511
512    #[test]
513    fn test_spiking_clusterer_creation() {
514        let clusterer = SpikingNeuralClusterer::new(3);
515        assert_eq!(clusterer.num_clusters(), 3);
516        assert_eq!(clusterer.spike_threshold(), 1.0);
517        assert!(clusterer.is_stdp_enabled());
518        assert!(clusterer.is_lateral_inhibition_enabled());
519    }
520
521    #[test]
522    fn test_clusterer_configuration() {
523        let clusterer = SpikingNeuralClusterer::new(2)
524            .with_spike_threshold(0.8)
525            .with_stdp_learning(false)
526            .with_lateral_inhibition(false)
527            .with_training_params(50, 5.0);
528
529        assert_eq!(clusterer.spike_threshold(), 0.8);
530        assert!(!clusterer.is_stdp_enabled());
531        assert!(!clusterer.is_lateral_inhibition_enabled());
532        assert_eq!(clusterer.max_epochs, 50);
533        assert_eq!(clusterer.simulation_duration, 5.0);
534    }
535
536    #[test]
537    fn test_simple_clustering() {
538        let points = Array2::from_shape_vec((4, 2), vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0])
539            .expect("Operation failed");
540
541        let mut clusterer = SpikingNeuralClusterer::new(2).with_training_params(5, 1.0); // Reduced for test speed
542
543        let result = clusterer.fit(&points.view());
544        assert!(result.is_ok());
545
546        let (assignments, spike_events) = result.expect("Operation failed");
547        assert_eq!(assignments.len(), 4);
548
549        // Should have recorded some spike events
550        assert!(!spike_events.is_empty());
551    }
552
553    #[test]
554    fn test_empty_input() {
555        let points = Array2::zeros((0, 2));
556        let mut clusterer = SpikingNeuralClusterer::new(2);
557
558        let result = clusterer.fit(&points.view());
559        assert!(result.is_err());
560    }
561
562    #[test]
563    fn test_network_initialization() {
564        let mut clusterer = SpikingNeuralClusterer::new(2);
565        clusterer.initialize_network(3).expect("Operation failed");
566
567        let stats = clusterer.network_stats();
568        assert_eq!(stats.num_neurons, 5); // 3 input + 2 output
569
570        // Should have input-to-output connections
571        let expected_connections = 3 * 2; // input_dims * num_clusters
572                                          // Plus lateral inhibition connections: num_clusters * (num_clusters - 1)
573        let lateral_connections = 2;
574        assert_eq!(
575            stats.num_synapses,
576            expected_connections + lateral_connections
577        );
578    }
579
580    #[test]
581    fn test_spike_encoding() {
582        let clusterer = SpikingNeuralClusterer::new(2);
583        let point = Array1::from_vec(vec![1.0, -1.0]);
584
585        let spike_train = clusterer
586            .encode_point_as_spikes(&point)
587            .expect("Operation failed");
588
589        // Should generate spikes for each dimension
590        assert!(!spike_train.is_empty());
591
592        // Spikes should be sorted by timestamp
593        for i in 1..spike_train.len() {
594            assert!(spike_train[i - 1].timestamp() <= spike_train[i].timestamp());
595        }
596    }
597
598    #[test]
599    fn test_network_reset() {
600        let mut clusterer = SpikingNeuralClusterer::new(2);
601        clusterer.initialize_network(2).expect("Operation failed");
602
603        // Add some activity
604        clusterer
605            .spike_history
606            .push(SpikeEvent::new(0, 1.0, 1.0, vec![0.0, 0.0]));
607        clusterer.current_time = 100.0;
608
609        // Reset should clear history and time
610        clusterer.reset();
611        assert!(clusterer.spike_history().is_empty());
612        assert_eq!(clusterer.current_time, 0.0);
613    }
614
615    #[test]
616    fn test_network_stats() {
617        let mut clusterer = SpikingNeuralClusterer::new(2);
618        clusterer.initialize_network(3).expect("Operation failed");
619
620        let stats = clusterer.network_stats();
621        assert_eq!(stats.num_neurons, 5);
622        assert!(stats.num_synapses > 0);
623        assert_eq!(stats.num_spikes, 0); // No activity yet
624        assert!(stats.average_weight.is_finite()); // Allow negative weights for inhibitory synapses
625    }
626}