tanat.metric.sequence.type.softdtw package#
Submodules#
tanat.metric.sequence.type.softdtw.kernels module#
Numba kernels for SoftDTWSequenceMetric.
All functions are @njit (no Python objects). They operate on int32-encoded
feature arrays produced by the entity metric’s prepare_batch_data.
Implementation: full (n+2) × (m+2) DP matrix with soft-minimum operator. The log-sum-exp trick is used for numerical stability.
- tanat.metric.sequence.type.softdtw.kernels.compute_softdtw_matrix(result, start, end, arrays_a, lengths_a, arrays_b, lengths_b, dist_kernel, context, gamma, symmetric)[source]#
Parallel SoftDTW matrix kernel.
Processes rows
[start, end).
- tanat.metric.sequence.type.softdtw.kernels.compute_softdtw_pair(arr_a, arr_b, len_a, len_b, dist_kernel, context, gamma)[source]#
Compute SoftDTW distance for a single pair of int32-encoded sequences.
Uses the full (n+2) × (m+2) DP matrix.
- Parameters:
arr_a – int32-encoded sequence A.
arr_b – int32-encoded sequence B.
len_a – Length of A.
len_b – Length of B.
dist_kernel – Numba entity distance kernel.
context – Opaque context tuple forwarded to
dist_kernel.gamma – Regularisation parameter (float32 > 0).
- Returns:
float32 SoftDTW distance, or
nanwhen either sequence is empty.
tanat.metric.sequence.type.softdtw.metric module#
SoftDTWSequenceMetric: Soft Dynamic Time Warping between sequences.
- class tanat.metric.sequence.type.softdtw.metric.SoftDTWSequenceMetric(entity_metric: EntityMetric | str = 'hamming', gamma: float = 1.0, *, store_path: str | Path | None = None, chunk_size: int = 500, resume: bool = True, dtype: str = 'float32')[source]#
Bases:
SequenceMetricSoft Dynamic Time Warping distance between two sequences.
Replaces the
minoperator in the DTW recurrence with a differentiable soft-minimum parameterised bygamma:\[\text{soft-min}(a, b, c; \gamma) = -\gamma \log\bigl( e^{-a/\gamma} + e^{-b/\gamma} + e^{-c/\gamma}\bigr)\]As
gamma → 0, SoftDTW converges to standard DTW. Asgamma → ∞, it approaches the mean of all alignment costs.Empty-sequence behaviour:
Both empty →
nan(no alignment possible).One empty →
nan(no alignment possible).
References
Cuturi & Blondel (2017) — Soft-DTW: a Differentiable Loss Function for Time-Series, ICML.
Example:
sdtw = SoftDTWSequenceMetric(gamma=0.1) d = sdtw(seq_a, seq_b) dm = sdtw.compute_matrix(pool)
- MEMMAP_SUPPORT: bool = True[source]#
Set to
Truein subclasses that implement disk-backed (memmap) computation. WhenFalse, passingstore_pathor an instance-levelStorageOptionsraisesNotImplementedErrorearly with a clear message.
- SETTINGS_CLASS[source]#
alias of
SoftDTWSettings
- __init__(entity_metric: EntityMetric | str = 'hamming', gamma: float = 1.0, *, store_path: str | Path | None = None, chunk_size: int = 500, resume: bool = True, dtype: str = 'float32') None[source]#
- class tanat.metric.sequence.type.softdtw.metric.SoftDTWSettings(*, entity_metric: EntityMetric = 'hamming', gamma: float = 1.0)[source]#
Bases:
objectSettings for
SoftDTWSequenceMetric.- Parameters:
entity_metric – Entity-level distance metric. Default:
"hamming".gamma – Regularisation parameter for the soft-min operator. Must be > 0. Large values produce a smoother (mean-like) approximation; small values approach standard DTW. Default: 1.0.
- entity_metric: EntityMetric = 'hamming'[source]#
Module contents#
SoftDTWSequenceMetric package.
- class tanat.metric.sequence.type.softdtw.SoftDTWSequenceMetric(entity_metric: EntityMetric | str = 'hamming', gamma: float = 1.0, *, store_path: str | Path | None = None, chunk_size: int = 500, resume: bool = True, dtype: str = 'float32')[source]#
Bases:
SequenceMetricSoft Dynamic Time Warping distance between two sequences.
Replaces the
minoperator in the DTW recurrence with a differentiable soft-minimum parameterised bygamma:\[\text{soft-min}(a, b, c; \gamma) = -\gamma \log\bigl( e^{-a/\gamma} + e^{-b/\gamma} + e^{-c/\gamma}\bigr)\]As
gamma → 0, SoftDTW converges to standard DTW. Asgamma → ∞, it approaches the mean of all alignment costs.Empty-sequence behaviour:
Both empty →
nan(no alignment possible).One empty →
nan(no alignment possible).
References
Cuturi & Blondel (2017) — Soft-DTW: a Differentiable Loss Function for Time-Series, ICML.
Example:
sdtw = SoftDTWSequenceMetric(gamma=0.1) d = sdtw(seq_a, seq_b) dm = sdtw.compute_matrix(pool)
- MEMMAP_SUPPORT: bool = True[source]#
Set to
Truein subclasses that implement disk-backed (memmap) computation. WhenFalse, passingstore_pathor an instance-levelStorageOptionsraisesNotImplementedErrorearly with a clear message.
- SETTINGS_CLASS[source]#
alias of
SoftDTWSettings
- __init__(entity_metric: EntityMetric | str = 'hamming', gamma: float = 1.0, *, store_path: str | Path | None = None, chunk_size: int = 500, resume: bool = True, dtype: str = 'float32') None[source]#
- class tanat.metric.sequence.type.softdtw.SoftDTWSettings(*, entity_metric: EntityMetric = 'hamming', gamma: float = 1.0)[source]#
Bases:
objectSettings for
SoftDTWSequenceMetric.- Parameters:
entity_metric – Entity-level distance metric. Default:
"hamming".gamma – Regularisation parameter for the soft-min operator. Must be > 0. Large values produce a smoother (mean-like) approximation; small values approach standard DTW. Default: 1.0.
- entity_metric: EntityMetric = 'hamming'[source]#