> ## 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.

# spatial_leaf_subsampler_test.py

Here's a detailed documentation of the `test/simulator_tests/spatial_leaf_subsampler_test.py` file:

## High-level description

This file contains unit tests for the `SpatialLeafSubsampler` 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 class `SpatialLeafSubsamplerTest` 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 the `SpatialLeafSubsampler` class.

#### Internal Logic

1. Sets up test data in the `setUp` method.
2. Tests various aspects of the `SpatialLeafSubsampler` class through individual test methods.

### `setUp`

#### Description

Initializes test data for the test cases.

#### Internal Logic

1. Creates a balanced tree with spatial attributes.
2. Sets up 2D and 3D versions of the tree.
3. 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 for `LeafSubsamplerError` in various scenarios:

1. Both ratio and number of leaves provided
2. Neither space nor bounding box provided
3. Both space and bounding box provided
4. Negative number of leaves
5. Ratio > 1
6. Merge cells without space

### `test_bad_subsample_parameters`

#### Description

Tests error handling for invalid subsampling parameters.

#### Internal Logic

Checks for `LeafSubsamplerError` in various scenarios:

1. Tree without spatial attributes
2. Incompatible space dimensions
3. Incompatible bounding box dimensions
4. Leaf coordinates outside space
5. No leaves in region
6. Number of leaves too large

### `test_bad_scale`

#### Description

Tests warning generation for potentially problematic scale issues.

#### Internal Logic

1. Creates a tree with very small spatial coordinates.
2. Checks for `LeafSubsamplerWarning` when subsampling.

### `test_subsample_3d_tree`

#### Description

Tests subsampling of a 3D tree.

#### Internal Logic

1. Tests subsampling using a bounding box.
2. Tests subsampling using a space.
3. Verifies the resulting tree structure.

### `test_subsample_2d_tree`

#### Description

Tests subsampling of a 2D tree.

#### Internal Logic

Similar to `test_subsample_3d_tree`, but for 2D trees.

### `test_keep_singular_root_edge`

#### Description

Tests the `keep_singular_root_edge` parameter.

#### Internal Logic

1. Creates a tree with a singular root edge.
2. Tests subsampling with `keep_singular_root_edge=True` and `False`.
3. Verifies the resulting tree structure in both cases.

### `test_subsample_with_ratio`

#### Description

Tests subsampling using a ratio.

#### Internal Logic

1. Sets up a subsampler with a ratio of 0.7.
2. Verifies the resulting tree structure after subsampling.

### `test_subsample_with_number`

#### Description

Tests subsampling using a specific number of leaves.

#### Internal Logic

1. Sets up a subsampler with `number_of_leaves=2`.
2. Verifies the resulting tree structure after subsampling.

### `test_merge_cells`

#### Description

Tests the cell merging functionality.

#### Internal Logic

1. Tests merging without a character matrix.
2. Tests merging with a character matrix.
3. Tests merging without collapsing duplicates.
4. 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 the `SpatialLeafSubsampler` class, using `assertRaises` to verify that appropriate exceptions are raised for invalid inputs or operations.

## Logging

This test file does not implement any specific logging mechanisms.

## TODOs

There are no explicit TODOs in this test file.
