cyanea-omics 0.1.0

Omics data structures for the Cyanea bioinformatics ecosystem
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
//! Single-cell clustering: kNN graph construction, Leiden, Louvain, NMI, ARI.

use std::collections::HashMap;

use cyanea_core::{CyaneaError, Result};

use crate::network::Graph;
use crate::single_cell::{AnnData, ColumnData};
use crate::sparse::SparseMatrix;

// ── Neighbors ──────────────────────────────────────────────────────────────

/// Distance metric for neighbor graph construction.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DistanceMetric {
    Euclidean,
    Cosine,
}

/// Configuration for kNN graph construction.
#[derive(Debug, Clone)]
pub struct NeighborsConfig {
    /// Number of nearest neighbors.
    pub n_neighbors: usize,
    /// Number of PCs to use from `obsm["X_pca"]`.
    pub n_pcs: usize,
    /// Distance metric.
    pub metric: DistanceMetric,
    /// Random seed (unused currently, reserved for approximate methods).
    pub seed: u64,
}

impl Default for NeighborsConfig {
    fn default() -> Self {
        Self {
            n_neighbors: 15,
            n_pcs: 50,
            metric: DistanceMetric::Euclidean,
            seed: 42,
        }
    }
}

/// Build a kNN graph from PCA embeddings, storing connectivities and distances.
///
/// Reads `obsm["X_pca"]` and builds:
/// - `obsp["distances"]` — kNN distance matrix
/// - `obsp["connectivities"]` — UMAP-style fuzzy simplicial set connectivities
pub fn neighbors(adata: &mut AnnData, config: &NeighborsConfig) -> Result<()> {
    let pca = adata
        .get_obsm("X_pca")
        .ok_or_else(|| CyaneaError::InvalidInput("obsm['X_pca'] not found; run PCA first".into()))?
        .clone();

    let n_obs = pca.len();
    let n_dims = pca[0].len().min(config.n_pcs);

    if n_obs < 2 {
        return Err(CyaneaError::InvalidInput(
            "need at least 2 observations for neighbor graph".into(),
        ));
    }

    let k = config.n_neighbors.min(n_obs - 1);

    // Compute pairwise distances and find kNN
    let mut knn_indices = vec![Vec::new(); n_obs];
    let mut knn_distances = vec![Vec::new(); n_obs];

    for i in 0..n_obs {
        let mut dists: Vec<(usize, f64)> = (0..n_obs)
            .filter(|&j| j != i)
            .map(|j| {
                let d = compute_distance(&pca[i][..n_dims], &pca[j][..n_dims], config.metric);
                (j, d)
            })
            .collect();
        dists.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        knn_indices[i] = dists[..k].iter().map(|&(j, _)| j).collect();
        knn_distances[i] = dists[..k].iter().map(|&(_, d)| d).collect();
    }

    // Build distance sparse matrix
    let mut dist_matrix = SparseMatrix::new(n_obs, n_obs);
    for i in 0..n_obs {
        for (idx, &j) in knn_indices[i].iter().enumerate() {
            let _ = dist_matrix.insert(i, j, knn_distances[i][idx]);
        }
    }

    // Build UMAP-style fuzzy connectivities
    // For each point, compute sigma (bandwidth) such that perplexity ~ k
    let mut conn_matrix = SparseMatrix::new(n_obs, n_obs);
    for i in 0..n_obs {
        let dists = &knn_distances[i];
        if dists.is_empty() {
            continue;
        }
        let rho = dists[0]; // distance to nearest neighbor
        let sigma = smooth_knn_dist(dists, (k as f64).ln() / std::f64::consts::LN_2);

        for (idx, &j) in knn_indices[i].iter().enumerate() {
            let d = dists[idx];
            let w = if d <= rho {
                1.0
            } else {
                (-(d - rho) / sigma.max(1e-10)).exp()
            };
            let _ = conn_matrix.insert(i, j, w);
        }
    }

    // Symmetrize: w_sym = w_ij + w_ji - w_ij * w_ji
    let mut sym_conn = SparseMatrix::new(n_obs, n_obs);
    for i in 0..n_obs {
        for &j in &knn_indices[i] {
            let w_ij = conn_matrix.get(i, j);
            let w_ji = conn_matrix.get(j, i);
            let w_sym = w_ij + w_ji - w_ij * w_ji;
            if w_sym > 0.0 {
                let _ = sym_conn.insert(i, j, w_sym);
            }
        }
    }

    adata.add_obsp("distances", dist_matrix)?;
    adata.add_obsp("connectivities", sym_conn)?;
    adata.add_uns("neighbors_n_neighbors", config.n_neighbors.to_string());

    Ok(())
}

/// Binary search for sigma in smooth kNN distance kernel.
fn smooth_knn_dist(distances: &[f64], target: f64) -> f64 {
    let mut lo = 1e-10_f64;
    let mut hi = 1000.0_f64;

    for _ in 0..64 {
        let mid = (lo + hi) / 2.0;
        let rho = distances[0];
        let sum: f64 = distances
            .iter()
            .map(|&d| {
                if d <= rho {
                    1.0
                } else {
                    (-(d - rho) / mid).exp()
                }
            })
            .sum();
        if sum > target {
            hi = mid;
        } else {
            lo = mid;
        }
    }
    (lo + hi) / 2.0
}

fn compute_distance(a: &[f64], b: &[f64], metric: DistanceMetric) -> f64 {
    match metric {
        DistanceMetric::Euclidean => {
            a.iter()
                .zip(b.iter())
                .map(|(x, y)| (x - y).powi(2))
                .sum::<f64>()
                .sqrt()
        }
        DistanceMetric::Cosine => {
            let dot: f64 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
            let norm_a: f64 = a.iter().map(|x| x * x).sum::<f64>().sqrt();
            let norm_b: f64 = b.iter().map(|x| x * x).sum::<f64>().sqrt();
            let denom = norm_a * norm_b;
            if denom < 1e-15 {
                1.0
            } else {
                1.0 - dot / denom
            }
        }
    }
}

// ── Clustering ─────────────────────────────────────────────────────────────

/// Configuration for Leiden/Louvain clustering.
#[derive(Debug, Clone)]
pub struct ClusterConfig {
    /// Resolution parameter (higher = more clusters).
    pub resolution: f64,
    /// Maximum iterations for the algorithm.
    pub n_iterations: usize,
    /// Random seed.
    pub seed: u64,
    /// Key name to store cluster labels in obs.
    pub key_added: String,
}

impl Default for ClusterConfig {
    fn default() -> Self {
        Self {
            resolution: 1.0,
            n_iterations: 10,
            seed: 42,
            key_added: "leiden".into(),
        }
    }
}

/// Leiden community detection (Traag 2019).
///
/// Three-phase algorithm guaranteeing well-connected communities:
/// 1. Local moves (like Louvain Phase 1)
/// 2. Refinement: each community is refined by checking if nodes should form subcommunities
/// 3. Aggregation: contract graph, repeat
///
/// Stores cluster labels in `obs[key_added]`.
pub fn leiden(adata: &mut AnnData, config: &ClusterConfig) -> Result<()> {
    let conn = adata
        .get_obsp("connectivities")
        .ok_or_else(|| {
            CyaneaError::InvalidInput(
                "obsp['connectivities'] not found; run neighbors() first".into(),
            )
        })?
        .clone();

    let n = adata.n_obs();
    let graph = Graph::from_sparse_matrix(&conn);

    // Start with each node in its own community
    let mut assignments: Vec<usize> = (0..n).collect();

    // Total edge weight
    let total_weight: f64 = conn.iter().filter(|(i, j, _)| i < j).map(|(_, _, w)| w).sum();
    if total_weight == 0.0 {
        let labels: Vec<String> = (0..n).map(|i| i.to_string()).collect();
        adata.add_obs_column(&config.key_added, ColumnData::Strings(labels))?;
        return Ok(());
    }

    let m2 = total_weight * 2.0;

    for _iteration in 0..config.n_iterations {
        let mut moved = false;

        // Phase 1: local moves with resolution
        let mut local_improved = true;
        while local_improved {
            local_improved = false;

            // Use seeded deterministic order
            let mut order: Vec<usize> = (0..n).collect();
            let mut rng_state = config.seed.wrapping_add(_iteration as u64);
            for i in (1..n).rev() {
                rng_state = rng_state
                    .wrapping_mul(6364136223846793005)
                    .wrapping_add(1442695040888963407);
                let j = (rng_state >> 33) as usize % (i + 1);
                order.swap(i, j);
            }

            for &i in &order {
                let current = assignments[i];
                let k_i: f64 = graph.neighbors(i).iter().map(|(_, w)| w).sum();

                // Weights to each neighbor community
                let mut comm_weights: HashMap<usize, f64> = HashMap::new();
                for &(j, w) in graph.neighbors(i) {
                    *comm_weights.entry(assignments[j]).or_insert(0.0) += w;
                }

                let sigma_tot_current = community_weight(&graph, &assignments, current, n);
                let k_i_in_current = comm_weights.get(&current).copied().unwrap_or(0.0);

                let mut best = current;
                let mut best_dq = 0.0;

                for (&c, &k_i_in) in &comm_weights {
                    if c == current {
                        continue;
                    }
                    let sigma_tot = community_weight(&graph, &assignments, c, n);
                    let dq = (k_i_in - k_i_in_current) / m2
                        - config.resolution * k_i * (sigma_tot - sigma_tot_current + k_i)
                            / (m2 * m2)
                            * 2.0;
                    if dq > best_dq {
                        best_dq = dq;
                        best = c;
                    }
                }

                if best != current {
                    assignments[i] = best;
                    local_improved = true;
                    moved = true;
                }
            }
        }

        // Phase 2: refinement
        // Check if poorly-connected nodes should be moved to better-fitting communities
        let communities = unique_sorted(&assignments);
        for &c in &communities {
            let members: Vec<usize> = (0..n).filter(|&i| assignments[i] == c).collect();
            if members.len() <= 2 {
                continue;
            }

            for &node in &members {
                let k_i: f64 = graph.neighbors(node).iter().map(|(_, w)| w).sum();
                let k_i_in: f64 = graph
                    .neighbors(node)
                    .iter()
                    .filter(|&&(j, _)| assignments[j] == c)
                    .map(|(_, w)| w)
                    .sum();

                // Only consider extraction if weakly connected to own community
                if k_i > 0.0 && k_i_in / k_i < 0.1 {
                    let sigma_tot = community_weight(&graph, &assignments, c, n);
                    let dq_remove = -k_i_in / m2
                        + config.resolution * k_i * (sigma_tot - k_i) / (m2 * m2) * 2.0;

                    if dq_remove > 1e-6 {
                        let new_c = assignments.iter().max().unwrap_or(&0) + 1;
                        assignments[node] = new_c;
                        moved = true;
                    }
                }
            }
        }

        if !moved {
            break;
        }

        // Renumber communities
        renumber_assignments(&mut assignments);
    }

    renumber_assignments(&mut assignments);

    let labels: Vec<String> = assignments.iter().map(|c| c.to_string()).collect();
    adata.add_obs_column(&config.key_added, ColumnData::Strings(labels))?;

    let modularity = graph.modularity_with_resolution(&assignments, config.resolution);
    adata.add_uns(
        &format!("{}_modularity", config.key_added),
        modularity.to_string(),
    );

    Ok(())
}

/// Louvain clustering wrapper using the enhanced Graph method.
///
/// Stores cluster labels in `obs[key_added]`.
pub fn louvain(adata: &mut AnnData, config: &ClusterConfig) -> Result<()> {
    let conn = adata
        .get_obsp("connectivities")
        .ok_or_else(|| {
            CyaneaError::InvalidInput(
                "obsp['connectivities'] not found; run neighbors() first".into(),
            )
        })?
        .clone();

    let graph = Graph::from_sparse_matrix(&conn);
    let result = graph.louvain_with_resolution(config.resolution);

    let labels: Vec<String> = result.assignments.iter().map(|c| c.to_string()).collect();
    adata.add_obs_column(&config.key_added, ColumnData::Strings(labels))?;
    adata.add_uns(
        &format!("{}_modularity", config.key_added),
        result.modularity.to_string(),
    );

    Ok(())
}

fn community_weight(graph: &Graph, assignments: &[usize], community: usize, n: usize) -> f64 {
    let mut total = 0.0;
    for v in 0..n {
        if assignments[v] == community {
            for &(_, w) in graph.neighbors(v) {
                total += w;
            }
        }
    }
    total
}

fn unique_sorted(v: &[usize]) -> Vec<usize> {
    let mut u = v.to_vec();
    u.sort_unstable();
    u.dedup();
    u
}

fn renumber_assignments(assignments: &mut [usize]) {
    let mut map: Vec<usize> = Vec::new();
    for a in assignments.iter_mut() {
        let pos = map.iter().position(|&c| c == *a);
        *a = match pos {
            Some(idx) => idx,
            None => {
                map.push(*a);
                map.len() - 1
            }
        };
    }
}

// ── Clustering Metrics ─────────────────────────────────────────────────────

/// Normalized Mutual Information between two partitions.
pub fn nmi(a: &[usize], b: &[usize]) -> Result<f64> {
    if a.len() != b.len() {
        return Err(CyaneaError::InvalidInput(
            "partitions must have the same length".into(),
        ));
    }
    let n = a.len();
    if n == 0 {
        return Ok(0.0);
    }

    let n_a = *a.iter().max().unwrap_or(&0) + 1;
    let n_b = *b.iter().max().unwrap_or(&0) + 1;

    // Contingency table
    let mut contingency = vec![0usize; n_a * n_b];
    for i in 0..n {
        contingency[a[i] * n_b + b[i]] += 1;
    }

    // Marginals
    let mut row_sums = vec![0usize; n_a];
    let mut col_sums = vec![0usize; n_b];
    for i in 0..n_a {
        for j in 0..n_b {
            row_sums[i] += contingency[i * n_b + j];
            col_sums[j] += contingency[i * n_b + j];
        }
    }

    let nf = n as f64;

    // Mutual information
    let mut mi = 0.0;
    for i in 0..n_a {
        for j in 0..n_b {
            let nij = contingency[i * n_b + j] as f64;
            if nij > 0.0 {
                mi += nij / nf * (nf * nij / (row_sums[i] as f64 * col_sums[j] as f64)).ln();
            }
        }
    }

    // Entropies
    let h_a: f64 = row_sums
        .iter()
        .filter(|&&c| c > 0)
        .map(|&c| {
            let p = c as f64 / nf;
            -p * p.ln()
        })
        .sum();

    let h_b: f64 = col_sums
        .iter()
        .filter(|&&c| c > 0)
        .map(|&c| {
            let p = c as f64 / nf;
            -p * p.ln()
        })
        .sum();

    let denom = ((h_a + h_b) / 2.0).max(1e-15);
    Ok((mi / denom).clamp(0.0, 1.0))
}

/// Adjusted Rand Index between two partitions.
pub fn adjusted_rand_index(a: &[usize], b: &[usize]) -> Result<f64> {
    if a.len() != b.len() {
        return Err(CyaneaError::InvalidInput(
            "partitions must have the same length".into(),
        ));
    }
    let n = a.len();
    if n < 2 {
        return Ok(0.0);
    }

    let n_a = *a.iter().max().unwrap_or(&0) + 1;
    let n_b = *b.iter().max().unwrap_or(&0) + 1;

    // Contingency table
    let mut contingency = vec![0i64; n_a * n_b];
    for i in 0..n {
        contingency[a[i] * n_b + b[i]] += 1;
    }

    // Row and column sums
    let mut row_sums = vec![0i64; n_a];
    let mut col_sums = vec![0i64; n_b];
    for i in 0..n_a {
        for j in 0..n_b {
            row_sums[i] += contingency[i * n_b + j];
            col_sums[j] += contingency[i * n_b + j];
        }
    }

    // C(n,2) helper
    let c2 = |x: i64| -> i64 { x * (x - 1) / 2 };

    let sum_comb_c: i64 = contingency.iter().map(|&x| c2(x)).sum();
    let sum_comb_a: i64 = row_sums.iter().map(|&x| c2(x)).sum();
    let sum_comb_b: i64 = col_sums.iter().map(|&x| c2(x)).sum();
    let comb_n = c2(n as i64);

    let expected = sum_comb_a as f64 * sum_comb_b as f64 / comb_n as f64;
    let max_index = (sum_comb_a as f64 + sum_comb_b as f64) / 2.0;
    let denom = max_index - expected;

    if denom.abs() < 1e-15 {
        return Ok(0.0);
    }

    Ok((sum_comb_c as f64 - expected) / denom)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::single_cell::MatrixData;

    fn make_adata_with_pca(n_obs: usize, n_dims: usize, data: Vec<Vec<f64>>) -> AnnData {
        let x = MatrixData::Dense(vec![vec![0.0; 2]; n_obs]);
        let obs_names: Vec<String> = (0..n_obs).map(|i| format!("cell_{}", i)).collect();
        let var_names = vec!["g0".into(), "g1".into()];
        let mut adata = AnnData::new(x, obs_names, var_names).unwrap();
        adata.add_obsm("X_pca", data).unwrap();
        adata
    }

    // ── Neighbors tests ──

    #[test]
    fn neighbors_basic() {
        // 6 points in 2D, two clusters
        let pca = vec![
            vec![0.0, 0.0],
            vec![0.1, 0.1],
            vec![0.2, 0.0],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
            vec![10.2, 10.0],
        ];
        let mut adata = make_adata_with_pca(6, 2, pca);
        neighbors(
            &mut adata,
            &NeighborsConfig {
                n_neighbors: 3,
                ..Default::default()
            },
        )
        .unwrap();

        assert!(adata.get_obsp("distances").is_some());
        assert!(adata.get_obsp("connectivities").is_some());

        // Points 0,1,2 should be close to each other
        let dist = adata.get_obsp("distances").unwrap();
        let d01 = dist.get(0, 1);
        let d03 = dist.get(0, 3);
        // d01 should be stored (neighbor), d03 might not be (too far)
        if d03 > 0.0 {
            assert!(d01 < d03);
        }
    }

    #[test]
    fn neighbors_missing_pca() {
        let x = MatrixData::Dense(vec![vec![1.0, 2.0]]);
        let mut adata =
            AnnData::new(x, vec!["c0".into()], vec!["g0".into(), "g1".into()]).unwrap();
        let result = neighbors(&mut adata, &NeighborsConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn neighbors_cosine_metric() {
        let pca = vec![
            vec![1.0, 0.0],
            vec![0.0, 1.0],
            vec![1.0, 1.0],
        ];
        let mut adata = make_adata_with_pca(3, 2, pca);
        neighbors(
            &mut adata,
            &NeighborsConfig {
                n_neighbors: 2,
                metric: DistanceMetric::Cosine,
                ..Default::default()
            },
        )
        .unwrap();
        assert!(adata.get_obsp("connectivities").is_some());
    }

    // ── Leiden tests ──

    #[test]
    fn leiden_two_clusters() {
        // Build connectivity matrix for two well-separated clusters
        let n = 10;
        let mut conn = SparseMatrix::new(n, n);
        // Cluster 1: nodes 0-4
        for i in 0..5 {
            for j in (i + 1)..5 {
                let _ = conn.insert(i, j, 1.0);
                let _ = conn.insert(j, i, 1.0);
            }
        }
        // Cluster 2: nodes 5-9
        for i in 5..10 {
            for j in (i + 1)..10 {
                let _ = conn.insert(i, j, 1.0);
                let _ = conn.insert(j, i, 1.0);
            }
        }
        // Weak bridge
        let _ = conn.insert(4, 5, 0.01);
        let _ = conn.insert(5, 4, 0.01);

        let x = MatrixData::Dense(vec![vec![0.0; 2]; n]);
        let obs_names: Vec<String> = (0..n).map(|i| format!("c{}", i)).collect();
        let var_names = vec!["g0".into(), "g1".into()];
        let mut adata = AnnData::new(x, obs_names, var_names).unwrap();
        adata.add_obsp("connectivities", conn).unwrap();

        leiden(&mut adata, &ClusterConfig::default()).unwrap();

        let labels = adata.get_obs("leiden").unwrap().as_strings().unwrap();
        // Nodes in same cluster should have same label
        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[0], labels[2]);
        assert_eq!(labels[5], labels[6]);
        assert_eq!(labels[5], labels[7]);
        // Different clusters
        assert_ne!(labels[0], labels[5]);
    }

    #[test]
    fn leiden_missing_connectivity() {
        let x = MatrixData::Dense(vec![vec![1.0]]);
        let mut adata = AnnData::new(x, vec!["c0".into()], vec!["g0".into()]).unwrap();
        let result = leiden(&mut adata, &ClusterConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn leiden_custom_key() {
        let n = 4;
        let mut conn = SparseMatrix::new(n, n);
        for i in 0..n {
            for j in (i + 1)..n {
                let _ = conn.insert(i, j, 1.0);
                let _ = conn.insert(j, i, 1.0);
            }
        }
        let x = MatrixData::Dense(vec![vec![0.0; 2]; n]);
        let obs_names: Vec<String> = (0..n).map(|i| format!("c{}", i)).collect();
        let mut adata = AnnData::new(x, obs_names, vec!["g0".into(), "g1".into()]).unwrap();
        adata.add_obsp("connectivities", conn).unwrap();

        leiden(
            &mut adata,
            &ClusterConfig {
                key_added: "my_clusters".into(),
                ..Default::default()
            },
        )
        .unwrap();

        assert!(adata.get_obs("my_clusters").is_some());
    }

    #[test]
    fn leiden_high_resolution() {
        let n = 8;
        let mut conn = SparseMatrix::new(n, n);
        for i in 0..n {
            for j in (i + 1)..n {
                let _ = conn.insert(i, j, 1.0);
                let _ = conn.insert(j, i, 1.0);
            }
        }
        let x = MatrixData::Dense(vec![vec![0.0; 2]; n]);
        let obs_names: Vec<String> = (0..n).map(|i| format!("c{}", i)).collect();
        let mut adata = AnnData::new(x, obs_names, vec!["g0".into(), "g1".into()]).unwrap();
        adata.add_obsp("connectivities", conn).unwrap();

        leiden(
            &mut adata,
            &ClusterConfig {
                resolution: 5.0,
                ..Default::default()
            },
        )
        .unwrap();

        let labels = adata.get_obs("leiden").unwrap().as_strings().unwrap();
        let n_clusters: usize = {
            let mut u: Vec<&String> = labels.iter().collect();
            u.sort();
            u.dedup();
            u.len()
        };
        // High resolution should produce more clusters
        assert!(n_clusters >= 1);
    }

    // ── Louvain wrapper tests ──

    #[test]
    fn louvain_wrapper_basic() {
        let n = 6;
        let mut conn = SparseMatrix::new(n, n);
        for i in 0..3 {
            for j in (i + 1)..3 {
                let _ = conn.insert(i, j, 1.0);
                let _ = conn.insert(j, i, 1.0);
            }
        }
        for i in 3..6 {
            for j in (i + 1)..6 {
                let _ = conn.insert(i, j, 1.0);
                let _ = conn.insert(j, i, 1.0);
            }
        }
        let _ = conn.insert(2, 3, 0.01);
        let _ = conn.insert(3, 2, 0.01);

        let x = MatrixData::Dense(vec![vec![0.0; 2]; n]);
        let obs_names: Vec<String> = (0..n).map(|i| format!("c{}", i)).collect();
        let mut adata = AnnData::new(x, obs_names, vec!["g0".into(), "g1".into()]).unwrap();
        adata.add_obsp("connectivities", conn).unwrap();

        louvain(
            &mut adata,
            &ClusterConfig {
                key_added: "louvain".into(),
                ..Default::default()
            },
        )
        .unwrap();

        let labels = adata.get_obs("louvain").unwrap().as_strings().unwrap();
        assert_eq!(labels[0], labels[1]);
        assert_ne!(labels[0], labels[3]);
    }

    // ── NMI tests ──

    #[test]
    fn nmi_identical() {
        let a = vec![0, 0, 1, 1, 2, 2];
        let b = vec![0, 0, 1, 1, 2, 2];
        let score = nmi(&a, &b).unwrap();
        assert!((score - 1.0).abs() < 1e-10);
    }

    #[test]
    fn nmi_independent() {
        // One partition: all same, other: all different
        let a = vec![0, 0, 0, 0];
        let b = vec![0, 1, 2, 3];
        let score = nmi(&a, &b).unwrap();
        assert!(score.abs() < 1e-10);
    }

    #[test]
    fn nmi_different_lengths() {
        let result = nmi(&[0, 1], &[0, 1, 2]);
        assert!(result.is_err());
    }

    #[test]
    fn nmi_empty() {
        let score = nmi(&[], &[]).unwrap();
        assert_eq!(score, 0.0);
    }

    #[test]
    fn nmi_permuted() {
        let a = vec![0, 0, 1, 1, 2, 2];
        let b = vec![2, 2, 0, 0, 1, 1]; // same partition, different labels
        let score = nmi(&a, &b).unwrap();
        assert!((score - 1.0).abs() < 1e-10);
    }

    // ── ARI tests ──

    #[test]
    fn ari_identical() {
        let a = vec![0, 0, 1, 1, 2, 2];
        let b = vec![0, 0, 1, 1, 2, 2];
        let score = adjusted_rand_index(&a, &b).unwrap();
        assert!((score - 1.0).abs() < 1e-10);
    }

    #[test]
    fn ari_random() {
        // Random assignment → ARI should be near 0
        let a = vec![0, 1, 0, 1, 0, 1, 0, 1];
        let b = vec![0, 0, 1, 1, 0, 0, 1, 1];
        let score = adjusted_rand_index(&a, &b).unwrap();
        assert!(score.abs() < 0.5); // should be close to 0
    }

    #[test]
    fn ari_permuted() {
        let a = vec![0, 0, 0, 1, 1, 1];
        let b = vec![1, 1, 1, 0, 0, 0]; // same partition, swapped labels
        let score = adjusted_rand_index(&a, &b).unwrap();
        assert!((score - 1.0).abs() < 1e-10);
    }

    #[test]
    fn ari_different_lengths() {
        let result = adjusted_rand_index(&[0, 1], &[0]);
        assert!(result.is_err());
    }

    #[test]
    fn ari_single_element() {
        let score = adjusted_rand_index(&[0], &[0]).unwrap();
        assert_eq!(score, 0.0);
    }

    // ── Distance tests ──

    #[test]
    fn euclidean_distance() {
        let d = compute_distance(&[0.0, 0.0], &[3.0, 4.0], DistanceMetric::Euclidean);
        assert!((d - 5.0).abs() < 1e-10);
    }

    #[test]
    fn cosine_distance_orthogonal() {
        let d = compute_distance(&[1.0, 0.0], &[0.0, 1.0], DistanceMetric::Cosine);
        assert!((d - 1.0).abs() < 1e-10);
    }

    #[test]
    fn cosine_distance_identical() {
        let d = compute_distance(&[1.0, 2.0], &[2.0, 4.0], DistanceMetric::Cosine);
        assert!(d.abs() < 1e-10);
    }

    // ── Integration test: neighbors → leiden pipeline ──

    #[test]
    fn neighbors_then_leiden() {
        let pca = vec![
            vec![0.0, 0.0],
            vec![0.1, 0.1],
            vec![0.05, 0.05],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
            vec![10.05, 10.05],
        ];
        let mut adata = make_adata_with_pca(6, 2, pca);
        neighbors(
            &mut adata,
            &NeighborsConfig {
                n_neighbors: 3,
                ..Default::default()
            },
        )
        .unwrap();
        leiden(&mut adata, &ClusterConfig::default()).unwrap();

        let labels = adata.get_obs("leiden").unwrap().as_strings().unwrap();
        // Points 0,1,2 should cluster together
        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[1], labels[2]);
        // Points 3,4,5 should cluster together
        assert_eq!(labels[3], labels[4]);
        assert_eq!(labels[4], labels[5]);
        // Two clusters should be different
        assert_ne!(labels[0], labels[3]);
    }

    #[test]
    fn neighbors_then_louvain() {
        let pca = vec![
            vec![0.0, 0.0],
            vec![0.1, 0.1],
            vec![0.05, 0.05],
            vec![10.0, 10.0],
            vec![10.1, 10.1],
            vec![10.05, 10.05],
        ];
        let mut adata = make_adata_with_pca(6, 2, pca);
        neighbors(
            &mut adata,
            &NeighborsConfig {
                n_neighbors: 3,
                ..Default::default()
            },
        )
        .unwrap();
        louvain(
            &mut adata,
            &ClusterConfig {
                key_added: "louvain".into(),
                ..Default::default()
            },
        )
        .unwrap();

        let labels = adata.get_obs("louvain").unwrap().as_strings().unwrap();
        assert_eq!(labels[0], labels[1]);
        assert_ne!(labels[0], labels[3]);
    }
}