-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_from_csv.py
More file actions
1240 lines (1099 loc) · 51.1 KB
/
plot_from_csv.py
File metadata and controls
1240 lines (1099 loc) · 51.1 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Simple plotting script that creates plots from existing CSV files."""
import argparse
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.patches import Patch
import pandas as pd
import numpy as np
STRATEGY_KEYWORDS = {
"backup_suppliers": "Backup Suppliers",
"capital_hardening": "Capital Hardening",
"reserved_capacity": "Reserved Capacity",
"stockpiling": "Stockpiling",
}
SMOOTHABLE_METRICS = {"Firm_Production", "Household_Consumption"}
def _smooth_series(y_vals, window):
"""Apply centered rolling-mean smoothing to a 1-D array."""
if window is None or window <= 1:
return y_vals
return pd.Series(y_vals).rolling(window=window, min_periods=1, center=True).mean().to_numpy()
def infer_scenario_name(csv_path: Path) -> str:
"""Infer a human-readable scenario name from a results filename."""
stem = csv_path.stem.lower()
if "baseline" in stem:
base = "Baseline"
elif "hazard" in stem:
base = "Hazard"
else:
base = csv_path.stem.replace("simulation_", "").replace("_", " ").title()
# Check for specific adaptation strategies first
for key, display in STRATEGY_KEYWORDS.items():
if key in stem:
return f"{base} + {display}"
if "noadaptation" in stem or "no_adaptation" in stem:
mode = "No Adaptation"
elif "adaptation" in stem:
mode = "Adaptation"
elif "nolearning" in stem or "no_learning" in stem:
mode = "No Learning"
elif "learning" in stem:
mode = "Learning"
else:
return base
return f"{base} + {mode}"
def is_hazard_scenario(scenario: str) -> bool:
return "hazard" in scenario.lower()
def is_no_adaptation_scenario(scenario: str) -> bool:
scenario_lower = scenario.lower()
return (
"no adaptation" in scenario_lower
or "noadaptation" in scenario_lower
or "no learning" in scenario_lower
or "nolearning" in scenario_lower
)
def is_adaptation_scenario(scenario: str) -> bool:
scenario_lower = scenario.lower()
# Check for specific strategy names
for key in STRATEGY_KEYWORDS:
if key in scenario_lower or STRATEGY_KEYWORDS[key].lower() in scenario_lower:
return True
return (
("adaptation" in scenario_lower and not is_no_adaptation_scenario(scenario))
or ("learning" in scenario_lower and not is_no_adaptation_scenario(scenario))
)
STRATEGY_ABBREVS = {
"backup suppliers": "BS",
"backup_suppliers": "BS",
"capital hardening": "CH",
"capital_hardening": "CH",
"reserved capacity": "RC",
"reserved_capacity": "RC",
"stockpiling": "SP",
}
def scenario_abbrev(scenario: str) -> str:
scenario_lower = scenario.lower()
if "baseline" in scenario_lower:
base = "Baseline"
elif "hazard" in scenario_lower:
base = "Hazard"
else:
return scenario
if is_no_adaptation_scenario(scenario):
return f"{base}-NA"
# Check for specific strategy abbreviation
for key, abbrev in STRATEGY_ABBREVS.items():
if key in scenario_lower:
return f"{base}-{abbrev}"
if is_adaptation_scenario(scenario):
return f"{base}-A"
return base
def _get_strategy_key(scenario: str) -> str:
"""Return the strategy key from a scenario name, or empty string if none."""
scenario_lower = scenario.lower()
for key in STRATEGY_KEYWORDS:
if key in scenario_lower or STRATEGY_KEYWORDS[key].lower() in scenario_lower:
return key
return ""
def scenario_main_color(scenario: str) -> str:
"""Return a unique main color for each core paper scenario."""
if "baseline" in scenario.lower():
return "#4C78A8" # blue
strategy = _get_strategy_key(scenario)
if strategy == "backup_suppliers":
return "#59A14F" # green
if strategy == "capital_hardening":
return "#F28E2B" # orange
if strategy == "reserved_capacity":
return "#76B7B2" # teal
if strategy == "stockpiling":
return "#B07AA1" # purple
if is_hazard_scenario(scenario) and is_no_adaptation_scenario(scenario):
return "#E15759" # red
if is_hazard_scenario(scenario) and is_adaptation_scenario(scenario):
return "#59A14F" # green (legacy "Adaptation")
if is_hazard_scenario(scenario):
return "#F28E2B" # orange
return "#7F7F7F"
def parse_args():
parser = argparse.ArgumentParser(
description="Create plots from existing simulation CSV files",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--csv-files",
nargs="+",
required=True,
help="Paths to one or more main results CSV files to compare"
)
parser.add_argument(
"--agents-csv",
help="Path to the agents CSV file (optional, will auto-detect if not provided)"
)
parser.add_argument(
"--no-sector",
action="store_true",
help="Hide sector-level time series from agent data",
)
parser.add_argument(
"--out",
default="timeseries.png",
help="Output plot filename"
)
parser.add_argument(
"--bottleneck-out",
default="bottleneck_plot.png",
help="Output filename for the bottleneck plots"
)
parser.add_argument(
"--show-inventory",
action="store_true",
help="Add a 4th row showing firm inventory and household labor sold"
)
parser.add_argument(
"--ensemble-stat",
choices=("mean", "median"),
default="mean",
help="Statistic to plot from ensemble summary CSVs"
)
parser.add_argument(
"--show-ensemble-members",
action="store_true",
help="When a summary CSV has a matching *_members.csv sidecar, overlay faint ensemble members"
)
parser.add_argument(
"--show-ensemble-band",
action="store_true",
help="Shade the p10-p90 band when ensemble summary CSVs are provided"
)
parser.add_argument(
"--plot-start-year",
type=float,
help="If provided, discard data before this calendar year when plotting",
)
parser.add_argument(
"--smooth",
type=int,
nargs="?",
const=8,
default=None,
help="Apply rolling-mean smoothing to production and consumption panels (window size in steps; default 8)",
)
return parser.parse_args()
ENSEMBLE_STAT_ORDER = ["mean", "median", "std", "p10", "p90"]
METADATA_PREFIX = "Meta_"
def summarize_members_for_plot(df: pd.DataFrame) -> pd.DataFrame:
"""Create a summary dataframe from a member-level ensemble CSV."""
group_cols = [col for col in ["Scenario", "Step", "Year"] if col in df.columns]
numeric_cols = [
col for col in df.select_dtypes(include=[np.number]).columns
if col not in set(group_cols + ["Seed"])
and not col.startswith(METADATA_PREFIX)
]
grouped = df.groupby(group_cols, sort=True)
ensemble_size = grouped["Seed"].nunique().rename("EnsembleSize").reset_index()
frames = []
aggregations = {
"mean": grouped[numeric_cols].mean(),
"median": grouped[numeric_cols].median(),
"std": grouped[numeric_cols].std().fillna(0.0),
"p10": grouped[numeric_cols].quantile(0.10),
"p90": grouped[numeric_cols].quantile(0.90),
}
for stat in ENSEMBLE_STAT_ORDER:
stat_df = aggregations[stat].reset_index()
stat_df["EnsembleStatistic"] = stat
stat_df = stat_df.merge(ensemble_size, on=group_cols, how="left")
frames.append(stat_df)
summary_df = pd.concat(frames, ignore_index=True)
summary_df["EnsembleStatistic"] = pd.Categorical(
summary_df["EnsembleStatistic"],
categories=ENSEMBLE_STAT_ORDER,
ordered=True,
)
return summary_df.sort_values(group_cols + ["EnsembleStatistic"]).reset_index(drop=True)
def main():
args = parse_args()
show_sector_series = not args.no_sector
# Load and combine multiple CSV files
dataframes = []
agent_dataframes = []
ensemble_member_dataframes = []
ensemble_band_frames = []
for csv_file in args.csv_files:
csv_path = Path(csv_file)
if not csv_path.exists():
raise FileNotFoundError(f"CSV file not found: {csv_path}")
df = pd.read_csv(csv_path)
print(f"Loaded data from {csv_path}")
# Extract scenario from filename or add step column if needed
if "Scenario" not in df.columns:
# Try to infer scenario from filename
scenario_name = infer_scenario_name(csv_path)
df["Scenario"] = scenario_name
else:
# Refine generic "Adaptation" scenarios using Meta_AdaptationStrategy
if "Meta_AdaptationStrategy" in df.columns:
strategy_col = df["Meta_AdaptationStrategy"].dropna()
if not strategy_col.empty:
strategy_val = str(strategy_col.iloc[0])
if strategy_val in STRATEGY_KEYWORDS:
display_name = STRATEGY_KEYWORDS[strategy_val]
df["Scenario"] = df["Scenario"].replace(
{"Hazard + Adaptation": f"Hazard + {display_name}"},
)
non_null_scenarios = df["Scenario"].dropna()
scenario_name = (
str(non_null_scenarios.iloc[0])
if not non_null_scenarios.empty
else infer_scenario_name(csv_path)
)
# Add step column if not present
if "Step" not in df.columns:
df["Step"] = df.index
if "EnsembleStatistic" in df.columns:
band_subset = df[df["EnsembleStatistic"].isin(["p10", "p90"])].copy()
if not band_subset.empty:
ensemble_band_frames.append(band_subset)
selected = df[df["EnsembleStatistic"] == args.ensemble_stat].copy()
if selected.empty:
raise ValueError(f"{csv_path} does not contain EnsembleStatistic={args.ensemble_stat}")
df = selected
if args.show_ensemble_members or args.show_ensemble_band:
members_path = csv_path.parent / f"{csv_path.stem}_members.csv"
if members_path.exists():
member_df = pd.read_csv(members_path)
if "Scenario" not in member_df.columns:
member_df["Scenario"] = scenario_name
if "Step" not in member_df.columns:
member_df["Step"] = member_df.index
ensemble_member_dataframes.append(member_df)
print(f"Loaded ensemble members from {members_path}")
else:
print(f"Warning: No ensemble members sidecar found for {csv_path}")
elif "Seed" in df.columns:
if args.show_ensemble_members or args.show_ensemble_band:
ensemble_member_dataframes.append(df.copy())
full_summary = summarize_members_for_plot(df)
band_subset = full_summary[full_summary["EnsembleStatistic"].isin(["p10", "p90"])].copy()
if not band_subset.empty:
ensemble_band_frames.append(band_subset)
df = full_summary[full_summary["EnsembleStatistic"] == args.ensemble_stat].copy()
dataframes.append(df)
# Load corresponding agent data
if args.agents_csv:
# Use explicitly provided agent CSV for all scenarios
agents_path = Path(args.agents_csv)
if agents_path.exists():
agent_df = pd.read_csv(args.agents_csv)
if "Scenario" not in agent_df.columns:
agent_df["Scenario"] = scenario_name
agent_dataframes.append(agent_df)
print(f"Loaded agent data from {agents_path}")
else:
print(f"Warning: Specified agent data file not found: {agents_path}")
else:
# Auto-detect agent CSV
agents_path = csv_path.parent / f"{csv_path.stem}_agents.csv"
if agents_path.exists():
agent_df = pd.read_csv(agents_path)
if "Scenario" not in agent_df.columns:
agent_df["Scenario"] = scenario_name
agent_dataframes.append(agent_df)
print(f"Loaded agent data from {agents_path}")
else:
print(f"Warning: No agent data found for {csv_path}")
# Combine all dataframes
df_combined = pd.concat(dataframes, ignore_index=True)
member_df_combined = (
pd.concat(ensemble_member_dataframes, ignore_index=True)
if ensemble_member_dataframes else pd.DataFrame()
)
band_df_combined = (
pd.concat(ensemble_band_frames, ignore_index=True)
if ensemble_band_frames else pd.DataFrame()
)
if agent_dataframes:
agent_df_combined = pd.concat(agent_dataframes, ignore_index=True)
# Separate firm and household agents
firm_agents_df = agent_df_combined[agent_df_combined["type"] == "FirmAgent"].copy()
household_agents_df = agent_df_combined[agent_df_combined["type"] == "HouseholdAgent"].copy()
else:
print("Warning: No agent data found - bottleneck plots will not work")
firm_agents_df = pd.DataFrame()
household_agents_df = pd.DataFrame()
# Determine x-axis column - prefer Year column if available
if "Year" in df_combined.columns:
x_col = "Year"
elif "Step" in df_combined.columns:
x_col = "Step"
else:
# Create step column from index
df_combined["Step"] = df_combined.index
x_col = "Step"
# Get unique scenarios
unique_scenarios = sorted(df_combined["Scenario"].unique())
print(f"Found scenarios: {unique_scenarios}")
# Map Step to Year per scenario so agent data can use actual years
step_to_year_map = {}
if "Year" in df_combined.columns and "Step" in df_combined.columns:
for scenario in unique_scenarios:
scen_df = df_combined[df_combined["Scenario"] == scenario]
mapping = (
scen_df.dropna(subset=["Step", "Year"])
.drop_duplicates(subset="Step")
.set_index("Step")["Year"]
.to_dict()
)
if mapping:
step_to_year_map[scenario] = mapping
if not step_to_year_map:
fallback_map = (
df_combined.dropna(subset=["Step", "Year"])
.drop_duplicates(subset="Step")
.set_index("Step")["Year"]
.to_dict()
)
if fallback_map:
step_to_year_map["__all__"] = fallback_map
def add_year_from_step(df, scenario):
"""Attach Year values to agent data using mapping from aggregate results."""
if "Year" in df.columns or "Step" not in df.columns or df.empty:
return df
year_map = step_to_year_map.get(scenario) or step_to_year_map.get("__all__")
if not year_map:
return df
df_copy = df.copy()
df_copy["Year"] = df_copy["Step"].map(year_map)
return df_copy
def filter_plot_start_year(df, scenario_hint="__all__"):
"""Filter a dataframe to the requested minimum plot year, if possible."""
if args.plot_start_year is None or df.empty:
return df
if "Year" in df.columns:
return df[df["Year"] >= args.plot_start_year].copy()
if "Step" not in df.columns:
return df
if "Scenario" in df.columns:
frames = []
for scenario, sub in df.groupby("Scenario", dropna=False):
scenario_key = str(scenario) if pd.notna(scenario) else scenario_hint
sub_with_year = add_year_from_step(sub, scenario_key)
if "Year" in sub_with_year.columns:
sub_with_year = sub_with_year[sub_with_year["Year"] >= args.plot_start_year]
frames.append(sub_with_year)
return pd.concat(frames, ignore_index=True) if frames else df.iloc[0:0].copy()
df_with_year = add_year_from_step(df, scenario_hint)
if "Year" in df_with_year.columns:
return df_with_year[df_with_year["Year"] >= args.plot_start_year].copy()
return df_with_year
if args.plot_start_year is not None:
if "Year" not in df_combined.columns and not step_to_year_map:
raise ValueError("--plot-start-year requires Year information in the results CSVs.")
df_combined = filter_plot_start_year(df_combined)
member_df_combined = filter_plot_start_year(member_df_combined)
band_df_combined = filter_plot_start_year(band_df_combined)
firm_agents_df = filter_plot_start_year(firm_agents_df)
household_agents_df = filter_plot_start_year(household_agents_df)
if df_combined.empty:
raise ValueError(
f"No rows remain after applying --plot-start-year {args.plot_start_year}."
)
household_demand_sectors = []
if not firm_agents_df.empty and "household_sales_last_step" in firm_agents_df.columns:
demand_totals = firm_agents_df.groupby("sector")["household_sales_last_step"].sum(min_count=1)
household_demand_sectors = sorted(demand_totals[demand_totals > 0].index.tolist())
# Use distinct colors for each scenario so ensemble bands remain visually separable.
scenario_style_map = {}
for scenario in unique_scenarios:
color = scenario_main_color(scenario)
linestyle = "-"
linewidth = 1.8
scenario_style_map[scenario] = {
"color": color,
"linestyle": linestyle,
"linewidth": linewidth,
}
# Define sector color palettes keyed by scenario so sector traces stay close to their parent scenario color.
scenario_sector_palettes = {
"baseline": ["#9ecae9", "#6baed6", "#3182bd"],
"hazard_adaptation": ["#A1D99B", "#74C476", "#31A354"],
"hazard_backup_suppliers": ["#A1D99B", "#74C476", "#31A354"],
"hazard_capital_hardening": ["#FDD0A2", "#FDAE6B", "#E6550D"],
"hazard_reserved_capacity": ["#B2E2E2", "#66C2A4", "#238B8D"],
"hazard_stockpiling": ["#D4B9DA", "#C994C7", "#88419D"],
"hazard_noadaptation": ["#FCAE91", "#FB6A4A", "#CB181D"],
"hazard": ["#FDD0A2", "#FDAE6B", "#E6550D"],
"default": ["#D0D0D0", "#A0A0A0", "#707070"],
}
def get_sector_style(scenario, sector_idx):
"""Get color and style for a sector line based on scenario and sector index."""
strategy = _get_strategy_key(scenario)
if "baseline" in scenario.lower():
palette = scenario_sector_palettes["baseline"]
elif strategy:
palette = scenario_sector_palettes.get(
f"hazard_{strategy}", scenario_sector_palettes["hazard_adaptation"]
)
elif is_hazard_scenario(scenario) and is_adaptation_scenario(scenario):
palette = scenario_sector_palettes["hazard_adaptation"]
elif is_hazard_scenario(scenario) and is_no_adaptation_scenario(scenario):
palette = scenario_sector_palettes["hazard_noadaptation"]
elif is_hazard_scenario(scenario):
palette = scenario_sector_palettes["hazard"]
else:
palette = scenario_sector_palettes["default"]
color = palette[sector_idx % len(palette)]
return {
"color": color,
"linestyle": "-",
"alpha": 0.8,
"linewidth": 0.7,
"zorder": 1
}
# Build per-scenario price deflator for converting nominal → real values.
# Maps (scenario, year_or_step) → mean_price. Metrics tagged as "real"
# will be divided by the deflator so they are expressed in base-period units.
price_deflator: dict[str, dict] = {}
for scenario, grp in df_combined.groupby("Scenario"):
if "Mean_Price" in grp.columns:
deflator = grp.set_index(x_col)["Mean_Price"].to_dict()
price_deflator[scenario] = deflator
# Define time-series metrics (separate from bottlenecks)
# Order: Production, Capital, Liquidity (real), Consumption, Wage (real), Price
# Optional 4th row: Firm Inventory + Household Labor Sold
ts_metrics = [
"Firm_Production", "Firm_Capital",
"Firm_Liquidity", "Household_Consumption",
"Mean_Wage", "Mean_Price",
]
# Optionally add diagnostic row
if args.show_inventory:
ts_metrics.extend(["Firm_Inventory", "Household_Labor_Sold"])
# Metrics that should be deflated (divided by mean price) to show real values
real_metrics = {"Firm_Liquidity", "Mean_Wage"}
# Define bottleneck metrics separately
bottleneck_metrics = [
"Bottleneck_Baseline_Adaptation", "Bottleneck_Hazard_Adaptation",
"Bottleneck_Baseline_NoAdaptation", "Bottleneck_Hazard_NoAdaptation"
]
# Create time-series figure (3x2 or 4x2 layout depending on --show-inventory)
n_rows = 4 if args.show_inventory else 3
fig_height = 13 if args.show_inventory else 10
fig_ts, axes_ts = plt.subplots(n_rows, 2, figsize=(12, fig_height))
# Units for y-axis labels
units = {
"Firm_Production": "Aggregate Units of Goods",
"Firm_Liquidity": "Real Dollars ($ / Mean Price)",
"Firm_Capital": "Aggregate Units of Capital",
"Firm_Inventory": "Aggregate Units of Goods",
"Mean_Price": "$ / Unit of Goods",
"Mean_Wage": "Real Dollars ($ / Mean Price)",
"Household_Labor_Sold": "Aggregate Units of Labor",
"Household_Consumption": "Aggregate Units of Goods",
"Household_Liquidity": "Aggregate Dollars ($)",
}
metric_title_map = {
"Firm_Production": "Aggregate Firm Production",
"Firm_Liquidity": "Aggregate Real Firm Liquidity",
"Firm_Capital": "Aggregate Firm Capital",
"Firm_Inventory": "Aggregate Firm Inventory",
"Mean_Price": "Mean Firm Price",
"Mean_Wage": "Mean Firm Wage Offer",
"Household_Labor_Sold": "Aggregate Household Labor Sold",
"Household_Consumption": "Aggregate Household Consumption",
"Household_Liquidity": "Aggregate Household Liquidity",
}
aggregate_metric_map = {
"Firm_Production": "Firm_Production",
"Firm_Liquidity": "Firm_Wealth",
"Firm_Capital": "Firm_Capital",
"Firm_Inventory": "Firm_Inventory",
"Household_Consumption": "Household_Consumption",
"Household_Labor_Sold": "Household_Labor_Sold",
"Household_Liquidity": "Household_Wealth",
"Mean_Price": "Mean_Price",
"Mean_Wage": "Mean_Wage",
}
sector_aggregation = {
"Firm_Production": "sum",
"Firm_Liquidity": "sum",
"Firm_Capital": "sum",
"Firm_Inventory": "sum",
"Mean_Price": "mean",
"Mean_Wage": "mean",
}
def deflate(x_vals, y_vals, scenario, metric_name, source_df=None, deflator_col="Mean_Price"):
"""Divide y-values by the mean price at each x-value if metric is real."""
if metric_name not in real_metrics:
return y_vals
if source_df is not None and deflator_col in source_df.columns:
prices = source_df[deflator_col].to_numpy(dtype=float)
else:
deflator = price_deflator.get(scenario, {})
if not deflator:
return y_vals
prices = np.array([deflator.get(x, np.nan) for x in x_vals], dtype=float)
prices[prices == 0] = np.nan
return np.where(np.isfinite(prices), y_vals / prices, y_vals)
def plot_ensemble_context(metric_col, metric_name, scenario, ax):
"""Overlay ensemble members and uncertainty band for aggregate metrics."""
style = scenario_style_map[scenario]
if args.show_ensemble_members and not member_df_combined.empty and metric_col in member_df_combined.columns:
member_subset = member_df_combined[member_df_combined["Scenario"] == scenario]
if not member_subset.empty:
for _, member_grp in member_subset.groupby("Seed"):
member_grp = member_grp.sort_values(x_col)
x_vals = member_grp[x_col].to_numpy()
y_vals = deflate(
x_vals,
member_grp[metric_col].to_numpy(dtype=float),
scenario,
metric_name,
source_df=member_grp,
)
if args.smooth and metric_name in SMOOTHABLE_METRICS:
y_vals = _smooth_series(y_vals, args.smooth)
ax.plot(
x_vals,
y_vals,
color=style["color"],
linestyle=style["linestyle"],
linewidth=0.8,
alpha=0.12,
zorder=1,
)
band_plotted = False
if args.show_ensemble_band and not member_df_combined.empty and metric_col in member_df_combined.columns:
member_subset = member_df_combined[member_df_combined["Scenario"] == scenario]
if not member_subset.empty:
member_band_frames = []
for seed, member_grp in member_subset.groupby("Seed"):
member_grp = member_grp.sort_values(x_col)
x_vals = member_grp[x_col].to_numpy()
y_vals = deflate(
x_vals,
member_grp[metric_col].to_numpy(dtype=float),
scenario,
metric_name,
source_df=member_grp,
)
if args.smooth and metric_name in SMOOTHABLE_METRICS:
y_vals = _smooth_series(y_vals, args.smooth)
member_band_frames.append(
pd.DataFrame(
{
x_col: x_vals,
"BandValue": y_vals,
"Seed": seed,
}
)
)
if member_band_frames:
member_band_df = pd.concat(member_band_frames, ignore_index=True)
quantiles = (
member_band_df.groupby(x_col)["BandValue"]
.quantile([0.10, 0.90])
.unstack()
.rename(columns={0.10: "p10", 0.90: "p90"})
.reset_index()
.sort_values(x_col)
)
if not quantiles.empty:
ax.fill_between(
quantiles[x_col].to_numpy(),
quantiles["p10"].to_numpy(dtype=float),
quantiles["p90"].to_numpy(dtype=float),
color=style["color"],
alpha=0.12,
linewidth=0,
zorder=2,
)
band_plotted = True
if (
args.show_ensemble_band
and not band_plotted
and not band_df_combined.empty
and metric_col in band_df_combined.columns
):
band_subset = band_df_combined[band_df_combined["Scenario"] == scenario]
p10 = band_subset[band_subset["EnsembleStatistic"] == "p10"].copy()
p90 = band_subset[band_subset["EnsembleStatistic"] == "p90"].copy()
if not p10.empty and not p90.empty:
merge_cols = [x_col, metric_col]
if metric_name in real_metrics:
if "Mean_Price" in p10.columns:
merge_cols.append("Mean_Price")
if "Mean_Price" in p90.columns and "Mean_Price" not in merge_cols:
merge_cols.append("Mean_Price")
lower_df = p10[merge_cols].sort_values(x_col).rename(
columns={
metric_col: f"{metric_col}_p10",
"Mean_Price": "Mean_Price_p10",
}
)
upper_df = p90[merge_cols].sort_values(x_col).rename(
columns={
metric_col: f"{metric_col}_p90",
"Mean_Price": "Mean_Price_p90",
}
)
band = lower_df.merge(upper_df, on=x_col, how="inner")
if not band.empty:
x_vals = band[x_col].to_numpy()
lower_vals = deflate(
x_vals,
band[f"{metric_col}_p10"].to_numpy(dtype=float),
scenario,
metric_name,
source_df=band,
deflator_col="Mean_Price_p10",
)
upper_vals = deflate(
x_vals,
band[f"{metric_col}_p90"].to_numpy(dtype=float),
scenario,
metric_name,
source_df=band,
deflator_col="Mean_Price_p90",
)
if args.smooth and metric_name in SMOOTHABLE_METRICS:
lower_vals = _smooth_series(lower_vals, args.smooth)
upper_vals = _smooth_series(upper_vals, args.smooth)
ax.fill_between(
x_vals,
lower_vals,
upper_vals,
color=style["color"],
alpha=0.12,
linewidth=0,
zorder=2,
)
def plot_metric(metric_name, ax):
"""Plot a single metric.
Args:
metric_name: Name of the metric to plot
ax: Matplotlib axes object
"""
# Define metric mappings for agent-level data
firm_metric_map = {
"Firm_Production": "production",
"Firm_Liquidity": "money",
"Firm_Capital": "capital",
"Firm_Inventory": "inventory"
}
household_metric_map = {
"Household_Labor_Sold": "labor_sold",
"Household_Consumption": "consumption",
"Household_Liquidity": "money"
}
if metric_name in ["Mean_Price", "Mean_Wage"]:
# Plot main scenario lines from aggregate data
for scenario, grp in df_combined.groupby("Scenario"):
style = scenario_style_map[scenario]
if metric_name in grp.columns:
x_data = grp[x_col].values
plot_ensemble_context(metric_name, metric_name, scenario, ax)
y_data = deflate(
x_data,
grp[metric_name].values,
scenario,
metric_name,
source_df=grp,
)
ax.plot(x_data, y_data,
color=style["color"], linestyle=style["linestyle"],
label=f"Mean - {scenario}", linewidth=style["linewidth"], alpha=0.7, zorder=3)
# Add sector lines from agent data for wages and prices
if show_sector_series and not firm_agents_df.empty and metric_name in ["Mean_Price", "Mean_Wage"]:
agent_col = "price" if metric_name == "Mean_Price" else "wage"
sectors = sorted(firm_agents_df["sector"].dropna().unique())
for scenario in unique_scenarios:
if not firm_agents_df.empty and "Scenario" in firm_agents_df.columns:
df_scen = firm_agents_df[firm_agents_df["Scenario"] == scenario]
else:
df_scen = firm_agents_df # Use all data if no scenario column
df_scen = add_year_from_step(df_scen, scenario)
style = scenario_style_map[scenario]
for idx_sec, sector in enumerate(sectors):
sector_data = df_scen[df_scen["sector"] == sector]
if sector_data.empty:
continue
# Use Year column if available, otherwise Step
if "Year" in sector_data.columns:
grp = sector_data.dropna(subset=["Year"]).groupby("Year")[agent_col].mean()
if grp.empty and "Step" in sector_data.columns:
grp = sector_data.groupby("Step")[agent_col].mean()
else:
grp = sector_data.groupby("Step")[agent_col].mean()
if grp.empty:
continue
x_vals = grp.index
y_vals = deflate(np.array(x_vals), grp.values, scenario, metric_name)
sector_style = get_sector_style(scenario, idx_sec)
ax.plot(x_vals, y_vals,
label=f"{sector} - {scenario}", **sector_style)
elif metric_name in firm_metric_map:
# Plot firm metrics with aggregate main lines and optional sector breakdown.
agent_col = firm_metric_map[metric_name]
aggregate_col = aggregate_metric_map.get(metric_name)
# Plot main scenario lines from aggregate model outputs when available.
if aggregate_col and aggregate_col in df_combined.columns:
for scenario, grp in df_combined.groupby("Scenario"):
style = scenario_style_map[scenario]
x_vals = grp[x_col].values
plot_ensemble_context(aggregate_col, metric_name, scenario, ax)
y_vals = deflate(
x_vals,
grp[aggregate_col].values,
scenario,
metric_name,
source_df=grp,
)
if args.smooth and metric_name in SMOOTHABLE_METRICS:
y_vals = _smooth_series(y_vals, args.smooth)
ax.plot(
x_vals,
y_vals,
color=style["color"],
linewidth=style["linewidth"],
alpha=0.7,
linestyle=style["linestyle"],
label=f"Mean - {scenario}",
zorder=3,
)
else:
for scenario in unique_scenarios:
if not firm_agents_df.empty and "Scenario" in firm_agents_df.columns:
df_scen = firm_agents_df[firm_agents_df["Scenario"] == scenario]
else:
df_scen = firm_agents_df
if df_scen.empty:
continue
df_scen = add_year_from_step(df_scen, scenario)
if "Year" in df_scen.columns:
main_grp = df_scen.dropna(subset=["Year"]).groupby("Year")[agent_col].mean()
if main_grp.empty and "Step" in df_scen.columns:
main_grp = df_scen.groupby("Step")[agent_col].mean()
else:
main_grp = df_scen.groupby("Step")[agent_col].mean()
if main_grp.empty:
continue
x_vals = np.array(main_grp.index)
y_vals = deflate(x_vals, main_grp.values, scenario, metric_name)
if args.smooth and metric_name in SMOOTHABLE_METRICS:
y_vals = _smooth_series(y_vals, args.smooth)
style = scenario_style_map[scenario]
ax.plot(
x_vals,
y_vals,
color=style["color"],
linewidth=style["linewidth"],
alpha=0.7,
linestyle=style["linestyle"],
label=f"Mean - {scenario}",
zorder=3,
)
# Add sector lines
if show_sector_series and not firm_agents_df.empty:
sectors = sorted(firm_agents_df["sector"].dropna().unique())
agg_mode = sector_aggregation.get(metric_name, "mean")
for scenario in unique_scenarios:
if not firm_agents_df.empty and "Scenario" in firm_agents_df.columns:
df_scen = firm_agents_df[firm_agents_df["Scenario"] == scenario]
else:
df_scen = firm_agents_df # Use all data if no scenario column
df_scen = add_year_from_step(df_scen, scenario)
style = scenario_style_map[scenario]
for idx_sec, sector in enumerate(sectors):
sector_data = df_scen[df_scen["sector"] == sector]
# Use Year column if available, otherwise Step
if "Year" in sector_data.columns:
groupby_obj = sector_data.dropna(subset=["Year"]).groupby("Year")[agent_col]
grp = groupby_obj.sum() if agg_mode == "sum" else groupby_obj.mean()
if grp.empty and "Step" in sector_data.columns:
groupby_obj = sector_data.groupby("Step")[agent_col]
grp = groupby_obj.sum() if agg_mode == "sum" else groupby_obj.mean()
else:
groupby_obj = sector_data.groupby("Step")[agent_col]
grp = groupby_obj.sum() if agg_mode == "sum" else groupby_obj.mean()
if grp.empty:
continue
x_vals = np.array(grp.index)
y_vals = deflate(x_vals, grp.values, scenario, metric_name)
sector_style = get_sector_style(scenario, idx_sec)
ax.plot(x_vals, y_vals,
label=f"{sector} - {scenario}", **sector_style)
elif metric_name in household_metric_map:
# Plot household metrics with aggregate main lines.
agent_col = household_metric_map[metric_name]
aggregate_col = aggregate_metric_map.get(metric_name)
if aggregate_col and aggregate_col in df_combined.columns:
for scenario, grp in df_combined.groupby("Scenario"):
style = scenario_style_map[scenario]
x_vals = grp[x_col].values
plot_ensemble_context(aggregate_col, metric_name, scenario, ax)
y_vals = grp[aggregate_col].values
if args.smooth and metric_name in SMOOTHABLE_METRICS:
y_vals = _smooth_series(y_vals, args.smooth)
ax.plot(
x_vals,
y_vals,
color=style["color"],
linewidth=style["linewidth"],
alpha=0.7,
linestyle=style["linestyle"],
label=f"Mean - {scenario}",
zorder=3,
)
else:
for scenario in unique_scenarios:
if not household_agents_df.empty and "Scenario" in household_agents_df.columns:
df_scen = household_agents_df[household_agents_df["Scenario"] == scenario]
else:
df_scen = household_agents_df
if df_scen.empty:
continue
df_scen = add_year_from_step(df_scen, scenario)
if "Year" in df_scen.columns:
main_grp = df_scen.dropna(subset=["Year"]).groupby("Year")[agent_col].mean()
if main_grp.empty and "Step" in df_scen.columns:
main_grp = df_scen.groupby("Step")[agent_col].mean()
else:
main_grp = df_scen.groupby("Step")[agent_col].mean()
if main_grp.empty:
continue
x_vals = np.array(main_grp.index)
y_vals = main_grp.values
if args.smooth and metric_name in SMOOTHABLE_METRICS:
y_vals = _smooth_series(y_vals, args.smooth)
style = scenario_style_map[scenario]
ax.plot(
x_vals,
y_vals,
color=style["color"],
linewidth=style["linewidth"],
alpha=0.7,
linestyle=style["linestyle"],
label=f"Mean - {scenario}",
zorder=3,
)
# For consumption, add actual household purchases by seller sector
# when the agent CSV contains the dedicated household-sales field.
if (
show_sector_series
and metric_name == "Household_Consumption"
and household_demand_sectors
and not firm_agents_df.empty
):
for scenario in unique_scenarios:
if not household_agents_df.empty and "Scenario" in household_agents_df.columns:
hh_scen = household_agents_df[household_agents_df["Scenario"] == scenario]
else:
hh_scen = household_agents_df
if not firm_agents_df.empty and "Scenario" in firm_agents_df.columns:
firm_scen = firm_agents_df[firm_agents_df["Scenario"] == scenario]
else: