Skip to content

Summary Results

logger module-attribute

logger = logging.getLogger('passengersim.summary')

SummaryTables

Source code in passengersim/summary.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
class SummaryTables:
    @classmethod
    def from_sqlite(
        cls,
        filename: str | pathlib.Path,
        make_indexes: bool = False,
        additional: Collection[str | tuple] | str | None = None,
    ):
        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:
            db.add_indexes()

        logger.info("loading configs")
        config = db.load_configs()

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

    @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",
        ),
    ) -> 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")
            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
                    )

        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
                )

        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, 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, 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, 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, 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)

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

        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, 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, burn_samples
            )

    def __init__(
        self,
        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,
        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,
        bid_price_history: pd.DataFrame | None = None,
        local_and_flow_yields: pd.DataFrame | None = None,
    ):
        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.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.bid_price_history = bid_price_history
        self.local_and_flow_yields = local_and_flow_yields

    def to_records(self):
        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 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").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):
        df = self.fare_class_mix.reset_index()[["carrier", "booking_class", "avg_sold"]]
        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})"
        )

    @property
    def raw_fare_class_mix(self) -> pd.DataFrame:
        """Raw data giving the fare class mix.

        This tidy dataframe contains these columns:

        - carrier (str)
        - booking_class (str)
        - avg_sold (float)
        """
        return self.fig_fare_class_mix(raw_df=True)

    @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 airlines 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", "rrd"])[f"avg_{c}"]
                    .sum()
                    .unstack(["trial", "carrier", "booking_class"])
                    .sort_index(ascending=False)
                    .apply(differs)
                    .stack(["carrier", "booking_class"])
                    .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", "rrd"])[f"avg_{c}"]
                    .sum()
                    .unstack(["trial", "carrier"])
                    .sort_index(ascending=False)
                    .apply(differs)
                    .stack("carrier")
                    .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"])

        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("(rrd>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 = ["rrd", "paxtype"]
            if by_class:
                g += ["booking_class"]
            df = df.groupby(g)[["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 = "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("rrd:O").scale(reverse=True).title("Days from Departure"),
                    y=alt.Y("sold"),
                    tooltip=(
                        [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                    )
                    + [
                        alt.Tooltip("paxtype", title="Passenger Type"),
                        alt.Tooltip("rrd", title="DfD"),
                        alt.Tooltip("sold", format=".2f"),
                    ],
                )
                .properties(
                    width=500,
                    height=200,
                )
                .facet(
                    row=alt.Row("paxtype:N", title="Passenger Type"),
                    title=title,
                )
                .configure_title(fontSize=18)
            )
        else:
            chart = (
                alt.Chart(df, title=title)
                .mark_line()
                .encode(
                    color=alt.Color(color).title(color_title),
                    x=alt.X("rrd:O").scale(reverse=True).title("Days from 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("rrd", 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", "rrd"])[f"avg_{c}"]
                .sum()
                .unstack(["trial", "carrier"])
                .sort_index(ascending=False)
                .apply(differs)
                .stack("carrier")
                .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("rrd>0")
        )
        if not by_carrier:
            df = (
                df.groupby(["rrd", "paxtype"])[["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("rrd:O").scale(reverse=True).title("Days from 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("rrd", 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("rrd:O").scale(reverse=True).title("Days from 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,
            )
        )

    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"
        )

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

        encoding = dict(
            x=alt.X("rrd:O").scale(reverse=True).title("Days from Departure"),
            y=alt.Y(f"{y}:Q", title="Avg Demand Forecast"),
        )
        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_flt_no: bool | int = True,
        by_class: bool | str = True,
        of: Literal["mu", "sigma"] = "mu",
        raw_df=False,
    ):
        y = "forecast_mean" if of == "mu" else "forecast_stdev"
        columns = [
            "carrier",
            "flt_no",
            "booking_class",
            "rrd",
            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_flt_no, int) and by_flt_no is not True:
            df = df[df.flt_no == by_flt_no]
        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)

    @report_figure
    def fig_path_forecasts(
        self,
        by_path_id: bool | int = True,
        by_class: bool | str = True,
        of: Literal["mu", "sigma", "closed"] = "mu",
        raw_df=False,
    ):
        of_columns = {
            "mu": "forecast_mean",
            "sigma": "forecast_stdev",
            "closed": "forecast_closed_in_tf",
        }
        y = of_columns.get(of)
        columns = [
            "path_id",
            "booking_class",
            "rrd",
            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("rrd:Q").scale(reverse=True).title("Days from 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("rrd:Q").scale(reverse=True).title("Days from 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("rrd:Q").scale(reverse=True).title("Days from 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

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

demand_to_come instance-attribute

demand_to_come = demand_to_come

demands instance-attribute

demands = demands

fare_class_mix instance-attribute

fare_class_mix = fare_class_mix

fares instance-attribute

fares = fares

leg_forecasts instance-attribute

leg_forecasts = leg_forecasts

legs instance-attribute

legs = legs

load_factors instance-attribute

load_factors = load_factors

local_and_flow_yields instance-attribute

local_and_flow_yields = local_and_flow_yields

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 property

raw_fare_class_mix: pd.DataFrame

Raw data giving the fare class mix.

This tidy dataframe contains these columns:

  • carrier (str)
  • booking_class (str)
  • avg_sold (float)

total_demand instance-attribute

total_demand = total_demand

__init__

__init__(
    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,
    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,
    bid_price_history: pd.DataFrame | None = None,
    local_and_flow_yields: pd.DataFrame | None = None,
)
Source code in passengersim/summary.py
def __init__(
    self,
    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,
    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,
    bid_price_history: pd.DataFrame | None = None,
    local_and_flow_yields: pd.DataFrame | None = None,
):
    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.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.bid_price_history = bid_price_history
    self.local_and_flow_yields = local_and_flow_yields

aggregate_demand_history

aggregate_demand_history(
    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:

  • 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() -> 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.

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("rrd:Q").scale(reverse=True).title("Days from 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("rrd:Q").scale(reverse=True).title("Days from 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("rrd:Q").scale(reverse=True).title("Days from 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 airlines 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", "rrd"])[f"avg_{c}"]
                .sum()
                .unstack(["trial", "carrier", "booking_class"])
                .sort_index(ascending=False)
                .apply(differs)
                .stack(["carrier", "booking_class"])
                .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", "rrd"])[f"avg_{c}"]
                .sum()
                .unstack(["trial", "carrier"])
                .sort_index(ascending=False)
                .apply(differs)
                .stack("carrier")
                .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"])

    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("(rrd>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 = ["rrd", "paxtype"]
        if by_class:
            g += ["booking_class"]
        df = df.groupby(g)[["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 = "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("rrd:O").scale(reverse=True).title("Days from Departure"),
                y=alt.Y("sold"),
                tooltip=(
                    [alt.Tooltip("carrier").title("Carrier")] if by_carrier else []
                )
                + [
                    alt.Tooltip("paxtype", title="Passenger Type"),
                    alt.Tooltip("rrd", title="DfD"),
                    alt.Tooltip("sold", format=".2f"),
                ],
            )
            .properties(
                width=500,
                height=200,
            )
            .facet(
                row=alt.Row("paxtype:N", title="Passenger Type"),
                title=title,
            )
            .configure_title(fontSize=18)
        )
    else:
        chart = (
            alt.Chart(df, title=title)
            .mark_line()
            .encode(
                color=alt.Color(color).title(color_title),
                x=alt.X("rrd:O").scale(reverse=True).title("Days from 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("rrd", 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_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_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):
    df = self.fare_class_mix.reset_index()[["carrier", "booking_class", "avg_sold"]]
    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_flt_no: bool | int = True,
    by_class: bool | str = True,
    of: Literal["mu", "sigma"] = "mu",
    raw_df=False,
)
Source code in passengersim/summary.py
@report_figure
def fig_leg_forecasts(
    self,
    by_flt_no: bool | int = True,
    by_class: bool | str = True,
    of: Literal["mu", "sigma"] = "mu",
    raw_df=False,
):
    y = "forecast_mean" if of == "mu" else "forecast_stdev"
    columns = [
        "carrier",
        "flt_no",
        "booking_class",
        "rrd",
        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_flt_no, int) and by_flt_no is not True:
        df = df[df.flt_no == by_flt_no]
    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)

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"] = "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"] = "mu",
    raw_df=False,
):
    of_columns = {
        "mu": "forecast_mean",
        "sigma": "forecast_stdev",
        "closed": "forecast_closed_in_tf",
    }
    y = of_columns.get(of)
    columns = [
        "path_id",
        "booking_class",
        "rrd",
        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)

from_sqlite classmethod

from_sqlite(
    filename: str | pathlib.Path,
    make_indexes: bool = 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 = False,
    additional: Collection[str | tuple] | str | None = None,
):
    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:
        db.add_indexes()

    logger.info("loading configs")
    config = db.load_configs()

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

load_additional_tables

load_additional_tables(
    db: database.Database,
    scenario: str,
    burn_samples: int,
    additional: Collection[str | tuple]
    | str
    | None = (
        "fare_class_mix",
        "bookings_by_timeframe",
        "total_demand",
    ),
) -> 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') ) –

    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",
    ),
) -> 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")
        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
                )

    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
            )

    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, 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, 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, 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, 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)

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

    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, 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, burn_samples
        )

load_basic_table classmethod

load_basic_table(db: database.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_records

to_records()
Source code in passengersim/summary.py
def to_records(self):
    return {k: v.to_dict(orient="records") for (k, v) in self.__dict__.items()}

to_xlsx

to_xlsx(filename: str | pathlib.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)