> ## Documentation Index
> Fetch the complete documentation index at: https://demo.agenticlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# neighborjoining_solver_test.py

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

| Name    | Type             | Description                 |
| :------ | :--------------- | :-------------------------- |
| triplet | tuple            | A tuple of three node names |
| T       | networkx.DiGraph | The tree to analyze         |

#### Outputs

| Name      | Type | Description                                             |
| :-------- | :--- | :------------------------------------------------------ |
| structure | str  | The 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

| Name           | Type                                     | Description                     |
| :------------- | :--------------------------------------- | :------------------------------ |
| x              | np.array                                 | First sequence                  |
| y              | np.array                                 | Second sequence                 |
| missing\_state | int                                      | Value representing missing data |
| priors         | Optional\[Dict\[int, Dict\[int, float]]] | Prior probabilities             |

#### Outputs

| Name | Type | Description                                     |
| :--- | :--- | :---------------------------------------------- |
| d    | int  | The 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

| Dependency    | Purpose                             |
| :------------ | :---------------------------------- |
| unittest      | For creating and running unit tests |
| typing        | For type hinting                    |
| unittest.mock | For creating mock objects           |
| itertools     | For generating combinations         |
| networkx      | For graph operations                |
| numpy         | For numerical operations            |
| pandas        | For data manipulation               |
| cassiopeia    | The 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.
