High-level description
This code defines a test suite for theClonalSpatialDataSimulator class, which is responsible for simulating spatial data with clonal spatial autocorrelation constraints. The tests cover the initialization of the simulator and the overlay of spatial data onto a CassiopeiaTree object.
Code Structure
The main classTestClonalSpatialDataSimulator contains several test methods that check different aspects of the ClonalSpatialDataSimulator functionality. The setUp method initializes common objects used across multiple tests.
Symbols
TestClonalSpatialDataSimulator
Description
A test class that inherits fromunittest.TestCase to test the ClonalSpatialDataSimulator class.
Internal Logic
- Sets up a basic tree structure and cell metadata for testing.
- Tests the initialization of the simulator with different parameters.
- Tests the overlay of spatial data onto a tree without existing cell metadata.
- Tests the overlay of spatial data onto a tree with existing cell metadata.
setUp
Description
Initializes the test environment by creating a basic tree structure and cell metadata.Internal Logic
- Sets a random seed for reproducibility.
- Creates a directed graph representing a tree topology.
- Initializes a
CassiopeiaTreeobject with the created topology. - Creates a DataFrame with cell metadata for some leaves of the tree.
test_init
Description
Tests the initialization of theClonalSpatialDataSimulator with different parameters.
Internal Logic
- Checks that initializing without parameters raises a
DataSimulatorError. - Tests initialization with a 2D space, verifying the dimension and space shape.
- Tests initialization with a 3D space, verifying the dimension and space shape.
test_overlay_data
Description
Tests theoverlay_data method of ClonalSpatialDataSimulator on a basic tree without existing cell metadata.
Internal Logic
- Initializes a simulator with a 2D space.
- Overlays spatial data onto the basic tree.
- Checks that spatial coordinates are assigned to all nodes in the tree.
- Verifies that the cell metadata is correctly updated with spatial coordinates for leaf nodes.
test_overlay_data_with_space
Description
Tests theoverlay_data method with a custom space parameter.
Internal Logic
Similar totest_overlay_data, but initializes the simulator with a custom space parameter.
test_overlay_data_with_existing_cell_meta
Description
Tests theoverlay_data method on a tree with existing cell metadata.
Internal Logic
- Initializes a simulator with a 2D space.
- Overlays spatial data onto the tree with existing cell metadata.
- Verifies that spatial coordinates are assigned to all nodes.
- Checks that the existing cell metadata is preserved and new spatial coordinates are added correctly.
Dependencies
| Dependency | Purpose |
|---|---|
| unittest | Provides the testing framework |
| networkx | Used for creating and manipulating graph structures |
| numpy | Used for numerical operations and testing array equality |
| pandas | Used for handling DataFrames (cell metadata) |
| cassiopeia | The main package being tested, provides CassiopeiaTree and ClonalSpatialDataSimulator |
Error Handling
The test cases useself.assertRaises to check for expected exceptions, particularly DataSimulatorError when initializing the simulator incorrectly.
Notes
- The tests use fixed random seeds to ensure reproducibility of spatial coordinate generation.
- The expected coordinate values in the tests are hardcoded, which might make the tests brittle if the underlying simulation algorithm changes.
