Skip to content

Commit

Permalink
Test get_idx_by_id and fix panic when not found
Browse files Browse the repository at this point in the history
Added a test for the det_idx_by_id function in the tree.
Changed function signature to return Result with the idx if the id
exists and an Error otherwise.
  • Loading branch information
junniest committed Nov 16, 2023
1 parent 42e6983 commit a8db4b0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
44 changes: 22 additions & 22 deletions phylo/src/pip_model/pip_model_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,42 +395,42 @@ fn pip_hky_likelihood_example_leaf_values() {
};
let iota = 0.133;
let beta = 0.787;
cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").unwrap().into());
assert_values(
&cost,
cost.info.tree.get_idx_by_id("A").into(),
cost.info.tree.get_idx_by_id("A").unwrap().into(),
iota,
beta,
&[[0.0, 1.0, 0.0, 0.0], [0.0; 4], [0.0; 4]].concat(),
&[0.0, 0.33, 0.0, 0.0],
&[0.0, 0.33 * iota * beta, 0.0, 0.0],
);
cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").unwrap().into());
assert_values(
&cost,
cost.info.tree.get_idx_by_id("B").into(),
cost.info.tree.get_idx_by_id("B").unwrap().into(),
iota,
beta,
&[[1.0, 1.0, 0.0, 0.0], [0.0; 4], [0.0; 4]].concat(),
&[0.26, 0.33, 0.0, 0.0],
&[0.26 * iota * beta, 0.33 * iota * beta, 0.0, 0.0],
);
cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").unwrap().into());
let iota = 0.067;
let beta = 0.885;
assert_values(
&cost,
cost.info.tree.get_idx_by_id("C").into(),
cost.info.tree.get_idx_by_id("C").unwrap().into(),
iota,
beta,
&[[0.0, 1.0, 0.0, 1.0], [0.0; 4], [0.0; 4]].concat(),
&[0.0, 0.33, 0.0, 0.19],
&[0.0, 0.33 * iota * beta, 0.0, 0.19 * iota * beta],
);
cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").unwrap().into());
assert_values(
&cost,
cost.info.tree.get_idx_by_id("D").into(),
cost.info.tree.get_idx_by_id("D").unwrap().into(),
iota,
beta,
&[[0.0, 1.0, 1.0, 1.0], [0.0; 4], [0.0; 4]].concat(),
Expand Down Expand Up @@ -460,7 +460,7 @@ fn pip_hky_likelihood_example_internals() {
}
let iota = 0.133;
let beta = 0.787;
let idx = usize::from(cost.info.tree.get_idx_by_id("E"));
let idx = usize::from(cost.info.tree.get_idx_by_id("E").unwrap());
cost.set_internal_values(idx);
assert_values(
&cost,
Expand All @@ -476,7 +476,7 @@ fn pip_hky_likelihood_example_internals() {
0.0,
],
);
let idx = usize::from(cost.info.tree.get_idx_by_id("F"));
let idx = usize::from(cost.info.tree.get_idx_by_id("F").unwrap());
cost.set_internal_values(idx);
let iota_f = 0.2;
let beta_f = 0.704;
Expand All @@ -501,7 +501,7 @@ fn pip_hky_likelihood_example_internals() {
let beta = 1.0;
let iota_e = 0.133;
let beta_e = 0.787;
let idx = usize::from(cost.info.tree.get_idx_by_id("R"));
let idx = usize::from(cost.info.tree.get_idx_by_id("R").unwrap());
cost.set_root_values(idx);
assert_values(
&cost,
Expand Down Expand Up @@ -561,39 +561,39 @@ fn pip_hky_likelihood_example_c0() {
model,
tmp: temp_values,
};
cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("A").unwrap().into());
assert_c0_values(
&cost,
cost.info.tree.get_idx_by_id("A").into(),
cost.info.tree.get_idx_by_id("A").unwrap().into(),
&[0.0, 0.0, 0.0, 0.0, 1.0],
0.0,
0.028329,
);
cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("B").unwrap().into());
assert_c0_values(
&cost,
cost.info.tree.get_idx_by_id("B").into(),
cost.info.tree.get_idx_by_id("B").unwrap().into(),
&[0.0, 0.0, 0.0, 0.0, 1.0],
0.0,
0.028329,
);
cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("C").unwrap().into());
assert_c0_values(
&cost,
cost.info.tree.get_idx_by_id("C").into(),
cost.info.tree.get_idx_by_id("C").unwrap().into(),
&[0.0, 0.0, 0.0, 0.0, 1.0],
0.0,
0.007705,
);
cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").into());
cost.set_leaf_values(cost.info.tree.get_idx_by_id("D").unwrap().into());
assert_c0_values(
&cost,
cost.info.tree.get_idx_by_id("D").into(),
cost.info.tree.get_idx_by_id("D").unwrap().into(),
&[0.0, 0.0, 0.0, 0.0, 1.0],
0.0,
0.007705,
);
let idx = usize::from(cost.info.tree.get_idx_by_id("E"));
let idx = usize::from(cost.info.tree.get_idx_by_id("E").unwrap());
cost.set_internal_values(idx);
assert_c0_values(
&cost,
Expand All @@ -602,7 +602,7 @@ fn pip_hky_likelihood_example_c0() {
0.154,
0.044448334 + 0.028329 * 2.0,
);
let idx = usize::from(cost.info.tree.get_idx_by_id("F"));
let idx = usize::from(cost.info.tree.get_idx_by_id("F").unwrap());
cost.set_internal_values(idx);
assert_c0_values(
&cost,
Expand All @@ -611,7 +611,7 @@ fn pip_hky_likelihood_example_c0() {
0.0488,
0.06607104 + 0.007705 * 2.0,
);
let idx = usize::from(cost.info.tree.get_idx_by_id("R"));
let idx = usize::from(cost.info.tree.get_idx_by_id("R").unwrap());
cost.set_root_values(idx);
assert_c0_values(
&cost,
Expand Down
8 changes: 4 additions & 4 deletions phylo/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,17 @@ impl Tree {
lengths
}

pub fn get_idx_by_id(&self, id: &str) -> NodeIdx {
pub fn get_idx_by_id(&self, id: &str) -> Result<NodeIdx> {
debug_assert!(self.complete);
let idx = self.leaves.iter().position(|node| node.id == id);
if let Some(idx) = idx {
return NodeIdx::Leaf(idx);
return Ok(NodeIdx::Leaf(idx));
}
let idx = self.internals.iter().position(|node| node.id == id);
if let Some(idx) = idx {
return NodeIdx::Internal(idx);
return Ok(NodeIdx::Internal(idx));
}
panic!("No node with id {} found in the tree", id);
bail!("No node with id {} found in the tree", id);
}
}

Expand Down
24 changes: 24 additions & 0 deletions phylo/src/tree/tree_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ fn setup_test_tree() -> Tree {
tree
}

#[test]
fn get_idx_by_id() {
let tree = from_newick_string(&String::from(
"(((A:1.0,B:1.0)E:2.0,C:1.0)F:1.0,D:1.0)G:2.0;",
))
.unwrap()
.pop()
.unwrap();
let nodes = [
("A", L(0)),
("B", L(1)),
("C", L(2)),
("D", L(3)),
("E", I(2)),
("F", I(1)),
("G", I(0)),
];
for (id, idx) in nodes.iter() {
assert!(tree.get_idx_by_id(id).is_ok());
assert_eq!(tree.get_idx_by_id(id).unwrap(), *idx);
}
assert!(tree.get_idx_by_id("H").is_err());
}

#[test]
fn subroot_preorder() {
let tree = setup_test_tree();
Expand Down

0 comments on commit a8db4b0

Please sign in to comment.