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 = 'region_name')
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
|
Calling function's argument name, shown in the corrected call. |
"region_name"
|
Examples:
>>> from neurospatial._exceptions import RegionNotFoundError
>>> try:
... raise RegionNotFoundError("goal")
... except KeyError as exc:
... print(str(exc))
Region 'goal' not found.
Fix: add it first: env.regions.add('goal', point=(x, y)) (or polygon=...), then pass region_name='goal'.
Source code in src/neurospatial/_exceptions.py
BinIndexOutOfRangeError
¶
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 ...
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
the same environment type (Cartesian Environment vs egocentric
EgocentricPolarEnvironment), 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
LayoutNotBuiltError
¶
Bases: RuntimeError, NeurospatialError
Raised when a :class:LayoutEngine is accessed before build().
Distinct from :class:EnvironmentNotFittedError: this signals that
the underlying layout engine itself has not been built (e.g. its
connectivity is None). 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.
Source code in src/neurospatial/_exceptions.py
EnvironmentNotFittedError
¶
EnvironmentNotFittedError(class_or_function_name: str, method_name: str | None = None, *, is_function: bool = False, error_code: str = 'E1004')
Bases: RuntimeError, NeurospatialError
Exception raised when an unfitted Environment is consumed.
This exception is raised both by the :func:check_fitted decorator on
bound methods and by free functions that receive an :class:Environment
argument. It supports two construction shapes:
- Bound-method form:
EnvironmentNotFittedError(class_name, method_name)— formats the message asEnvironment.method()with factory-method guidance. - Free-function form:
EnvironmentNotFittedError(function_name, *, is_function=True)— formats the message asfunction()(no class qualifier) and the same guidance about factory methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_or_function_name
|
str
|
For the bound-method form, the Environment class name (e.g. "Environment"). For the free-function form, the qualified function name (e.g. "path_progress" or "neurospatial.behavior.navigation.path_progress"). |
required |
method_name
|
str
|
Name of the method requiring initialization. Required for the
bound-method form; ignored when |
None
|
error_code
|
str
|
Error code for documentation reference. Default is "E1004". |
'E1004'
|
is_function
|
bool
|
If True, format the message as a free function (omit class qualifier). Default is False. |
False
|
Examples:
>>> from neurospatial.environment.decorators import EnvironmentNotFittedError
>>> raise EnvironmentNotFittedError("Environment", "bin_at")
Traceback (most recent call last):
...
neurospatial._exceptions.EnvironmentNotFittedError: [E1004] Environment.bin_at() requires...
>>> raise EnvironmentNotFittedError("path_progress", is_function=True)
Traceback (most recent call last):
...
neurospatial._exceptions.EnvironmentNotFittedError: [E1004] path_progress() requires...
See Also
check_fitted : Decorator that raises this exception for bound methods.
Notes
This exception inherits from RuntimeError to maintain backward
compatibility with existing code that catches RuntimeError. Users
can catch either EnvironmentNotFittedError for specific handling or
RuntimeError for general error handling.
Source code in src/neurospatial/_exceptions.py
GraphValidationError
¶
Bases: ValueError, NeurospatialError
Raised when connectivity graph has invalid structure or metadata.
This error indicates a bug in the layout engine that produced the graph, not a user error. All layout engines must produce graphs that pass validation.
See Also
validate_connectivity_graph : Main validation function