Quaternions¶
Quaternion math, loss functions, and augmentations.
QuaternionRegularizer ¶
Regularization loss that penalizes non-unit quaternion outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modules
|
list
|
list of nn.Module instances whose outputs are captured via hooks. |
required |
reg_unit
|
float
|
weight for the unit-norm regularization term. |
0.0
|
Source code in tsfast/quaternions/aux_losses.py
setup ¶
teardown ¶
__call__ ¶
Compute unit-norm regularization loss from captured hook output.
Source code in tsfast/quaternions/aux_losses.py
QuaternionAugmentation ¶
Apply random quaternion rotation to grouped signals during training.
Each call samples a new random quaternion and applies it to all specified signal groups. Groups of size 4 are rotated as quaternions, groups of size 3 are rotated as vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp_groups
|
list[list[int]]
|
list of [start, end] index pairs defining signal groups (groups of size 4 are rotated as quaternions, size 3 as vectors). |
required |
Source code in tsfast/quaternions/transforms.py
__call__ ¶
Apply random quaternion rotation augmentation.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (augmented xb, augmented yb). |
Source code in tsfast/quaternions/transforms.py
conjQuat ¶
diffQuat ¶
Compute the difference quaternion between q1 and q2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q1
|
Tensor
|
first quaternion tensor. |
required |
q2
|
Tensor
|
second quaternion tensor. |
required |
norm
|
bool
|
whether to normalize inputs before computing the difference. |
True
|
Source code in tsfast/quaternions/ops.py
inclinationAngle ¶
Inclination (tilt) angle between two quaternions.
Uses atan2 instead of acos for numerical stability.
Source code in tsfast/quaternions/ops.py
inclinationAngleAbs ¶
Absolute inclination angle relative to the identity quaternion.
Uses atan2 instead of acos for numerical stability.
Source code in tsfast/quaternions/ops.py
multiplyQuat ¶
Multiply two quaternions element-wise.
Source code in tsfast/quaternions/ops.py
norm_quaternion ¶
pitchAngle ¶
Euler pitch angle of the difference quaternion.
Uses atan2(sin, cos) instead of asin for numerical stability
near gimbal lock (+/-pi/2).
Source code in tsfast/quaternions/ops.py
quatFromAngleAxis ¶
Create quaternions from angle-axis representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
angle
|
Tensor
|
rotation angles, shape (N,) or (1,). |
required |
axis
|
Tensor
|
rotation axes, shape (3,) or (N, 3) or (1, 3). |
required |
Source code in tsfast/quaternions/ops.py
quatInterp ¶
Interpolate quaternions at non-integer indices using Slerp.
Sampling indices are in the range 0..N-1. For values outside this range,
depending on extend, the first/last element or NaN is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quat
|
Tensor
|
input quaternions, shape (N(xB)x4). |
required |
ind
|
Tensor
|
sampling indices, shape (M,). |
required |
extend
|
bool
|
if true, extend input by repeating first/last value. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Interpolated quaternions, shape (Mx4). |
Source code in tsfast/quaternions/ops.py
rad2deg ¶
rand_quat ¶
relativeAngle ¶
Full rotation angle between two quaternions.
Uses atan2 instead of acos for numerical stability.
Source code in tsfast/quaternions/ops.py
relativeQuat ¶
Compute the relative quaternion as quat1*inv(quat2).
Source code in tsfast/quaternions/ops.py
rollAngle ¶
Compute the roll angle between two quaternions.
Source code in tsfast/quaternions/ops.py
rot_vec ¶
safe_acos ¶
safe_acos_double ¶
Numerically stable arccosine using float64 internally for accuracy.
Source code in tsfast/quaternions/ops.py
abs_inclination ¶
abs_rel_angle ¶
angle_loss ¶
Per-element absolute angle error from difference quaternion w component.
angle_loss_opt ¶
Per-element absolute angle error (optimized, no full quaternion multiply).
Source code in tsfast/quaternions/losses.py
deg_rmse ¶
inclination_angle ¶
inclination_error ¶
Per-element inclination error from difference quaternion.
inclination_loss ¶
inclination_loss_abs ¶
inclination_loss_smooth ¶
inclination_loss_squared ¶
mean_inclination_deg ¶
mean_rel_angle_deg ¶
ms_inclination ¶
ms_rel_angle ¶
pitch_angle ¶
rel_angle ¶
rms_inclination ¶
rms_inclination_deg ¶
rms_pitch_deg ¶
rms_rel_angle_deg ¶
rms_roll_deg ¶
roll_angle ¶
smooth_inclination ¶
augmentation_groups ¶
Convert channel group sizes into start/end index pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u_groups
|
list[int]
|
list of group sizes (number of channels per group). |
required |
Source code in tsfast/quaternions/transforms.py
multiplyQuat_np ¶
Multiply two quaternions element-wise (numpy).
Source code in tsfast/quaternions/numpy_ops.py
quatFromAngleAxis_np ¶
Create quaternions from angle-axis representation (numpy).
If angle is 0, the output will be an identity quaternion. If axis is a zero vector, a ValueError will be raised unless the corresponding angle is 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
angle
|
ndarray
|
scalar or N angles in radians. |
required |
axis
|
ndarray
|
rotation axes, shape (3,) or (Nx3) or (1x3). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Quaternion array, shape (Nx4) or (1x4). |
Source code in tsfast/quaternions/numpy_ops.py
quatInterp_np ¶
Interpolate quaternions at non-integer indices using Slerp (numpy).
Sampling indices are in the range 0..N-1. For values outside this range,
depending on extend, the first/last element or NaN is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quat
|
ndarray
|
input quaternions, shape (Nx4). |
required |
ind
|
ndarray
|
sampling indices, shape (M,). |
required |
extend
|
bool
|
if true, extend input by repeating first/last value. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Interpolated quaternions, shape (Mx4). |
Source code in tsfast/quaternions/numpy_ops.py
relativeQuat_np ¶
Compute the relative quaternion as inv(quat1)*quat2 (numpy).
Source code in tsfast/quaternions/numpy_ops.py
plot_quaternion_inclination ¶
plot_quaternion_inclination(axs: list, in_sig: Tensor, targ_sig: Tensor, out_sig: Tensor | None = None, **kwargs)
Plot quaternion inclination target, prediction, and error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axs
|
list
|
list of matplotlib axes to plot on. |
required |
in_sig
|
Tensor
|
input signal tensor. |
required |
targ_sig
|
Tensor
|
target quaternion tensor. |
required |
out_sig
|
Tensor | None
|
predicted quaternion tensor, or None for batch display. |
None
|
Source code in tsfast/quaternions/viz.py
plot_quaternion_rel_angle ¶
plot_quaternion_rel_angle(axs: list, in_sig: Tensor, targ_sig: Tensor, out_sig: Tensor | None = None, **kwargs)
Plot relative quaternion angle target, prediction, and error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axs
|
list
|
list of matplotlib axes to plot on. |
required |
in_sig
|
Tensor
|
input signal tensor. |
required |
targ_sig
|
Tensor
|
target quaternion tensor. |
required |
out_sig
|
Tensor | None
|
predicted quaternion tensor, or None for batch display. |
None
|
Source code in tsfast/quaternions/viz.py
plot_scalar_inclination ¶
plot_scalar_inclination(axs: list, in_sig: Tensor, targ_sig: Tensor, out_sig: Tensor | None = None, **kwargs)
Plot scalar inclination target, prediction, and error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axs
|
list
|
list of matplotlib axes to plot on. |
required |
in_sig
|
Tensor
|
input signal tensor. |
required |
targ_sig
|
Tensor
|
target inclination tensor. |
required |
out_sig
|
Tensor | None
|
predicted inclination tensor, or None for batch display. |
None
|