rustygraph 0.4.2

A high-performance library for visibility graph computation from time series data
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Visibility graph construction and representation.
//!
//! This module provides types for building and working with visibility graphs
//! from time series data. It supports both natural and horizontal visibility algorithms.
//!
//! # Examples
//!
//! ```rust
//! use rustygraph::{TimeSeries, VisibilityGraph};
//!
//! let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 1.0]).unwrap();
//! let graph = VisibilityGraph::from_series(&series)
//!     .natural_visibility()
//!     .unwrap();
//!
//! println!("Number of edges: {}", graph.edges().len());
//! ```

use crate::core::time_series::TimeSeries;
use crate::core::features::FeatureSet;
use std::collections::HashMap;
use std::fmt;

/// Visibility graph representation with node features.
///
/// A visibility graph represents temporal connectivity patterns in time series data.
/// Each data point becomes a node, and edges connect points that have "visibility"
/// to each other according to the chosen algorithm.
///
/// # Examples
///
/// ## Basic usage
///
/// ```rust
/// use rustygraph::{TimeSeries, VisibilityGraph};
///
/// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 1.0]).unwrap();
/// let graph = VisibilityGraph::from_series(&series)
///     .natural_visibility()
///     .unwrap();
///
/// // Access graph properties
/// println!("Nodes: {}", graph.node_count);
/// println!("Edges: {}", graph.edges().len());
/// println!("Degree sequence: {:?}", graph.degree_sequence());
/// ```
///
/// ## With features
///
/// ```rust
/// use rustygraph::{TimeSeries, VisibilityGraph, FeatureSet, BuiltinFeature};
///
/// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0]).unwrap();
/// let graph = VisibilityGraph::from_series(&series)
///     .with_features(
///         FeatureSet::new()
///             .add_builtin(BuiltinFeature::DeltaForward)
///             .add_builtin(BuiltinFeature::LocalSlope)
///     )
///     .natural_visibility()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct VisibilityGraph<T> {
    /// Number of nodes (data points)
    pub node_count: usize,
    /// Graph edges as (source, target) pairs
    pub(crate) edges: HashMap<(usize, usize), f64>,
    /// Adjacency list representation
    adjacency: Vec<Vec<f64>>,
    /// Computed features for each node
    pub node_features: Vec<HashMap<String, T>>,
    /// Whether the graph is directed
    pub directed: bool,
}

/// Direction mode for visibility graphs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphDirection {
    /// Undirected graph (default) - edges go both ways
    Undirected,
    /// Directed graph - edges only from earlier to later time points
    Directed,
}

impl<T> VisibilityGraph<T> {
    /// Creates a visibility graph builder from a time series.
    ///
    /// This is the main entry point for constructing visibility graphs.
    /// Use the returned builder to configure options and select an algorithm.
    ///
    /// # Arguments
    ///
    /// - `series`: Reference to time series data
    ///
    /// # Returns
    ///
    /// A [`VisibilityGraphBuilder`] for configuration
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 2.0, 3.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    /// ```
    pub fn from_series(series: &TimeSeries<T>) -> VisibilityGraphBuilder<'_, T> {
        VisibilityGraphBuilder {
            series,
            feature_set: None,
            direction: GraphDirection::Undirected,
        }
    }

    /// Returns all edges in the graph.
    ///
    /// Each edge is represented as a tuple `(source, target)` where both
    /// values are node indices.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    ///
    /// for (&(src, dst), &weight) in graph.edges() {
    ///     println!("{} -> {}: {}", src, dst, weight);
    /// }
    /// ```
    pub fn edges(&self) -> &HashMap<(usize, usize), f64> {
        &self.edges
    }

    /// Returns the adjacency list for a specific node.
    ///
    /// # Arguments
    ///
    /// - `node`: Node index
    ///
    /// # Returns
    ///
    /// Slice of adjacent node indices, or `None` if node doesn't exist
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    ///
    /// if let Some(neighbors) = graph.neighbors(0) {
    ///     println!("Node 0 is connected to: {:?}", neighbors);
    /// }
    /// ```
    pub fn neighbors(&self, node: usize) -> Option<&[f64]> {
        self.adjacency.get(node).map(|v| v.as_slice())
    }

    /// Checks if an edge exists between two nodes.
    ///
    /// # Arguments
    ///
    /// - `from`: Source node index
    /// - `to`: Target node index
    ///
    /// # Returns
    ///
    /// `true` if an edge exists, `false` otherwise
    pub fn has_edge(&self, from: usize, to: usize) -> bool {
        self.edges.contains_key(&(from, to)) ||
        (!self.directed && self.edges.contains_key(&(to, from)))
    }

    /// Returns the indices of neighboring nodes.
    ///
    /// # Arguments
    ///
    /// - `node`: Node index
    ///
    /// # Returns
    ///
    /// Vector of neighbor node indices
    pub fn neighbor_indices(&self, node: usize) -> Vec<usize> {
        self.edges
            .keys()
            .filter_map(|(i, j)| {
                if *i == node {
                    Some(*j)
                } else if !self.directed && *j == node {
                    Some(*i)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns the degree of a node (number of connections).
    ///
    /// # Arguments
    ///
    /// - `node`: Node index
    ///
    /// # Returns
    ///
    /// The number of edges connected to this node, or `None` if node doesn't exist
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    ///
    /// if let Some(degree) = graph.degree(0) {
    ///     println!("Node 0 has degree {}", degree);
    /// }
    /// ```
    pub fn degree(&self, node: usize) -> Option<usize> {
        self.adjacency.get(node).map(|v| v.len())
    }

    /// Returns the degree sequence of all nodes.
    ///
    /// The degree sequence is a vector where each element is the degree
    /// of the corresponding node.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    ///
    /// let degrees = graph.degree_sequence();
    /// println!("Degree sequence: {:?}", degrees);
    /// ```
    pub fn degree_sequence(&self) -> Vec<usize> {
        self.adjacency.iter().map(|v| v.len()).collect()
    }

    /// Returns computed features for a specific node.
    ///
    /// # Arguments
    ///
    /// - `node`: Node index
    ///
    /// # Returns
    ///
    /// A map of feature names to values, or `None` if node doesn't exist
    /// or no features were computed
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph, FeatureSet, BuiltinFeature};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .with_features(
    ///         FeatureSet::new().add_builtin(BuiltinFeature::DeltaForward)
    ///     )
    ///     .natural_visibility()
    ///     .unwrap();
    ///
    /// if let Some(features) = graph.node_features(0) {
    ///     for (name, value) in features {
    ///         println!("{}: {}", name, value);
    ///     }
    /// }
    /// ```
    pub fn node_features(&self, node: usize) -> Option<&HashMap<String, T>> {
        self.node_features.get(node)
    }

    /// Exports the graph to an adjacency matrix.
    ///
    /// The adjacency matrix is a square boolean matrix where `matrix[i][j]`
    /// is `true` if there is an edge from node `i` to node `j`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    ///
    /// let matrix = graph.to_adjacency_matrix();
    /// for row in &matrix {
    ///     println!("{:?}", row);
    /// }
    /// ```
    pub fn to_adjacency_matrix(&self) -> Vec<Vec<f64>> {
        let mut matrix = vec![vec![0.0; self.node_count]; self.node_count];
        for &(src, dst) in self.edges.keys() {
            matrix[src][dst] = self.edges[&(src, dst)];
        }
        matrix
    }
}

/// Builder for constructing visibility graphs with options.
///
/// This builder allows you to configure features and select the visibility
/// algorithm before constructing the graph.
///
/// # Examples
///
/// ```rust
/// use rustygraph::{TimeSeries, VisibilityGraph, FeatureSet, BuiltinFeature};
///
/// let series = TimeSeries::from_raw(vec![1.0, 2.0, 3.0]).unwrap();
/// let graph = VisibilityGraph::from_series(&series)
///     .with_features(
///         FeatureSet::new().add_builtin(BuiltinFeature::DeltaForward)
///     )
///     .natural_visibility()
///     .unwrap();
/// ```
pub struct VisibilityGraphBuilder<'a, T> {
    #[allow(dead_code)] // Will be used when algorithms are implemented
    series: &'a TimeSeries<T>,
    feature_set: Option<FeatureSet<T>>,
    direction: GraphDirection,
}

impl<'a, T> VisibilityGraphBuilder<'a, T>
where
    T: Copy + PartialOrd + Into<f64>,
{
    /// Specifies features to compute for each node.
    ///
    /// Features are computed after the graph structure is built.
    ///
    /// # Arguments
    ///
    /// - `features`: A configured [`FeatureSet`]
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph, FeatureSet, BuiltinFeature};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 2.0, 3.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .with_features(
    ///         FeatureSet::new()
    ///             .add_builtin(BuiltinFeature::DeltaForward)
    ///             .add_builtin(BuiltinFeature::LocalSlope)
    ///     )
    ///     .natural_visibility()
    ///     .unwrap();
    /// ```
    pub fn with_features(mut self, features: FeatureSet<T>) -> Self {
        self.feature_set = Some(features);
        self
    }

    /// Sets the graph direction mode.
    ///
    /// # Arguments
    ///
    /// - `direction`: Whether the graph should be directed or undirected
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph, GraphDirection};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 2.0, 3.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .with_direction(GraphDirection::Directed)
    ///     .natural_visibility()
    ///     .unwrap();
    /// ```
    pub fn with_direction(mut self, direction: GraphDirection) -> Self {
        self.direction = direction;
        self
    }

    /// Constructs a natural visibility graph.
    ///
    /// In a natural visibility graph, two nodes (i, yi) and (j, yj) are connected
    /// if all intermediate points (k, yk) satisfy the visibility criterion:
    ///
    /// `yk < yi + (yj - yi) * (tk - ti) / (tj - ti)`
    ///
    /// # Algorithm
    ///
    /// Uses a monotonic stack optimization for O(n) complexity per node.
    ///
    /// # Errors
    ///
    /// Returns [`GraphError`] if the time series is empty or feature computation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 1.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .natural_visibility()
    ///     .unwrap();
    /// ```
    pub fn natural_visibility(self) -> Result<VisibilityGraph<T>, GraphError>
    where
        T: Copy + PartialOrd + Into<f64> + std::ops::Add<Output = T> + std::ops::Sub<Output = T>
           + std::ops::Mul<Output = T> + std::ops::Div<Output = T> + From<f64> + Send + Sync,
    {
        use crate::core::algorithms::edges::{VisibilityEdges as create_visibility_edges, VisibilityType};

        // Check for empty series
        if self.series.is_empty() {
            return Err(GraphError::EmptyTimeSeries);
        }

        // Check for all missing values
        if self.series.values.iter().all(|v| v.is_none()) {
            return Err(GraphError::AllValuesMissing);
        }

        // Compute edges using natural visibility algorithm
        // Use parallel computation when available (significant speedup for large graphs)
        let edges: HashMap<(usize, usize), f64> = {
            let edge_computer = create_visibility_edges::new(
                self.series,
                VisibilityType::Natural,
                |_, _, _, _| 1.0
            );

            #[cfg(feature = "parallel")]
            {
                edge_computer.compute_edges_parallel()
            }

            #[cfg(not(feature = "parallel"))]
            {
                edge_computer.compute_edges()
            }
        };

        // Compute adjacency list
        let directed = matches!(self.direction, GraphDirection::Directed);
        let adj: Vec<Vec<f64>> = build_adjacency_list(self.series.len(), &edges, directed);

        // Compute node features if feature set is provided
        let node_features = if let Some(feature_set) = self.feature_set {
            compute_node_features(&self.series.values, &feature_set)
        } else {
            vec![]
        };

        Ok(VisibilityGraph {
            node_count: self.series.len(),
            edges,
            adjacency: adj,
            node_features,
            directed,
        })
    }

    /// Constructs a horizontal visibility graph.
    ///
    /// In a horizontal visibility graph, two nodes are connected if all
    /// intermediate values are strictly lower than both endpoints.
    ///
    /// # Algorithm
    ///
    /// Uses a linear scan approach with O(n) average case complexity.
    ///
    /// # Errors
    ///
    /// Returns [`GraphError`] if the time series is empty or feature computation fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustygraph::{TimeSeries, VisibilityGraph};
    ///
    /// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 1.0]).unwrap();
    /// let graph = VisibilityGraph::from_series(&series)
    ///     .horizontal_visibility()
    ///     .unwrap();
    /// ```
    pub fn horizontal_visibility(self) -> Result<VisibilityGraph<T>, GraphError>
    where
        T: Copy + PartialOrd + Into<f64> + std::ops::Add<Output = T> + std::ops::Sub<Output = T>
           + std::ops::Mul<Output = T> + std::ops::Div<Output = T> + From<f64> + Send + Sync,
    {
        use crate::core::algorithms::edges::{VisibilityEdges as create_visibility_edges, VisibilityType};

        // Check for empty series
        if self.series.is_empty() {
            return Err(GraphError::EmptyTimeSeries);
        }

        // Check for all missing values
        if self.series.values.iter().all(|v| v.is_none()) {
            return Err(GraphError::AllValuesMissing);
        }

        // Compute edges using horizontal visibility algorithm
        // Use parallel computation when available (significant speedup for large graphs)
        let edges: HashMap<(usize, usize), f64> = {
            let edge_computer = create_visibility_edges::new(
                self.series,
                VisibilityType::Horizontal,
                |_, _, _, _| 1.0
            );

            #[cfg(feature = "parallel")]
            {
                edge_computer.compute_edges_parallel()
            }

            #[cfg(not(feature = "parallel"))]
            {
                edge_computer.compute_edges()
            }
        };

        // Compute adjacency list
        let directed = matches!(self.direction, GraphDirection::Directed);
        let adj: Vec<Vec<f64>> = build_adjacency_list(self.series.len(), &edges, directed);

        // Compute node features if feature set is provided
        let node_features = if let Some(feature_set) = self.feature_set {
            compute_node_features(&self.series.values, &feature_set)
        } else {
            vec![]
        };

        Ok(VisibilityGraph {
            node_count: self.series.len(),
            edges,
            adjacency: adj,
            node_features,
            directed,
        })
    }
}

/// Errors during visibility graph construction.
#[derive(Debug, Clone, PartialEq)]
pub enum GraphError {
    /// Time series is empty
    EmptyTimeSeries,
    /// Feature computation failed
    FeatureComputationFailed {
        /// Name of the feature that failed
        feature: String,
        /// Node index where failure occurred
        node: usize,
    },
    /// All values are missing
    AllValuesMissing,
}

impl fmt::Display for GraphError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GraphError::EmptyTimeSeries => write!(f, "Time series is empty"),
            GraphError::FeatureComputationFailed { feature, node } => {
                write!(f, "Feature '{}' computation failed at node {}", feature, node)
            }
            GraphError::AllValuesMissing => write!(f, "All values are missing"),
        }
    }
}


fn build_adjacency_list(node_count: usize, edges: &HashMap<(usize, usize), f64>, directed: bool) -> Vec<Vec<f64>> {
    let mut adjacency = vec![Vec::new(); node_count];
    for &(src, dst) in edges.keys() {
        let weight = edges.get(&(src, dst)).copied().unwrap_or(0.0);
        adjacency[src].push(weight);
        if !directed {
            adjacency[dst].push(weight); // Add reverse edge for undirected
        }
    }
    adjacency
}

/// Computes all features for all nodes in the series.
///
/// This function automatically uses parallel computation when the `parallel` feature is enabled,
/// otherwise falls back to sequential computation.
pub(crate) fn compute_node_features<T>(
    series: &[Option<T>],
    feature_set: &FeatureSet<T>,
) -> Vec<HashMap<String, T>>
where
    T: Copy + PartialOrd + std::ops::Add<Output = T> + std::ops::Sub<Output = T>
       + std::ops::Mul<Output = T> + std::ops::Div<Output = T> + From<f64> + Into<f64>
       + Send + Sync,
{
    // Use parallel implementation when parallel feature is enabled
    #[cfg(feature = "parallel")]
    {
        crate::performance::parallel::compute_node_features_parallel(series, feature_set)
    }

    // Fall back to sequential implementation
    #[cfg(not(feature = "parallel"))]
    {
        let mut node_features = Vec::with_capacity(series.len());

        for i in 0..series.len() {
            let mut features = HashMap::new();

            // Compute each feature for this node
            for feature in &feature_set.features {
                if let Some(value) = feature.compute(series, i, &feature_set.missing_strategy) {
                    features.insert(feature.name().to_string(), value);
                }
            }

            node_features.push(features);
        }

        node_features
    }
}

impl std::error::Error for GraphError {}