Esempio n. 1
0
def get_layout(circuit_id=-1, download_image=True, **kwargs):
    circuit_races = races[races["circuitId"] == circuit_id]
    circuit_rids = circuit_races.index
    circuit_years = sorted(circuit_races["year"].values.tolist())
    circuit_fastest_lap_data = fastest_lap_data[fastest_lap_data["raceId"].isin(circuit_rids)]
    circuit_quali = qualifying[qualifying["raceId"].isin(circuit_rids)]
    circuit_results = results[results["raceId"].isin(circuit_rids)]
    circuit_driver_standings = driver_standings[driver_standings["raceId"].isin(circuit_rids)]

    logging.info(f"Generating layout for mode CIRCUIT in circuit, circuit_id={circuit_id}")

    times_plot = PlotItem(generate_times_plot, [circuit_years, circuit_quali, circuit_fastest_lap_data, circuit_races,
                                                circuit_results, circuit_id],
                          COMMON_PLOT_DESCRIPTIONS["generate_times_plot"])

    description = u"DNF Plot \u2014 plots number of DNFs and DNF percent vs year for this circuit"
    dnf_plot = PlotItem(generate_dnf_plot, [circuit_years, circuit_results, circuit_races, circuit_id], description)

    description = u"Average Start Position minus Finish Position Plot \u2014 " \
                  u"How many places do drivers make up on average?"
    spmfp_plot = PlotItem(generate_spmfp_plot, [circuit_years, circuit_races, circuit_results], description)

    spvfp_scatter = PlotItem(generate_spvfp_scatter, [circuit_results, circuit_races, circuit_driver_standings],
                             COMMON_PLOT_DESCRIPTIONS["generate_spvfp_scatter"])

    mltr_fp_scatter = PlotItem(generate_mltr_fp_scatter, [circuit_results, circuit_races, circuit_driver_standings],
                               COMMON_PLOT_DESCRIPTIONS["generate_mltr_fp_scatter"])

    description = u"Circuit Results Table \u2014 table of results for every GP held at this circuit"
    circuit_results_table = PlotItem(generate_circuit_results_table, [circuit_years, circuit_races, circuit_results,
                                                                      circuit_quali, circuit_fastest_lap_data],
                                     description)

    description = u"Various statistics on this circuit along with an image of the circuit layout"
    circuit_stats = PlotItem(generate_stats_layout, [circuit_id, circuit_years, circuit_fastest_lap_data,
                                                     circuit_results, circuit_races], description,
                             kwargs=dict(download_image=download_image))

    description = u"Winner's Table \u2014 table of the drivers and constructors who have won the most at this circuit"
    winners_table = PlotItem(generate_winners_table, [circuit_years, circuit_results, circuit_races], description)

    circuit_name = get_circuit_name(circuit_id)
    header = generate_div_item(f"<h2><b>{circuit_name}</b></h2><br>")

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector([
        [header],
        [times_plot], [middle_spacer],
        [dnf_plot], [middle_spacer],
        [spmfp_plot], [middle_spacer],
        [spvfp_scatter, mltr_fp_scatter],
        [circuit_results_table],
        [circuit_stats],
        [winners_table]
    ])

    logging.info("Finished generating layout for mode CIRCUIT")

    return group
def get_layout(circuit_id=-1, driver_id=-1, constructor_id=-1, download_image=True, **kwargs):
    circuit_rids = races[races["circuitId"] == circuit_id].index.values
    driver_results = results[results["driverId"] == driver_id]
    cdc_results = driver_results[(driver_results["raceId"].isin(circuit_rids)) &
                                 (driver_results["constructorId"] == constructor_id)]

    logging.info(f"Generating layout for mode CIRCUITDRIVERCONSTRUCTOR in circuitdriverconstructor, "
                 f"circuit_id={circuit_id}, driver_id={driver_id}, constructor_id={constructor_id}")

    # Detect invalid combos
    if cdc_results.shape[0] == 0:
        return generate_error_layout(circuit_id, driver_id, constructor_id)

    cdc_rids = cdc_results["raceId"]
    cdc_races = races.loc[cdc_rids]
    cdc_years = cdc_races["year"].unique()
    driver_driver_standings = driver_standings[driver_standings["driverId"] == driver_id]
    circuit_fastest_lap_data = fastest_lap_data[fastest_lap_data["raceId"].isin(circuit_rids)]
    cdc_fastest_lap_data = circuit_fastest_lap_data[(circuit_fastest_lap_data["driver_id"] == driver_id) &
                                                    (circuit_fastest_lap_data["raceId"].isin(cdc_rids))]
    cdc_lap_times = lap_times[(lap_times["raceId"].isin(cdc_rids)) & (lap_times["driverId"] == driver_id)]
    circuit_results = results[results["raceId"].isin(circuit_rids)]
    constructor_results_idxs = []
    for idx, results_row in cdc_results.iterrows():
        constructor_id = results_row["constructorId"]
        rid = results_row["raceId"]
        results_slice = circuit_results[(circuit_results["constructorId"] == constructor_id) &
                                        (circuit_results["raceId"] == rid)]
        constructor_results_idxs.extend(results_slice.index.values.tolist())
    constructor_results = circuit_results.loc[constructor_results_idxs]

    positions_plot, positions_source = circuitdriver.generate_positions_plot(cdc_years, driver_driver_standings,
                                                                             driver_results, cdc_fastest_lap_data,
                                                                             cdc_races, driver_id)
    positions_plot = PlotItem(positions_plot, [], COMMON_PLOT_DESCRIPTIONS["generate_positions_plot"])

    win_plot = PlotItem(circuitdriver.generate_win_plot, [positions_source],
                        COMMON_PLOT_DESCRIPTIONS["generate_win_plot"])

    lap_time_dist = PlotItem(circuitdriver.generate_lap_time_plot, [cdc_lap_times, cdc_rids, circuit_id, driver_id],
                             COMMON_PLOT_DESCRIPTIONS["generate_times_plot"])

    spvfp_scatter = PlotItem(circuitdriver.generate_spvfp_scatter, [cdc_results, cdc_races, driver_driver_standings],
                             COMMON_PLOT_DESCRIPTIONS["generate_spvfp_scatter"])

    mltr_fp_scatter = PlotItem(circuitdriver.generate_mltr_fp_scatter, [cdc_results, cdc_races,
                                                                        driver_driver_standings],
                               COMMON_PLOT_DESCRIPTIONS["generate_mltr_fp_scatter"])

    finish_pos_dist = PlotItem(circuitdriver.generate_finishing_position_bar_plot, [cdc_results],
                               COMMON_PLOT_DESCRIPTIONS["generate_finishing_position_bar_plot"])

    teammate_comparison_line_plot = PlotItem(generate_teammate_comparison_line_plot, [positions_source,
                                                                                      constructor_results, driver_id],
                                             COMMON_PLOT_DESCRIPTIONS["generate_teammate_comparison_line_plot"])

    description = u"Results Table \u2014 table containing results for every time this driver raced at this circuit " \
                  u"with this constructor"
    results_table = PlotItem(circuitdriver.generate_results_table, [cdc_results, cdc_fastest_lap_data, circuit_results,
                                                                    circuit_fastest_lap_data], description)

    description = u"Various statistics on this driver at this circuit with this constructor"
    stats_div = PlotItem(circuitdriver.generate_stats_layout, [cdc_years, cdc_races, cdc_results, cdc_fastest_lap_data,
                                                               positions_source, circuit_id, driver_id], description)

    if download_image:
        # Track image
        circuit_row = circuits.loc[circuit_id]
        image_url = str(circuit_row["imgUrl"])
        image_view = plot_image_url(image_url)
        disclaimer = Div(text="The image is of the current configuration of the track.")
        image_view = column([image_view, disclaimer], sizing_mode="stretch_both")
    else:
        image_view = Div()
    image_view = PlotItem(image_view, [], "", listed=False)

    header = generate_div_item(f"<h2><b>{get_driver_name(driver_id)} at {get_circuit_name(circuit_id)} with "
                               f"{get_constructor_name(constructor_id)}</b></h2><br>")

    middle_spacer = generate_spacer_item()
    layout = generate_plot_list_selector([
        [header],
        [positions_plot], [middle_spacer],
        [win_plot], [middle_spacer],
        [lap_time_dist], [middle_spacer],
        [spvfp_scatter, mltr_fp_scatter], [middle_spacer],
        [finish_pos_dist],
        [teammate_comparison_line_plot],
        [image_view],
        [results_table],
        [stats_div]
    ])

    logging.info("Finished generating layout for mode CIRCUITDRIVERCONSTRUCTOR")

    return layout
Esempio n. 3
0
def get_layout(year_id=-1,
               circuit_id=-1,
               driver_id=-1,
               download_image=True,
               **kwargs):
    # Generate slices
    year_races = races[races["year"] == year_id]
    race = year_races[year_races["circuitId"] == circuit_id]
    if race.shape[0] == 0:
        return generate_error_layout(year_id=year_id,
                                     circuit_id=circuit_id,
                                     driver_id=driver_id)
    rid = race.index.values[0]
    race_results = results[results["raceId"] == rid]
    ycd_results = race_results[race_results["driverId"] == driver_id]

    logging.info(
        f"Generating layout for mode YEARCIRCUITDRIVER in yearcircuitdriver, year_id={year_id}, "
        f"circuit_id={circuit_id}, driver_id={driver_id}")

    if ycd_results.shape[0] == 0:
        return generate_error_layout(year_id=year_id,
                                     circuit_id=circuit_id,
                                     driver_id=driver_id)

    # Generate more slices
    race_laps = lap_times[lap_times["raceId"] == rid]
    ycd_laps = race_laps[race_laps["driverId"] == driver_id]
    ycd_pit_stop_data = pit_stop_data[(pit_stop_data["raceId"] == rid) &
                                      (pit_stop_data["driverId"] == driver_id)]
    race_quali = quali[quali["raceId"] == rid]
    ycd_fastest_lap_data = fastest_lap_data[
        (fastest_lap_data["raceId"] == rid)
        & (fastest_lap_data["driver_id"] == driver_id)]
    year_driver_standings = driver_standings[driver_standings["raceId"].isin(
        year_races.index.values)]

    disclaimer_sc, sc_starts, sc_ends = detect_safety_car(race_laps, race)
    disclaimer_sc = PlotItem(disclaimer_sc, [], "", listed=False)
    disclaimer_overtakes, overtake_data = detect_overtakes(ycd_laps, race_laps)
    disclaimer_overtakes = PlotItem(disclaimer_overtakes, [], "", listed=False)

    gap_plot, cached_driver_map = generate_gap_plot(
        race_laps,
        race_results,
        driver_id,
        sc_starts=sc_starts,
        sc_ends=sc_ends,
        overtake_data=overtake_data,
        ycd_pit_stop_data=ycd_pit_stop_data)
    gap_plot = PlotItem(gap_plot, [],
                        COMMON_PLOT_DESCRIPTIONS["generate_gap_plot"])

    position_plot = PlotItem(
        generate_position_plot,
        [race_laps, race_results, cached_driver_map, driver_id],
        COMMON_PLOT_DESCRIPTIONS["generate_position_plot"],
        kwargs=dict(sc_starts=sc_starts,
                    sc_ends=sc_ends,
                    ycd_pit_stop_data=ycd_pit_stop_data))

    lap_time_plot = PlotItem(
        generate_lap_time_plot,
        [race_laps, race_results, cached_driver_map, driver_id],
        COMMON_PLOT_DESCRIPTIONS["generate_times_plot"],
        kwargs=dict(sc_starts=sc_starts,
                    sc_ends=sc_ends,
                    overtake_data=overtake_data))

    description = u"Qualifying Table \u2014 table containing information about qualifying times and rank"
    quali_table, quali_source = generate_quali_table(race_quali, race_results,
                                                     driver_id)
    quali_table = PlotItem(quali_table, [], description)

    description = u"Various statistics on this driver at this race"
    stats_layout = PlotItem(generate_stats_layout, [
        ycd_results, ycd_pit_stop_data, ycd_fastest_lap_data,
        year_driver_standings, race_results, quali_source, rid, circuit_id,
        driver_id
    ],
                            description,
                            kwargs=dict(download_image=download_image))

    driver_name = get_driver_name(driver_id)
    race_name = get_race_name(rid, include_year=True)
    header = generate_div_item(
        f"<h2><b>What did {driver_name}'s {race_name} look like?"
        f"</b></h2><br><i>Yellow dashed vertical lines show the start of a safety car period, "
        f"orange vertical lines show the end*. "
        f"<br>The white line marks the fastest lap of the race."
        f"<br>Green lines show overtakes and red lines show being overtaken**."
        f"<br>Pink lines show pit stops along with how long was spent in the pits.</i>"
    )

    middle_spacer = generate_spacer_item()
    layout = generate_plot_list_selector([[header], [gap_plot],
                                          [middle_spacer], [position_plot],
                                          [middle_spacer], [lap_time_plot],
                                          [middle_spacer], [disclaimer_sc],
                                          [disclaimer_overtakes],
                                          [quali_table], [stats_layout]])

    logging.info("Finished generating layout for mode YEARCIRCUITDRIVER")

    return layout
Esempio n. 4
0
def get_layout(year_id=-1, driver_id=-1, download_image=True, **kwargs):
    year_races = races[races["year"] == year_id]
    year_rids = sorted(year_races.index.values)
    year_results = results[results["raceId"].isin(year_rids)].sort_values(
        by="raceId")

    # Detect invalid combos
    if driver_id not in year_results["driverId"].unique():
        return generate_error_layout(year_id, driver_id)

    # Generate more slices
    year_driver_standings = driver_standings[driver_standings["raceId"].isin(
        year_rids)].sort_values(by="raceId")
    yd_driver_standings = year_driver_standings[
        year_driver_standings["driverId"] == driver_id]
    yd_results = year_results[year_results["driverId"] == driver_id]
    year_fastest_lap_data = fastest_lap_data[fastest_lap_data["raceId"].isin(
        year_rids)]
    yd_fastest_lap_data = year_fastest_lap_data[
        year_fastest_lap_data["driver_id"] == driver_id]
    yd_races = year_races
    constructor_results_idxs = []
    for idx, results_row in yd_results.iterrows():
        cid = results_row["constructorId"]
        rid = results_row["raceId"]
        constructor_results_idxs.extend(year_results[
            (year_results["raceId"] == rid)
            & (year_results["constructorId"] == cid)].index.values.tolist())
    constructor_results = year_results.loc[constructor_results_idxs]

    logging.info(
        f"Generating layout for mode YEARDRIVER in yeardriver, year_id={year_id}, driver_id={driver_id}"
    )

    wdc_plot = PlotItem(
        generate_wdc_plot,
        [year_races, year_driver_standings, year_results, driver_id],
        COMMON_PLOT_DESCRIPTIONS["generate_wdc_plot"])

    positions_plot, positions_source = generate_positions_plot(
        yd_driver_standings, yd_results, yd_fastest_lap_data, year_id,
        driver_id)
    positions_plot = PlotItem(
        positions_plot, [],
        COMMON_PLOT_DESCRIPTIONS["generate_positions_plot"])

    win_plot = PlotItem(generate_win_plot, [positions_source, year_results],
                        COMMON_PLOT_DESCRIPTIONS["generate_win_plot"])

    finishing_position_bar_plot = PlotItem(
        generate_finishing_position_bar_plot, [yd_results],
        COMMON_PLOT_DESCRIPTIONS["generate_finishing_position_bar_plot"])

    spvfp_scatter = PlotItem(
        generate_spvfp_scatter, [yd_results, yd_races, yd_driver_standings],
        COMMON_PLOT_DESCRIPTIONS["generate_spvfp_scatter"])

    mltr_fp_scatter = PlotItem(
        generate_mltr_fp_scatter, [yd_results, yd_races, yd_driver_standings],
        COMMON_PLOT_DESCRIPTIONS["generate_mltr_fp_scatter"])

    teammate_comparison_line_plot, comparison_source = generate_teammate_comparison_line_plot(
        positions_source, constructor_results, yd_results, driver_id)
    teammate_comparison_line_plot = PlotItem(
        teammate_comparison_line_plot, [],
        COMMON_PLOT_DESCRIPTIONS["generate_teammate_comparison_line_plot"])

    description = u"Results Table \u2014 table of results for race this driver raced at this year"
    results_table = PlotItem(generate_results_table, [
        yd_results, yd_fastest_lap_data, year_results, year_fastest_lap_data,
        driver_id
    ], description)

    description = u"Various statistics on this driver during this year"
    stats_layout = PlotItem(generate_stats_layout, [
        positions_source, comparison_source, constructor_results, year_id,
        driver_id
    ], description)

    header = generate_div_item(
        f"<h2><b>{get_driver_name(driver_id)} in {year_id}</b></h2><br>")

    if download_image:
        image_url = str(drivers.loc[driver_id, "imgUrl"])
        image_view = plot_image_url(image_url)
    else:
        image_view = Div()
    image_view = PlotItem(image_view, [], "", listed=False)

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector(
        [[header], [wdc_plot], [middle_spacer], [positions_plot],
         [middle_spacer], [win_plot], [middle_spacer],
         [finishing_position_bar_plot], [middle_spacer],
         [spvfp_scatter, mltr_fp_scatter], [middle_spacer],
         [teammate_comparison_line_plot], [results_table],
         [image_view, generate_vdivider_item(), stats_layout]])

    logging.info("Finished generating layout for mode YEARDRIVER")

    return group
def get_layout(year_id=-1,
               circuit_id=-1,
               constructor_id=-1,
               download_image=True,
               **kwargs):
    # Generate slices
    year_races = races[races["year"] == year_id]
    race = year_races[year_races["circuitId"] == circuit_id]
    if race.shape[0] == 0:
        return generate_error_layout(year_id=year_id,
                                     circuit_id=circuit_id,
                                     constructor_id=constructor_id)
    rid = race.index.values[0]
    race_results = results[results["raceId"] == rid]
    ycc_results = race_results[race_results["constructorId"] == constructor_id]

    logging.info(
        f"Generating layout for mode YEARCIRCUITCONSTRUCTOR in yearcircuitconstructor, year_id={year_id}, "
        f"circuit_id={circuit_id}, constructor_id={constructor_id}")

    if ycc_results.shape[0] == 0:
        return generate_error_layout(year_id=year_id,
                                     circuit_id=circuit_id,
                                     constructor_id=constructor_id)

    # Generate more slices
    driver_ids = ycc_results["driverId"].unique()
    ycc_pit_stop_data = pit_stop_data[(pit_stop_data["raceId"] == rid) & (
        pit_stop_data["driverId"].isin(driver_ids))]
    race_laps = lap_times[lap_times["raceId"] == rid]
    race_quali = quali[quali["raceId"] == rid]
    ycc_fastest_lap_data = fastest_lap_data[
        (fastest_lap_data["raceId"] == rid)
        & (fastest_lap_data["driver_id"].isin(driver_ids))]
    year_driver_standings = driver_standings[driver_standings["raceId"].isin(
        year_races.index.values)]
    year_constructor_standings = constructor_standings[
        constructor_standings["raceId"].isin(year_races.index.values)]

    disclaimer_sc, sc_starts, sc_ends = detect_safety_car(race_laps, race)
    disclaimer_sc = PlotItem(disclaimer_sc, [], "", listed=False)

    gap_plot, cached_driver_map = generate_gap_plot(
        race_laps,
        race_results,
        driver_ids,
        constructor_id,
        sc_starts=sc_starts,
        sc_ends=sc_ends,
        ycc_pit_stop_data=ycc_pit_stop_data)
    gap_plot = PlotItem(gap_plot, [],
                        COMMON_PLOT_DESCRIPTIONS["generate_gap_plot"])

    position_plot = PlotItem(
        generate_position_plot, [
            race_laps, race_results, cached_driver_map, driver_ids,
            constructor_id
        ],
        COMMON_PLOT_DESCRIPTIONS["generate_position_plot"],
        kwargs=dict(sc_starts=sc_starts,
                    sc_ends=sc_ends,
                    ycc_pit_stop_data=ycc_pit_stop_data))

    lap_time_plot = PlotItem(generate_lap_time_plot, [
        race_laps, race_results, cached_driver_map, driver_ids, constructor_id
    ],
                             COMMON_PLOT_DESCRIPTIONS["generate_times_plot"],
                             kwargs=dict(sc_starts=sc_starts, sc_ends=sc_ends))

    quali_table, quali_source = generate_quali_table(race_quali, race_results,
                                                     driver_ids)
    quali_table = PlotItem(quali_table, [], "add descr. " * 5)

    stats_layout = PlotItem(generate_stats_layout, [
        ycc_results, ycc_pit_stop_data, ycc_fastest_lap_data,
        year_driver_standings, year_constructor_standings, quali_source, rid,
        circuit_id, constructor_id, driver_ids
    ],
                            "add descr. " * 5,
                            kwargs=dict(download_image=download_image))

    constructor_name = get_constructor_name(constructor_id)
    race_name = get_race_name(rid, include_year=True)
    header = generate_div_item(
        f"<h2><b>What did {constructor_name}'s {race_name} look like?"
        f"</b></h2><br><i>Yellow dashed vertical lines show the start of a safety car period, "
        f"orange vertical lines show the end.*"
        f"<br>The white line marks the fastest lap of the race."
        f"<br>Pink lines show pit stops along with how long was spent in the pits."
    )

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector([[header], [gap_plot], [middle_spacer],
                                         [position_plot], [middle_spacer],
                                         [lap_time_plot], [middle_spacer],
                                         [disclaimer_sc], [quali_table],
                                         [stats_layout]])

    logging.info("Finished generating layout for mode YEARCIRCUITCONSTRUCTOR")

    return group
Esempio n. 6
0
def get_layout(year_id=-1,
               driver_id=-1,
               constructor_id=-1,
               download_image=True,
               **kwargs):
    # Generate slices
    year_races = races[races["year"] == year_id]
    year_rids = sorted(year_races.index.values)
    year_results = results[results["raceId"].isin(year_rids)]
    ydc_results = year_results[(year_results["driverId"] == driver_id) & (
        year_results["constructorId"] == constructor_id)].sort_values(
            by="raceId")

    logging.info(
        f"Generating layout for mode YEARDRIVERCONSTRUCTOR in yeardriverconstructor, year_id={year_id}, "
        f"driver_id={driver_id}, constructor_id={constructor_id}")

    # Check if valid
    if ydc_results.shape[0] == 0:
        return generate_error_layout(year_id, driver_id, constructor_id)

    # Generate more slices
    yd_races = year_races.loc[ydc_results["raceId"].values]
    year_driver_standings = driver_standings[driver_standings["raceId"].isin(
        year_rids)]
    yd_driver_standings = year_driver_standings[
        year_driver_standings["driverId"] == driver_id]
    year_constructor_standings = constructor_standings[
        constructor_standings["raceId"].isin(year_rids)]
    year_constructor_standings = year_constructor_standings.sort_values(
        by="raceId")
    year_fastest_lap_data = fastest_lap_data[fastest_lap_data["raceId"].isin(
        year_rids)]
    yd_fastest_lap_data = year_fastest_lap_data[
        year_fastest_lap_data["driver_id"] == driver_id]
    constructor_results = year_results[year_results["constructorId"] ==
                                       constructor_id]

    wdc_plot = PlotItem(
        generate_wdc_plot,
        [year_races, year_driver_standings, year_results, driver_id],
        COMMON_PLOT_DESCRIPTIONS["generate_wdc_plot"])

    wcc_plot = PlotItem(
        generate_wcc_plot,
        [year_races, year_constructor_standings, year_results, constructor_id],
        COMMON_PLOT_DESCRIPTIONS["generate_wcc_plot"])

    positions_plot, positions_source = generate_positions_plot(
        ydc_results, yd_driver_standings, yd_fastest_lap_data, driver_id,
        year_id)
    positions_plot = PlotItem(
        positions_plot, [],
        COMMON_PLOT_DESCRIPTIONS["generate_positions_plot"])

    spvfp_scatter = PlotItem(
        generate_spvfp_scatter, [ydc_results, yd_races, yd_driver_standings],
        COMMON_PLOT_DESCRIPTIONS["generate_spvfp_scatter"])

    mltr_fp_scatter = PlotItem(
        generate_mltr_fp_scatter, [ydc_results, yd_races, yd_driver_standings],
        COMMON_PLOT_DESCRIPTIONS["generate_mltr_fp_scatter"])

    win_plot = PlotItem(generate_win_plot, [positions_source, driver_id],
                        COMMON_PLOT_DESCRIPTIONS["generate_win_plot"])

    position_dist = PlotItem(
        generate_finishing_position_bar_plot, [ydc_results],
        COMMON_PLOT_DESCRIPTIONS["generate_finishing_position_bar_plot"])

    teammatefp_fp_scatter = PlotItem(
        generate_teammatefp_fp_scatter,
        [positions_source, constructor_results, driver_id],
        COMMON_PLOT_DESCRIPTIONS["generate_teammatefp_fp_scatter"])

    teammate_diff_plot = PlotItem(
        generate_teammate_diff_comparison_scatter,
        [positions_source, constructor_results, driver_id],
        COMMON_PLOT_DESCRIPTIONS["generate_teammate_diff_comparison_scatter"])

    teammate_comparison_line_plot, comparison_source = generate_teammate_comparison_line_plot(
        positions_source, constructor_results, driver_id)
    teammate_comparison_line_plot = PlotItem(
        teammate_comparison_line_plot, [],
        COMMON_PLOT_DESCRIPTIONS["generate_teammate_comparison_line_plot"])

    description = u"Results Table \u2014 table of results for every race this driver competed at this year with " \
                  u"this constructor"
    results_table = PlotItem(generate_results_table, [
        ydc_results, yd_fastest_lap_data, year_results, year_fastest_lap_data,
        driver_id
    ], description)

    description = u"Various statistics on this driver at this constructor during this year"
    stats_layout = PlotItem(generate_stats_layout, [
        positions_source, comparison_source, constructor_results, year_id,
        driver_id
    ], description)

    if download_image:
        image_url = str(drivers.loc[driver_id, "imgUrl"])
        image_view = plot_image_url(image_url)
    else:
        image_view = Div()
    image_view = PlotItem(image_view, [], "", listed=False)

    driver_name = get_driver_name(driver_id)
    constructor_name = get_constructor_name(constructor_id)
    header = generate_div_item(
        f"<h2><b>What did {driver_name}'s {year_id} season with "
        f"{constructor_name} look like?</b></h2>")

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector(
        [[header], [wdc_plot], [middle_spacer], [wcc_plot], [middle_spacer],
         [positions_plot], [middle_spacer], [win_plot], [middle_spacer],
         [spvfp_scatter, mltr_fp_scatter], [middle_spacer], [position_dist],
         [middle_spacer], [teammatefp_fp_scatter, teammate_diff_plot],
         [middle_spacer], [teammate_comparison_line_plot], [results_table],
         [image_view, generate_vdivider_item(), stats_layout]])

    logging.info("Finished generating layout for mode YEARDRIVERCONSTRUCTOR")

    return group
Esempio n. 7
0
def get_layout(circuit_id=-1,
               constructor_id=-1,
               download_image=True,
               **kwargs):
    # Grab some useful slices
    circuit_rids = races[races["circuitId"] == circuit_id].index.values
    constructor_results = results[results["constructorId"] == constructor_id]
    cc_results = constructor_results[constructor_results["raceId"].isin(
        circuit_rids)]
    cc_rids = cc_results["raceId"]
    cc_races = races[races.index.isin(cc_rids)]

    logging.info(
        f"Generating layout for mode CIRCUITCONSTRUCTOR in circuitconstructor, circuit_id={circuit_id}, "
        f"constructor_id={constructor_id}")

    # Detect invalid combos
    if cc_races.shape[0] == 0:
        return generate_error_layout(circuit_id, constructor_id)

    # Generate some more slices
    circuit_results = results[results["raceId"].isin(circuit_rids)]
    circuit_fastest_lap_data = fastest_lap_data[
        fastest_lap_data["raceId"].isin(circuit_rids)]
    cc_years = cc_races["year"].unique()
    cc_years.sort()
    cc_fastest_lap_data_idxs = []
    cc_lap_time_idxs = []
    cc_driver_standings_idxs = []
    for idx, results_row in cc_results.iterrows():
        rid = results_row["raceId"]
        did = results_row["driverId"]
        fl_slice = circuit_fastest_lap_data[
            circuit_fastest_lap_data["driver_id"] == did]
        cc_fastest_lap_data_idxs.extend(fl_slice.index.values.tolist())
        lt_slice = lap_times[(lap_times["raceId"] == rid)
                             & (lap_times["driverId"] == did)]
        cc_lap_time_idxs.extend(lt_slice.index.values.tolist())
        driver_standings_slice = driver_standings[
            (driver_standings["raceId"] == rid)
            & (driver_standings["driverId"] == did)]
        cc_driver_standings_idxs.extend(
            driver_standings_slice.index.values.tolist())
    cc_fastest_lap_data = circuit_fastest_lap_data.loc[
        cc_fastest_lap_data_idxs]
    cc_lap_times = lap_times.loc[cc_lap_time_idxs]
    cc_driver_standings = driver_standings.loc[cc_driver_standings_idxs]
    constructor_constructor_standings = constructor_standings[
        constructor_standings["constructorId"] == constructor_id]

    positions_plot, positions_source = generate_positions_plot(
        cc_years, cc_fastest_lap_data, constructor_results,
        constructor_constructor_standings, cc_races, constructor_id)
    positions_plot = PlotItem(
        positions_plot, [],
        COMMON_PLOT_DESCRIPTIONS["generate_positions_plot"])

    win_plot = PlotItem(generate_win_plot, [positions_source, constructor_id],
                        COMMON_PLOT_DESCRIPTIONS["generate_win_plot"])

    lap_time_distribution_plot = PlotItem(
        generate_lap_time_plot,
        [cc_lap_times, cc_rids, circuit_id, constructor_id],
        COMMON_PLOT_DESCRIPTIONS["generate_times_plot"])

    finish_position_bar_plot = PlotItem(
        generate_finishing_position_bar_plot, [cc_results],
        COMMON_PLOT_DESCRIPTIONS["generate_finishing_position_bar_plot"])

    spvfp_scatter = PlotItem(
        generate_spvfp_scatter, [cc_results, cc_races, cc_driver_standings],
        COMMON_PLOT_DESCRIPTIONS["generate_spvfp_scatter"])

    mltr_fp_scatter = PlotItem(
        generate_mltr_fp_scatter, [cc_results, cc_races, cc_driver_standings],
        COMMON_PLOT_DESCRIPTIONS["generate_mltr_fp_scatter"])

    description = u"Various statistics on this constructor at this circuit"
    stats_div = PlotItem(generate_stats_layout, [
        cc_years, cc_races, cc_results, cc_fastest_lap_data, positions_source,
        circuit_id, constructor_id
    ], description)

    description = u"Results Table \u2014 table showing this constructor's results at every race this circuit"
    results_table = PlotItem(generate_results_table, [
        cc_results, cc_fastest_lap_data, circuit_results,
        circuit_fastest_lap_data
    ], description)

    if download_image:
        # Track image
        circuit_row = circuits.loc[circuit_id]
        image_url = str(circuit_row["imgUrl"])
        image_view = plot_image_url(image_url)
        disclaimer = Div(
            text="The image is of the current configuration of the track.")
        image_view = column([image_view, disclaimer],
                            sizing_mode="stretch_both")
    else:
        image_view = Div()
    image_view = PlotItem(image_view, [], "", listed=False)

    circuit_name = get_circuit_name(circuit_id)
    constructor_name = get_constructor_name(constructor_id)
    header = generate_div_item(
        f"<h2><b>What did/does {constructor_name}'s performance at "
        f"{circuit_name} look like?</b></h2><br>")

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector([[header], [positions_plot],
                                         [middle_spacer], [win_plot],
                                         [middle_spacer],
                                         [lap_time_distribution_plot],
                                         [middle_spacer],
                                         [finish_position_bar_plot],
                                         [middle_spacer],
                                         [spvfp_scatter, mltr_fp_scatter],
                                         [middle_spacer], [image_view],
                                         [stats_div], [results_table]])

    logging.info("Finished generating layout for mode CIRCUITCONSTRUCTOR")

    return group
Esempio n. 8
0
def get_layout(**kwargs):
    logging.info(f"Generating layout for mode ALLDRIVERS in alldrivers")

    description = u"Drivers Win Plot \u2014 plots the win percentage vs years of experience for every driver who has " \
                  u"won at least one race"
    all_drivers_win_plot = PlotItem(generate_all_drivers_win_plot, [], description)

    description = u"Start Position vs Finish Position Scatter Plot \u2014 each dot on this plot represents a " \
                  u"driver's career average start position vs finish position, and can show drivers who often made " \
                  u"up many places during races"
    spvfp_scatter = PlotItem(generate_career_spvfp_scatter, [], description)

    description = u"Start Position vs WDC Position Scatter Plot \u2014 each dot on this plot represents a driver's " \
                  u"career average start position vs World Drivers' Championship finish position, and is more " \
                  u"outlier-resistant than the above plot"
    sp_position_scatter = PlotItem(generate_sp_position_scatter, [], description)

    description = u"Average Lap Time Rank vs Finish Position Scatter Plot \u2014 each dot on this plot represents " \
                  u"a driver's career, and can show drivers who often out-drove their car"
    mltr_fp_scatter = PlotItem(generate_career_mltr_fp_scatter, [], description)

    description = u"Average Lap Time Rank vs WDC Finish Position Scatter Plot \u2014 each dot on this plot " \
                  u"represents a driver's career, and provides a different perspective on drivers who often " \
                  u"out-drove their car"
    mltr_position_scatter = PlotItem(generate_career_mltr_position_scatter, [], description)

    description = u"DNF Percent vs Podium Percent Scatter Plot \u2014 each dot on this plot represents a driver's " \
                  u"career and can show drivers who often did well despite an unreliable car, or vice-versa"
    dnf_podium_scatter = PlotItem(generate_dnf_podium_scatter, [], description)

    description = u"DNF Percent vs Win Percent Scatter Plot \u2014 each dot on this plot represents a driver's " \
                  u"career and can show drivers who often did well despite an unreliable car, or vice-versa " \
                  u"(a different perspective)"
    dnf_win_scatter = PlotItem(generate_dnf_win_scatter, [], description)

    description = u"DNF Percent vs Avg. Finish Position Scatter Plot \u2014 each dot on this plot represents a " \
                  u"driver's career and can show drivers who often did well despite an unreliable car, or vice-versa " \
                  u"(a third perspective)"
    dnf_fp_scatter = PlotItem(generate_dnf_fp_scatter, [], description)

    description = u"Winners Pie Chart \u2014 pie chart showing who won each race and World Drivers' Championship by " \
                  u"percentage"
    winners_pie = PlotItem(generate_winners_pie, [], description)

    description = u"All Drivers Table \u2014 table of every driver, including basic information such as who they " \
                  u"raced for, when they raced, and how well they did"
    drivers_table = PlotItem(generate_drivers_table, [], description)

    header = generate_div_item("<h2><b>All Drivers</b></h2>")

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector([
        [header],
        [all_drivers_win_plot], [middle_spacer],
        [spvfp_scatter], [middle_spacer],
        [sp_position_scatter], [middle_spacer],
        [mltr_fp_scatter], [middle_spacer],
        [mltr_position_scatter], [middle_spacer],
        [dnf_podium_scatter], [middle_spacer],
        [dnf_win_scatter], [middle_spacer],
        [dnf_fp_scatter], [middle_spacer],
        [winners_pie],
        [drivers_table]
    ], header_addition="<br><i>Please note that </i><b>each</b><i> plot takes about 10 seconds to generate</i>")

    logging.info("Finished generating layout for mode ALLDRIVERS")

    return group
Esempio n. 9
0
def get_layout(year_id=-1, constructor_id=-1, **kwargs):
    if year_id < 1958:
        return Div(text="The constructor's championship did not exist this year! It started in 1958.")
    year_races = races[races["year"] == year_id]
    year_results = results[results["raceId"].isin(year_races.index)]
    yc_results = year_results[year_results["constructorId"] == constructor_id]

    # Detect if it is a valid combo
    if yc_results.shape[0] == 0:
        return generate_error_layout(year_id, constructor_id)

    # Generate more slices
    year_constructor_standings = constructor_standings[constructor_standings["raceId"].isin(year_races.index)]
    year_constructor_standings = year_constructor_standings.sort_values(by="raceId")
    yc_constructor_standings = year_constructor_standings[year_constructor_standings["constructorId"] == constructor_id]
    year_fastest_lap_data = fastest_lap_data[fastest_lap_data["raceId"].isin(year_races.index)]
    yc_fastest_lap_data_idxs = []
    yc_driver_standings_idxs = []
    for idx, results_row in yc_results.iterrows():
        rid = results_row["raceId"]
        did = results_row["driverId"]
        fl_slice = year_fastest_lap_data[(year_fastest_lap_data["raceId"] == rid) &
                                         (year_fastest_lap_data["driver_id"] == did)]
        yc_fastest_lap_data_idxs.extend(fl_slice.index.values.tolist())
        driver_standings_slice = driver_standings[(driver_standings["raceId"] == rid) &
                                                  (driver_standings["driverId"] == did)]
        yc_driver_standings_idxs.extend(driver_standings_slice.index.values.tolist())
    yc_fastest_lap_data = fastest_lap_data.loc[yc_fastest_lap_data_idxs]
    yc_driver_standings = driver_standings.loc[yc_driver_standings_idxs]
    yc_races = year_races[year_races.index.isin(yc_results["raceId"])]

    logging.info(f"Generating layout for mode YEARCONSTRUCTOR in yearconstructor, year_id={year_id}, "
                 f"constructor_id={constructor_id}")

    wcc_plot = PlotItem(generate_wcc_plot, [year_races, year_constructor_standings, year_results, constructor_id],
                        COMMON_PLOT_DESCRIPTIONS["generate_wcc_plot"])

    positions_plot, positions_source = generate_positions_plot(yc_constructor_standings, yc_results,
                                                               yc_fastest_lap_data, year_id, constructor_id)
    positions_plot = PlotItem(positions_plot, [], COMMON_PLOT_DESCRIPTIONS["generate_positions_plot"])

    spvfp_scatter = PlotItem(generate_spvfp_scatter, [yc_results, yc_races, yc_driver_standings],
                             COMMON_PLOT_DESCRIPTIONS["generate_spvfp_scatter"])

    mltr_fp_scatter = PlotItem(generate_mltr_fp_scatter, [yc_results, yc_races, yc_driver_standings],
                               COMMON_PLOT_DESCRIPTIONS["generate_mltr_fp_scatter"])

    win_plot = PlotItem(generate_win_plot, [positions_source, constructor_id],
                        COMMON_PLOT_DESCRIPTIONS["generate_win_plot"])

    finishing_position_bar_plot = PlotItem(generate_finishing_position_bar_plot, [yc_results],
                                           COMMON_PLOT_DESCRIPTIONS["generate_finishing_position_bar_plot"])

    description = u"Driver Performance Table \u2014 table showing the performance of every driver that competed for " \
                  u"this constructor for this season, including number of races, podiums, wins, podiums, etc."
    driver_performance_layout = PlotItem(generate_driver_performance_table, [yc_races, yc_results], description)

    description = u"Results Table \u2014 table showing this constructor's results at every race this season"
    results_table = PlotItem(generate_results_table, [yc_results, yc_fastest_lap_data, year_results,
                                                      year_fastest_lap_data], description)

    teammate_comparison_line_plot, comparison_source = generate_teammate_comparison_line_plot(yc_results, year_races,
                                                                                              yc_driver_standings,
                                                                                              year_id)
    teammate_comparison_line_plot = PlotItem(teammate_comparison_line_plot, [],
                                             COMMON_PLOT_DESCRIPTIONS["generate_teammate_comparison_line_plot"])

    description = u"Various statistics on this constructor during this year"
    stats_layout = PlotItem(generate_stats_layout, [positions_source, yc_results, comparison_source, year_id,
                                                    constructor_id], description)

    constructor_name = get_constructor_name(constructor_id)
    header = generate_div_item(f"<h2><b>What did {constructor_name}'s {year_id} season look like?</b></h2>")

    logging.info("Finished generating layout for mode YEARCONSTRUCTOR")

    middle_spacer = generate_spacer_item()
    group = generate_plot_list_selector([
        [header],
        [wcc_plot], [middle_spacer],
        [positions_plot], [middle_spacer],
        [win_plot], [middle_spacer],
        [finishing_position_bar_plot], [middle_spacer],
        [spvfp_scatter, mltr_fp_scatter], [middle_spacer],
        [teammate_comparison_line_plot], [middle_spacer],
        [driver_performance_layout],
        [stats_layout],
        [results_table]
    ])

    return group
Esempio n. 10
0
def get_layout(**kwargs):
    logging.info(f"Generating layout for mode ALLCIRCUITS in allcircuits")

    description = u"Number of Races Bar Chart \u2014 bar chart showing number of races held at each circuit"
    num_races_bar_chart = PlotItem(generate_num_races_bar, [], description)

    description = u"DNF Percent Bar Chart \u2014 bar chart showing the average DNF percent at each circuit"
    dnf_bar_chart = PlotItem(generate_dnf_bar, [], description)

    description = u"Number of Overtakes Bar Chart \u2014 bar chart showing the average number of overtakes at " \
                  u"each circuit"
    overtakes_bar_chart = PlotItem(generate_num_overtakes_bar, [], description)

    description = u"Average Start Pos. - Finish Pos. Bar Chart \u2014 bar chart showing the average start position " \
                  u"minus finish position, giving an indication of how many places drivers make up on average at " \
                  u"each circuit"
    mspmfp_bar_chart = PlotItem(generate_mspmfp_bar, [], description)

    description = u"Countries Bar Chart \u2014 bar chart showing how many circuits are in each country"
    countries_bar_chart = PlotItem(generate_countries_bar, [], description)

    description = u"Ratings Bar Chart \u2014 bar chart showing the average rating of races at many circuits, " \
                  u"according to racefans.net"
    rating_bar_chart = PlotItem(generate_rating_bar, [], description)

    description = u"Average Lap Time Bar Chart \u2014 bar chart showing the average lap time of every circuit across " \
                  u"all of its years"
    avg_lap_time_bar_chart = PlotItem(generate_avg_lap_time_bar, [],
                                      description)

    description = u"Pit Stop Bar Chart \u2014 bar chart showing how many pit stops per race a circuit has on average"
    pit_stop_bar_chart = PlotItem(generate_pit_stop_bar, [], description)

    description = u"Safety Car Bar Chart \u2014 bar chart showing how many safety car laps per race a circuit has " \
                  u"on average"
    sc_laps_bar_chart = PlotItem(generate_sc_laps_bar, [], description)

    description = u"Weather Bar Chart \u2014 bar chart showing how often there are dry, varied, and wet races at " \
                  u"each circuit"
    weather_bar_chart = PlotItem(generate_weather_bar, [], description)

    description = u"""Upset Scatter Plot \u2014 complex scatter plot showing with a dot for each circuit showing which 
    circuits are prone to "upsets" """
    upset_scatter = PlotItem(generate_upset_scatter, [], description)

    description = u"Start Position vs Finish Position Scatter Plot \u2014 each dot on this plot represents a circuit " \
                  u"and can show circuits where drivers on average make up many positions in a race"
    spvfp_scatter = PlotItem(generate_spvfp_scatter, [], description)

    description = u"Circuits Table \u2014 table of all circuits, showing information such as how many times there was" \
                  u" a race held at each circuit and who won the most at each circuit"
    circuits_table = PlotItem(generate_circuits_table, [], description)

    header = generate_div_item(
        text=
        u"<h2><b>All Circuits \u2014 Some Stats on All Circuits that have held F1 Races"
    )

    middle_spacer = generate_spacer_item()
    layout = generate_plot_list_selector([[header], [num_races_bar_chart],
                                          [middle_spacer], [dnf_bar_chart],
                                          [middle_spacer
                                           ], [overtakes_bar_chart],
                                          [middle_spacer], [mspmfp_bar_chart],
                                          [middle_spacer], [rating_bar_chart],
                                          [middle_spacer],
                                          [avg_lap_time_bar_chart],
                                          [middle_spacer
                                           ], [pit_stop_bar_chart],
                                          [middle_spacer], [sc_laps_bar_chart],
                                          [middle_spacer], [weather_bar_chart],
                                          [middle_spacer],
                                          [upset_scatter, spvfp_scatter],
                                          [countries_bar_chart],
                                          [middle_spacer], [circuits_table]])

    logging.info("Finished generating layout for mode ALLCIRCUITS")

    return layout