unifom_leaf_subsampler_test.py
Here’s a detailed documentation of the target file:
High-level description
This file contains unit tests for the UniformLeafSubsampler class, which is responsible for uniformly subsampling leaves from a CassiopeiaTree. The tests cover various scenarios including parameter validation, error handling, and the correctness of the subsampling process.
Symbols
UniformLeafSubsamplerTest
Description
A test class that inherits from unittest.TestCase
, containing multiple test methods to verify the functionality of the UniformLeafSubsampler class.
Internal Logic
The class contains three main test methods:
test_bad_parameters
: Tests error handling for invalid initialization parameters.test_bad_number_of_samples
: Tests error handling for invalid sample sizes.test_subsample_balanced_tree
: Tests the subsampling process on a balanced tree.test_subsample_custom_tree
: Tests the subsampling process on a custom tree.
test_bad_parameters
Description
Tests that the UniformLeafSubsampler raises appropriate errors when initialized with invalid parameters.
Internal Logic
- Attempts to create a UniformLeafSubsampler with both ratio and number_of_leaves specified, expecting a LeafSubsamplerError.
- Attempts to create a UniformLeafSubsampler with neither ratio nor number_of_leaves specified, expecting a LeafSubsamplerError.
test_bad_number_of_samples
Description
Tests that the UniformLeafSubsampler raises appropriate errors when attempting to subsample with invalid sample sizes.
Internal Logic
- Creates a balanced tree using networkx.
- Attempts to subsample with 0 leaves, expecting a LeafSubsamplerError.
- Attempts to subsample with a very small ratio (0.0001), expecting a LeafSubsamplerError.
test_subsample_balanced_tree
Description
Tests the subsampling process on a balanced tree, verifying the correctness of the resulting subsampled tree.
Internal Logic
- Creates a balanced tree using networkx.
- Performs subsampling with a fixed number of leaves (3) and verifies the resulting tree structure.
- Performs subsampling with a ratio (0.65) and verifies the resulting tree structure.
test_subsample_custom_tree
Description
Tests the subsampling process on a custom tree, verifying the correctness of the resulting subsampled tree, including branch lengths and node times.
Internal Logic
- Creates a custom tree using networkx.
- Performs subsampling with a ratio (0.5) and verifies the resulting tree structure, branch lengths, and node times.
- Performs subsampling with a fixed number of leaves (6) and verifies the resulting tree structure.
Dependencies
Dependency | Purpose |
---|---|
unittest | Provides the testing framework |
networkx | Used to create and manipulate graph structures for testing |
numpy | Used for random number generation in the tests |
cassiopeia.data.CassiopeiaTree | The main tree structure being tested |
cassiopeia.simulator.LeafSubsampler | Provides the LeafSubsamplerError |
cassiopeia.simulator.UniformLeafSubsampler | The class being tested |
Error Handling
The tests specifically check for proper error handling in the following cases:
- Invalid initialization parameters for UniformLeafSubsampler.
- Attempting to subsample with invalid sample sizes (0 or very small ratio).
Notes
- The tests use fixed random seeds (np.random.seed(10) and np.random.seed(11)) to ensure reproducibility of the random subsampling process.
- The tests cover both the case of subsampling with a fixed number of leaves and with a ratio of the total leaves.
- The tests verify not only the structure of the resulting subsampled tree but also the correctness of branch lengths and node times in the custom tree test case.