Utils Checks#
checks
#
| CLASS | DESCRIPTION |
|---|---|
Interval |
Represents a mathematical interval with bounds and inclusivity. |
RangeValidationError |
Custom exception for range validation. |
| FUNCTION | DESCRIPTION |
|---|---|
check_in_interval |
Validate that a value is within the specified interval. |
check_isinstance |
Check that all elements in X are of the specified type. |
check_same_shape |
Check that two tensors have the same shape along specified axis. |
check_same_len |
Check that two array-like objects have the same length. |
check_same_device |
Check that two tensor objects are on the same device. |
check_square_shape |
Check that tensor(s) have square shape (n_rows == n_cols). |
check_ndim |
Check that tensor(s) have the specified number of dimensions. |
check_2d |
Check that tensor(s) are 2-dimensional. |
check_1d |
Check that tensor(s) are 1-dimensional. |
check_array_type |
Checks if X is a torch.Tensor or np.ndarray (array-like type). |
check_is_tensor |
Checks if X is a torch.Tensor and if not, raises a TypeError. |
check_to_tensor |
Convert array-like input to torch.Tensor. |
check_sample_weights |
Check that |
experimental |
This is a decorator which can be used to mark functions as experimental. |
Classes#
Interval(left: float, right: float, left_closed: bool, right_closed: bool)
dataclass
#
RangeValidationError(value: float, interval: Interval | None = None, min_val: float | None = None, max_val: float | None = None)
#
Bases: ValueError
Custom exception for range validation.
Initialize RangeValidationError.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The invalid value.
TYPE:
|
interval
|
The interval constraint that was violated, by default None.
TYPE:
|
min_val
|
Minimum allowed value (for backward compatibility), by default None.
TYPE:
|
max_val
|
Maximum allowed value (for backward compatibility), by default None.
TYPE:
|
Source code in spectre/utils/checks.py
Functions#
parse_interval(interval_str: str) -> Interval
#
Parse mathematical interval notation into an Interval object.
Supports standard mathematical interval notation: - [a, b]: closed interval (a ≤ x ≤ b) - (a, b): open interval (a < x < b) - [a, b): left-closed, right-open (a ≤ x < b) - (a, b]: left-open, right-closed (a < x ≤ b) - Infinite bounds: (-∞, b), (a, ∞), (-∞, ∞)
| PARAMETER | DESCRIPTION |
|---|---|
interval_str
|
Interval in mathematical notation, e.g., "[0, 1)", "(0, ∞)".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Interval
|
Parsed interval object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the interval string cannot be parsed. |
Examples:
Source code in spectre/utils/checks.py
check_in_interval(value: float, interval: str | Interval) -> None
#
Validate that a value is within the specified interval.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Value to validate.
TYPE:
|
interval
|
Interval constraint. Can be a string in mathematical notation (e.g., "[0, 1)") or an Interval object.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RangeValidationError
|
If the value is not within the specified interval. |
Examples:
Source code in spectre/utils/checks.py
check_isinstance(X: list | Any, type: Any) -> None
#
Check that all elements in X are of the specified type.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Single object or list/tuple of objects to check.
TYPE:
|
type
|
Expected type for all elements.
TYPE:
|
Source code in spectre/utils/checks.py
check_same_shape(X: np.ndarray | torch.Tensor, Y: np.ndarray | torch.Tensor, axis: int | None = None) -> None
#
Check that two tensors have the same shape along specified axis.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First tensor to compare.
TYPE:
|
Y
|
Second tensor to compare.
TYPE:
|
axis
|
Axis along which to compare shapes. If None, compares full shapes.
TYPE:
|
Source code in spectre/utils/checks.py
check_same_len(X: list | np.ndarray | torch.Tensor, Y: list | np.ndarray | torch.Tensor) -> None
#
Check that two array-like objects have the same length.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First array-like object.
TYPE:
|
Y
|
Second array-like object.
TYPE:
|
Source code in spectre/utils/checks.py
check_same_device(X: torch.Tensor, Y: torch.Tensor) -> None
#
Check that two tensor objects are on the same device.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First tensor object.
TYPE:
|
Y
|
Second tensor object.
TYPE:
|
Source code in spectre/utils/checks.py
check_same_dtype(X: torch.Tensor, Y: torch.Tensor) -> None
#
Check that two tensor objects have the same dtype.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First tensor object.
TYPE:
|
Y
|
Second tensor object.
TYPE:
|
Source code in spectre/utils/checks.py
check_square_shape(X: np.ndarray | torch.Tensor | list) -> None
#
Check that tensor(s) have square shape (n_rows == n_cols).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Tensor or list of tensors to check for square shape.
TYPE:
|
Source code in spectre/utils/checks.py
check_ndim(X: np.ndarray | torch.Tensor | list, ndim: int = 1) -> None
#
Check that tensor(s) have the specified number of dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Tensor or list of tensors to check.
TYPE:
|
ndim
|
Expected number of dimensions, by default 1.
TYPE:
|
Source code in spectre/utils/checks.py
check_2d(X: np.ndarray | torch.Tensor | list) -> None
#
Check that tensor(s) are 2-dimensional.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Tensor or list of tensors to check.
TYPE:
|
Source code in spectre/utils/checks.py
check_1d(X: np.ndarray | torch.Tensor | list) -> None
#
Check that tensor(s) are 1-dimensional.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Tensor or list of tensors to check.
TYPE:
|
Source code in spectre/utils/checks.py
check_array_type(X: ArrayLike) -> None
#
Checks if X is a torch.Tensor or np.ndarray (array-like type).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
Source code in spectre/utils/checks.py
check_is_tensor(X: Any) -> None
#
Checks if X is a torch.Tensor and if not, raises a TypeError.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
Source code in spectre/utils/checks.py
check_to_tensor(X: ArrayLike, dtype: torch.dtype = torch.float32, device: torch.device | None = None) -> torch.Tensor
#
Convert array-like input to torch.Tensor.
Handles both numpy arrays and existing torch tensors, converting them to the specified dtype and device.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input array (numpy array or torch tensor).
TYPE:
|
dtype
|
Target data type for the tensor, by default torch.float32.
TYPE:
|
device
|
Target device for the tensor. If None, keeps original device. By default None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Converted tensor with specified dtype and device. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If X is not a torch.Tensor or np.ndarray. |
Source code in spectre/utils/checks.py
check_from_tensor(X: torch.Tensor) -> np.ndarray
#
Convert torch.Tensor to numpy array.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Converted numpy array on CPU. |
Source code in spectre/utils/checks.py
check_all_positive(X: torch.Tensor) -> None
#
Check that all elements in the array-like input are positive.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input array.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any element in X is not positive. |
Source code in spectre/utils/checks.py
check_all_non_negative(X: torch.Tensor) -> None
#
Check that all elements in the array-like input are non-negative.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input array.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any element in X is negative. |
Source code in spectre/utils/checks.py
check_sample_weights(weights: torch.Tensor) -> None
#
Check that weights can be used as sample weights.
| PARAMETER | DESCRIPTION |
|---|---|
weights
|
Input array.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any element in weights is negative. |
Source code in spectre/utils/checks.py
experimental(reason: str | F) -> Callable[[F], F] | F
#
This is a decorator which can be used to mark functions as experimental. It will result in a warning being emitted when the function is used.
| PARAMETER | DESCRIPTION |
|---|---|
reason
|
Additional comment for the decorator, or the function being decorated.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable | function
|
Decorated function or decorator. |