scirs2-metrics 0.4.2

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
//! Helper functions for visualization
//!
//! This module provides helper functions for creating visualizations for common
//! metrics result types.

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use std::error::Error;

use crate::visualization::interactive::InteractiveOptions;
use crate::visualization::{ColorMap, PlotType, VisualizationData, VisualizationMetadata};

/// Create a confusion matrix visualization from a confusion matrix array
///
/// # Arguments
///
/// * `confusion_matrix` - The confusion matrix as a 2D array
/// * `class_names` - Optional class names
/// * `normalize` - Whether to normalize the confusion matrix
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the confusion matrix
#[allow(dead_code)]
pub fn visualize_confusion_matrix<A>(
    confusion_matrix: ArrayView2<A>,
    class_names: Option<Vec<String>>,
    normalize: bool,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert the confusion _matrix to f64
    let cm_f64 = Array2::from_shape_fn(confusion_matrix.dim(), |(i, j)| {
        confusion_matrix[[i, j]].clone().into()
    });

    crate::visualization::confusion_matrix::confusion_matrix_visualization(
        cm_f64,
        class_names,
        normalize,
    )
}

/// Create a ROC curve visualization
///
/// # Arguments
///
/// * `fpr` - False positive rates
/// * `tpr` - True positive rates
/// * `thresholds` - Optional thresholds
/// * `auc` - Optional area under the curve
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the ROC curve
#[allow(dead_code)]
pub fn visualize_roc_curve<A>(
    fpr: ArrayView1<A>,
    tpr: ArrayView1<A>,
    thresholds: Option<ArrayView1<A>>,
    auc: Option<f64>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert the arrays to f64 vectors
    let fpr_vec = fpr.iter().map(|x| x.clone().into()).collect::<Vec<f64>>();
    let tpr_vec = tpr.iter().map(|x| x.clone().into()).collect::<Vec<f64>>();
    let thresholds_vec =
        thresholds.map(|t| t.iter().map(|x| x.clone().into()).collect::<Vec<f64>>());

    Box::new(crate::visualization::roc_curve::roc_curve_visualization(
        fpr_vec,
        tpr_vec,
        thresholds_vec,
        auc,
    ))
}

/// Create an interactive ROC curve visualization
///
/// # Arguments
///
/// * `fpr` - False positive rates
/// * `tpr` - True positive rates
/// * `thresholds` - Optional thresholds
/// * `auc` - Optional area under the curve
/// * `interactive_options` - Optional interactive options
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - An interactive visualizer for the ROC curve
#[allow(dead_code)]
pub fn visualize_interactive_roc_curve<A>(
    fpr: ArrayView1<A>,
    tpr: ArrayView1<A>,
    thresholds: Option<ArrayView1<A>>,
    auc: Option<f64>,
    interactive_options: Option<InteractiveOptions>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert the arrays to f64 vectors
    let fpr_vec = fpr.iter().map(|x| x.clone().into()).collect::<Vec<f64>>();
    let tpr_vec = tpr.iter().map(|x| x.clone().into()).collect::<Vec<f64>>();
    let thresholds_vec =
        thresholds.map(|t| t.iter().map(|x| x.clone().into()).collect::<Vec<f64>>());

    let mut visualizer = crate::visualization::interactive::interactive_roc_curve_visualization(
        fpr_vec,
        tpr_vec,
        thresholds_vec,
        auc,
    );

    if let Some(_options) = interactive_options {
        visualizer = visualizer.with_interactive_options(_options);
    }

    Box::new(visualizer)
}

/// Create an interactive ROC curve visualization from labels and scores
///
/// # Arguments
///
/// * `y_true` - True binary labels
/// * `y_score` - Target scores (probabilities or decision function output)
/// * `pos_label` - Optional positive class label
/// * `interactive_options` - Optional interactive options
///
/// # Returns
///
/// * `Result<Box<dyn crate::visualization::MetricVisualizer>, Box<dyn Error>>` - An interactive visualizer for the ROC curve
#[allow(dead_code)]
pub fn visualize_interactive_roc_from_labels<A, B>(
    y_true: ArrayView1<A>,
    y_score: ArrayView1<B>,
    _pos_label: Option<A>,
    interactive_options: Option<InteractiveOptions>,
) -> Result<Box<dyn crate::visualization::MetricVisualizer>, Box<dyn Error>>
where
    A: Clone + PartialOrd + 'static,
    B: Clone + PartialOrd + 'static,
    f64: From<A> + From<B>,
{
    // Compute ROC curve
    let (fpr, tpr, _thresholds) = crate::classification::curves::roc_curve(&y_true, &y_score)
        .map_err(|e| Box::new(e) as Box<dyn Error>)?;

    // Calculate AUC - simplified version
    let auc = {
        let n = fpr.len();
        let mut area = 0.0;
        for i in 1..n {
            area += (fpr[i] - fpr[i - 1]) * (tpr[i] + tpr[i - 1]) / 2.0;
        }
        area
    };

    // Create an owned ROC curve visualizer using raw data
    let mut visualizer = crate::visualization::interactive::roc_curve::InteractiveROCVisualizer::<
        f64,
        scirs2_core::ndarray::OwnedRepr<f64>,
    >::new(fpr.to_vec(), tpr.to_vec(), None, Some(auc));

    if let Some(_options) = interactive_options {
        visualizer = visualizer.with_interactive_options(_options);
    }

    Ok(Box::new(visualizer))
}

/// Create a precision-recall curve visualization
///
/// # Arguments
///
/// * `precision` - Precision values
/// * `recall` - Recall values
/// * `thresholds` - Optional thresholds
/// * `average_precision` - Optional average precision
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the precision-recall curve
#[allow(dead_code)]
pub fn visualize_precision_recall_curve<A>(
    precision: ArrayView1<A>,
    recall: ArrayView1<A>,
    thresholds: Option<ArrayView1<A>>,
    average_precision: Option<f64>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert the arrays to f64 vectors
    let precision_vec = precision
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();
    let recall_vec = recall
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();
    let thresholds_vec =
        thresholds.map(|t| t.iter().map(|x| x.clone().into()).collect::<Vec<f64>>());

    Box::new(
        crate::visualization::precision_recall::precision_recall_visualization(
            precision_vec,
            recall_vec,
            thresholds_vec,
            average_precision,
        ),
    )
}

/// Create a calibration curve visualization
///
/// # Arguments
///
/// * `prob_true` - True probabilities
/// * `prob_pred` - Predicted probabilities
/// * `n_bins` - Number of bins
/// * `strategy` - Binning strategy ("uniform" or "quantile")
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the calibration curve
#[allow(dead_code)]
pub fn visualize_calibration_curve<A>(
    prob_true: ArrayView1<A>,
    prob_pred: ArrayView1<A>,
    n_bins: usize,
    strategy: impl Into<String>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert the arrays to f64 vectors
    let prob_true_vec = prob_true
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();
    let prob_pred_vec = prob_pred
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();

    Box::new(
        crate::visualization::calibration::calibration_visualization(
            prob_true_vec,
            prob_pred_vec,
            n_bins,
            strategy.into(),
        ),
    )
}

/// Create a learning curve visualization
///
/// # Arguments
///
/// * `train_sizes` - Training set sizes
/// * `train_scores` - Training scores (multiple runs for each size)
/// * `val_scores` - Validation scores (multiple runs for each size)
/// * `score_name` - Name of the score (e.g., "Accuracy")
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the learning curve
/// * `Result<Box<dyn crate::visualization::MetricVisualizer>, Box<dyn Error>>` - A visualizer for the learning curve, or an error
#[allow(dead_code)]
pub fn visualize_learning_curve(
    train_sizes: Vec<usize>,
    train_scores: Vec<Vec<f64>>,
    val_scores: Vec<Vec<f64>>,
    score_name: impl Into<String>,
) -> Result<Box<dyn crate::visualization::MetricVisualizer>, Box<dyn Error>> {
    let visualizer = crate::visualization::learning_curve::learning_curve_visualization(
        train_sizes,
        train_scores,
        val_scores,
        score_name,
    )?;

    Ok(Box::new(visualizer))
}

/// Create a generic metric visualization
///
/// This function creates a visualization for generic metric data,
/// such as performance over time, hyperparameter tuning results, etc.
///
/// # Arguments
///
/// * `x_values` - X-axis values
/// * `y_values` - Y-axis values
/// * `title` - Plot title
/// * `x_label` - X-axis label
/// * `y_label` - Y-axis label
/// * `plot_type` - Plot type
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the generic metric
#[allow(dead_code)]
pub fn visualize_metric<A, B>(
    x_values: ArrayView1<A>,
    y_values: ArrayView1<B>,
    title: impl Into<String>,
    x_label: impl Into<String>,
    y_label: impl Into<String>,
    plot_type: PlotType,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
    B: Clone + Into<f64>,
{
    let x_vec = x_values
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();
    let y_vec = y_values
        .iter()
        .map(|y| y.clone().into())
        .collect::<Vec<f64>>();

    Box::new(GenericMetricVisualizer::new(
        x_vec,
        y_vec,
        title.into(),
        x_label.into(),
        y_label.into(),
        plot_type,
    ))
}

/// A generic visualizer for metric data
pub struct GenericMetricVisualizer {
    /// X-axis values
    pub x: Vec<f64>,
    /// Y-axis values
    pub y: Vec<f64>,
    /// Title
    pub title: String,
    /// X-axis label
    pub x_label: String,
    /// Y-axis label
    pub y_label: String,
    /// Plot type
    pub plot_type: PlotType,
    /// Optional series names
    pub series_names: Option<Vec<String>>,
}

impl GenericMetricVisualizer {
    /// Create a new generic metric visualizer
    pub fn new(
        x: Vec<f64>,
        y: Vec<f64>,
        title: impl Into<String>,
        x_label: impl Into<String>,
        y_label: impl Into<String>,
        plot_type: PlotType,
    ) -> Self {
        Self {
            x,
            y,
            title: title.into(),
            x_label: x_label.into(),
            y_label: y_label.into(),
            plot_type,
            series_names: None,
        }
    }

    /// Add series names
    pub fn with_series_names(mut self, seriesnames: Vec<String>) -> Self {
        self.series_names = Some(seriesnames);
        self
    }
}

impl crate::visualization::MetricVisualizer for GenericMetricVisualizer {
    fn prepare_data(&self) -> Result<VisualizationData, Box<dyn Error>> {
        let mut data = VisualizationData::new();

        // Set x and y data
        data.x = self.x.clone();
        data.y = self.y.clone();

        // Add series names if available
        if let Some(series_names) = &self.series_names {
            data.series_names = Some(series_names.clone());
        }

        Ok(data)
    }

    fn get_metadata(&self) -> VisualizationMetadata {
        let mut metadata = VisualizationMetadata::new(self.title.clone());
        metadata.set_plot_type(self.plot_type.clone());
        metadata.set_x_label(self.x_label.clone());
        metadata.set_y_label(self.y_label.clone());
        metadata
    }
}

/// Create a multi-curve visualization
///
/// This function creates a visualization with multiple curves,
/// such as performance comparisons between different models.
///
/// # Arguments
///
/// * `x_values` - X-axis values (common for all curves)
/// * `y_values_list` - List of Y-axis values, one for each curve
/// * `series_names` - Names for each curve
/// * `title` - Plot title
/// * `x_label` - X-axis label
/// * `y_label` - Y-axis label
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the multi-curve plot
#[allow(dead_code)]
pub fn visualize_multi_curve<A, B>(
    x_values: ArrayView1<A>,
    y_values_list: Vec<ArrayView1<B>>,
    series_names: Vec<String>,
    title: impl Into<String>,
    x_label: impl Into<String>,
    y_label: impl Into<String>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
    B: Clone + Into<f64>,
{
    let x_vec = x_values
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();

    // Set the first y-_values as the main y-axis data
    let y_vec = if !y_values_list.is_empty() {
        y_values_list[0]
            .iter()
            .map(|y| y.clone().into())
            .collect::<Vec<f64>>()
    } else {
        Vec::new()
    };

    // Create a visualizer
    let mut visualizer =
        MultiCurveVisualizer::new(x_vec, y_vec, title.into(), x_label.into(), y_label.into());

    // Add all series
    for (i, y_values) in y_values_list.iter().enumerate() {
        if i == 0 {
            // Skip the first one, already added as main y-axis
            continue;
        }

        let name = if i < series_names.len() {
            series_names[i].clone()
        } else {
            format!("Series {}", i + 1)
        };

        let y_vec = y_values
            .iter()
            .map(|y| y.clone().into())
            .collect::<Vec<f64>>();
        visualizer.add_series(name, y_vec);
    }

    // Set all series _names
    visualizer.set_series_names(series_names);

    Box::new(visualizer)
}

/// A visualizer for multi-curve plots
pub struct MultiCurveVisualizer {
    /// X-axis values
    pub x: Vec<f64>,
    /// Y-axis values for the main curve
    pub y: Vec<f64>,
    /// Additional Y-axis values for secondary curves
    pub secondary_y: Vec<(String, Vec<f64>)>,
    /// Title
    pub title: String,
    /// X-axis label
    pub x_label: String,
    /// Y-axis label
    pub y_label: String,
    /// Series names
    pub series_names: Vec<String>,
}

impl MultiCurveVisualizer {
    /// Create a new multi-curve visualizer
    pub fn new(
        x: Vec<f64>,
        y: Vec<f64>,
        title: impl Into<String>,
        x_label: impl Into<String>,
        y_label: impl Into<String>,
    ) -> Self {
        Self {
            x,
            y,
            secondary_y: Vec::new(),
            title: title.into(),
            x_label: x_label.into(),
            y_label: y_label.into(),
            series_names: Vec::new(),
        }
    }

    /// Add a secondary curve
    pub fn add_series(&mut self, name: impl Into<String>, y: Vec<f64>) {
        self.secondary_y.push((name.into(), y));
    }

    /// Set series names
    pub fn set_series_names(&mut self, names: Vec<String>) {
        self.series_names = names;
    }
}

impl crate::visualization::MetricVisualizer for MultiCurveVisualizer {
    fn prepare_data(&self) -> Result<VisualizationData, Box<dyn Error>> {
        let mut data = VisualizationData::new();

        // Set main x and y data
        data.x = self.x.clone();
        data.y = self.y.clone();

        // Add secondary curves
        for (name, y) in &self.secondary_y {
            data.series.insert(name.clone(), y.clone());
        }

        // Add series names
        if !self.series_names.is_empty() {
            data.series_names = Some(self.series_names.clone());
        }

        Ok(data)
    }

    fn get_metadata(&self) -> VisualizationMetadata {
        let mut metadata = VisualizationMetadata::new(self.title.clone());
        metadata.set_plot_type(PlotType::Line);
        metadata.set_x_label(self.x_label.clone());
        metadata.set_y_label(self.y_label.clone());
        metadata
    }
}

/// Create a heatmap visualization
///
/// This function creates a heatmap visualization for 2D data,
/// such as correlation matrices, distance matrices, etc.
///
/// # Arguments
///
/// * `matrix` - 2D data matrix
/// * `x_labels` - Optional labels for x-axis
/// * `y_labels` - Optional labels for y-axis
/// * `title` - Plot title
/// * `color_map` - Optional color map
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the heatmap
#[allow(dead_code)]
pub fn visualize_heatmap<A>(
    matrix: ArrayView2<A>,
    x_labels: Option<Vec<String>>,
    y_labels: Option<Vec<String>>,
    title: impl Into<String>,
    color_map: Option<ColorMap>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert matrix to Vec<Vec<f64>>
    let z = Array2::from_shape_fn(matrix.dim(), |(i, j)| matrix[[i, j]].clone().into());

    let z_vec = (0..z.shape()[0])
        .map(|i| (0..z.shape()[1]).map(|j| z[[i, j]]).collect::<Vec<f64>>())
        .collect::<Vec<Vec<f64>>>();

    // Create x and y coordinates for the heatmap
    let x = (0..z.shape()[1]).map(|i| i as f64).collect::<Vec<f64>>();
    let y = (0..z.shape()[0]).map(|i| i as f64).collect::<Vec<f64>>();

    Box::new(HeatmapVisualizer::new(
        x,
        y,
        z_vec,
        title.into(),
        x_labels,
        y_labels,
        color_map,
    ))
}

/// A visualizer for heatmaps
pub struct HeatmapVisualizer {
    /// X-axis values
    pub x: Vec<f64>,
    /// Y-axis values
    pub y: Vec<f64>,
    /// Z-axis values (2D matrix)
    pub z: Vec<Vec<f64>>,
    /// Title
    pub title: String,
    /// X-axis labels
    pub x_labels: Option<Vec<String>>,
    /// Y-axis labels
    pub y_labels: Option<Vec<String>>,
    /// Color map
    pub color_map: Option<ColorMap>,
}

impl HeatmapVisualizer {
    /// Create a new heatmap visualizer
    pub fn new(
        x: Vec<f64>,
        y: Vec<f64>,
        z: Vec<Vec<f64>>,
        title: impl Into<String>,
        x_labels: Option<Vec<String>>,
        y_labels: Option<Vec<String>>,
        color_map: Option<ColorMap>,
    ) -> Self {
        Self {
            x,
            y,
            z,
            title: title.into(),
            x_labels,
            y_labels,
            color_map,
        }
    }
}

impl crate::visualization::MetricVisualizer for HeatmapVisualizer {
    fn prepare_data(&self) -> Result<VisualizationData, Box<dyn Error>> {
        let mut data = VisualizationData::new();

        // Set x, y, and z data
        data.x = self.x.clone();
        data.y = self.y.clone();
        data.z = Some(self.z.clone());

        // Add axis labels if available
        if let Some(x_labels) = &self.x_labels {
            data.x_labels = Some(x_labels.clone());
        }

        if let Some(y_labels) = &self.y_labels {
            data.y_labels = Some(y_labels.clone());
        }

        Ok(data)
    }

    fn get_metadata(&self) -> VisualizationMetadata {
        let mut metadata = VisualizationMetadata::new(self.title.clone());
        metadata.set_plot_type(PlotType::Heatmap);

        // Set default axis labels if none are provided
        if self.x_labels.is_none() {
            metadata.set_x_label("X");
        } else {
            metadata.set_x_label(""); // Labels are provided directly
        }

        if self.y_labels.is_none() {
            metadata.set_y_label("Y");
        } else {
            metadata.set_y_label(""); // Labels are provided directly
        }

        metadata
    }
}

/// Create a histogram visualization
///
/// This function creates a histogram visualization for 1D data.
///
/// # Arguments
///
/// * `values` - Data values
/// * `bins` - Number of bins
/// * `title` - Plot title
/// * `x_label` - X-axis label
/// * `y_label` - Y-axis label (defaults to "Frequency")
///
/// # Returns
///
/// * `Box<dyn crate::visualization::MetricVisualizer>` - A visualizer for the histogram
#[allow(dead_code)]
pub fn visualize_histogram<A>(
    values: ArrayView1<A>,
    bins: usize,
    title: impl Into<String>,
    x_label: impl Into<String>,
    y_label: Option<String>,
) -> Box<dyn crate::visualization::MetricVisualizer>
where
    A: Clone + Into<f64>,
{
    // Convert values to f64 vector
    let values_vec = values
        .iter()
        .map(|x| x.clone().into())
        .collect::<Vec<f64>>();

    // Create histogram bins
    let (bin_edges, bin_counts) = create_histogram_bins(&values_vec, bins);

    Box::new(HistogramVisualizer::new(
        bin_edges,
        bin_counts,
        title.into(),
        x_label.into(),
        y_label.unwrap_or_else(|| "Frequency".to_string()),
    ))
}

/// Create histogram bins from data values
///
/// # Arguments
///
/// * `values` - Data values
/// * `bins` - Number of bins
///
/// # Returns
///
/// * `(Vec<f64>, Vec<f64>)` - Bin edges and bin counts
#[allow(dead_code)]
fn create_histogram_bins(values: &[f64], bins: usize) -> (Vec<f64>, Vec<f64>) {
    // Ensure we have at least one value and valid bins
    if values.is_empty() || bins == 0 {
        return (Vec::new(), Vec::new());
    }

    // Find min and max _values
    let min_val = values.iter().fold(f64::INFINITY, |min, &val| min.min(val));
    let max_val = values
        .iter()
        .fold(f64::NEG_INFINITY, |max, &val| max.max(val));

    // Create bin edges
    let bin_width = (max_val - min_val) / bins as f64;
    let mut bin_edges = Vec::with_capacity(bins + 1);
    for i in 0..=bins {
        bin_edges.push(min_val + i as f64 * bin_width);
    }

    // Count _values in each bin
    let mut bin_counts = vec![0.0; bins];
    for &val in values {
        if val >= min_val && val <= max_val {
            let bin_idx = ((val - min_val) / bin_width).floor() as usize;
            // Handle the edge case where val is exactly max_val
            let bin_idx = bin_idx.min(bins - 1);
            bin_counts[bin_idx] += 1.0;
        }
    }

    (bin_edges, bin_counts)
}

/// A visualizer for histograms
pub struct HistogramVisualizer {
    /// Bin edges
    pub bin_edges: Vec<f64>,
    /// Bin counts
    pub bin_counts: Vec<f64>,
    /// Title
    pub title: String,
    /// X-axis label
    pub x_label: String,
    /// Y-axis label
    pub y_label: String,
}

impl HistogramVisualizer {
    /// Create a new histogram visualizer
    pub fn new(
        bin_edges: Vec<f64>,
        bin_counts: Vec<f64>,
        title: impl Into<String>,
        x_label: impl Into<String>,
        y_label: impl Into<String>,
    ) -> Self {
        Self {
            bin_edges,
            bin_counts,
            title: title.into(),
            x_label: x_label.into(),
            y_label: y_label.into(),
        }
    }
}

impl crate::visualization::MetricVisualizer for HistogramVisualizer {
    fn prepare_data(&self) -> Result<VisualizationData, Box<dyn Error>> {
        let mut data = VisualizationData::new();

        // Use bin centers as x values
        if self.bin_edges.len() > 1 {
            let bin_centers = self
                .bin_edges
                .windows(2)
                .map(|w| (w[0] + w[1]) / 2.0)
                .collect::<Vec<f64>>();

            data.x = bin_centers;
        } else {
            data.x = Vec::new();
        }

        // Use bin counts as y values
        data.y = self.bin_counts.clone();

        // Store bin edges in auxiliary data
        data.add_auxiliary_data("bin_edges", self.bin_edges.clone());

        Ok(data)
    }

    fn get_metadata(&self) -> VisualizationMetadata {
        let mut metadata = VisualizationMetadata::new(self.title.clone());
        metadata.set_plot_type(PlotType::Histogram);
        metadata.set_x_label(self.x_label.clone());
        metadata.set_y_label(self.y_label.clone());
        metadata
    }
}