> ## Documentation Index
> Fetch the complete documentation index at: https://demo.agenticlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# brownian_spatial_simulator_test.py

## High-level description

This file contains unit tests for the `BrownianSpatialDataSimulator` 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 class `TestBrownianSpatialDataSimulator` 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 the `BrownianSpatialDataSimulator` class, containing multiple test methods to verify its functionality.

#### Internal Logic

1. Sets up test data in the `setUp` method, including a basic tree and a tree with cell metadata.
2. Tests the initialization of the simulator with various parameters.
3. Tests the overlay of spatial data onto a basic tree and verifies the results.
4. Tests the overlay of spatial data without scaling to unit area.
5. 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

1. Creates a directed graph representing a tree topology.
2. Initializes a `CassiopeiaTree` object with the topology.
3. Sets node times for the tree.
4. Creates a DataFrame with cell metadata for some nodes.
5. Initializes another `CassiopeiaTree` object with the topology and cell metadata.

### test\_init

#### Description

Tests the initialization of the `BrownianSpatialDataSimulator` with various parameters.

#### Internal Logic

1. Attempts to initialize the simulator with invalid parameters (dimension 0 and negative diffusion coefficient) and expects `DataSimulatorError` to be raised.
2. Initializes the simulator with valid parameters and verifies the attribute values.

### test\_overlay\_data

#### Description

Tests the `overlay_data` method of the `BrownianSpatialDataSimulator` on a basic tree.

#### Internal Logic

1. Initializes the simulator with 2 dimensions and a diffusion coefficient of 1.
2. Overlays spatial data onto the basic tree.
3. Verifies the generated spatial coordinates for each node.
4. Checks that the cell metadata is correctly updated with spatial coordinates.

### test\_overlay\_data\_without\_scale

#### Description

Tests the `overlay_data` method without scaling the coordinates to unit area.

#### Internal Logic

1. Initializes the simulator with `scale_unit_area=False`.
2. Overlays spatial data onto the basic tree.
3. Verifies the generated spatial coordinates for each node.
4. Checks that the cell metadata is correctly updated with spatial coordinates.

### test\_overlay\_data\_with\_existing\_cell\_meta

#### Description

Tests the `overlay_data` method on a tree with existing cell metadata.

#### Internal Logic

1. Initializes the simulator with 2 dimensions and a diffusion coefficient of 1.
2. Overlays spatial data onto the tree with existing cell metadata.
3. Verifies the generated spatial coordinates for each node.
4. 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 use `self.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.
