neighborjoining_solver_test.py
Here’s a comprehensive documentation of the test/solver_tests/neighborjoining_solver_test.py
file:
High-level description
This file contains unit tests for the NeighborJoiningSolver in Cassiopeia’s solver module. It tests various aspects of the neighbor-joining algorithm implementation, including basic functionality, error correction, and handling of different input scenarios.
Code Structure
The main class TestNeighborJoiningSolver
inherits from unittest.TestCase
and contains multiple test methods. It uses mock objects and sample data to test different aspects of the NeighborJoiningSolver. The file also includes helper functions for creating test data and analyzing tree structures.
Symbols
find_triplet_structure
Description
This function determines the structure of a triplet of nodes in a tree.
Inputs
Name | Type | Description |
---|---|---|
triplet | tuple | A tuple of three node names |
T | networkx.DiGraph | The tree to analyze |
Outputs
Name | Type | Description |
---|---|---|
structure | str | The structure of the triplet (“ab”, “ac”, “bc”, or ”-“) |
Internal Logic
It compares the number of common ancestors between pairs of nodes to determine the closest pair.
delta_fn
Description
A dissimilarity function used by the solvers to compute distances between sequences.
Inputs
Name | Type | Description |
---|---|---|
x | np.array | First sequence |
y | np.array | Second sequence |
missing_state | int | Value representing missing data |
priors | Optional[Dict[int, Dict[int, float]]] | Prior probabilities |
Outputs
Name | Type | Description |
---|---|---|
d | int | The number of differences between the sequences |
TestNeighborJoiningSolver
Description
The main test class containing all the unit tests for the NeighborJoiningSolver.
Internal Logic
The setUp
method initializes various test scenarios, including:
- General Neighbor Joining
- Lineage Tracing Neighbor Joining
- Character Matrix with Duplicates
- Neighbor Joining with modified hamming dissimilarity
Each test method focuses on a specific aspect of the NeighborJoiningSolver:
test_constructor
: Tests the constructor and error handlingtest_compute_q
: Tests the Q-matrix computationtest_find_cherry
: Tests the cherry-picking steptest_update_dissimilarity_map
: Tests updating the dissimilarity maptest_basic_solver
: Tests the basic solver functionalitytest_nj_solver_weights
: Tests the solver with weighted distancestest_pp_solver
: Tests the solver with a different dissimilarity functiontest_duplicate_sample_neighbor_joining
: Tests handling of duplicate samplestest_setup_root_finder_missing_dissimilarity_map
: Tests root finding with missing dissimilarity maptest_setup_root_finder_existing_dissimilarity_map
: Tests root finding with existing dissimilarity map
Dependencies
Dependency | Purpose |
---|---|
unittest | For creating and running unit tests |
typing | For type hinting |
unittest.mock | For creating mock objects |
itertools | For generating combinations |
networkx | For graph operations |
numpy | For numerical operations |
pandas | For data manipulation |
cassiopeia | The main package being tested |
Error Handling
The tests use assertRaises
to check if the solver raises appropriate exceptions in error cases.
Performance Considerations
The tests don’t explicitly address performance, but they do test the correctness of the algorithm on various input sizes and scenarios.
This documentation provides a comprehensive overview of the test file, its structure, and the various aspects of the NeighborJoiningSolver that it tests. The file ensures that the solver works correctly for different input scenarios and handles edge cases appropriately.