Skip to content

Commit

Permalink
Added Tree method to get all branch lengths in an nalgebra Vector so …
Browse files Browse the repository at this point in the history
…that it can be used with an optimiser
  • Loading branch information
jhellewell14 committed Oct 1, 2024
1 parent 0623118 commit 3468ab3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
22 changes: 1 addition & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ pub fn main() {
let args = cli_args();
let start = Instant::now();

// Trees initialise with a default rate matrix equal to this
// let q: na::Matrix4<f64> = na::Matrix4::new(
// -1.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// -1.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// -1.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// 1.0 / 3.0,
// -1.0,
// );

// let mut tr = vector_to_tree(&random_vector(4));
// tr.add_genetic_data(&String::from("/Users/joel/Downloads/listeria0.aln"));
let mut tr = vector_to_tree(&random_vector(27), &GTR::default());
Expand All @@ -55,7 +35,7 @@ pub fn main() {

if !args.no_optimise {
let start = Instant::now();
tr.hillclimb(5);
tr.hillclimb(1);
let end = Instant::now();

eprintln!("Done in {}s", end.duration_since(start).as_secs());
Expand Down
5 changes: 5 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ impl<T: RateMatrix> Tree<T> {
self.get_node(index).unwrap().branch_length
}

pub fn get_branchlengths(&self) -> nalgebra::Vector1<f64> {
let out: Vec<f64> = self.nodes.iter().map(|node| node.branch_length).collect();
nalgebra::Vector1::from_vec(out)
}

// Find maximum node depth
pub fn max_treedepth(&self) -> usize {
self.nodes.iter().map(|node| node.depth).max().unwrap_or(0)
Expand Down
39 changes: 20 additions & 19 deletions src/tree_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::thread::current;

use clap::builder::NonEmptyStringValueParser;
use ndarray::AssignElem;

Expand Down Expand Up @@ -108,29 +110,28 @@ impl<'a, T: RateMatrix> Tree<T> {

// Changes iterator
// #[derive(Debug)]
// pub struct ChangeOrder {
// nodevec: Option<Vec<usize>>,
// pub struct ChangeOrder<'a, T: RateMatrix> {
// tree: &'a mut Tree<T>,
// current_vec: Vec<usize>,
// }

// impl Iterator for ChangeOrder {
// impl<'a, T: RateMatrix> Iterator for ChangeOrder<'a, T> {
// type Item = &'a Node;
// Will changes be faster and easier using
// Vec<Option<Vec<usize>>> ?
// fn next(&mut self) -> Option<Self::Item> {
// if self.tree.changes.is_empty() {
// return None;
// }

// while !self.tree.changes.contains_key(&self.depth) {
// self.depth -= 1;
// fn next(&mut self) -> Option<Self::Item> {
// if self.tree.changes.is_empty() {
// return None
// } else if self.current_vec.is_empty() {
// let k = *self.tree.changes.keys().max().unwrap();
// self.current_vec = self.tree.changes.remove(&k).unwrap();
// let x = self.current_vec.pop().unwrap();
// let n: &'a Node = self.tree.get_node(x)?;
// return Some(n)
// } else {
// let x = self.current_vec.pop().unwrap();
// let n: Self::Item = self.tree.get_node(x)?;
// return Some(n)
// };
// }

// let x = self.tree.changes.remove(&self.depth).unwrap();
// let ni = x.pop();
// self.tree.changes.insert(self.depth, x);

// next_node
// }
// }

// impl<'a> Tree {
Expand Down

0 comments on commit 3468ab3

Please sign in to comment.