data-beans 0.6.12

Sparse genomics data backends, QC, algorithms, and simulation
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
#![allow(dead_code)]

use crate::alg::batch_mixing::*;
use crate::sparse_data_visitors::*;
use crate::sparse_io_stack::SparseIoStack;
use crate::sparse_io_vector::SparseIoVec;
use legume_numeric::matrix::dmatrix_util::*;
use legume_numeric::matrix::rand_util::mix_seed;
use std::sync::{Arc, Mutex};

use legume_numeric::matrix::traits::*;
use log::{info, warn};
use nalgebra::DVector;

/// Bundle passed as `SharedIn` so the visitor signature stays compatible
/// with `visit_columns_by_block`. The basis is stored as `K × D`
/// (transposed) so the inner kernel accesses contiguous K-vectors per
/// non-zero, instead of stride-D reads across `D × K` column-major
/// storage.
struct ProjVisitorIn<'a> {
    basis_kd: &'a nalgebra::DMatrix<f32>,
}

pub struct RandColProjOut {
    pub basis: nalgebra::DMatrix<f32>,
    pub proj: nalgebra::DMatrix<f32>,
}

pub struct RandRowProjOut {
    pub basis: nalgebra::DMatrix<f32>,
    pub proj: nalgebra::DMatrix<f32>,
}

/// Default random-basis seed for the non-seeded projection entry points.
///
/// The random-projection basis only needs to be *stable* (a fixed
/// Johnson–Lindenstrauss draw is still a valid projection), so callers that do
/// not thread their own `--seed` get a pinned, reproducible basis for free.
/// Callers that want `--seed` to also drive the projection use the `_seeded`
/// variants (e.g. the shared `graph_embedding_util::fit` gem/bge path).
pub const DEFAULT_PROJECTION_SEED: u64 = 0x50524F4A_50524F4A; // "PROJPROJ"

pub trait RandProjOps {
    ///
    /// Create K x ncol projection by concatenating data across
    /// columns and returns the random basis matrix `D` x `target_dim`
    /// and the projection result `target_dim` x `N`
    ///
    /// # Arguments
    /// * `target_dim`: target dimensionality
    /// * `block_size`: block size for parallel computation
    ///
    /// # Output
    /// * `basis`: `D x K` random basis matrix
    /// * `proj`: `K x N` projection results
    ///
    /// Uses [`DEFAULT_PROJECTION_SEED`] for the basis; for a caller-controlled
    /// seed see [`RandProjOps::project_columns_with_batch_correction_seeded`].
    fn project_columns(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
    ) -> anyhow::Result<RandColProjOut> {
        self.project_columns_with_batch_correction::<usize>(target_dim, block_size, None)
    }

    ///
    /// Create K x ncol projection by concatenating data across
    /// columns and returns the random basis matrix `D` x `target_dim`
    /// and the projection result `target_dim` x `N`
    ///
    /// # Arguments
    /// * `target_dim`: target dimensionality
    /// * `block_size`: block size for parallel computation
    ///
    /// # Output
    /// * `basis`: `D x K` random basis matrix
    /// * `proj`: `K x N` projection results
    ///
    /// Uses [`DEFAULT_PROJECTION_SEED`]; delegates to
    /// [`RandProjOps::project_columns_with_batch_correction_seeded`].
    fn project_columns_with_batch_correction<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString,
    {
        self.project_columns_with_batch_correction_seeded(
            target_dim,
            block_size,
            batch_membership,
            DEFAULT_PROJECTION_SEED,
        )
    }

    /// Seeded variant of [`RandProjOps::project_columns_with_batch_correction`]:
    /// `seed` pins the random basis so identical-config runs are byte-identical.
    fn project_columns_with_batch_correction_seeded<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        seed: u64,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString;

    /// Like `project_columns_with_batch_correction` but with per-row
    /// (per-feature) weights applied to the random basis. Features with
    /// weight 0 are excluded from the projection geometry.
    ///
    /// Uses [`DEFAULT_PROJECTION_SEED`]; delegates to
    /// [`RandProjOps::project_columns_weighted_seeded`].
    fn project_columns_weighted<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        row_weights: &[f32],
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString,
    {
        self.project_columns_weighted_seeded(
            target_dim,
            block_size,
            batch_membership,
            row_weights,
            DEFAULT_PROJECTION_SEED,
        )
    }

    /// Seeded variant of [`RandProjOps::project_columns_weighted`]: `seed` pins
    /// the random basis so identical-config runs are byte-identical.
    fn project_columns_weighted_seeded<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        row_weights: &[f32],
        seed: u64,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString;

    /// Assign each column/cell to random group by binary encoding
    ///
    /// # Arguments
    ///
    /// * `proj_kn` - random projection matrix (feature x column/cell)
    ///
    /// * `num_features` - number of top features (if None, use all of them)
    ///
    fn partition_columns_to_groups(
        &mut self,
        proj_kn: &nalgebra::DMatrix<f32>,
        num_features: Option<usize>,
        ncols_per_group: Option<usize>,
    ) -> anyhow::Result<usize>;

    /// Like [`RandProjOps::partition_columns_to_groups`], but bins that hold
    /// fewer than `min_batches` batches merge up the code tree, for at most
    /// `merge_levels` levels (see [`crate::alg::batch_mixing`]).
    /// `min_batches` is capped by the number of batches. Returns the number
    /// of groups.
    fn partition_columns_to_mixed_groups<T>(
        &mut self,
        proj_kn: &nalgebra::DMatrix<f32>,
        num_features: Option<usize>,
        batch_membership: &[T],
        min_batches: usize,
        merge_levels: usize,
    ) -> anyhow::Result<usize>
    where
        T: std::hash::Hash + Eq + Clone,
    {
        let part = mixed_partition(
            proj_kn,
            num_features,
            batch_membership,
            min_batches,
            merge_levels,
        )?;
        self.assign_group_labels(&part.labels);
        Ok(part.num_groups)
    }

    /// Assign each column to the group of its label.
    fn assign_group_labels(&mut self, labels: &[usize]);

    // Take samples assigned
    // fn groups_assigned(&self) -> anyhow::Result<&Vec<usize>>;
}

/// column-wise visitor for random projection
fn project_columns_visitor(
    job: (usize, usize),
    data_vec: &SparseIoVec,
    shared_in: &ProjVisitorIn<'_>,
    arc_proj_kn: Arc<Mutex<&mut nalgebra::DMatrix<f32>>>,
) -> anyhow::Result<()> {
    let (lb, ub) = job;
    let basis_kd = shared_in.basis_kd;
    let target_dim = basis_kd.nrows();
    let n_block = ub - lb;

    let mut xx_dm = data_vec.read_columns_csc(lb..ub)?;
    for x in xx_dm.values_mut() {
        *x = x.ln_1p();
    }
    xx_dm.normalize_columns_inplace();

    // Direct K×N kernel: chunk[:, j] = Σᵢ X[i,j] · B_kd[:, i]. One pass
    // over CSC non-zeros. Both column slices are unit-stride (contiguous),
    // so the SAXPY vectorizes; no sparse transpose, no dense transpose.
    let mut chunk = nalgebra::DMatrix::<f32>::zeros(target_dim, n_block);
    for (j, col) in xx_dm.col_iter().enumerate() {
        for (&i, &v) in col.row_indices().iter().zip(col.values()) {
            chunk.column_mut(j).axpy(v, &basis_kd.column(i), 1.0);
        }
    }

    let mut proj_kn = arc_proj_kn.lock().expect("proj_kn lock");
    proj_kn.columns_range_mut(lb..ub).copy_from(&chunk);
    Ok(())
}

impl RandProjOps for SparseIoStack {
    fn project_columns_with_batch_correction_seeded<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        seed: u64,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString,
    {
        let ncols = self.num_columns()?;
        let batch_membership = batch_membership.and_then(|x| x.get(0..ncols));

        let proj_vec = self
            .stack
            .iter()
            .enumerate()
            .map(|(m, data_vec)| -> anyhow::Result<_> {
                // Distinct sub-seed per stacked modality so same-sized modalities
                // do not share a basis.
                data_vec.project_columns_with_batch_correction_seeded(
                    target_dim,
                    block_size,
                    batch_membership,
                    mix_seed(seed, m as u64),
                )
            })
            .collect::<anyhow::Result<Vec<_>>>()?;

        let basis = concatenate_vertical(
            &proj_vec
                .iter()
                .map(|x| {
                    // info!("{} x {}", x.basis.nrows(), x.basis.ncols());
                    x.basis.to_owned()
                })
                .collect::<Vec<_>>(),
        )?;

        let proj = concatenate_vertical(
            &proj_vec
                .iter()
                .map(|x| {
                    // info!("{} x {}", x.proj.nrows(), x.proj.ncols());
                    x.proj.to_owned()
                })
                .collect::<Vec<_>>(),
        )?;

        Ok(RandColProjOut { basis, proj })
    }

    fn project_columns_weighted_seeded<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        row_weights: &[f32],
        seed: u64,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString,
    {
        let ncols = self.num_columns()?;
        let batch_membership = batch_membership.and_then(|x| x.get(0..ncols));

        // Interpret `row_weights` as the concatenated feature axis across
        // all stacked modalities, in `self.stack` order. Slice per
        // modality so each per-vec call sees weights of length equal to
        // its own `num_rows()`.
        let total_rows: usize = self.stack.iter().map(|v| v.num_rows()).sum();
        if row_weights.len() != total_rows {
            return Err(anyhow::anyhow!(
                "row_weights length {} does not match total feature count {} \
                 (sum of num_rows() across the stack)",
                row_weights.len(),
                total_rows
            ));
        }

        let mut offset = 0usize;
        let mut proj_vec = Vec::with_capacity(self.stack.len());
        for (m, data_vec) in self.stack.iter().enumerate() {
            let nrows = data_vec.num_rows();
            let slice = &row_weights[offset..offset + nrows];
            offset += nrows;
            proj_vec.push(data_vec.project_columns_weighted_seeded(
                target_dim,
                block_size,
                batch_membership,
                slice,
                mix_seed(seed, m as u64),
            )?);
        }

        let basis = concatenate_vertical(
            &proj_vec
                .iter()
                .map(|x| x.basis.to_owned())
                .collect::<Vec<_>>(),
        )?;
        let proj = concatenate_vertical(
            &proj_vec
                .iter()
                .map(|x| x.proj.to_owned())
                .collect::<Vec<_>>(),
        )?;

        Ok(RandColProjOut { basis, proj })
    }

    fn partition_columns_to_groups(
        &mut self,
        proj_kn: &nalgebra::DMatrix<f32>,
        num_sorting_features: Option<usize>,
        ncols_per_group: Option<usize>,
    ) -> anyhow::Result<usize> {
        let nn = proj_kn.ncols();
        if nn != self.num_columns()? {
            return Err(anyhow::anyhow!("number of columns mismatch"));
        }

        let target_kk = num_sorting_features.unwrap_or(proj_kn.nrows());
        let kk = proj_kn.nrows().min(target_kk).min(nn);

        let binary_codes = binary_sort_columns(proj_kn, kk)?;
        let max_group = *binary_codes
            .iter()
            .max()
            .ok_or(anyhow::anyhow!("unable to determine max element"))?;

        for x in self.stack.iter_mut() {
            x.assign_groups(&binary_codes, ncols_per_group);
        }

        Ok(max_group + 1)
    }

    fn assign_group_labels(&mut self, labels: &[usize]) {
        for x in self.stack.iter_mut() {
            x.assign_groups(labels, None);
        }
    }
}

impl RandProjOps for SparseIoVec {
    fn project_columns_with_batch_correction_seeded<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        seed: u64,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString,
    {
        let nrows = self.num_rows();
        let ncols = self.num_columns();

        let mut proj_kn = nalgebra::DMatrix::<f32>::zeros(target_dim, ncols);

        let basis_dk = nalgebra::DMatrix::<f32>::rnorm_seeded(nrows, target_dim, seed);
        // Carry a K×D copy for the inner kernel so per-non-zero column
        // slices into the basis are contiguous (unit-stride SAXPY).
        let basis_kd = basis_dk.transpose();

        let shared_in = ProjVisitorIn {
            basis_kd: &basis_kd,
        };
        info!(
            "starting random projection loop (project_columns_with_batch_correction): \
             D={nrows}, N={ncols}, K={target_dim}, block_size={:?}",
            block_size
        );
        self.visit_columns_by_block(
            &project_columns_visitor,
            &shared_in,
            &mut proj_kn,
            block_size,
        )?;
        info!("finished random projection loop");

        if let Some(col_to_batch) = batch_membership {
            adjust_batch_biases(&mut proj_kn, col_to_batch)?;
        }

        let (lb, ub) = (-4., 4.);
        proj_kn.scale_columns_inplace();

        if proj_kn.max() > ub || proj_kn.min() < lb {
            // info!("Clamping values [{}, {}] after standardization", lb, ub);
            proj_kn.iter_mut().for_each(|x| {
                *x = x.clamp(lb, ub);
            });
            proj_kn.scale_columns_inplace();
        }
        Ok(RandColProjOut {
            basis: basis_dk,
            proj: proj_kn,
        })
    }

    /// Like `project_columns_with_batch_correction` but with per-row
    /// (per-feature) weights applied to the random basis. Features with
    /// weight 0 are excluded from the projection geometry.
    fn project_columns_weighted_seeded<T>(
        &self,
        target_dim: usize,
        block_size: Option<usize>,
        batch_membership: Option<&[T]>,
        row_weights: &[f32],
        seed: u64,
    ) -> anyhow::Result<RandColProjOut>
    where
        T: Sync + Send + std::hash::Hash + Eq + Clone + ToString,
    {
        let nrows = self.num_rows();
        let ncols = self.num_columns();

        assert_eq!(row_weights.len(), nrows, "row_weights length mismatch");

        let mut proj_kn = nalgebra::DMatrix::<f32>::zeros(target_dim, ncols);

        let mut basis_dk = nalgebra::DMatrix::<f32>::rnorm_seeded(nrows, target_dim, seed);

        // Zero out basis rows for filtered features
        for (r, &w) in row_weights.iter().enumerate() {
            if w <= 0.0 {
                basis_dk.row_mut(r).fill(0.0);
            } else if (w - 1.0).abs() > 1e-6 {
                basis_dk.row_mut(r).scale_mut(w);
            }
        }

        let basis_kd = basis_dk.transpose();
        let shared_in = ProjVisitorIn {
            basis_kd: &basis_kd,
        };
        info!(
            "starting random projection loop (project_columns_weighted): \
             D={nrows}, N={ncols}, K={target_dim}, block_size={:?}",
            block_size
        );
        self.visit_columns_by_block(
            &project_columns_visitor,
            &shared_in,
            &mut proj_kn,
            block_size,
        )?;
        info!("finished random projection loop");

        if let Some(col_to_batch) = batch_membership {
            adjust_batch_biases(&mut proj_kn, col_to_batch)?;
        }

        let (lb, ub) = (-4., 4.);
        proj_kn.scale_columns_inplace();
        if proj_kn.max() > ub || proj_kn.min() < lb {
            proj_kn.iter_mut().for_each(|x| {
                *x = x.clamp(lb, ub);
            });
            proj_kn.scale_columns_inplace();
        }
        Ok(RandColProjOut {
            basis: basis_dk,
            proj: proj_kn,
        })
    }

    /// Assign each column/cell to a sample by sorting through binary
    /// encoding
    ///
    /// # Arguments
    ///
    /// * `proj_kn` - random projection matrix (feature x column/cell)
    ///
    /// * `num_features` - number of top features (if None, use all of them)
    ///
    fn partition_columns_to_groups(
        &mut self,
        proj_kn: &nalgebra::DMatrix<f32>,
        num_sorting_features: Option<usize>,
        ncols_per_group: Option<usize>,
    ) -> anyhow::Result<usize> {
        let nn = proj_kn.ncols();
        if nn != self.num_columns() {
            return Err(anyhow::anyhow!("number of columns mismatch"));
        }

        let target_kk = num_sorting_features.unwrap_or(proj_kn.nrows());
        let kk = proj_kn.nrows().min(target_kk).min(nn);

        let binary_codes = binary_sort_columns(proj_kn, kk)?;
        let max_group = *binary_codes
            .iter()
            .max()
            .ok_or(anyhow::anyhow!("unable to determine max element"))?;
        self.assign_groups(&binary_codes, ncols_per_group);
        Ok(max_group + 1)
    }

    fn assign_group_labels(&mut self, labels: &[usize]) {
        self.assign_groups(labels, None);
    }
}

/// Remove batch shifts from a projection, keeping each batch's cell-state
/// composition: batches are centred within cell state (see
/// [`crate::alg::batch_mixing`]), not by their plain means. A membership of
/// the wrong length is ignored with a warning.
fn adjust_batch_biases<T>(
    proj_kn: &mut nalgebra::DMatrix<f32>,
    col_to_batch: &[T],
) -> anyhow::Result<()>
where
    T: std::hash::Hash + Eq + Clone,
{
    let ncols = proj_kn.ncols();
    if col_to_batch.len() != ncols {
        warn!(
            "The batch membership size {} mismatches with the number of columns {} ...",
            col_to_batch.len(),
            ncols
        );
        return Ok(());
    }
    let batch = batch_indices(col_to_batch);
    let n_batches = batch.iter().max().map_or(0, |&m| m + 1);
    let bits = default_state_bits(ncols, n_batches, proj_kn.nrows());
    info!(
        "adjusting batch biases within cell state ({} batches, {} state bits) ...",
        n_batches, bits
    );
    *proj_kn = centre_batches_within_state(proj_kn, &batch, bits)?;
    Ok(())
}

/// The mixed partition of [`RandProjOps::partition_columns_to_mixed_groups`]
/// before it is assigned: codes, merged labels and batches of the columns.
pub struct MixedPartition {
    /// binary code of each column
    pub codes: Vec<usize>,
    /// merged group label of each column
    pub labels: Vec<usize>,
    pub num_groups: usize,
    /// bits of the codes
    pub bits: usize,
    /// batch of each column, in `0..n_batches`
    pub batch: Vec<usize>,
    /// batches a group needs, capped by the number of batches
    pub min_batches: usize,
}

/// Bin columns by the sign codes of `proj_kn` and merge bins holding fewer
/// than `min_batches` batches up the code tree, for at most `merge_levels`
/// levels.
pub fn mixed_partition<T>(
    proj_kn: &nalgebra::DMatrix<f32>,
    num_features: Option<usize>,
    batch_membership: &[T],
    min_batches: usize,
    merge_levels: usize,
) -> anyhow::Result<MixedPartition>
where
    T: std::hash::Hash + Eq + Clone,
{
    let nn = proj_kn.ncols();
    anyhow::ensure!(
        batch_membership.len() == nn,
        "batch membership size {} mismatches the number of columns {}",
        batch_membership.len(),
        nn
    );
    let batch = batch_indices(batch_membership);
    let n_batches = batch.iter().max().map_or(0, |&m| m + 1);
    let min_batches = min_batches.min(n_batches);
    let bits = proj_kn
        .nrows()
        .min(num_features.unwrap_or(proj_kn.nrows()))
        .min(nn);
    let codes = binary_sort_columns(proj_kn, bits)?;
    let labels = merge_poorly_mixed_bins(&codes, &batch, bits, merge_levels, min_batches);
    let num_groups = labels
        .iter()
        .collect::<std::collections::HashSet<_>>()
        .len();
    info!(
        "partitioned columns into {} groups ({} bits, bins with fewer than {} batches merged up to {} levels)",
        num_groups, bits, min_batches, merge_levels
    );
    Ok(MixedPartition {
        codes,
        labels,
        num_groups,
        bits,
        batch,
        min_batches,
    })
}

/// Binarize the projection matrix and assign columns to some groups
///
/// # Arguments
/// * `proj_kn` - random projection matrix (feature x column/cell)
/// * `kk` - number of features
pub fn binary_sort_columns(
    proj_kn: &nalgebra::DMatrix<f32>,
    kk: usize,
) -> anyhow::Result<Vec<usize>> {
    // // standardization again to make sure that each coordinates is treated equally
    // let proj_nk = proj_kn.transpose().scale_columns();
    // let nn = proj_kn.ncols();
    // // SVD to spread out the points
    // let (mut q_nk, _, _) = proj_nk.rsvd(kk)?;
    // q_nk.scale_columns_inplace();

    // SVD to spread out the points
    let nn = proj_kn.ncols();
    let (_, _, mut q_nk) = proj_kn.rsvd(kk)?;
    q_nk.scale_columns_inplace();

    let mut binary_codes = DVector::<usize>::zeros(nn);
    for k in 0..kk {
        let binary_shift = |x: f32| -> usize {
            if x > 0.0 {
                1 << k
            } else {
                0
            }
        };
        binary_codes += q_nk.column(k).map(binary_shift);
    }

    Ok(binary_codes.data.as_vec().clone())
}

/// maximum binary code (binary shifting)
pub fn max_binary_code(kk: usize) -> usize {
    (1 << kk) - 1
}

#[cfg(test)]
mod repro_tests {
    use super::*;
    use crate::sparse_io::{create_sparse_from_triplets, SparseIoBackend};
    use std::sync::Arc;

    /// Small 8-gene × 12-cell `SparseIoVec` for exercising the projection.
    fn tiny_data_vec(dir: &tempfile::TempDir) -> anyhow::Result<SparseIoVec> {
        let path = dir.path().join("proj.zarr");
        let path_str = path.to_str().unwrap();
        let (d, n) = (8usize, 12usize);
        // Deterministic sparse pattern (nonzero where (i*7 + j*3) % 5 < 3).
        let mut triplets: Vec<(u64, u64, f32)> = Vec::new();
        for i in 0..d {
            for j in 0..n {
                if (i * 7 + j * 3) % 5 < 3 {
                    triplets.push((i as u64, j as u64, 1.0 + ((i + j) % 4) as f32));
                }
            }
        }
        let nnz = triplets.len();
        let mut backend = create_sparse_from_triplets(
            &triplets,
            (d, n, nnz),
            Some(path_str),
            Some(&SparseIoBackend::Zarr),
        )?;
        let gene_names: Vec<Box<str>> = (0..d).map(|i| format!("g{i}").into()).collect();
        let cell_names: Vec<Box<str>> = (0..n).map(|j| format!("c{j}").into()).collect();
        backend.register_row_names_vec(&gene_names);
        backend.register_column_names_vec(&cell_names);
        let mut data_vec = SparseIoVec::new();
        data_vec.push(Arc::from(backend), None)?;
        Ok(data_vec)
    }

    /// The seeded projection must be byte-identical across runs for a fixed seed
    /// — this is the reproducibility guarantee the gem/bge pipeline relies on.
    #[test]
    fn seeded_projection_is_reproducible() -> anyhow::Result<()> {
        let dir = tempfile::tempdir()?;
        let data_vec = tiny_data_vec(&dir)?;
        let a =
            data_vec.project_columns_with_batch_correction_seeded::<usize>(4, None, None, 123)?;
        let b =
            data_vec.project_columns_with_batch_correction_seeded::<usize>(4, None, None, 123)?;
        assert_eq!(a.basis, b.basis, "same seed → identical basis");
        assert_eq!(a.proj, b.proj, "same seed → identical projection");
        Ok(())
    }

    /// The non-seeded entry point must also be reproducible now (it pins
    /// `DEFAULT_PROJECTION_SEED` internally rather than drawing fresh entropy).
    #[test]
    fn default_projection_is_reproducible() -> anyhow::Result<()> {
        let dir = tempfile::tempdir()?;
        let data_vec = tiny_data_vec(&dir)?;
        let a = data_vec.project_columns(4, None)?;
        let b = data_vec.project_columns(4, None)?;
        assert_eq!(a.basis, b.basis);
        assert_eq!(a.proj, b.proj);
        Ok(())
    }

    /// Distinct seeds must yield distinct bases (the seed genuinely drives the
    /// projection, so `--seed` is authoritative where callers pass it).
    #[test]
    fn distinct_seeds_diverge() -> anyhow::Result<()> {
        let dir = tempfile::tempdir()?;
        let data_vec = tiny_data_vec(&dir)?;
        let a = data_vec.project_columns_with_batch_correction_seeded::<usize>(4, None, None, 1)?;
        let b = data_vec.project_columns_with_batch_correction_seeded::<usize>(4, None, None, 2)?;
        assert_ne!(a.basis, b.basis, "distinct seeds → distinct basis");
        Ok(())
    }
}