Skip to content

Summary Results

logger module-attribute

logger = getLogger('passengersim.summary')

SummaryTables

Source code in passengersim/summary.py
  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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
class SummaryTables:
    @classmethod
    def from_sqlite(
        cls,
        filename: str | pathlib.Path,
        make_indexes: bool | dict = False,
        additional: Collection[str | tuple] | str | None = None,
    ):
        summarytables_is_deprecated()
        if not os.path.isfile(filename):
            raise FileNotFoundError(filename)
        db = database.Database(
            engine="sqlite",
            filename=filename,
        )

        demands = cls.load_basic_table(db, "demand_summary")
        legs = cls.load_basic_table(db, "leg_summary")
        paths = cls.load_basic_table(db, "path_summary")
        carriers = cls.load_basic_table(db, "carrier_summary")

        summary = cls(
            demands=demands,
            legs=legs,
            paths=paths,
            carriers=carriers,
        )

        if make_indexes:
            if isinstance(make_indexes, dict):
                db.add_indexes(**make_indexes)
            else:
                db.add_indexes()

        logger.info("loading configs")
        config = db.load_configs(on_validation_error="ignore")
        try:
            scenario = config.scenario
            burn_samples = config.simulation_controls.burn_samples
        except AttributeError:
            scenario = config.get("scenario", "unknown")
            burn_samples = config.get("simulation_controls", {}).get(
                "burn_samples", 100
            )

        summary.load_additional_tables(
            db,
            scenario=scenario,
            burn_samples=burn_samples,
            additional=additional,
        )
        summary.cnx = db
        return summary

    def __getstate__(self):
        state = self.__dict__.copy()
        if "cnx" in state:
            del state["cnx"]
        if "config" in state:
            # state["_config_yaml"] = state["config"].to_yaml()
            del state["config"]
        if "meta_trials" in state and not state.get("_preserve_meta_trials", True):
            del state["meta_trials"]
        if "_preserve_meta_trials" in state:
            del state["_preserve_meta_trials"]
        return state

    def __setstate__(self, state):
        # if "_config_yaml" in state:
        #     state["config"] = Config.from_raw_yaml(state.pop("_config_yaml"))
        self.__dict__.update(state)

    def to_pickle(
        self,
        filename: str | pathlib.Path,
        add_timestamp_ext: bool = True,
        preserve_meta_trials: bool = False,
    ):
        """Save the object to a pickle file.

        Parameters
        ----------
        filename : str or Path-like
            The filename to save the object to.
        add_timestamp_ext : bool, default True
            Add a timestamp extension to the filename.
        """
        import pickle

        if add_timestamp_ext:
            filename = filename_with_timestamp(filename, suffix=".pkl")

        with open(filename, "wb") as f:
            self._preserve_meta_trials = preserve_meta_trials
            pickle.dump(self, f)
            del self._preserve_meta_trials

    @classmethod
    def from_pickle(cls, filename: str | pathlib.Path, read_latest: bool = True):
        """Load the object from a pickle file.

        Parameters
        ----------
        filename : str or Path-like
            The filename to load the object from.
        read_latest : bool, default True
            If True, read the latest file matching the pattern.
        """
        summarytables_is_deprecated()

        import glob
        import pickle

        if read_latest:
            filename_glob = pathlib.Path(filename).with_suffix(".*.pkl")
            files = sorted(glob.glob(str(filename_glob)))
            if not files:
                if not os.path.exists(filename):
                    raise FileNotFoundError(filename)
            else:
                filename = files[-1]

        with open(filename, "rb") as f:
            result = pickle.load(f)
            if result.__class__.__name__ != cls.__name__:
                raise TypeError(f"Expected {cls}, got {type(result)}")
            return result

    @classmethod
    def aggregate(cls, summaries: Collection[SummaryTables]):
        """Aggregate multiple summary tables."""
        summarytables_is_deprecated()

        if not summaries:
            return None

        # dataframes where trial is in the index, just concatenate
        def concat(name):
            frames = []
            for s in summaries:
                frame = getattr(s, name)
                if frame is not None:
                    frames.append(frame)
            if frames:
                return pd.concat(frames)
            return None

        carrier_history = concat("carrier_history")
        bookings_by_timeframe = concat("bookings_by_timeframe")
        segmentation_by_timeframe = concat("segmentation_by_timeframe")
        demand_to_come = concat("demand_to_come")

        # demands has some columns that are averages and some that are sums
        demands_avg = sum(
            s.demands.set_index(["orig", "dest", "segment"])[
                ["sold", "revenue", "avg_fare"]
            ]
            for s in summaries
        ) / len(summaries)
        demands_sum = sum(
            s.demands.set_index(["orig", "dest", "segment"])[
                ["gt_demand", "gt_sold", "gt_revenue"]
            ]
            for s in summaries
        )
        demands = pd.concat([demands_avg, demands_sum], axis=1).reset_index()

        # TODO: aggregate fares
        # fares has some columns that are (weighted) averages and some that are sums
        # fares_gt_adj_price = sum(
        #     s.fares.set_index(
        #     ["carrier", "orig", "dest", "booking_class", "dcp_index"]
        #     ).eval("gt_sold * avg_adjusted_price")
        #     for s in summaries
        # )
        # fares_avg = sum(
        #     s.fares.set_index(
        #     ["carrier", "orig", "dest", "booking_class", "dcp_index"]
        #     )[
        #         ["price", "gt_sold"]
        #     ]
        #     for s in summaries
        # ) / len(summaries)
        # fares_sum = sum(
        #     s.fares.set_index(
        #     ["carrier", "orig", "dest", "booking_class", "dcp_index"]
        #     )[
        #         ["gt_sold"]
        #     ]
        #     for s in summaries
        # )

        # TODO: aggregate path_classes

        # these are averages, but need to have the index values excluded
        # TODO: the index values should be set properly on the original dataframes
        carriers = sum(s.carriers.set_index("carrier") for s in summaries) / len(
            summaries
        )
        legs = sum(
            s.legs.set_index(["carrier", "leg_id", "flt_no", "orig", "dest"])
            for s in summaries
        ) / len(summaries)
        legs = legs.reset_index()
        paths = sum(
            s.paths.set_index(["orig", "dest", "carrier1", "leg_id1", "carrier2"])
            for s in summaries
        ) / len(summaries)

        def average(name):
            frames = []
            for s in summaries:
                frame = getattr(s, name)
                if frame is not None:
                    frames.append(frame)
            if frames:
                return sum(frames) / len(frames)
            return None

        fare_class_mix = average("fare_class_mix")
        leg_forecasts = average("leg_forecasts")
        path_forecasts = average("path_forecasts")
        bid_price_history = average("bid_price_history")
        displacement_history = average("displacement_history")
        demand_to_come_summary = average("demand_to_come_summary")

        # dataframes with count data that need to be summed
        def sum_count(name):
            frames = []
            for s in summaries:
                frame = getattr(s, name)
                if frame is not None:
                    frames.append(frame)
            if frames:
                return sum(frames)
            return None

        raw_load_factor_distribution = sum_count("raw_load_factor_distribution")
        leg_avg_load_factor_distribution = sum_count("leg_avg_load_factor_distribution")
        raw_fare_class_mix = sum_count("raw_fare_class_mix")
        leg_local_fraction_distribution = sum_count("leg_local_fraction_distribution")

        result = cls(
            demands=demands,
            legs=legs,
            paths=paths,
            carriers=carriers,
            fare_class_mix=fare_class_mix,
            leg_forecasts=leg_forecasts,
            path_forecasts=path_forecasts,
            carrier_history=carrier_history,
            bookings_by_timeframe=bookings_by_timeframe,
            segmentation_by_timeframe=segmentation_by_timeframe,
            bid_price_history=bid_price_history,
            displacement_history=displacement_history,
            demand_to_come=demand_to_come,
            demand_to_come_summary=demand_to_come_summary,
            leg_avg_load_factor_distribution=leg_avg_load_factor_distribution,
            leg_local_fraction_distribution=leg_local_fraction_distribution,
            raw_load_factor_distribution=raw_load_factor_distribution,
            raw_fare_class_mix=raw_fare_class_mix,
            n_total_samples=sum(s.n_total_samples for s in summaries),
        )
        result.meta_trials = summaries
        return result

    @classmethod
    def from_sqlite_glob(
        cls,
        pattern: str,
        make_indexes: bool | dict = False,
        additional: Collection[str | tuple] | str | None = None,
        *,
        load_config: bool = True,
        max_num_files: int = 9999,
    ):
        """
        Load and aggregate multiple summary tables from a glob pattern.

        Parameters
        ----------
        pattern : str
            A glob pattern to match the files to load.
        make_indexes
        additional
        load_config

        Returns
        -------
        SummaryTables
        """
        summarytables_is_deprecated()

        import glob

        cfg = None
        raw = []
        n = 0
        for filename in glob.glob(pattern):
            n += 1
            if n > max_num_files:
                continue
            raw.append(
                cls.from_sqlite(
                    filename,
                    make_indexes=make_indexes,
                    additional=additional,
                )
            )
            if cfg is None and load_config:
                cfg = raw[-1].cnx.load_configs(on_validation_error="ignore")
        if n > max_num_files:
            warnings.warn(
                f"Only loaded {max_num_files} of {n} files matching pattern",
                stacklevel=2,
            )
        try:
            result = cls.aggregate(raw)
        except Exception as e:
            logger.error("Error aggregating summary tables: %s", e)
            logger.exception(e)
            return raw
        if cfg is not None:
            result.config = cfg
        return result

    @classmethod
    def load_basic_table(self, db: database.Database, tablename: str):
        """Load a basic table"""
        logger.info("loading %s", tablename)
        return db.dataframe(f"SELECT * FROM {tablename}")

    def load_additional_tables(
        self,
        db: database.Database,
        scenario: str,
        burn_samples: int,
        additional: Collection[str | tuple] | str | None = (
            "fare_class_mix",
            "bookings_by_timeframe",
            "total_demand",
            "load_factor_distribution",
        ),
    ) -> None:
        """
        Load additional summary tables based on common queries.

        Parameters
        ----------
        db : Database
        scenario : str
        burn_samples : int
            The number of samples in the burn period.  The data from these samples
            is ignored in most common queries.
        additional : Collection[str | tuple] | str
            One or more additional tables to load.  If "*", then this will load
            all common queries supported by the configuration used during the
            simulation.
        """
        if isinstance(additional, str):
            if additional == "*":
                additional = set()
                cfg = db.load_configs(scenario)
                if "fare" in cfg.db.write_items:
                    additional.add("fare_class_mix")
                if "fare_final" in cfg.db.write_items:
                    additional.add("fare_class_mix")
                if "bookings" in cfg.db.write_items:
                    additional.add("bookings_by_timeframe")
                if "demand" in cfg.db.write_items:
                    additional.add("total_demand")
                    additional.add("demand_to_come")
                if "demand_final" in cfg.db.write_items:
                    additional.add("total_demand")
                if "bucket" in cfg.db.write_items:
                    additional.add("leg_forecasts")
                    additional.add("carrier_history")
                if "pathclass" in cfg.db.write_items:
                    additional.add("path_forecasts")
                    additional.add("local_and_flow_yields")
                if "pathclass_final" in cfg.db.write_items:
                    additional.add("local_and_flow_yields")
                if "leg" in cfg.db.write_items and cfg.db.store_leg_bid_prices:
                    additional.add("bid_price_history")
                if "leg" in cfg.db.write_items and cfg.db.store_displacements:
                    additional.add("displacement_history")
                if "leg" in cfg.db.write_items or "leg_final" in cfg.db.write_items:
                    additional.add("load_factor_distribution")
            else:
                additional = [additional]
        elif additional is None:
            additional = []

        if "fare_class_mix" in additional and db.is_open:
            logger.info("loading fare_class_mix")
            self.fare_class_mix = database.common_queries.fare_class_mix(
                db, scenario, burn_samples=burn_samples
            )
            if self.od_fare_class_mix:
                for orig, dest in list(self.od_fare_class_mix):
                    self.od_fare_class_mix[(orig, dest)] = (
                        database.common_queries.od_fare_class_mix(
                            db, orig, dest, scenario, burn_samples=burn_samples
                        )
                    )
        # load additional fare class mix tables
        for i in additional:
            if isinstance(i, tuple) and i[0] == "od_fare_class_mix" and db.is_open:
                orig, dest = i[1], i[2]
                if self.od_fare_class_mix is None:
                    self.od_fare_class_mix = {}
                logger.info(f"loading od_fare_class_mix({orig},{dest})")
                self.od_fare_class_mix[(orig, dest)] = (
                    database.common_queries.od_fare_class_mix(
                        db, orig, dest, scenario, burn_samples=burn_samples
                    )
                )

        for i in additional:
            cutoffs = None
            if i == "load_factor_distribution" and db.is_open:
                cutoffs = (0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95)  # default cutoffs
            elif (
                isinstance(i, tuple)
                and i[0] == "load_factor_distribution"
                and db.is_open
            ):
                cutoffs = ast.literal_eval(i[1])
            if cutoffs is not None:
                logger.info("loading load_factor_distribution")
                self.load_factor_distribution = (
                    database.common_queries.load_factor_distribution(
                        db,
                        scenario=scenario,
                        burn_samples=burn_samples,
                        cutoffs=cutoffs,
                    )
                )

        if "bookings_by_timeframe" in additional and db.is_open:
            logger.info("loading bookings_by_timeframe")
            self.bookings_by_timeframe = database.common_queries.bookings_by_timeframe(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "total_demand" in additional and db.is_open:
            logger.info("loading total_demand")
            self.total_demand = database.common_queries.total_demand(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "leg_forecasts" in additional and db.is_open:
            logger.info("loading leg_forecasts")
            self.leg_forecasts = database.common_queries.leg_forecasts(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "path_forecasts" in additional and db.is_open:
            logger.info("loading path_forecasts")
            self.path_forecasts = database.common_queries.path_forecasts(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "demand_to_come" in additional and db.is_open:
            logger.info("loading demand_to_come")
            self.demand_to_come = database.common_queries.demand_to_come(
                db, scenario=scenario
            )

        if "demand_to_come_summary" in additional and db.is_open:
            logger.info("loading demand_to_come_summary")
            self.demand_to_come_summary = (
                database.common_queries.demand_to_come_summary(db, scenario=scenario)
            )

        if "carrier_history" in additional and db.is_open:
            logger.info("loading carrier_history")
            self.carrier_history = database.common_queries.carrier_history(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "bid_price_history" in additional and db.is_open:
            logger.info("loading bid_price_history")
            self.bid_price_history = database.common_queries.bid_price_history(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "displacement_history" in additional and db.is_open:
            logger.info("loading displacement_history")
            self.displacement_history = database.common_queries.displacement_history(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "local_and_flow_yields" in additional and db.is_open:
            logger.info("loading local_and_flow_yields")
            self.local_and_flow_yields = database.common_queries.local_and_flow_yields(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "leg_local_and_flow_by_class" in additional and db.is_open:
            logger.info("loading leg_local_and_flow_by_class")
            self.leg_carried = database.common_queries.leg_local_and_flow_by_class(
                db, scenario=scenario, burn_samples=burn_samples
            )

        if "edgar" in additional and db.is_open:
            logger.info("loading edgar")
            self.edgar = database.common_queries.edgar(
                db, scenario=scenario, burn_samples=burn_samples
            )

    def __init__(
        self,
        *,
        name: str | None = "name?",
        config: Config | None = None,
        cnx: database.Database | None = None,
        sim: Simulation | None = None,
        class_dist: pd.DataFrame | None = None,
        demands: pd.DataFrame | None = None,
        fares: pd.DataFrame | None = None,
        legs: pd.DataFrame | None = None,
        paths: pd.DataFrame | None = None,
        path_classes: pd.DataFrame | None = None,
        carriers: pd.DataFrame | None = None,
        fare_class_mix: pd.DataFrame | None = None,
        load_factors: pd.DataFrame | None = None,
        bookings_by_timeframe: pd.DataFrame | None = None,
        segmentation_by_timeframe: pd.DataFrame | None = None,
        total_demand: float | None = None,
        od_fare_class_mix: dict[tuple[str, str], pd.DataFrame] | None = None,
        leg_forecasts: pd.DataFrame | None = None,
        path_forecasts: pd.DataFrame | None = None,
        carrier_history: pd.DataFrame | None = None,
        demand_to_come: pd.DataFrame | None = None,
        demand_to_come_summary: pd.DataFrame | None = None,
        bid_price_history: pd.DataFrame | None = None,
        displacement_history: pd.DataFrame | None = None,
        local_and_flow_yields: pd.DataFrame | None = None,
        leg_carried: pd.DataFrame | None = None,
        load_factor_distribution: pd.DataFrame | None = None,
        leg_avg_load_factor_distribution: pd.DataFrame | None = None,
        raw_load_factor_distribution: pd.DataFrame | None = None,
        raw_fare_class_mix: pd.DataFrame | None = None,
        leg_local_fraction_distribution: pd.DataFrame | None = None,
        local_fraction_by_place: pd.DataFrame | None = None,
        n_total_samples: int = 0,
    ):
        summarytables_is_deprecated()

        self.config = config
        """Configuration used in the simulation that generated the summary tables."""

        self.cnx = cnx
        """The database connection used to load the summary tables."""

        self.sim = sim
        """The simulation object that generated the summary tables."""

        self.class_dist = class_dist
        self.demands = demands
        self.fares = fares
        self.legs = legs
        self.paths = paths
        self.path_classes = path_classes
        self.carriers = carriers
        self.fare_class_mix = fare_class_mix
        self.od_fare_class_mix = od_fare_class_mix
        self.load_factors = load_factors
        self.bookings_by_timeframe = bookings_by_timeframe
        self.segmentation_by_timeframe = segmentation_by_timeframe
        self.total_demand = total_demand
        self.leg_forecasts = leg_forecasts
        self.path_forecasts = path_forecasts
        self.carrier_history = carrier_history
        self.demand_to_come = demand_to_come
        self.demand_to_come_summary = demand_to_come_summary
        self.bid_price_history = bid_price_history
        self.displacement_history = displacement_history
        self.local_and_flow_yields = local_and_flow_yields
        self.leg_carried = leg_carried
        self.load_factor_distribution = load_factor_distribution

        self.leg_avg_load_factor_distribution = leg_avg_load_factor_distribution
        """Leg average load factor distribution (integers 0-100)."""

        self.raw_load_factor_distribution = raw_load_factor_distribution
        """Total number of departures by carrier by load factor (integers 0-100)."""

        self.raw_fare_class_mix = raw_fare_class_mix
        """Total number of passengers by carrier by fare class."""

        self.leg_local_fraction_distribution = leg_local_fraction_distribution
        """Fraction of local passengers on each leg."""

        self.local_fraction_by_place = local_fraction_by_place
        """Fraction of local passengers by place."""

        self.n_total_samples = n_total_samples
        """Total number of sample departures simulated to create these summaries.

        This excludes any burn samples.
        """

    def to_records(self) -> dict[str, list[dict]]:
        """Convert all summary tables to a dictionary of records."""
        return {k: v.to_dict(orient="records") for (k, v) in self.__dict__.items()}

    def to_xlsx(self, filename: str | pathlib.Path) -> None:
        """Write summary tables to excel.

        Parameters
        ----------
        filename : Path-like
            The excel file to write.
        """
        if isinstance(filename, str):
            filename = pathlib.Path(filename)
        filename.parent.mkdir(exist_ok=True, parents=True)
        with pd.ExcelWriter(filename) as writer:
            for k, v in self.__dict__.items():
                if isinstance(v, pd.DataFrame):
                    v.to_excel(writer, sheet_name=k)

    def to_dataframe(self, table) -> pd.DataFrame:
        """Convert the summary tables to a individual dataframes."""
        sheet_count = 0
        for k, v in self.__dict__.items():
            if isinstance(v, pd.DataFrame):
                sheet_count += 1
                if sheet_count == table:
                    return v.assign(table=k)

        raise IndexError("There are fewer than", table, " DataFrames in the object")

    def aggregate_demand_history(self, by_segment: bool = True) -> pd.Series:
        """
        Total demand by sample, aggregated over all markets.

        Parameters
        ----------
        by_segment : bool, default True
            Aggregate by segment.  If false, segments are also aggregated.

        Returns
        -------
        pandas.Series
            Total demand, indexed by trial, sample, and segment
            (business/leisure).
        """
        groupbys = ["trial", "sample"]
        if by_segment:
            groupbys.append("segment")
        return self.demand_to_come.iloc[:, 0].groupby(groupbys, observed=False).sum()

    def demand_in_tf(self) -> pd.DataFrame | None:
        """History of demand arriving in each timeframe.

        This dataframe is derived from the `demand_to_come` dataframe
        by taking the sequential differences.
        """
        if self.demand_to_come is None:
            return None
        return self.demand_to_come.diff(-1, axis=1).iloc[:, :-1]

    def fig_carrier_mileage(self, raw_df: bool = False, report=None):
        """
        Figure showing ASM, RPM by carrier.

        ASM is available seat miles.  RPM is revenue passenger miles.

        Parameters
        ----------
        raw_df : bool, default False
            Return the raw data for this figure as a pandas DataFrame, instead
            of generating the figure itself.
        report : xmle.Reporter, optional
            Also append this figure to the given report.
        """
        df = (
            self.carriers.reset_index()[["carrier", "asm", "rpm"]]
            .set_index("carrier")
            .rename_axis(columns="measure")
            .unstack()
            .to_frame("value")
            .reset_index()
        )
        if raw_df:
            return df
        import altair as alt

        chart = alt.Chart(df, title="Carrier Loads")
        bars = chart.mark_bar().encode(
            x=alt.X("carrier:N", title="Carrier"),
            y=alt.Y("value", stack=None, title="miles"),
            color="measure",
            tooltip=["carrier", "measure", alt.Tooltip("value", format=".4s")],
        )
        text = chart.mark_text(
            dx=0,
            dy=5,
            color="white",
            baseline="top",
        ).encode(
            x=alt.X("carrier:N"),
            y=alt.Y("value").stack(None),
            text=alt.Text("value:Q", format=".4s"),
        )
        fig = (
            (bars + text)
            .properties(
                width=400,
                height=300,
            )
            .configure_axis(
                labelFontSize=12,
                titleFontSize=12,
            )
            .configure_legend(
                titleFontSize=12,
                labelFontSize=15,
            )
        )
        if report:
            report.add_figure(fig)
        return fig

    def _fig_fare_class_mix(
        self, df: pd.DataFrame, label_threshold: float = 0.06, title=None
    ):
        import altair as alt

        label_threshold_value = (
            df.groupby("carrier", observed=False).avg_sold.sum().max() * label_threshold
        )
        chart = alt.Chart(
            df, **({"title": title} if title else {})
        ).transform_calculate(
            halfsold="datum.avg_sold / 2.0",
        )
        bars = chart.mark_bar().encode(
            x=alt.X("carrier:N", title="Carrier"),
            y=alt.Y("avg_sold:Q", title="Seats").stack("zero"),
            color="booking_class",
            tooltip=[
                "carrier",
                "booking_class",
                alt.Tooltip("avg_sold", format=".2f"),
            ],
        )
        text = chart.mark_text(dx=0, dy=3, color="white", baseline="top").encode(
            x=alt.X("carrier:N", title="Carrier"),
            y=alt.Y("avg_sold:Q", title="Seats").stack("zero"),
            text=alt.Text("avg_sold:Q", format=".2f"),
            opacity=alt.condition(
                f"datum.avg_sold < {label_threshold_value:.3f}",
                alt.value(0),
                alt.value(1),
            ),
            order=alt.Order("booking_class:N", sort="descending"),
        )
        return (
            (bars + text)
            .properties(
                width=400,
                height=300,
            )
            .configure_axis(
                labelFontSize=12,
                titleFontSize=12,
            )
            .configure_legend(
                titleFontSize=12,
                labelFontSize=15,
            )
        )

    @report_figure
    def fig_fare_class_mix(self, raw_df=False, label_threshold=0.06):
        if self.fare_class_mix is not None:
            df = self.fare_class_mix.reset_index()[
                ["carrier", "booking_class", "avg_sold"]
            ]
        elif self.raw_fare_class_mix is not None and self.n_total_samples > 0:
            df = self.raw_fare_class_mix / self.n_total_samples
            df = df.rename(columns={"sold": "avg_sold"})
            df = df.reset_index()[["carrier", "booking_class", "avg_sold"]]
        else:
            return None

        if raw_df:
            return df
        return self._fig_fare_class_mix(
            df,
            label_threshold=label_threshold,
            title="Fare Class Mix",
        )

    @report_figure
    def fig_od_fare_class_mix(
        self, orig: str, dest: str, raw_df=False, label_threshold=0.06
    ):
        df = self.od_fare_class_mix[orig, dest].reset_index()[
            ["carrier", "booking_class", "avg_sold"]
        ]
        if raw_df:
            return df
        return self._fig_fare_class_mix(
            df, label_threshold=label_threshold, title=f"Fare Class Mix ({orig}-{dest})"
        )

    @report_figure
    def fig_load_factor_distribution(
        self,
        by_carrier: bool | str = True,
        breakpoints: Collection[int] = (
            50,
            55,
            60,
            65,
            70,
            75,
            80,
            85,
            90,
            95,
            100,
        ),
        source: Literal["leg_avg", "raw", "db"] = "leg_avg",
        raw_df=False,
    ):
        """
        Figure showing the distribution of leg load factors.

        Parameters
        ----------
        by_carrier : bool or str, default True
            If True, show the distribution by carrier.  If a string, show the
            distribution for that carrier. If False, show the distribution
            aggregated over all carriers.
        breakpoints : Collection[int, ...], default (50, 55, 60, 65, ..., 90, 95, 100)
            The breakpoints for the load factor ranges, which represent the lowest
            load factor value in each bin. The first and last breakpoints are always
            bounded to 0 and 101, respectively; these bounds can be included explicitly
            or omitted to be included implicitly. Setting the top value to 101 ensures
            that the highest load factor value (100) is included in the last bin.
        source : {"raw", "db"}, default "raw"
            The source of the data.  "raw" uses the raw load factor distribution
            output from the simulation, which is faster and preferred if available.
            "db" uses the older load factor distribution table, which is extracted
            as a query from the database.  This requires leg level departure (final)
            details to have been recorded in the database, but potentially allows
            arbitrary custom filters or transformations to be applied.
        raw_df : bool, default False
            Return the raw data for this figure as a pandas DataFrame, instead
            of generating the figure itself.

        Returns
        -------
        altair.Chart or pd.DataFrame
        """
        title = "Load Factor Frequency"  # default title
        if source == "raw" or source == "leg_avg":
            # Load using faster raw load factor data generated by the simulation
            # This is faster than loading from the database and now preferred
            if source == "raw":
                if self.raw_load_factor_distribution is None:
                    raise AttributeError(
                        "raw_load_factor_distribution not found, "
                        "it is required for using raw source data."
                    )
                df_for_chart = (
                    self.raw_load_factor_distribution.rename_axis(columns="carrier")
                    .stack(future_stack=True)
                    .rename("Count")
                    .reset_index()
                )
                title = "Raw Load Factor Frequency"
            elif source == "leg_avg":
                if self.leg_avg_load_factor_distribution is None:
                    raise AttributeError(
                        "leg_avg_load_factor_distribution not found, "
                        "it is required for using leg_avg source data."
                    )
                df_for_chart = (
                    self.leg_avg_load_factor_distribution.rename_axis(columns="carrier")
                    .stack(future_stack=True)
                    .rename("Count")
                    .reset_index()
                )
                title = "Leg Average Load Factor Frequency"
            if not isinstance(breakpoints, tuple):
                breakpoints = tuple(breakpoints)
            if breakpoints[0] <= 0:
                breakpoints = (-1,) + breakpoints[1:]
            else:
                breakpoints = (-1,) + breakpoints
            if breakpoints[-1] >= 101:
                breakpoints = breakpoints[:-1] + (101,)
            else:
                breakpoints = breakpoints + (101,)

            # Create labels for categories
            def make_label(i, j):
                if i == j - 1:
                    return f"{i}"
                else:
                    return f"{i}-{j-1}"

            labels = [make_label(0, breakpoints[1])]
            for i in range(1, len(breakpoints) - 2):
                labels += [make_label(breakpoints[i], breakpoints[i + 1])]
            if breakpoints[-2] < 100:
                labels += [make_label(breakpoints[-2], 101)]
            else:
                labels += ["100"]
            breaker = pd.cut(
                df_for_chart.leg_load_factor,
                bins=breakpoints,
                right=False,
                labels=labels,
            ).rename("Load Factor Range")
            df_for_chart = (
                df_for_chart.groupby(["carrier", breaker], observed=False)
                .Count.sum()
                .reset_index()
            )

        elif source == "db":
            # Older load factor distribution table, taken from database
            if not hasattr(self, "load_factor_distribution"):
                raise AttributeError(
                    "load_factor_distribution data not found. Please load it first."
                )

            df_for_chart = self.load_factor_distribution
            df_for_chart.columns.names = ["Load Factor Range"]
            df_for_chart = df_for_chart.set_index("carrier")
            df_for_chart = (
                df_for_chart.stack(future_stack=True).rename("Count").reset_index()
            )

        else:
            raise ValueError(f"Unknown source {source}, should be 'raw' or 'db'")

        if not by_carrier:
            df_for_chart = (
                df_for_chart.groupby(["Load Factor Range"], observed=False)
                .Count.sum()
                .reset_index()
            )
        elif isinstance(by_carrier, str):
            df_for_chart = df_for_chart[df_for_chart["carrier"] == by_carrier]
            df_for_chart = df_for_chart.drop(columns=["carrier"])

        if raw_df:
            return df_for_chart

        import altair as alt

        if by_carrier is True:
            chart = (
                alt.Chart(df_for_chart)
                .mark_bar()
                .encode(
                    x=alt.X("Load Factor Range", title="Load Factor Range"),
                    y=alt.Y("Count:Q", title="Count"),
                    facet=alt.Facet("carrier:N", columns=2, title="Carrier"),
                    tooltip=[
                        alt.Tooltip("carrier", title="Carrier"),
                        alt.Tooltip("Count", title="Count"),
                    ],
                )
                .properties(width=300, height=250, title=f"{title} by Carrier")
            )
        else:
            chart = (
                alt.Chart(df_for_chart)
                .mark_bar()
                .encode(
                    x=alt.X("Load Factor Range", title="Load Factor Range"),
                    y=alt.Y("Count:Q", title="Count"),
                )
                .properties(
                    width=600,
                    height=400,
                    title=title if not by_carrier else f"{title} ({by_carrier})",
                )
            )

        return chart

    def fig_leg_local_fraction_distribution(
        self,
        by_carrier: bool | str = True,
        breakpoints: Collection[int] = (
            50,
            55,
            60,
            65,
            70,
            75,
            80,
            85,
            90,
            95,
            100,
        ),
        raw_df=False,
    ):
        """
        Figure showing the distribution of fraction of leg passengers who are local.

        Parameters
        ----------
        by_carrier : bool or str, default True
            If True, show the distribution by carrier.  If a string, show the
            distribution for that carrier. If False, show the distribution
            aggregated over all carriers.
        breakpoints : Collection[int, ...], default (50, 55, 60, 65, ..., 90, 95, 100)
            The breakpoints for the local fraction ranges, which represent the lowest
            local fraction value in each bin. The first and last breakpoints are always
            bounded to 0 and 101, respectively; these bounds can be included explicitly
            or omitted to be included implicitly. Setting the top value to 101 ensures
            that the highest local fraction value (100) is included in the last bin.
        raw_df : bool, default False
            Return the raw data for this figure as a pandas DataFrame, instead
            of generating the figure itself.

        Returns
        -------
        altair.Chart or pd.DataFrame
        """
        title = "Local Fraction Frequency"  # default title
        if self.leg_local_fraction_distribution is None:
            raise AttributeError(
                "leg_local_fraction_distribution not found, "
                "it is required for using raw source data."
            )
        df_for_chart = (
            self.leg_local_fraction_distribution.rename_axis(columns="carrier")
            .stack(future_stack=True)
            .rename("Count")
            .reset_index()
        )
        if not isinstance(breakpoints, tuple):
            breakpoints = tuple(breakpoints)
        if breakpoints[0] <= 0:
            breakpoints = (-1,) + breakpoints[1:]
        else:
            breakpoints = (-1,) + breakpoints
        if breakpoints[-1] >= 101:
            breakpoints = breakpoints[:-1] + (101,)
        else:
            breakpoints = breakpoints + (101,)

        # Create labels for categories
        def make_label(i, j):
            if i == j - 1:
                return f"{i}"
            else:
                return f"{i}-{j-1}"

        labels = [make_label(0, breakpoints[1])]
        for i in range(1, len(breakpoints) - 2):
            labels += [make_label(breakpoints[i], breakpoints[i + 1])]
        if breakpoints[-2] < 100:
            labels += [make_label(breakpoints[-2], 101)]
        else:
            labels += ["100"]

        breaker = pd.cut(
            df_for_chart.local_fraction,
            bins=breakpoints,
            right=False,
            labels=labels,
        ).rename("Leg Local Fraction Range")
        df_for_chart = (
            df_for_chart.groupby(["carrier", breaker], observed=False)
            .Count.sum()
            .reset_index()
        )

        if not by_carrier:
            df_for_chart = (
                df_for_chart.groupby(["Leg Local Fraction Range"], observed=False)
                .Count.sum()
                .reset_index()
            )
        elif isinstance(by_carrier, str):
            df_for_chart = df_for_chart[df_for_chart["carrier"] == by_carrier]
            df_for_chart = df_for_chart.drop(columns=["carrier"])

        if raw_df:
            return df_for_chart

        import altair as alt

        if by_carrier is True:
            chart = (
                alt.Chart(df_for_chart)
                .mark_bar()
                .encode(
                    x=alt.X(
                        "Leg Local Fraction Range", title="Leg Local Fraction Range"
                    ),
                    y=alt.Y("Count:Q", title="Count"),
                    facet=alt.Facet("carrier:N", columns=2, title="Carrier"),
                    tooltip=[
                        alt.Tooltip("carrier", title="Carrier"),
                        alt.Tooltip("Count", title="Count"),
                    ],
                )
                .properties(width=300, height=250, title=f"{title} by Carrier")
            )
        else:
            chart = (
                alt.Chart(df_for_chart)
                .mark_bar()
                .encode(
                    x=alt.X(
                        "Leg Local Fraction Range", title="Leg Local Fraction Range"
                    ),
                    y=alt.Y("Count:Q", title="Count"),
                )
                .properties(
                    width=600,
                    height=400,
                    title=title if not by_carrier else f"{title} ({by_carrier})",
                )
            )
        return chart

    @report_figure
    def fig_bookings_by_timeframe(
        self,
        by_carrier: bool | str = True,
        by_class: bool | str = False,
        raw_df: bool = False,
        errorbands: bool = False,
        exclude_nogo: bool = True,
    ):
        if errorbands:
            if by_carrier is True:
                raise NotImplementedError("error bands for all carriers is messy")
            return self._fig_bookings_by_timeframe_errorband(
                by_carrier=by_carrier, raw_df=raw_df
            )

        def differs(x):
            return x.shift(-1, fill_value=0) - x

        def _summarize(x: pd.DataFrame, c: str):
            if "trial" not in x.columns:
                x = x.assign(trial=0)
            if by_class:
                y = (
                    x.groupby(
                        ["trial", "carrier", "booking_class", "days_prior"],
                        observed=False,
                    )[f"avg_{c}"]
                    .sum()
                    .unstack(["trial", "carrier", "booking_class"])
                    .sort_index(ascending=False)
                    .apply(differs)
                    .stack(["carrier", "booking_class"], future_stack=True)
                    .aggregate(["mean", "sem"], axis=1)
                    .assign(
                        ci0=lambda x: np.maximum(x["mean"] - 1.96 * x["sem"], 0),
                        ci1=lambda x: x["mean"] + 1.96 * x["sem"],
                    )
                )
            else:
                y = (
                    x.groupby(["trial", "carrier", "days_prior"], observed=False)[
                        f"avg_{c}"
                    ]
                    .sum()
                    .unstack(["trial", "carrier"])
                    .sort_index(ascending=False)
                    .apply(differs)
                    .stack("carrier", future_stack=True)
                    .aggregate(["mean", "sem"], axis=1)
                    .assign(
                        ci0=lambda x: np.maximum(x["mean"] - 1.96 * x["sem"], 0),
                        ci1=lambda x: x["mean"] + 1.96 * x["sem"],
                    )
                )
            return pd.concat({c: y}, names=["paxtype"])

        if self.bookings_by_timeframe is None:
            raise ValueError("bookings_by_timeframe not found")
        bookings_by_timeframe = self.bookings_by_timeframe.reset_index()
        df0 = _summarize(bookings_by_timeframe, "business")
        df1 = _summarize(bookings_by_timeframe, "leisure")
        df = (
            pd.concat([df0, df1], axis=0)
            .rename(columns={"mean": "sold"})
            .reset_index()
            .query("(days_prior>0) & (sold>0)")
        )
        title = "Bookings by Timeframe"
        if by_class is True:
            title = "Bookings by Timeframe and Booking Class"
        title_annot = []
        if not by_carrier:
            g = ["days_prior", "paxtype"]
            if by_class:
                g += ["booking_class"]
            df = (
                df.groupby(g, observed=False)[["sold", "ci0", "ci1"]]
                .sum()
                .reset_index()
            )
        if isinstance(by_carrier, str):
            df = df[df["carrier"] == by_carrier]
            df = df.drop(columns=["carrier"])
            title_annot.append(by_carrier)
            by_carrier = False
        if isinstance(by_class, str):
            df = df[df["booking_class"] == by_class]
            df = df.drop(columns=["booking_class"])
            title_annot.append(f"Class {by_class}")
            by_class = False
        if title_annot:
            title = f"{title} ({', '.join(title_annot)})"
        if exclude_nogo and "carrier" in df.columns:
            df = df[df["carrier"] != "NONE"]
        if raw_df:
            return df

        import altair as alt

        if by_carrier:
            color = "carrier:N"
            color_title = "Carrier"
        elif by_class:
            color = "booking_class:N"
            color_title = "Booking Class"
        else:
            color = "paxtype:N"
            color_title = "Passenger Type"

        if by_class:
            chart = (
                alt.Chart(df)
                .mark_bar()
                .encode(
                    color=alt.Color(color).title(color_title),
                    x=alt.X("days_prior:O")
                    .scale(reverse=True)
                    .title("Days Prior to Departure"),
                    y=alt.Y("sold"),
                    tooltip=(
                        [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                    )
                    + [
                        alt.Tooltip("paxtype", title="Passenger Type"),
                        alt.Tooltip("days_prior", title="DfD"),
                        alt.Tooltip("sold", format=".2f"),
                    ],
                )
                .properties(
                    width=500,
                    height=200,
                )
                .facet(
                    row=alt.Row("paxtype:N", title="Passenger Type"),
                    title=title,
                )
            )
        else:
            chart = (
                alt.Chart(df, title=title)
                .mark_line()
                .encode(
                    color=alt.Color(color).title(color_title),
                    x=alt.X("days_prior:O")
                    .scale(reverse=True)
                    .title("Days Prior to Departure"),
                    y=alt.Y("sold") if by_class else "sold",
                    strokeDash=alt.StrokeDash("paxtype").title("Passenger Type"),
                    tooltip=(
                        [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                    )
                    + [
                        alt.Tooltip("paxtype", title="Passenger Type"),
                        alt.Tooltip("days_prior", title="DfD"),
                        alt.Tooltip("sold", format=".2f"),
                    ],
                )
                .properties(
                    width=500,
                    height=300,
                )
                .configure_axis(
                    labelFontSize=12,
                    titleFontSize=12,
                )
                .configure_legend(
                    titleFontSize=12,
                    labelFontSize=15,
                )
            )
        return chart

    def _fig_bookings_by_timeframe_errorband(
        self, by_carrier: bool | str = True, raw_df=False
    ):
        def differs(x):
            return x.shift(-1, fill_value=0) - x

        b = self.bookings_by_timeframe.reset_index()

        def _summarize(x, c):
            y = (
                x.groupby(["trial", "carrier", "days_prior"], observed=False)[
                    f"avg_{c}"
                ]
                .sum()
                .unstack(["trial", "carrier"])
                .sort_index(ascending=False)
                .apply(differs)
                .stack("carrier", future_stack=True)
                .aggregate(["mean", "sem"], axis=1)
                .assign(
                    ci0=lambda x: x["mean"] - 1.96 * x["sem"],
                    ci1=lambda x: x["mean"] + 1.96 * x["sem"],
                )
            )
            return pd.concat({c: y}, names=["paxtype"])

        df0 = _summarize(b, "business")
        df1 = _summarize(b, "leisure")
        df = (
            pd.concat([df0, df1], axis=0)
            .rename(columns={"mean": "sold"})
            .reset_index()
            .query("days_prior>0")
        )
        if not by_carrier:
            df = (
                df.groupby(["days_prior", "paxtype"], observed=False)[
                    ["sold", "ci0", "ci1"]
                ]
                .sum()
                .reset_index()
            )
        if isinstance(by_carrier, str):
            df = df[df["carrier"] == by_carrier]
            df = df.drop(columns=["carrier"])
            by_carrier = False
        if raw_df:
            return df
        import altair as alt

        chart = alt.Chart(df)
        lines = chart.mark_line().encode(
            color=alt.Color("carrier:N" if by_carrier else "paxtype").title(
                "Carrier" if by_carrier else "Passenger Type"
            ),
            x=alt.X("days_prior:O")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y="sold",
            strokeDash=alt.StrokeDash("paxtype").title("Passenger Type"),
            tooltip=([alt.Tooltip("carrier").title("Carrier")] if by_carrier else [])
            + [
                alt.Tooltip("paxtype", title="Passenger Type"),
                alt.Tooltip("days_prior", title="DfD"),
                alt.Tooltip("sold", format=".2f"),
            ],
        )
        bands = chart.mark_errorband().encode(
            color=alt.Color(
                "carrier:N" if by_carrier else "paxtype",
                title="Carrier" if by_carrier else "Passenger Type",
            ),
            x=alt.X("days_prior:O")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y="ci0",
            y2="ci1",
            strokeDash=alt.StrokeDash("paxtype").title("Passenger Type"),
        )

        return (
            (lines + bands)
            .properties(
                width=500,
                height=300,
            )
            .configure_axis(
                labelFontSize=12,
                titleFontSize=12,
            )
            .configure_legend(
                titleFontSize=12,
                labelFontSize=15,
            )
        )

    @report_figure
    def fig_segmentation_by_timeframe(
        self,
        metric: Literal["bookings", "revenue"],
        by_carrier: bool | str = True,
        by_class: bool | str = False,
        raw_df: bool = False,
        exclude_nogo: bool = True,
    ):
        if self.segmentation_by_timeframe is None:
            raise ValueError("segmentation_by_timeframe not found")
        df = self.segmentation_by_timeframe
        idxs = list(df.index.names)
        if "trial" in idxs:
            idxs.remove("trial")
            df = df.groupby(idxs).mean()
        df = df[metric].stack().rename(metric).reset_index()

        title = f"{metric.title()} by Timeframe"
        if by_class is True:
            title = f"{metric.title()} by Timeframe and Booking Class"
        title_annot = []
        if not by_carrier:
            g = ["days_prior", "segment"]
            if by_class:
                g += ["booking_class"]
            df = df.groupby(g, observed=False)[[metric]].sum().reset_index()
        if by_carrier and not by_class:
            df = (
                df.groupby(["carrier", "days_prior", "segment"], observed=False)[
                    [metric]
                ]
                .sum()
                .reset_index()
            )
        if isinstance(by_carrier, str):
            df = df[df["carrier"] == by_carrier]
            df = df.drop(columns=["carrier"])
            title_annot.append(by_carrier)
            by_carrier = False
        if isinstance(by_class, str):
            df = df[df["booking_class"] == by_class]
            df = df.drop(columns=["booking_class"])
            title_annot.append(f"Class {by_class}")
            by_class = False
        if title_annot:
            title = f"{title} ({', '.join(title_annot)})"
        if exclude_nogo and "carrier" in df.columns:
            df = df[df["carrier"] != "NONE"]
        if raw_df:
            return df

        import altair as alt

        if by_carrier:
            color = "carrier:N"
            color_title = "Carrier"
        elif by_class:
            color = "booking_class:N"
            color_title = "Booking Class"
        else:
            color = "segment:N"
            color_title = "Passenger Type"

        if metric == "revenue":
            metric_fmt = "$,.0f"
        else:
            metric_fmt = ",.2f"

        chart = (
            alt.Chart(df)
            .mark_bar()
            .encode(
                color=alt.Color(color).title(color_title),
                x=alt.X("days_prior:O")
                .scale(reverse=True)
                .title("Days Prior to Departure"),
                y=alt.Y(metric),
                tooltip=(
                    [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                )
                + (
                    [alt.Tooltip("booking_class").title("Booking Class")]
                    if by_class
                    else []
                )
                + [
                    alt.Tooltip("segment", title="Passenger Type"),
                    alt.Tooltip("days_prior", title="Days Prior"),
                    alt.Tooltip(metric, format=metric_fmt, title=metric.title()),
                ],
            )
            .properties(
                width=500,
                height=200,
            )
        )
        if by_carrier or by_class:
            chart = chart.facet(
                row=alt.Row("segment:N", title="Passenger Type"),
                title=title,
            )
        return chart

    def _fig_carrier_load_factors(
        self,
        raw_df: bool,
        load_measure: str,
        measure_name: str,
        measure_format: str = ".2f",
        orient: Literal["h", "v"] = "h",
        title: str | None = None,
    ):
        df = self.carriers.reset_index()[["carrier", load_measure]]
        if raw_df:
            return df
        import altair as alt

        chart = alt.Chart(df)
        if orient == "v":
            bars = chart.mark_bar().encode(
                x=alt.X("carrier:N", title="Carrier"),
                y=alt.Y(f"{load_measure}:Q", title=measure_name).stack("zero"),
                tooltip=[
                    alt.Tooltip("carrier", title="Carrier"),
                    alt.Tooltip(
                        f"{load_measure}:Q", title=measure_name, format=measure_format
                    ),
                ],
            )
            text = chart.mark_text(dx=0, dy=3, color="white", baseline="top").encode(
                x=alt.X("carrier:N", title="Carrier"),
                y=alt.Y(f"{load_measure}:Q", title=measure_name).stack("zero"),
                text=alt.Text(f"{load_measure}:Q", format=measure_format),
            )
        else:
            bars = chart.mark_bar().encode(
                y=alt.Y("carrier:N", title="Carrier"),
                x=alt.X(f"{load_measure}:Q", title=measure_name).stack("zero"),
                tooltip=[
                    alt.Tooltip("carrier", title="Carrier"),
                    alt.Tooltip(
                        f"{load_measure}:Q", title=measure_name, format=measure_format
                    ),
                ],
            )
            text = chart.mark_text(
                dx=-5, dy=0, color="white", baseline="middle", align="right"
            ).encode(
                y=alt.Y("carrier:N", title="Carrier"),
                x=alt.X(f"{load_measure}:Q", title=measure_name).stack("zero"),
                text=alt.Text(f"{load_measure}:Q", format=measure_format),
            )
        fig = (
            (bars + text)
            .properties(
                width=500,
                height=10 + 20 * len(df),
            )
            .configure_axis(
                labelFontSize=12,
                titleFontSize=12,
            )
            .configure_legend(
                titleFontSize=12,
                labelFontSize=15,
            )
        )
        if title:
            fig.title = title
        return fig

    @report_figure
    def fig_carrier_load_factors(
        self, raw_df=False, load_measure: Literal["sys_lf", "avg_leg_lf"] = "sys_lf"
    ):
        measure_name = (
            "System Load Factor" if load_measure == "sys_lf" else "Leg Load Factor"
        )
        return self._fig_carrier_load_factors(
            raw_df,
            load_measure,
            measure_name,
            title=f"Carrier {measure_name}s",
        )

    @report_figure
    def fig_carrier_revenues(self, raw_df=False):
        return self._fig_carrier_load_factors(
            raw_df, "avg_rev", "Average Revenue", "$.4s", title="Carrier Revenues"
        )

    @report_figure
    def fig_carrier_yields(self, raw_df=False):
        return self._fig_carrier_load_factors(
            raw_df, "yield", "Average Yield", "$.4f", title="Carrier Yields"
        )

    @report_figure
    def fig_carrier_total_bookings(self, raw_df=False):
        return self._fig_carrier_load_factors(
            raw_df, "sold", "Total Bookings", ".4s", title="Carrier Total Bookings"
        )

    def _fig_forecasts(
        self,
        df,
        facet_on=None,
        y="forecast_mean",
        color="booking_class:N",
        y_title="Avg Demand Forecast",
    ):
        import altair as alt

        encoding = dict(
            x=alt.X("days_prior:O")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y=alt.Y(f"{y}:Q", title=y_title),
        )
        if color:
            encoding["color"] = color
        if not facet_on:
            return alt.Chart(df).mark_line().encode(**encoding)
        else:
            return (
                alt.Chart(df)
                .mark_line()
                .encode(**encoding)
                .facet(
                    facet=f"{facet_on}:N",
                    columns=3,
                )
            )

    @report_figure
    def fig_leg_forecasts(
        self,
        by_leg_id: bool | int = True,
        by_class: bool | str = True,
        of: Literal["mu", "sigma"] | list[Literal["mu", "sigma"]] = "mu",
        raw_df=False,
    ):
        if isinstance(of, list):
            if raw_df:
                raise NotImplementedError
            fig = self.fig_leg_forecasts(
                by_leg_id=by_leg_id,
                by_class=by_class,
                of=of[0],
            )
            for of_ in of[1:]:
                fig |= self.fig_leg_forecasts(
                    by_leg_id=by_leg_id,
                    by_class=by_class,
                    of=of_,
                )
            return fig
        y = "forecast_mean" if of == "mu" else "forecast_stdev"
        columns = [
            "carrier",
            "leg_id",
            "booking_class",
            "days_prior",
            y,
        ]
        if self.leg_forecasts is None:
            raise ValueError("the leg_forecasts summary table is not available")
        df = self.leg_forecasts.reset_index()[columns]
        color = "booking_class:N"
        if isinstance(by_leg_id, int) and by_leg_id is not True:
            df = df[df.leg_id == by_leg_id]
        if isinstance(by_class, str):
            df = df[df.booking_class == by_class]
            color = None
        if raw_df:
            return df
        return self._fig_forecasts(
            df,
            facet_on=None,
            y=y,
            color=color,
            y_title="Mean Demand Forecast" if of == "mu" else "Std Dev Demand Forecast",
        )

    @report_figure
    def fig_path_forecasts(
        self,
        by_path_id: bool | int = True,
        by_class: bool | str = True,
        of: Literal["mu", "sigma", "closed", "adj_price"] = "mu",
        raw_df=False,
    ):
        if self.path_forecasts is None:
            raise ValueError("the path_forecasts summary table is not available")
        of_columns = {
            "mu": "forecast_mean",
            "sigma": "forecast_stdev",
            "closed": "forecast_closed_in_tf",
            "adj_price": "adjusted_price",
        }
        y = of_columns.get(of)
        columns = [
            "path_id",
            "booking_class",
            "days_prior",
            y,
        ]
        df = self.path_forecasts.reset_index()[columns]
        color = "booking_class:N"
        if isinstance(by_path_id, int) and by_path_id is not True:
            df = df[df.path_id == by_path_id]
        if isinstance(by_class, str):
            df = df[df.booking_class == by_class]
            color = None
        if raw_df:
            return df
        facet_on = None
        if by_path_id is True:
            facet_on = "path_id"
        return self._fig_forecasts(df, facet_on=facet_on, y=y, color=color)

    @report_figure
    def fig_bid_price_history(
        self,
        by_carrier: bool | str = True,
        show_stdev: float | bool | None = None,
        cap: Literal["some", "zero", None] = None,
        raw_df=False,
    ):
        if cap is None:
            bp_mean = "bid_price_mean"
        elif cap == "some":
            bp_mean = "some_cap_bid_price_mean"
        elif cap == "zero":
            bp_mean = "zero_cap_bid_price_mean"
        else:
            raise ValueError(f"cap={cap!r} not in ['some', 'zero', None]")
        df = self.bid_price_history.reset_index()
        color = None
        if isinstance(by_carrier, str):
            df = df[df.carrier == by_carrier]
        elif by_carrier:
            color = "carrier:N"
            if show_stdev is None:
                show_stdev = False
        if show_stdev:
            if show_stdev is True:
                show_stdev = 2
            df["bid_price_upper"] = df[bp_mean] + show_stdev * df["bid_price_stdev"]
            df["bid_price_lower"] = (
                df[bp_mean] - show_stdev * df["bid_price_stdev"]
            ).clip(0, None)
        if raw_df:
            return df

        import altair as alt

        line_encoding = dict(
            x=alt.X("days_prior:Q")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y=alt.Y(bp_mean, title="Bid Price"),
        )
        if color:
            line_encoding["color"] = color
        chart = alt.Chart(df)
        fig = chart.mark_line(interpolate="step-before").encode(**line_encoding)
        if show_stdev:
            area_encoding = dict(
                x=alt.X("days_prior:Q")
                .scale(reverse=True)
                .title("Days Prior to Departure"),
                y=alt.Y("bid_price_lower:Q", title="Bid Price"),
                y2=alt.Y2("bid_price_upper:Q", title="Bid Price"),
            )
            bound = chart.mark_area(
                opacity=0.1,
                interpolate="step-before",
            ).encode(**area_encoding)
            bound_line = chart.mark_line(
                opacity=0.4, strokeDash=[5, 5], interpolate="step-before"
            ).encode(
                x=alt.X("days_prior:Q")
                .scale(reverse=True)
                .title("Days Prior to Departure")
            )
            top_line = bound_line.encode(
                y=alt.Y("bid_price_lower:Q", title="Bid Price")
            )
            bottom_line = bound_line.encode(
                y=alt.Y("bid_price_upper:Q", title="Bid Price")
            )
            fig = fig + bound + top_line + bottom_line
        return fig

    @report_figure
    def fig_displacement_history(
        self,
        by_carrier: bool | str = True,
        show_stdev: float | bool | None = None,
        raw_df=False,
    ):
        df = self.displacement_history.reset_index()
        color = None
        if isinstance(by_carrier, str):
            df = df[df.carrier == by_carrier]
        elif by_carrier:
            color = "carrier:N"
            if show_stdev is None:
                show_stdev = False
        if show_stdev:
            if show_stdev is True:
                show_stdev = 2
            df["displacement_upper"] = (
                df["displacement_mean"] + show_stdev * df["displacement_stdev"]
            )
            df["displacement_lower"] = (
                df["displacement_mean"] - show_stdev * df["displacement_stdev"]
            ).clip(0, None)
        if raw_df:
            return df

        import altair as alt

        line_encoding = dict(
            x=alt.X("days_prior:Q")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y=alt.Y("displacement_mean", title="Displacement Cost"),
        )
        if color:
            line_encoding["color"] = color
        chart = alt.Chart(df)
        fig = chart.mark_line(interpolate="step-before").encode(**line_encoding)
        if show_stdev:
            area_encoding = dict(
                x=alt.X("days_prior:Q")
                .scale(reverse=True)
                .title("Days Prior to Departure"),
                y=alt.Y("displacement_lower:Q", title="Displacement Cost"),
                y2=alt.Y2("displacement_upper:Q", title="Displacement Cost"),
            )
            bound = chart.mark_area(
                opacity=0.1,
                interpolate="step-before",
            ).encode(**area_encoding)
            bound_line = chart.mark_line(
                opacity=0.4, strokeDash=[5, 5], interpolate="step-before"
            ).encode(
                x=alt.X("days_prior:Q")
                .scale(reverse=True)
                .title("Days Prior to Departure")
            )
            top_line = bound_line.encode(
                y=alt.Y("displacement_lower:Q", title="Displacement Cost")
            )
            bottom_line = bound_line.encode(
                y=alt.Y("displacement_upper:Q", title="Displacement Cost")
            )
            fig = fig + bound + top_line + bottom_line
        return fig

bid_price_history instance-attribute

bid_price_history = bid_price_history

bookings_by_timeframe instance-attribute

bookings_by_timeframe = bookings_by_timeframe

carrier_history instance-attribute

carrier_history = carrier_history

carriers instance-attribute

carriers = carriers

class_dist instance-attribute

class_dist = class_dist

cnx instance-attribute

cnx = cnx

The database connection used to load the summary tables.

config instance-attribute

config = config

Configuration used in the simulation that generated the summary tables.

demand_to_come instance-attribute

demand_to_come = demand_to_come

demand_to_come_summary instance-attribute

demand_to_come_summary = demand_to_come_summary

demands instance-attribute

demands = demands

displacement_history instance-attribute

displacement_history = displacement_history

fare_class_mix instance-attribute

fare_class_mix = fare_class_mix

fares instance-attribute

fares = fares

leg_avg_load_factor_distribution instance-attribute

leg_avg_load_factor_distribution = (
    leg_avg_load_factor_distribution
)

Leg average load factor distribution (integers 0-100).

leg_carried instance-attribute

leg_carried = leg_carried

leg_forecasts instance-attribute

leg_forecasts = leg_forecasts

leg_local_fraction_distribution instance-attribute

leg_local_fraction_distribution = (
    leg_local_fraction_distribution
)

Fraction of local passengers on each leg.

legs instance-attribute

legs = legs

load_factor_distribution instance-attribute

load_factor_distribution = load_factor_distribution

load_factors instance-attribute

load_factors = load_factors

local_and_flow_yields instance-attribute

local_and_flow_yields = local_and_flow_yields

local_fraction_by_place instance-attribute

local_fraction_by_place = local_fraction_by_place

Fraction of local passengers by place.

n_total_samples instance-attribute

n_total_samples = n_total_samples

Total number of sample departures simulated to create these summaries.

This excludes any burn samples.

od_fare_class_mix instance-attribute

od_fare_class_mix = od_fare_class_mix

path_classes instance-attribute

path_classes = path_classes

path_forecasts instance-attribute

path_forecasts = path_forecasts

paths instance-attribute

paths = paths

raw_fare_class_mix instance-attribute

raw_fare_class_mix = raw_fare_class_mix

Total number of passengers by carrier by fare class.

raw_load_factor_distribution instance-attribute

raw_load_factor_distribution = raw_load_factor_distribution

Total number of departures by carrier by load factor (integers 0-100).

segmentation_by_timeframe instance-attribute

segmentation_by_timeframe = segmentation_by_timeframe

sim instance-attribute

sim = sim

The simulation object that generated the summary tables.

total_demand instance-attribute

total_demand = total_demand

__getstate__

__getstate__()
Source code in passengersim/summary.py
def __getstate__(self):
    state = self.__dict__.copy()
    if "cnx" in state:
        del state["cnx"]
    if "config" in state:
        # state["_config_yaml"] = state["config"].to_yaml()
        del state["config"]
    if "meta_trials" in state and not state.get("_preserve_meta_trials", True):
        del state["meta_trials"]
    if "_preserve_meta_trials" in state:
        del state["_preserve_meta_trials"]
    return state

__init__

__init__(
    *,
    name: str | None = "name?",
    config: Config | None = None,
    cnx: Database | None = None,
    sim: Simulation | None = None,
    class_dist: DataFrame | None = None,
    demands: DataFrame | None = None,
    fares: DataFrame | None = None,
    legs: DataFrame | None = None,
    paths: DataFrame | None = None,
    path_classes: DataFrame | None = None,
    carriers: DataFrame | None = None,
    fare_class_mix: DataFrame | None = None,
    load_factors: DataFrame | None = None,
    bookings_by_timeframe: DataFrame | None = None,
    segmentation_by_timeframe: DataFrame | None = None,
    total_demand: float | None = None,
    od_fare_class_mix: dict[tuple[str, str], DataFrame]
    | None = None,
    leg_forecasts: DataFrame | None = None,
    path_forecasts: DataFrame | None = None,
    carrier_history: DataFrame | None = None,
    demand_to_come: DataFrame | None = None,
    demand_to_come_summary: DataFrame | None = None,
    bid_price_history: DataFrame | None = None,
    displacement_history: DataFrame | None = None,
    local_and_flow_yields: DataFrame | None = None,
    leg_carried: DataFrame | None = None,
    load_factor_distribution: DataFrame | None = None,
    leg_avg_load_factor_distribution: DataFrame
    | None = None,
    raw_load_factor_distribution: DataFrame | None = None,
    raw_fare_class_mix: DataFrame | None = None,
    leg_local_fraction_distribution: DataFrame
    | None = None,
    local_fraction_by_place: DataFrame | None = None,
    n_total_samples: int = 0,
)
Source code in passengersim/summary.py
def __init__(
    self,
    *,
    name: str | None = "name?",
    config: Config | None = None,
    cnx: database.Database | None = None,
    sim: Simulation | None = None,
    class_dist: pd.DataFrame | None = None,
    demands: pd.DataFrame | None = None,
    fares: pd.DataFrame | None = None,
    legs: pd.DataFrame | None = None,
    paths: pd.DataFrame | None = None,
    path_classes: pd.DataFrame | None = None,
    carriers: pd.DataFrame | None = None,
    fare_class_mix: pd.DataFrame | None = None,
    load_factors: pd.DataFrame | None = None,
    bookings_by_timeframe: pd.DataFrame | None = None,
    segmentation_by_timeframe: pd.DataFrame | None = None,
    total_demand: float | None = None,
    od_fare_class_mix: dict[tuple[str, str], pd.DataFrame] | None = None,
    leg_forecasts: pd.DataFrame | None = None,
    path_forecasts: pd.DataFrame | None = None,
    carrier_history: pd.DataFrame | None = None,
    demand_to_come: pd.DataFrame | None = None,
    demand_to_come_summary: pd.DataFrame | None = None,
    bid_price_history: pd.DataFrame | None = None,
    displacement_history: pd.DataFrame | None = None,
    local_and_flow_yields: pd.DataFrame | None = None,
    leg_carried: pd.DataFrame | None = None,
    load_factor_distribution: pd.DataFrame | None = None,
    leg_avg_load_factor_distribution: pd.DataFrame | None = None,
    raw_load_factor_distribution: pd.DataFrame | None = None,
    raw_fare_class_mix: pd.DataFrame | None = None,
    leg_local_fraction_distribution: pd.DataFrame | None = None,
    local_fraction_by_place: pd.DataFrame | None = None,
    n_total_samples: int = 0,
):
    summarytables_is_deprecated()

    self.config = config
    """Configuration used in the simulation that generated the summary tables."""

    self.cnx = cnx
    """The database connection used to load the summary tables."""

    self.sim = sim
    """The simulation object that generated the summary tables."""

    self.class_dist = class_dist
    self.demands = demands
    self.fares = fares
    self.legs = legs
    self.paths = paths
    self.path_classes = path_classes
    self.carriers = carriers
    self.fare_class_mix = fare_class_mix
    self.od_fare_class_mix = od_fare_class_mix
    self.load_factors = load_factors
    self.bookings_by_timeframe = bookings_by_timeframe
    self.segmentation_by_timeframe = segmentation_by_timeframe
    self.total_demand = total_demand
    self.leg_forecasts = leg_forecasts
    self.path_forecasts = path_forecasts
    self.carrier_history = carrier_history
    self.demand_to_come = demand_to_come
    self.demand_to_come_summary = demand_to_come_summary
    self.bid_price_history = bid_price_history
    self.displacement_history = displacement_history
    self.local_and_flow_yields = local_and_flow_yields
    self.leg_carried = leg_carried
    self.load_factor_distribution = load_factor_distribution

    self.leg_avg_load_factor_distribution = leg_avg_load_factor_distribution
    """Leg average load factor distribution (integers 0-100)."""

    self.raw_load_factor_distribution = raw_load_factor_distribution
    """Total number of departures by carrier by load factor (integers 0-100)."""

    self.raw_fare_class_mix = raw_fare_class_mix
    """Total number of passengers by carrier by fare class."""

    self.leg_local_fraction_distribution = leg_local_fraction_distribution
    """Fraction of local passengers on each leg."""

    self.local_fraction_by_place = local_fraction_by_place
    """Fraction of local passengers by place."""

    self.n_total_samples = n_total_samples
    """Total number of sample departures simulated to create these summaries.

    This excludes any burn samples.
    """

__setstate__

__setstate__(state)
Source code in passengersim/summary.py
def __setstate__(self, state):
    # if "_config_yaml" in state:
    #     state["config"] = Config.from_raw_yaml(state.pop("_config_yaml"))
    self.__dict__.update(state)

aggregate classmethod

aggregate(summaries: Collection[SummaryTables])

Aggregate multiple summary tables.

Source code in passengersim/summary.py
@classmethod
def aggregate(cls, summaries: Collection[SummaryTables]):
    """Aggregate multiple summary tables."""
    summarytables_is_deprecated()

    if not summaries:
        return None

    # dataframes where trial is in the index, just concatenate
    def concat(name):
        frames = []
        for s in summaries:
            frame = getattr(s, name)
            if frame is not None:
                frames.append(frame)
        if frames:
            return pd.concat(frames)
        return None

    carrier_history = concat("carrier_history")
    bookings_by_timeframe = concat("bookings_by_timeframe")
    segmentation_by_timeframe = concat("segmentation_by_timeframe")
    demand_to_come = concat("demand_to_come")

    # demands has some columns that are averages and some that are sums
    demands_avg = sum(
        s.demands.set_index(["orig", "dest", "segment"])[
            ["sold", "revenue", "avg_fare"]
        ]
        for s in summaries
    ) / len(summaries)
    demands_sum = sum(
        s.demands.set_index(["orig", "dest", "segment"])[
            ["gt_demand", "gt_sold", "gt_revenue"]
        ]
        for s in summaries
    )
    demands = pd.concat([demands_avg, demands_sum], axis=1).reset_index()

    # TODO: aggregate fares
    # fares has some columns that are (weighted) averages and some that are sums
    # fares_gt_adj_price = sum(
    #     s.fares.set_index(
    #     ["carrier", "orig", "dest", "booking_class", "dcp_index"]
    #     ).eval("gt_sold * avg_adjusted_price")
    #     for s in summaries
    # )
    # fares_avg = sum(
    #     s.fares.set_index(
    #     ["carrier", "orig", "dest", "booking_class", "dcp_index"]
    #     )[
    #         ["price", "gt_sold"]
    #     ]
    #     for s in summaries
    # ) / len(summaries)
    # fares_sum = sum(
    #     s.fares.set_index(
    #     ["carrier", "orig", "dest", "booking_class", "dcp_index"]
    #     )[
    #         ["gt_sold"]
    #     ]
    #     for s in summaries
    # )

    # TODO: aggregate path_classes

    # these are averages, but need to have the index values excluded
    # TODO: the index values should be set properly on the original dataframes
    carriers = sum(s.carriers.set_index("carrier") for s in summaries) / len(
        summaries
    )
    legs = sum(
        s.legs.set_index(["carrier", "leg_id", "flt_no", "orig", "dest"])
        for s in summaries
    ) / len(summaries)
    legs = legs.reset_index()
    paths = sum(
        s.paths.set_index(["orig", "dest", "carrier1", "leg_id1", "carrier2"])
        for s in summaries
    ) / len(summaries)

    def average(name):
        frames = []
        for s in summaries:
            frame = getattr(s, name)
            if frame is not None:
                frames.append(frame)
        if frames:
            return sum(frames) / len(frames)
        return None

    fare_class_mix = average("fare_class_mix")
    leg_forecasts = average("leg_forecasts")
    path_forecasts = average("path_forecasts")
    bid_price_history = average("bid_price_history")
    displacement_history = average("displacement_history")
    demand_to_come_summary = average("demand_to_come_summary")

    # dataframes with count data that need to be summed
    def sum_count(name):
        frames = []
        for s in summaries:
            frame = getattr(s, name)
            if frame is not None:
                frames.append(frame)
        if frames:
            return sum(frames)
        return None

    raw_load_factor_distribution = sum_count("raw_load_factor_distribution")
    leg_avg_load_factor_distribution = sum_count("leg_avg_load_factor_distribution")
    raw_fare_class_mix = sum_count("raw_fare_class_mix")
    leg_local_fraction_distribution = sum_count("leg_local_fraction_distribution")

    result = cls(
        demands=demands,
        legs=legs,
        paths=paths,
        carriers=carriers,
        fare_class_mix=fare_class_mix,
        leg_forecasts=leg_forecasts,
        path_forecasts=path_forecasts,
        carrier_history=carrier_history,
        bookings_by_timeframe=bookings_by_timeframe,
        segmentation_by_timeframe=segmentation_by_timeframe,
        bid_price_history=bid_price_history,
        displacement_history=displacement_history,
        demand_to_come=demand_to_come,
        demand_to_come_summary=demand_to_come_summary,
        leg_avg_load_factor_distribution=leg_avg_load_factor_distribution,
        leg_local_fraction_distribution=leg_local_fraction_distribution,
        raw_load_factor_distribution=raw_load_factor_distribution,
        raw_fare_class_mix=raw_fare_class_mix,
        n_total_samples=sum(s.n_total_samples for s in summaries),
    )
    result.meta_trials = summaries
    return result

aggregate_demand_history

aggregate_demand_history(by_segment: bool = True) -> Series

Total demand by sample, aggregated over all markets.

Parameters:

  • by_segment (bool, default: True ) –

    Aggregate by segment. If false, segments are also aggregated.

Returns:

  • Series

    Total demand, indexed by trial, sample, and segment (business/leisure).

Source code in passengersim/summary.py
def aggregate_demand_history(self, by_segment: bool = True) -> pd.Series:
    """
    Total demand by sample, aggregated over all markets.

    Parameters
    ----------
    by_segment : bool, default True
        Aggregate by segment.  If false, segments are also aggregated.

    Returns
    -------
    pandas.Series
        Total demand, indexed by trial, sample, and segment
        (business/leisure).
    """
    groupbys = ["trial", "sample"]
    if by_segment:
        groupbys.append("segment")
    return self.demand_to_come.iloc[:, 0].groupby(groupbys, observed=False).sum()

demand_in_tf

demand_in_tf() -> DataFrame | None

History of demand arriving in each timeframe.

This dataframe is derived from the demand_to_come dataframe by taking the sequential differences.

Source code in passengersim/summary.py
def demand_in_tf(self) -> pd.DataFrame | None:
    """History of demand arriving in each timeframe.

    This dataframe is derived from the `demand_to_come` dataframe
    by taking the sequential differences.
    """
    if self.demand_to_come is None:
        return None
    return self.demand_to_come.diff(-1, axis=1).iloc[:, :-1]

fig_bid_price_history

fig_bid_price_history(
    by_carrier: bool | str = True,
    show_stdev: float | bool | None = None,
    cap: Literal["some", "zero", None] = None,
    raw_df=False,
)
Source code in passengersim/summary.py
@report_figure
def fig_bid_price_history(
    self,
    by_carrier: bool | str = True,
    show_stdev: float | bool | None = None,
    cap: Literal["some", "zero", None] = None,
    raw_df=False,
):
    if cap is None:
        bp_mean = "bid_price_mean"
    elif cap == "some":
        bp_mean = "some_cap_bid_price_mean"
    elif cap == "zero":
        bp_mean = "zero_cap_bid_price_mean"
    else:
        raise ValueError(f"cap={cap!r} not in ['some', 'zero', None]")
    df = self.bid_price_history.reset_index()
    color = None
    if isinstance(by_carrier, str):
        df = df[df.carrier == by_carrier]
    elif by_carrier:
        color = "carrier:N"
        if show_stdev is None:
            show_stdev = False
    if show_stdev:
        if show_stdev is True:
            show_stdev = 2
        df["bid_price_upper"] = df[bp_mean] + show_stdev * df["bid_price_stdev"]
        df["bid_price_lower"] = (
            df[bp_mean] - show_stdev * df["bid_price_stdev"]
        ).clip(0, None)
    if raw_df:
        return df

    import altair as alt

    line_encoding = dict(
        x=alt.X("days_prior:Q")
        .scale(reverse=True)
        .title("Days Prior to Departure"),
        y=alt.Y(bp_mean, title="Bid Price"),
    )
    if color:
        line_encoding["color"] = color
    chart = alt.Chart(df)
    fig = chart.mark_line(interpolate="step-before").encode(**line_encoding)
    if show_stdev:
        area_encoding = dict(
            x=alt.X("days_prior:Q")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y=alt.Y("bid_price_lower:Q", title="Bid Price"),
            y2=alt.Y2("bid_price_upper:Q", title="Bid Price"),
        )
        bound = chart.mark_area(
            opacity=0.1,
            interpolate="step-before",
        ).encode(**area_encoding)
        bound_line = chart.mark_line(
            opacity=0.4, strokeDash=[5, 5], interpolate="step-before"
        ).encode(
            x=alt.X("days_prior:Q")
            .scale(reverse=True)
            .title("Days Prior to Departure")
        )
        top_line = bound_line.encode(
            y=alt.Y("bid_price_lower:Q", title="Bid Price")
        )
        bottom_line = bound_line.encode(
            y=alt.Y("bid_price_upper:Q", title="Bid Price")
        )
        fig = fig + bound + top_line + bottom_line
    return fig

fig_bookings_by_timeframe

fig_bookings_by_timeframe(
    by_carrier: bool | str = True,
    by_class: bool | str = False,
    raw_df: bool = False,
    errorbands: bool = False,
    exclude_nogo: bool = True,
)
Source code in passengersim/summary.py
@report_figure
def fig_bookings_by_timeframe(
    self,
    by_carrier: bool | str = True,
    by_class: bool | str = False,
    raw_df: bool = False,
    errorbands: bool = False,
    exclude_nogo: bool = True,
):
    if errorbands:
        if by_carrier is True:
            raise NotImplementedError("error bands for all carriers is messy")
        return self._fig_bookings_by_timeframe_errorband(
            by_carrier=by_carrier, raw_df=raw_df
        )

    def differs(x):
        return x.shift(-1, fill_value=0) - x

    def _summarize(x: pd.DataFrame, c: str):
        if "trial" not in x.columns:
            x = x.assign(trial=0)
        if by_class:
            y = (
                x.groupby(
                    ["trial", "carrier", "booking_class", "days_prior"],
                    observed=False,
                )[f"avg_{c}"]
                .sum()
                .unstack(["trial", "carrier", "booking_class"])
                .sort_index(ascending=False)
                .apply(differs)
                .stack(["carrier", "booking_class"], future_stack=True)
                .aggregate(["mean", "sem"], axis=1)
                .assign(
                    ci0=lambda x: np.maximum(x["mean"] - 1.96 * x["sem"], 0),
                    ci1=lambda x: x["mean"] + 1.96 * x["sem"],
                )
            )
        else:
            y = (
                x.groupby(["trial", "carrier", "days_prior"], observed=False)[
                    f"avg_{c}"
                ]
                .sum()
                .unstack(["trial", "carrier"])
                .sort_index(ascending=False)
                .apply(differs)
                .stack("carrier", future_stack=True)
                .aggregate(["mean", "sem"], axis=1)
                .assign(
                    ci0=lambda x: np.maximum(x["mean"] - 1.96 * x["sem"], 0),
                    ci1=lambda x: x["mean"] + 1.96 * x["sem"],
                )
            )
        return pd.concat({c: y}, names=["paxtype"])

    if self.bookings_by_timeframe is None:
        raise ValueError("bookings_by_timeframe not found")
    bookings_by_timeframe = self.bookings_by_timeframe.reset_index()
    df0 = _summarize(bookings_by_timeframe, "business")
    df1 = _summarize(bookings_by_timeframe, "leisure")
    df = (
        pd.concat([df0, df1], axis=0)
        .rename(columns={"mean": "sold"})
        .reset_index()
        .query("(days_prior>0) & (sold>0)")
    )
    title = "Bookings by Timeframe"
    if by_class is True:
        title = "Bookings by Timeframe and Booking Class"
    title_annot = []
    if not by_carrier:
        g = ["days_prior", "paxtype"]
        if by_class:
            g += ["booking_class"]
        df = (
            df.groupby(g, observed=False)[["sold", "ci0", "ci1"]]
            .sum()
            .reset_index()
        )
    if isinstance(by_carrier, str):
        df = df[df["carrier"] == by_carrier]
        df = df.drop(columns=["carrier"])
        title_annot.append(by_carrier)
        by_carrier = False
    if isinstance(by_class, str):
        df = df[df["booking_class"] == by_class]
        df = df.drop(columns=["booking_class"])
        title_annot.append(f"Class {by_class}")
        by_class = False
    if title_annot:
        title = f"{title} ({', '.join(title_annot)})"
    if exclude_nogo and "carrier" in df.columns:
        df = df[df["carrier"] != "NONE"]
    if raw_df:
        return df

    import altair as alt

    if by_carrier:
        color = "carrier:N"
        color_title = "Carrier"
    elif by_class:
        color = "booking_class:N"
        color_title = "Booking Class"
    else:
        color = "paxtype:N"
        color_title = "Passenger Type"

    if by_class:
        chart = (
            alt.Chart(df)
            .mark_bar()
            .encode(
                color=alt.Color(color).title(color_title),
                x=alt.X("days_prior:O")
                .scale(reverse=True)
                .title("Days Prior to Departure"),
                y=alt.Y("sold"),
                tooltip=(
                    [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                )
                + [
                    alt.Tooltip("paxtype", title="Passenger Type"),
                    alt.Tooltip("days_prior", title="DfD"),
                    alt.Tooltip("sold", format=".2f"),
                ],
            )
            .properties(
                width=500,
                height=200,
            )
            .facet(
                row=alt.Row("paxtype:N", title="Passenger Type"),
                title=title,
            )
        )
    else:
        chart = (
            alt.Chart(df, title=title)
            .mark_line()
            .encode(
                color=alt.Color(color).title(color_title),
                x=alt.X("days_prior:O")
                .scale(reverse=True)
                .title("Days Prior to Departure"),
                y=alt.Y("sold") if by_class else "sold",
                strokeDash=alt.StrokeDash("paxtype").title("Passenger Type"),
                tooltip=(
                    [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                )
                + [
                    alt.Tooltip("paxtype", title="Passenger Type"),
                    alt.Tooltip("days_prior", title="DfD"),
                    alt.Tooltip("sold", format=".2f"),
                ],
            )
            .properties(
                width=500,
                height=300,
            )
            .configure_axis(
                labelFontSize=12,
                titleFontSize=12,
            )
            .configure_legend(
                titleFontSize=12,
                labelFontSize=15,
            )
        )
    return chart

fig_carrier_load_factors

fig_carrier_load_factors(
    raw_df=False,
    load_measure: Literal[
        "sys_lf", "avg_leg_lf"
    ] = "sys_lf",
)
Source code in passengersim/summary.py
@report_figure
def fig_carrier_load_factors(
    self, raw_df=False, load_measure: Literal["sys_lf", "avg_leg_lf"] = "sys_lf"
):
    measure_name = (
        "System Load Factor" if load_measure == "sys_lf" else "Leg Load Factor"
    )
    return self._fig_carrier_load_factors(
        raw_df,
        load_measure,
        measure_name,
        title=f"Carrier {measure_name}s",
    )

fig_carrier_mileage

fig_carrier_mileage(raw_df: bool = False, report=None)

Figure showing ASM, RPM by carrier.

ASM is available seat miles. RPM is revenue passenger miles.

Parameters:

  • raw_df (bool, default: False ) –

    Return the raw data for this figure as a pandas DataFrame, instead of generating the figure itself.

  • report (Reporter, default: None ) –

    Also append this figure to the given report.

Source code in passengersim/summary.py
def fig_carrier_mileage(self, raw_df: bool = False, report=None):
    """
    Figure showing ASM, RPM by carrier.

    ASM is available seat miles.  RPM is revenue passenger miles.

    Parameters
    ----------
    raw_df : bool, default False
        Return the raw data for this figure as a pandas DataFrame, instead
        of generating the figure itself.
    report : xmle.Reporter, optional
        Also append this figure to the given report.
    """
    df = (
        self.carriers.reset_index()[["carrier", "asm", "rpm"]]
        .set_index("carrier")
        .rename_axis(columns="measure")
        .unstack()
        .to_frame("value")
        .reset_index()
    )
    if raw_df:
        return df
    import altair as alt

    chart = alt.Chart(df, title="Carrier Loads")
    bars = chart.mark_bar().encode(
        x=alt.X("carrier:N", title="Carrier"),
        y=alt.Y("value", stack=None, title="miles"),
        color="measure",
        tooltip=["carrier", "measure", alt.Tooltip("value", format=".4s")],
    )
    text = chart.mark_text(
        dx=0,
        dy=5,
        color="white",
        baseline="top",
    ).encode(
        x=alt.X("carrier:N"),
        y=alt.Y("value").stack(None),
        text=alt.Text("value:Q", format=".4s"),
    )
    fig = (
        (bars + text)
        .properties(
            width=400,
            height=300,
        )
        .configure_axis(
            labelFontSize=12,
            titleFontSize=12,
        )
        .configure_legend(
            titleFontSize=12,
            labelFontSize=15,
        )
    )
    if report:
        report.add_figure(fig)
    return fig

fig_carrier_revenues

fig_carrier_revenues(raw_df=False)
Source code in passengersim/summary.py
@report_figure
def fig_carrier_revenues(self, raw_df=False):
    return self._fig_carrier_load_factors(
        raw_df, "avg_rev", "Average Revenue", "$.4s", title="Carrier Revenues"
    )

fig_carrier_total_bookings

fig_carrier_total_bookings(raw_df=False)
Source code in passengersim/summary.py
@report_figure
def fig_carrier_total_bookings(self, raw_df=False):
    return self._fig_carrier_load_factors(
        raw_df, "sold", "Total Bookings", ".4s", title="Carrier Total Bookings"
    )

fig_carrier_yields

fig_carrier_yields(raw_df=False)
Source code in passengersim/summary.py
@report_figure
def fig_carrier_yields(self, raw_df=False):
    return self._fig_carrier_load_factors(
        raw_df, "yield", "Average Yield", "$.4f", title="Carrier Yields"
    )

fig_displacement_history

fig_displacement_history(
    by_carrier: bool | str = True,
    show_stdev: float | bool | None = None,
    raw_df=False,
)
Source code in passengersim/summary.py
@report_figure
def fig_displacement_history(
    self,
    by_carrier: bool | str = True,
    show_stdev: float | bool | None = None,
    raw_df=False,
):
    df = self.displacement_history.reset_index()
    color = None
    if isinstance(by_carrier, str):
        df = df[df.carrier == by_carrier]
    elif by_carrier:
        color = "carrier:N"
        if show_stdev is None:
            show_stdev = False
    if show_stdev:
        if show_stdev is True:
            show_stdev = 2
        df["displacement_upper"] = (
            df["displacement_mean"] + show_stdev * df["displacement_stdev"]
        )
        df["displacement_lower"] = (
            df["displacement_mean"] - show_stdev * df["displacement_stdev"]
        ).clip(0, None)
    if raw_df:
        return df

    import altair as alt

    line_encoding = dict(
        x=alt.X("days_prior:Q")
        .scale(reverse=True)
        .title("Days Prior to Departure"),
        y=alt.Y("displacement_mean", title="Displacement Cost"),
    )
    if color:
        line_encoding["color"] = color
    chart = alt.Chart(df)
    fig = chart.mark_line(interpolate="step-before").encode(**line_encoding)
    if show_stdev:
        area_encoding = dict(
            x=alt.X("days_prior:Q")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y=alt.Y("displacement_lower:Q", title="Displacement Cost"),
            y2=alt.Y2("displacement_upper:Q", title="Displacement Cost"),
        )
        bound = chart.mark_area(
            opacity=0.1,
            interpolate="step-before",
        ).encode(**area_encoding)
        bound_line = chart.mark_line(
            opacity=0.4, strokeDash=[5, 5], interpolate="step-before"
        ).encode(
            x=alt.X("days_prior:Q")
            .scale(reverse=True)
            .title("Days Prior to Departure")
        )
        top_line = bound_line.encode(
            y=alt.Y("displacement_lower:Q", title="Displacement Cost")
        )
        bottom_line = bound_line.encode(
            y=alt.Y("displacement_upper:Q", title="Displacement Cost")
        )
        fig = fig + bound + top_line + bottom_line
    return fig

fig_fare_class_mix

fig_fare_class_mix(raw_df=False, label_threshold=0.06)
Source code in passengersim/summary.py
@report_figure
def fig_fare_class_mix(self, raw_df=False, label_threshold=0.06):
    if self.fare_class_mix is not None:
        df = self.fare_class_mix.reset_index()[
            ["carrier", "booking_class", "avg_sold"]
        ]
    elif self.raw_fare_class_mix is not None and self.n_total_samples > 0:
        df = self.raw_fare_class_mix / self.n_total_samples
        df = df.rename(columns={"sold": "avg_sold"})
        df = df.reset_index()[["carrier", "booking_class", "avg_sold"]]
    else:
        return None

    if raw_df:
        return df
    return self._fig_fare_class_mix(
        df,
        label_threshold=label_threshold,
        title="Fare Class Mix",
    )

fig_leg_forecasts

fig_leg_forecasts(
    by_leg_id: bool | int = True,
    by_class: bool | str = True,
    of: Literal["mu", "sigma"]
    | list[Literal["mu", "sigma"]] = "mu",
    raw_df=False,
)
Source code in passengersim/summary.py
@report_figure
def fig_leg_forecasts(
    self,
    by_leg_id: bool | int = True,
    by_class: bool | str = True,
    of: Literal["mu", "sigma"] | list[Literal["mu", "sigma"]] = "mu",
    raw_df=False,
):
    if isinstance(of, list):
        if raw_df:
            raise NotImplementedError
        fig = self.fig_leg_forecasts(
            by_leg_id=by_leg_id,
            by_class=by_class,
            of=of[0],
        )
        for of_ in of[1:]:
            fig |= self.fig_leg_forecasts(
                by_leg_id=by_leg_id,
                by_class=by_class,
                of=of_,
            )
        return fig
    y = "forecast_mean" if of == "mu" else "forecast_stdev"
    columns = [
        "carrier",
        "leg_id",
        "booking_class",
        "days_prior",
        y,
    ]
    if self.leg_forecasts is None:
        raise ValueError("the leg_forecasts summary table is not available")
    df = self.leg_forecasts.reset_index()[columns]
    color = "booking_class:N"
    if isinstance(by_leg_id, int) and by_leg_id is not True:
        df = df[df.leg_id == by_leg_id]
    if isinstance(by_class, str):
        df = df[df.booking_class == by_class]
        color = None
    if raw_df:
        return df
    return self._fig_forecasts(
        df,
        facet_on=None,
        y=y,
        color=color,
        y_title="Mean Demand Forecast" if of == "mu" else "Std Dev Demand Forecast",
    )

fig_leg_local_fraction_distribution

fig_leg_local_fraction_distribution(
    by_carrier: bool | str = True,
    breakpoints: Collection[int] = (
        50,
        55,
        60,
        65,
        70,
        75,
        80,
        85,
        90,
        95,
        100,
    ),
    raw_df=False,
)

Figure showing the distribution of fraction of leg passengers who are local.

Parameters:

  • by_carrier (bool or str, default: True ) –

    If True, show the distribution by carrier. If a string, show the distribution for that carrier. If False, show the distribution aggregated over all carriers.

  • breakpoints (Collection[int, ...], default: (50, 55, 60, 65, ..., 90, 95, 100) ) –

    The breakpoints for the local fraction ranges, which represent the lowest local fraction value in each bin. The first and last breakpoints are always bounded to 0 and 101, respectively; these bounds can be included explicitly or omitted to be included implicitly. Setting the top value to 101 ensures that the highest local fraction value (100) is included in the last bin.

  • raw_df (bool, default: False ) –

    Return the raw data for this figure as a pandas DataFrame, instead of generating the figure itself.

Returns:

  • Chart or DataFrame
Source code in passengersim/summary.py
def fig_leg_local_fraction_distribution(
    self,
    by_carrier: bool | str = True,
    breakpoints: Collection[int] = (
        50,
        55,
        60,
        65,
        70,
        75,
        80,
        85,
        90,
        95,
        100,
    ),
    raw_df=False,
):
    """
    Figure showing the distribution of fraction of leg passengers who are local.

    Parameters
    ----------
    by_carrier : bool or str, default True
        If True, show the distribution by carrier.  If a string, show the
        distribution for that carrier. If False, show the distribution
        aggregated over all carriers.
    breakpoints : Collection[int, ...], default (50, 55, 60, 65, ..., 90, 95, 100)
        The breakpoints for the local fraction ranges, which represent the lowest
        local fraction value in each bin. The first and last breakpoints are always
        bounded to 0 and 101, respectively; these bounds can be included explicitly
        or omitted to be included implicitly. Setting the top value to 101 ensures
        that the highest local fraction value (100) is included in the last bin.
    raw_df : bool, default False
        Return the raw data for this figure as a pandas DataFrame, instead
        of generating the figure itself.

    Returns
    -------
    altair.Chart or pd.DataFrame
    """
    title = "Local Fraction Frequency"  # default title
    if self.leg_local_fraction_distribution is None:
        raise AttributeError(
            "leg_local_fraction_distribution not found, "
            "it is required for using raw source data."
        )
    df_for_chart = (
        self.leg_local_fraction_distribution.rename_axis(columns="carrier")
        .stack(future_stack=True)
        .rename("Count")
        .reset_index()
    )
    if not isinstance(breakpoints, tuple):
        breakpoints = tuple(breakpoints)
    if breakpoints[0] <= 0:
        breakpoints = (-1,) + breakpoints[1:]
    else:
        breakpoints = (-1,) + breakpoints
    if breakpoints[-1] >= 101:
        breakpoints = breakpoints[:-1] + (101,)
    else:
        breakpoints = breakpoints + (101,)

    # Create labels for categories
    def make_label(i, j):
        if i == j - 1:
            return f"{i}"
        else:
            return f"{i}-{j-1}"

    labels = [make_label(0, breakpoints[1])]
    for i in range(1, len(breakpoints) - 2):
        labels += [make_label(breakpoints[i], breakpoints[i + 1])]
    if breakpoints[-2] < 100:
        labels += [make_label(breakpoints[-2], 101)]
    else:
        labels += ["100"]

    breaker = pd.cut(
        df_for_chart.local_fraction,
        bins=breakpoints,
        right=False,
        labels=labels,
    ).rename("Leg Local Fraction Range")
    df_for_chart = (
        df_for_chart.groupby(["carrier", breaker], observed=False)
        .Count.sum()
        .reset_index()
    )

    if not by_carrier:
        df_for_chart = (
            df_for_chart.groupby(["Leg Local Fraction Range"], observed=False)
            .Count.sum()
            .reset_index()
        )
    elif isinstance(by_carrier, str):
        df_for_chart = df_for_chart[df_for_chart["carrier"] == by_carrier]
        df_for_chart = df_for_chart.drop(columns=["carrier"])

    if raw_df:
        return df_for_chart

    import altair as alt

    if by_carrier is True:
        chart = (
            alt.Chart(df_for_chart)
            .mark_bar()
            .encode(
                x=alt.X(
                    "Leg Local Fraction Range", title="Leg Local Fraction Range"
                ),
                y=alt.Y("Count:Q", title="Count"),
                facet=alt.Facet("carrier:N", columns=2, title="Carrier"),
                tooltip=[
                    alt.Tooltip("carrier", title="Carrier"),
                    alt.Tooltip("Count", title="Count"),
                ],
            )
            .properties(width=300, height=250, title=f"{title} by Carrier")
        )
    else:
        chart = (
            alt.Chart(df_for_chart)
            .mark_bar()
            .encode(
                x=alt.X(
                    "Leg Local Fraction Range", title="Leg Local Fraction Range"
                ),
                y=alt.Y("Count:Q", title="Count"),
            )
            .properties(
                width=600,
                height=400,
                title=title if not by_carrier else f"{title} ({by_carrier})",
            )
        )
    return chart

fig_load_factor_distribution

fig_load_factor_distribution(
    by_carrier: bool | str = True,
    breakpoints: Collection[int] = (
        50,
        55,
        60,
        65,
        70,
        75,
        80,
        85,
        90,
        95,
        100,
    ),
    source: Literal["leg_avg", "raw", "db"] = "leg_avg",
    raw_df=False,
)

Figure showing the distribution of leg load factors.

Parameters:

  • by_carrier (bool or str, default: True ) –

    If True, show the distribution by carrier. If a string, show the distribution for that carrier. If False, show the distribution aggregated over all carriers.

  • breakpoints (Collection[int, ...], default: (50, 55, 60, 65, ..., 90, 95, 100) ) –

    The breakpoints for the load factor ranges, which represent the lowest load factor value in each bin. The first and last breakpoints are always bounded to 0 and 101, respectively; these bounds can be included explicitly or omitted to be included implicitly. Setting the top value to 101 ensures that the highest load factor value (100) is included in the last bin.

  • source (('raw', 'db'), default: "raw" ) –

    The source of the data. "raw" uses the raw load factor distribution output from the simulation, which is faster and preferred if available. "db" uses the older load factor distribution table, which is extracted as a query from the database. This requires leg level departure (final) details to have been recorded in the database, but potentially allows arbitrary custom filters or transformations to be applied.

  • raw_df (bool, default: False ) –

    Return the raw data for this figure as a pandas DataFrame, instead of generating the figure itself.

Returns:

  • Chart or DataFrame
Source code in passengersim/summary.py
@report_figure
def fig_load_factor_distribution(
    self,
    by_carrier: bool | str = True,
    breakpoints: Collection[int] = (
        50,
        55,
        60,
        65,
        70,
        75,
        80,
        85,
        90,
        95,
        100,
    ),
    source: Literal["leg_avg", "raw", "db"] = "leg_avg",
    raw_df=False,
):
    """
    Figure showing the distribution of leg load factors.

    Parameters
    ----------
    by_carrier : bool or str, default True
        If True, show the distribution by carrier.  If a string, show the
        distribution for that carrier. If False, show the distribution
        aggregated over all carriers.
    breakpoints : Collection[int, ...], default (50, 55, 60, 65, ..., 90, 95, 100)
        The breakpoints for the load factor ranges, which represent the lowest
        load factor value in each bin. The first and last breakpoints are always
        bounded to 0 and 101, respectively; these bounds can be included explicitly
        or omitted to be included implicitly. Setting the top value to 101 ensures
        that the highest load factor value (100) is included in the last bin.
    source : {"raw", "db"}, default "raw"
        The source of the data.  "raw" uses the raw load factor distribution
        output from the simulation, which is faster and preferred if available.
        "db" uses the older load factor distribution table, which is extracted
        as a query from the database.  This requires leg level departure (final)
        details to have been recorded in the database, but potentially allows
        arbitrary custom filters or transformations to be applied.
    raw_df : bool, default False
        Return the raw data for this figure as a pandas DataFrame, instead
        of generating the figure itself.

    Returns
    -------
    altair.Chart or pd.DataFrame
    """
    title = "Load Factor Frequency"  # default title
    if source == "raw" or source == "leg_avg":
        # Load using faster raw load factor data generated by the simulation
        # This is faster than loading from the database and now preferred
        if source == "raw":
            if self.raw_load_factor_distribution is None:
                raise AttributeError(
                    "raw_load_factor_distribution not found, "
                    "it is required for using raw source data."
                )
            df_for_chart = (
                self.raw_load_factor_distribution.rename_axis(columns="carrier")
                .stack(future_stack=True)
                .rename("Count")
                .reset_index()
            )
            title = "Raw Load Factor Frequency"
        elif source == "leg_avg":
            if self.leg_avg_load_factor_distribution is None:
                raise AttributeError(
                    "leg_avg_load_factor_distribution not found, "
                    "it is required for using leg_avg source data."
                )
            df_for_chart = (
                self.leg_avg_load_factor_distribution.rename_axis(columns="carrier")
                .stack(future_stack=True)
                .rename("Count")
                .reset_index()
            )
            title = "Leg Average Load Factor Frequency"
        if not isinstance(breakpoints, tuple):
            breakpoints = tuple(breakpoints)
        if breakpoints[0] <= 0:
            breakpoints = (-1,) + breakpoints[1:]
        else:
            breakpoints = (-1,) + breakpoints
        if breakpoints[-1] >= 101:
            breakpoints = breakpoints[:-1] + (101,)
        else:
            breakpoints = breakpoints + (101,)

        # Create labels for categories
        def make_label(i, j):
            if i == j - 1:
                return f"{i}"
            else:
                return f"{i}-{j-1}"

        labels = [make_label(0, breakpoints[1])]
        for i in range(1, len(breakpoints) - 2):
            labels += [make_label(breakpoints[i], breakpoints[i + 1])]
        if breakpoints[-2] < 100:
            labels += [make_label(breakpoints[-2], 101)]
        else:
            labels += ["100"]
        breaker = pd.cut(
            df_for_chart.leg_load_factor,
            bins=breakpoints,
            right=False,
            labels=labels,
        ).rename("Load Factor Range")
        df_for_chart = (
            df_for_chart.groupby(["carrier", breaker], observed=False)
            .Count.sum()
            .reset_index()
        )

    elif source == "db":
        # Older load factor distribution table, taken from database
        if not hasattr(self, "load_factor_distribution"):
            raise AttributeError(
                "load_factor_distribution data not found. Please load it first."
            )

        df_for_chart = self.load_factor_distribution
        df_for_chart.columns.names = ["Load Factor Range"]
        df_for_chart = df_for_chart.set_index("carrier")
        df_for_chart = (
            df_for_chart.stack(future_stack=True).rename("Count").reset_index()
        )

    else:
        raise ValueError(f"Unknown source {source}, should be 'raw' or 'db'")

    if not by_carrier:
        df_for_chart = (
            df_for_chart.groupby(["Load Factor Range"], observed=False)
            .Count.sum()
            .reset_index()
        )
    elif isinstance(by_carrier, str):
        df_for_chart = df_for_chart[df_for_chart["carrier"] == by_carrier]
        df_for_chart = df_for_chart.drop(columns=["carrier"])

    if raw_df:
        return df_for_chart

    import altair as alt

    if by_carrier is True:
        chart = (
            alt.Chart(df_for_chart)
            .mark_bar()
            .encode(
                x=alt.X("Load Factor Range", title="Load Factor Range"),
                y=alt.Y("Count:Q", title="Count"),
                facet=alt.Facet("carrier:N", columns=2, title="Carrier"),
                tooltip=[
                    alt.Tooltip("carrier", title="Carrier"),
                    alt.Tooltip("Count", title="Count"),
                ],
            )
            .properties(width=300, height=250, title=f"{title} by Carrier")
        )
    else:
        chart = (
            alt.Chart(df_for_chart)
            .mark_bar()
            .encode(
                x=alt.X("Load Factor Range", title="Load Factor Range"),
                y=alt.Y("Count:Q", title="Count"),
            )
            .properties(
                width=600,
                height=400,
                title=title if not by_carrier else f"{title} ({by_carrier})",
            )
        )

    return chart

fig_od_fare_class_mix

fig_od_fare_class_mix(
    orig: str, dest: str, raw_df=False, label_threshold=0.06
)
Source code in passengersim/summary.py
@report_figure
def fig_od_fare_class_mix(
    self, orig: str, dest: str, raw_df=False, label_threshold=0.06
):
    df = self.od_fare_class_mix[orig, dest].reset_index()[
        ["carrier", "booking_class", "avg_sold"]
    ]
    if raw_df:
        return df
    return self._fig_fare_class_mix(
        df, label_threshold=label_threshold, title=f"Fare Class Mix ({orig}-{dest})"
    )

fig_path_forecasts

fig_path_forecasts(
    by_path_id: bool | int = True,
    by_class: bool | str = True,
    of: Literal[
        "mu", "sigma", "closed", "adj_price"
    ] = "mu",
    raw_df=False,
)
Source code in passengersim/summary.py
@report_figure
def fig_path_forecasts(
    self,
    by_path_id: bool | int = True,
    by_class: bool | str = True,
    of: Literal["mu", "sigma", "closed", "adj_price"] = "mu",
    raw_df=False,
):
    if self.path_forecasts is None:
        raise ValueError("the path_forecasts summary table is not available")
    of_columns = {
        "mu": "forecast_mean",
        "sigma": "forecast_stdev",
        "closed": "forecast_closed_in_tf",
        "adj_price": "adjusted_price",
    }
    y = of_columns.get(of)
    columns = [
        "path_id",
        "booking_class",
        "days_prior",
        y,
    ]
    df = self.path_forecasts.reset_index()[columns]
    color = "booking_class:N"
    if isinstance(by_path_id, int) and by_path_id is not True:
        df = df[df.path_id == by_path_id]
    if isinstance(by_class, str):
        df = df[df.booking_class == by_class]
        color = None
    if raw_df:
        return df
    facet_on = None
    if by_path_id is True:
        facet_on = "path_id"
    return self._fig_forecasts(df, facet_on=facet_on, y=y, color=color)

fig_segmentation_by_timeframe

fig_segmentation_by_timeframe(
    metric: Literal["bookings", "revenue"],
    by_carrier: bool | str = True,
    by_class: bool | str = False,
    raw_df: bool = False,
    exclude_nogo: bool = True,
)
Source code in passengersim/summary.py
@report_figure
def fig_segmentation_by_timeframe(
    self,
    metric: Literal["bookings", "revenue"],
    by_carrier: bool | str = True,
    by_class: bool | str = False,
    raw_df: bool = False,
    exclude_nogo: bool = True,
):
    if self.segmentation_by_timeframe is None:
        raise ValueError("segmentation_by_timeframe not found")
    df = self.segmentation_by_timeframe
    idxs = list(df.index.names)
    if "trial" in idxs:
        idxs.remove("trial")
        df = df.groupby(idxs).mean()
    df = df[metric].stack().rename(metric).reset_index()

    title = f"{metric.title()} by Timeframe"
    if by_class is True:
        title = f"{metric.title()} by Timeframe and Booking Class"
    title_annot = []
    if not by_carrier:
        g = ["days_prior", "segment"]
        if by_class:
            g += ["booking_class"]
        df = df.groupby(g, observed=False)[[metric]].sum().reset_index()
    if by_carrier and not by_class:
        df = (
            df.groupby(["carrier", "days_prior", "segment"], observed=False)[
                [metric]
            ]
            .sum()
            .reset_index()
        )
    if isinstance(by_carrier, str):
        df = df[df["carrier"] == by_carrier]
        df = df.drop(columns=["carrier"])
        title_annot.append(by_carrier)
        by_carrier = False
    if isinstance(by_class, str):
        df = df[df["booking_class"] == by_class]
        df = df.drop(columns=["booking_class"])
        title_annot.append(f"Class {by_class}")
        by_class = False
    if title_annot:
        title = f"{title} ({', '.join(title_annot)})"
    if exclude_nogo and "carrier" in df.columns:
        df = df[df["carrier"] != "NONE"]
    if raw_df:
        return df

    import altair as alt

    if by_carrier:
        color = "carrier:N"
        color_title = "Carrier"
    elif by_class:
        color = "booking_class:N"
        color_title = "Booking Class"
    else:
        color = "segment:N"
        color_title = "Passenger Type"

    if metric == "revenue":
        metric_fmt = "$,.0f"
    else:
        metric_fmt = ",.2f"

    chart = (
        alt.Chart(df)
        .mark_bar()
        .encode(
            color=alt.Color(color).title(color_title),
            x=alt.X("days_prior:O")
            .scale(reverse=True)
            .title("Days Prior to Departure"),
            y=alt.Y(metric),
            tooltip=(
                [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
            )
            + (
                [alt.Tooltip("booking_class").title("Booking Class")]
                if by_class
                else []
            )
            + [
                alt.Tooltip("segment", title="Passenger Type"),
                alt.Tooltip("days_prior", title="Days Prior"),
                alt.Tooltip(metric, format=metric_fmt, title=metric.title()),
            ],
        )
        .properties(
            width=500,
            height=200,
        )
    )
    if by_carrier or by_class:
        chart = chart.facet(
            row=alt.Row("segment:N", title="Passenger Type"),
            title=title,
        )
    return chart

from_pickle classmethod

from_pickle(filename: str | Path, read_latest: bool = True)

Load the object from a pickle file.

Parameters:

  • filename (str or Path - like) –

    The filename to load the object from.

  • read_latest (bool, default: True ) –

    If True, read the latest file matching the pattern.

Source code in passengersim/summary.py
@classmethod
def from_pickle(cls, filename: str | pathlib.Path, read_latest: bool = True):
    """Load the object from a pickle file.

    Parameters
    ----------
    filename : str or Path-like
        The filename to load the object from.
    read_latest : bool, default True
        If True, read the latest file matching the pattern.
    """
    summarytables_is_deprecated()

    import glob
    import pickle

    if read_latest:
        filename_glob = pathlib.Path(filename).with_suffix(".*.pkl")
        files = sorted(glob.glob(str(filename_glob)))
        if not files:
            if not os.path.exists(filename):
                raise FileNotFoundError(filename)
        else:
            filename = files[-1]

    with open(filename, "rb") as f:
        result = pickle.load(f)
        if result.__class__.__name__ != cls.__name__:
            raise TypeError(f"Expected {cls}, got {type(result)}")
        return result

from_sqlite classmethod

from_sqlite(
    filename: str | Path,
    make_indexes: bool | dict = False,
    additional: Collection[str | tuple] | str | None = None,
)
Source code in passengersim/summary.py
@classmethod
def from_sqlite(
    cls,
    filename: str | pathlib.Path,
    make_indexes: bool | dict = False,
    additional: Collection[str | tuple] | str | None = None,
):
    summarytables_is_deprecated()
    if not os.path.isfile(filename):
        raise FileNotFoundError(filename)
    db = database.Database(
        engine="sqlite",
        filename=filename,
    )

    demands = cls.load_basic_table(db, "demand_summary")
    legs = cls.load_basic_table(db, "leg_summary")
    paths = cls.load_basic_table(db, "path_summary")
    carriers = cls.load_basic_table(db, "carrier_summary")

    summary = cls(
        demands=demands,
        legs=legs,
        paths=paths,
        carriers=carriers,
    )

    if make_indexes:
        if isinstance(make_indexes, dict):
            db.add_indexes(**make_indexes)
        else:
            db.add_indexes()

    logger.info("loading configs")
    config = db.load_configs(on_validation_error="ignore")
    try:
        scenario = config.scenario
        burn_samples = config.simulation_controls.burn_samples
    except AttributeError:
        scenario = config.get("scenario", "unknown")
        burn_samples = config.get("simulation_controls", {}).get(
            "burn_samples", 100
        )

    summary.load_additional_tables(
        db,
        scenario=scenario,
        burn_samples=burn_samples,
        additional=additional,
    )
    summary.cnx = db
    return summary

from_sqlite_glob classmethod

from_sqlite_glob(
    pattern: str,
    make_indexes: bool | dict = False,
    additional: Collection[str | tuple] | str | None = None,
    *,
    load_config: bool = True,
    max_num_files: int = 9999,
)

Load and aggregate multiple summary tables from a glob pattern.

Parameters:

  • pattern (str) –

    A glob pattern to match the files to load.

  • make_indexes (bool | dict, default: False ) –
  • additional (Collection[str | tuple] | str | None, default: None ) –
  • load_config (bool, default: True ) –

Returns:

Source code in passengersim/summary.py
@classmethod
def from_sqlite_glob(
    cls,
    pattern: str,
    make_indexes: bool | dict = False,
    additional: Collection[str | tuple] | str | None = None,
    *,
    load_config: bool = True,
    max_num_files: int = 9999,
):
    """
    Load and aggregate multiple summary tables from a glob pattern.

    Parameters
    ----------
    pattern : str
        A glob pattern to match the files to load.
    make_indexes
    additional
    load_config

    Returns
    -------
    SummaryTables
    """
    summarytables_is_deprecated()

    import glob

    cfg = None
    raw = []
    n = 0
    for filename in glob.glob(pattern):
        n += 1
        if n > max_num_files:
            continue
        raw.append(
            cls.from_sqlite(
                filename,
                make_indexes=make_indexes,
                additional=additional,
            )
        )
        if cfg is None and load_config:
            cfg = raw[-1].cnx.load_configs(on_validation_error="ignore")
    if n > max_num_files:
        warnings.warn(
            f"Only loaded {max_num_files} of {n} files matching pattern",
            stacklevel=2,
        )
    try:
        result = cls.aggregate(raw)
    except Exception as e:
        logger.error("Error aggregating summary tables: %s", e)
        logger.exception(e)
        return raw
    if cfg is not None:
        result.config = cfg
    return result

load_additional_tables

load_additional_tables(
    db: Database,
    scenario: str,
    burn_samples: int,
    additional: Collection[str | tuple] | str | None = (
        "fare_class_mix",
        "bookings_by_timeframe",
        "total_demand",
        "load_factor_distribution",
    ),
) -> None

Load additional summary tables based on common queries.

Parameters:

  • db (Database) –
  • scenario (str) –
  • burn_samples (int) –

    The number of samples in the burn period. The data from these samples is ignored in most common queries.

  • additional (Collection[str | tuple] | str, default: ('fare_class_mix', 'bookings_by_timeframe', 'total_demand', 'load_factor_distribution') ) –

    One or more additional tables to load. If "*", then this will load all common queries supported by the configuration used during the simulation.

Source code in passengersim/summary.py
def load_additional_tables(
    self,
    db: database.Database,
    scenario: str,
    burn_samples: int,
    additional: Collection[str | tuple] | str | None = (
        "fare_class_mix",
        "bookings_by_timeframe",
        "total_demand",
        "load_factor_distribution",
    ),
) -> None:
    """
    Load additional summary tables based on common queries.

    Parameters
    ----------
    db : Database
    scenario : str
    burn_samples : int
        The number of samples in the burn period.  The data from these samples
        is ignored in most common queries.
    additional : Collection[str | tuple] | str
        One or more additional tables to load.  If "*", then this will load
        all common queries supported by the configuration used during the
        simulation.
    """
    if isinstance(additional, str):
        if additional == "*":
            additional = set()
            cfg = db.load_configs(scenario)
            if "fare" in cfg.db.write_items:
                additional.add("fare_class_mix")
            if "fare_final" in cfg.db.write_items:
                additional.add("fare_class_mix")
            if "bookings" in cfg.db.write_items:
                additional.add("bookings_by_timeframe")
            if "demand" in cfg.db.write_items:
                additional.add("total_demand")
                additional.add("demand_to_come")
            if "demand_final" in cfg.db.write_items:
                additional.add("total_demand")
            if "bucket" in cfg.db.write_items:
                additional.add("leg_forecasts")
                additional.add("carrier_history")
            if "pathclass" in cfg.db.write_items:
                additional.add("path_forecasts")
                additional.add("local_and_flow_yields")
            if "pathclass_final" in cfg.db.write_items:
                additional.add("local_and_flow_yields")
            if "leg" in cfg.db.write_items and cfg.db.store_leg_bid_prices:
                additional.add("bid_price_history")
            if "leg" in cfg.db.write_items and cfg.db.store_displacements:
                additional.add("displacement_history")
            if "leg" in cfg.db.write_items or "leg_final" in cfg.db.write_items:
                additional.add("load_factor_distribution")
        else:
            additional = [additional]
    elif additional is None:
        additional = []

    if "fare_class_mix" in additional and db.is_open:
        logger.info("loading fare_class_mix")
        self.fare_class_mix = database.common_queries.fare_class_mix(
            db, scenario, burn_samples=burn_samples
        )
        if self.od_fare_class_mix:
            for orig, dest in list(self.od_fare_class_mix):
                self.od_fare_class_mix[(orig, dest)] = (
                    database.common_queries.od_fare_class_mix(
                        db, orig, dest, scenario, burn_samples=burn_samples
                    )
                )
    # load additional fare class mix tables
    for i in additional:
        if isinstance(i, tuple) and i[0] == "od_fare_class_mix" and db.is_open:
            orig, dest = i[1], i[2]
            if self.od_fare_class_mix is None:
                self.od_fare_class_mix = {}
            logger.info(f"loading od_fare_class_mix({orig},{dest})")
            self.od_fare_class_mix[(orig, dest)] = (
                database.common_queries.od_fare_class_mix(
                    db, orig, dest, scenario, burn_samples=burn_samples
                )
            )

    for i in additional:
        cutoffs = None
        if i == "load_factor_distribution" and db.is_open:
            cutoffs = (0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95)  # default cutoffs
        elif (
            isinstance(i, tuple)
            and i[0] == "load_factor_distribution"
            and db.is_open
        ):
            cutoffs = ast.literal_eval(i[1])
        if cutoffs is not None:
            logger.info("loading load_factor_distribution")
            self.load_factor_distribution = (
                database.common_queries.load_factor_distribution(
                    db,
                    scenario=scenario,
                    burn_samples=burn_samples,
                    cutoffs=cutoffs,
                )
            )

    if "bookings_by_timeframe" in additional and db.is_open:
        logger.info("loading bookings_by_timeframe")
        self.bookings_by_timeframe = database.common_queries.bookings_by_timeframe(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "total_demand" in additional and db.is_open:
        logger.info("loading total_demand")
        self.total_demand = database.common_queries.total_demand(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "leg_forecasts" in additional and db.is_open:
        logger.info("loading leg_forecasts")
        self.leg_forecasts = database.common_queries.leg_forecasts(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "path_forecasts" in additional and db.is_open:
        logger.info("loading path_forecasts")
        self.path_forecasts = database.common_queries.path_forecasts(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "demand_to_come" in additional and db.is_open:
        logger.info("loading demand_to_come")
        self.demand_to_come = database.common_queries.demand_to_come(
            db, scenario=scenario
        )

    if "demand_to_come_summary" in additional and db.is_open:
        logger.info("loading demand_to_come_summary")
        self.demand_to_come_summary = (
            database.common_queries.demand_to_come_summary(db, scenario=scenario)
        )

    if "carrier_history" in additional and db.is_open:
        logger.info("loading carrier_history")
        self.carrier_history = database.common_queries.carrier_history(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "bid_price_history" in additional and db.is_open:
        logger.info("loading bid_price_history")
        self.bid_price_history = database.common_queries.bid_price_history(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "displacement_history" in additional and db.is_open:
        logger.info("loading displacement_history")
        self.displacement_history = database.common_queries.displacement_history(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "local_and_flow_yields" in additional and db.is_open:
        logger.info("loading local_and_flow_yields")
        self.local_and_flow_yields = database.common_queries.local_and_flow_yields(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "leg_local_and_flow_by_class" in additional and db.is_open:
        logger.info("loading leg_local_and_flow_by_class")
        self.leg_carried = database.common_queries.leg_local_and_flow_by_class(
            db, scenario=scenario, burn_samples=burn_samples
        )

    if "edgar" in additional and db.is_open:
        logger.info("loading edgar")
        self.edgar = database.common_queries.edgar(
            db, scenario=scenario, burn_samples=burn_samples
        )

load_basic_table classmethod

load_basic_table(db: Database, tablename: str)

Load a basic table

Source code in passengersim/summary.py
@classmethod
def load_basic_table(self, db: database.Database, tablename: str):
    """Load a basic table"""
    logger.info("loading %s", tablename)
    return db.dataframe(f"SELECT * FROM {tablename}")

to_dataframe

to_dataframe(table) -> DataFrame

Convert the summary tables to a individual dataframes.

Source code in passengersim/summary.py
def to_dataframe(self, table) -> pd.DataFrame:
    """Convert the summary tables to a individual dataframes."""
    sheet_count = 0
    for k, v in self.__dict__.items():
        if isinstance(v, pd.DataFrame):
            sheet_count += 1
            if sheet_count == table:
                return v.assign(table=k)

    raise IndexError("There are fewer than", table, " DataFrames in the object")

to_pickle

to_pickle(
    filename: str | Path,
    add_timestamp_ext: bool = True,
    preserve_meta_trials: bool = False,
)

Save the object to a pickle file.

Parameters:

  • filename (str or Path - like) –

    The filename to save the object to.

  • add_timestamp_ext (bool, default: True ) –

    Add a timestamp extension to the filename.

Source code in passengersim/summary.py
def to_pickle(
    self,
    filename: str | pathlib.Path,
    add_timestamp_ext: bool = True,
    preserve_meta_trials: bool = False,
):
    """Save the object to a pickle file.

    Parameters
    ----------
    filename : str or Path-like
        The filename to save the object to.
    add_timestamp_ext : bool, default True
        Add a timestamp extension to the filename.
    """
    import pickle

    if add_timestamp_ext:
        filename = filename_with_timestamp(filename, suffix=".pkl")

    with open(filename, "wb") as f:
        self._preserve_meta_trials = preserve_meta_trials
        pickle.dump(self, f)
        del self._preserve_meta_trials

to_records

to_records() -> dict[str, list[dict]]

Convert all summary tables to a dictionary of records.

Source code in passengersim/summary.py
def to_records(self) -> dict[str, list[dict]]:
    """Convert all summary tables to a dictionary of records."""
    return {k: v.to_dict(orient="records") for (k, v) in self.__dict__.items()}

to_xlsx

to_xlsx(filename: str | Path) -> None

Write summary tables to excel.

Parameters:

  • filename (Path - like) –

    The excel file to write.

Source code in passengersim/summary.py
def to_xlsx(self, filename: str | pathlib.Path) -> None:
    """Write summary tables to excel.

    Parameters
    ----------
    filename : Path-like
        The excel file to write.
    """
    if isinstance(filename, str):
        filename = pathlib.Path(filename)
    filename.parent.mkdir(exist_ok=True, parents=True)
    with pd.ExcelWriter(filename) as writer:
        for k, v in self.__dict__.items():
            if isinstance(v, pd.DataFrame):
                v.to_excel(writer, sheet_name=k)

summarytables_is_deprecated

summarytables_is_deprecated()
Source code in passengersim/summary.py
def summarytables_is_deprecated():
    warnings.warn(
        "SummaryTables is deprecated and will be removed in a future version.",
        DeprecationWarning,
        stacklevel=2,
    )