embedded-charts 0.3.0

A rich graph framework for embedded systems using embedded-graphics with std/no_std support
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
//! Comprehensive tests for data aggregation and downsampling functionality

use embedded_charts::{
    data::{
        aggregation::{
            AggregationConfig, AggregationStrategy, DataAggregation, DownsamplingConfig,
        },
        DataPoint, DataSeries, Point2D, StaticDataSeries,
    },
    error::DataError,
};

// Helper function to create test data
fn create_test_data() -> StaticDataSeries<Point2D, 256> {
    let mut series = StaticDataSeries::new();
    let points = [
        (0.0, 10.0),
        (1.0, 20.0),
        (2.0, 5.0),
        (3.0, 30.0),
        (4.0, 15.0),
        (5.0, 25.0),
        (6.0, 8.0),
        (7.0, 35.0),
    ];

    for (x, y) in points.iter() {
        series.push(Point2D::new(*x, *y)).unwrap();
    }

    series
}

fn create_large_test_data() -> StaticDataSeries<Point2D, 512> {
    let mut series = StaticDataSeries::new();

    for i in 0..100 {
        let x = i as f32;
        let y = (x * 0.1).sin() * 50.0 + 50.0 + (i % 10) as f32; // Sin wave with some variation
        series.push(Point2D::new(x, y)).unwrap();
    }

    series
}

mod aggregation_config_tests {
    use super::*;

    #[test]
    fn test_aggregation_config_default() {
        let config = AggregationConfig::default();
        assert_eq!(config.strategy, AggregationStrategy::Mean);
        assert_eq!(config.target_points, 100);
        assert!(config.preserve_endpoints);
        assert_eq!(config.min_group_size, 1);
    }

    #[test]
    fn test_aggregation_config_custom() {
        let config = AggregationConfig {
            strategy: AggregationStrategy::Max,
            target_points: 50,
            preserve_endpoints: false,
            min_group_size: 2,
        };

        assert_eq!(config.strategy, AggregationStrategy::Max);
        assert_eq!(config.target_points, 50);
        assert!(!config.preserve_endpoints);
        assert_eq!(config.min_group_size, 2);
    }
}

mod downsampling_config_tests {
    use super::*;

    #[test]
    fn test_downsampling_config_default() {
        let config = DownsamplingConfig::default();
        assert_eq!(config.max_points, 1000);
        assert!(config.preserve_endpoints);
        assert_eq!(config.min_reduction_ratio, 1.5);
    }

    #[test]
    fn test_downsampling_config_custom() {
        let config = DownsamplingConfig {
            max_points: 200,
            preserve_endpoints: false,
            min_reduction_ratio: 2.0,
        };

        assert_eq!(config.max_points, 200);
        assert!(!config.preserve_endpoints);
        assert_eq!(config.min_reduction_ratio, 2.0);
    }
}

mod group_stats_tests {
    use super::*;

    #[test]
    fn test_group_stats_calculation() {
        let series = create_test_data();
        let stats = series.calculate_group_stats(series.as_slice()).unwrap();

        assert_eq!(stats.count, 8);
        assert_eq!(stats.min_x, 0.0);
        assert_eq!(stats.max_x, 7.0);
        assert_eq!(stats.min_y, 5.0);
        assert_eq!(stats.max_y, 35.0);
        assert_eq!(stats.first.x(), 0.0);
        assert_eq!(stats.first.y(), 10.0);
        assert_eq!(stats.last.x(), 7.0);
        assert_eq!(stats.last.y(), 35.0);

        // Mean should be (0+1+2+3+4+5+6+7)/8 = 3.5 for X
        assert_eq!(stats.mean_x, 3.5);
        // Mean should be (10+20+5+30+15+25+8+35)/8 = 18.5 for Y
        assert_eq!(stats.mean_y, 18.5);
    }

    #[test]
    fn test_group_stats_single_point() {
        let series = create_test_data();
        let stats = series
            .calculate_group_stats(&[Point2D::new(5.0, 15.0)])
            .unwrap();

        assert_eq!(stats.count, 1);
        assert_eq!(stats.min_x, 5.0);
        assert_eq!(stats.max_x, 5.0);
        assert_eq!(stats.min_y, 15.0);
        assert_eq!(stats.max_y, 15.0);
        assert_eq!(stats.mean_x, 5.0);
        assert_eq!(stats.mean_y, 15.0);
        assert_eq!(stats.first.x(), 5.0);
        assert_eq!(stats.last.x(), 5.0);
    }

    #[test]
    fn test_group_stats_empty_error() {
        let series = create_test_data();
        let result = series.calculate_group_stats(&[]);
        assert!(matches!(result, Err(DataError::InsufficientData { .. })));
    }
}

mod mean_aggregation_tests {
    use super::*;

    #[test]
    fn test_mean_aggregation_basic() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Mean,
            target_points: 4,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 4);

        // Each group should have 2 points (8 total / 4 target = 2 per group)
        // Group 1: (0,10) and (1,20) -> mean (0.5, 15.0)
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 0.5);
        assert_eq!(first.y(), 15.0);

        // Group 2: (2,5) and (3,30) -> mean (2.5, 17.5)
        let second = aggregated.get(1).unwrap();
        assert_eq!(second.x(), 2.5);
        assert_eq!(second.y(), 17.5);
    }

    #[test]
    fn test_mean_aggregation_with_endpoints() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Mean,
            target_points: 4,
            preserve_endpoints: true,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();

        // Should preserve first and last points exactly
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 0.0);
        assert_eq!(first.y(), 10.0);

        let last = aggregated.get(aggregated.len() - 1).unwrap();
        assert_eq!(last.x(), 7.0);
        assert_eq!(last.y(), 35.0);
    }

    #[test]
    fn test_mean_aggregation_no_reduction_needed() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Mean,
            target_points: 20, // More than we have
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), series.len()); // Should be unchanged

        // Points should be identical
        for i in 0..series.len() {
            let original = series.get(i).unwrap();
            let aggregated_point = aggregated.get(i).unwrap();
            assert_eq!(original.x(), aggregated_point.x());
            assert_eq!(original.y(), aggregated_point.y());
        }
    }
}

mod other_aggregation_strategies_tests {
    use super::*;

    #[test]
    fn test_first_aggregation() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::First,
            target_points: 4,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 4);

        // Each group should take the first point
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 0.0);
        assert_eq!(first.y(), 10.0);

        let second = aggregated.get(1).unwrap();
        assert_eq!(second.x(), 2.0);
        assert_eq!(second.y(), 5.0);
    }

    #[test]
    fn test_last_aggregation() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Last,
            target_points: 4,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 4);

        // Each group should take the last point
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 1.0);
        assert_eq!(first.y(), 20.0);
    }

    #[test]
    fn test_max_aggregation() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Max,
            target_points: 4,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 4);

        // First group: (0,10) and (1,20) -> max should be (1,20)
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 1.0);
        assert_eq!(first.y(), 20.0);

        // Second group: (2,5) and (3,30) -> max should be (3,30)
        let second = aggregated.get(1).unwrap();
        assert_eq!(second.x(), 3.0);
        assert_eq!(second.y(), 30.0);
    }

    #[test]
    fn test_min_aggregation() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Min,
            target_points: 4,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 4);

        // First group: (0,10) and (1,20) -> min should be (0,10)
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 0.0);
        assert_eq!(first.y(), 10.0);

        // Second group: (2,5) and (3,30) -> min should be (2,5)
        let second = aggregated.get(1).unwrap();
        assert_eq!(second.x(), 2.0);
        assert_eq!(second.y(), 5.0);
    }

    #[test]
    fn test_minmax_aggregation() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::MinMax,
            target_points: 4,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 4);

        // MinMax should preserve extreme values (currently returns point with max Y)
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.y(), 20.0); // Max in first group
    }

    #[test]
    fn test_median_aggregation() {
        // Create data with clear median values
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(0.0, 10.0)).unwrap();
        series.push(Point2D::new(1.0, 20.0)).unwrap();
        series.push(Point2D::new(2.0, 30.0)).unwrap();
        series.push(Point2D::new(3.0, 40.0)).unwrap();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Median,
            target_points: 2,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 2);

        // First group: (0,10) and (1,20) -> median should be (0.5, 15.0)
        let first = aggregated.get(0).unwrap();
        assert_eq!(first.x(), 0.5);
        assert_eq!(first.y(), 15.0);

        // Second group: (2,30) and (3,40) -> median should be (2.5, 35.0)
        let second = aggregated.get(1).unwrap();
        assert_eq!(second.x(), 2.5);
        assert_eq!(second.y(), 35.0);
    }
}

mod lttb_downsampling_tests {
    use super::*;

    #[test]
    fn test_lttb_basic() {
        let series = create_large_test_data();

        let config = DownsamplingConfig {
            max_points: 20,
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> = series.downsample_lttb(&config).unwrap();
        assert_eq!(downsampled.len(), 20);

        // Should preserve endpoints
        let first = downsampled.get(0).unwrap();
        let original_first = series.get(0).unwrap();
        assert_eq!(first.x(), original_first.x());
        assert_eq!(first.y(), original_first.y());

        let last = downsampled.get(downsampled.len() - 1).unwrap();
        let original_last = series.get(series.len() - 1).unwrap();
        assert_eq!(last.x(), original_last.x());
        assert_eq!(last.y(), original_last.y());
    }

    #[test]
    fn test_lttb_no_reduction_needed() {
        let series = create_test_data(); // 8 points

        let config = DownsamplingConfig {
            max_points: 20, // More than we have
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> = series.downsample_lttb(&config).unwrap();
        assert_eq!(downsampled.len(), series.len()); // Should be unchanged
    }

    #[test]
    fn test_lttb_insufficient_reduction() {
        let series = create_test_data(); // 8 points

        let config = DownsamplingConfig {
            max_points: 6,
            preserve_endpoints: true,
            min_reduction_ratio: 2.0, // Require 2x reduction, but 8->6 is only 1.33x
        };

        let downsampled: StaticDataSeries<Point2D, 256> = series.downsample_lttb(&config).unwrap();
        assert_eq!(downsampled.len(), series.len()); // Should be unchanged due to ratio check
    }

    #[test]
    fn test_lttb_minimal_points() {
        let series = create_test_data();

        let config = DownsamplingConfig {
            max_points: 2,
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> = series.downsample_lttb(&config).unwrap();
        assert_eq!(downsampled.len(), 2);

        // Should be first and last points
        let first = downsampled.get(0).unwrap();
        assert_eq!(first.x(), 0.0);

        let last = downsampled.get(1).unwrap();
        assert_eq!(last.x(), 7.0);
    }

    #[test]
    fn test_lttb_single_point() {
        let series = create_test_data();

        let config = DownsamplingConfig {
            max_points: 1,
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> = series.downsample_lttb(&config).unwrap();
        assert_eq!(downsampled.len(), 1);

        // Should be the first point
        let first = downsampled.get(0).unwrap();
        assert_eq!(first.x(), 0.0);
        assert_eq!(first.y(), 10.0);
    }
}

mod uniform_downsampling_tests {
    use super::*;

    #[test]
    fn test_uniform_basic() {
        let series = create_test_data(); // 8 points

        let config = DownsamplingConfig {
            max_points: 4,
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> =
            series.downsample_uniform(&config).unwrap();
        assert_eq!(downsampled.len(), 4);

        // Should sample uniformly across the range
        // With 8 points -> 4 points, step = 2.0
        // Indices should be approximately: 0, 2, 4, 6
        let points: Vec<_> = (0..downsampled.len())
            .map(|i| downsampled.get(i).unwrap())
            .collect();

        // Check that we get a reasonable sampling
        assert!(points[0].x() < points[1].x());
        assert!(points[1].x() < points[2].x());
        assert!(points[2].x() < points[3].x());
    }

    #[test]
    fn test_uniform_no_reduction_needed() {
        let series = create_test_data(); // 8 points

        let config = DownsamplingConfig {
            max_points: 20, // More than we have
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> =
            series.downsample_uniform(&config).unwrap();
        assert_eq!(downsampled.len(), series.len()); // Should be unchanged
    }

    #[test]
    fn test_uniform_single_point() {
        let series = create_test_data();

        let config = DownsamplingConfig {
            max_points: 1,
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> =
            series.downsample_uniform(&config).unwrap();
        assert_eq!(downsampled.len(), 1);

        let point = downsampled.get(0).unwrap();
        assert_eq!(point.x(), 0.0);
        assert_eq!(point.y(), 10.0);
    }
}

mod edge_cases_tests {
    use super::*;

    #[test]
    fn test_empty_series_aggregation() {
        let series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

        let config = AggregationConfig::default();
        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 0);
    }

    #[test]
    fn test_empty_series_lttb() {
        let series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

        let config = DownsamplingConfig::default();
        let downsampled: StaticDataSeries<Point2D, 256> = series.downsample_lttb(&config).unwrap();
        assert_eq!(downsampled.len(), 0);
    }

    #[test]
    fn test_empty_series_uniform() {
        let series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

        let config = DownsamplingConfig::default();
        let downsampled: StaticDataSeries<Point2D, 256> =
            series.downsample_uniform(&config).unwrap();
        assert_eq!(downsampled.len(), 0);
    }

    #[test]
    fn test_single_point_aggregation() {
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(1.0, 2.0)).unwrap();

        let config = AggregationConfig::default();
        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 1);

        let point = aggregated.get(0).unwrap();
        assert_eq!(point.x(), 1.0);
        assert_eq!(point.y(), 2.0);
    }

    #[test]
    fn test_large_group_size() {
        let series = create_test_data();

        let config = AggregationConfig {
            strategy: AggregationStrategy::Mean,
            target_points: 1,
            preserve_endpoints: false,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        assert_eq!(aggregated.len(), 1);

        // Should be the mean of all points
        let point = aggregated.get(0).unwrap();
        assert_eq!(point.x(), 3.5); // Mean of 0,1,2,3,4,5,6,7
        assert_eq!(point.y(), 18.5); // Mean of Y values
    }
}

mod performance_tests {
    use super::*;

    #[test]
    fn test_large_dataset_performance() {
        // Create a larger dataset to test performance
        let mut series: StaticDataSeries<Point2D, 1024> = StaticDataSeries::new();

        for i in 0..1000 {
            let x = i as f32;
            let y = (x * 0.01).sin() * 100.0 + 100.0;
            series.push(Point2D::new(x, y)).unwrap();
        }

        // Test aggregation performance
        let config = AggregationConfig {
            strategy: AggregationStrategy::Mean,
            target_points: 100,
            preserve_endpoints: true,
            min_group_size: 1,
        };

        let aggregated: StaticDataSeries<Point2D, 256> = series.aggregate(&config).unwrap();
        // With endpoint preservation, we may get slightly more than target_points
        assert!(aggregated.len() >= 100 && aggregated.len() <= 102);

        // Test LTTB performance
        let lttb_config = DownsamplingConfig {
            max_points: 100,
            preserve_endpoints: true,
            min_reduction_ratio: 1.0,
        };

        let downsampled: StaticDataSeries<Point2D, 256> =
            series.downsample_lttb(&lttb_config).unwrap();
        assert_eq!(downsampled.len(), 100);
    }
}