Fix wealth distribution chart functionality
- Add get_wealth_histogram() method to EconomicModel for histogram data - Add new API endpoint /simulation/<id>/distribution for chart data - Extend main data API with include_distribution parameter - Update real-time progress updates to include distribution data - Fix frontend updateCharts() to handle wealth distribution chart - Add distribution data processing in simulation.js - Update test_charts.py to verify histogram functionality Resolves issue where wealth distribution chart was not updating during simulations.
This commit is contained in:
@@ -17,8 +17,8 @@ print('Started simulation:', start_resp.json()['status'])
|
||||
# Wait for completion
|
||||
time.sleep(3)
|
||||
|
||||
# Get evolution data
|
||||
data_resp = requests.get(f'http://localhost:5000/api/simulation/{sim_id}/data?include_evolution=true')
|
||||
# Get evolution data with distribution
|
||||
data_resp = requests.get(f'http://localhost:5000/api/simulation/{sim_id}/data?include_evolution=true&include_distribution=true')
|
||||
result = data_resp.json()
|
||||
|
||||
print('\nFinal metrics:')
|
||||
@@ -28,7 +28,40 @@ print(f' Final Total Wealth: ${result["evolution"]["total_wealth"][-1]:.2f}')
|
||||
print(f' Top 10% data points: {len(result["evolution"].get("top10_shares", []))}')
|
||||
print(f' Capital share data points: {len(result["evolution"].get("capital_shares", []))}')
|
||||
|
||||
# Test wealth distribution
|
||||
if 'distribution' in result:
|
||||
dist_data = result['distribution']
|
||||
print(f'\nWealth Distribution:')
|
||||
print(f' Bins: {dist_data.get("bins", 0)}')
|
||||
print(f' Labels: {len(dist_data.get("labels", []))}')
|
||||
print(f' Counts: {len(dist_data.get("counts", []))}')
|
||||
|
||||
if dist_data.get('labels') and dist_data.get('counts'):
|
||||
print('\n Distribution Histogram:')
|
||||
for label, count in zip(dist_data['labels'], dist_data['counts']):
|
||||
bar = '█' * max(1, count // 2) # Visual bar representation
|
||||
print(f' {label:<20} {count:>3} agents {bar}')
|
||||
print('\n✅ Wealth distribution chart should now work!')
|
||||
else:
|
||||
print('\n❌ No distribution data available')
|
||||
else:
|
||||
print('\n❌ No distribution data in response')
|
||||
|
||||
# Test dedicated distribution endpoint
|
||||
print('\nTesting dedicated distribution endpoint...')
|
||||
dist_resp = requests.get(f'http://localhost:5000/api/simulation/{sim_id}/distribution?bins=8')
|
||||
if dist_resp.status_code == 200:
|
||||
dist_result = dist_resp.json()
|
||||
hist_data = dist_result.get('histogram', {})
|
||||
print(f' Total agents: {hist_data.get("total_agents", 0)}')
|
||||
print(f' Bins: {hist_data.get("bins", 0)}')
|
||||
print(f' Labels count: {len(hist_data.get("labels", []))}')
|
||||
print(f' Counts sum: {sum(hist_data.get("counts", []))}')
|
||||
print('\n✅ Distribution endpoint working!')
|
||||
else:
|
||||
print(f'\n❌ Distribution endpoint failed: {dist_resp.status_code}')
|
||||
|
||||
if len(result["evolution"]["gini_coefficients"]) > 0:
|
||||
print('\n✅ Charts should now be populated with data!')
|
||||
print('\n🎯 All charts should now be populated with data!')
|
||||
else:
|
||||
print('\n❌ No data available for charts')
|
Reference in New Issue
Block a user