neurospatial.composite¶
composite
¶
CompositeEnvironment: merges multiple Environment instances into a single unified Environment-like API. Bridge edges between sub-environments are inferred automatically via mutual-nearest-neighbor (MNN).
This class exposes the same public interface as the base Environment class:
- Properties: n_dims, n_bins, bin_centers, connectivity, is_1d, dimension_ranges,
grid_edges, grid_shape, active_mask, regions
- Methods: bin_at, contains, neighbors, distance_between, bin_center_of,
bins_in_region, mask_for_region, path_between, info,
save, load, bin_attributes, edge_attributes, plot
(Note: factory methods like from_layout are not included, since CompositeEnvironment wraps pre-fitted sub-environments. plot_1d is not applicable for composite environments.)
Classes¶
CompositeEnvironment
¶
CompositeEnvironment(subenvs: list[Environment], auto_bridge: bool = True, max_mnn_distance: float | None = None, use_kdtree_query: bool = True)
A composite environment that merges multiple child Environment instances into one.
It automatically infers "bridge" edges between every pair of sub-environments by finding
mutually nearest neighbor bin-centers (MNN). It then presents the same interface as
the base Environment class.
Attributes:
| Name | Type | Description |
|---|---|---|
environments |
List[Environment]
|
List of constituent Environment instances that make up the composite. |
name |
str
|
Name for the composite environment. |
layout |
None
|
Not applicable for composite environments (set to None). |
bin_centers |
NDArray[float64]
|
Combined bin centers from all sub-environments, shape (n_total_bins, n_dims). |
connectivity |
Graph
|
Combined connectivity graph with bridge edges between sub-environments. |
bridges |
List[Tuple[int, int, Dict[str, Any]]]
|
List of bridge edges connecting different sub-environments. Each tuple is (source_bin, target_bin, edge_attributes). |
dimension_ranges |
Sequence[Tuple[float, float]]
|
Combined dimension ranges across all sub-environments. |
grid_edges |
Tuple[NDArray[float64], ...] | None
|
Not applicable for composite environments (set to None). |
grid_shape |
Tuple[int, ...] | None
|
Not applicable for composite environments (set to None). |
active_mask |
NDArray[bool_] | None
|
Not applicable for composite environments (set to None). |
regions |
Regions
|
Manages symbolic spatial regions defined within this composite environment. |
is_1d |
bool
|
True if all sub-environments are 1D, False otherwise. |
_environment_bin_ranges |
Dict[str, Tuple[int, int]]
|
Mapping of sub-environment names to their bin index ranges in the composite. |
_layout_type_used |
str
|
Always "Composite" for composite environments. |
_layout_params_used |
Dict[str, Any]
|
Parameters used to construct the composite. |
Build a CompositeEnvironment from a list of pre-fitted Environment instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subenvs
|
List[Environment]
|
A list of fitted Environment objects. All must share the same n_dims. |
required |
auto_bridge
|
bool
|
If True, automatically infer "bridge edges" between each pair of sub-environments using a mutual nearest-neighbor heuristic on their bin_centers. |
True
|
max_mnn_distance
|
Optional[float]
|
If provided, any automatically inferred bridge whose Euclidean distance exceeds this threshold is discarded. If None, no distance filtering is applied. |
None
|
use_kdtree_query
|
bool
|
If True, use KDTree-based bin_at() for O(M log N) performance. If False, use sequential query through each sub-environment (original O(N×M) behavior). |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If subenvs is not a list or tuple, or if any element is not an Environment instance. |
ValueError
|
If subenvs is empty, if any environment is not fitted, or if environments have different dimensionalities. |
Common Pitfalls
-
Dimension mismatch: All sub-environments must have the same number of dimensions (n_dims). Mixing 2D and 3D environments will raise an error. Before creating the composite, verify that all environments have the same n_dims property (e.g., check env1.n_dims == env2.n_dims). This typically occurs when combining data from different recording modalities.
-
No bridge edges found: If auto_bridge=True but the sub-environments are very far apart, no bridge edges may be created, leaving the composite disconnected. Try increasing max_mnn_distance to allow bridges over longer distances, or set auto_bridge=False if you intend to work with disconnected components. Use the bridges property to verify that bridge edges were created.
-
Overlapping bins: If sub-environments have bins at the same or very similar spatial locations, the composite will have duplicate bins at those locations. This can lead to unexpected behavior in spatial queries. Ensure that sub-environments represent distinct, non-overlapping spatial regions (e.g., different arms of a maze, different rooms). Check bin_centers to verify that bin locations are spatially separated.
Source code in src/neurospatial/composite.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
Attributes¶
n_dims
property
¶
Number of spatial dimensions (same as each sub-environment).
Returns:
| Type | Description |
|---|---|
int
|
Number of spatial dimensions. |
n_bins
property
¶
Total number of active bins in the composite environment.
Returns:
| Type | Description |
|---|---|
int
|
Total number of bins across all sub-environments. |
layout_parameters
property
¶
Returns parameters used to construct the CompositeEnvironment.
Functions¶
bin_at
¶
Map points to composite bin indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points_nd
|
(NDArray[float64], shape(M, n_dims))
|
Array of M points in n_dims-dimensional space. |
required |
Returns:
| Type | Description |
|---|---|
(NDArray[int_], shape(M))
|
Composite bin indices for each point. Returns -1 for points outside all sub-environments. |
Notes
If use_kdtree_query=True (default), uses KDTree for O(M log N) performance. Otherwise, sequentially queries each sub-environment for O(N×M) performance.
The KDTree approach finds nearest bin centers globally, then verifies each point is actually contained by that bin using the sub-environment's contains() method. This is much faster for large numbers of sub-environments.
Source code in src/neurospatial/composite.py
contains
¶
Check if points are contained in any bin of the composite environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points_nd
|
(NDArray[float64], shape(M, n_dims))
|
Array of M points in n_dims-dimensional space. |
required |
Returns:
| Type | Description |
|---|---|
(NDArray[bool_], shape(M))
|
Boolean array where True indicates point is within any bin. Equivalent to self.bin_at(points_nd) != -1. |
Source code in src/neurospatial/composite.py
neighbors
¶
Get neighboring bins in the merged connectivity graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bin_index
|
int
|
Composite bin index to query. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of composite bin indices that are neighbors of bin_index. Can be used directly for array indexing. |
Source code in src/neurospatial/composite.py
distance_between
¶
distance_between(point1: ndarray | list[float] | tuple[float, ...], point2: ndarray | list[float] | tuple[float, ...], edge_weight: str = 'distance') -> float
Compute shortest-path distance between two points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point1
|
ndarray or list or tuple
|
First point coordinates (length n_dims). |
required |
point2
|
ndarray or list or tuple
|
Second point coordinates (length n_dims). |
required |
edge_weight
|
str
|
Edge attribute to use as weight for path computation. |
"distance"
|
Returns:
| Type | Description |
|---|---|
float
|
Shortest path distance between the two points. Returns np.inf if either point is outside all sub-environments. |
Notes
Maps each point to a bin index via bin_at, then computes the shortest path length in the connectivity graph.
Source code in src/neurospatial/composite.py
bin_center_of
¶
Get bin center coordinates for specified bin indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bin_indices
|
int or NDArray[int_]
|
Single composite bin index or 1-D array of bin indices. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
N-D coordinate(s) of the specified bin(s). Shape (n_dims,) for a single index, or (M, n_dims) for M indices. |
Source code in src/neurospatial/composite.py
bin_attributes
¶
Get concatenated DataFrame of per-bin attributes from all sub-environments.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Concatenated bin attributes with columns 'child_active_bin_id' and 'composite_bin_id' added to track mapping from sub-environment bins to composite bins. |
Source code in src/neurospatial/composite.py
edge_attributes
¶
Get concatenated DataFrame of per-edge attributes from all sub-environments.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Concatenated edge attributes with 'u_idx' and 'v_idx' shifted to composite bin indices. Includes MNN-inferred bridge edges connecting sub-environments. |
Source code in src/neurospatial/composite.py
bins_in_region
¶
Get composite bin indices that fall within a specified named region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region_name
|
str
|
Name of a defined region in |
required |
Returns:
| Type | Description |
|---|---|
NDArray[np.int_] of shape (n_bins_in_region,)
|
Array of composite bin indices (0 to n_bins - 1) that fall within the specified region. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If region type is unsupported or dimensions mismatch. |
Notes
This method queries the region against all bin centers in the composite environment. For point regions, returns bins containing that point. For polygon regions (requires shapely), returns all bins whose centers fall within the polygon.
Examples:
>>> comp = CompositeEnvironment([env1, env2])
>>> comp.regions.add("goal", point=[10.0, 5.0])
>>> goal_bins = comp.bins_in_region("goal")
>>> print(f"Goal region contains {len(goal_bins)} bins")
Source code in src/neurospatial/composite.py
mask_for_region
¶
Get boolean mask for bins in a specified region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region_name
|
str
|
Name of a defined region in |
required |
Returns:
| Type | Description |
|---|---|
(NDArray[bool_], shape(n_bins))
|
Boolean mask where True indicates the bin is within the region. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ValueError
|
If region type is unsupported or dimensions mismatch. |
Notes
This is a convenience method that returns a boolean mask instead of bin indices. Equivalent to: mask = np.zeros(env.n_bins, dtype=bool) mask[env.bins_in_region(region_name)] = True
Examples:
>>> comp = CompositeEnvironment([env1, env2])
>>> comp.regions.add("arena", polygon=shapely_polygon)
>>> arena_mask = comp.mask_for_region("arena")
>>> occupancy_in_arena = occupancy[arena_mask]
Source code in src/neurospatial/composite.py
path_between
¶
Find shortest path between two bin indices in the composite graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_bin
|
int
|
Composite bin index to start from (0 to n_bins - 1). |
required |
target_bin
|
int
|
Composite bin index to reach (0 to n_bins - 1). |
required |
edge_weight
|
str
|
Edge attribute to use as weight for pathfinding. |
"distance"
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of composite bin indices forming the shortest path from source_bin to target_bin, including both endpoints. Returns empty list if no path exists. |
Raises:
| Type | Description |
|---|---|
NodeNotFound
|
If source_bin or target_bin is not in the graph. |
Warnings
UserWarning If no path exists between the bins (disconnected components).
Notes
Uses NetworkX shortest_path with specified edge weights. The path may cross bridge edges connecting different sub-environments.
Examples:
>>> comp = CompositeEnvironment([env1, env2], auto_bridge=True)
>>> path = comp.path_between(0, 100) # Path from bin 0 to bin 100
>>> print(f"Path length: {len(path)} bins")
Source code in src/neurospatial/composite.py
info
¶
Print or return diagnostic information about the composite environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_string
|
bool
|
If True, return the info string instead of printing. |
False
|
Returns:
| Type | Description |
|---|---|
str or None
|
If return_string=True, returns the formatted info string. Otherwise prints to stdout and returns None. |
Notes
Displays summary information including: - Number of sub-environments - Total bins and dimensions - Number of bridge edges connecting sub-environments - Per-sub-environment statistics (type, bins, regions) - Bridge edge statistics
Examples:
>>> comp = CompositeEnvironment([env1, env2], auto_bridge=True)
>>> comp.info()
Composite Environment Information
==================================
...
Source code in src/neurospatial/composite.py
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 | |
save
¶
Save the CompositeEnvironment to a file using pickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path where the composite environment will be saved. |
required |
Warnings
This method uses pickle serialization. Only load files from trusted sources, as pickle can execute arbitrary code.
Notes
The saved file contains: - All sub-environments with their complete state - Bridge edges and connectivity information - Regions from all sub-environments - Composite metadata
The file can be loaded with CompositeEnvironment.load().
Examples:
>>> comp = CompositeEnvironment([env1, env2])
>>> comp.save("my_composite_env.pkl")
>>> loaded = CompositeEnvironment.load("my_composite_env.pkl")
Source code in src/neurospatial/composite.py
load
classmethod
¶
Load a CompositeEnvironment from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the saved composite environment file. |
required |
Returns:
| Type | Description |
|---|---|
CompositeEnvironment
|
Reconstructed composite environment with all sub-environments and bridge edges restored. |
Warnings
This method uses pickle deserialization. Only load files from trusted sources, as pickle can execute arbitrary code.
Examples:
>>> comp = CompositeEnvironment.load("my_composite_env.pkl")
>>> print(f"Loaded composite with {comp.n_bins} bins")
Source code in src/neurospatial/composite.py
plot
¶
plot(ax: Axes | None = None, sub_env_plot_kwargs: dict[str, Any] | list[dict[str, Any] | None] | None = None, bridge_edge_kwargs: dict[str, Any] | None = None, show_sub_env_labels: bool = False, **kwargs) -> matplotlib.axes.Axes
Plot the composite environment.
This method plots each sub-environment and then overlays the bridge edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Optional[Axes]
|
The Matplotlib axes to plot on. If None, a new figure and axes are created. Defaults to None. |
None
|
sub_env_plot_kwargs
|
Optional[Union[Dict[str, Any], List[Optional[Dict[str, Any]]]]]
|
Keyword arguments to pass to the |
None
|
bridge_edge_kwargs
|
Optional[Dict[str, Any]]
|
Keyword arguments for plotting the bridge edges (passed to |
None
|
show_sub_env_labels
|
bool
|
If True, attempts to label the approximate center of each sub-environment. |
False
|
**kwargs
|
Any
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Axes
|
The axes on which the composite environment was plotted. |
Source code in src/neurospatial/composite.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 | |