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

# tree_metrics_test.py

## High-level description

This file contains unit tests for the `tree_metrics.py` module in the Cassiopeia package. These tests cover functions that calculate various tree metrics, including parsimony, log transition probability, log likelihood (both discrete and continuous), and parameter estimation for lineage tracing models.

## Code Structure

This file defines a single test class, `TestCassiopeiaTree`, which houses multiple test methods. Each test method focuses on a specific function or aspect of the `tree_metrics.py` module. The tests utilize small, predefined tree structures and character matrices to validate the correctness of the implemented algorithms.

## Symbols

### `TestCassiopeiaTree`

#### Description

This class contains unit tests for functions in the `tree_metrics.py` module. It sets up test data representing small trees and character matrices, then uses these to test the calculation of various tree metrics.

#### Inputs

This class does not take any explicit inputs.

#### Outputs

This class does not return any explicit outputs. It is used for running tests.

#### Internal Logic

The class defines several test methods, each testing a specific function or aspect of the `tree_metrics.py` module. These methods use `assertEqual`, `assertTrue`, `assertRaises`, and `assertAlmostEqual` to check for expected outcomes.

##### `test_parsimony_bad_cases`

Tests the `calculate_parsimony` function for expected error handling in cases where the tree is not properly initialized or ancestral character inference is required but not enabled.

##### `test_parsimony_reconstruct_internal_states`

Tests the `calculate_parsimony` function with the option to infer ancestral character states, verifying the calculated parsimony score.

##### `test_parsimony_specify_internal_states`

Tests the `calculate_parsimony` function with pre-specified internal character states, verifying the calculated parsimony score.

##### `test_log_transition_probability`

Tests the `log_transition_probability` function, which calculates the log probability of transitioning between character states, considering factors like mutation rate, missing data rate, and time.

##### `test_log_likelihood_of_character`

Tests the `log_likelihood_of_character` function, which calculates the log likelihood of observing a specific character on the tree, considering mutation and missing data probabilities.

##### `test_bad_lineage_tracing_parameters`

Tests for expected error handling when invalid lineage tracing parameters (mutation rate, heritable missing rate, stochastic missing probability) are provided.

##### `test_get_lineage_tracing_parameters`

Tests the `get_lineage_tracing_parameters` function, which retrieves or estimates lineage tracing parameters from the tree, ensuring the returned values are as expected.

##### `test_likelihood_bad_cases`

Tests the likelihood calculation functions (`calculate_likelihood_discrete` and `calculate_likelihood_continuous`) for expected error handling in cases of improper tree initialization or missing data.

##### `test_likelihood_simple_mostly_missing`

Tests the `calculate_likelihood_discrete` function with a simple tree and character matrix containing mostly missing data, verifying the calculated likelihood.

##### `test_likelihood_more_complex_case`

Tests the `calculate_likelihood_discrete` function with a more complex tree and character matrix, verifying the calculated likelihood.

##### `test_likelihood_set_internal_states`

Tests the `calculate_likelihood_discrete` function with pre-specified internal character states, verifying the calculated likelihood.

##### `test_likelihood_time`

Tests the `calculate_likelihood_continuous` function, which incorporates branch lengths (time) into the likelihood calculation, verifying the results.

##### `test_likelihood_sum_to_one`

Tests that the calculated likelihoods for all possible character state combinations sum to one, as expected for a probability distribution.

## Dependencies

* unittest
* itertools
* networkx
* numpy
* pandas
* cassiopeia

```python theme={null}
import unittest
import itertools
import networkx as nx
import numpy as np
import pandas as pd
import cassiopeia as cas
```
