Here’s a documentation of the target file:

High-level description

This file contains unit tests for the BirthDeathFitnessSimulator class in Cassiopeia. It tests various aspects of the simulator, including error handling, basic functionality, and more complex scenarios involving birth, death, and fitness mutations.

Code Structure

The main class BirthDeathSimulatorTest inherits from unittest.TestCase and contains multiple test methods. Each method tests a specific aspect or scenario of the BirthDeathFitnessSimulator.

Symbols

BirthDeathSimulatorTest

Description

A test class that contains various unit tests for the BirthDeathFitnessSimulator class.

Internal Logic

The class sets up a character matrix and priors in the setUp method, which are used in some of the tests. It then defines multiple test methods, each focusing on a specific aspect of the simulator’s functionality.

test_bad_waiting_distributions

Description

Tests that the simulator raises appropriate errors when invalid waiting time distributions are provided.

Internal Logic

Creates BirthDeathFitnessSimulator instances with invalid parameters and asserts that TreeSimulatorError is raised in each case.

test_bad_stopping_conditions

Description

Ensures that the simulator raises errors when invalid stopping conditions are given.

Internal Logic

Creates BirthDeathFitnessSimulator instances with various invalid stopping conditions and asserts that TreeSimulatorError is raised in each case.

test_dead_at_start

Description

Tests that the simulator raises an error when all lineages die on the first event.

Internal Logic

Creates BirthDeathFitnessSimulator instances with parameters that would cause immediate death of all lineages and asserts that TreeSimulatorError is raised.

test_dead_before_end

Description

Ensures that the simulator raises an error when all lineages die before the stopping condition is met.

Internal Logic

Creates BirthDeathFitnessSimulator instances with parameters that would cause all lineages to die before the stopping condition and asserts that TreeSimulatorError is raised.

test_single_lineage

Description

Tests the base case where stopping conditions are met before any divisions occur.

Internal Logic

Creates BirthDeathFitnessSimulator instances with parameters that result in a single lineage and verifies the resulting tree structure.

test_constant_yule

Description

Tests a small case without death and with constant waiting times.

Internal Logic

Creates BirthDeathFitnessSimulator instances with constant birth rates and no death, and verifies the resulting tree structure.

test_nonconstant_yule

Description

Tests a case without death but with variable waiting times.

Internal Logic

Creates BirthDeathFitnessSimulator instances with variable birth rates and no death, and verifies the resulting tree structure.

test_nonconstant_birth_death

Description

Tests a case with variable birth and death waiting times, including pruning of dead lineages and collapsing of unifurcations.

Internal Logic

Creates BirthDeathFitnessSimulator instances with variable birth and death rates, and verifies the resulting tree structure, including the absence of certain nodes due to pruning and collapsing.

test_nonconstant_birth_death_no_unifurcation_collapsing

Description

Tests a case with variable birth and death waiting times, but without collapsing unifurcations.

Internal Logic

Similar to the previous test, but with collapse_unifurcations=False, and verifies that certain nodes are present in the resulting tree.

test_nonconstant_birth_death_both_stopping_conditions

Description

Tests a case with variable birth and death waiting times, using both stopping conditions (number of extant nodes and experiment time).

Internal Logic

Creates BirthDeathFitnessSimulator instances with both stopping conditions and verifies the resulting tree structure under different scenarios.

test_nonconstant_yule_with_predictable_fitness

Description

Tests a case with birth and death with constant fitness.

Internal Logic

Creates a BirthDeathFitnessSimulator with predictable fitness mutations and verifies that the fitness values in the resulting tree match the expected values.

test_nonconstant_birth_death_with_variable_fitness

Description

Tests a case with variable birth and death waiting times, as well as variable fitness evolution.

Internal Logic

Creates a BirthDeathFitnessSimulator with variable fitness mutations and verifies the resulting tree structure.

test_no_initial_birth_scale

Description

Tests the simulator’s behavior when initializing with a tree that doesn’t have default initial birth scales.

Internal Logic

Creates an initial tree without birth scales, initializes a BirthDeathFitnessSimulator with this tree, and verifies that the resulting tree has the correct number of leaves and birth scales.

test_birth_scale

Description

Tests that the birth scales are correctly maintained when initializing the simulator with an existing tree.

Internal Logic

Creates an initial tree using the simulator, then uses this tree to initialize another simulation, and verifies that the birth scales are correctly maintained in the final tree.

Dependencies

  • unittest
  • networkx
  • numpy
  • pandas
  • cassiopeia.data.CassiopeiaTree
  • cassiopeia.mixins.TreeSimulatorError
  • cassiopeia.simulator.BirthDeathFitnessSimulator
  • cassiopeia.data.utilities

Error Handling

The test cases use assertRaises to check for expected exceptions in various scenarios, such as invalid input parameters or unexpected simulation outcomes.

TODOs

There are no explicit TODOs in the code.