spectral_test.py
Here’s a detailed documentation of the target file test/solver_tests/spectral_test.py
:
High-level description
This file contains unit tests for the SpectralSolver class and related utility functions in the Cassiopeia package. It tests various aspects of the spectral solver algorithm, including similarity calculations, graph construction, and the overall solving process.
Code Structure
The main class SpectralSolverTest
inherits from unittest.TestCase
and contains multiple test methods. These methods test different components of the SpectralSolver, such as similarity calculations, graph construction, and the complete solving process. The file also includes helper functions like find_triplet_structure
to assist in verifying the correctness of the generated trees.
Symbols
find_triplet_structure
Description
This function determines the structure of a triplet 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.
SpectralSolverTest
Description
This class contains unit tests for the SpectralSolver and related utilities.
Test Methods
-
test_similarity
- Tests the hamming similarity calculation without missing data.
-
test_similarity_weighted
- Tests the weighted hamming similarity calculation.
-
test_graph_construction
- Tests the construction of the similarity graph.
-
test_graph_construction_weighted
- Tests the construction of the weighted similarity graph.
-
test_hill_climb
- Tests the hill-climbing procedure for improving the spectral cut.
-
test_simple_base_case
- Tests the complete solving process on a simple character matrix.
-
test_simple_base_case_string
- Tests the solving process with string-based cell barcodes.
-
test_simple_base_case2
- Tests another simple base case for the solver.
-
test_simple_base_case2_priors
- Tests the solver with prior probabilities for mutations.
Dependencies
Dependency | Purpose |
---|---|
unittest | Provides the testing framework |
itertools | Used for generating combinations |
networkx | Used for graph operations |
pandas | Used for data manipulation |
cassiopeia | The main package being tested |
Error Handling
The tests use assertions provided by the unittest
framework to check for correctness. No explicit error handling is implemented in this test file.
Logging
No explicit logging is implemented in this test file. The unittest
framework handles test result reporting.
Notes
- The tests cover various aspects of the SpectralSolver, including similarity calculations, graph construction, and the complete solving process.
- Some tests use predefined character matrices and expected tree structures to verify the correctness of the solver.
- The tests include cases with and without weighted similarities and priors.
- The
find_triplet_structure
function is used to compare the structure of the generated trees with expected structures.
This test file is crucial for ensuring the correctness and reliability of the SpectralSolver implementation in the Cassiopeia package.