snj_solver_test.py
Here’s a detailed explanation of the test/solver_tests/snj_solver_test.py
file:
High-level description
This file contains unit tests for the SpectralNeighborJoiningSolver in Cassiopeia’s solver module. It tests various aspects of the solver, including its constructor, core functionality, and integration with different types of phylogenetic trees.
Code Structure
The main class TestSpectralNeighborJoiningSolver
inherits from unittest.TestCase
and contains multiple test methods. The setUp
method initializes common test data and objects used across different tests. Helper functions like find_triplet_structure
and assertTripletCorrectness
are defined outside the class to assist in verifying tree structures.
Symbols
TestSpectralNeighborJoiningSolver
Description
This is the main test class that contains all the unit tests for the SpectralNeighborJoiningSolver.
Internal Logic
- Initializes test data in the
setUp
method. - Defines various test methods to check different aspects of the solver.
- Uses helper functions to verify tree structures.
find_triplet_structure
Description
A helper function that identifies the two nodes with the most similar ancestry in a triplet of nodes.
Inputs
Name | Type | Description |
---|---|---|
triplet | tuple | A tuple of three node names |
T | networkx.DiGraph | The tree containing the nodes |
Outputs
Name | Type | Description |
---|---|---|
structure | str | The structure of the triplet (“ab”, “ac”, “bc”, or ”-“) |
assertTripletCorrectness
Description
A helper method to check if two trees are isomorphic by comparing their triplet structures.
Inputs
Name | Type | Description |
---|---|---|
self | TestCase | The test case instance |
nodes | List[str] | List of leaf nodes to get triplets from |
expected_tree | networkx.DiGraph | The expected tree structure |
observed_tree | networkx.DiGraph | The observed tree structure |
Test Methods
test_constructor
Tests the constructor of the SpectralNeighborJoiningSolver and checks for proper initialization and error handling.
test_compute_svd2_pairwise
Tests the _compute_svd2
method for pairwise comparisons of subsets.
test_compute_svd2_N3
Tests the _compute_svd2
method when there are only 3 subsets left.
test_update_dissimilarity_map_base
Tests the update_dissimilarity_map
method for updating the lambda matrix.
test_update_dissimilarity_map_N3
Tests the update_dissimilarity_map
method when there are only 3 subsets left.
test_get_dissimilarity_map
Tests the get_dissimilarity_map
method for outputting a lambda matrix.
test_find_cherry
Tests the find_cherry
method for identifying the closest pair of nodes.
test_basic_solver
Tests the basic functionality of the solver on a root-specified input tree.
test_snj_solver_weights
Tests the solver with a perfect phylogenetic tree and priors.
test_pp_solver
Integration test for a Perfect Phylogenetic tree.
test_duplicate_sample
Tests the solving of a tree with duplicate leaves.
test_integration1
Integration test on a 7-leaf tree.
test_integration2
Integration test on a 10-leaf tree.
test_setup_root_finder_missing_dissimilarity_map
Checks that the root is still set despite missing dissimilarity map.
test_setup_root_finder_existing_dissimilarity_map
Checks that the root is still set with an existing dissimilarity map.
Dependencies
The test file imports and uses several modules and classes:
- Standard Python libraries: typing, itertools, unittest, mock
- Third-party libraries: networkx, numpy, pandas
- Cassiopeia modules: cassiopeia as cas
Error Handling
The tests use various assertions to check for expected behavior and raise exceptions when unexpected results occur.
This test suite provides comprehensive coverage of the SpectralNeighborJoiningSolver’s functionality, ensuring its correctness and robustness across different scenarios and tree structures.