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:

  1. test_bad_parameters: Tests error handling for invalid initialization parameters.
  2. test_bad_number_of_samples: Tests error handling for invalid sample sizes.
  3. test_subsample_balanced_tree: Tests the subsampling process on a balanced tree.
  4. 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

  1. Attempts to create a UniformLeafSubsampler with both ratio and number_of_leaves specified, expecting a LeafSubsamplerError.
  2. 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

  1. Creates a balanced tree using networkx.
  2. Attempts to subsample with 0 leaves, expecting a LeafSubsamplerError.
  3. 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

  1. Creates a balanced tree using networkx.
  2. Performs subsampling with a fixed number of leaves (3) and verifies the resulting tree structure.
  3. 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

  1. Creates a custom tree using networkx.
  2. Performs subsampling with a ratio (0.5) and verifies the resulting tree structure, branch lengths, and node times.
  3. Performs subsampling with a fixed number of leaves (6) and verifies the resulting tree structure.

Dependencies

DependencyPurpose
unittestProvides the testing framework
networkxUsed to create and manipulate graph structures for testing
numpyUsed for random number generation in the tests
cassiopeia.data.CassiopeiaTreeThe main tree structure being tested
cassiopeia.simulator.LeafSubsamplerProvides the LeafSubsamplerError
cassiopeia.simulator.UniformLeafSubsamplerThe class being tested

Error Handling

The tests specifically check for proper error handling in the following cases:

  1. Invalid initialization parameters for UniformLeafSubsampler.
  2. 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.