maxcut_test.py
Here’s a detailed documentation of the target file test/solver_tests/maxcut_test.py
:
High-level description
This file contains unit tests for the MaxCutSolver class, which is part of the Cassiopeia lineage reconstruction framework. The tests verify the functionality of various components of the MaxCutSolver, including graph construction, cut evaluation, and the overall solving process.
Code Structure
The main class MaxCutSolverTest
inherits from unittest.TestCase
and contains several test methods. It uses sample character matrices and trees to test different aspects of the MaxCutSolver. The file also includes a helper function find_triplet_structure
used in comparing tree topologies.
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.
- Compare the number of common ancestors between pairs.
- Return the structure based on which pair has the most common ancestors.
MaxCutSolverTest
Description
This is the main test class containing various unit tests for the MaxCutSolver.
Methods
setUp
Description
Initializes test data and objects for use in the test methods.
Internal Logic
- Creates sample character matrices.
- Initializes CassiopeiaTrees and a MaxCutSolver.
- Computes mutation frequencies.
test_check_if_cut
Description
Tests the check_if_cut
function from graph_utilities.
test_evaluate_cut
Description
Tests the evaluate_cut
method of MaxCutSolver.
test_graph_construction
Description
Tests the construction of the connectivity graph.
test_graph_construction_weights
Description
Tests the construction of the connectivity graph with custom weights.
test_hill_climb
Description
Tests the hill-climbing procedure for improving cuts.
test_polytomy_base_case
Description
Tests the solver’s handling of polytomies (unresolved relationships).
test_simple_base_case
Description
Tests the solver on a simple case without priors.
test_simple_base_case_priors
Description
Tests the solver on a simple case with priors.
Dependencies
Dependency | Purpose |
---|---|
unittest | Provides testing framework |
itertools | Used for combinations in triplet analysis |
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 expected outcomes. No explicit error handling is implemented in this test file.
Performance Considerations
The tests involve graph operations and tree comparisons, which can be computationally expensive for large datasets. However, as these are unit tests with small sample data, performance is not a significant concern.
This documentation provides a comprehensive overview of the maxcut_test.py
file, explaining its purpose, structure, and key components. The file is crucial for ensuring the correct functionality of the MaxCutSolver in the Cassiopeia framework.