|
| 1 | +from vectordb_bench.frontend.components.check_results.expanderStyle import ( |
| 2 | + initMainExpanderStyle, |
| 3 | +) |
| 4 | +from vectordb_bench.metric import metric_order, isLowerIsBetterMetric, metric_unit_map |
| 5 | +from vectordb_bench.frontend.config.styles import * |
| 6 | +import plotly.express as px |
| 7 | +import pandas as pd |
| 8 | +import plotly.graph_objects as go |
| 9 | +import matplotlib.pyplot as plt |
| 10 | + |
| 11 | + |
| 12 | +def drawCharts(st, allData, caseNames: list[str]): |
| 13 | + initMainExpanderStyle(st) |
| 14 | + for caseName in caseNames: |
| 15 | + chartContainer = st.expander(caseName, True) |
| 16 | + data = [data for data in allData if data["case_name"] == caseName] |
| 17 | + drawChart(data, chartContainer, key_prefix=caseName) |
| 18 | + |
| 19 | + |
| 20 | +def drawChart(data, st, key_prefix: str): |
| 21 | + metricsSet = set() |
| 22 | + for d in data: |
| 23 | + metricsSet = metricsSet.union(d["metricsSet"]) |
| 24 | + showlineMetrics = [metric for metric in metric_order[:2] if metric in metricsSet] |
| 25 | + |
| 26 | + if showlineMetrics: |
| 27 | + metric = showlineMetrics[0] |
| 28 | + key = f"{key_prefix}-{metric}" |
| 29 | + drawlinechart(st, data, metric, key=key) |
| 30 | + |
| 31 | + |
| 32 | +def drawBestperformance(data, y, group): |
| 33 | + all_filter_points = [] |
| 34 | + data = pd.DataFrame(data) |
| 35 | + grouped = data.groupby(group) |
| 36 | + for name, group_df in grouped: |
| 37 | + filter_points = [] |
| 38 | + current_start = 0 |
| 39 | + for _ in range(len(group_df)): |
| 40 | + if current_start >= len(group_df): |
| 41 | + break |
| 42 | + max_index = group_df[y].iloc[current_start:].idxmax() |
| 43 | + filter_points.append(group_df.loc[max_index]) |
| 44 | + |
| 45 | + current_start = group_df.index.get_loc(max_index) + 1 |
| 46 | + all_filter_points.extend(filter_points) |
| 47 | + |
| 48 | + all_filter_df = pd.DataFrame(all_filter_points) |
| 49 | + remaining_df = data[~data.isin(all_filter_df).any(axis=1)] |
| 50 | + new_data = all_filter_df.to_dict(orient="records") |
| 51 | + remain_data = remaining_df.to_dict(orient="records") |
| 52 | + return new_data, remain_data |
| 53 | + |
| 54 | + |
| 55 | +def drawlinechart(st, data: list[object], metric, key: str): |
| 56 | + unit = metric_unit_map.get(metric, "") |
| 57 | + minV = min([d.get(metric, 0) for d in data]) |
| 58 | + maxV = max([d.get(metric, 0) for d in data]) |
| 59 | + padding = maxV - minV |
| 60 | + rangeV = [ |
| 61 | + minV - padding * 0.1, |
| 62 | + maxV + padding * 0.1, |
| 63 | + ] |
| 64 | + x = "recall" |
| 65 | + xrange = [0.8, 1.01] |
| 66 | + y = "qps" |
| 67 | + yrange = rangeV |
| 68 | + data.sort(key=lambda a: a[x]) |
| 69 | + group = "db_name" |
| 70 | + new_data, new_remain_data = drawBestperformance(data, y, group) |
| 71 | + unique_db_names = list(set(item["db_name"] for item in new_data + new_remain_data)) |
| 72 | + |
| 73 | + colors = plt.cm.get_cmap("tab10", len(unique_db_names)) |
| 74 | + |
| 75 | + color_map = { |
| 76 | + db: f"rgb({int(colors(i)[0] * 255)}, {int(colors(i)[1] * 255)}, {int(colors(i)[2] * 255)})" |
| 77 | + for i, db in enumerate(unique_db_names) |
| 78 | + } |
| 79 | + |
| 80 | + fig = go.Figure() |
| 81 | + |
| 82 | + new_data_df = pd.DataFrame(new_data) |
| 83 | + |
| 84 | + for db in unique_db_names: |
| 85 | + db_data = new_data_df[new_data_df["db_name"] == db] |
| 86 | + fig.add_trace( |
| 87 | + go.Scatter( |
| 88 | + x=db_data["recall"], |
| 89 | + y=db_data["qps"], |
| 90 | + mode="lines+markers", |
| 91 | + name=db, |
| 92 | + line=dict(color=color_map[db]), |
| 93 | + marker=dict(color=color_map[db]), |
| 94 | + showlegend=True, |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + for item in new_remain_data: |
| 99 | + fig.add_trace( |
| 100 | + go.Scatter( |
| 101 | + x=[item["recall"]], |
| 102 | + y=[item["qps"]], |
| 103 | + mode="markers", |
| 104 | + name=item["db_name"], |
| 105 | + marker=dict(color=color_map[item["db_name"]]), |
| 106 | + showlegend=False, |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + fig.update_xaxes(range=xrange) |
| 111 | + fig.update_yaxes(range=yrange) |
| 112 | + fig.update_traces(textposition="bottom right", texttemplate="%{y:,.4~r}" + unit) |
| 113 | + fig.update_layout( |
| 114 | + margin=dict(l=0, r=0, t=40, b=0, pad=8), |
| 115 | + legend=dict(orientation="h", yanchor="bottom", y=1, xanchor="right", x=1, title=""), |
| 116 | + ) |
| 117 | + st.plotly_chart(fig, use_container_width=True, key=key) |
0 commit comments