neurospatial._exceptions¶
_exceptions
¶
Public exception classes and error-message formatting.
Library errors inherit a standard Python exception first and the common NeurospatialError base second. This module has no internal dependencies.
Classes¶
NeurospatialError
¶
Bases: Exception
Base class for every exception neurospatial defines.
Each concrete error also inherits a built-in type, listed first, so
except ValueError keeps working. except NeurospatialError catches
only problems that neurospatial itself detected.
Subclasses build their message from structured constructor arguments, so
the default pickling (which re-calls the class with the formatted message)
would fail or double-format. Each subclass __init__ therefore records
its arguments, and __reduce__ rebuilds the error from them, which lets
errors raised in worker processes reach the caller intact.
RegionNotFoundError
¶
RegionNotFoundError(name: str | None, *, available: list[str] | None = None, argument: str | None = 'region_name', in_list: bool = False)
Bases: KeyError, ValueError, NeurospatialError
Raised when a region name is requested but not in the Regions container.
Inherits from :class:KeyError and :class:ValueError so existing
catch blocks for region lookups and segmentation keep working.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str or None
|
Requested region name; |
required |
available
|
list of str
|
Available region names, used to suggest a close match. |
None
|
argument
|
str or None
|
Calling function's argument name, shown in the corrected call. None
for a lookup by key, such as |
"region_name"
|
in_list
|
bool
|
Whether |
False
|
Examples:
>>> from neurospatial._exceptions import RegionNotFoundError
>>> try:
... raise RegionNotFoundError("goal")
... except KeyError as exc:
... print(str(exc))
Region 'goal' not found.
Fix: create regions = Regions(env.regions.values()); regions.add('goal', point=(x, y)) (or polygon=...); env = env.with_regions(regions), then pass region_name='goal'.
Source code in src/neurospatial/_exceptions.py
BinIndexOutOfRangeError
¶
BinIndexOutOfRangeError(index: int, *, n_bins: int, coordinate_kind: Literal['cartesian', 'polar'] = 'cartesian')
Bases: ValueError, NeurospatialError
Raised when a bin index falls outside [0, n_bins).
Inherits from :class:ValueError so existing except ValueError
blocks around bin lookups keep working.
Examples:
>>> from neurospatial._exceptions import BinIndexOutOfRangeError
>>> raise BinIndexOutOfRangeError(99, n_bins=42)
Traceback (most recent call last):
...
neurospatial._exceptions.BinIndexOutOfRangeError: Bin index 99 ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The refused index. |
required |
n_bins
|
int
|
Number of active bins. |
required |
coordinate_kind
|
('cartesian', 'polar')
|
The environment's coordinates, so the fix shows the rows |
"cartesian"
|
Source code in src/neurospatial/_exceptions.py
IncompatibleEnvironmentError
¶
IncompatibleEnvironmentError(message: str, *, fix: str = 'use environments that share the property named above.', first: object | None = None, second: object | None = None)
Bases: ValueError, NeurospatialError
Raised when two environments are required to share a property but do not.
Typical examples: composing a 2D environment with a 3D one,
requiring matching bin_size between source and target, requiring
compatible coordinate meanings (Cartesian positions or egocentric
radius/angle), etc.
Inherits from :class:ValueError so existing except ValueError
blocks keep working.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
What differs between the environments. |
required |
fix
|
str
|
The |
'use environments that share the property named above.'
|
first
|
object
|
The two mismatched objects, kept as attributes for inspection. |
None
|
second
|
object
|
The two mismatched objects, kept as attributes for inspection. |
None
|
Source code in src/neurospatial/_exceptions.py
CartesianOnlyError
¶
Bases: TypeError, NeurospatialError
Raised when an operation needs Cartesian positions and the environment is polar.
A polar Environment stores (distance, angle) bins around the
animal, so an operation that reads positions or bin centers as Cartesian
(x, y[, z]) coordinates would return plausible but wrong numbers. The
operation does not apply to this kind of environment, so the error is a
:class:TypeError; static type checkers flag the same calls. Every
Cartesian-only method, Cartesian-only mode and Cartesian-only analysis
function raises it with the same message layout: what was refused, why it
needs Cartesian coordinates, and what works on a polar environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
str
|
The refused call, e.g. |
required |
why
|
str
|
Why the operation needs Cartesian coordinates. |
required |
fix
|
str
|
What to use instead on a polar environment. |
required |
example
|
str or None
|
Code for the alternative, shown after the fix: statements separated by
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
operation |
str
|
The refused call. |
example |
str or None
|
The code shown after the fix. |
Source code in src/neurospatial/_exceptions.py
LayoutNotBuiltError
¶
Bases: RuntimeError, NeurospatialError
Raised when a :class:LayoutEngine is accessed before build().
This signals that the layout engine has not been built (e.g. its
binning() is requested before build()). It is mostly raised inside layout
engines and helpers; user code typically sees it surfacing through
a layout property accessed at the wrong time.
Inherits from :class:RuntimeError to match the broader
"object-not-ready" exception family.