High-level description

This file contains unit tests for the SpectralGreedySolver and MaxCutGreedySolver classes, which are variants of the greedy solver algorithm used in the Cassiopeia project for phylogenetic tree reconstruction. The tests verify the correctness of the solvers’ behavior in various scenarios, including handling of sparse data, base cases, and different weighting schemes.

Code Structure

The main class GreedyVariantsTest contains multiple test methods, each focusing on a specific aspect of the solvers’ functionality. The tests create sample character matrices, instantiate solver objects, and compare the resulting tree structures against expected outcomes.

Symbols

find_triplet_structure

Description

This function determines the structure of a triplet of nodes in a given tree.

Inputs

NameTypeDescription
triplettupleA tuple of three node labels
Tnetworkx.DiGraphThe tree graph

Outputs

NameTypeDescription
structurestrThe structure of the triplet (“ab”, “ac”, “bc”, or ”-“)

Internal Logic

  1. Find ancestors for each node in the triplet
  2. Calculate the number of common ancestors for each pair
  3. Determine the structure based on which pair has the most common ancestors

GreedyVariantsTest

Description

A test class containing multiple test methods for the SpectralGreedySolver and MaxCutGreedySolver classes.

Test Methods

  1. test_raises_error_on_ambiguous: Verifies that the solver raises an error for ambiguous data.
  2. test_spectral_sparse_case: Tests the SpectralGreedySolver with sparse data.
  3. test_spectral_base_case: Tests the SpectralGreedySolver with a base case scenario.
  4. test_spectral_base_case_weights_almost_one: Tests the SpectralGreedySolver with weights close to 1.
  5. test_maxcut_base_case: Tests the MaxCutGreedySolver with a base case scenario.
  6. test_maxcut_base_case_weights_trivial: Tests the MaxCutGreedySolver with trivial weights.

Internal Logic

Each test method follows a similar pattern:

  1. Create a character matrix
  2. Instantiate a solver object
  3. Solve the tree
  4. Compare the resulting tree structure against expected outcomes using triplet structures

Dependencies

  • unittest
  • itertools
  • networkx
  • pandas
  • cassiopeia

Error Handling

The tests use assertRaises to check for expected errors and assertEqual to compare results against expected values.

Notes

  • The tests focus on verifying the correctness of the solvers’ output rather than performance optimization.
  • The use of triplet structures for comparison allows for flexible testing of tree topologies.
  • The tests cover various scenarios, including sparse data and different weighting schemes, to ensure robust solver behavior.