traj-dist-rs 1.0.0-rc.5

High-performance trajectory distance & similarity measures in Rust with Python bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Python bindings for batch computation functions (New elegant design)

use crate::binding::sequence::types::PyTrajectoryType;
use crate::distance::batch::{DistanceAlgorithm, Metric};
use crate::distance::distance_type::DistanceType;
use numpy::{PyArray1, PyArray2};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyList;
use pyo3_stub_gen::{
    define_stub_info_gatherer, derive::gen_stub_pyclass, derive::gen_stub_pyfunction,
};
use pyo3_stub_gen_derive::gen_stub_pymethods;
use std::str::FromStr;

// ===================================================================================
// 1. Define a PyClass that holds the complete configuration, but hide its internal construction
// ===================================================================================

/// A metric configuration object for distance calculations.
///
/// This class encapsulates the distance algorithm, its parameters, and the
/// underlying point-to-point distance type (e.g., Euclidean or Spherical).
///
/// Do not instantiate this class directly. Use the provided static factory
/// methods instead (e.g., `Metric.sspd()`, `Metric.lcss(...)`).
#[cfg(feature = "python-binding")]
#[gen_stub_pyclass]
#[pyclass(name = "Metric")]
pub struct PyMetric {
    // Internally holds a pure Rust Metric
    // This field is invisible to Python, forcing users to use factory methods
    inner: Metric,
}

#[cfg(feature = "python-binding")]
#[gen_stub_pymethods]
#[pymethods]
impl PyMetric {
    // Hide default constructor, preventing users from calling Metric()
    #[new]
    #[pyo3(signature = ())]
    fn new() -> PyResult<Self> {
        Err(PyValueError::new_err(
            "Metric objects cannot be created directly. Use a factory method like Metric.sspd() or Metric.lcss().",
        ))
    }

    // ================== Factory Methods ==================

    /// Symmetric Segment-Path Distance.
    #[staticmethod]
    #[pyo3(signature = (type_d = "euclidean"))]
    fn sspd(type_d: &str) -> PyResult<Self> {
        let calculator = build_calculator(DistanceAlgorithm::SSPD, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Dynamic Time Warping.
    #[staticmethod]
    #[pyo3(signature = (type_d = "euclidean"))]
    fn dtw(type_d: &str) -> PyResult<Self> {
        let calculator = build_calculator(DistanceAlgorithm::DTW, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Hausdorff Distance.
    #[staticmethod]
    #[pyo3(signature = (type_d = "euclidean"))]
    fn hausdorff(type_d: &str) -> PyResult<Self> {
        let calculator = build_calculator(DistanceAlgorithm::Hausdorff, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Discrete Frechet Distance.
    #[staticmethod]
    #[pyo3(signature = (type_d = "euclidean"))]
    fn discret_frechet(type_d: &str) -> PyResult<Self> {
        let calculator = build_calculator(DistanceAlgorithm::DiscretFrechet, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Longest Common Subsequence.
    #[staticmethod]
    #[pyo3(signature = (eps, type_d = "euclidean"))]
    fn lcss(eps: f64, type_d: &str) -> PyResult<Self> {
        let calculator = build_calculator(DistanceAlgorithm::LCSS { eps }, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Edit Distance on Real sequence.
    #[staticmethod]
    #[pyo3(signature = (eps, type_d = "euclidean"))]
    fn edr(eps: f64, type_d: &str) -> PyResult<Self> {
        let calculator = build_calculator(DistanceAlgorithm::EDR { eps }, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Edit distance with Real Penalty.
    #[staticmethod]
    #[pyo3(signature = (g, type_d = "euclidean"))]
    fn erp(g: Vec<f64>, type_d: &str) -> PyResult<Self> {
        if g.len() < 2 {
            return Err(PyValueError::new_err(
                "ERP 'g' parameter must have at least 2 elements.",
            ));
        }
        let algorithm = DistanceAlgorithm::ERP { g: [g[0], g[1]] };
        let calculator = build_calculator(algorithm, type_d)?;
        Ok(Self { inner: calculator })
    }

    /// Edit Distance with Projections.
    ///
    /// EDwP is designed for trajectories with inconsistent sampling rates.
    /// Note: EDwP only supports Euclidean distance (not spherical distance).
    #[staticmethod]
    #[pyo3(signature = ())]
    fn edwp() -> PyResult<Self> {
        let algorithm = DistanceAlgorithm::EDwP;
        let distance_type = DistanceType::Euclidean;
        Ok(Self {
            inner: Metric::new(algorithm, distance_type),
        })
    }

    /// Frechet Distance (continuous).
    ///
    /// Unlike Discrete Frechet, this considers all continuous points along
    /// the curve segments, providing an exact solution.
    /// Note: Frechet only supports Euclidean distance (not spherical distance).
    #[staticmethod]
    #[pyo3(signature = ())]
    fn frechet() -> PyResult<Self> {
        let algorithm = DistanceAlgorithm::Frechet;
        let distance_type = DistanceType::Euclidean;
        Ok(Self {
            inner: Metric::new(algorithm, distance_type),
        })
    }
}

// Helper function to keep factory method code clean
fn build_calculator(algorithm: DistanceAlgorithm, type_d: &str) -> PyResult<Metric> {
    let distance_type = DistanceType::from_str(type_d).map_err(|_| {
        PyValueError::new_err(format!(
            "Invalid distance type '{}'. Expected 'euclidean' or 'spherical'.",
            type_d
        ))
    })?;
    Ok(Metric::new(algorithm, distance_type))
}

// ===================================================================================
// 2. Update pdist and cdist signatures
// ===================================================================================

/// Compute pairwise distances between trajectories
///
/// This function computes the distances between all unique pairs of trajectories
/// in the input list. The result is a compressed distance matrix (1D array)
/// containing distances for all pairs (i, j) where i < j.
///
/// # Symmetry Assumption
///
/// This function assumes that the distance metric is **symmetric**, i.e.,
/// `distance(A, B) == distance(B, A)`. All standard distance algorithms
/// in traj-dist-rs (SSPD, DTW, Hausdorff, LCSS, EDR, ERP, Discret Frechet)
/// satisfy this property.
///
/// **Important**: If your distance metric is **asymmetric**, use `cdist` instead
/// to compute the full distance matrix. Using `pdist` with asymmetric distances
/// will only compute half of the distances and may produce incorrect results.
///
/// # Arguments
/// * `trajectories` - List of trajectories, where each trajectory is a 2D numpy array
///                    or list of [x, y] pairs
/// * `metric` - Metric configuration object (e.g., `Metric.sspd()`, `Metric.lcss(eps=5.0)`)
/// * `parallel` - Whether to use parallel processing (default: True)
/// * `show_progress` - Whether to display a progress bar during computation (default: False).
///                     The progress bar is rendered to stderr so it does not interfere with stdout.
///
/// # Returns
/// * `distances` - 1D numpy array containing distances for all unique pairs
///
/// # Output Format
///
/// For `n` trajectories, the result is a 1D array of length `n * (n - 1) / 2`.
/// The distances are ordered as `d(0,1), d(0,2), ..., d(0,n-1), d(1,2), d(1,3), ..., d(n-2,n-1)`.
///
/// # Examples
/// ```python
/// import traj_dist_rs
/// import numpy as np
///
/// # Create metric configuration
/// metric = traj_dist_rs.Metric.sspd(type_d="euclidean")
///
/// # Using numpy arrays (zero-copy)
/// trajectories = [
///     np.array([[0.0, 0.0], [1.0, 1.0]]),
///     np.array([[0.0, 1.0], [1.0, 0.0]]),
///     np.array([[0.5, 0.5], [1.5, 1.5]])
/// ]
/// distances = traj_dist_rs.pdist(trajectories, metric=metric)
///
/// # Using lists (will be copied)
/// trajectories = [
///     [[0.0, 0.0], [1.0, 1.0]],
///     [[0.0, 1.0], [1.0, 0.0]],
///     [[0.5, 0.5], [1.5, 1.5]]
/// ]
/// distances = traj_dist_rs.pdist(trajectories, metric=metric)
///
/// # Using LCSS with epsilon parameter
/// metric_lcss = traj_dist_rs.Metric.lcss(eps=5.0, type_d="euclidean")
/// distances = traj_dist_rs.pdist(trajectories, metric=metric_lcss)
///
/// # With progress bar
/// distances = traj_dist_rs.pdist(trajectories, metric=metric, show_progress=True)
/// ```
#[cfg(feature = "python-binding")]
#[gen_stub_pyfunction]
#[pyfunction(signature = (trajectories, metric, parallel=true, show_progress=false))]
pub fn pdist<'py>(
    py: Python<'py>,
    #[gen_stub(override_type(type_repr="typing.Sequence[typing.List[typing.List[float]] | numpy.ndarray]", imports=("typing", "numpy")))]
    trajectories: &Bound<'py, PyList>,
    metric: &PyMetric,
    parallel: bool,
    show_progress: bool,
) -> PyResult<Py<PyArray1<f64>>> {
    if trajectories.len() < 2 {
        return Err(PyValueError::new_err(
            "pdist requires at least 2 trajectories",
        ));
    }

    // Convert each trajectory to PyTrajectoryType
    let trajectories: Vec<PyTrajectoryType> = trajectories
        .iter()
        .map(|t| {
            PyTrajectoryType::try_from(&t)
                .map_err(|e| PyValueError::new_err(format!("Failed to convert trajectory: {}", e)))
        })
        .collect::<Result<Vec<_>, _>>()?;

    // Release the GIL so Rust can freely use Rayon parallelism and indicatif progress output
    let metric_inner = metric.inner;
    let distances = py
        .detach(|| {
            crate::distance::batch::pdist(&trajectories, &metric_inner, parallel, show_progress)
        })
        .map_err(|e| PyValueError::new_err(format!("Failed to compute distances: {}", e)))?;

    let array = PyArray1::from_vec(py, distances);
    Ok(array.unbind())
}

/// Compute distances between two trajectory collections
///
/// This function computes the distances between all pairs of trajectories
/// from two collections. The result is a full distance matrix (2D array)
/// with shape (n_a, n_b).
///
/// When to Use `cdist` vs `pdist`
///
/// - Use `cdist` when:
/// 1. Computing distances between two different trajectory collections
/// 2. Your distance metric is **asymmetric** (distance(A, B) != distance(B, A))
/// 3. You need the full distance matrix for both directions
///
/// - Use `pdist` when:
/// 1. Computing distances within a single trajectory collection
/// 2. Your distance metric is **symmetric** (distance(A, B) == distance(B, A))
/// 3. You want to save memory by using the compressed distance matrix format
///
/// # Arguments
/// * `trajectories_a` - First collection of trajectories, where each trajectory is a
///                      2D numpy array or list of [x, y] pairs
/// * `trajectories_b` - Second collection of trajectories
/// * `metric` - Metric configuration object (e.g., `Metric.sspd()`, `Metric.lcss(eps=5.0)`)
/// * `parallel` - Whether to use parallel processing (default: True)
/// * `show_progress` - Whether to display a progress bar during computation (default: False).
///                     The progress bar is rendered to stderr so it does not interfere with stdout.
///
/// # Returns
/// * `distances` - 2D numpy array with shape (len(trajectories_a), len(trajectories_b))
///
/// # Output Format
///
/// For `n_a` trajectories in the first collection and `n_b` trajectories in the second,
/// the result is a 2D array with shape `(n_a, n_b)`. The distance at index `[i, j]`
/// represents the distance from `trajectories_a[i]` to `trajectories_b[j]`.
///
/// # Examples
/// ```python
/// import traj_dist_rs
/// import numpy as np
///
/// # Create metric configuration
/// metric = traj_dist_rs.Metric.sspd(type_d="euclidean")
///
/// # Using numpy arrays (zero-copy)
/// trajectories_a = [
///     np.array([[0.0, 0.0], [1.0, 1.0]]),
///     np.array([[0.0, 1.0], [1.0, 0.0]])
/// ]
/// trajectories_b = [
///     np.array([[0.5, 0.5], [1.5, 1.5]]),
///     np.array([[0.5, 1.5], [1.5, 0.5]])
/// ]
/// distances = traj_dist_rs.cdist(trajectories_a, trajectories_b, metric=metric)
/// print(distances.shape)  # (2, 2)
///
/// # Using lists (will be copied)
/// trajectories_a = [
///     [[0.0, 0.0], [1.0, 1.0]],
///     [[0.0, 1.0], [1.0, 0.0]]
/// ]
/// trajectories_b = [
///     [[0.5, 0.5], [1.5, 1.5]],
///     [[0.5, 1.5], [1.5, 0.5]]
/// ]
/// distances = traj_dist_rs.cdist(trajectories_a, trajectories_b, metric=metric)
///
/// # Using ERP with gap point parameter
/// metric_erp = traj_dist_rs.Metric.erp(g=[0.0, 1.0], type_d="euclidean")
/// distances = traj_dist_rs.cdist(trajectories_a, trajectories_b, metric=metric_erp)
///
/// # With progress bar
/// distances = traj_dist_rs.cdist(trajectories_a, trajectories_b, metric=metric, show_progress=True)
/// ```
#[cfg(feature = "python-binding")]
#[gen_stub_pyfunction]
#[pyfunction(signature = (trajectories_a, trajectories_b, metric, parallel=true, show_progress=false))]
pub fn cdist<'py>(
    py: Python<'py>,
    #[gen_stub(override_type(type_repr="typing.Sequence[typing.List[typing.List[float]] | numpy.ndarray]", imports=("typing", "numpy")))]
    trajectories_a: &Bound<'py, PyList>,
    #[gen_stub(override_type(type_repr="typing.Sequence[typing.List[typing.List[float]] | numpy.ndarray]", imports=("typing", "numpy")))]
    trajectories_b: &Bound<'py, PyList>,
    metric: &PyMetric,
    parallel: bool,
    show_progress: bool,
) -> PyResult<Py<PyArray2<f64>>> {
    if trajectories_a.is_empty() {
        return Err(PyValueError::new_err(
            "cdist requires at least 1 trajectory in the first collection",
        ));
    }

    if trajectories_b.is_empty() {
        return Err(PyValueError::new_err(
            "cdist requires at least 1 trajectory in the second collection",
        ));
    }

    // Convert each trajectory to PyTrajectoryType
    let trajectories_a: Vec<PyTrajectoryType> = trajectories_a
        .iter()
        .map(|t| {
            PyTrajectoryType::try_from(&t).map_err(|e| {
                PyValueError::new_err(format!(
                    "Failed to convert trajectory in first collection: {}",
                    e
                ))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    let trajectories_b: Vec<PyTrajectoryType> = trajectories_b
        .iter()
        .map(|t| {
            PyTrajectoryType::try_from(&t).map_err(|e| {
                PyValueError::new_err(format!(
                    "Failed to convert trajectory in second collection: {}",
                    e
                ))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    // Release the GIL so Rust can freely use Rayon parallelism and indicatif progress output
    let metric_inner = metric.inner;
    let n_b = trajectories_b.len();
    let distances = py
        .detach(|| {
            crate::distance::batch::cdist(
                &trajectories_a,
                &trajectories_b,
                &metric_inner,
                parallel,
                show_progress,
            )
        })
        .map_err(|e| PyValueError::new_err(format!("Failed to compute distances: {}", e)))?;

    // Convert Vec<f64> to Vec<Vec<f64>> for PyArray2::from_vec2
    let distances_2d: Vec<Vec<f64>> = distances.chunks(n_b).map(|row| row.to_vec()).collect();
    let array = PyArray2::from_vec2(py, &distances_2d)
        .map_err(|e| PyValueError::new_err(format!("Failed to create 2D array: {}", e)))?;

    Ok(array.unbind())
}

define_stub_info_gatherer!(stub_info);