Here’s a comprehensive documentation of the test/solver_tests/neighborjoining_solver_test.py file:

High-level description

This file contains unit tests for the NeighborJoiningSolver in Cassiopeia’s solver module. It tests various aspects of the neighbor-joining algorithm implementation, including basic functionality, error correction, and handling of different input scenarios.

Code Structure

The main class TestNeighborJoiningSolver inherits from unittest.TestCase and contains multiple test methods. It uses mock objects and sample data to test different aspects of the NeighborJoiningSolver. The file also includes helper functions for creating test data and analyzing tree structures.

Symbols

find_triplet_structure

Description

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

Inputs

NameTypeDescription
triplettupleA tuple of three node names
Tnetworkx.DiGraphThe tree to analyze

Outputs

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

Internal Logic

It compares the number of common ancestors between pairs of nodes to determine the closest pair.

delta_fn

Description

A dissimilarity function used by the solvers to compute distances between sequences.

Inputs

NameTypeDescription
xnp.arrayFirst sequence
ynp.arraySecond sequence
missing_stateintValue representing missing data
priorsOptional[Dict[int, Dict[int, float]]]Prior probabilities

Outputs

NameTypeDescription
dintThe number of differences between the sequences

TestNeighborJoiningSolver

Description

The main test class containing all the unit tests for the NeighborJoiningSolver.

Internal Logic

The setUp method initializes various test scenarios, including:

  1. General Neighbor Joining
  2. Lineage Tracing Neighbor Joining
  3. Character Matrix with Duplicates
  4. Neighbor Joining with modified hamming dissimilarity

Each test method focuses on a specific aspect of the NeighborJoiningSolver:

  1. test_constructor: Tests the constructor and error handling
  2. test_compute_q: Tests the Q-matrix computation
  3. test_find_cherry: Tests the cherry-picking step
  4. test_update_dissimilarity_map: Tests updating the dissimilarity map
  5. test_basic_solver: Tests the basic solver functionality
  6. test_nj_solver_weights: Tests the solver with weighted distances
  7. test_pp_solver: Tests the solver with a different dissimilarity function
  8. test_duplicate_sample_neighbor_joining: Tests handling of duplicate samples
  9. test_setup_root_finder_missing_dissimilarity_map: Tests root finding with missing dissimilarity map
  10. test_setup_root_finder_existing_dissimilarity_map: Tests root finding with existing dissimilarity map

Dependencies

DependencyPurpose
unittestFor creating and running unit tests
typingFor type hinting
unittest.mockFor creating mock objects
itertoolsFor generating combinations
networkxFor graph operations
numpyFor numerical operations
pandasFor data manipulation
cassiopeiaThe main package being tested

Error Handling

The tests use assertRaises to check if the solver raises appropriate exceptions in error cases.

Performance Considerations

The tests don’t explicitly address performance, but they do test the correctness of the algorithm on various input sizes and scenarios.

This documentation provides a comprehensive overview of the test file, its structure, and the various aspects of the NeighborJoiningSolver that it tests. The file ensures that the solver works correctly for different input scenarios and handles edge cases appropriately.