data-beans 0.11.122

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
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
use super::*;
use rand::SeedableRng;

fn toy_gene_sums(
    num_entities: usize,
    num_features: usize,
    labels: &[usize],
    rng_seed: u64,
) -> Vec<Vec<(usize, f32)>> {
    let mut rng = SmallRng::seed_from_u64(rng_seed);
    let num_blocks = *labels.iter().max().unwrap() + 1;
    let per_block = num_features / num_blocks;
    (0..num_entities)
        .map(|e| {
            let c = labels[e];
            let start = c * per_block;
            let end = ((c + 1) * per_block).min(num_features);
            let mut row: Vec<(usize, f32)> = (start..end)
                .map(|g| (g, 5.0 + rng.random_range(0.0..3.0_f32)))
                .collect();
            for _ in 0..3 {
                let g: usize = rng.random_range(0..num_features);
                row.push((g, rng.random_range(0.0..1.0_f32)));
            }
            row.sort_unstable_by_key(|&(g, _)| g);
            row.dedup_by_key(|&mut (g, _)| g);
            row
        })
        .collect()
}

fn make_profiles(gene_sums: &[Vec<(usize, f32)>], num_features: usize) -> Profiles {
    Profiles::from_gene_sums(gene_sums, num_features)
}

#[test]
fn test_log_probs_match_after_delta_moves() {
    let n = 24;
    let m = 16;
    let labels: Vec<usize> = (0..n).map(|i| i % 4).collect();
    let gs = toy_gene_sums(n, m, &labels, 1);
    let profiles = make_profiles(&gs, m);

    let mut stats = DcPoissonStats::from_profiles(&profiles, 4, &labels);
    let mut rng = SmallRng::seed_from_u64(7);
    for _ in 0..50 {
        let e: usize = rng.random_range(0..n);
        let to: usize = rng.random_range(0..4);
        let from = stats.membership[e];
        stats.delta_move(e, from, to, &profiles);
    }

    let kept_mem = stats.membership.clone();
    let mut fresh = DcPoissonStats::from_profiles(&profiles, 4, &kept_mem);
    fresh.recompute(&profiles);

    for i in 0..stats.gene_sum.len() {
        assert!((stats.gene_sum[i] - fresh.gene_sum[i]).abs() < 1e-6);
        assert!((stats.log_gene[i] - fresh.log_gene[i]).abs() < 1e-6);
    }
    for i in 0..stats.size_sum.len() {
        assert!((stats.size_sum[i] - fresh.size_sum[i]).abs() < 1e-6);
        assert!((stats.log_size_offset[i] - fresh.log_size_offset[i]).abs() < 1e-6);
    }
}

#[test]
fn test_restricted_matches_unrestricted_on_allowed() {
    let n = 20;
    let m = 12;
    let labels: Vec<usize> = (0..n).map(|i| i % 4).collect();
    let gs = toy_gene_sums(n, m, &labels, 2);
    let profiles = make_profiles(&gs, m);
    let stats = DcPoissonStats::from_profiles(&profiles, 4, &labels);

    let mut full = vec![f64::NEG_INFINITY; 4];
    let mut restricted = vec![f64::NEG_INFINITY; 4];
    compute_log_probs(3, &stats, &profiles, &mut full);

    let allowed = vec![1usize, 3usize];
    compute_log_probs_restricted(3, &stats, &profiles, &allowed, &mut restricted);
    for &k in &allowed {
        assert!((full[k] - restricted[k]).abs() < 1e-9);
    }
    for (k, &lp) in restricted.iter().enumerate() {
        if !allowed.contains(&k) {
            assert!(lp.is_infinite() && lp < 0.0);
        }
    }
}

#[test]
fn test_empty_block_finite() {
    let gs = vec![vec![(0usize, 3.0f32)], vec![(1usize, 4.0f32)]];
    let profiles = Profiles::from_gene_sums(&gs, 2);
    let stats = DcPoissonStats::from_profiles(&profiles, 3, &[1, 2]);
    assert!(stats.log_size_offset[0].is_finite());
    assert_eq!(stats.size_sum[0], 0.0);
}

#[test]
fn test_delta_move_drift_keeps_logs_finite() {
    // Large-magnitude masses make the incremental f64 accumulator's residues
    // exceed LOG_EPS when a block's mass is drained back to zero; without
    // clamping, `ln(negative)` poisons the log caches with NaN.
    let n = 64;
    let m = 8;
    let k = 4;
    let mut rng = SmallRng::seed_from_u64(11);
    // Wide magnitude spread maximizes rounding in the running sums: adding a
    // tiny value to a huge accumulator loses low bits, so draining a block
    // back to (true) zero leaves a signed residue.
    let gs: Vec<Vec<(usize, f32)>> = (0..n)
        .map(|e| {
            let scale = if e % 3 == 0 { 1.0e-3 } else { 1.0e7 };
            (0..m)
                .map(|g| (g, scale * (1.0 + rng.random_range(0.0..1.0_f32))))
                .collect()
        })
        .collect();
    let profiles = make_profiles(&gs, m);
    let labels: Vec<usize> = (0..n).map(|i| i % k).collect();
    let mut stats = DcPoissonStats::from_profiles(&profiles, k, &labels);
    let mut order: Vec<usize> = (0..n).collect();
    // Churn: repeatedly collapse every entity into one block and scatter
    // back, visiting entities in a fresh random order each round.
    for round in 0..100 {
        order.shuffle(&mut rng);
        for &e in &order {
            let from = stats.membership[e];
            let to = if round % 2 == 0 { round % k } else { e % k };
            stats.delta_move(e, from, to, &profiles);
        }
        for &s in &stats.gene_sum {
            assert!(s >= 0.0, "gene_sum went negative: {s}");
        }
        for &s in &stats.size_sum {
            assert!(s >= 0.0, "size_sum went negative: {s}");
        }
        for &l in &stats.log_gene {
            assert!(l.is_finite(), "log_gene not finite: {l}");
        }
        for &l in &stats.log_size_offset {
            assert!(l.is_finite(), "log_size_offset not finite: {l}");
        }
    }
}

#[test]
fn test_score_is_leave_one_out_for_current_block() {
    // The score of e's current block must equal the score of the same block
    // computed against stats built with e removed — otherwise staying put
    // earns a self-inclusion bonus over every candidate destination.
    let n = 12;
    let m = 10;
    let labels: Vec<usize> = (0..n).map(|i| i % 3).collect();
    let gs = toy_gene_sums(n, m, &labels, 5);
    let profiles = make_profiles(&gs, m);
    // No noise charge: it applies to destinations only, and the block is
    // the current one on one side and a destination on the other.
    let flat = RefineParams {
        min_gain_z: 0.0,
        ..Default::default()
    };
    let stats = DcPoissonStats::from_profiles_with(&profiles, 3, &labels, &flat);

    let e = 4;
    let k_cur = labels[e];
    let mut lp = vec![f64::NEG_INFINITY; 3];
    compute_log_probs_restricted(e, &stats, &profiles, &[0, 1, 2], &mut lp);

    // Rebuild stats with e parked in a scratch block so k_cur excludes it;
    // scoring e -> k_cur then goes through the ordinary cached path.
    let mut labels_wo = labels.clone();
    labels_wo[e] = 3;
    let stats_wo = DcPoissonStats::from_profiles_with(&profiles, 4, &labels_wo, &flat);
    let mut lp_wo = vec![f64::NEG_INFINITY; 4];
    compute_log_probs_restricted(e, &stats_wo, &profiles, &[k_cur], &mut lp_wo);

    assert!(
        (lp[k_cur] - lp_wo[k_cur]).abs() < 1e-3,
        "current-block score {} differs from leave-one-out score {}",
        lp[k_cur],
        lp_wo[k_cur]
    );
}

fn planted_partition_recovery(parallel: bool) {
    let n = 80;
    let m = 40;
    let k = 4;
    let planted: Vec<usize> = (0..n).map(|i| i % k).collect();
    let gs = toy_gene_sums(n, m, &planted, 9);
    let profiles = make_profiles(&gs, m);

    let mut labels = planted.clone();
    let mut rng = SmallRng::seed_from_u64(3);
    for (e, l) in labels.iter_mut().enumerate() {
        if e % 4 == 0 {
            *l = rng.random_range(0..k);
        }
    }
    let candidates: Vec<Vec<usize>> = vec![(0..k).collect(); n];
    let params = RefineParams {
        parallel,
        ..Default::default()
    };
    let ctx = RefineContext {
        profiles: &profiles,
        k,
        params: &params,
        level_label: "test",
    };
    let mut sweep_rng = SmallRng::seed_from_u64(4);
    refine_with_candidates(&mut labels, &candidates, &mut sweep_rng, &ctx);
    let acc = labels
        .iter()
        .zip(planted.iter())
        .filter(|(a, b)| a == b)
        .count();
    assert!(
        acc as f64 >= 0.95 * n as f64,
        "recovered only {acc}/{n} planted labels (parallel={parallel})"
    );
}

#[test]
fn test_refine_recovers_planted_partition_sequential() {
    planted_partition_recovery(false);
}

#[test]
fn test_refine_recovers_planted_partition_jacobi() {
    planted_partition_recovery(true);
}

#[test]
fn test_compact_labels() {
    let (c, k) = compact_labels(&[5, 5, 2, 7, 2, 7, 5]);
    assert_eq!(k, 3);
    assert_eq!(c, vec![0, 0, 1, 2, 1, 2, 0]);
}

#[test]
fn test_compute_sibling_sets_at_coarsest_gives_all() {
    let refined = vec![
        vec![0usize, 1, 2, 3], // finest
        vec![0usize, 0, 1, 1], // coarsest
    ];
    let top = refined.len() - 1;
    let sibs = compute_sibling_sets(&refined, top, 2);
    assert!(sibs.iter().all(|s| s == &vec![0usize, 1]));
}

#[test]
fn test_compute_sibling_sets_respects_parent() {
    let refined = vec![
        vec![0usize, 1, 2, 3], // finest: 4 groups
        vec![0usize, 0, 1, 1], // parent: {0,1}→0, {2,3}→1
    ];
    let sibs = compute_sibling_sets(&refined, 0, 4);
    assert_eq!(sibs[0], vec![0, 1]);
    assert_eq!(sibs[1], vec![0, 1]);
    assert_eq!(sibs[2], vec![2, 3]);
    assert_eq!(sibs[3], vec![2, 3]);
}

#[test]
fn test_one_pass_scores_match_per_block_scores_exactly() {
    // The one-pass kernel must reproduce the per-block reference bit for bit,
    // for the full block set (dense path), for unsorted subsets with and
    // without the entity's current block (gather path), after moves.
    let n = 40;
    let m = 30;
    let k = 6;
    let labels: Vec<usize> = (0..n).map(|i| i % k).collect();
    let gs = toy_gene_sums(n, m, &labels, 21);
    let mut profiles = make_profiles(&gs, m);
    profiles.apply_feature_weighting(FeatureWeighting::FisherInfoNb);
    let mut stats = DcPoissonStats::from_profiles(&profiles, k, &labels);
    let mut rng = SmallRng::seed_from_u64(5);
    for _ in 0..30 {
        let e: usize = rng.random_range(0..n);
        let to: usize = rng.random_range(0..k);
        let from = stats.membership[e];
        stats.delta_move(e, from, to, &profiles);
    }
    let subsets: Vec<Vec<usize>> = vec![
        (0..k).collect(),
        (0..k).rev().collect(),
        vec![4, 1],
        vec![5, 0, 2],
        vec![3],
    ];
    for e in 0..n {
        let mut reference = vec![f64::NAN; k];
        compute_log_probs(e, &stats, &profiles, &mut reference);
        for allowed in &subsets {
            let mut fast = vec![f64::NEG_INFINITY; k];
            compute_log_probs_restricted(e, &stats, &profiles, allowed, &mut fast);
            for &b in allowed {
                assert_eq!(
                    fast[b].to_bits(),
                    reference[b].to_bits(),
                    "entity {e}, block {b}, allowed {allowed:?}"
                );
            }
        }
    }
}

#[test]
fn test_stats_layout_is_feature_major() {
    let gs = vec![vec![(0usize, 1.0f32), (2, 3.0)], vec![(1usize, 4.0f32)]];
    let profiles = Profiles::from_gene_sums(&gs, 3);
    let stats = DcPoissonStats::from_profiles(&profiles, 2, &[0, 1]);
    assert_eq!(stats.idx(1, 2), 2 * 2 + 1);
    assert_eq!(stats.gene_sum, vec![1.0, 0.0, 0.0, 4.0, 3.0, 0.0]);
}

#[test]
fn test_repeated_candidates_score_like_single_ones() {
    // A proposer may list a block twice; each listing must get the block's
    // own score (the per-block reference), never a double-counted one.
    let n = 30;
    let m = 20;
    let k = 5;
    let labels: Vec<usize> = (0..n).map(|i| i % k).collect();
    let gs = toy_gene_sums(n, m, &labels, 33);
    let profiles = make_profiles(&gs, m);
    let stats = DcPoissonStats::from_profiles(&profiles, k, &labels);
    let subsets: Vec<Vec<usize>> = vec![
        vec![2, 2],
        vec![1, 3, 1],
        vec![0, 4, 0, 4, 2],
        // as long as K, with a repeat (dense path, block 4 missing)
        vec![0, 1, 1, 2, 3],
        // longer than K
        vec![0, 1, 2, 3, 4, 3, 0],
    ];
    for e in 0..n {
        let mut reference = vec![f64::NAN; k];
        compute_log_probs(e, &stats, &profiles, &mut reference);
        let cur = stats.membership[e];
        for base in &subsets {
            for allowed in [base.clone(), {
                let mut a = base.clone();
                a.push(cur);
                a.push(cur);
                a
            }] {
                let mut fast = vec![f64::NEG_INFINITY; k];
                compute_log_probs_restricted(e, &stats, &profiles, &allowed, &mut fast);
                for &b in &allowed {
                    assert_eq!(
                        fast[b].to_bits(),
                        reference[b].to_bits(),
                        "entity {e}, block {b}, allowed {allowed:?}"
                    );
                }
            }
        }
    }
}

#[test]
fn fast_ln_is_ln_to_f32_rounding() {
    // f32 rounding of the input and of the result: below 4e-6 for |ln x| < 30.
    let mut x = LOG_EPS;
    while x < 1e12 {
        let err = (fast_ln(x) - x.ln()).abs();
        assert!(err < 4e-6, "x = {x}: {} vs {}", fast_ln(x), x.ln());
        x *= 1.0137;
    }
    // Monotone and exact near 1, where -ln(u) is tiny.
    let mut prev = f64::NEG_INFINITY;
    for i in (0..1000).rev() {
        let u = 1.0 - i as f64 * 1e-7;
        let v = fast_ln(u);
        assert!(v >= prev && (v - u.ln()).abs() < 1e-7, "u = {u}");
        prev = v;
    }
    assert!(fast_ln(f64::NAN).is_nan());
    assert!(fast_ln(-1.0).is_nan());
    assert_eq!(fast_ln(0.0), f64::NEG_INFINITY);
    assert_eq!(fast_ln(f64::INFINITY), f64::INFINITY);
}

/// An entity with an empty profile scores every block alike; a greedy sweep
/// leaves it where it is instead of moving it to its first candidate.
fn greedy_keeps_an_entity_without_evidence(parallel: bool) {
    let (n, m, k) = (24, 16, 4);
    let planted: Vec<usize> = (0..n).map(|i| i % k).collect();
    let mut gs = toy_gene_sums(n, m, &planted, 5);
    let blank = 7;
    gs[blank].clear();
    let profiles = make_profiles(&gs, m);
    assert_eq!(profiles.size_factor[blank], 0.0);

    let mut labels = planted.clone();
    assert_eq!(labels[blank], 3);
    let candidates: Vec<Vec<usize>> = vec![vec![1, 3]; n];
    let params = RefineParams {
        num_gibbs: 0,
        num_greedy: 3,
        parallel,
        ..Default::default()
    };
    let ctx = RefineContext {
        profiles: &profiles,
        k,
        params: &params,
        level_label: "test",
    };
    let mut rng = SmallRng::seed_from_u64(1);
    refine_with_candidates(&mut labels, &candidates, &mut rng, &ctx);
    assert_eq!(labels[blank], 3, "parallel={parallel}");
}

#[test]
fn greedy_keeps_an_entity_without_evidence_sequential() {
    greedy_keeps_an_entity_without_evidence(false);
}

#[test]
fn greedy_keeps_an_entity_without_evidence_jacobi() {
    greedy_keeps_an_entity_without_evidence(true);
}

/// The Fisher weights of (non-integer) profiles come out the same bits on
/// any number of threads.
#[test]
fn nb_fisher_weights_do_not_depend_on_the_thread_count() {
    let (n, m, k) = (3000, 24, 4);
    let labels: Vec<usize> = (0..n).map(|i| i % k).collect();
    let profiles = make_profiles(&toy_gene_sums(n, m, &labels, 11), m);
    let on_threads = |threads: usize| -> Vec<u32> {
        rayon::ThreadPoolBuilder::new()
            .num_threads(threads)
            .build()
            .expect("thread pool")
            .install(|| profiles.nb_fisher_weights())
            .iter()
            .map(|w| w.to_bits())
            .collect()
    };
    let one = on_threads(1);
    assert_eq!(one.len(), m);
    for threads in [3, 6] {
        assert_eq!(on_threads(threads), one, "{threads} threads");
    }
}

/////////////////////////////////////////////
// Block sizes: ties, floors, small blocks //
/////////////////////////////////////////////

use rand_distr::{Distribution, LogNormal, Poisson};

/// Feature count of the simulated profiles below.
const SIM_M: usize = 1000;

/// Lognormal(0, 1) rates on features `lo..hi`, normalised to one.
fn sim_profile(rng: &mut SmallRng, lo: usize, hi: usize) -> Vec<f64> {
    let ln = LogNormal::new(0.0, 1.0).unwrap();
    let mut p = vec![0f64; SIM_M];
    for x in p[lo..hi].iter_mut() {
        *x = ln.sample(rng);
    }
    let s: f64 = p.iter().sum();
    p.iter_mut().for_each(|x| *x /= s);
    p
}

/// `p` with a tenth of its features doubled or halved, renormalised.
fn sim_perturbed(rng: &mut SmallRng, p: &[f64]) -> Vec<f64> {
    let mut q = p.to_vec();
    for x in q.iter_mut() {
        if rng.random::<f64>() < 0.1 {
            *x *= if rng.random::<f64>() < 0.5 { 2.0 } else { 0.5 };
        }
    }
    let s: f64 = q.iter().sum();
    q.iter_mut().for_each(|x| *x /= s);
    q
}

/// One entity of `depth` expected counts drawn from profile `p`.
fn sim_entity(rng: &mut SmallRng, p: &[f64], depth: f64) -> Vec<(usize, f32)> {
    let mut row = Vec::new();
    for (g, &pg) in p.iter().enumerate() {
        if pg > 0.0 {
            let x: f64 = Poisson::new(depth * pg).unwrap().sample(rng);
            if x > 0.0 {
                row.push((g, x as f32));
            }
        }
    }
    row
}

/// Refine `labels` with every block a candidate of every entity; returns
/// the number of moves.
fn sim_refine(
    rows: &[Vec<(usize, f32)>],
    labels: &mut [usize],
    k: usize,
    params: &RefineParams,
) -> usize {
    let mut profiles = Profiles::from_gene_sums(rows, SIM_M);
    profiles.apply_feature_weighting(params.feature_weighting);
    let candidates: Vec<Vec<usize>> = vec![(0..k).collect(); labels.len()];
    let ctx = RefineContext {
        profiles: &profiles,
        k,
        params,
        level_label: "test",
    };
    let mut rng = SmallRng::seed_from_u64(params.seed);
    refine_with_candidates(labels, &candidates, &mut rng, &ctx)
}

fn block_sizes(labels: &[usize], k: usize) -> Vec<usize> {
    let mut n = vec![0usize; k];
    labels.iter().for_each(|&l| n[l] += 1);
    n
}

/// Sweep settings of the two callers, Jacobi and sequential.
fn sim_param_sets() -> Vec<RefineParams> {
    let mut out = Vec::new();
    for parallel in [true, false] {
        for (num_gibbs, num_greedy) in [(3, 10), (20, 10), (0, 10)] {
            out.push(RefineParams {
                num_gibbs,
                num_greedy,
                parallel,
                ..Default::default()
            });
        }
    }
    out
}

#[test]
fn equal_scores_keep_the_current_block() {
    let lp = [1.0, 1.0, 1.0, 0.5];
    for current in 0..3 {
        assert_eq!(argmax_log_staying(&lp, &[0, 1, 2, 3], current), current);
        assert_eq!(argmax_log_staying(&lp, &[3, 2, 1, 0], current), current);
    }
    assert_eq!(argmax_log_staying(&lp, &[0, 1, 2, 3], 3), 0);
    // a score that does not compare is no evidence either
    assert_eq!(argmax_log_staying(&[f64::NAN, 2.0], &[0, 1], 0), 0);
    assert_eq!(argmax_log_staying(&[1.0, f64::NAN], &[0, 1], 0), 0);

    // Draws never pick a block that does not beat the current one.
    let mut rng = SmallRng::seed_from_u64(1);
    for _ in 0..200 {
        assert_eq!(sample_improving_log(&lp, &[0, 1, 2, 3], 1, &mut rng), 1);
    }
    let stays = (0..2000)
        .filter(|_| sample_improving_log(&[0.0, 0.1], &[0, 1], 0, &mut rng) == 0)
        .count();
    assert!(
        (800..1100).contains(&stays),
        "a slightly better block is drawn in proportion: {stays}"
    );
}

#[test]
fn entities_without_evidence_do_not_move() {
    // Four entities, each alone in its block and on features nothing else
    // shows, next to a large block and four empty blocks of lower index.
    // With the floor lifted nothing but the scores holds them: leaving one
    // out, its block is empty too, an exact tie with every empty block —
    // which must not pull the four into one of them. A fifth entity has no
    // counts at all.
    let mut rng = SmallRng::seed_from_u64(2);
    let p = sim_profile(&mut rng, 0, 800);
    let mut rows = Vec::new();
    let mut start = Vec::new();
    for i in 0..4 {
        let q = sim_profile(&mut rng, 800 + 50 * i, 850 + 50 * i);
        rows.push(sim_entity(&mut rng, &q, 300.0));
        start.push(5 + i);
    }
    rows.push(Vec::new());
    start.push(4);
    for _ in 0..40 {
        rows.push(sim_entity(&mut rng, &p, 300.0));
        start.push(4);
    }
    for mut params in sim_param_sets() {
        params.size_floor = 0.0;
        params.size_cap = 0.0;
        let mut labels = start.clone();
        sim_refine(&rows, &mut labels, 9, &params);
        assert_eq!(
            &labels[..5],
            &start[..5],
            "parallel={} gibbs={}",
            params.parallel,
            params.num_gibbs
        );
    }
}

#[test]
fn singleton_blocks_keep_their_member() {
    // Three large blocks and nine one-member blocks: three drawn from the
    // large blocks' own profiles, three from fresh profiles, three on
    // features nothing else shows.
    let mut rng = SmallRng::seed_from_u64(3);
    let big: Vec<Vec<f64>> = (0..3).map(|_| sim_profile(&mut rng, 0, 850)).collect();
    let mut rows = Vec::new();
    let mut start = Vec::new();
    for (b, p) in big.iter().enumerate() {
        for _ in 0..60 {
            rows.push(sim_entity(&mut rng, p, 500.0));
            start.push(b);
        }
    }
    let mut singles: Vec<Vec<f64>> = big.clone();
    for _ in 0..3 {
        singles.push(sim_profile(&mut rng, 0, 850));
    }
    for i in 0..3 {
        singles.push(sim_profile(&mut rng, 850 + 50 * i, 900 + 50 * i));
    }
    let k = 3 + singles.len();
    for (i, p) in singles.iter().enumerate() {
        rows.push(sim_entity(&mut rng, p, 500.0));
        start.push(3 + i);
    }
    for params in sim_param_sets() {
        let mut labels = start.clone();
        sim_refine(&rows, &mut labels, k, &params);
        assert_eq!(
            &labels[180..],
            &start[180..],
            "singletons moved (parallel={})",
            params.parallel
        );
        assert!(block_sizes(&labels, k).iter().all(|&n| n > 0));
        // A fixed point: one more greedy sweep moves nothing.
        let again = RefineParams {
            num_gibbs: 0,
            num_greedy: 1,
            ..params.clone()
        };
        assert_eq!(sim_refine(&rows, &mut labels, k, &again), 0);
    }
}

/// 30 entities of profile A in block 0 against 300 in block 1, of profile A
/// again (`same`) or of A with a tenth of the features doubled or halved.
fn small_against_large(same: bool, seed: u64) -> (Vec<Vec<(usize, f32)>>, Vec<usize>) {
    let mut rng = SmallRng::seed_from_u64(seed);
    let pa = sim_profile(&mut rng, 0, SIM_M);
    let pb = if same {
        pa.clone()
    } else {
        sim_perturbed(&mut rng, &pa)
    };
    let mut rows = Vec::new();
    let mut labels = Vec::new();
    for e in 0..330 {
        rows.push(sim_entity(&mut rng, if e < 30 { &pa } else { &pb }, 500.0));
        labels.push(usize::from(e >= 30));
    }
    (rows, labels)
}

#[test]
fn small_distinct_block_keeps_its_members() {
    // The score, not the size limits, has to hold the small block: with
    // the limits lifted its members still stay, and the large block's
    // members do not flood it.
    for limits in [true, false] {
        let (mut kept, mut joined) = (0usize, 0usize);
        for seed in 0..4 {
            let (rows, start) = small_against_large(false, 10 + seed);
            let mut params = RefineParams {
                seed,
                ..Default::default()
            };
            if !limits {
                params.size_floor = 0.0;
                params.size_cap = 0.0;
            }
            let mut labels = start.clone();
            sim_refine(&rows, &mut labels, 2, &params);
            kept += labels[..30].iter().filter(|&&l| l == 0).count();
            joined += labels[30..].iter().filter(|&&l| l == 0).count();
        }
        assert!(kept >= 4 * 27, "limits={limits}: kept {kept} of 120");
        assert!(joined <= 4 * 3, "limits={limits}: {joined} joined");
    }
}

#[test]
fn small_block_of_the_same_profile_is_not_drained() {
    // Same profile, a tenth of the size. Nothing tells the blocks apart, so
    // members may go either way, but the small block must not empty — with
    // the size limits lifted too.
    for limits in [true, false] {
        for seed in 0..4 {
            let (rows, start) = small_against_large(true, 20 + seed);
            let mut params = RefineParams {
                seed,
                ..Default::default()
            };
            if !limits {
                params.size_floor = 0.0;
                params.size_cap = 0.0;
            }
            let mut labels = start.clone();
            sim_refine(&rows, &mut labels, 2, &params);
            let sizes = block_sizes(&labels, 2);
            let floor = if limits { 27 } else { 15 };
            assert!(
                sizes[0] >= floor,
                "limits={limits} seed={seed}: small block left with {}",
                sizes[0]
            );
        }
    }
}

#[test]
fn mislabelled_entities_are_corrected() {
    // Eight blocks of sixty with distinct profiles; a fifth of the entities
    // start in a wrong block.
    let k = 8;
    let mut rng = SmallRng::seed_from_u64(30);
    let mut rows = Vec::new();
    let mut truth = Vec::new();
    let mut start = Vec::new();
    for b in 0..k {
        let p = sim_profile(&mut rng, 0, SIM_M);
        for _ in 0..60 {
            rows.push(sim_entity(&mut rng, &p, 500.0));
            truth.push(b);
            start.push(if rng.random::<f64>() < 0.2 {
                (b + 1 + rng.random_range(0..k - 1)) % k
            } else {
                b
            });
        }
    }
    let wrong = start.iter().zip(&truth).filter(|(a, b)| a != b).count();
    assert!(wrong >= 60, "fixture should start mislabelled ({wrong})");
    for params in sim_param_sets() {
        let mut labels = start.clone();
        sim_refine(&rows, &mut labels, k, &params);
        let right = labels.iter().zip(&truth).filter(|(a, b)| a == b).count();
        assert!(
            right >= truth.len() - 2,
            "{right} of {} in their block (parallel={}, gibbs={})",
            truth.len(),
            params.parallel,
            params.num_gibbs
        );
    }
}

/// Twelve blocks over three profiles, sizes 6 to 48: every block starts
/// with two-thirds of its members of one profile and the rest of the two
/// others.
struct MixedBlocks {
    rows: Vec<Vec<(usize, f32)>>,
    /// Profile of each entity.
    truth: Vec<usize>,
    /// Starting block of each entity.
    start: Vec<usize>,
    /// Number of blocks.
    k: usize,
    /// Number of profiles.
    t: usize,
}

fn mixed_blocks_fixture() -> MixedBlocks {
    let (k, t) = (12, 3);
    let mut rng = SmallRng::seed_from_u64(40);
    let profs: Vec<Vec<f64>> = (0..t).map(|_| sim_profile(&mut rng, 0, SIM_M)).collect();
    let mut rows = Vec::new();
    let mut truth = Vec::new();
    let mut start = Vec::new();
    for b in 0..k {
        let n = [6, 12, 24, 48][b % 4];
        for i in 0..n {
            let ty = if i % 3 == 2 {
                (b + 1 + i % 2) % t
            } else {
                b % t
            };
            rows.push(sim_entity(&mut rng, &profs[ty], 1000.0));
            truth.push(ty);
            start.push(b);
        }
    }
    MixedBlocks {
        rows,
        truth,
        start,
        k,
        t,
    }
}

#[test]
fn mixed_blocks_are_purified_within_the_size_limits() {
    // Twelve blocks of unequal size over three profiles, each two-thirds
    // one profile. The members that do not belong move out; no block falls
    // under its floor, exceeds its cap, or empties, and the members a
    // block is named after stay.
    let MixedBlocks {
        rows,
        truth,
        start,
        k,
        t,
    } = mixed_blocks_fixture();
    let purity = |labels: &[usize]| {
        let mut c = vec![0usize; k * t];
        for (&l, &ty) in labels.iter().zip(&truth) {
            c[l * t + ty] += 1;
        }
        let dom: usize = c.chunks(t).map(|b| *b.iter().max().unwrap()).sum();
        dom as f64 / labels.len() as f64
    };
    let n0 = block_sizes(&start, k);
    // 270 members in 12 blocks: a mean of 22.5. Nine tenths of
    // min(n0, mean) and five quarters of max(n0, mean), rounded up.
    assert_eq!(start.len(), 270);
    let limit_of = |n: usize, limits: [usize; 4]| {
        limits[[6, 12, 24, 48].iter().position(|&x| x == n).unwrap()]
    };
    let floor = |n: usize| limit_of(n, [6, 11, 21, 21]);
    let cap = |n: usize| limit_of(n, [29, 29, 30, 60]);
    for params in sim_param_sets() {
        let mut labels = start.clone();
        sim_refine(&rows, &mut labels, k, &params);
        let n1 = block_sizes(&labels, k);
        for b in 0..k {
            assert!(
                (floor(n0[b])..=cap(n0[b])).contains(&n1[b]),
                "block {b}: {} -> {} outside {}..={}",
                n0[b],
                n1[b],
                floor(n0[b]),
                cap(n0[b])
            );
        }
        assert!(
            purity(&labels) >= purity(&start) + 0.1,
            "purity {} -> {} (parallel={})",
            purity(&start),
            purity(&labels),
            params.parallel
        );
        let named = (0..start.len()).filter(|&e| truth[e] == start[e] % t);
        let (mut all, mut stayed) = (0, 0);
        for e in named {
            all += 1;
            stayed += usize::from(labels[e] == start[e]);
        }
        // A Jacobi sweep applies its moves by gain, so the members that fit
        // worst leave first; a sequential sweep takes them as it visits,
        // and more of a block's own members leave for a purer block of
        // their profile.
        let share = if params.parallel { 0.9 } else { 0.6 };
        assert!(
            stayed as f64 >= share * all as f64,
            "{stayed} of {all} stayed (parallel={})",
            params.parallel
        );
    }
}

#[test]
fn lifted_floor_lets_a_mixed_block_shed_one_profile() {
    // Without a floor the likelihood alone decides: a block that is half
    // one profile and half another gives the members of one of them to
    // that profile's pure block. What is left is a small block of the
    // other profile, which nothing tells apart from the large pure block
    // of that profile, so it stays.
    let mut rng = SmallRng::seed_from_u64(50);
    let pa = sim_profile(&mut rng, 0, SIM_M);
    let pb = sim_profile(&mut rng, 0, SIM_M);
    let mut rows = Vec::new();
    let mut start = Vec::new();
    for b in 0..3 {
        for i in 0..40 {
            let p = match b {
                0 => &pa,
                1 => &pb,
                _ if i % 2 == 0 => &pa,
                _ => &pb,
            };
            rows.push(sim_entity(&mut rng, p, 500.0));
            start.push(b);
        }
    }
    let free = RefineParams {
        size_floor: 0.0,
        size_cap: 0.0,
        ..Default::default()
    };
    let mut labels = start.clone();
    sim_refine(&rows, &mut labels, 3, &free);
    let left: Vec<usize> = (80..120).filter(|&e| labels[e] == 2).collect();
    assert!(left.len() <= 20, "{} of 40 left", left.len());
    assert!(
        left.iter().all(|e| e % 2 == 0) || left.iter().all(|e| e % 2 == 1),
        "what is left is of one profile"
    );
    assert_eq!(
        labels[..80],
        start[..80],
        "the pure blocks keep their members"
    );

    let mut labels = start.clone();
    sim_refine(&rows, &mut labels, 3, &RefineParams::default());
    assert!(block_sizes(&labels, 3)[2] >= 36, "the default floor holds");
}

#[test]
fn log_caches_follow_delta_moves_with_counts() {
    let n = 30;
    let m = 12;
    let labels: Vec<usize> = (0..n).map(|i| i % 3).collect();
    let gs = toy_gene_sums(n, m, &labels, 8);
    let profiles = make_profiles(&gs, m);
    let mut stats = DcPoissonStats::from_profiles(&profiles, 3, &labels);
    let mut rng = SmallRng::seed_from_u64(9);
    for _ in 0..40 {
        let e: usize = rng.random_range(0..n);
        let from = stats.membership[e];
        stats.delta_move(e, from, rng.random_range(0..3), &profiles);
    }
    let fresh = DcPoissonStats::from_profiles(&profiles, 3, &stats.membership);
    assert_eq!(stats.count, fresh.count);
    assert_eq!(stats.count.iter().sum::<usize>(), n);
    for (moved, built) in [
        (&stats.class_null, &fresh.class_null),
        (&stats.class_var, &fresh.class_var),
    ] {
        assert_eq!(moved.len(), built.len());
        for (a, b) in moved.iter().zip(built) {
            assert!((a - b).abs() < 1e-9, "{a} vs {b}");
        }
    }
}

#[test]
fn empty_block_scores_as_the_pooled_profile() {
    // An empty block and the leave-one-out view of a one-member block are
    // the same thing, the pooled profile, and score alike.
    let mut rng = SmallRng::seed_from_u64(60);
    let p = sim_profile(&mut rng, 0, SIM_M);
    let rows: Vec<Vec<(usize, f32)>> = (0..20).map(|_| sim_entity(&mut rng, &p, 400.0)).collect();
    let profiles = Profiles::from_gene_sums(&rows, SIM_M);
    // entity 0 alone in block 1; block 2 empty
    let labels: Vec<usize> = (0..20).map(|e| usize::from(e == 0)).collect();
    let flat = RefineParams {
        min_gain_z: 0.0,
        ..Default::default()
    };
    let stats = DcPoissonStats::from_profiles_with(&profiles, 3, &labels, &flat);
    let mut lp = vec![0f64; 3];
    compute_log_probs_restricted(0, &stats, &profiles, &[0, 1, 2], &mut lp);
    assert!(lp.iter().all(|x| x.is_finite()));
    assert!(
        (lp[1] - lp[2]).abs() < 1e-6 * lp[1].abs(),
        "own block {} vs empty block {}",
        lp[1],
        lp[2]
    );
    // -ln M per count would be the uniform profile; the pooled one is better.
    let per_count = lp[2] / profiles.size_factor[0] as f64;
    assert!(per_count > -(SIM_M as f64).ln());
}

#[test]
fn repeated_calls_do_not_empty_a_block() {
    // The limits are those of each call's input, so a block may shrink a
    // step per call: a tenth of its members while it has ten or more, and
    // not at all below ten. Called again and again on its own output, no
    // block of the mixed partition falls under min(its start, nine).
    let MixedBlocks { rows, start, k, .. } = mixed_blocks_fixture();
    let n0 = block_sizes(&start, k);
    for parallel in [true, false] {
        let params = RefineParams {
            parallel,
            ..Default::default()
        };
        let mut labels = start.clone();
        for call in 0..12 {
            sim_refine(&rows, &mut labels, k, &params);
            let n = block_sizes(&labels, k);
            for b in 0..k {
                assert!(
                    n[b] >= n0[b].min(9),
                    "call {call}, block {b}: {} -> {} (parallel={parallel})",
                    n0[b],
                    n[b]
                );
            }
        }
    }
}

#[test]
fn size_limits_of_small_and_empty_blocks() {
    let limits = |count: &[usize], size_floor: f32, size_cap: f32| {
        let l = SizeLimits::new(
            count,
            &RefineParams {
                size_floor,
                size_cap,
                ..Default::default()
            },
        );
        (l.floor, l.cap)
    };
    // Four occupied blocks of 120 members: a mean of 30.
    let count = [0, 1, 9, 10, 100];
    let (floor, cap) = limits(&count, 0.9, 1.5);
    // An empty block and a block of one have a floor of one; nine members
    // stay nine and ten may fall to nine; the large block may fall to nine
    // tenths of the mean.
    assert_eq!(floor, vec![1, 1, 9, 9, 27]);
    assert_eq!(cap, vec![45, 45, 45, 45, 150]);
    let l = SizeLimits::new(
        &count,
        &RefineParams {
            size_floor: 0.9,
            size_cap: 1.5,
            ..Default::default()
        },
    );
    assert!(!l.can_leave(&count, 0) && !l.can_leave(&count, 1));
    assert!(!l.can_leave(&count, 2) && l.can_leave(&count, 3));
    assert!(l.admits(&count, 4, 0), "an empty block takes members");
    assert!(l.admits(&count, 3, 2), "a block at its floor takes members");

    // Zero switches a limit off.
    let (floor, cap) = limits(&count, 0.0, 0.0);
    assert_eq!(floor, vec![0; 5]);
    assert_eq!(cap, vec![usize::MAX; 5]);

    // A decimal fraction gives the limit it gives on paper: eight tenths of
    // ten is eight, both ways.
    let (floor, cap) = limits(&[10, 10], 0.8, 0.8);
    assert_eq!((floor, cap), (vec![8, 8], vec![8, 8]));
    let (floor, cap) = limits(&[10, 10, 10], 0.7, 1.1);
    assert_eq!((floor, cap), (vec![7; 3], vec![11; 3]));
    // A mean that is no whole number: 7 members in 2 blocks.
    let (floor, cap) = limits(&[3, 4], 1.0, 1.0);
    assert_eq!((floor, cap), (vec![3, 4], vec![4, 4]));
    // No block at all.
    let (floor, cap) = limits(&[], 0.9, 1.5);
    assert!(floor.is_empty() && cap.is_empty());
}

/// Blocks 0 (ten of profile A), 1 (ten of B) and 2 (ten of C and twenty of
/// A, which belong in block 0). The mean size is 50 / 3.
fn crowded_block_fixture() -> (Vec<Vec<(usize, f32)>>, Vec<usize>) {
    let mut rng = SmallRng::seed_from_u64(80);
    let profs: Vec<Vec<f64>> = (0..3).map(|_| sim_profile(&mut rng, 0, SIM_M)).collect();
    let mut rows = Vec::new();
    let mut start = Vec::new();
    for (block, ty, n) in [(0, 0, 10), (1, 1, 10), (2, 2, 10), (2, 0, 20)] {
        for _ in 0..n {
            rows.push(sim_entity(&mut rng, &profs[ty], 800.0));
            start.push(block);
        }
    }
    (rows, start)
}

#[test]
fn the_cap_and_the_floor_bind() {
    let (rows, start) = crowded_block_fixture();
    for parallel in [true, false] {
        let run = |size_floor: f32, size_cap: f32| {
            let params = RefineParams {
                size_floor,
                size_cap,
                parallel,
                ..Default::default()
            };
            let mut labels = start.clone();
            sim_refine(&rows, &mut labels, 3, &params);
            block_sizes(&labels, 3)
        };
        // Cap of block 0: the mean, rounded up.
        assert_eq!(run(0.9, 1.0), vec![17, 10, 23], "parallel={parallel}");
        // No cap: block 2 gives members down to nine tenths of the mean.
        assert_eq!(run(0.9, 0.0), vec![25, 10, 15], "parallel={parallel}");
        // No limits: every member of profile A joins block 0.
        assert_eq!(run(0.0, 0.0), vec![30, 10, 10], "parallel={parallel}");
    }
}

/// Vetoes every move of an entity with an even index.
struct EvenStay;

impl MoveGuard for EvenStay {
    fn accept_move(&self, entity: usize, _from: usize, _labels: &[usize]) -> bool {
        entity % 2 == 1
    }
}

#[test]
fn a_vetoed_move_changes_no_count() {
    // The limits are read off the member counts, so a move the guard
    // refuses must leave them as they were: afterwards the counts are those
    // of the labels, the vetoed entities are where they started, and the
    // cap still holds.
    let (rows, start) = crowded_block_fixture();
    let mut profiles = Profiles::from_gene_sums(&rows, SIM_M);
    profiles.apply_feature_weighting(FeatureWeighting::FisherInfoNb);
    let candidates: Vec<Vec<usize>> = vec![(0..3).collect(); start.len()];
    for parallel in [true, false] {
        let params = RefineParams {
            size_cap: 1.0,
            parallel,
            ..Default::default()
        };
        let ctx = RefineContext {
            profiles: &profiles,
            k: 3,
            params: &params,
            level_label: "test",
        };
        let mut labels = start.clone();
        let mut rng = SmallRng::seed_from_u64(1);
        let moves =
            refine_with_candidates_guarded(&mut labels, &candidates, &EvenStay, &mut rng, &ctx);
        for e in (0..labels.len()).step_by(2) {
            assert_eq!(labels[e], start[e], "entity {e} moved past the guard");
        }
        let moved = labels.iter().zip(&start).filter(|(a, b)| a != b).count();
        assert!(moved > 0 && moves >= moved, "parallel={parallel}");
        // Seven places under the cap of block 0, ten odd entities that want
        // them: the vetoed ones did not use any up.
        assert_eq!(
            block_sizes(&labels, 3),
            vec![17, 10, 23],
            "parallel={parallel}"
        );
    }
}

/// Vetoes every move of one entity.
struct Pinned(usize);

impl MoveGuard for Pinned {
    fn accept_move(&self, entity: usize, _from: usize, _labels: &[usize]) -> bool {
        entity != self.0
    }
}

#[test]
fn small_blocks_exchange_their_misfits() {
    // Eight entities in two blocks of four, `n_a` of profile A and the
    // rest of profile B. Nine tenths of four, rounded up, is four: neither
    // block may give a member.
    let refine = |n_a: usize, start: &[usize], params: &RefineParams, pinned: Option<usize>| {
        let mut rng = SmallRng::seed_from_u64(85);
        let pa = sim_profile(&mut rng, 0, SIM_M);
        let pb = sim_profile(&mut rng, 0, SIM_M);
        let rows: Vec<Vec<(usize, f32)>> = (0..8)
            .map(|e| sim_entity(&mut rng, if e < n_a { &pa } else { &pb }, 500.0))
            .collect();
        let profiles = Profiles::from_gene_sums(&rows, SIM_M);
        let candidates: Vec<Vec<usize>> = vec![vec![0, 1]; 8];
        let ctx = RefineContext {
            profiles: &profiles,
            k: 2,
            params,
            level_label: "test",
        };
        let mut labels = start.to_vec();
        let mut rng = SmallRng::seed_from_u64(1);
        let moves = match pinned {
            Some(e) => {
                refine_with_candidates_guarded(&mut labels, &candidates, &Pinned(e), &mut rng, &ctx)
            }
            None => refine_with_candidates(&mut labels, &candidates, &mut rng, &ctx),
        };
        (labels, moves)
    };
    for parallel in [true, false] {
        let params = RefineParams {
            feature_weighting: FeatureWeighting::None,
            parallel,
            ..Default::default()
        };
        // One member of each block belongs in the other: the two change
        // places, and the sizes stay.
        let swapped = [0, 0, 0, 1, 0, 1, 1, 1];
        assert_eq!(
            refine(4, &swapped, &params, None),
            (vec![0, 0, 0, 0, 1, 1, 1, 1], 2),
            "parallel={parallel}"
        );
        // With either of the two held by the guard, neither moves.
        for held in [3, 4] {
            assert_eq!(
                refine(4, &swapped, &params, Some(held)).0,
                swapped,
                "parallel={parallel}, entity {held} held"
            );
        }
        // Three of profile A and one of B against four of B: the misfit
        // has no counterpart and stays, since its block would shrink.
        let lone = [0, 0, 0, 0, 1, 1, 1, 1];
        assert_eq!(
            refine(3, &lone, &params, None),
            (lone.to_vec(), 0),
            "parallel={parallel}"
        );
        // Without the floor it joins its own.
        let free = RefineParams {
            size_floor: 0.0,
            ..params.clone()
        };
        assert_eq!(
            refine(3, &lone, &free, None).0,
            vec![0, 0, 0, 1, 1, 1, 1, 1],
            "parallel={parallel}"
        );
    }
}

#[test]
fn a_jacobi_sweep_rescores_moves_out_of_a_changed_block() {
    // Block 2 is half profile A, half profile B, next to a pure block of
    // each. Against the snapshot every member of block 2 gains by leaving.
    // Applied in turn, the moves of one half leave a block of the other
    // half only, which the rest then has no reason to leave: one sweep
    // moves all of one half and keeps the block.
    let mut rng = SmallRng::seed_from_u64(90);
    let pa = sim_profile(&mut rng, 0, SIM_M);
    let pb = sim_profile(&mut rng, 0, SIM_M);
    let mut rows = Vec::new();
    let mut start = Vec::new();
    for (block, p, n) in [(0, &pa, 30), (1, &pb, 30), (2, &pa, 6), (2, &pb, 6)] {
        for _ in 0..n {
            rows.push(sim_entity(&mut rng, p, 500.0));
            start.push(block);
        }
    }
    let params = RefineParams {
        num_gibbs: 0,
        num_greedy: 1,
        size_floor: 0.0,
        size_cap: 0.0,
        parallel: true,
        ..Default::default()
    };
    // Every member of block 2 proposes to leave.
    let profiles = Profiles::from_gene_sums(&rows, SIM_M);
    let stats = DcPoissonStats::from_profiles_with(&profiles, 3, &start, &params);
    let mut lp = vec![0f64; 3];
    for e in 60..72 {
        compute_log_probs_restricted(e, &stats, &profiles, &[0, 1, 2], &mut lp);
        assert_ne!(argmax_log_staying(&lp, &[0, 1, 2], 2), 2, "entity {e}");
    }
    let mut labels = start.clone();
    let moves = sim_refine(&rows, &mut labels, 3, &params);
    let left: Vec<usize> = (60..72).filter(|&e| labels[e] == 2).collect();
    assert_eq!(moves, 12 - left.len());
    assert!(left.len() >= 2, "part of one half stays: {left:?}");
    assert!(
        left.iter().all(|&e| e < 66) || left.iter().all(|&e| e >= 66),
        "what stays is of one profile: {left:?}"
    );
}

#[test]
fn zero_prior_count_scores_stay_finite() {
    // `prior_count = 0` is the floor under the log and nothing more: scores
    // are finite, empty blocks included, and distinct profiles are still
    // told apart.
    let k = 4;
    let mut rng = SmallRng::seed_from_u64(100);
    let mut rows = Vec::new();
    let mut truth = Vec::new();
    for b in 0..3 {
        let p = sim_profile(&mut rng, 0, SIM_M);
        for _ in 0..40 {
            rows.push(sim_entity(&mut rng, &p, 500.0));
            truth.push(b);
        }
    }
    let mut start = truth.clone();
    for e in (0..start.len()).step_by(5) {
        start[e] = (truth[e] + 1) % 3;
    }
    let profiles = Profiles::from_gene_sums(&rows, SIM_M);
    for parallel in [true, false] {
        let params = RefineParams {
            prior_count: 0.0,
            parallel,
            ..Default::default()
        };
        let stats = DcPoissonStats::from_profiles_with(&profiles, k, &start, &params);
        let all: Vec<usize> = (0..k).collect();
        let mut lp = vec![0f64; k];
        for e in 0..rows.len() {
            compute_log_probs_restricted(e, &stats, &profiles, &all, &mut lp);
            assert!(lp.iter().all(|x| x.is_finite()), "entity {e}: {lp:?}");
        }
        let mut labels = start.clone();
        sim_refine(&rows, &mut labels, k, &params);
        assert!(labels.iter().all(|&l| l < k));
        let right = labels.iter().zip(&truth).filter(|(a, b)| a == b).count();
        assert!(right >= truth.len() - 2, "{right} of {} right", truth.len());
    }
}

#[test]
fn labels_do_not_depend_on_the_thread_count() {
    // Eight blocks of forty, a fifth mislabelled. Every third entity is
    // given a candidate list without its current block, which leaves that
    // block's slot of the score buffer to whatever a thread scored last.
    let k = 8;
    let mut rng = SmallRng::seed_from_u64(110);
    let mut rows = Vec::new();
    let mut start = Vec::new();
    let base = sim_profile(&mut rng, 0, SIM_M);
    for b in 0..k {
        let p = sim_perturbed(&mut rng, &base);
        for _ in 0..40 {
            rows.push(sim_entity(&mut rng, &p, 400.0));
            start.push(if rng.random::<f64>() < 0.2 {
                (b + 1 + rng.random_range(0..k - 1)) % k
            } else {
                b
            });
        }
    }
    let mut profiles = Profiles::from_gene_sums(&rows, SIM_M);
    profiles.apply_feature_weighting(FeatureWeighting::FisherInfoNb);
    let candidates: Vec<Vec<usize>> = (0..rows.len())
        .map(|e| {
            (0..k)
                .filter(|&b| e % 3 != 0 || b != start[e])
                .collect::<Vec<usize>>()
        })
        .collect();
    for parallel in [true, false] {
        let params = RefineParams {
            parallel,
            ..Default::default()
        };
        let on_threads = |threads: usize| {
            rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .expect("thread pool")
                .install(|| {
                    let ctx = RefineContext {
                        profiles: &profiles,
                        k,
                        params: &params,
                        level_label: "test",
                    };
                    let mut labels = start.clone();
                    let mut rng = SmallRng::seed_from_u64(params.seed);
                    let moves = refine_with_candidates(&mut labels, &candidates, &mut rng, &ctx);
                    (labels, moves)
                })
        };
        let one = on_threads(1);
        assert!(one.1 > 0, "the fixture has moves to make");
        for threads in [3, 6] {
            assert_eq!(
                on_threads(threads),
                one,
                "{threads} threads, parallel={parallel}"
            );
        }
    }
}

/////////////////////////////////////////////
// Many features, most of them rarely seen //
/////////////////////////////////////////////

/// Lognormal(0, `sd`) rates on `m` features, normalised to one.
fn heavy_tailed_profile(rng: &mut SmallRng, m: usize, sd: f64) -> Vec<f64> {
    let ln = LogNormal::new(0.0, sd).unwrap();
    let mut p: Vec<f64> = (0..m).map(|_| ln.sample(rng)).collect();
    let s: f64 = p.iter().sum();
    p.iter_mut().for_each(|x| *x /= s);
    p
}

/// Blocks 0 (`n_small` entities) and 1 (`n_big`) of one heavy-tailed
/// profile over 20,000 features, `N_OUT` further entities of the same
/// profile in block 2, and `n_rest` more in block 3. Returns, for each
/// entity of block 2, its score for the small block less its score for
/// the large one, and the charge on a move between the two.
fn size_gap(n_small: usize, n_big: usize, n_rest: usize, depth: f64, seed: u64) -> Vec<(f64, f64)> {
    const N_OUT: usize = 24;
    let m = 20_000;
    let mut rng = SmallRng::seed_from_u64(seed);
    let p = heavy_tailed_profile(&mut rng, m, 2.0);
    let mut labels = vec![0; n_small];
    labels.extend(vec![1; n_big]);
    labels.extend(vec![2; N_OUT]);
    labels.extend(vec![3; n_rest]);
    let rows: Vec<Vec<(usize, f32)>> = labels
        .iter()
        .map(|_| sim_entity(&mut rng, &p, depth))
        .collect();
    let profiles = Profiles::from_gene_sums(&rows, m);
    let flat = RefineParams {
        min_gain_z: 0.0,
        ..Default::default()
    };
    let mut raw = DcPoissonStats::from_profiles_with(&profiles, 4, &labels, &flat);
    let mut charged = DcPoissonStats::from_profiles(&profiles, 4, &labels);
    let mut lp = vec![0f64; 4];
    (n_small + n_big..n_small + n_big + N_OUT)
        .map(|e| {
            // As a member of the small block, the entity's leave-one-out
            // score there is its score against the block as an outsider.
            raw.delta_move(e, 2, 0, &profiles);
            charged.delta_move(e, 2, 0, &profiles);
            compute_log_probs_restricted(e, &raw, &profiles, &[0, 1], &mut lp);
            let gap = lp[0] - lp[1];
            compute_log_probs_restricted(e, &charged, &profiles, &[0, 1], &mut lp);
            let charge = (lp[0] - lp[1]) - gap;
            raw.delta_move(e, 0, 2, &profiles);
            charged.delta_move(e, 0, 2, &profiles);
            (gap, charge)
        })
        .collect()
}

#[test]
fn blocks_of_one_profile_neither_attract_nor_repel_by_size() {
    // Most of 20,000 heavy-tailed features have a total of zero or one in
    // the small block. The score difference between the two blocks stays
    // within the charge a move has to clear, whichever way. (The two
    // blocks are a quarter of the entities: a block that makes up most of
    // the pooled profile is its own null, and is favoured for it.)
    for (n_small, n_big, depth) in [(6, 60, 3000.0), (3, 50, 1000.0)] {
        let gaps = size_gap(n_small, n_big, 200, depth, 120);
        let n = gaps.len() as f64;
        let mean_gap = gaps.iter().map(|g| g.0).sum::<f64>() / n;
        let mean_charge = gaps.iter().map(|g| g.1).sum::<f64>() / n;
        let beyond = gaps.iter().filter(|g| g.0.abs() > g.1).count();
        assert!(mean_charge > 0.0);
        assert!(
            mean_gap.abs() < 0.5 * mean_charge,
            "{n_small} vs {n_big}: mean gap {mean_gap:.1}, charge {mean_charge:.1}"
        );
        assert!(
            beyond as f64 <= 0.25 * n,
            "{n_small} vs {n_big}: {beyond} of {n} entities beyond the charge"
        );
    }
}

#[test]
fn rate_classes_reproduce_the_score_feature_by_feature() {
    // The null means are kept on a grid of pooled rates. Against the same
    // score with every feature's null mean taken at its own rate, the grid
    // costs a small fraction of a nat per thousand counts.
    let m = 4000;
    let mut rng = SmallRng::seed_from_u64(130);
    let p = heavy_tailed_profile(&mut rng, m, 2.0);
    let labels: Vec<usize> = (0..60).map(|e| [0, 1, 1, 2, 2, 2][e % 6]).collect();
    let rows: Vec<Vec<(usize, f32)>> = labels
        .iter()
        .map(|_| sim_entity(&mut rng, &p, 1500.0))
        .collect();
    let profiles = Profiles::from_gene_sums(&rows, m);
    let flat = RefineParams {
        min_gain_z: 0.0,
        ..Default::default()
    };
    let stats = DcPoissonStats::from_profiles_with(&profiles, 3, &labels, &flat);
    let mut pooled = vec![0f64; m];
    for row in &profiles.rows {
        for &(g, v) in row {
            pooled[g as usize] += v as f64;
        }
    }
    let total: f64 = pooled.iter().sum();
    let mut lp = vec![0f64; 3];
    let mut worst = 0f64;
    for (e, row) in profiles.rows.iter().enumerate() {
        compute_log_probs_restricted(e, &stats, &profiles, &[0, 1, 2], &mut lp);
        let sf = profiles.size_factor[e] as f64;
        for (k, &score) in lp.iter().enumerate() {
            let own = usize::from(k == labels[e]) as f64;
            let size = stats.size_sum[k] - own * sf;
            let exact: f64 = row
                .iter()
                .map(|&(g, v)| {
                    let v = v as f64;
                    let total_k = stats.gene_sum[stats.idx(k, g as usize)] - own * v;
                    let rate = (pooled[g as usize] - v) / (total - sf);
                    let mu = size * rate;
                    v * ((total_k + stats.prior).ln() - stats.null.at(mu.ln(), mu).0)
                })
                .sum();
            worst = worst.max((score - exact).abs() / sf);
        }
    }
    assert!(worst < 2e-4, "{worst} nats per count");
}

////////////////////
// Batch factors  //
////////////////////

/// Mean profile (normalised to one) of the entities `pick` selects.
fn mean_profile(profiles: &Profiles, pick: impl Fn(usize) -> bool) -> Vec<f64> {
    let mut sum = vec![0f64; profiles.num_features];
    for (e, row) in profiles.rows.iter().enumerate() {
        if pick(e) {
            for &(g, v) in row {
                sum[g as usize] += v as f64;
            }
        }
    }
    let s: f64 = sum.iter().sum();
    sum.iter_mut().for_each(|x| *x /= s);
    sum
}

fn l1(a: &[f64], b: &[f64]) -> f64 {
    a.iter().zip(b).map(|(x, y)| (x - y).abs()).sum()
}

#[test]
fn rescaling_removes_a_batch_factor() {
    // Batch 1 doubles the even features. After rescaling, entities of the
    // two batches drawn from one profile have the same expected profile.
    let mut rng = SmallRng::seed_from_u64(70);
    let p = sim_profile(&mut rng, 0, SIM_M);
    let mut q = p.clone();
    q.iter_mut().step_by(2).for_each(|x| *x *= 2.0);
    let s: f64 = q.iter().sum();
    q.iter_mut().for_each(|x| *x /= s);
    let mut rows = Vec::new();
    let mut batch = Vec::new();
    for e in 0..200 {
        let b = e % 2;
        rows.push(sim_entity(&mut rng, if b == 0 { &p } else { &q }, 2000.0));
        batch.push(b);
    }
    let plain = Profiles::from_gene_sums(&rows, SIM_M);
    let apart = l1(
        &mean_profile(&plain, |e| batch[e] == 0),
        &mean_profile(&plain, |e| batch[e] == 1),
    );
    let mut profiles = Profiles::from_gene_sums(&rows, SIM_M);
    profiles.rescale_batches(&batch, None);
    let together = l1(
        &mean_profile(&profiles, |e| batch[e] == 0),
        &mean_profile(&profiles, |e| batch[e] == 1),
    );
    assert!(apart > 0.3, "the batches start apart: {apart}");
    assert!(together < 0.1 * apart, "{apart} -> {together}");
    for (row, &sf) in profiles.rows.iter().zip(&profiles.size_factor) {
        let sum: f32 = row.iter().map(|&(_, v)| v).sum();
        assert!((sum - sf).abs() <= 1e-3 * sf);
    }
    // One batch: nothing to remove.
    let mut single = Profiles::from_gene_sums(&rows, SIM_M);
    single.rescale_batches(&vec![0; rows.len()], None);
    assert_eq!(single.rows, plain.rows);
    assert_eq!(single.size_factor, plain.size_factor);
}

#[test]
fn a_stray_count_and_a_lone_entity_are_left_nearly_as_they_are() {
    // Feature 0: a thousand counts in batch 0, one stray count in batch 1.
    // Feature 1: five counts in batch 0, one in batch 1. Feature 2 carries
    // the bulk of every entity. Batch 2 is a single entity with a profile
    // of its own.
    let mut rows: Vec<Vec<(usize, f32)>> = Vec::new();
    let mut batch = Vec::new();
    for e in 0..40 {
        let b = e % 2;
        let mut row = vec![(2usize, 500.0f32), (3, 100.0 + (e % 7) as f32)];
        if b == 0 {
            row.push((0, 50.0));
        }
        if e < 10 && b == 0 {
            row.push((1, 1.0));
        }
        rows.push(row);
        batch.push(b);
    }
    rows[1].push((0, 1.0));
    rows[3].push((1, 1.0));
    rows.push(vec![(2, 10.0), (3, 700.0), (4, 90.0)]);
    batch.push(2);
    let plain = Profiles::from_gene_sums(&rows, 5);
    let mut profiles = Profiles::from_gene_sums(&rows, 5);
    profiles.rescale_batches(&batch, None);
    let value = |p: &Profiles, e: usize, g: u32| {
        p.rows[e]
            .iter()
            .find(|&&(f, _)| f == g)
            .map_or(0.0, |&(_, v)| v)
    };
    // The stray count: the raw ratio of pooled to batch rate is in the
    // hundreds; the factor is bounded.
    let stray = value(&profiles, 1, 0);
    assert!(
        (1.0..=BATCH_FACTOR_BOUND).contains(&stray),
        "stray count became {stray}"
    );
    // Thin evidence either way: nearly untouched.
    let thin = value(&profiles, 3, 1);
    assert!((0.8..=1.25).contains(&thin), "thin count became {thin}");
    // The lone entity of batch 2 keeps its profile exactly.
    let lone = rows.len() - 1;
    assert_eq!(profiles.rows[lone], plain.rows[lone]);
    assert_eq!(profiles.size_factor[lone], plain.size_factor[lone]);
}

#[test]
fn strata_keep_a_type_alike_across_batches_of_unequal_composition() {
    // Two types; batch 0 is nine tenths type 0, batch 1 one tenth, and
    // batch 1 doubles the even features. Compared with the pooled profile,
    // batch 0 looks like "too much of type 0" and the fit scales the
    // type's features down there and up in batch 1: the type no longer
    // matches itself across the batches. Compared within the types, only
    // the doubling is taken out.
    let mut rng = SmallRng::seed_from_u64(140);
    let types = [
        sim_profile(&mut rng, 0, SIM_M),
        sim_profile(&mut rng, 0, SIM_M),
    ];
    let doubled: Vec<Vec<f64>> = types
        .iter()
        .map(|p| {
            let mut q = p.clone();
            q.iter_mut().step_by(2).for_each(|x| *x *= 2.0);
            let s: f64 = q.iter().sum();
            q.iter_mut().for_each(|x| *x /= s);
            q
        })
        .collect();
    let mut rows = Vec::new();
    let mut batch = Vec::new();
    let mut ty = Vec::new();
    for e in 0..400 {
        let b = e % 2;
        let t = usize::from((e / 2) % 10 == 0) ^ b;
        rows.push(sim_entity(
            &mut rng,
            if b == 0 { &types[t] } else { &doubled[t] },
            2000.0,
        ));
        batch.push(b);
        ty.push(t);
    }
    assert_eq!(
        (0..400).filter(|&e| batch[e] == 0 && ty[e] == 0).count(),
        180
    );
    assert_eq!(
        (0..400).filter(|&e| batch[e] == 1 && ty[e] == 0).count(),
        20
    );
    // Distance between the two batches' entities of one type, summed over
    // the types.
    let apart = |p: &Profiles| -> f64 {
        (0..2)
            .map(|t| {
                l1(
                    &mean_profile(p, |e| ty[e] == t && batch[e] == 0),
                    &mean_profile(p, |e| ty[e] == t && batch[e] == 1),
                )
            })
            .sum()
    };
    let raw = apart(&Profiles::from_gene_sums(&rows, SIM_M));
    let mut pooled = Profiles::from_gene_sums(&rows, SIM_M);
    pooled.rescale_batches(&batch, None);
    let mut within = Profiles::from_gene_sums(&rows, SIM_M);
    within.rescale_batches(&batch, Some(&ty));
    let (pooled, within) = (apart(&pooled), apart(&within));
    assert!(raw > 0.6, "the doubling sets the batches apart: {raw}");
    assert!(
        within < 0.3 * raw,
        "within the types the factor is removed: {raw} -> {within}"
    );
    assert!(
        pooled > raw,
        "against the pool the composition is taken for a factor: {raw} -> {pooled}"
    );
}

/// An entity's rates are taken without it, so one that holds most of the
/// mass sees the others' rates at up to `max_pooled / (T − s_e)`, well above
/// `max_pooled / T`. The top rate class has to cover them, or they are
/// clamped into it and read at a lower rate than they have.
#[test]
fn leave_one_out_rates_fall_inside_the_rate_classes() {
    // Entity 0 holds most of the mass, little of it on feature 0, the
    // largest pooled feature: without entity 0, feature 0 has rate 700/700,
    // where 2 · max_pooled / T is 1402/1901.
    let gene_sums = vec![
        vec![(0usize, 1.0f32), (1, 600.0), (2, 600.0)],
        vec![(0, 350.0)],
        vec![(0, 350.0)],
    ];
    let profiles = make_profiles(&gene_sums, 3);
    let stats = DcPoissonStats::from_profiles(&profiles, 2, &[0, 1, 1]);
    let pooled = [701.0f64, 600.0, 600.0];
    let total = 1901.0f64;
    for (e, row) in gene_sums.iter().enumerate() {
        let rest = total - profiles.size_factor[e] as f64;
        for &(g, v) in row {
            let rate = (pooled[g] - v as f64) / rest;
            assert!(
                rate.ln() <= stats.classes.top + 1e-12,
                "entity {e} feature {g}: rate {rate} above the top class {}",
                stats.classes.top.exp()
            );
        }
    }
}