clonal_spatial_simulator_test.py
High-level description
This code defines a test suite for the ClonalSpatialDataSimulator
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 class TestClonalSpatialDataSimulator
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 from unittest.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
CassiopeiaTree
object with the created topology. - Creates a DataFrame with cell metadata for some leaves of the tree.
test_init
Description
Tests the initialization of the ClonalSpatialDataSimulator
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 the overlay_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 the overlay_data
method with a custom space parameter.
Internal Logic
Similar to test_overlay_data
, but initializes the simulator with a custom space parameter.
test_overlay_data_with_existing_cell_meta
Description
Tests the overlay_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 use self.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.