File size: 4,915 Bytes
7cd7ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Simple test script to verify the Sundew demo functionality
Run this before deploying to Hugging Face to ensure everything works
"""

import numpy as np
import random
import sys

def test_demo_imports():
    """Test that all required modules can be imported"""
    try:
        import numpy
        import plotly.graph_objects as go
        import plotly.express as px
        from plotly.subplots import make_subplots
        import random
        import time
        from typing import List, Tuple, Dict, Optional
        print("βœ… All imports successful")
        return True
    except ImportError as e:
        print(f"❌ Import error: {e}")
        return False

def test_data_generation():
    """Test the data generation function"""
    try:
        # Import the function from app.py
        sys.path.append('.')
        from app import generate_domain_stream, PRODUCTION_PRESETS
        
        # Test each preset
        for preset_name in PRODUCTION_PRESETS.keys():
            samples = generate_domain_stream(preset_name, 10)
            if len(samples) != 10:
                print(f"❌ Data generation failed for {preset_name}: expected 10 samples, got {len(samples)}")
                return False
            
            # Check sample structure
            sample = samples[0]
            required_keys = ['magnitude', 'anomaly', 'context', 'urgency', 'ground_truth']
            if not all(key in sample for key in required_keys):
                print(f"❌ Sample structure invalid for {preset_name}: missing keys")
                return False
        
        print("βœ… Data generation working correctly")
        return True
    except Exception as e:
        print(f"❌ Data generation error: {e}")
        return False

def test_algorithm():
    """Test the core algorithm functionality"""
    try:
        from app import SundewAlgorithmV2, generate_domain_stream
        
        # Create algorithm instance
        algo = SundewAlgorithmV2("auto_tuned")
        
        # Generate test data
        samples = generate_domain_stream("auto_tuned", 20)
        
        # Process samples
        for sample in samples:
            ground_truth = sample.pop('ground_truth')
            result = algo.process_sample(sample, ground_truth)
            
            # Check result structure
            required_keys = ['activated', 'significance', 'threshold', 'energy_level']
            if not all(key in result for key in required_keys):
                print("❌ Algorithm result structure invalid")
                return False
        
        # Check that data was stored
        if len(algo.significances) != 20:
            print(f"❌ Algorithm data storage failed: expected 20 significances, got {len(algo.significances)}")
            return False
        
        print("βœ… Algorithm functionality working correctly")
        return True
    except Exception as e:
        print(f"❌ Algorithm error: {e}")
        return False

def test_visualization():
    """Test the visualization creation"""
    try:
        from app import SundewAlgorithmV2, generate_domain_stream, create_comprehensive_visualization
        
        # Create algorithm and process some data
        algo = SundewAlgorithmV2("custom_breast_probe")
        samples = generate_domain_stream("custom_breast_probe", 15)
        
        for sample in samples:
            ground_truth = sample.pop('ground_truth')
            algo.process_sample(sample, ground_truth)
        
        # Create visualization
        fig = create_comprehensive_visualization(algo, "custom_breast_probe")
        
        if fig is None:
            print("❌ Visualization creation failed: returned None")
            return False
        
        print("βœ… Visualization creation working correctly")
        return True
    except Exception as e:
        print(f"❌ Visualization error: {e}")
        return False

def main():
    """Run all tests"""
    print("πŸ§ͺ Testing Sundew Demo Functionality\n")
    
    tests = [
        ("Module Imports", test_demo_imports),
        ("Data Generation", test_data_generation),
        ("Algorithm Processing", test_algorithm),
        ("Visualization Creation", test_visualization)
    ]
    
    all_passed = True
    for test_name, test_func in tests:
        print(f"Testing {test_name}...")
        if not test_func():
            all_passed = False
        print()
    
    if all_passed:
        print("πŸŽ‰ All tests passed! Demo is ready for deployment.")
        print("\nTo run the demo locally:")
        print("1. Install requirements: pip install -r requirements.txt")
        print("2. Run demo: python app.py")
        print("3. Open browser to displayed URL")
    else:
        print("❌ Some tests failed. Please fix issues before deployment.")
        return 1
    
    return 0

if __name__ == "__main__":
    exit(main())