High-level description
This file contains unit tests for theBrownianSpatialDataSimulator class, which is part of the Cassiopeia package. The tests verify the initialization of the simulator and its ability to overlay spatial data onto a CassiopeiaTree object, both with and without existing cell metadata.
Code Structure
The code defines a single test classTestBrownianSpatialDataSimulator that inherits from unittest.TestCase. It contains a setUp method to initialize test data and several test methods to verify different aspects of the BrownianSpatialDataSimulator class.
Symbols
TestBrownianSpatialDataSimulator
Description
A test class for theBrownianSpatialDataSimulator class, containing multiple test methods to verify its functionality.
Internal Logic
- Sets up test data in the
setUpmethod, including a basic tree and a tree with cell metadata. - Tests the initialization of the simulator with various parameters.
- Tests the overlay of spatial data onto a basic tree and verifies the results.
- Tests the overlay of spatial data without scaling to unit area.
- Tests the overlay of spatial data onto a tree with existing cell metadata.
setUp
Description
Initializes test data, including a basic tree and a tree with cell metadata.Internal Logic
- Creates a directed graph representing a tree topology.
- Initializes a
CassiopeiaTreeobject with the topology. - Sets node times for the tree.
- Creates a DataFrame with cell metadata for some nodes.
- Initializes another
CassiopeiaTreeobject with the topology and cell metadata.
test_init
Description
Tests the initialization of theBrownianSpatialDataSimulator with various parameters.
Internal Logic
- Attempts to initialize the simulator with invalid parameters (dimension 0 and negative diffusion coefficient) and expects
DataSimulatorErrorto be raised. - Initializes the simulator with valid parameters and verifies the attribute values.
test_overlay_data
Description
Tests theoverlay_data method of the BrownianSpatialDataSimulator on a basic tree.
Internal Logic
- Initializes the simulator with 2 dimensions and a diffusion coefficient of 1.
- Overlays spatial data onto the basic tree.
- Verifies the generated spatial coordinates for each node.
- Checks that the cell metadata is correctly updated with spatial coordinates.
test_overlay_data_without_scale
Description
Tests theoverlay_data method without scaling the coordinates to unit area.
Internal Logic
- Initializes the simulator with
scale_unit_area=False. - Overlays spatial data onto the basic tree.
- Verifies the generated spatial coordinates for each node.
- Checks that the cell metadata is correctly updated with spatial coordinates.
test_overlay_data_with_existing_cell_meta
Description
Tests theoverlay_data method on a tree with existing cell metadata.
Internal Logic
- Initializes the simulator with 2 dimensions and a diffusion coefficient of 1.
- Overlays spatial data onto the tree with existing cell metadata.
- Verifies the generated spatial coordinates for each node.
- Checks that the cell metadata is correctly updated with spatial coordinates while preserving existing metadata.
Dependencies
| Dependency | Purpose |
|---|---|
| unittest | Provides the testing framework |
| networkx | Used for creating and manipulating graph structures |
| numpy | Used for numerical operations and testing |
| pandas | Used for handling DataFrames and testing |
| cassiopeia | The package being tested |
Error Handling
The tests useself.assertRaises to verify that the BrownianSpatialDataSimulator raises DataSimulatorError when initialized with invalid parameters.
Notes
- The tests use a fixed random seed (0) to ensure reproducibility of the generated spatial coordinates.
- The expected coordinates in the tests are hardcoded, which might make the tests brittle if the underlying implementation changes.
