ilp_solver_test.py
Here’s a comprehensive documentation for the test/solver_tests/ilp_solver_test.py
file:
High-level description
This file contains unit tests for the ILPSolver class in Cassiopeia’s solver module. It tests various aspects of the ILP (Integer Linear Programming) solver, including its ability to handle perfect phylogenies, duplicates, missing data, and edge cases.
Code Structure
The main class TestILPSolver
inherits from unittest.TestCase
and contains multiple test methods. It sets up test data in the setUp
method and defines helper functions for specific test scenarios. The tests cover different aspects of the ILPSolver, from basic functionality to edge cases and error handling.
Symbols
TestILPSolver
Description
A test class that inherits from unittest.TestCase
, containing various test methods for the ILPSolver.
Internal Logic
- Sets up test data in
setUp
method. - Defines helper functions like
find_triplet_structure
. - Implements multiple test methods to cover different scenarios and functionalities of the ILPSolver.
find_triplet_structure
Description
A helper function that determines the structure of a triplet in a given tree.
Inputs
Name | Type | Description |
---|---|---|
triplet | tuple | A tuple of three nodes |
T | networkx.DiGraph | The tree to analyze |
Outputs
Name | Type | Description |
---|---|---|
structure | str | The structure of the triplet (“ab”, “ac”, “bc”, or ”-“) |
setUp
Description
Sets up the test environment by creating test data and initializing the ILPSolver.
test_raises_error_on_ambiguous
Description
Tests if the ILPSolver raises an error when given ambiguous data.
test_get_lca_cython
Description
Tests the functionality of the get_lca_characters_cython function.
test_cython_hamming_dist
Description
Tests the Cython implementation of the Hamming distance function.
test_single_sample_ilp
Description
Tests the ILPSolver with a single sample input.
test_basic_ilp_constructor
Description
Tests the basic constructor of the ILPSolver.
test_get_layer_for_potential_graph
Description
Tests the functionality of inferring a layer of the potential graph.
test_simple_potential_graph_inference
Description
Tests the inference of a simple potential graph.
test_post_process_steiner_solution
Description
Tests the post-processing of a Steiner tree solution.
test_append_sample_nodes_and_remove_spurious_leaves
Description
Tests the functionality of appending sample nodes and removing spurious leaves.
test_ilp_solver_perfect_phylogeny
Description
Tests the ILPSolver with a perfect phylogeny scenario.
test_potential_graph_inference_with_duplicates
Description
Tests the potential graph inference with duplicate samples.
test_ilp_solver_with_duplicates
Description
Tests the ILPSolver with duplicate samples.
test_ilp_solver_missing_data
Description
Tests the ILPSolver with missing data.
test_ilp_throws_error_when_potential_graph_is_not_found
Description
Tests if the ILPSolver throws an error when a potential graph cannot be found.
Dependencies
Dependency | Purpose |
---|---|
unittest | Provides testing framework |
networkx | Used for graph operations |
numpy | Used for numerical operations |
pandas | Used for data manipulation |
cassiopeia | The main package being tested |
gurobipy | Used for ILP optimization (optional) |
Error Handling
The tests include various assertions to check for expected behavior and raise appropriate errors. The ILPSolverError
is used to handle specific errors related to the ILPSolver.
Performance Considerations
Some tests may be skipped if Gurobi is not installed, as indicated by the @unittest.skipUnless(GUROBI_INSTALLED, "Gurobi installation not found.")
decorator.
TODOs
There are no explicit TODOs in the code.
This documentation provides a comprehensive overview of the ilp_solver_test.py
file, covering its purpose, structure, and key components. It should help developers understand the testing suite for the ILPSolver in Cassiopeia.