High-level description
This file contains unit tests for thesmall_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 classTestSmallParsimony 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 thecassiopeia.tools.small_parsimony module:
fitch_hartigan_bottom_upfitch_hartigan_top_downfitch_hartiganscore_small_parsimonyfitch_count
Symbols
TestSmallParsimony
Description
This class contains unit tests for functions in thecassiopeia.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
NoneOutputs
NoneInternal Logic
The class defines several test methods, each testing a specific function or aspect of thesmall_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 thefitch_hartigan_bottom_up function, which performs the bottom-up phase of the Fitch-Hartigan algorithm.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Checks for expected errors when invalid input is provided.
- Runs the
fitch_hartigan_bottom_upfunction on the binary tree. - Verifies that the correct ancestral state sets are assigned to each node.
- Tests the function with different
add_keyparameters.
test_fitch_hartigan_top_down
Description
Tests thefitch_hartigan_top_down function, which performs the top-down refinement step of the Fitch-Hartigan algorithm.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_hartigan_bottom_upfunction to initialize ancestral state sets. - Runs the
fitch_hartigan_top_downfunction on the binary tree. - Verifies that the correct character states are assigned to each node.
- Tests the function with different
label_keyparameters.
test_fitch_hartigan
Description
Tests thefitch_hartigan function, which combines the bottom-up and top-down steps of the Fitch-Hartigan algorithm.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_hartiganfunction on the binary tree. - Verifies that the correct character states are assigned to each node.
test_score_parsimony
Description
Tests thescore_small_parsimony function, which calculates the parsimony score of a tree.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Checks for expected errors when invalid input is provided.
- Runs the
score_small_parsimonyfunction 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 thefitch_hartigan_bottom_up function on a general (non-binary) tree.
Inputs
Uses theself.general_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_hartigan_bottom_upfunction on the general tree. - Verifies that the correct ancestral state sets are assigned to each node.
test_general_tree_fitch_hartigan
Description
Tests thefitch_hartigan function on a general (non-binary) tree.
Inputs
Uses theself.general_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_hartiganfunction on the general tree. - Verifies that the correct character states are assigned to each node.
test_general_tree_parsimony
Description
Tests thescore_small_parsimony function on a general (non-binary) tree.
Inputs
Uses theself.general_tree object as input.
Outputs
NoneInternal Logic
- Runs the
score_small_parsimonyfunction on the general tree. - Verifies that the calculated parsimony score is correct.
test_fitch_count_basic_binary
Description
Tests thefitch_count function on a basic binary tree.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_countfunction 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 thefitch_count function with a custom state space.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_countfunction 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 thefitch_count function with a specified root node.
Inputs
Uses theself.binary_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_countfunction with a specified root node. - Verifies that the resulting count matrix has the correct dimensions and values.
test_fitch_count_general_tree
Description
Tests thefitch_count function on a general (non-binary) tree.
Inputs
Uses theself.general_tree object as input.
Outputs
NoneInternal Logic
- Runs the
fitch_countfunction on the general tree. - Verifies that the resulting count matrix has the correct dimensions and values.
Dependencies
unittestnetworkxnumpypandascassiopeia
Error Handling
The test methods use assertions provided by theunittest 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.