← back to Watches

analytics/run_all_analytics.py

286 lines

#!/usr/bin/env python3
"""
Master Analytics Runner
Executes all analytics modules in sequence
"""

import sys
import time
from datetime import datetime

# Import all analytics modules
sys.path.insert(0, '/root/Projects/watches/analytics')

from price_prediction import WatchPricePredictor
from market_analysis import WatchMarketAnalyzer
from statistical_insights import StatisticalAnalyzer
from report_generator import AnalyticsReportGenerator
from utils import save_json, convert_numpy_types


def run_all_analytics():
    """Execute complete analytics pipeline"""
    start_time = time.time()

    print("\n" + "=" * 80)
    print(" " * 20 + "OMEGA WATCH ANALYTICS SUITE")
    print(" " * 15 + "Comprehensive Data Science Analysis")
    print("=" * 80)
    print(f"\nStart Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")

    results = {}
    data_path = '/root/Projects/watches/data/watches.json'

    # =========================================================================
    # MODULE 1: PRICE PREDICTION
    # =========================================================================
    print("\n" + "=" * 80)
    print("MODULE 1: PRICE PREDICTION & FORECASTING")
    print("=" * 80)
    module_start = time.time()

    try:
        predictor = WatchPricePredictor(data_path)
        predictor.load_data()

        print("\n  [1/7] Calculating appreciation metrics...")
        metrics_df = predictor.calculate_appreciation_metrics()

        print("  [2/7] Building prediction models...")
        model_scores = predictor.build_prediction_models()
        best_model = model_scores['best_model']
        best_r2 = model_scores[f"{best_model}_r2"]
        print(f"        Best model: {best_model} (R2: {best_r2})")

        print("  [3/7] Generating future predictions...")
        all_predictions = []
        for watch in predictor.watches:
            pred = predictor.predict_future_prices(watch['id'])
            if pred:
                all_predictions.append(pred)

        print("  [4/7] Detecting price anomalies...")
        anomalies = predictor.analyze_price_anomalies()

        print("  [5/7] Calculating correlations...")
        correlations = predictor.calculate_correlation_matrix()

        print("  [6/7] Generating investment insights...")
        insights = predictor.generate_investment_insights()

        print("  [7/7] Saving results...")
        output = {
            'generated_at': datetime.now().isoformat(),
            'summary': {
                'total_watches': len(predictor.watches),
                'total_price_points': len(predictor.price_df),
                'watches_with_predictions': len(all_predictions),
                'anomalies_detected': len(anomalies)
            },
            'model_performance': model_scores,
            'appreciation_metrics': metrics_df.to_dict('records'),
            'future_predictions': all_predictions,
            'price_anomalies': anomalies,
            'correlations': correlations,
            'investment_insights': insights
        }

        # Convert numpy types before saving
        output = convert_numpy_types(output)
        save_json(output, '/root/Projects/watches/analytics/price_predictions.json')

        results['predictions'] = output
        print(f"\n  ✓ Complete! ({time.time() - module_start:.2f}s)")
        print(f"    - {len(all_predictions)} watches with predictions")
        print(f"    - {len(anomalies)} price anomalies detected")

    except Exception as e:
        print(f"\n  ✗ Error in price prediction module: {e}")
        import traceback
        traceback.print_exc()

    # =========================================================================
    # MODULE 2: MARKET ANALYSIS
    # =========================================================================
    print("\n" + "=" * 80)
    print("MODULE 2: MARKET ANALYSIS & CLUSTERING")
    print("=" * 80)
    module_start = time.time()

    try:
        analyzer = WatchMarketAnalyzer(data_path)
        analyzer.load_data()

        print("\n  [1/8] Performing k-means clustering...")
        clustering = analyzer.perform_clustering(n_clusters=4)

        print("  [2/8] Identifying value watches...")
        value_watches = analyzer.identify_value_watches()

        print("  [3/8] Analyzing by collection...")
        collection_analysis = analyzer.analyze_by_collection()

        print("  [4/8] Analyzing by decade...")
        decade_analysis = analyzer.analyze_by_decade()

        print("  [5/8] Calculating market volatility...")
        volatility = analyzer.calculate_market_volatility()

        print("  [6/8] Identifying investment opportunities...")
        opportunities = analyzer.identify_investment_opportunities()

        print("  [7/8] Generating heat map data...")
        heatmap_data = analyzer.generate_heat_map_data()

        print("  [8/8] Saving results...")
        output = {
            'generated_at': datetime.now().isoformat(),
            'summary': {
                'total_watches_analyzed': len(analyzer.watch_df),
                'collections': analyzer.watch_df['series'].nunique(),
                'avg_market_cagr': round(analyzer.watch_df['cagr'].mean(), 2),
                'median_market_price': round(analyzer.watch_df['current_price'].median(), 2)
            },
            'clustering': clustering,
            'value_watches': value_watches[:10],
            'collection_analysis': collection_analysis,
            'decade_analysis': decade_analysis,
            'volatility_analysis': volatility,
            'investment_opportunities': opportunities[:15],
            'visualization_data': heatmap_data
        }

        # Convert numpy types before saving
        output = convert_numpy_types(output)
        save_json(output, '/root/Projects/watches/analytics/market_analysis.json')

        results['market'] = output
        print(f"\n  ✓ Complete! ({time.time() - module_start:.2f}s)")
        print(f"    - {len(clustering['cluster_profiles'])} clusters created")
        print(f"    - {len(opportunities)} investment opportunities identified")

    except Exception as e:
        print(f"\n  ✗ Error in market analysis module: {e}")
        import traceback
        traceback.print_exc()

    # =========================================================================
    # MODULE 3: STATISTICAL INSIGHTS
    # =========================================================================
    print("\n" + "=" * 80)
    print("MODULE 3: STATISTICAL INSIGHTS & RISK ANALYSIS")
    print("=" * 80)
    module_start = time.time()

    try:
        stat_analyzer = StatisticalAnalyzer(data_path)
        stat_analyzer.load_data()

        print("\n  [1/8] Calculating moving averages...")
        ma_results = stat_analyzer.calculate_moving_averages()

        print("  [2/8] Analyzing best/worst periods...")
        period_analysis = stat_analyzer.analyze_best_worst_periods()

        print("  [3/8] Computing advanced statistics...")
        advanced_stats = stat_analyzer.calculate_advanced_statistics()

        print("  [4/8] Calculating percentile rankings...")
        rankings = stat_analyzer.calculate_percentile_rankings()

        print("  [5/8] Performing hypothesis tests...")
        hypothesis_tests = stat_analyzer.hypothesis_testing()

        print("  [6/8] Calculating risk metrics...")
        risk_metrics = stat_analyzer.calculate_risk_metrics()

        print("  [7/8] Generating market summary...")
        summary = stat_analyzer.generate_summary_statistics()

        print("  [8/8] Saving results...")
        output = {
            'generated_at': datetime.now().isoformat(),
            'summary_statistics': summary,
            'moving_averages': ma_results,
            'period_analysis': period_analysis,
            'advanced_statistics': advanced_stats,
            'percentile_rankings': rankings,
            'hypothesis_tests': hypothesis_tests,
            'risk_metrics': risk_metrics
        }

        # Convert numpy types before saving
        output = convert_numpy_types(output)
        save_json(output, '/root/Projects/watches/analytics/statistical_insights.json')

        results['statistics'] = output
        print(f"\n  ✓ Complete! ({time.time() - module_start:.2f}s)")
        print(f"    - {len(hypothesis_tests)} hypothesis tests performed")
        print(f"    - Market median CAGR: {summary['cagr']['median']}%")

    except Exception as e:
        print(f"\n  ✗ Error in statistical insights module: {e}")
        import traceback
        traceback.print_exc()

    # =========================================================================
    # MODULE 4: COMPREHENSIVE REPORT
    # =========================================================================
    print("\n" + "=" * 80)
    print("MODULE 4: COMPREHENSIVE REPORT GENERATION")
    print("=" * 80)
    module_start = time.time()

    try:
        print("\n  [1/3] Loading all analyses...")
        generator = AnalyticsReportGenerator()
        generator.load_all_analyses()

        print("  [2/3] Generating executive summary...")
        report = generator.generate_full_report()

        print("  [3/3] Creating markdown report...")

        results['report'] = report
        print(f"\n  ✓ Complete! ({time.time() - module_start:.2f}s)")
        print(f"    - JSON: {report['json_report']}")
        print(f"    - Markdown: {report['markdown_report']}")

    except Exception as e:
        print(f"\n  ✗ Error in report generation module: {e}")
        import traceback
        traceback.print_exc()

    # =========================================================================
    # FINAL SUMMARY
    # =========================================================================
    total_time = time.time() - start_time

    print("\n" + "=" * 80)
    print("ANALYTICS PIPELINE COMPLETE")
    print("=" * 80)
    print(f"\nTotal Execution Time: {total_time:.2f} seconds")
    print(f"End Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")

    print("Generated Files:")
    print("  1. /root/Projects/watches/analytics/price_predictions.json")
    print("  2. /root/Projects/watches/analytics/market_analysis.json")
    print("  3. /root/Projects/watches/analytics/statistical_insights.json")
    print("  4. /root/Projects/watches/analytics/comprehensive_report.json")
    print("  5. /root/Projects/watches/analytics/INVESTMENT_REPORT.md")

    print("\n" + "=" * 80)

    return results


if __name__ == '__main__':
    try:
        results = run_all_analytics()
        print("\n✓ All analytics modules executed successfully!\n")
    except Exception as e:
        print(f"\n✗ Fatal error in analytics pipeline: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)