File size: 5,233 Bytes
6ce20d9 |
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 |
#!/usr/bin/env python3
"""
Test script for visualization generation and S3 storage
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from src.visualization.chart_generator import ChartGenerator
def test_visualization_generation():
"""Test the visualization generation functionality"""
print("π§ͺ Testing visualization generation...")
try:
# Create sample economic data
dates = pd.date_range('2020-01-01', periods=50, freq='M')
sample_data = pd.DataFrame({
'GDPC1': np.random.normal(100, 10, 50),
'INDPRO': np.random.normal(50, 5, 50),
'CPIAUCSL': np.random.normal(200, 20, 50),
'FEDFUNDS': np.random.normal(2, 0.5, 50),
'UNRATE': np.random.normal(4, 1, 50)
}, index=dates)
print(f"β
Created sample data with shape: {sample_data.shape}")
# Initialize chart generator
chart_gen = ChartGenerator()
print("β
Initialized ChartGenerator")
# Test individual chart generation
print("\nπ Testing individual chart generation...")
# Time series chart
time_series_key = chart_gen.create_time_series_chart(sample_data)
if time_series_key:
print(f"β
Time series chart created: {time_series_key}")
else:
print("β Time series chart failed")
# Correlation heatmap
correlation_key = chart_gen.create_correlation_heatmap(sample_data)
if correlation_key:
print(f"β
Correlation heatmap created: {correlation_key}")
else:
print("β Correlation heatmap failed")
# Distribution charts
distribution_keys = chart_gen.create_distribution_charts(sample_data)
if distribution_keys:
print(f"β
Distribution charts created: {len(distribution_keys)} charts")
else:
print("β Distribution charts failed")
# PCA visualization
pca_key = chart_gen.create_pca_visualization(sample_data)
if pca_key:
print(f"β
PCA visualization created: {pca_key}")
else:
print("β PCA visualization failed")
# Clustering chart
clustering_key = chart_gen.create_clustering_chart(sample_data)
if clustering_key:
print(f"β
Clustering chart created: {clustering_key}")
else:
print("β Clustering chart failed")
# Test comprehensive visualization generation
print("\nπ― Testing comprehensive visualization generation...")
visualizations = chart_gen.generate_comprehensive_visualizations(sample_data, "comprehensive")
if visualizations:
print(f"β
Generated {len(visualizations)} comprehensive visualizations:")
for chart_type, chart_key in visualizations.items():
print(f" - {chart_type}: {chart_key}")
else:
print("β Comprehensive visualization generation failed")
# Test chart listing
print("\nπ Testing chart listing...")
charts = chart_gen.list_available_charts()
if charts:
print(f"β
Found {len(charts)} charts in S3")
for chart in charts[:3]: # Show first 3
print(f" - {chart['key']} ({chart['size']} bytes)")
else:
print("βΉοΈ No charts found in S3 (this is normal for first run)")
print("\nπ Visualization tests completed successfully!")
return True
except Exception as e:
print(f"β Visualization test failed: {e}")
return False
def test_chart_retrieval():
"""Test retrieving charts from S3"""
print("\nπ Testing chart retrieval...")
try:
chart_gen = ChartGenerator()
charts = chart_gen.list_available_charts()
if charts:
# Test retrieving the first chart
first_chart = charts[0]
print(f"Testing retrieval of: {first_chart['key']}")
response = chart_gen.s3_client.get_object(
Bucket=chart_gen.s3_bucket,
Key=first_chart['key']
)
chart_data = response['Body'].read()
print(f"β
Successfully retrieved chart ({len(chart_data)} bytes)")
return True
else:
print("βΉοΈ No charts available for retrieval test")
return True
except Exception as e:
print(f"β Chart retrieval test failed: {e}")
return False
if __name__ == "__main__":
print("π Starting visualization tests...")
# Test visualization generation
gen_success = test_visualization_generation()
# Test chart retrieval
retrieval_success = test_chart_retrieval()
if gen_success and retrieval_success:
print("\nβ
All visualization tests passed!")
sys.exit(0)
else:
print("\nβ Some visualization tests failed!")
sys.exit(1) |