Implement masking logic for pairwise reduction

Introduces masking based on `free_loss` and `no_ambiguity` flags within pairwise algorithms. Updates reducer initialization and adds tests to verify cardinality calculation.
This commit is contained in:
Eric Coissac committed 2026-10-13 08:43:29 +02:00
1 parent 1702addbf9
commit cd06fa2ce3
3 files changed
+93 -11

No files matched your search

+2 -1
View File
@@ -278,7 +278,8 @@ pub fn run(args: PhyloArgs) {
let mut alignment_reducer = need_alignment.then(|| { let mut alignment_reducer = need_alignment.then(|| {
obikphylo::siblings::AlignmentReducer::new(&cache, &snp_exclude_mask, args.free_loss, args.no_ambiguity) obikphylo::siblings::AlignmentReducer::new(&cache, &snp_exclude_mask, args.free_loss, args.no_ambiguity)
}); });
let mut pairwise_reducer = need_pairwise.then(|| obikphylo::siblings::PairwiseReducer::new(n)); let mut pairwise_reducer =
need_pairwise.then(|| obikphylo::siblings::PairwiseReducer::new(n, args.free_loss, args.no_ambiguity));
if need_alignment || need_pairwise { if need_alignment || need_pairwise {
let mut reducers: Vec<&mut dyn obikphylo::siblings::LayerReducer> = Vec::new(); let mut reducers: Vec<&mut dyn obikphylo::siblings::LayerReducer> = Vec::new();
@@ -407,13 +407,29 @@ impl PartitionDispersion {
/// own docs for why the partition axis must be captured here rather than /// own docs for why the partition axis must be captured here rather than
/// after the fact) — the "reduce" half of the map/reduce /// after the fact) — the "reduce" half of the map/reduce
/// [`super::subsample::sample_index`] drives, self-contained (no /// [`super::subsample::sample_index`] drives, self-contained (no
/// sampling/masking logic of its own) so it can run alongside any other /// sampling logic of its own) so it can run alongside any other reduce
/// reduce over the same batch (see `algorithms::alignment::reduce_alignment`, /// over the same batch (see `algorithms::alignment::reduce_alignment`,
/// called from the same `on_layer` closure in /// run alongside this one whenever both an alignment and a tally are
/// `algorithms::sankoff::sankoff_bundle`). /// wanted from the same [`super::scan::sampled_scan`] call).
///
/// `free_loss`/`no_ambiguity`: the same masking
/// [`algorithms::alignment::reduce_alignment`] applies per genome via
/// `masking::masked_state`, applied here too so the tally agrees with
/// what the alignment actually shows for the same family/genome — used
/// directly for `cardinality` (a masked-away genome contributes `0`,
/// matching its `?` in the alignment, not its raw, pre-masking bit
/// count). `subst`/`same_all`/`same_variable` don't need to look at
/// `masked_state` separately: `single_form`'s own "exactly one bit"
/// requirement already agrees with "masked, then exactly one bit" for
/// every possible mask, for any combination of these two flags (masking
/// only ever turns an already-`None` case — 0 bits or ≥2 bits — into a
/// differently-shaped `None`, never a `Some` into a `None` or vice
/// versa), so routing them through `masked_state` too would be a no-op.
pub(crate) fn reduce_pairwise( pub(crate) fn reduce_pairwise(
survivors: &[SurvivingFamily], survivors: &[SurvivingFamily],
partition: usize, partition: usize,
free_loss: bool,
no_ambiguity: bool,
tally: &mut PairwiseTally, tally: &mut PairwiseTally,
dispersion: &mut PartitionDispersion, dispersion: &mut PartitionDispersion,
) { ) {
@@ -427,10 +443,14 @@ pub(crate) fn reduce_pairwise(
let m = genome_mask[g]; let m = genome_mask[g];
(m.count_ones() == 1).then(|| m.trailing_zeros() as u8) (m.count_ones() == 1).then(|| m.trailing_zeros() as u8)
}; };
let masked_cardinality = |g: usize| -> usize {
super::masking::masked_state(genome_mask[g], free_loss, no_ambiguity)
.map_or(0, |m| m.count_ones() as usize)
};
for i in 0..n_genomes { for i in 0..n_genomes {
let bi = single_form(i); let bi = single_form(i);
let card_i = genome_mask[i].count_ones() as usize; let card_i = masked_cardinality(i);
for j in (i + 1)..n_genomes { for j in (i + 1)..n_genomes {
let stats = tally.pair_mut(i, j); let stats = tally.pair_mut(i, j);
@@ -449,7 +469,7 @@ pub(crate) fn reduce_pairwise(
} }
if variable { if variable {
let card_j = genome_mask[j].count_ones() as usize; let card_j = masked_cardinality(j);
stats.cardinality[card_i][card_j] += 1; stats.cardinality[card_i][card_j] += 1;
if card_i != card_j { if card_i != card_j {
stats.cardinality[card_j][card_i] += 1; stats.cardinality[card_j][card_i] += 1;
@@ -471,11 +491,23 @@ pub(crate) fn reduce_pairwise(
pub struct PairwiseReducer { pub struct PairwiseReducer {
tally: PairwiseTally, tally: PairwiseTally,
dispersion: PartitionDispersion, dispersion: PartitionDispersion,
free_loss: bool,
no_ambiguity: bool,
} }
impl PairwiseReducer { impl PairwiseReducer {
pub fn new(n_genomes: usize) -> Self { /// `free_loss`/`no_ambiguity`: the same masking flags passed to
Self { tally: PairwiseTally::new(n_genomes), dispersion: PartitionDispersion::default() } /// `AlignmentReducer::new` — must match when both run over the same
/// [`super::scan::sampled_scan`] call, so the cardinality tally agrees
/// with what the alignment actually shows (see [`reduce_pairwise`]'s
/// own docs).
pub fn new(n_genomes: usize, free_loss: bool, no_ambiguity: bool) -> Self {
Self {
tally: PairwiseTally::new(n_genomes),
dispersion: PartitionDispersion::default(),
free_loss,
no_ambiguity,
}
} }
pub fn finish(self) -> (PairwiseTally, PartitionDispersion) { pub fn finish(self) -> (PairwiseTally, PartitionDispersion) {
@@ -485,7 +517,56 @@ impl PairwiseReducer {
impl super::scan::LayerReducer for PairwiseReducer { impl super::scan::LayerReducer for PairwiseReducer {
fn reduce(&mut self, partition: usize, _layer: usize, survivors: &[SurvivingFamily]) { fn reduce(&mut self, partition: usize, _layer: usize, survivors: &[SurvivingFamily]) {
reduce_pairwise(survivors, partition, &mut self.tally, &mut self.dispersion); reduce_pairwise(
survivors,
partition,
self.free_loss,
self.no_ambiguity,
&mut self.tally,
&mut self.dispersion,
);
}
}
#[cfg(test)]
mod cardinality_masking_tests {
use super::{PairwiseTally, PartitionDispersion, reduce_pairwise};
use crate::siblings::FamilyMask;
use crate::siblings::algorithms::SurvivingFamily;
// Genome 0 is ambiguous (carries both A and C, mask 0b0011 = cardinality
// 2); genome 1 carries only A (mask 0b0001 = cardinality 1). The family
// is variable (both A and C are observed somewhere), so it contributes
// to `cardinality` regardless of masking.
fn ambiguous_vs_single() -> Vec<SurvivingFamily> {
vec![SurvivingFamily {
family_idx: 0,
mask: FamilyMask::EMPTY.with(0).with(1), // A, C observed
genome_mask: vec![0b0011, 0b0001],
}]
}
#[test]
fn cardinality_uses_raw_mask_without_no_ambiguity() {
let mut tally = PairwiseTally::new(2);
let mut dispersion = PartitionDispersion::default();
reduce_pairwise(&ambiguous_vs_single(), 0, false, false, &mut tally, &mut dispersion);
let counts = tally.cardinality_tally(&[]).counts;
assert_eq!(counts[2][1], 1, "genome 0's raw cardinality (2) should be tallied");
assert_eq!(counts[0][1], 0, "no masking requested — nothing should collapse to 0");
}
#[test]
fn cardinality_masks_ambiguous_genome_to_zero_under_no_ambiguity() {
// Matches what `AlignmentReducer`/`masked_state` would show for
// genome 0 at this site under `--no-ambiguity`: `?`, i.e. no
// information — cardinality 0, not its raw 2.
let mut tally = PairwiseTally::new(2);
let mut dispersion = PartitionDispersion::default();
reduce_pairwise(&ambiguous_vs_single(), 0, false, true, &mut tally, &mut dispersion);
let counts = tally.cardinality_tally(&[]).counts;
assert_eq!(counts[2][1], 0, "raw cardinality must not leak through under --no-ambiguity");
assert_eq!(counts[0][1], 1, "the ambiguous genome should be masked to cardinality 0");
} }
} }
@@ -298,7 +298,7 @@ mod tests {
} }
let mut tally = PairwiseTally::new(2); let mut tally = PairwiseTally::new(2);
let mut dispersion = PartitionDispersion::default(); let mut dispersion = PartitionDispersion::default();
reduce_pairwise(&families, 0, &mut tally, &mut dispersion); reduce_pairwise(&families, 0, false, false, &mut tally, &mut dispersion);
tally tally
} }