Skip to content

Commit

Permalink
Tidying code and addressing clippy comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jhellewell14 committed Apr 5, 2024
1 parent 026a9ab commit 6d08fd9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 49 deletions.
36 changes: 10 additions & 26 deletions src/dspsa.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use rand::{distributions::Bernoulli, rngs::ThreadRng, Rng};
use rand::Rng;
use crate::Tree;

pub fn hill_peturb(mut v: &Vec<usize>, n: usize) -> Vec<usize> {
let mut vout = v.clone();
pub fn hill_peturb(v: &[usize], n: usize) -> Vec<usize> {
let mut vout = v.to_vec();
let mut rng = rand::thread_rng();
let mut ind_rng = rand::thread_rng();
let ind_rng = rand::thread_rng();
let distr = rand::distributions::Bernoulli::new(0.5).unwrap();
let ind_distr = rand::distributions::Uniform::new(0, v.len());

Expand All @@ -23,21 +23,6 @@ pub fn hill_peturb(mut v: &Vec<usize>, n: usize) -> Vec<usize> {
}
};

// let vout: Vec<usize> = v.iter().enumerate().map(|(i, el)|{
// let out = match rng.sample(distr) {
// true => el + 1,
// false => el - 1,
// };

// if i.eq(&0) || out.lt(&0) {
// 0
// } else if out.gt(&(2 * (i - 1))) {
// 2 * (i - 1)
// } else {
// out
// }
// }).collect();
// println!("{:?}", vout);
vout
}

Expand All @@ -53,25 +38,24 @@ impl Tree {
mutation_lists: self.mutation_lists.clone()
};

let n: usize = self.tree_vec.len();
let mut candidate_vec: Vec<usize>;
for k in 0..=iterations {

println!("Optimisation step {} out of {}", k, iterations);
println!("Old vector {:?}", self.tree_vec);
// println!("Old vector {:?}", self.tree_vec);
println!("Tree log likelihood: {}", self.get_tree_likelihood());

candidate_vec = hill_peturb(&self.tree_vec, 2);
candidate_vec = hill_peturb(&self.tree_vec, 28);
working_tree.update_quad(&candidate_vec);
working_tree.update_likelihood(&q);
working_tree.update_likelihood(q);

println!("New vector {:?}", candidate_vec);
// println!("New vector {:?}", candidate_vec);
println!("New likelihood {}", working_tree.get_tree_likelihood());

if(working_tree.get_tree_likelihood() > self.get_tree_likelihood()) {
if working_tree.get_tree_likelihood() > self.get_tree_likelihood() {
println!("Climbing hill!");
self.update_quad(&working_tree.tree_vec);
self.update_likelihood(&q);
self.update_likelihood(q);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/gen_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::Tree;
use needletail::*;
use rand::Rng;
use std::cmp::Ordering;

#[derive(Debug, Copy, Clone)]
pub struct Mutation(pub f64, pub f64, pub f64, pub f64);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn main() {
// println!("{:?}", tr.mutation_lists);

if !args.no_optimise {
tr.hillclimb(&q, 20);
tr.hillclimb(&q, 100);
}

// let end = Instant::now();
Expand Down
4 changes: 1 addition & 3 deletions src/likelihoods.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::f64::NEG_INFINITY;
use std::thread::current;
use crate::combine_lists;
use crate::Mutation;
use crate::Tree;
Expand Down Expand Up @@ -104,7 +102,7 @@ impl Mutation {
for i in 0..=3 {
x.push(logse(prob_matrix.row(i).iter().zip(&lnx).map(|(a, b)| a.ln() + b).collect()));
}
Mutation(*x.get(0).unwrap(), *x.get(1).unwrap(), *x.get(2).unwrap(), *x.get(3).unwrap())
Mutation(*x.first().unwrap(), *x.get(1).unwrap(), *x.get(2).unwrap(), *x.get(3).unwrap())
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/phylo2vec.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::os::unix::thread;

use crate::node::Node;
use crate::Tree;
use ndarray::*;
use rand::{seq::SliceRandom, thread_rng, Rng};
use cxx::let_cxx_string;

pub fn phylo2vec_quad(v: &Vec<usize>) -> Tree {
pub fn phylo2vec_quad(v: &[usize]) -> Tree {
let mut tree = Tree::new(v);
let mut sub_vec = tree.tree_vec.clone();
let mut sub_vec = v.to_vec();
sub_vec.remove(0);
let k = sub_vec.len();
let mut not_processed = [true].repeat(k);
Expand Down Expand Up @@ -191,7 +188,7 @@ impl Tree {
// }
// }

pub fn update_quad(&mut self, new_vec: &Vec<usize>) {
pub fn update_quad(&mut self, new_vec: &[usize]) {

let new_tree: Tree = phylo2vec_quad(new_vec);
let k: usize = new_tree.nodes.len();
Expand Down Expand Up @@ -230,6 +227,6 @@ pub mod ffi {
pub fn newick_to_vec(nw: &String, n_leaves: usize) -> Vec<usize>{
let_cxx_string!(nw_cpp = nw);
let x = ffi::doToVector(nw_cpp, n_leaves as i32, false);
let mut y: Vec<usize> = x.iter().map(|el| *el as usize).collect();
let y: Vec<usize> = x.iter().map(|el| *el as usize).collect();
y
}
21 changes: 11 additions & 10 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub struct Tree {
// UTILITY FUNCTIONS FOR ADDING, ACCESSING, AND MUTATING NODES AND DATA IN NODES
impl Tree {
// Constructor function for a new tree
pub fn new(tree_vec: &Vec<usize>) -> Tree {
pub fn new(tree_vec: &[usize]) -> Tree {
let k = tree_vec.len();
Tree {
tree_vec: tree_vec.clone(),
tree_vec: tree_vec.to_vec(),
nodes: vec![Node::default(); 2 * k + 1],
max_depth: 0,
leaf_permutations: (0..=k).collect(),
Expand Down Expand Up @@ -236,14 +236,15 @@ impl<'a> Tree {
};
if next_node.is_some() {
let n: usize = current_node.unwrap().depth - next_node.unwrap().depth;

if n == 0 {
newick.push(String::from(","));
} else if n > 0 {
for _ in 1..=n {
newick.push(String::from("("));
}
newick.push(String::from(","));

match n {
0 => {newick.push(String::from(","));},
_ => {
for _ in 1..=n {
newick.push(String::from("("));
}
newick.push(String::from(","));
},
}

newick.push(next_node.unwrap().branch_length.to_string());
Expand Down

0 comments on commit 6d08fd9

Please sign in to comment.