bixverse-rs 0.4.7

A Rust library with various utilities for computational biology and single cell.
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
//! Sparse backend for NMF. Holds the chosen layer of V as CSC and CSR with
//! storage type `S`; widens to compute type `F` per element inside two
//! hand-rolled spMM kernels.

use faer::{Mat, MatRef};
use num_traits::{Float, ToPrimitive};
use rayon::prelude::*;

use super::NmfInput;
use crate::core::math::pca_svd::SvdResults;
use crate::core::math::sparse::sparse_svd_lanczos;
use crate::core::math::vector_helpers::sum_sq_f64;
use crate::prelude::*;

/////////////
// Helpers //
/////////////

/// Cast and validate a slice of values for NMF input.
///
/// Casts each element from `T` to `S`, then checks finiteness and
/// non-negativity.
///
/// ### Params
///
/// * `src` - Source slice to cast and validate.
///
/// ### Returns
///
/// A validated `Vec<S>`, or [`BixverseErrors::NmfNonFinite`] if any cast
/// value is non-finite, or [`BixverseErrors::NmfNonNegativeViolated`] if
/// any cast value is negative.
fn cast_and_validate<T, S>(src: &[T]) -> Result<Vec<S>, BixverseErrors>
where
    T: Copy + Into<S>,
    S: Float,
{
    let mut out = Vec::with_capacity(src.len());
    for &v in src {
        let s: S = v.into();
        if !s.is_finite() {
            return Err(BixverseErrors::NmfNonFinite);
        }
        if s < S::zero() {
            return Err(BixverseErrors::NmfNonNegativeViolated);
        }
        out.push(s);
    }
    Ok(out)
}

/// Squared Frobenius norm of a flat data buffer.
///
/// Accumulates in `f64` via [`sum_sq_f64`] and casts once, rather than summing in
/// `F`. Over 3e7 non-zeros the `F = f32` version measured 4.4% low, and that error
/// lands directly in every relative loss the solver reports.
///
/// ### Params
///
/// * `data` - Flat non-zero value buffer.
///
/// ### Returns
///
/// The scalar `||V||_F^2`.
fn compute_sq_frob<S, F>(data: &[S]) -> F
where
    S: BixverseNumeric + ToPrimitive,
    F: BixverseFloat,
{
    F::from_f64(sum_sq_f64(data)).unwrap()
}

/// Build a matched (CSR, CSC) pair from a source matrix and its transpose.
///
/// Branches on the source orientation so the returned tuple is always
/// `(csr, csc)` regardless of which was passed as `source`.
///
/// ### Params
///
/// * `source` - The primary sparse matrix.
/// * `other` - Its transpose in the opposite format.
/// * `source_cast` - Cast non-zero values for `source`.
/// * `other_cast` - Cast non-zero values for `other`.
/// * `shape` - Logical shape `(m, n)` of the matrix.
///
/// ### Returns
///
/// A `(csr, csc)` pair with indices and indptr borrowed from the inputs.
fn build_pair<S, T, U>(
    source: &CompressedSparseData2<T, U>,
    other: CompressedSparseData2<T, U>,
    source_cast: Vec<S>,
    other_cast: Vec<S>,
    shape: (usize, usize),
) -> (CompressedSparseData2<S>, CompressedSparseData2<S>)
where
    S: Clone,
    T: Clone,
    U: Clone,
{
    match source.cs_type {
        CompressedSparseFormat::Csr => {
            let csr = CompressedSparseData2 {
                data: source_cast,
                indices: source.indices.clone(),
                indptr: source.indptr.clone(),
                cs_type: CompressedSparseFormat::Csr,
                data_2: None,
                shape,
            };
            let csc = CompressedSparseData2 {
                data: other_cast,
                indices: other.indices,
                indptr: other.indptr,
                cs_type: CompressedSparseFormat::Csc,
                data_2: None,
                shape,
            };
            (csr, csc)
        }
        CompressedSparseFormat::Csc => {
            let csc = CompressedSparseData2 {
                data: source_cast,
                indices: source.indices.clone(),
                indptr: source.indptr.clone(),
                cs_type: CompressedSparseFormat::Csc,
                data_2: None,
                shape,
            };
            let csr = CompressedSparseData2 {
                data: other_cast,
                indices: other.indices,
                indptr: other.indptr,
                cs_type: CompressedSparseFormat::Csr,
                data_2: None,
                shape,
            };
            (csr, csc)
        }
    }
}

/// Sparse W^T V via CSC.
///
/// Computes `out = W^T V` using the CSC layout of V, parallelising over output
/// columns.
///
/// ### Params
///
/// * `csc` - CSC representation of V, shape `m x n`.
/// * `w` - Left factor, shape `m x k`.
/// * `out` - Output buffer `k x n`. Overwritten on return.
fn spmm_csc_wt_v<S, F>(csc: &CompressedSparseData2<S>, w: MatRef<F>, out: &mut Mat<F>)
where
    S: BixverseNumeric + Into<F> + Sync,
    F: BixverseFloat + Send + Sync,
{
    let n = csc.shape.1;
    let k = w.ncols();

    let out_ptr = out.as_ptr_mut() as usize;
    let out_row_stride = out.row_stride();
    let out_col_stride = out.col_stride();

    (0..n).into_par_iter().for_each_init(
        || vec![F::zero(); k],
        |acc, j| {
            acc.fill(F::zero());

            let start = csc.indptr[j] as usize;
            let end = csc.indptr[j + 1] as usize;
            for idx in start..end {
                let i = csc.indices[idx] as usize;
                let val_f: F = csc.data[idx].into();
                for r in 0..k {
                    acc[r] += val_f * w[(i, r)];
                }
            }

            let base = out_ptr as *mut F;
            for r in 0..k {
                unsafe {
                    let ptr =
                        base.offset(r as isize * out_row_stride + j as isize * out_col_stride);
                    *ptr = acc[r];
                }
            }
        },
    );
}

/// Sparse V H^T via CSR.
///
/// Computes `out = V H^T` using the CSR layout of V, parallelising over output
/// rows.
///
/// ### Params
///
/// * `csr` - CSR representation of V, shape `m x n`.
/// * `h` - Right factor, shape `k x n`.
/// * `out` - Output buffer `m x k`. Overwritten on return.
fn spmm_csr_v_ht<S, F>(csr: &CompressedSparseData2<S>, h: MatRef<F>, out: &mut Mat<F>)
where
    S: BixverseNumeric + Into<F> + Sync,
    F: BixverseFloat + Send + Sync,
{
    let m = csr.shape.0;
    let k = h.nrows();

    let out_ptr = out.as_ptr_mut() as usize;
    let out_row_stride = out.row_stride();
    let out_col_stride = out.col_stride();

    (0..m).into_par_iter().for_each_init(
        || vec![F::zero(); k],
        |acc, i| {
            acc.fill(F::zero());

            let start = csr.indptr[i] as usize;
            let end = csr.indptr[i + 1] as usize;
            for idx in start..end {
                let j = csr.indices[idx] as usize;
                let val_f: F = csr.data[idx].into();
                for r in 0..k {
                    acc[r] += val_f * h[(r, j)];
                }
            }

            let base = out_ptr as *mut F;
            for r in 0..k {
                unsafe {
                    let ptr =
                        base.offset(i as isize * out_row_stride + r as isize * out_col_stride);
                    *ptr = acc[r];
                }
            }
        },
    );
}

//////////
// Main //
//////////

/// Sparse NMF input backed by a matched (CSR, CSC) pair.
///
/// Validates non-negativity and finiteness once at construction, then computes
/// and caches `||V||_F^2`.
#[derive(Debug)]
pub struct SparseInput<S, F>
where
    S: BixverseNumeric + Into<F> + Float,
    F: BixverseFloat,
{
    /// CSR layout of V, used for `V H^T`.
    csr: CompressedSparseData2<S>,
    /// CSC layout of V, used for `W^T V`.
    csc: CompressedSparseData2<S>,
    /// Cached `||V||_F^2`.
    sq_frob: F,
    /// Logical shape `(m, n)` of V.
    shape: (usize, usize),
}

impl<S, F> SparseInput<S, F>
where
    S: BixverseNumeric + BixverseSimd + Into<F> + Float + Clone,
    F: BixverseFloat + BixverseSimd + std::iter::Sum,
{
    /// Construct a [`SparseInput`] from the primary data layer of `matrix`.
    ///
    /// Casts and validates `data`, derives the transpose, and builds the
    /// matched (CSR, CSC) pair.
    ///
    /// ### Params
    ///
    /// * `matrix` - Source sparse matrix whose `data` field is used.
    ///
    /// ### Returns
    ///
    /// A [`SparseInput`], or [`BixverseErrors::NmfNonFinite`] /
    /// [`BixverseErrors::NmfNonNegativeViolated`] if validation fails.
    pub fn from_primary<T, U>(matrix: &CompressedSparseData2<T, U>) -> Result<Self, BixverseErrors>
    where
        T: BixverseNumeric + Into<S>,
        U: BixverseNumeric,
    {
        let shape = matrix.shape;
        let other = matrix.transform_single_layer(false)?;

        let primary: Vec<S> = cast_and_validate::<T, S>(&matrix.data)?;
        let secondary: Vec<S> = other.data.iter().map(|&v| v.into()).collect();

        let (csr, csc) = build_pair(matrix, other, primary, secondary, shape);
        let sq_frob = compute_sq_frob::<S, F>(&csr.data);

        Ok(Self {
            csr,
            csc,
            sq_frob,
            shape,
        })
    }

    /// Construct a [`SparseInput`] from the secondary data layer of `matrix`.
    ///
    /// Casts and validates `data_2`, derives the transpose, and builds the
    /// matched (CSR, CSC) pair. Intended for the log1p-normalised layer in the
    /// single-cell case.
    ///
    /// ### Params
    ///
    /// * `matrix` - Source sparse matrix whose `data_2` field is used.
    ///
    /// ### Returns
    ///
    /// A [`SparseInput`], or [`BixverseErrors::Data2NotAvailable`] if
    /// `data_2` is absent, or [`BixverseErrors::NmfNonFinite`] /
    /// [`BixverseErrors::NmfNonNegativeViolated`] if validation fails.
    pub fn from_secondary<T, U>(
        matrix: &CompressedSparseData2<T, U>,
    ) -> Result<Self, BixverseErrors>
    where
        T: BixverseNumeric,
        U: BixverseNumeric + Into<S>,
    {
        let shape = matrix.shape;
        let other = matrix.transform_single_layer(true)?;

        let m_data2 = matrix
            .data_2
            .as_ref()
            .ok_or(BixverseErrors::Data2NotAvailable)?;
        let o_data2 = other
            .data_2
            .as_ref()
            .ok_or(BixverseErrors::Data2NotAvailable)?;

        let primary: Vec<S> = cast_and_validate::<U, S>(m_data2)?;
        let secondary: Vec<S> = o_data2.iter().map(|&v| v.into()).collect();

        let (csr, csc) = build_pair(matrix, other, primary, secondary, shape);
        let sq_frob = compute_sq_frob::<S, F>(&csr.data);

        Ok(Self {
            csr,
            csc,
            sq_frob,
            shape,
        })
    }

    /// The CSR layout of V, used for `V H^T`.
    ///
    /// Exposed so a GPU backend can upload the already-validated matched pair
    /// rather than rebuilding and revalidating it.
    ///
    /// ### Returns
    ///
    /// A reference to the CSR layer.
    pub fn csr(&self) -> &CompressedSparseData2<S> {
        &self.csr
    }

    /// The CSC layout of V, used for `W^T V`.
    ///
    /// Exposed for the same reason as [`Self::csr`].
    ///
    /// ### Returns
    ///
    /// A reference to the CSC layer.
    pub fn csc(&self) -> &CompressedSparseData2<S> {
        &self.csc
    }
}

/// NmfInput trait implementation for [SparseInput]
impl<S, F> NmfInput<F> for SparseInput<S, F>
where
    S: BixverseNumeric + BixverseSimd + Into<F> + Float + Clone + Sync,
    F: BixverseFloat + BixverseSimd + Send + Sync + std::iter::Sum,
{
    fn shape(&self) -> (usize, usize) {
        self.shape
    }

    fn sq_frob(&self) -> F {
        self.sq_frob
    }

    fn wt_v(&self, w: MatRef<F>, out: &mut Mat<F>) {
        spmm_csc_wt_v::<S, F>(&self.csc, w, out);
    }

    fn v_ht(&self, h: MatRef<F>, out: &mut Mat<F>) {
        spmm_csr_v_ht::<S, F>(&self.csr, h, out);
    }

    fn top_k_svd(&self, k: usize) -> Result<SvdResults<F>, BixverseErrors> {
        sparse_svd_lanczos::<S, S, F>(&self.csr, k, 42, false, None, None, None)
    }
}

///////////
// Tests //
///////////

#[cfg(test)]
mod tests {
    use super::*;
    use crate::methods::nmf_hals::dense::DenseInput;
    use faer::Mat;

    /// One 3x4 matrix in both CSR and dense form, for sparse against dense parity checks.
    fn fixture_csr() -> (CompressedSparseData2<f64, f64>, Mat<f64>) {
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let indices: Vec<u32> = vec![0, 2, 1, 3, 0, 3];
        let indptr: Vec<u32> = vec![0, 2, 4, 6];
        let csr =
            CompressedSparseData2::<f64, f64>::new_csr(&data, &indices, &indptr, None, (3, 4));
        let dense = Mat::from_fn(3, 4, |i, j| match (i, j) {
            (0, 0) => 1.0,
            (0, 2) => 2.0,
            (1, 1) => 3.0,
            (1, 3) => 4.0,
            (2, 0) => 5.0,
            (2, 3) => 6.0,
            _ => 0.0,
        });
        (csr, dense)
    }

    /// `||V||_F^2` over the non-zeros has to be accurate on data where summing in
    /// `F` visibly is not. Sparse is the path where this bit hardest, because the
    /// term count is the non-zero count: measured 4.4% low over 3e7 non-zeros
    /// before the accumulator moved to f64.
    #[test]
    fn sq_frob_survives_a_swamped_accumulator() {
        // One leading non-zero squaring to 1e8, then a million ones. At 1e8 the
        // f32 spacing is 8, so a running f32 total swallows every one of them.
        let n_ones = 1_000_000usize;
        let mut data = vec![1e4f32];
        data.extend(std::iter::repeat_n(1.0f32, n_ones));
        let nnz = data.len();
        let indices: Vec<u32> = (0..nnz as u32).collect();
        let indptr: Vec<u32> = vec![0, nnz as u32];
        let csr =
            CompressedSparseData2::<f32, f32>::new_csr(&data, &indices, &indptr, None, (1, nnz));

        let sparse_in: SparseInput<f32, f32> = SparseInput::from_primary(&csr).unwrap();
        let exact = 1e8 + n_ones as f64;
        let got = sparse_in.sq_frob() as f64;
        assert!(
            (got - exact).abs() <= 1e-5 * exact,
            "sq_frob is off: got {got}, exact {exact}"
        );

        let naive = data.iter().fold(0f32, |acc, &v| acc + v * v) as f64;
        assert!(
            (naive - exact).abs() > 0.005 * exact,
            "the f32 reference was supposed to be visibly wrong, so this test proves nothing"
        );
    }

    /// The sparse backend reports the same shape and squared Frobenius norm as the dense one.
    #[test]
    fn shape_and_sq_frob_match_dense() {
        let (csr, dense) = fixture_csr();
        let sparse_in: SparseInput<f64, f64> = SparseInput::from_primary(&csr).unwrap();
        let dense_in = DenseInput::new(dense.as_ref()).unwrap();
        assert_eq!(sparse_in.shape(), dense_in.shape());
        assert!((sparse_in.sq_frob() - dense_in.sq_frob()).abs() < 1e-12);
    }

    /// A negative non-zero is refused at construction rather than silently factorised.
    #[test]
    fn rejects_negative() {
        let data = vec![1.0, -2.0];
        let indices: Vec<u32> = vec![0, 1];
        let indptr: Vec<u32> = vec![0, 2];
        let csr =
            CompressedSparseData2::<f64, f64>::new_csr(&data, &indices, &indptr, None, (1, 2));
        let err = SparseInput::<f64, f64>::from_primary(&csr).unwrap_err();
        assert!(matches!(err, BixverseErrors::NmfNonNegativeViolated));
    }

    /// A NaN non-zero is refused at construction.
    #[test]
    fn rejects_non_finite() {
        let data = vec![1.0, f64::NAN];
        let indices: Vec<u32> = vec![0, 1];
        let indptr: Vec<u32> = vec![0, 2];
        let csr =
            CompressedSparseData2::<f64, f64>::new_csr(&data, &indices, &indptr, None, (1, 2));
        let err = SparseInput::<f64, f64>::from_primary(&csr).unwrap_err();
        assert!(matches!(err, BixverseErrors::NmfNonFinite));
    }

    /// The hand-rolled CSC `W^T V` kernel agrees with the dense faer matmul.
    #[test]
    fn wt_v_matches_dense() {
        let (csr, dense) = fixture_csr();
        let k = 2;
        let w: Mat<f64> = Mat::from_fn(3, k, |i, j| (i + j + 1) as f64);
        let sparse_in: SparseInput<f64, f64> = SparseInput::from_primary(&csr).unwrap();
        let dense_in = DenseInput::new(dense.as_ref()).unwrap();
        let mut out_s = Mat::<f64>::zeros(k, 4);
        let mut out_d = Mat::<f64>::zeros(k, 4);
        sparse_in.wt_v(w.as_ref(), &mut out_s);
        dense_in.wt_v(w.as_ref(), &mut out_d);
        for r in 0..k {
            for j in 0..4 {
                assert!((out_s[(r, j)] - out_d[(r, j)]).abs() < 1e-9);
            }
        }
    }

    /// The hand-rolled CSR `V H^T` kernel agrees with the dense faer matmul.
    #[test]
    fn v_ht_matches_dense() {
        let (csr, dense) = fixture_csr();
        let k = 2;
        let h: Mat<f64> = Mat::from_fn(k, 4, |i, j| (i + j + 1) as f64);
        let sparse_in: SparseInput<f64, f64> = SparseInput::from_primary(&csr).unwrap();
        let dense_in = DenseInput::new(dense.as_ref()).unwrap();
        let mut out_s = Mat::<f64>::zeros(3, k);
        let mut out_d = Mat::<f64>::zeros(3, k);
        sparse_in.v_ht(h.as_ref(), &mut out_s);
        dense_in.v_ht(h.as_ref(), &mut out_d);
        for i in 0..3 {
            for r in 0..k {
                assert!((out_s[(i, r)] - out_d[(i, r)]).abs() < 1e-9);
            }
        }
    }

    /// A CSC source takes the other arm of `build_pair`, and must still yield the same products.
    #[test]
    fn csc_input_produces_same_products() {
        // Same logical matrix but constructed as CSC.
        let data = vec![1.0, 5.0, 3.0, 2.0, 4.0, 6.0];
        let indices: Vec<u32> = vec![0, 2, 1, 0, 1, 2];
        let indptr: Vec<u32> = vec![0, 2, 3, 4, 6];
        let csc =
            CompressedSparseData2::<f64, f64>::new_csc(&data, &indices, &indptr, None, (3, 4));
        let dense = Mat::from_fn(3, 4, |i, j| match (i, j) {
            (0, 0) => 1.0,
            (0, 2) => 2.0,
            (1, 1) => 3.0,
            (1, 3) => 4.0,
            (2, 0) => 5.0,
            (2, 3) => 6.0,
            _ => 0.0,
        });

        let sparse_in: SparseInput<f64, f64> = SparseInput::from_primary(&csc).unwrap();
        let dense_in = DenseInput::new(dense.as_ref()).unwrap();

        let k = 2;
        let w: Mat<f64> = Mat::from_fn(3, k, |i, j| (i + j + 1) as f64);
        let mut out_s = Mat::<f64>::zeros(k, 4);
        let mut out_d = Mat::<f64>::zeros(k, 4);
        sparse_in.wt_v(w.as_ref(), &mut out_s);
        dense_in.wt_v(w.as_ref(), &mut out_d);
        for r in 0..k {
            for j in 0..4 {
                assert!((out_s[(r, j)] - out_d[(r, j)]).abs() < 1e-9);
            }
        }
    }

    /// `from_secondary` reads the `data_2` layer, so the cached norm comes from it, not `data`.
    #[test]
    fn from_secondary_uses_data_2() {
        let data = vec![10.0, 20.0, 30.0, 40.0, 50.0, 60.0];
        let data_2 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let indices: Vec<u32> = vec![0, 2, 1, 3, 0, 3];
        let indptr: Vec<u32> = vec![0, 2, 4, 6];
        let csr = CompressedSparseData2::<f64, f64>::new_csr(
            &data,
            &indices,
            &indptr,
            Some(&data_2),
            (3, 4),
        );

        let sparse_in: SparseInput<f64, f64> = SparseInput::from_secondary(&csr).unwrap();
        // sq_frob is from data_2, not data
        let expected: f64 = data_2.iter().map(|v| v * v).sum();
        assert!((sparse_in.sq_frob() - expected).abs() < 1e-12);
    }

    /// The Lanczos SVD returns k triplets of the right shape, in descending singular value order.
    #[test]
    fn top_k_svd_shapes() {
        let (csr, _) = fixture_csr();
        let sparse_in: SparseInput<f64, f64> = SparseInput::from_primary(&csr).unwrap();
        let svd = sparse_in.top_k_svd(2).unwrap();
        assert_eq!(svd.u.shape(), (3, 2));
        assert_eq!(svd.v.shape(), (4, 2));
        assert_eq!(svd.s.len(), 2);
        assert!(svd.s[0] >= svd.s[1]);
    }
}