Skip to main content

shapelet_transform

Function shapelet_transform 

Source
pub fn shapelet_transform(
    shapelets: &ShapeletSet,
    data: &FdMatrix,
) -> Result<FdMatrix, FdarError>
Expand description

Apply a fitted ShapeletSet to a curve set, producing an n×K distance feature matrix.

Returns an FdMatrix with n = data rows (curves) and K = shapelets.len() columns, where X[(i, j)] = shapelet_distance(&shapelets.shapelets()[j].values, curve_i, f64::INFINITY).0.

The shapelet values are already z-normalized, so they are reused directly — no re-normalization against data. Each input row is z-normalized per-window inside shapelet_distance. The row loop is parallelized with iter_maybe_parallel!; distances are order-independent, so the result is identical with or without the parallel feature.

Output columns are shapelet distances, not evaluation points.

§Errors

All returned entries are finite (guaranteed by the Phase 57 z-normalization constant-window guard).

§Examples

use fdars_core::matrix::FdMatrix;
use fdars_core::shapelet::{discover_shapelets, shapelet_transform, ShapeletDiscoveryConfig};

let n = 8usize;
let m = 8usize;
let mut flat = vec![0.0f64; n * m];
let mut labels = vec![0usize; n];
for i in 0..n {
    let class1 = i % 2 == 1;
    labels[i] = usize::from(class1);
    for j in 0..m {
        let base = 0.1 * (i as f64) + 0.05 * (j as f64);
        let motif = if class1 && (3..6).contains(&j) { (j as f64) * 2.0 } else { 0.0 };
        flat[i + j * n] = base + motif;
    }
}
let data = FdMatrix::from_column_major(flat, n, m).unwrap();

let cfg = ShapeletDiscoveryConfig { max_shapelets: 3, ..Default::default() };
let set = discover_shapelets(&data, &labels, &cfg).unwrap();

let features = shapelet_transform(&set, &data).unwrap();
assert_eq!(features.shape(), (n, set.len()));
// Every entry is a finite shapelet distance.
for j in 0..set.len() {
    for i in 0..n {
        assert!(features.get(i, j).unwrap().is_finite());
    }
}