Here’s a comprehensive documentation for the target file test/solver_tests/dissimilarity_functions_test.py:

High-level description

This file contains unit tests for the dissimilarity functions used in the DistanceSolver module of the Cassiopeia project. It tests various dissimilarity and similarity functions, as well as utility functions related to prior transformations and dissimilarity map saving.

Code Structure

The main class TestDissimilarityFunctions inherits from unittest.TestCase and contains multiple test methods. Each method tests a specific function from the dissimilarity_functions or solver_utilities modules.

Symbols

TestDissimilarityFunctions

Description

A test class that contains unit tests for various dissimilarity functions and related utilities.

Internal Logic

  1. Sets up test data in the setUp method.
  2. Tests different dissimilarity and similarity functions with various inputs and configurations.
  3. Tests prior transformation functions.
  4. Tests the function for saving dissimilarity maps as phylip files.

setUp

Description

Initializes test data for use in the test methods.

Internal Logic

  1. Creates sample arrays and lists for testing.
  2. Sets up prior probabilities and transforms them using different methods.

test_bad_prior_transformations

Description

Tests that invalid prior transformations raise the expected error.

test_negative_log_prior_transformations

Description

Verifies the correct transformation of priors using the negative log method.

test_inverse_prior_transformations

Description

Verifies the correct transformation of priors using the inverse method.

test_sq_inverse_prior_transformations

Description

Verifies the correct transformation of priors using the square root inverse method.

test_weighted_hamming_distance_identical

Description

Tests the weighted Hamming distance function with identical inputs.

test_weighted_hamming_distance_no_priors

Description

Tests the weighted Hamming distance function without using priors.

test_weighted_hamming_distance_priors_negative_log

Description

Tests the weighted Hamming distance function using negative log transformed priors.

test_weighted_hamming_distance_priors_inverse

Description

Tests the weighted Hamming distance function using inverse transformed priors.

test_weighted_hamming_distance_priors_sq_inverse

Description

Tests the weighted Hamming distance function using square root inverse transformed priors.

test_weighted_hamming_distance_all_missing

Description

Tests the weighted Hamming distance function with all missing values.

test_hamming_similarity_without_missing_identical

Description

Tests the Hamming similarity function without missing values for identical inputs.

test_hamming_similarity_without_missing_no_priors

Description

Tests the Hamming similarity function without missing values and without priors.

test_hamming_similarity_without_missing_priors

Description

Tests the Hamming similarity function without missing values but with priors.

test_hamming_similarity_without_missing_all_missing

Description

Tests the Hamming similarity function with all missing values.

test_hamming_similarity_normalized_identical

Description

Tests the normalized Hamming similarity function with identical inputs.

test_hamming_similarity_normalized_no_priors

Description

Tests the normalized Hamming similarity function without priors.

test_hamming_similarity_normalized_priors

Description

Tests the normalized Hamming similarity function with priors.

test_hamming_similarity_normalized_all_missing

Description

Tests the normalized Hamming similarity function with all missing values.

test_weighted_hamming_similarity_identical

Description

Tests the weighted Hamming similarity function with identical inputs.

test_weighted_hamming_similarity_no_priors

Description

Tests the weighted Hamming similarity function without priors.

test_weighted_hamming_similarity_priors

Description

Tests the weighted Hamming similarity function with priors.

test_weighted_hamming_similarity_all_missing

Description

Tests the weighted Hamming similarity function with all missing values.

test_cluster_dissimilarity

Description

Tests the cluster dissimilarity function.

test_cluster_dissimilarity_weighted_hamming_distance_min_linkage

Description

Tests the cluster dissimilarity function using weighted Hamming distance with minimum linkage.

test_hamming_distance

Description

Tests the Hamming distance function.

test_hamming_distance_ignore_missing

Description

Tests the Hamming distance function with the option to ignore missing values.

test_save_dissimilarity_as_phylip

Description

Tests the function for saving dissimilarity maps as phylip files.

Internal Logic

  1. Creates a sample dissimilarity map.
  2. Mocks the file writing process.
  3. Calls the function to save the dissimilarity map.
  4. Verifies that the correct content is written to the file.

Dependencies

  • unittest
  • unittest.mock
  • numpy
  • pandas
  • cassiopeia.solver.dissimilarity_functions
  • cassiopeia.solver.solver_utilities

Error Handling

The test class uses assertions provided by unittest.TestCase to verify the correctness of function outputs and to check for expected exceptions.

Performance Considerations

Some of the tested functions, particularly those involving dissimilarity calculations, may have performance implications for large datasets. However, this test file focuses on correctness rather than performance testing.