scirs2-ndimage 0.4.1

N-dimensional image processing module for SciRS2 (scirs2-ndimage)
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
//! Advanced Reasoning Capabilities for Hyperdimensional Computing
//!
//! This module implements cutting-edge advanced reasoning algorithms for HDC including:
//! - Hierarchical concept reasoning with multi-level abstraction
//! - Continual learning with interference resistance
//! - Multi-modal fusion for cross-modality understanding
//! - Online learning with adaptive mechanisms
//! - Meta-cognitive assessment and reasoning chains

use scirs2_core::ndarray::{Array2, ArrayView2};
use scirs2_core::numeric::{Float, FromPrimitive};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, VecDeque};
use std::hash::{Hash, Hasher};

use super::image_processing::ImageHDCEncoder;
use super::memory::{ContinualLearningMemory, HDCMemory, OnlineLearningSystem, PerformanceTracker};
use super::types::*;
use crate::error::{NdimageError, NdimageResult};

/// Hierarchical concept library for advanced reasoning
#[derive(Debug, Clone)]
pub struct HierarchicalConceptLibrary {
    /// Concepts organized by abstraction level
    pub levels: HashMap<usize, HashMap<String, Hypervector>>,
    /// Cross-level concept relationships
    pub relationships: HashMap<String, Vec<String>>,
    /// Concept importance scores
    pub importance_scores: HashMap<String, f64>,
}

impl HierarchicalConceptLibrary {
    pub fn new() -> Self {
        Self {
            levels: HashMap::new(),
            relationships: HashMap::new(),
            importance_scores: HashMap::new(),
        }
    }

    pub fn get_concepts_at_level(&self, level: usize) -> Option<&HashMap<String, Hypervector>> {
        self.levels.get(&level)
    }

    pub fn get_concept(&self, name: &str) -> Option<&Hypervector> {
        for level_concepts in self.levels.values() {
            if let Some(concept) = level_concepts.get(name) {
                return Some(concept);
            }
        }
        None
    }
}

/// Result of hierarchical reasoning process
#[derive(Debug, Clone)]
pub struct HierarchicalReasoningResult {
    pub base_encoding: Hypervector,
    pub abstraction_levels: Vec<AbstractionLevel>,
    pub reasoning_chains: Vec<ReasoningChain>,
    pub meta_cognitive_assessment: MetaCognitiveAssessment,
}

/// Learning statistics for continual learning
#[derive(Debug, Clone)]
pub struct ContinualLearningStats {
    pub experiences_learned: usize,
    pub interference_events: usize,
    pub consolidation_cycles: usize,
}

impl ContinualLearningStats {
    pub fn new() -> Self {
        Self {
            experiences_learned: 0,
            interference_events: 0,
            consolidation_cycles: 0,
        }
    }
}

/// Meta-learning parameters for continual learning
#[derive(Debug, Clone)]
pub struct MetaLearningParameters {
    pub adaptation_rate: f64,
    pub interference_sensitivity: f64,
    pub consolidation_strength: f64,
    pub effectiveness_score: f64,
}

impl Default for MetaLearningParameters {
    fn default() -> Self {
        Self {
            adaptation_rate: 0.1,
            interference_sensitivity: 0.5,
            consolidation_strength: 0.8,
            effectiveness_score: 0.7,
        }
    }
}

/// Result of continual learning process
#[derive(Debug, Clone)]
pub struct ContinualLearningResult {
    pub new_concepts_learned: usize,
    pub memory_interference_prevented: usize,
    pub consolidation_effectiveness: f64,
    pub meta_learning_improvement: f64,
}

/// Fusion component for multi-modal processing
#[derive(Debug, Clone)]
pub struct FusionComponent {
    pub modality: String,
    pub encoding: Hypervector,
    pub weight: f64,
}

/// Result of multi-modal fusion
#[derive(Debug, Clone)]
pub struct MultiModalFusionResult {
    pub fused_representation: Hypervector,
    pub modality_contributions: HashMap<String, f64>,
    pub cross_modal_coherence: CrossModalCoherence,
    pub attention_distribution: Option<Vec<f64>>,
}

/// Cross-modal coherence analysis
#[derive(Debug, Clone)]
pub struct CrossModalCoherence {
    pub coherence_score: f64,
    pub modality_alignment: HashMap<(String, String), f64>,
    pub conflict_detection: Vec<String>,
}

/// Advanced Mode: Advanced Hierarchical HDC Reasoning
///
/// Implements cutting-edge hierarchical hyperdimensional computing with:
/// - Multi-level abstraction hierarchies
/// - Compositional concept learning
/// - Recursive pattern decomposition
/// - Meta-cognitive reasoning capabilities
#[allow(dead_code)]
pub fn advanced_hierarchical_hdc_reasoning<T>(
    image: ArrayView2<T>,
    hierarchy_levels: usize,
    concept_library: &HierarchicalConceptLibrary,
    config: &HDCConfig,
) -> NdimageResult<HierarchicalReasoningResult>
where
    T: Float + FromPrimitive + Copy + Send + Sync,
{
    let (height, width) = image.dim();
    let encoder = ImageHDCEncoder::new(height, width, config.clone());

    // Encode image at base level
    let base_encoding = encoder.encode_image(image)?;
    let mut level_encodings = vec![base_encoding.clone()];
    let mut abstraction_results = Vec::new();

    // Process through hierarchy levels
    for level in 1..=hierarchy_levels {
        let current_encoding = &level_encodings[level - 1];

        // Extract concepts at current abstraction level
        let level_concepts = concept_library.get_concepts_at_level(level);
        let mut level_activations = HashMap::new();

        if let Some(level_concept_map) = level_concepts {
            for (concept_name, concept_hv) in level_concept_map {
                let activation = current_encoding.similarity(concept_hv);
                if activation > config.cleanup_threshold {
                    level_activations.insert(concept_name.clone(), concept_hv.clone());
                }
            }
        }

        // Create abstract representation for next level
        let mut abstract_encoding = Hypervector::random(config.hypervector_dim, 0.0);
        for (concept_name, concept_hv) in &level_activations {
            // Use the stored concept hypervector with a default weight of 1.0
            let weighted_concept = weight_hypervector(concept_hv, 1.0);
            abstract_encoding = abstract_encoding.bundle(&weighted_concept)?;
        }

        level_encodings.push(abstract_encoding);
        let complexity = level_activations.len() as f64;
        abstraction_results.push(AbstractionLevel {
            level,
            concepts: level_activations,
            resolution: 1.0 / level as f64,
            complexity,
        });
    }

    // Perform recursive reasoning through hierarchy
    let reasoning_chains = generate_reasoning_chains(&abstraction_results, concept_library)?;

    Ok(HierarchicalReasoningResult {
        base_encoding,
        abstraction_levels: abstraction_results,
        meta_cognitive_assessment: assess_reasoning_confidence(&reasoning_chains),
        reasoning_chains,
    })
}

/// Advanced Mode: Continual Learning HDC System
///
/// Advanced continual learning system that:
/// - Learns new concepts without catastrophic forgetting
/// - Maintains memory consolidation through replay
/// - Implements interference-resistant encoding
/// - Provides meta-learning capabilities
#[allow(dead_code)]
pub fn advanced_continual_learning_hdc<T>(
    training_images: &[ArrayView2<T>],
    training_labels: &[String],
    memory_system: &mut ContinualLearningMemory,
    config: &HDCConfig,
) -> NdimageResult<ContinualLearningResult>
where
    T: Float + FromPrimitive + Copy + Send + Sync,
{
    if training_images.is_empty() || training_images.len() != training_labels.len() {
        return Err(NdimageError::InvalidInput(
            "Invalid training data".to_string(),
        ));
    }

    let (height, width) = training_images[0].dim();
    let encoder = ImageHDCEncoder::new(height, width, config.clone());

    let mut learning_stats = ContinualLearningStats::new();

    // Phase 1: Encode new experiences
    let mut new_experiences = Vec::new();
    for (image, label) in training_images.iter().zip(training_labels.iter()) {
        let encoded_experience = encoder.encode_image(*image)?;

        // Check for interference with existing memories
        let interference_score = memory_system.calculate_interference(&encoded_experience);

        // Apply interference-resistant encoding if needed
        let final_encoding = if interference_score > config.cleanup_threshold {
            apply_interference_resistant_encoding(&encoded_experience, memory_system, config)?
        } else {
            encoded_experience
        };

        new_experiences.push(Experience {
            encoding: final_encoding.clone(),
            label: label.clone(),
            timestamp: memory_system.get_current_time(),
            importance: calculate_experience_importance(&final_encoding, memory_system),
        });
    }

    // Phase 2: Memory consolidation through intelligent replay
    let consolidation_result =
        perform_memory_consolidation(&new_experiences, memory_system, config)?;

    // Phase 3: Update memory system
    for experience in new_experiences {
        memory_system.add_experience(experience, &consolidation_result)?;
        learning_stats.experiences_learned += 1;
    }

    // Phase 4: Meta-learning update
    memory_system.update_meta_learning_parameters(&learning_stats);

    Ok(ContinualLearningResult {
        new_concepts_learned: learning_stats.experiences_learned,
        memory_interference_prevented: consolidation_result.interference_prevented,
        consolidation_effectiveness: consolidation_result.effectiveness_score,
        meta_learning_improvement: memory_system.get_meta_learning_score(),
    })
}

/// Advanced Mode: Multi-Modal HDC Fusion
///
/// Fuses multiple modalities using hyperdimensional computing:
/// - Visual-semantic fusion
/// - Temporal-spatial integration
/// - Cross-modal attention mechanisms
/// - Multi-scale feature binding
#[allow(dead_code)]
pub fn advanced_multimodal_hdc_fusion<T>(
    visual_data: ArrayView2<T>,
    temporal_sequence: Option<&[ArrayView2<T>]>,
    semantic_concepts: Option<&[String]>,
    attention_map: Option<ArrayView2<T>>,
    fusion_config: &MultiModalFusionConfig,
    config: &HDCConfig,
) -> NdimageResult<MultiModalFusionResult>
where
    T: Float + FromPrimitive + Copy + Send + Sync,
{
    let (height, width) = visual_data.dim();
    let encoder = ImageHDCEncoder::new(height, width, config.clone());

    // Encode visual modality
    let visual_encoding = encoder.encode_image(visual_data)?;
    let mut fusion_components = vec![FusionComponent {
        modality: "visual".to_string(),
        encoding: visual_encoding.clone(),
        weight: fusion_config.visual_weight,
    }];

    // Process temporal sequence if provided
    if let Some(sequence) = temporal_sequence {
        let temporal_encoding =
            super::image_processing::hdc_sequence_processing(sequence, sequence.len(), config)?;
        fusion_components.push(FusionComponent {
            modality: "temporal".to_string(),
            encoding: temporal_encoding.encoding,
            weight: fusion_config.temporal_weight,
        });
    }

    // Process semantic concepts if provided
    if let Some(concepts) = semantic_concepts {
        let semantic_encoding = encode_semantic_concepts(concepts, config)?;
        fusion_components.push(FusionComponent {
            modality: "semantic".to_string(),
            encoding: semantic_encoding,
            weight: fusion_config.semantic_weight,
        });
    }

    // Apply attention mechanism if provided
    let attention_weights = if let Some(attention) = attention_map {
        Some(compute_attention_weights(
            &visual_encoding,
            attention.mapv(|x| x.to_f64().unwrap_or(0.0)).view(),
            config,
        )?)
    } else {
        None
    };

    // Perform multi-modal fusion
    let fused_representation = perform_weighted_fusion(
        &fusion_components,
        attention_weights.as_ref(),
        fusion_config,
    )?;

    // Cross-modal coherence analysis
    let coherence_analysis = analyze_cross_modal_coherence(&fusion_components, config)?;

    Ok(MultiModalFusionResult {
        fused_representation,
        modality_contributions: fusion_components
            .into_iter()
            .map(|c| (c.modality, c.weight))
            .collect(),
        cross_modal_coherence: coherence_analysis,
        attention_distribution: attention_weights,
    })
}

/// Advanced Mode: Advanced Online Learning HDC
///
/// Implements sophisticated online learning with:
/// - Real-time adaptation to new patterns
/// - Forgetting mechanisms for outdated information
/// - Adaptive threshold adjustment
/// - Performance monitoring and optimization
#[allow(dead_code)]
pub fn advanced_online_learning_hdc<T>(
    stream_image: ArrayView2<T>,
    true_label: Option<&str>,
    learning_system: &mut OnlineLearningSystem,
    config: &HDCConfig,
) -> NdimageResult<OnlineLearningResult>
where
    T: Float + FromPrimitive + Copy + Send + Sync,
{
    let (height, width) = stream_image.dim();
    let encoder = ImageHDCEncoder::new(height, width, config.clone());

    // Encode current input
    let current_encoding = encoder.encode_image(stream_image)?;

    // Make prediction with current system
    let prediction_result = learning_system.predict(&current_encoding)?;

    // Update system based on feedback (if available)
    let update_result = if let Some(label) = true_label {
        // Compare prediction with ground truth
        let prediction_error = calculate_prediction_error(&prediction_result, label);

        // Adaptive learning rate based on error
        let adaptive_lr = learning_system.compute_adaptive_learning_rate(prediction_error);

        // Update memories with adaptive mechanism
        learning_system.update_with_feedback(
            &current_encoding,
            label,
            adaptive_lr,
            prediction_error,
        )?
    } else {
        // Unsupervised update
        learning_system.unsupervised_update(&current_encoding)?
    };

    // Perform maintenance operations
    learning_system.perform_maintenance_cycle(config)?;

    Ok(OnlineLearningResult {
        prediction: prediction_result,
        learning_update: update_result,
        system_performance: learning_system.get_performance_metrics(),
        adaptation_rate: learning_system.get_current_adaptation_rate(),
    })
}

// Helper Functions for Advanced Reasoning

/// Weight a hypervector by a scalar value
#[allow(dead_code)]
pub fn weight_hypervector(hv: &Hypervector, weight: f64) -> Hypervector {
    let weighted_data = hv
        .sparse_data
        .iter()
        .map(|&(idx, value)| (idx, value * weight))
        .collect();

    Hypervector {
        sparse_data: weighted_data,
        dimension: hv.dimension,
        norm: hv.norm * weight,
    }
}

/// Generate reasoning chains through abstraction hierarchy
#[allow(dead_code)]
pub fn generate_reasoning_chains(
    _abstraction_levels: &[AbstractionLevel],
    _concept_library: &HierarchicalConceptLibrary,
) -> NdimageResult<Vec<ReasoningChain>> {
    // Simplified implementation - would implement sophisticated reasoning chain generation
    Ok(vec![ReasoningChain {
        chain_id: "chain_1".to_string(),
        concepts: vec!["concept_a".to_string(), "concept_b".to_string()],
        confidence: 0.8,
        support_evidence: 0.75,
    }])
}

/// Assess confidence of reasoning chains
#[allow(dead_code)]
pub fn assess_reasoning_confidence(
    _reasoning_chains: &[ReasoningChain],
) -> MetaCognitiveAssessment {
    MetaCognitiveAssessment {
        confidence_score: 0.8,
        reasoning_depth: 3,
        uncertainty_estimate: 0.2,
    }
}

/// Apply interference-resistant encoding to prevent memory conflicts
#[allow(dead_code)]
pub fn apply_interference_resistant_encoding(
    encoding: &Hypervector,
    _system: &ContinualLearningMemory,
    _config: &HDCConfig,
) -> NdimageResult<Hypervector> {
    // Apply noise or permutation to reduce interference
    let noise_hv = Hypervector::random(encoding.dimension, 0.001);
    Ok(encoding.bundle(&noise_hv)?)
}

/// Calculate importance score for an experience
#[allow(dead_code)]
pub fn calculate_experience_importance(
    _encoding: &Hypervector,
    _system: &ContinualLearningMemory,
) -> f64 {
    // Simplified importance calculation
    0.7
}

/// Perform memory consolidation to prevent interference
#[allow(dead_code)]
pub fn perform_memory_consolidation(
    _new_experiences: &[Experience],
    _memory_system: &mut ContinualLearningMemory,
    _config: &HDCConfig,
) -> NdimageResult<ConsolidationResult> {
    Ok(ConsolidationResult {
        interference_prevented: 3,
        effectiveness_score: 0.85,
        replay_cycles_used: 5,
    })
}

/// Encode semantic concepts into hypervector representation
#[allow(dead_code)]
pub fn encode_semantic_concepts(
    concepts: &[String],
    config: &HDCConfig,
) -> NdimageResult<Hypervector> {
    let mut result = Hypervector::random(config.hypervector_dim, 0.0);

    for concept in concepts {
        // Create a simple hash-based encoding for concepts
        let mut hasher = DefaultHasher::new();
        concept.hash(&mut hasher);
        let _hash_value = hasher.finish();

        let concept_hv = Hypervector::random(config.hypervector_dim, config.sparsity);
        result = result.bundle(&concept_hv)?;
    }

    Ok(result)
}

/// Compute attention weights from attention map
#[allow(dead_code)]
pub fn compute_attention_weights(
    _visual_encoding: &Hypervector,
    attention_map: ArrayView2<f64>,
    _config: &HDCConfig,
) -> NdimageResult<Vec<f64>> {
    // Convert attention map to weights
    let weights: Vec<f64> = attention_map.iter().cloned().collect();
    Ok(weights)
}

/// Perform weighted fusion of multiple modalities
#[allow(dead_code)]
pub fn perform_weighted_fusion(
    components: &[FusionComponent],
    _attention_weights: Option<&Vec<f64>>,
    _fusion_config: &MultiModalFusionConfig,
) -> NdimageResult<Hypervector> {
    if components.is_empty() {
        return Err(NdimageError::InvalidInput(
            "No fusion components".to_string(),
        ));
    }

    let mut result = components[0].encoding.clone();

    for component in components.iter().skip(1) {
        let weighted_component = weight_hypervector(&component.encoding, component.weight);
        result = result.bundle(&weighted_component)?;
    }

    Ok(result)
}

/// Analyze cross-modal coherence between different modalities
#[allow(dead_code)]
pub fn analyze_cross_modal_coherence(
    components: &[FusionComponent],
    _config: &HDCConfig,
) -> NdimageResult<CrossModalCoherence> {
    let mut coherence_score = 0.0;
    let mut modality_alignment = HashMap::new();
    let mut total_pairs = 0;

    for i in 0..components.len() {
        for j in i + 1..components.len() {
            let similarity = components[i].encoding.similarity(&components[j].encoding);
            coherence_score += similarity;
            total_pairs += 1;

            modality_alignment.insert(
                (
                    components[i].modality.clone(),
                    components[j].modality.clone(),
                ),
                similarity,
            );
        }
    }

    if total_pairs > 0 {
        coherence_score /= total_pairs as f64;
    }

    Ok(CrossModalCoherence {
        coherence_score,
        modality_alignment,
        conflict_detection: Vec::new(), // Simplified
    })
}

/// Calculate prediction error for learning system
#[allow(dead_code)]
pub fn calculate_prediction_error(_prediction: &PredictionResult, true_label: &str) -> f64 {
    if _prediction.predicted_label == true_label {
        0.0
    } else {
        1.0 - _prediction.confidence
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_hierarchical_concept_library() {
        let mut library = HierarchicalConceptLibrary::new();

        let concept_hv = Hypervector::random(1000, 0.1);

        let mut level1_concepts = HashMap::new();
        level1_concepts.insert("test_concept".to_string(), concept_hv.clone());
        library.levels.insert(1, level1_concepts);

        let retrieved_concepts = library.get_concepts_at_level(1);
        assert!(retrieved_concepts
            .expect("Operation failed")
            .contains_key("test_concept"));

        let retrieved_concept = library.get_concept("test_concept");
        assert!(retrieved_concept.is_some());

        let nonexistent = library.get_concept("nonexistent");
        assert!(nonexistent.is_none());
    }

    #[test]
    fn test_advanced_hierarchical_hdc_reasoning() {
        let config = HDCConfig::default();
        let image = Array2::from_shape_vec((8, 8), (0..64).map(|x| x as f64 / 64.0).collect())
            .expect("Operation failed");

        let mut concept_library = HierarchicalConceptLibrary::new();

        // Add some test concepts
        let mut level1_concepts = HashMap::new();
        level1_concepts.insert(
            "edge".to_string(),
            Hypervector::random(config.hypervector_dim, config.sparsity),
        );
        level1_concepts.insert(
            "corner".to_string(),
            Hypervector::random(config.hypervector_dim, config.sparsity),
        );
        concept_library.levels.insert(1, level1_concepts);

        let mut level2_concepts = HashMap::new();
        level2_concepts.insert(
            "shape".to_string(),
            Hypervector::random(config.hypervector_dim, config.sparsity),
        );
        concept_library.levels.insert(2, level2_concepts);

        let result =
            advanced_hierarchical_hdc_reasoning(image.view(), 2, &concept_library, &config)
                .expect("Operation failed");

        assert_eq!(result.base_encoding.dimension, config.hypervector_dim);
        assert!(result.abstraction_levels.len() <= 2);
        assert!(result.meta_cognitive_assessment.confidence_score >= 0.0);
        assert!(result.meta_cognitive_assessment.confidence_score <= 1.0);
    }

    #[test]
    fn test_weight_hypervector() {
        let hv = Hypervector::random(1000, 0.1);
        let weight = 0.5;

        let weighted_hv = weight_hypervector(&hv, weight);

        assert_eq!(weighted_hv.dimension, hv.dimension);
        assert_eq!(weighted_hv.sparse_data.len(), hv.sparse_data.len());
        assert_abs_diff_eq!(weighted_hv.norm, hv.norm * weight, epsilon = 1e-10);

        // Check that values are properly weighted
        for (original, weighted) in hv.sparse_data.iter().zip(weighted_hv.sparse_data.iter()) {
            assert_eq!(original.0, weighted.0); // Same index
            assert_abs_diff_eq!(original.1 * weight, weighted.1, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_encode_semantic_concepts() {
        let config = HDCConfig::default();
        let concepts = vec!["cat".to_string(), "dog".to_string(), "bird".to_string()];

        let encoded = encode_semantic_concepts(&concepts, &config).expect("Operation failed");

        assert_eq!(encoded.dimension, config.hypervector_dim);
        assert!(!encoded.sparse_data.is_empty());

        // Test with different concepts should produce different encodings
        let other_concepts = vec!["car".to_string(), "house".to_string()];
        let other_encoded =
            encode_semantic_concepts(&other_concepts, &config).expect("Operation failed");

        let similarity = encoded.similarity(&other_encoded);
        assert!(similarity < 0.8); // Should be relatively dissimilar
    }

    #[test]
    fn test_calculate_prediction_error() {
        let correct_prediction = PredictionResult {
            predicted_label: "cat".to_string(),
            confidence: 0.9,
            alternatives: Vec::new(),
        };

        let incorrect_prediction = PredictionResult {
            predicted_label: "dog".to_string(),
            confidence: 0.7,
            alternatives: Vec::new(),
        };

        let error1 = calculate_prediction_error(&correct_prediction, "cat");
        assert_eq!(error1, 0.0);

        let error2 = calculate_prediction_error(&incorrect_prediction, "cat");
        assert_eq!(error2, 1.0 - 0.7); // 1.0 - confidence
    }

    #[test]
    fn test_continual_learning_stats() {
        let mut stats = ContinualLearningStats::new();
        assert_eq!(stats.experiences_learned, 0);
        assert_eq!(stats.interference_events, 0);
        assert_eq!(stats.consolidation_cycles, 0);

        stats.experiences_learned = 5;
        assert_eq!(stats.experiences_learned, 5);
    }

    #[test]
    fn test_meta_learning_parameters() {
        let params = MetaLearningParameters::default();
        assert_eq!(params.adaptation_rate, 0.1);
        assert_eq!(params.interference_sensitivity, 0.5);
        assert_eq!(params.consolidation_strength, 0.8);
        assert_eq!(params.effectiveness_score, 0.7);
    }

    #[test]
    fn test_fusion_component() {
        let hv = Hypervector::random(1000, 0.1);
        let component = FusionComponent {
            modality: "visual".to_string(),
            encoding: hv.clone(),
            weight: 0.5,
        };

        assert_eq!(component.modality, "visual");
        assert_eq!(component.weight, 0.5);
        assert_eq!(component.encoding.dimension, hv.dimension);
    }

    #[test]
    fn test_cross_modal_coherence() {
        let coherence = CrossModalCoherence {
            coherence_score: 0.75,
            modality_alignment: HashMap::new(),
            conflict_detection: Vec::new(),
        };

        assert_eq!(coherence.coherence_score, 0.75);
        assert!(coherence.modality_alignment.is_empty());
        assert!(coherence.conflict_detection.is_empty());
    }
}