Here’s a detailed documentation of the test/simulator_tests/supercellular_sampler_test.py file:

High-level description

This file contains unit tests for the SupercellularSampler class, which is a subclass of LeafSubsampler. The tests verify the functionality of the SupercellularSampler, including its initialization, error handling, and leaf subsampling behavior.

Code Structure

The main class SupercellularSamplerTest inherits from unittest.TestCase and contains several test methods. The setUp method initializes a test network and character matrix used in multiple tests.

Symbols

SupercellularSamplerTest

Description

A test class that contains unit tests for the SupercellularSampler class.

Internal Logic

The class sets up a test network and character matrix in the setUp method, which are used across multiple test cases.

setUp

Description

Initializes the test environment by creating a test network and character matrix.

Internal Logic

  • Creates a directed graph (self.test_network) representing a tree structure.
  • Defines a character matrix (self.character_matrix) with sample data for testing.

test_bad_parameters

Description

Tests the SupercellularSampler initialization with invalid parameters.

Internal Logic

  • Attempts to create a SupercellularSampler with both ratio and number_of_merges specified, which should raise a LeafSubsamplerError.
  • Attempts to create a SupercellularSampler with neither ratio nor number_of_merges specified, which should also raise a LeafSubsamplerError.

test_bad_number_of_samples

Description

Tests the subsample_leaves method with invalid inputs.

Internal Logic

  • Creates a CassiopeiaTree with the test network and character matrix.
  • Attempts to subsample leaves with invalid number_of_merges values (10, 0, and 2 for a tree without a character matrix), which should raise appropriate errors.

test_subsample_balanced_tree

Description

Tests the subsample_leaves method with valid inputs and verifies the output.

Internal Logic

  • Creates a CassiopeiaTree with the test network and character matrix.
  • Subsamples leaves using SupercellularSampler with number_of_merges=2.
  • Verifies the resulting character matrix and edges against expected values.
  • Repeats the test with collapse_duplicates=False and verifies the results.

Dependencies

  • unittest
  • networkx
  • numpy
  • pandas
  • cassiopeia.data.CassiopeiaTree
  • cassiopeia.simulator.LeafSubsampler
  • cassiopeia.simulator.SupercellularSampler
  • cassiopeia.data.utilities

Error Handling

The tests check for various error conditions, including:

  • Incorrect initialization parameters for SupercellularSampler
  • Invalid number of merges in subsample_leaves method
  • Attempting to subsample leaves on a tree without a character matrix

TODOs

There are no explicit TODOs in this file.

This test file ensures that the SupercellularSampler class behaves correctly under various conditions and produces expected outputs when subsampling leaves from a CassiopeiaTree.