test/solver_tests/percolation_test.py:
High-level description
This file contains unit tests for the PercolationSolver in Cassiopeia’s solver module. It tests the solver’s ability to construct phylogenetic trees from character matrices using different joining solvers and dissimilarity functions.Code Structure
The main classPercolationSolverTest inherits from unittest.TestCase and contains several test methods. These methods create character matrices, solve them using different configurations of the PercolationSolver, and compare the resulting trees to expected structures.
Symbols
find_triplet_structure
Description
This function determines the structure of a triplet of nodes in a given tree.Inputs
| Name | Type | Description |
|---|---|---|
| triplet | tuple | A tuple of three node labels |
| T | networkx.DiGraph | The tree to analyze |
Outputs
| Name | Type | Description |
|---|---|---|
| structure | str | The structure of the triplet (“ab”, “ac”, “bc”, or ”-“) |
Internal Logic
- Find ancestors for each node in the triplet.
- Count common ancestors between pairs of nodes.
- Determine the structure based on which pair has the most common ancestors.
neg_hamming_similarity_without_missing
Description
This function computes the negative Hamming similarity between two sequences, ignoring missing data.Inputs
| Name | Type | Description |
|---|---|---|
| s1 | np.array | First sequence |
| s2 | np.array | Second sequence |
| missing_state_indicator | int | Value indicating missing data |
| weights | Optional[Dict[int, Dict[int, float]]] | Optional weights for states |
Outputs
| Name | Type | Description |
|---|---|---|
| similarity | float | Negative Hamming similarity |
class PercolationSolverTest(unittest.TestCase)
Description
This class contains unit tests for the PercolationSolver.Test Methods
test_NJ_negative_similarity
Tests the PercolationSolver using NeighborJoiningSolver with negative Hamming similarity.
test_NJ_weighted_hamming_distance
Tests the PercolationSolver using NeighborJoiningSolver with weighted Hamming distance.
test_Greedy
Tests the PercolationSolver using VanillaGreedySolver.
test_priors_case
Tests the PercolationSolver with prior probabilities for mutations.
Internal Logic
For each test:- Create a character matrix.
- Initialize a CassiopeiaTree with the character matrix.
- Set up a joining solver (NeighborJoiningSolver or VanillaGreedySolver).
- Create a PercolationSolver with the joining solver.
- Solve the tree.
- Compare the resulting tree structure with the expected structure using triplet comparisons.
Dependencies
| Dependency | Purpose |
|---|---|
| unittest | For creating and running unit tests |
| itertools | For generating combinations |
| networkx | For graph operations |
| numba | For JIT compilation of functions |
| numpy | For numerical operations |
| pandas | For data manipulation |
| cassiopeia | The main package being tested |
