Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf/loop over kids first #297

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions packages/treetime/src/commands/ancestral/fitch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(dead_code)]

use crate::alphabet::alphabet::{FILL_CHAR, NON_CHAR, VARIABLE_CHAR};
use crate::graph::breadth_first::GraphTraversalContinuation;
use crate::io::fasta::FastaRecord;
Expand Down Expand Up @@ -165,38 +164,32 @@ fn fitch_backwards(graph: &SparseGraph, sparse_partitions: &[PartitionParsimony]
}
}

// Process all positions where the children are fixed or completely unknown in some children.
for (pos, parent_state) in sequence.iter_mut().enumerate() {
if *parent_state != FILL_CHAR {
continue;
}

let mut determined_states = Vec::with_capacity(alphabet.n_chars());
for &(child, _) in &children {
let state = child.sequence[pos];
if !determined_states.contains(&state) && alphabet.is_canonical(state) {
determined_states.push(state);
}
}

// Find the state of the current node at this position
*parent_state = match determined_states.as_slice() {
[state] => {
// All children have the same state, that will be the state of the current node
*state
}
[] => {
// No child states. Impossible
unreachable!("No child states. This is impossible");
// // Process all positions where the children are fixed or completely unknown in some children.
// loop over all children
for &(child, _) in &children {
for (pos, parent_state) in sequence.iter_mut().enumerate() {
let cstate = child.sequence[pos];
if *parent_state == cstate || *parent_state == NON_CHAR {
continue; // if parent is equal to child state or we know its a non-char, skip
}
states => {
// Child states differ. This is variable state.
// Save child states and postpone the decision until forward pass.
let dis = states.iter().collect();
seq_dis.variable.insert(pos, dis);
VARIABLE_CHAR
if alphabet.is_canonical(cstate) {
if *parent_state == FILL_CHAR { // if cstate is canonical and parent is still FILL_STATE, set parent_state
*parent_state = cstate;
} else { // otherwise set or update the variable state
if seq_dis.variable.contains_key(&pos) {
seq_dis.variable.insert(
pos,
StateSet::from_union(&[seq_dis.variable[&pos], StateSet::from_char(cstate)]),
);
} else {
seq_dis
.variable
.insert(pos, StateSet::from_slice(&[*parent_state, cstate]));
}
*parent_state = VARIABLE_CHAR;
}
}
};
}
}

// Process insertions and deletions.
Expand Down
2 changes: 1 addition & 1 deletion packages/treetime/src/representation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod bitset128;
pub mod graph_dense;
pub mod graph_sparse;
pub mod infer_dense;
pub mod partitions_likelihood;
pub mod partitions_parsimony;
pub mod state_set;
pub mod bitset128;
2 changes: 1 addition & 1 deletion packages/treetime/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod assert;
pub mod openblas;
pub mod container;
pub mod datetime;
pub mod error;
Expand All @@ -8,6 +7,7 @@ pub mod global_init;
pub mod interval;
pub mod manyzip;
pub mod ndarray;
pub mod openblas;
pub mod random;
pub mod string;
pub mod vec;