greedy_variants_test.py
High-level description
This file contains unit tests for the SpectralGreedySolver
and MaxCutGreedySolver
classes, which are variants of the greedy solver algorithm used in the Cassiopeia project for phylogenetic tree reconstruction. The tests verify the correctness of the solvers’ behavior in various scenarios, including handling of sparse data, base cases, and different weighting schemes.
Code Structure
The main class GreedyVariantsTest
contains multiple test methods, each focusing on a specific aspect of the solvers’ functionality. The tests create sample character matrices, instantiate solver objects, and compare the resulting tree structures against expected outcomes.
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 graph |
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
- Calculate the number of common ancestors for each pair
- Determine the structure based on which pair has the most common ancestors
GreedyVariantsTest
Description
A test class containing multiple test methods for the SpectralGreedySolver
and MaxCutGreedySolver
classes.
Test Methods
test_raises_error_on_ambiguous
: Verifies that the solver raises an error for ambiguous data.test_spectral_sparse_case
: Tests theSpectralGreedySolver
with sparse data.test_spectral_base_case
: Tests theSpectralGreedySolver
with a base case scenario.test_spectral_base_case_weights_almost_one
: Tests theSpectralGreedySolver
with weights close to 1.test_maxcut_base_case
: Tests theMaxCutGreedySolver
with a base case scenario.test_maxcut_base_case_weights_trivial
: Tests theMaxCutGreedySolver
with trivial weights.
Internal Logic
Each test method follows a similar pattern:
- Create a character matrix
- Instantiate a solver object
- Solve the tree
- Compare the resulting tree structure against expected outcomes using triplet structures
Dependencies
- unittest
- itertools
- networkx
- pandas
- cassiopeia
Error Handling
The tests use assertRaises
to check for expected errors and assertEqual
to compare results against expected values.
Notes
- The tests focus on verifying the correctness of the solvers’ output rather than performance optimization.
- The use of triplet structures for comparison allows for flexible testing of tree topologies.
- The tests cover various scenarios, including sparse data and different weighting schemes, to ensure robust solver behavior.