autocorrelation_test.py
High-level description
This file contains unit tests for the compute_morans_i
function, which calculates Moran’s I statistic for spatial autocorrelation in a phylogenetic tree. The tests cover various scenarios, including single and multiple variables, custom weight matrices, and error handling.
References
This file references the following symbols:
cassiopeia.tl.compute_morans_i
: The function being tested, which calculates Moran’s I statistic.cassiopeia.data.CassiopeiaTree
: A class representing a phylogenetic tree.cassiopeia.mixins.AutocorrelationError
: An exception class for errors related to autocorrelation calculations.
Symbols
TestAutocorrelation
Description
This class contains unit tests for the compute_morans_i
function. It sets up a basic tree structure and example data for testing.
Inputs
This class does not take any inputs.
Outputs
This class does not return any outputs. It uses the unittest
framework to run assertions and report test results.
Internal Logic
The class defines several test methods, each testing a specific aspect of the compute_morans_i
function:
test_simple_moran_single_variable
: Tests the function with a single variable and compares the result to a known value.test_moran_bivariate
: Tests the function with multiple variables and compares the resulting correlation matrix to expected values.test_moran_custom_weights
: Tests the function with a user-provided weight matrix.test_moran_exceptions
: Tests various error conditions, such as invalid input data types and mismatched tree leaves.
setUp
Description
This method sets up the test environment by creating a basic tree structure and example data.
Inputs
This method does not take any inputs.
Outputs
This method does not explicitly return any outputs. It sets the self.basic_tree
and self.X
attributes, which are used by the test methods.
Error Handling
The test_moran_exceptions
method specifically tests for various error conditions that can occur in the compute_morans_i
function, such as:
- Passing non-numerical data in the
X
argument. - Providing a weight matrix (
W
) that does not have the same leaves as the tree. - Not providing any data for calculating autocorrelations.
- Providing a weight matrix with incorrect leaves.
These error conditions are handled by raising AutocorrelationError
exceptions with informative messages.