gspx 0.1.2

Sparse graph signal processing and spectral graph wavelets in Rust
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
use ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use num_complex::Complex64;

use crate::{
    Laplacian, conv::DynamicConvolver, error::GspError, functions::gaussian_wavelet,
    kernel::impulse,
};

use super::{
    numerics::{
        arrays_close, gradient_complex_axis, gradient_real_axis, peak_local_max,
        real_times_complex, scalar_close, stddev,
    },
    peaks::{ClusterCloud, ModeTable, NetworkAnalysisResult, PeakCloud, Peaks},
};

/// Spectral Graph Modal Analysis engine.
pub struct Sgma {
    laplacian: Laplacian,
    /// Spatial analysis scales.
    pub scales: Array1<f64>,
    /// Temporal analysis frequencies (Hz).
    pub freqs: Array1<f64>,
    /// Spatial filter order.
    pub order: usize,
    /// Wavelet central frequency.
    pub w0: f64,
    /// Temporal scales derived from `freqs`.
    pub ts: Array1<f64>,
    /// Wavelength proxy derived from `scales`.
    pub wavlen: Array1<f64>,
    spatial_convolver: DynamicConvolver,
    temporal_matrix_cache: Option<Array2<Complex64>>,
    temporal_grid_cache: Option<Array1<f64>>,
    temporal_target_cache: Option<f64>,
}

impl Sgma {
    /// Creates an SGMA engine.
    ///
    /// # Errors
    /// Returns an error for invalid scales/frequencies or non-square Laplacians.
    pub fn new(
        l: Laplacian,
        scales: Array1<f64>,
        freqs: Array1<f64>,
        order: usize,
        w0: f64,
    ) -> Result<Self, GspError> {
        if scales.is_empty() || freqs.is_empty() {
            return Err(GspError::Dimensions(
                "scales and freqs must be non-empty".to_string(),
            ));
        }
        if scales.iter().any(|s| *s <= 0.0 || !s.is_finite()) {
            return Err(GspError::InvalidScales(
                "all scales must be finite and > 0".to_string(),
            ));
        }
        if freqs.iter().any(|f| *f <= 0.0 || !f.is_finite()) {
            return Err(GspError::InvalidScales(
                "all frequencies must be finite and > 0".to_string(),
            ));
        }

        let ts = freqs.mapv(|f| w0 / (2.0 * std::f64::consts::PI * f));
        let wavlen = scales.mapv(|s| s.sqrt());
        let spatial_convolver = DynamicConvolver::new(l.clone())?;
        Ok(Self {
            laplacian: l,
            scales,
            freqs,
            order,
            w0,
            ts,
            wavlen,
            spatial_convolver,
            temporal_matrix_cache: None,
            temporal_grid_cache: None,
            temporal_target_cache: None,
        })
    }

    /// Computes a magnitude spectrum at one bus and one analysis time.
    ///
    /// # Errors
    /// Returns an error for dimension/index mismatches.
    pub fn spectrum(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        bus: usize,
        time: f64,
    ) -> Result<Array2<f64>, GspError> {
        Ok(self
            .spectrum_core(v, t, bus, time, None)?
            .mapv(|z| z.norm()))
    }

    /// Computes a magnitude spectrum using precomputed temporal projections.
    ///
    /// # Errors
    /// Returns an error for dimension/index mismatches.
    pub fn spectrum_with_precomputed_temporal(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        bus: usize,
        time: f64,
        vb: ArrayView2<Complex64>,
    ) -> Result<Array2<f64>, GspError> {
        Ok(self
            .spectrum_core(v, t, bus, time, Some(vb))?
            .mapv(|z| z.norm()))
    }

    /// Computes a complex SGMA spectrum at one bus and one analysis time.
    ///
    /// # Errors
    /// Returns an error for dimension/index mismatches.
    pub fn spectrum_complex(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        bus: usize,
        time: f64,
    ) -> Result<Array2<Complex64>, GspError> {
        self.spectrum_core(v, t, bus, time, None)
    }

    /// Computes a complex SGMA spectrum with precomputed temporal projections.
    ///
    /// # Errors
    /// Returns an error for dimension/index mismatches.
    pub fn spectrum_complex_with_precomputed_temporal(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        bus: usize,
        time: f64,
        vb: ArrayView2<Complex64>,
    ) -> Result<Array2<Complex64>, GspError> {
        self.spectrum_core(v, t, bus, time, Some(vb))
    }

    fn spectrum_core(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        bus: usize,
        time: f64,
        vb: Option<ArrayView2<Complex64>>,
    ) -> Result<Array2<Complex64>, GspError> {
        self.validate_bus(bus)?;
        self.validate_signal_frame(v, t)?;
        let n_nodes = self.laplacian.rows();

        let vb_mat = if let Some(vb_in) = vb {
            if vb_in.nrows() != n_nodes || vb_in.ncols() != self.ts.len() {
                return Err(GspError::Dimensions(format!(
                    "precomputed VB shape {:?} does not match expected ({}, {})",
                    vb_in.raw_dim(),
                    n_nodes,
                    self.ts.len()
                )));
            }
            vb_in.to_owned()
        } else {
            let bmat = self.build_temporal_matrix(t, time)?;
            real_times_complex(v, bmat.view())
        };

        let impulse_signal = impulse(&self.laplacian, bus, 1)?;
        let spatial_scales = self.scales.as_slice().ok_or_else(|| {
            GspError::Dimensions("SGMA scales must be stored contiguously".to_string())
        })?;
        let spatial =
            self.spatial_convolver
                .bandpass(impulse_signal.view(), spatial_scales, self.order)?;
        let mut a = Array2::<f64>::zeros((spatial.len(), n_nodes));
        for (idx, r) in spatial.iter().enumerate() {
            a.row_mut(idx).assign(&r.column(0).to_owned());
        }
        Ok(real_times_complex(a.view(), vb_mat.view()))
    }

    /// Computes peaks for one bus/time pair from a magnitude SGMA spectrum.
    ///
    /// # Errors
    /// Returns an error for invalid dimensions or bus index.
    pub fn analyze(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        bus: usize,
        time: f64,
        top_n: usize,
        min_dist: usize,
    ) -> Result<Peaks, GspError> {
        if top_n == 0 {
            return Ok(Peaks::empty(false));
        }
        let s = self.spectrum(v, t, bus, time)?;
        self.find_peaks(s.view(), top_n, min_dist, false)
    }

    /// Runs SGMA analysis over many buses and aggregates peaks/clusters.
    ///
    /// # Errors
    /// Returns an error for invalid dimensions or bus indices.
    pub fn analyze_many(
        &mut self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
        time: f64,
        buses: Option<&[usize]>,
        top_n: usize,
        min_dist: usize,
    ) -> Result<NetworkAnalysisResult, GspError> {
        self.validate_signal_frame(v, t)?;
        if top_n == 0 {
            return Ok(NetworkAnalysisResult::empty());
        }

        let bus_list = if let Some(b) = buses {
            b.to_vec()
        } else {
            (0..v.nrows()).collect::<Vec<_>>()
        };
        let bmat = self.build_temporal_matrix(t, time)?;
        let vb = real_times_complex(v, bmat.view());

        let mut all_w = Vec::new();
        let mut all_f = Vec::new();
        let mut all_m = Vec::new();
        let mut all_b = Vec::new();

        for &bus in &bus_list {
            self.validate_bus(bus)?;

            let y = self.spectrum_with_precomputed_temporal(v, t, bus, time, vb.view())?;
            let p = self.find_peaks(y.view(), top_n, min_dist, false)?;
            for idx in 0..p.wavelength.len() {
                all_w.push(p.wavelength[idx]);
                all_f.push(p.frequency[idx]);
                all_m.push(p.magnitude[idx]);
                all_b.push(bus);
            }
        }

        let peaks = PeakCloud {
            wavelength: Array1::from(all_w),
            frequency: Array1::from(all_f),
            magnitude: Array1::from(all_m),
            bus_id: Array1::from(all_b),
        };
        let clusters = self.compute_density_clusters(&peaks, top_n, min_dist);
        Ok(NetworkAnalysisResult { peaks, clusters })
    }

    /// Finds local maxima in a magnitude spectrum.
    ///
    /// # Errors
    /// Returns an error when spectrum dimensions are inconsistent with SGMA grids.
    pub fn find_peaks(
        &self,
        spectrum: ArrayView2<f64>,
        top_n: usize,
        min_dist: usize,
        return_indices: bool,
    ) -> Result<Peaks, GspError> {
        if top_n == 0 {
            return Ok(Peaks::empty(return_indices));
        }
        if spectrum.nrows() != self.scales.len() || spectrum.ncols() != self.freqs.len() {
            return Err(GspError::Dimensions(format!(
                "spectrum shape {:?} does not match expected ({}, {})",
                spectrum.raw_dim(),
                self.scales.len(),
                self.freqs.len()
            )));
        }
        let min_distance = min_dist.max(1);
        let coords = peak_local_max(spectrum, min_distance, top_n);
        if coords.is_empty() {
            return Ok(Peaks::empty(return_indices));
        }
        let mut wavelengths = Vec::with_capacity(coords.len());
        let mut freqs = Vec::with_capacity(coords.len());
        let mut mags = Vec::with_capacity(coords.len());
        let mut sidx = Vec::with_capacity(coords.len());
        let mut fidx = Vec::with_capacity(coords.len());
        for (si, fi) in coords {
            wavelengths.push(self.wavlen[si]);
            freqs.push(self.freqs[fi]);
            mags.push(spectrum[[si, fi]]);
            sidx.push(si);
            fidx.push(fi);
        }
        Ok(Peaks {
            wavelength: Array1::from(wavelengths),
            frequency: Array1::from(freqs),
            magnitude: Array1::from(mags),
            scale_idx: return_indices.then(|| Array1::from(sidx)),
            freq_idx: return_indices.then(|| Array1::from(fidx)),
        })
    }

    /// Estimates modal parameters from a complex SGMA spectrum.
    ///
    /// # Errors
    /// Returns an error when gradients cannot be computed from the provided grids.
    pub fn find_modes(
        &self,
        spectrum: ArrayView2<Complex64>,
        top_n: usize,
        min_dist: usize,
    ) -> Result<ModeTable, GspError> {
        if top_n == 0 {
            return Ok(ModeTable::empty());
        }
        let mag = spectrum.mapv(|z| z.norm());
        let peaks = self.find_peaks(mag.view(), top_n, min_dist, true)?;
        if peaks.wavelength.is_empty() {
            return Ok(ModeTable::empty());
        }
        let si = peaks
            .scale_idx
            .as_ref()
            .ok_or_else(|| GspError::Parse("missing scale indices".to_string()))?;
        let fi = peaks
            .freq_idx
            .as_ref()
            .ok_or_else(|| GspError::Parse("missing frequency indices".to_string()))?;

        let log_spec = spectrum.mapv(|z| (z + Complex64::new(1e-20, 0.0)).ln());
        let grad_f = gradient_complex_axis(log_spec.view(), self.freqs.view(), 1)?;
        let grad_s = gradient_complex_axis(log_spec.view(), self.wavlen.view(), 0)?;

        let mut damping = Array1::<f64>::zeros(peaks.frequency.len());
        for idx in 0..peaks.frequency.len() {
            let s_idx = si[idx];
            let f_idx = fi[idx];
            let f0 = peaks.frequency[idx];
            let s0 = peaks.wavelength[idx];
            let omega_n = 2.0 * std::f64::consts::PI * f0;
            let dphi_df = grad_f[[s_idx, f_idx]].im;
            let dphi_ds = grad_s[[s_idx, f_idx]].im;
            let mut zeta = if dphi_df.abs() < 1e-8 {
                let log_mag = mag.mapv(|v| (v + 1e-20).ln());
                let first = gradient_real_axis(log_mag.view(), self.freqs.view(), 1)?;
                let d2 = gradient_real_axis(first.view(), self.freqs.view(), 1)?;
                let curv = d2[[s_idx, f_idx]].min(-1e-10);
                ((-2.0 / curv).sqrt()) / (2.0 * f0)
            } else {
                let zeta_f = -1.0 / (omega_n * dphi_df);
                let zeta_s = -s0 / (omega_n * dphi_ds * s0 * s0 + 1e-10);
                let w_f = dphi_df.abs() + 1e-10;
                let w_s = (dphi_ds * s0).abs() + 1e-10;
                (w_f * zeta_f + w_s * zeta_s) / (w_f + w_s)
            };
            if !zeta.is_finite() {
                zeta = 0.0;
            }
            damping[idx] = zeta.clamp(0.0, 1.0);
        }

        Ok(ModeTable {
            frequency: peaks.frequency,
            damping,
            wavelength: peaks.wavelength,
            magnitude: peaks.magnitude,
        })
    }

    /// Clears cached temporal wavelet projections.
    pub fn clear_temporal_cache(&mut self) {
        self.temporal_matrix_cache = None;
        self.temporal_grid_cache = None;
        self.temporal_target_cache = None;
    }

    fn build_temporal_matrix(
        &mut self,
        t: ArrayView1<f64>,
        time_target: f64,
    ) -> Result<&Array2<Complex64>, GspError> {
        let use_cache = self
            .temporal_grid_cache
            .as_ref()
            .zip(self.temporal_target_cache)
            .is_some_and(|(tc, tt)| {
                tc.len() == t.len()
                    && scalar_close(tt, time_target, 1e-12)
                    && arrays_close(tc.view(), t, 1e-10)
            });
        if !use_cache {
            let time_grid = t.to_owned();
            let mut temporal_matrix = Array2::<Complex64>::zeros((t.len(), self.ts.len()));
            for (idx, &sc) in self.ts.iter().enumerate() {
                let wavelet = gaussian_wavelet(&time_grid, sc, time_target, self.w0);
                temporal_matrix.column_mut(idx).assign(&wavelet);
            }
            self.temporal_matrix_cache = Some(temporal_matrix);
            self.temporal_grid_cache = Some(time_grid);
            self.temporal_target_cache = Some(time_target);
        }
        self.temporal_matrix_cache
            .as_ref()
            .ok_or_else(|| GspError::Cache("temporal matrix cache missing".to_string()))
    }

    fn compute_density_clusters(
        &self,
        peaks: &PeakCloud,
        top_n: usize,
        min_dist: usize,
    ) -> ClusterCloud {
        if peaks.wavelength.len() < 2 {
            return ClusterCloud::empty();
        }

        let x = peaks.wavelength.mapv(|w| w.log10());
        let y = peaks.frequency.clone();
        let n = x.len() as f64;
        let hx = (1.06 * stddev(x.view()) * n.powf(-0.2)).max(1e-6);
        let hy = (1.06 * stddev(y.view()) * n.powf(-0.2)).max(1e-6);
        let gx = self.wavlen.mapv(|w| w.log10());
        let gy = self.freqs.clone();
        let mut z = Array2::<f64>::zeros((gx.len(), gy.len()));
        let norm = 1.0 / (2.0 * std::f64::consts::PI * hx * hy * n);

        for i in 0..gx.len() {
            for j in 0..gy.len() {
                let mut acc = 0.0;
                for k in 0..x.len() {
                    let dx = (gx[i] - x[k]) / hx;
                    let dy = (gy[j] - y[k]) / hy;
                    acc += (-0.5 * (dx * dx + dy * dy)).exp();
                }
                z[[i, j]] = norm * acc;
            }
        }

        self.find_peaks(z.view(), top_n, min_dist, false)
            .map(|p| ClusterCloud {
                wavelength: p.wavelength,
                frequency: p.frequency,
                density: p.magnitude,
            })
            .unwrap_or_else(|_| ClusterCloud::empty())
    }

    fn validate_signal_frame(
        &self,
        v: ArrayView2<f64>,
        t: ArrayView1<f64>,
    ) -> Result<(), GspError> {
        let n_nodes = self.laplacian.rows();
        if v.nrows() != n_nodes {
            return Err(GspError::Dimensions(format!(
                "signal matrix rows {} does not match graph size {}",
                v.nrows(),
                n_nodes
            )));
        }
        if v.ncols() != t.len() {
            return Err(GspError::Dimensions(format!(
                "signal time columns {} does not match time vector length {}",
                v.ncols(),
                t.len()
            )));
        }
        Ok(())
    }

    fn validate_bus(&self, bus: usize) -> Result<(), GspError> {
        let n_nodes = self.laplacian.rows();
        if bus >= n_nodes {
            return Err(GspError::Index(format!(
                "bus {bus} out of bounds for graph with {n_nodes} nodes"
            )));
        }
        Ok(())
    }
}