File size: 7,212 Bytes
cc54551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for dashboard visualization components."""

import pytest
import pandas as pd
import plotly.graph_objects as go

from ..components import (
    # Traffic flow
    create_speed_heatmap,
    create_hourly_volume_chart,
    create_congestion_timeline,
    # Incentive analytics
    create_funnel_chart,
    create_spend_chart,
    create_effectiveness_chart,
    # Behavioral
    create_elasticity_curve,
    create_feature_importance_chart,
    create_model_metrics_display,
    # Simulation
    create_scenario_comparison_chart,
    create_cost_effectiveness_chart,
    # Metrics
    create_kpi_gauge,
    create_metric_card,
    create_sparkline,
    # Map
    create_corridor_map,
    create_zone_comparison,
)


class TestTrafficComponents:
    """Test traffic flow visualization components."""

    def test_speed_heatmap_empty_data(self):
        """Test heatmap handles empty data gracefully."""
        df = pd.DataFrame()
        fig = create_speed_heatmap(df)
        assert isinstance(fig, go.Figure)

    def test_speed_heatmap_with_data(self):
        """Test heatmap with valid data."""
        df = pd.DataFrame({
            'hour': list(range(24)),
            'day_of_week': [0] * 24,
            'avg_speed': [50] * 24
        })
        fig = create_speed_heatmap(df)
        assert isinstance(fig, go.Figure)
        assert len(fig.data) > 0

    def test_hourly_volume_chart(self):
        """Test hourly volume chart creation."""
        df = pd.DataFrame({
            'hour': [7, 8, 9, 17, 18, 19],
            'time_period': ['AM_PEAK'] * 3 + ['PM_PEAK'] * 3,
            'total_vehicles': [100, 150, 120, 130, 160, 140],
            'avg_speed': [35, 30, 32, 28, 25, 30]
        })
        fig = create_hourly_volume_chart(df)
        assert isinstance(fig, go.Figure)

    def test_congestion_timeline(self):
        """Test congestion timeline chart."""
        df = pd.DataFrame({
            'hour_bucket': pd.date_range('2024-01-01', periods=24, freq='h'),
            'corridor_id': ['I-24'] * 24,
            'avg_speed_mph': [45 + i % 10 for i in range(24)],
            'level_of_service': ['B'] * 24
        })
        fig = create_congestion_timeline(df)
        assert isinstance(fig, go.Figure)


class TestIncentiveComponents:
    """Test incentive analytics components."""

    def test_funnel_chart(self):
        """Test funnel chart creation."""
        df = pd.DataFrame({
            'incentive_type': ['CARPOOL', 'PACER'],
            'total_offers': [100, 80],
            'accepts': [60, 50],
            'completions': [40, 35]
        })
        fig = create_funnel_chart(df)
        assert isinstance(fig, go.Figure)

    def test_spend_chart(self):
        """Test spending pie chart."""
        df = pd.DataFrame({
            'incentive_type': ['CARPOOL', 'PACER', 'TRANSIT'],
            'total_spend': [5000, 3000, 2000],
            'n_events': [200, 150, 100],
            'avg_payout': [25, 20, 20]
        })
        fig = create_spend_chart(df)
        assert isinstance(fig, go.Figure)

    def test_effectiveness_chart(self):
        """Test cost effectiveness bar chart."""
        df = pd.DataFrame({
            'incentive_type': ['CARPOOL', 'PACER'],
            'n_completed': [40, 35],
            'total_cost': [1000, 700],
            'avg_cost': [25, 20]
        })
        fig = create_effectiveness_chart(df)
        assert isinstance(fig, go.Figure)


class TestBehavioralComponents:
    """Test behavioral calibration components."""

    def test_elasticity_curve(self):
        """Test elasticity curve visualization."""
        df = pd.DataFrame({
            'incentive_bucket': ['NONE', 'LOW', 'MEDIUM', 'HIGH'],
            'n_trips': [100, 150, 120, 80],
            'carpool_rate': [0.1, 0.18, 0.28, 0.38],
            'avg_incentive': [0, 1.5, 3.5, 7.0]
        })
        fig = create_elasticity_curve(df)
        assert isinstance(fig, go.Figure)

    def test_feature_importance_chart(self):
        """Test feature importance bar chart."""
        df = pd.DataFrame({
            'feature': ['incentive', 'distance', 'time'],
            'importance': [0.4, 0.35, 0.25]
        })
        fig = create_feature_importance_chart(df)
        assert isinstance(fig, go.Figure)

    def test_model_metrics_display(self):
        """Test model metrics radar chart."""
        metrics = {'auc': 0.78, 'rmse': 0.15, 'accuracy': 0.82, 'n_samples': 1000}
        fig = create_model_metrics_display(metrics)
        assert isinstance(fig, go.Figure)


class TestSimulationComponents:
    """Test simulation comparison components."""

    def test_scenario_comparison_chart(self):
        """Test scenario comparison bar chart."""
        df = pd.DataFrame({
            'scenario_name': ['Carpool', 'Pacer', 'Transit'],
            'n_agents': [10000] * 3,
            'treatment_avg_speed': [48, 46, 44],
            'baseline_avg_speed': [42] * 3,
            'speed_improvement_pct': [14.3, 9.5, 4.8],
            'vmt_reduction_pct': [12, 8, 5],
            'peak_reduction_pct': [10, 6, 4],
            'treatment_spend': [5000, 4000, 3000]
        })
        fig = create_scenario_comparison_chart(df)
        assert isinstance(fig, go.Figure)

    def test_cost_effectiveness_chart(self):
        """Test cost vs impact scatter."""
        df = pd.DataFrame({
            'scenario_name': ['A', 'B'],
            'treatment_spend': [5000, 3000],
            'vmt_reduction_pct': [12, 8],
            'speed_improvement_pct': [10, 6]
        })
        fig = create_cost_effectiveness_chart(df)
        assert isinstance(fig, go.Figure)


class TestMetricsComponents:
    """Test real-time metrics components."""

    def test_kpi_gauge(self):
        """Test KPI gauge creation."""
        fig = create_kpi_gauge(75, 'Test Metric', '%', 0, 100)
        assert isinstance(fig, go.Figure)

    def test_metric_card(self):
        """Test metric card creation."""
        fig = create_metric_card(42.5, 'Test Value', prefix='$', format_str='.1f')
        assert isinstance(fig, go.Figure)

    def test_sparkline(self):
        """Test sparkline creation."""
        values = [10, 12, 15, 14, 18, 20]
        fig = create_sparkline(values, 'Trend')
        assert isinstance(fig, go.Figure)


class TestMapComponents:
    """Test geospatial components."""

    def test_corridor_map(self):
        """Test corridor map creation."""
        df = pd.DataFrame({
            'segment_id': ['1', '2'],
            'segment_name': ['Seg 1', 'Seg 2'],
            'latitude': [36.10, 36.12],
            'longitude': [-86.72, -86.75],
            'avg_speed_mph': [35, 45],
            'congestion_level': ['Moderate', 'Light'],
            'vehicle_count': [400, 300]
        })
        fig = create_corridor_map(df)
        assert isinstance(fig, go.Figure)

    def test_zone_comparison(self):
        """Test zone comparison chart."""
        df = pd.DataFrame({
            'zone': ['Downtown', 'Suburbs'],
            'avg_speed': [25, 45],
            'carpool_rate': [0.25, 0.15],
            'incentive_uptake': [0.35, 0.20]
        })
        fig = create_zone_comparison(df)
        assert isinstance(fig, go.Figure)