""" Quick validation tests for app.py improvements Tests the new functions without launching the full Gradio app """ import numpy as np import pandas as pd # Import test - check if app.py can be imported try: import sys sys.path.insert(0, '/Users/dennissinden/GradioApp/TempoPFN') print("✓ Python path configured") except Exception as e: print(f"✗ Path configuration failed: {e}") sys.exit(1) # Test metrics calculation logic (standalone) def test_metrics_calculation(): """Test the metrics calculation with sample data""" print("\n=== Testing Metrics Calculation ===") # Create sample data np.random.seed(42) history = np.random.randn(100, 1) * 10 + 50 predictions = np.random.randn(20, 1) * 10 + 50 future = np.random.randn(20, 1) * 10 + 50 # Simulate the calculate_metrics function logic try: from scipy import stats as scipy_stats metrics = {} metrics['data_mean'] = float(np.mean(history)) metrics['data_std'] = float(np.std(history)) metrics['latest_price'] = float(history[-1, 0]) metrics['forecast_next'] = float(predictions[0, 0]) print(f"✓ Mean: {metrics['data_mean']:.2f}") print(f"✓ Std: {metrics['data_std']:.2f}") print(f"✓ Latest: {metrics['latest_price']:.2f}") print(f"✓ Forecast: {metrics['forecast_next']:.2f}") return True except Exception as e: print(f"✗ Metrics calculation failed: {e}") return False # Test export functionality logic def test_export_logic(): """Test export CSV logic""" print("\n=== Testing Export Logic ===") try: # Simulate forecast results forecast_results = { 'history': np.random.randn(100, 1), 'predictions': np.random.randn(20, 1), 'future': np.random.randn(20, 1) } history = forecast_results['history'].flatten() predictions = forecast_results['predictions'].flatten() future = forecast_results['future'].flatten() max_len = max(len(history), len(predictions)) df_data = { 'Time_Index': list(range(max_len)), 'Historical_Value': list(history) + [np.nan] * (max_len - len(history)), 'Predicted_Value': [np.nan] * len(history) + list(predictions[:max_len - len(history)]), 'True_Future_Value': [np.nan] * len(history) + list(future[:max_len - len(history)]) } df = pd.DataFrame(df_data) print(f"✓ DataFrame created with {len(df)} rows") print(f"✓ Columns: {list(df.columns)}") return True except Exception as e: print(f"✗ Export logic failed: {e}") return False # Test visualization logic def test_visualization_logic(): """Test advanced visualization creation logic""" print("\n=== Testing Visualization Logic ===") try: from plotly.subplots import make_subplots import plotly.graph_objects as go # Create sample subplots fig = make_subplots( rows=2, cols=2, subplot_titles=('Test 1', 'Test 2', 'Test 3', 'Test 4') ) # Add sample data x = np.arange(10) y = np.random.randn(10) fig.add_trace(go.Scatter(x=x, y=y, name='Test'), row=1, col=1) print("✓ Plotly subplots created successfully") print("✓ Trace added successfully") return True except Exception as e: print(f"✗ Visualization logic failed: {e}") return False # Test syntax and imports def test_app_syntax(): """Test if app.py has valid syntax""" print("\n=== Testing App Syntax ===") try: import py_compile py_compile.compile('app.py', doraise=True) print("✓ app.py syntax is valid") return True except py_compile.PyCompileError as e: print(f"✗ Syntax error in app.py: {e}") return False def main(): print("=" * 50) print("APP IMPROVEMENTS VALIDATION TEST") print("=" * 50) results = [] results.append(("Metrics Calculation", test_metrics_calculation())) results.append(("Export Logic", test_export_logic())) results.append(("Visualization Logic", test_visualization_logic())) results.append(("App Syntax", test_app_syntax())) print("\n" + "=" * 50) print("TEST SUMMARY") print("=" * 50) for name, passed in results: status = "PASS" if passed else "FAIL" symbol = "✓" if passed else "✗" print(f"{symbol} {name}: {status}") all_passed = all(result[1] for result in results) print("\n" + "=" * 50) if all_passed: print("✓ ALL TESTS PASSED") else: print("✗ SOME TESTS FAILED") print("=" * 50) return all_passed if __name__ == "__main__": import sys success = main() sys.exit(0 if success else 1)