sklears-neural 0.2.0

Neural network implementations for the sklears machine learning library
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
//! Comprehensive experiment tracking system for neural network training.
//!
//! This module provides a robust experiment tracking framework that enables researchers
//! and practitioners to log, monitor, and compare machine learning experiments. It includes
//! support for hyperparameter logging, metric tracking, model versioning, artifact storage,
//! and experiment comparison utilities.

use crate::versioning::ModelVersion;
use sklears_core::error::SklearsError;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Result type for experiment tracking operations
pub type ExperimentResult<T> = Result<T, SklearsError>;

/// Unique identifier for experiments
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ExperimentId(pub String);

impl ExperimentId {
    /// Generate a new unique experiment ID
    pub fn generate() -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_millis();

        let random_suffix = scirs2_core::random::thread_rng().random::<u32>();
        Self(format!("exp_{timestamp}_{random_suffix}"))
    }

    /// Create an experiment ID from a custom string
    pub fn from_string(id: String) -> Self {
        Self(id)
    }

    /// Get the inner string representation
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for ExperimentId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for ExperimentId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

/// Experiment status
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ExperimentStatus {
    /// Experiment is currently running
    Running,
    /// Experiment completed successfully
    Completed,
    /// Experiment failed with error
    Failed {
        /// Human-readable error message or traceback describing the failure
        error: String,
    },
    /// Experiment was cancelled
    Cancelled,
    /// Experiment is queued for execution
    Queued,
}

/// Hyperparameter value types
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum HyperparameterValue {
    /// Single 64-bit floating-point value
    Float(f64),
    /// Single 64-bit signed integer value
    Int(i64),
    /// UTF-8 string value
    String(String),
    /// Boolean flag
    Bool(bool),
    /// Array of floating-point values (e.g., layer sizes as floats)
    FloatArray(Vec<f64>),
    /// Array of integer values (e.g., layer sizes)
    IntArray(Vec<i64>),
    /// Array of string values (e.g., activation names per layer)
    StringArray(Vec<String>),
}

impl From<f64> for HyperparameterValue {
    fn from(val: f64) -> Self {
        HyperparameterValue::Float(val)
    }
}

impl From<i64> for HyperparameterValue {
    fn from(val: i64) -> Self {
        HyperparameterValue::Int(val)
    }
}

impl From<String> for HyperparameterValue {
    fn from(val: String) -> Self {
        HyperparameterValue::String(val)
    }
}

impl From<bool> for HyperparameterValue {
    fn from(val: bool) -> Self {
        HyperparameterValue::Bool(val)
    }
}

impl From<Vec<f64>> for HyperparameterValue {
    fn from(val: Vec<f64>) -> Self {
        HyperparameterValue::FloatArray(val)
    }
}

/// Metric value with optional step information
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MetricValue {
    /// Numeric scalar value of the metric at this observation
    pub value: f64,
    /// Training step or epoch at which this value was recorded, if applicable
    pub step: Option<u64>,
    /// Unix timestamp (seconds) when this metric was recorded
    pub timestamp: u64,
    /// Arbitrary key-value tags associated with this metric observation
    pub tags: HashMap<String, String>,
}

impl MetricValue {
    /// Create a new metric value observed at the current wall-clock time with no step or tags
    pub fn new(value: f64) -> Self {
        Self {
            value,
            step: None,
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
            tags: HashMap::new(),
        }
    }

    /// Attach a training step or epoch index to this metric observation
    pub fn with_step(mut self, step: u64) -> Self {
        self.step = Some(step);
        self
    }

    /// Attach a single key-value tag to this metric observation
    pub fn with_tag(mut self, key: String, value: String) -> Self {
        self.tags.insert(key, value);
        self
    }
}

/// Artifact information
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Artifact {
    /// Human-readable name identifying this artifact
    pub name: String,
    /// Category of the artifact (e.g., `"model"`, `"dataset"`, `"plot"`)
    pub artifact_type: String,
    /// Filesystem path where the artifact is stored
    pub path: PathBuf,
    /// Size of the artifact file in bytes, or 0 if the path was not accessible at creation time
    pub size_bytes: u64,
    /// Optional content checksum (e.g., SHA-256 hex string) for integrity verification
    pub checksum: Option<String>,
    /// Arbitrary key-value metadata attached to this artifact
    pub metadata: HashMap<String, String>,
    /// Unix timestamp (seconds) when this artifact record was created
    pub created_at: u64,
}

impl Artifact {
    /// Create an artifact record pointing to the given path, auto-detecting file size
    pub fn new(name: String, artifact_type: String, path: PathBuf) -> Self {
        let size_bytes = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);

        Self {
            name,
            artifact_type,
            path,
            size_bytes,
            checksum: None,
            metadata: HashMap::new(),
            created_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
        }
    }

    /// Attach a checksum string (e.g., SHA-256 hex) to this artifact for integrity verification
    pub fn with_checksum(mut self, checksum: String) -> Self {
        self.checksum = Some(checksum);
        self
    }

    /// Replace this artifact's metadata map with the provided key-value pairs
    pub fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
        self.metadata = metadata;
        self
    }
}

/// Comprehensive experiment configuration and results
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Experiment {
    /// Unique experiment identifier
    pub id: ExperimentId,
    /// Human-readable experiment name
    pub name: String,
    /// Experiment description
    pub description: Option<String>,
    /// Experiment status
    pub status: ExperimentStatus,
    /// Hyperparameters used in the experiment
    pub hyperparameters: HashMap<String, HyperparameterValue>,
    /// Metrics collected during the experiment
    pub metrics: HashMap<String, Vec<MetricValue>>,
    /// Final evaluation results
    pub results: HashMap<String, f64>,
    /// Model version used
    pub model_version: Option<ModelVersion>,
    /// Experiment tags for organization
    pub tags: HashMap<String, String>,
    /// Artifacts produced by the experiment
    pub artifacts: Vec<Artifact>,
    /// Parent experiment (for experiment hierarchies)
    pub parent_id: Option<ExperimentId>,
    /// Child experiments
    pub child_ids: Vec<ExperimentId>,
    /// Unix timestamp (seconds) when this experiment record was created
    pub created_at: u64,
    /// Unix timestamp (seconds) when the experiment began executing; `None` if not yet started
    pub started_at: Option<u64>,
    /// Unix timestamp (seconds) when the experiment finished; `None` if still running or not started
    pub finished_at: Option<u64>,
    /// Environment information
    pub environment: HashMap<String, String>,
    /// Git commit hash at experiment time, if determinable
    pub git_commit: Option<String>,
    /// Name of the active Git branch at experiment time, if determinable
    pub git_branch: Option<String>,
    /// Notes and observations
    pub notes: Vec<String>,
    /// Resource usage
    pub resource_usage: Option<ResourceUsage>,
}

/// Resource usage information
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ResourceUsage {
    /// Total CPU-hours consumed by the experiment
    pub cpu_hours: f64,
    /// Peak RAM usage in gigabytes during the experiment
    pub memory_peak_gb: f64,
    /// Total GPU-hours consumed by the experiment
    pub gpu_hours: f64,
    /// Disk space used by experiment artifacts in gigabytes
    pub disk_usage_gb: f64,
}

impl Experiment {
    /// Create a new experiment with the given name, auto-generating an ID and capturing environment info
    pub fn new(name: String) -> Self {
        let id = ExperimentId::generate();
        let created_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();

        Self {
            id,
            name,
            description: None,
            status: ExperimentStatus::Queued,
            hyperparameters: HashMap::new(),
            metrics: HashMap::new(),
            results: HashMap::new(),
            model_version: None,
            tags: HashMap::new(),
            artifacts: Vec::new(),
            parent_id: None,
            child_ids: Vec::new(),
            created_at,
            started_at: None,
            finished_at: None,
            environment: Self::collect_environment_info(),
            git_commit: Self::get_git_commit(),
            git_branch: Self::get_git_branch(),
            notes: Vec::new(),
            resource_usage: None,
        }
    }

    /// Override the auto-generated experiment ID with a specific value
    pub fn with_id(mut self, id: ExperimentId) -> Self {
        self.id = id;
        self
    }

    /// Attach a human-readable description to this experiment
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    /// Record which model version was used in this experiment
    pub fn with_model_version(mut self, version: ModelVersion) -> Self {
        self.model_version = Some(version);
        self
    }

    /// Link this experiment as a child of the given parent experiment
    pub fn with_parent(mut self, parent_id: ExperimentId) -> Self {
        self.parent_id = Some(parent_id);
        self
    }

    /// Start the experiment (sets status to Running and records start time)
    pub fn start(&mut self) {
        self.status = ExperimentStatus::Running;
        self.started_at = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
        );
    }

    /// Complete the experiment successfully
    pub fn complete(&mut self) {
        self.status = ExperimentStatus::Completed;
        self.finished_at = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
        );
    }

    /// Mark the experiment as failed
    pub fn fail(&mut self, error: String) {
        self.status = ExperimentStatus::Failed { error };
        self.finished_at = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
        );
    }

    /// Cancel the experiment
    pub fn cancel(&mut self) {
        self.status = ExperimentStatus::Cancelled;
        self.finished_at = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("value should be present")
                .as_secs(),
        );
    }

    /// Log a hyperparameter
    pub fn log_hyperparameter<T: Into<HyperparameterValue>>(&mut self, name: String, value: T) {
        self.hyperparameters.insert(name, value.into());
    }

    /// Log multiple hyperparameters at once
    pub fn log_hyperparameters(&mut self, params: HashMap<String, HyperparameterValue>) {
        self.hyperparameters.extend(params);
    }

    /// Log a metric value
    /// Log a metric value to the experiment
    pub fn log_metric(&mut self, name: String, value: MetricValue) {
        self.metrics.entry(name).or_default().push(value);
    }

    /// Log a simple metric value with current timestamp
    pub fn log_metric_simple(&mut self, name: String, value: f64, step: Option<u64>) {
        let metric = if let Some(s) = step {
            MetricValue::new(value).with_step(s)
        } else {
            MetricValue::new(value)
        };
        self.log_metric(name, metric);
    }

    /// Log multiple metrics at once
    pub fn log_metrics(&mut self, metrics: HashMap<String, f64>, step: Option<u64>) {
        for (name, value) in metrics {
            self.log_metric_simple(name, value, step);
        }
    }

    /// Set a final result
    pub fn set_result(&mut self, name: String, value: f64) {
        self.results.insert(name, value);
    }

    /// Set multiple results
    pub fn set_results(&mut self, results: HashMap<String, f64>) {
        self.results.extend(results);
    }

    /// Add a tag
    pub fn add_tag(&mut self, key: String, value: String) {
        self.tags.insert(key, value);
    }

    /// Add multiple tags
    pub fn add_tags(&mut self, tags: HashMap<String, String>) {
        self.tags.extend(tags);
    }

    /// Add an artifact
    pub fn add_artifact(&mut self, artifact: Artifact) {
        self.artifacts.push(artifact);
    }

    /// Add a child experiment
    pub fn add_child(&mut self, child_id: ExperimentId) {
        self.child_ids.push(child_id);
    }

    /// Add a note
    pub fn add_note(&mut self, note: String) {
        self.notes.push(note);
    }

    /// Set resource usage
    pub fn set_resource_usage(&mut self, usage: ResourceUsage) {
        self.resource_usage = Some(usage);
    }

    /// Get experiment duration in seconds
    pub fn duration_seconds(&self) -> Option<u64> {
        match (self.started_at, self.finished_at) {
            (Some(start), Some(end)) => Some(end - start),
            _ => None,
        }
    }

    /// Check if experiment is finished
    pub fn is_finished(&self) -> bool {
        matches!(
            self.status,
            ExperimentStatus::Completed
                | ExperimentStatus::Failed { .. }
                | ExperimentStatus::Cancelled
        )
    }

    /// Collect environment information
    fn collect_environment_info() -> HashMap<String, String> {
        let mut env = HashMap::new();

        env.insert(
            "rust_version".to_string(),
            env!("CARGO_PKG_RUST_VERSION").to_string(),
        );
        env.insert(
            "sklears_version".to_string(),
            env!("CARGO_PKG_VERSION").to_string(),
        );

        if let Ok(hostname) = std::env::var("HOSTNAME") {
            env.insert("hostname".to_string(), hostname);
        }

        if let Ok(user) = std::env::var("USER") {
            env.insert("user".to_string(), user);
        }

        env.insert("os".to_string(), std::env::consts::OS.to_string());
        env.insert("arch".to_string(), std::env::consts::ARCH.to_string());

        env
    }

    /// Get git commit hash (if available)
    fn get_git_commit() -> Option<String> {
        std::process::Command::new("git")
            .args(["rev-parse", "HEAD"])
            .output()
            .ok()
            .and_then(|output| {
                if output.status.success() {
                    String::from_utf8(output.stdout)
                        .ok()
                        .map(|s| s.trim().to_string())
                } else {
                    None
                }
            })
    }

    /// Get git branch (if available)
    fn get_git_branch() -> Option<String> {
        std::process::Command::new("git")
            .args(["branch", "--show-current"])
            .output()
            .ok()
            .and_then(|output| {
                if output.status.success() {
                    String::from_utf8(output.stdout)
                        .ok()
                        .map(|s| s.trim().to_string())
                } else {
                    None
                }
            })
    }
}

/// Experiment tracking backend trait
pub trait ExperimentBackend {
    /// Store an experiment
    fn store_experiment(&mut self, experiment: &Experiment) -> ExperimentResult<()>;

    /// Load an experiment by ID
    fn load_experiment(&self, id: &ExperimentId) -> ExperimentResult<Experiment>;

    /// Update an existing experiment
    fn update_experiment(&mut self, experiment: &Experiment) -> ExperimentResult<()>;

    /// Delete an experiment
    fn delete_experiment(&mut self, id: &ExperimentId) -> ExperimentResult<()>;

    /// List all experiments
    fn list_experiments(&self) -> ExperimentResult<Vec<ExperimentId>>;

    /// Search experiments by criteria
    fn search_experiments(&self, query: &ExperimentQuery) -> ExperimentResult<Vec<ExperimentId>>;
}

/// Query criteria for searching experiments
#[derive(Debug, Clone)]
pub struct ExperimentQuery {
    /// Substring pattern that the experiment name must contain; `None` matches all names
    pub name_pattern: Option<String>,
    /// Required experiment status; `None` matches all statuses
    pub status: Option<ExperimentStatus>,
    /// Required tag key-value pairs; all entries must be present on a matching experiment
    pub tags: HashMap<String, String>,
    /// Lower bound on creation timestamp (Unix seconds, inclusive); `None` disables the lower bound
    pub created_after: Option<u64>,
    /// Upper bound on creation timestamp (Unix seconds, inclusive); `None` disables the upper bound
    pub created_before: Option<u64>,
    /// Required parent experiment ID; `None` matches experiments regardless of parent
    pub parent_id: Option<ExperimentId>,
}

impl ExperimentQuery {
    /// Create an empty query that matches all experiments
    pub fn new() -> Self {
        Self {
            name_pattern: None,
            status: None,
            tags: HashMap::new(),
            created_after: None,
            created_before: None,
            parent_id: None,
        }
    }

    /// Restrict matches to experiments whose name contains the given substring pattern
    pub fn with_name_pattern(mut self, pattern: String) -> Self {
        self.name_pattern = Some(pattern);
        self
    }

    /// Restrict matches to experiments in the given status
    pub fn with_status(mut self, status: ExperimentStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Add a required tag key-value pair that matching experiments must possess
    pub fn with_tag(mut self, key: String, value: String) -> Self {
        self.tags.insert(key, value);
        self
    }

    /// Restrict matches to experiments that are children of the given parent
    pub fn with_parent(mut self, parent_id: ExperimentId) -> Self {
        self.parent_id = Some(parent_id);
        self
    }
}

impl Default for ExperimentQuery {
    fn default() -> Self {
        Self::new()
    }
}

/// In-memory experiment backend for testing and simple use cases
#[derive(Debug, Clone, Default)]
pub struct InMemoryBackend {
    experiments: HashMap<ExperimentId, Experiment>,
}

impl InMemoryBackend {
    /// Create an empty in-memory backend with no stored experiments
    pub fn new() -> Self {
        Self {
            experiments: HashMap::new(),
        }
    }
}

impl ExperimentBackend for InMemoryBackend {
    fn store_experiment(&mut self, experiment: &Experiment) -> ExperimentResult<()> {
        self.experiments
            .insert(experiment.id.clone(), experiment.clone());
        Ok(())
    }

    fn load_experiment(&self, id: &ExperimentId) -> ExperimentResult<Experiment> {
        self.experiments
            .get(id)
            .cloned()
            .ok_or_else(|| SklearsError::InvalidParameter {
                name: "experiment_id".to_string(),
                reason: format!("Experiment with ID {} not found", id),
            })
    }

    fn update_experiment(&mut self, experiment: &Experiment) -> ExperimentResult<()> {
        if self.experiments.contains_key(&experiment.id) {
            self.experiments
                .insert(experiment.id.clone(), experiment.clone());
            Ok(())
        } else {
            Err(SklearsError::InvalidParameter {
                name: "experiment_id".to_string(),
                reason: format!("Experiment with ID {} not found", experiment.id),
            })
        }
    }

    fn delete_experiment(&mut self, id: &ExperimentId) -> ExperimentResult<()> {
        self.experiments
            .remove(id)
            .ok_or_else(|| SklearsError::InvalidParameter {
                name: "experiment_id".to_string(),
                reason: format!("Experiment with ID {} not found", id),
            })?;
        Ok(())
    }

    fn list_experiments(&self) -> ExperimentResult<Vec<ExperimentId>> {
        Ok(self.experiments.keys().cloned().collect())
    }

    fn search_experiments(&self, query: &ExperimentQuery) -> ExperimentResult<Vec<ExperimentId>> {
        let results: Vec<ExperimentId> = self
            .experiments
            .values()
            .filter(|exp| {
                // Filter by name pattern
                if let Some(ref pattern) = query.name_pattern {
                    if !exp.name.contains(pattern) {
                        return false;
                    }
                }

                // Filter by status
                if let Some(ref status) = query.status {
                    if exp.status != *status {
                        return false;
                    }
                }

                // Filter by tags
                for (key, value) in &query.tags {
                    if exp.tags.get(key) != Some(value) {
                        return false;
                    }
                }

                // Filter by creation time
                if let Some(after) = query.created_after {
                    if exp.created_at < after {
                        return false;
                    }
                }

                if let Some(before) = query.created_before {
                    if exp.created_at > before {
                        return false;
                    }
                }

                // Filter by parent
                if let Some(ref parent) = query.parent_id {
                    if exp.parent_id.as_ref() != Some(parent) {
                        return false;
                    }
                }

                true
            })
            .map(|exp| exp.id.clone())
            .collect();

        Ok(results)
    }
}

/// Main experiment tracker
pub struct ExperimentTracker<B: ExperimentBackend> {
    backend: B,
    current_experiment: Option<ExperimentId>,
}

impl<B: ExperimentBackend> ExperimentTracker<B> {
    /// Create a new tracker backed by the given storage backend
    pub fn new(backend: B) -> Self {
        Self {
            backend,
            current_experiment: None,
        }
    }

    /// Create a new experiment
    pub fn create_experiment(&mut self, name: String) -> ExperimentResult<ExperimentId> {
        let experiment = Experiment::new(name);
        let id = experiment.id.clone();
        self.backend.store_experiment(&experiment)?;
        Ok(id)
    }

    /// Start tracking an experiment
    pub fn start_experiment(&mut self, id: ExperimentId) -> ExperimentResult<()> {
        let mut experiment = self.backend.load_experiment(&id)?;
        experiment.start();
        self.backend.update_experiment(&experiment)?;
        self.current_experiment = Some(id);
        Ok(())
    }

    /// Get the current experiment
    pub fn current_experiment(&self) -> Option<&ExperimentId> {
        self.current_experiment.as_ref()
    }

    /// Set current experiment
    pub fn set_current_experiment(&mut self, id: ExperimentId) {
        self.current_experiment = Some(id);
    }

    /// Log hyperparameter to current experiment
    pub fn log_hyperparameter<T: Into<HyperparameterValue>>(
        &mut self,
        name: String,
        value: T,
    ) -> ExperimentResult<()> {
        if let Some(ref id) = self.current_experiment {
            let mut experiment = self.backend.load_experiment(id)?;
            experiment.log_hyperparameter(name, value);
            self.backend.update_experiment(&experiment)?;
        }
        Ok(())
    }

    /// Log metric to current experiment
    pub fn log_metric(
        &mut self,
        name: String,
        value: f64,
        step: Option<u64>,
    ) -> ExperimentResult<()> {
        if let Some(ref id) = self.current_experiment {
            let mut experiment = self.backend.load_experiment(id)?;
            experiment.log_metric_simple(name, value, step);
            self.backend.update_experiment(&experiment)?;
        }
        Ok(())
    }

    /// Complete current experiment
    pub fn complete_experiment(&mut self) -> ExperimentResult<()> {
        if let Some(ref id) = self.current_experiment.take() {
            let mut experiment = self.backend.load_experiment(id)?;
            experiment.complete();
            self.backend.update_experiment(&experiment)?;
        }
        Ok(())
    }

    /// Fail current experiment
    pub fn fail_experiment(&mut self, error: String) -> ExperimentResult<()> {
        if let Some(ref id) = self.current_experiment.take() {
            let mut experiment = self.backend.load_experiment(id)?;
            experiment.fail(error);
            self.backend.update_experiment(&experiment)?;
        }
        Ok(())
    }

    /// Get experiment by ID
    pub fn get_experiment(&self, id: &ExperimentId) -> ExperimentResult<Experiment> {
        self.backend.load_experiment(id)
    }

    /// Search experiments
    pub fn search_experiments(&self, query: &ExperimentQuery) -> ExperimentResult<Vec<Experiment>> {
        let ids = self.backend.search_experiments(query)?;
        let mut experiments = Vec::new();
        for id in ids {
            experiments.push(self.backend.load_experiment(&id)?);
        }
        Ok(experiments)
    }

    /// Compare experiments by metrics
    pub fn compare_experiments(
        &self,
        exp_ids: &[ExperimentId],
        metric_names: &[String],
    ) -> ExperimentResult<ExperimentComparison> {
        let mut experiments = Vec::new();
        for id in exp_ids {
            experiments.push(self.backend.load_experiment(id)?);
        }

        let mut comparison = ExperimentComparison {
            experiments: experiments.clone(),
            metric_comparison: HashMap::new(),
        };

        for metric_name in metric_names {
            let mut metric_values = Vec::new();
            for exp in &experiments {
                if let Some(values) = exp.metrics.get(metric_name) {
                    let latest_value = values.last().map(|v| v.value);
                    metric_values.push((exp.id.clone(), latest_value));
                } else {
                    metric_values.push((exp.id.clone(), None));
                }
            }
            comparison
                .metric_comparison
                .insert(metric_name.clone(), metric_values);
        }

        Ok(comparison)
    }
}

/// Experiment comparison results
#[derive(Debug, Clone)]
pub struct ExperimentComparison {
    /// Full experiment records included in this comparison
    pub experiments: Vec<Experiment>,
    /// Per-metric list of `(experiment_id, final_value)` pairs for side-by-side comparison
    pub metric_comparison: HashMap<String, Vec<(ExperimentId, Option<f64>)>>,
}

impl ExperimentComparison {
    /// Get the best experiment for a given metric (highest value)
    pub fn best_experiment_for_metric(&self, metric_name: &str) -> Option<&ExperimentId> {
        self.metric_comparison
            .get(metric_name)?
            .iter()
            .filter_map(|(id, value)| value.map(|v| (id, v)))
            .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(id, _)| id)
    }

    /// Get the worst experiment for a given metric (lowest value)
    pub fn worst_experiment_for_metric(&self, metric_name: &str) -> Option<&ExperimentId> {
        self.metric_comparison
            .get(metric_name)?
            .iter()
            .filter_map(|(id, value)| value.map(|v| (id, v)))
            .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(id, _)| id)
    }
}

/// Convenience type for in-memory experiment tracker
pub type InMemoryTracker = ExperimentTracker<InMemoryBackend>;

impl Default for ExperimentTracker<InMemoryBackend> {
    fn default() -> Self {
        Self::new(InMemoryBackend::new())
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_experiment_creation() {
        let experiment = Experiment::new("test_experiment".to_string());
        assert_eq!(experiment.name, "test_experiment");
        assert_eq!(experiment.status, ExperimentStatus::Queued);
        assert!(!experiment.is_finished());
    }

    #[test]
    fn test_experiment_lifecycle() {
        let mut experiment = Experiment::new("test".to_string());

        experiment.start();
        assert_eq!(experiment.status, ExperimentStatus::Running);
        assert!(experiment.started_at.is_some());

        experiment.complete();
        assert_eq!(experiment.status, ExperimentStatus::Completed);
        assert!(experiment.finished_at.is_some());
        assert!(experiment.is_finished());
    }

    #[test]
    fn test_hyperparameter_logging() {
        let mut experiment = Experiment::new("test".to_string());

        experiment.log_hyperparameter("learning_rate".to_string(), 0.01);
        experiment.log_hyperparameter("batch_size".to_string(), 32i64);
        experiment.log_hyperparameter("model_type".to_string(), "MLP".to_string());

        assert_eq!(experiment.hyperparameters.len(), 3);

        match experiment.hyperparameters.get("learning_rate") {
            Some(HyperparameterValue::Float(val)) => assert_eq!(*val, 0.01),
            _ => panic!("Expected float hyperparameter"),
        }
    }

    #[test]
    fn test_metric_logging() {
        let mut experiment = Experiment::new("test".to_string());

        experiment.log_metric_simple("loss".to_string(), 0.5, Some(1));
        experiment.log_metric_simple("loss".to_string(), 0.3, Some(2));
        experiment.log_metric_simple("accuracy".to_string(), 0.85, Some(1));

        assert_eq!(experiment.metrics.len(), 2);
        assert_eq!(
            experiment
                .metrics
                .get("loss")
                .expect("operation should succeed")
                .len(),
            2
        );
        assert_eq!(
            experiment
                .metrics
                .get("accuracy")
                .expect("operation should succeed")
                .len(),
            1
        );
    }

    #[test]
    fn test_in_memory_backend() {
        let mut backend = InMemoryBackend::new();
        let experiment = Experiment::new("test".to_string());
        let id = experiment.id.clone();

        backend
            .store_experiment(&experiment)
            .expect("operation should succeed");
        let loaded = backend
            .load_experiment(&id)
            .expect("operation should succeed");
        assert_eq!(loaded.name, experiment.name);

        let experiments = backend
            .list_experiments()
            .expect("operation should succeed");
        assert_eq!(experiments.len(), 1);
        assert!(experiments.contains(&id));
    }

    #[test]
    fn test_experiment_tracker() {
        let mut tracker = ExperimentTracker::new(InMemoryBackend::new());

        let exp_id = tracker
            .create_experiment("test_exp".to_string())
            .expect("operation should succeed");
        tracker
            .start_experiment(exp_id.clone())
            .expect("operation should succeed");

        tracker
            .log_hyperparameter("lr".to_string(), 0.001)
            .expect("operation should succeed");
        tracker
            .log_metric("loss".to_string(), 0.5, Some(1))
            .expect("operation should succeed");
        tracker
            .log_metric("accuracy".to_string(), 0.9, Some(1))
            .expect("operation should succeed");

        tracker
            .complete_experiment()
            .expect("operation should succeed");

        let experiment = tracker
            .get_experiment(&exp_id)
            .expect("operation should succeed");
        assert_eq!(experiment.status, ExperimentStatus::Completed);
        assert!(experiment.hyperparameters.contains_key("lr"));
        assert!(experiment.metrics.contains_key("loss"));
        assert!(experiment.metrics.contains_key("accuracy"));
    }

    #[test]
    fn test_experiment_search() {
        let mut tracker = ExperimentTracker::new(InMemoryBackend::new());

        let exp1_id = tracker
            .create_experiment("exp1".to_string())
            .expect("operation should succeed");
        let exp2_id = tracker
            .create_experiment("exp2".to_string())
            .expect("operation should succeed");

        // Add tags to distinguish experiments
        let mut exp1 = tracker
            .get_experiment(&exp1_id)
            .expect("operation should succeed");
        exp1.add_tag("type".to_string(), "test".to_string());
        tracker
            .backend
            .update_experiment(&exp1)
            .expect("operation should succeed");

        let mut exp2 = tracker
            .get_experiment(&exp2_id)
            .expect("operation should succeed");
        exp2.add_tag("type".to_string(), "production".to_string());
        tracker
            .backend
            .update_experiment(&exp2)
            .expect("operation should succeed");

        // Search for test experiments
        let query = ExperimentQuery::new().with_tag("type".to_string(), "test".to_string());
        let results = tracker
            .search_experiments(&query)
            .expect("operation should succeed");

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, exp1_id);
    }

    #[test]
    fn test_experiment_comparison() {
        let mut tracker = ExperimentTracker::new(InMemoryBackend::new());

        let exp1_id = tracker
            .create_experiment("exp1".to_string())
            .expect("operation should succeed");
        let exp2_id = tracker
            .create_experiment("exp2".to_string())
            .expect("operation should succeed");

        // Add different metric values
        tracker.set_current_experiment(exp1_id.clone());
        tracker
            .log_metric("accuracy".to_string(), 0.85, None)
            .expect("operation should succeed");

        tracker.set_current_experiment(exp2_id.clone());
        tracker
            .log_metric("accuracy".to_string(), 0.92, None)
            .expect("operation should succeed");

        let comparison = tracker
            .compare_experiments(
                &[exp1_id.clone(), exp2_id.clone()],
                &["accuracy".to_string()],
            )
            .expect("operation should succeed");

        let best_exp = comparison.best_experiment_for_metric("accuracy");
        assert_eq!(best_exp, Some(&exp2_id));

        let worst_exp = comparison.worst_experiment_for_metric("accuracy");
        assert_eq!(worst_exp, Some(&exp1_id));
    }
}