> ## 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.

# small_parsimony_test.py

## High-level description

This file contains unit tests for the `small_parsimony.py` module in Cassiopeia. The tests cover functions related to small parsimony inference on phylogenetic trees, including Fitch-Hartigan reconstruction, parsimony scoring, and the FitchCount algorithm.

## Code Structure

The code defines a single test class `TestSmallParsimony` that inherits from `unittest.TestCase`. This class contains multiple test methods, each testing a specific function or aspect of the `small_parsimony` module. The test methods use various assertions provided by the `unittest` framework to check for expected outcomes.

## References

This test file references the following symbols from the `cassiopeia.tools.small_parsimony` module:

* `fitch_hartigan_bottom_up`
* `fitch_hartigan_top_down`
* `fitch_hartigan`
* `score_small_parsimony`
* `fitch_count`

## Symbols

### `TestSmallParsimony`

#### Description

This class contains unit tests for functions in the `cassiopeia.tools.small_parsimony` module. It sets up two test trees, a binary tree and a general tree, with associated character data, to be used in the test cases.

#### Inputs

None

#### Outputs

None

#### Internal Logic

The class defines several test methods, each testing a specific function or aspect of the `small_parsimony` module. Each test method sets up the necessary input data and calls the function being tested. Assertions are then used to verify that the function behaves as expected.

### `test_fitch_hartigan_bottom_up`

#### Description

Tests the `fitch_hartigan_bottom_up` function, which performs the bottom-up phase of the Fitch-Hartigan algorithm.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Checks for expected errors when invalid input is provided.
* Runs the `fitch_hartigan_bottom_up` function on the binary tree.
* Verifies that the correct ancestral state sets are assigned to each node.
* Tests the function with different `add_key` parameters.

### `test_fitch_hartigan_top_down`

#### Description

Tests the `fitch_hartigan_top_down` function, which performs the top-down refinement step of the Fitch-Hartigan algorithm.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_hartigan_bottom_up` function to initialize ancestral state sets.
* Runs the `fitch_hartigan_top_down` function on the binary tree.
* Verifies that the correct character states are assigned to each node.
* Tests the function with different `label_key` parameters.

### `test_fitch_hartigan`

#### Description

Tests the `fitch_hartigan` function, which combines the bottom-up and top-down steps of the Fitch-Hartigan algorithm.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_hartigan` function on the binary tree.
* Verifies that the correct character states are assigned to each node.

### `test_score_parsimony`

#### Description

Tests the `score_small_parsimony` function, which calculates the parsimony score of a tree.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Checks for expected errors when invalid input is provided.
* Runs the `score_small_parsimony` function with and without ancestral state inference.
* Verifies that the calculated parsimony score is correct in both cases.

### `test_general_tree_fitch_bottom_up`

#### Description

Tests the `fitch_hartigan_bottom_up` function on a general (non-binary) tree.

#### Inputs

Uses the `self.general_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_hartigan_bottom_up` function on the general tree.
* Verifies that the correct ancestral state sets are assigned to each node.

### `test_general_tree_fitch_hartigan`

#### Description

Tests the `fitch_hartigan` function on a general (non-binary) tree.

#### Inputs

Uses the `self.general_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_hartigan` function on the general tree.
* Verifies that the correct character states are assigned to each node.

### `test_general_tree_parsimony`

#### Description

Tests the `score_small_parsimony` function on a general (non-binary) tree.

#### Inputs

Uses the `self.general_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `score_small_parsimony` function on the general tree.
* Verifies that the calculated parsimony score is correct.

### `test_fitch_count_basic_binary`

#### Description

Tests the `fitch_count` function on a basic binary tree.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_count` function on the binary tree.
* Verifies that the resulting count matrix has the correct dimensions and values.
* Tests the function with and without pre-computed ancestral states.

### `test_fitch_count_basic_binary_custom_state_space`

#### Description

Tests the `fitch_count` function with a custom state space.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_count` function with a specified state space.
* Verifies that the resulting count matrix has the correct dimensions and values.
* Checks for expected errors when an invalid state space is provided.

### `test_fitch_count_basic_binary_internal_node`

#### Description

Tests the `fitch_count` function with a specified root node.

#### Inputs

Uses the `self.binary_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_count` function with a specified root node.
* Verifies that the resulting count matrix has the correct dimensions and values.

### `test_fitch_count_general_tree`

#### Description

Tests the `fitch_count` function on a general (non-binary) tree.

#### Inputs

Uses the `self.general_tree` object as input.

#### Outputs

None

#### Internal Logic

* Runs the `fitch_count` function on the general tree.
* Verifies that the resulting count matrix has the correct dimensions and values.

## Dependencies

* `unittest`
* `networkx`
* `numpy`
* `pandas`
* `cassiopeia`

## Error Handling

The test methods use assertions provided by the `unittest` framework to check for expected errors. For example, the `assertRaises` method is used to verify that a specific exception is raised when invalid input is provided to a function.
