test/simulator_tests/spatial_leaf_subsampler_test.py
file:
High-level description
This file contains unit tests for theSpatialLeafSubsampler
class in the Cassiopeia package. It tests various functionalities of the subsampler, including initialization, parameter validation, and leaf subsampling in both 2D and 3D spaces.
Code Structure
The main classSpatialLeafSubsamplerTest
inherits from unittest.TestCase
and contains multiple test methods. The setUp
method initializes test data, including 2D and 3D trees and spaces.
Symbols
SpatialLeafSubsamplerTest
Description
A test class for theSpatialLeafSubsampler
class.
Internal Logic
- Sets up test data in the
setUp
method. - Tests various aspects of the
SpatialLeafSubsampler
class through individual test methods.
setUp
Description
Initializes test data for the test cases.Internal Logic
- Creates a balanced tree with spatial attributes.
- Sets up 2D and 3D versions of the tree.
- Defines 2D and 3D bounding boxes and spaces for testing.
test_bad_init_parameters
Description
Tests error handling for invalid initialization parameters.Internal Logic
Checks forLeafSubsamplerError
in various scenarios:
- Both ratio and number of leaves provided
- Neither space nor bounding box provided
- Both space and bounding box provided
- Negative number of leaves
- Ratio > 1
- Merge cells without space
test_bad_subsample_parameters
Description
Tests error handling for invalid subsampling parameters.Internal Logic
Checks forLeafSubsamplerError
in various scenarios:
- Tree without spatial attributes
- Incompatible space dimensions
- Incompatible bounding box dimensions
- Leaf coordinates outside space
- No leaves in region
- Number of leaves too large
test_bad_scale
Description
Tests warning generation for potentially problematic scale issues.Internal Logic
- Creates a tree with very small spatial coordinates.
- Checks for
LeafSubsamplerWarning
when subsampling.
test_subsample_3d_tree
Description
Tests subsampling of a 3D tree.Internal Logic
- Tests subsampling using a bounding box.
- Tests subsampling using a space.
- Verifies the resulting tree structure.
test_subsample_2d_tree
Description
Tests subsampling of a 2D tree.Internal Logic
Similar totest_subsample_3d_tree
, but for 2D trees.
test_keep_singular_root_edge
Description
Tests thekeep_singular_root_edge
parameter.
Internal Logic
- Creates a tree with a singular root edge.
- Tests subsampling with
keep_singular_root_edge=True
andFalse
. - Verifies the resulting tree structure in both cases.
test_subsample_with_ratio
Description
Tests subsampling using a ratio.Internal Logic
- Sets up a subsampler with a ratio of 0.7.
- Verifies the resulting tree structure after subsampling.
test_subsample_with_number
Description
Tests subsampling using a specific number of leaves.Internal Logic
- Sets up a subsampler with
number_of_leaves=2
. - Verifies the resulting tree structure after subsampling.
test_merge_cells
Description
Tests the cell merging functionality.Internal Logic
- Tests merging without a character matrix.
- Tests merging with a character matrix.
- Tests merging without collapsing duplicates.
- Verifies the resulting tree structure and attributes after merging.
Dependencies
unittest
: For creating and running unit tests.networkx
: For creating and manipulating graph structures.numpy
: For numerical operations and random number generation.pandas
: For data manipulation and analysis.cassiopeia.data.CassiopeiaTree
: The main tree structure being tested.cassiopeia.mixins
: For custom errors and warnings.cassiopeia.simulator.SpatialLeafSubsampler
: The class being tested.
Error Handling
The test cases extensively check for proper error handling in theSpatialLeafSubsampler
class, using assertRaises
to verify that appropriate exceptions are raised for invalid inputs or operations.