neurospatial.regions.core¶
core
¶
regions/core.py¶
Pure data layer for continuous regions of interest (ROIs).
Classes¶
Region
dataclass
¶
Region(name: str, kind: Kind, data: NDArray[float64] | Polygon | Point, metadata: Mapping[str, Any] = dict())
Immutable description of a spatial ROI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique region identifier. |
required |
kind
|
Kind
|
Either |
required |
data
|
NDArray[float64] | Polygon | Point
|
• point → |
required |
metadata
|
Mapping[str, Any]
|
Optional, JSON-serialisable attributes (colour, label, …). |
dict()
|
Functions¶
to_dict
¶
Export Region to a JSON-serializable dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation of the Region. |
Source code in src/neurospatial/regions/core.py
from_dict
classmethod
¶
Create Region from dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Mapping[str, Any]
|
Dictionary containing region data with keys: 'name', 'kind', 'geom', 'metadata'. |
required |
Returns:
| Type | Description |
|---|---|
Region
|
Reconstructed Region instance. |
Source code in src/neurospatial/regions/core.py
Regions
¶
Bases: MutableMapping[str, Region]
A small dict-like container mapping name → Region.
Provides the usual mapping API plus a few helpers
(add, remove, list_names, buffer, …).
Source code in src/neurospatial/regions/core.py
Functions¶
add
¶
add(name: str, *, point: PointCoords | None = None, polygon: Polygon | None = None, metadata: Mapping[str, Any] | None = None) -> Region
Create and insert a new Region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name for the region. |
required |
point
|
PointCoords or None
|
Point coordinates or Shapely Point object. Mutually exclusive with polygon. |
None
|
polygon
|
Polygon or None
|
Shapely Polygon object. Mutually exclusive with point. |
None
|
metadata
|
Mapping[str, Any] or None
|
Optional metadata dictionary to attach to the region. |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
The newly created Region instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both or neither of point/polygon are specified. |
KeyError
|
If name already exists in the collection. |
Source code in src/neurospatial/regions/core.py
update_region
¶
update_region(name: str, *, point: PointCoords | None = None, polygon: Polygon | None = None, metadata: Mapping[str, Any] | None = None) -> Region
Update an existing Region.
This method replaces an existing region with a new one. The region can change its type (point vs polygon) and/or data and/or metadata. Metadata is preserved from the existing region if not explicitly provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the region to update. |
required |
point
|
PointCoords or None
|
Point coordinates or Shapely Point object. Mutually exclusive with polygon. |
None
|
polygon
|
Polygon or None
|
Shapely Polygon object. Mutually exclusive with point. |
None
|
metadata
|
Mapping[str, Any] or None
|
Optional metadata dictionary to attach to the region. If None, preserves the existing region's metadata. |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
The newly created Region instance that replaced the old one. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both or neither of point/polygon are specified. |
KeyError
|
If name does not exist in the collection. |
Examples:
>>> from neurospatial.regions import Regions
>>> regs = Regions()
>>> _ = regs.add("center", point=[0.0, 0.0], metadata={"color": "red"})
>>> # Update coordinates while preserving metadata
>>> _ = regs.update_region("center", point=[1.0, 1.0])
>>> regs["center"].data
array([1., 1.])
>>> regs["center"].metadata["color"]
'red'
Source code in src/neurospatial/regions/core.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
remove
¶
Delete a region by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of region to remove. No error if absent. |
required |
list_names
¶
Get list of region names in insertion order.
Returns:
| Type | Description |
|---|---|
list[str]
|
Region names in the order they were added. |
area
¶
Compute area of a region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of region to query. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Area of the polygon region, or 0.0 for point regions. |
Source code in src/neurospatial/regions/core.py
region_center
¶
Calculate the center of a specified named region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region_name
|
str
|
Name of region to query. |
required |
Returns:
| Type | Description |
|---|---|
Optional[NDArray[float64]]
|
N-D coordinates of the region's center, or None if the region is empty or center cannot be determined. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in src/neurospatial/regions/core.py
buffer
¶
Create a buffered region around a point or existing region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str or NDArray[float64]
|
Region name or point coordinates to buffer around. |
required |
distance
|
float
|
Buffer distance in spatial units. |
required |
new_name
|
str
|
Name for the new buffered region. |
required |
**meta
|
Any
|
Additional metadata for the new region. |
{}
|
Returns:
| Type | Description |
|---|---|
Region
|
The newly created buffered region. |
Source code in src/neurospatial/regions/core.py
to_dataframe
¶
Convert this collection to a Pandas DataFrame. Requires Pandas to be installed.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns ['name', 'kind', 'data', 'metadata']. The 'data' column contains the coordinates for points or polygons. |
Source code in src/neurospatial/regions/core.py
to_json
¶
Write collection to disk in a simple, version-tagged schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Output file path for JSON data. |
required |
indent
|
int
|
Indentation level for pretty-printed JSON. |
2
|
Source code in src/neurospatial/regions/core.py
from_json
classmethod
¶
Load Regions from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Path to JSON file containing regions data. |
required |
Returns:
| Type | Description |
|---|---|
Regions
|
Loaded Regions collection. |