Fancy-yousa's picture
Rename Webapp/app.py to Webapp/app111.py
46fd693 verified
import os
from dash import Dash
import dash_mantine_components as dmc
from dash import html, dcc
# =====================================================
# 全局变量(只声明,不初始化!)
# =====================================================
DATASET_METADATA = None
DISPLAY_COMPLEXITY = None
dataset_options = None
complexity_data = None
INITIALIZED = False
RESULT_DIR = "/code/results"
# =====================================================
# 原有函数(保持你的实现)
# =====================================================
def build_dataset_metadata():
"""
你的原始实现
"""
metadata = {}
for name in list_available_datasets():
last_updated = datetime.datetime.fromtimestamp(1707382400).strftime("%Y-%m-%d")
num_samples = None
total_features = None
if scipy:
mat_path = os.path.join(DATA_DIR, f"{name}.mat")
if os.path.exists(mat_path):
try:
mat = scipy.io.loadmat(mat_path)
if "X" in mat:
X = mat["X"]
num_samples, total_features = X.shape
except Exception:
num_samples = None
total_features = None
metadata[name] = {
"name": name,
"last_updated": last_updated,
"num_samples": num_samples,
"total_features": total_features,
}
print(f"Loaded datasets from {RESULT_DIR}")
return metadata
def build_complexity_display():
"""
你的原始实现
"""
display_complexity = {}
for algo, comp in ALGO_COMPLEXITY.items():
t = comp.get("time", "")
s = comp.get("space", "")
t_disp = t.replace("**", "^").replace(" * ", "")
if "O(" not in t_disp:
t_disp = f"O({t_disp})" if t_disp else ""
s_disp = s.replace("**", "^").replace(" * ", "")
if "O(" not in s_disp:
s_disp = f"O({s_disp})" if s_disp else ""
display_complexity[algo] = {"time": t_disp, "space": s_disp}
return display_complexity
# =====================================================
# ⭐ 关键:只初始化一次(Lazy Init)
# =====================================================
def initialize_once():
global INITIALIZED
global DATASET_METADATA
global DISPLAY_COMPLEXITY
global dataset_options
global complexity_data
if INITIALIZED:
return
print("Initializing system ...")
DATASET_METADATA = build_dataset_metadata()
DISPLAY_COMPLEXITY = build_complexity_display()
dataset_options = [
{"label": name, "value": name}
for name in sorted(DATASET_METADATA.keys())
]
complexity_options = sorted({
v.get("time")
for v in DISPLAY_COMPLEXITY.values()
if v.get("time")
})
complexity_data = (
[{"label": "All Complexities", "value": "all"}]
+ [{"label": c, "value": c} for c in complexity_options]
)
INITIALIZED = True
print("Initialization finished.")
# =====================================================
# Dash App 创建(允许被 import)
# =====================================================
app = Dash(__name__)
server = app.server
# 你的 layout 定义(保持原样)
app.layout =dmc.MantineProvider(
children=html.Div(
className="app-shell",
children=[
html.Aside(
className="sidebar",
children=[
html.Div(
[
html.H1("AutoFS", style={"fontSize": "1.5em", "margin": 0, "color": "white"}),
html.Div("Feature Selection Leaderboard", style={"fontSize": "0.8em", "color": "#bdc3c7"}),
],
style={"textAlign": "center", "marginBottom": "10px"},
),
html.Div(
className="stats-grid",
children=[
html.Div([html.Div("-", id="stat-count", className="stat-value"), html.Div("Methods", className="stat-label")], className="stat-card"),
html.Div([html.Div("-", id="stat-best", className="stat-value"), html.Div("Best F1", className="stat-label")], className="stat-card"),
html.Div([html.Div("-", id="stat-updated", className="stat-value"), html.Div("Updated", className="stat-label")], className="stat-card"),
],
),
html.Div(
[
html.H2("Navigation"),
html.Ul(
className="nav-links",
children=[
html.Li(html.A("📊 Overview", href="#overview")),
html.Li(html.A("🏆 Leaderboard", href="#main-table")),
html.Li(html.A("📈 Charts", href="#charts")),
html.Li(html.A("ℹ️ Details", href="#details")),
html.Li(html.A("🌍 Global Rankings", href="/global")),
html.Li(html.A("📤 Submit Data/Method", href="/submit")),
],
),
]
),
html.Div(
[
html.H2("Global Controls"),
dmc.Select(
id="view-mode",
data=[
{"label": "Overall (Mean)", "value": "overall"},
{"label": "F1 by Classifier", "value": "classifiers-f1"},
{"label": "AUC by Classifier", "value": "classifiers-auc"},
],
value="overall",
clearable=False,
style={"marginBottom": "10px"},
),
]
),
html.Div(
[
html.H2("Filters"),
dmc.Select(
id="dataset-select",
data=dataset_options,
value="Authorship",
clearable=False,
style={"marginBottom": "10px"},
),
html.Div(
[
html.Div(
[
html.Span("Min F1 Score: "),
html.Span("0.0000", id="val-f1", style={"color": "var(--accent-color)"}),
],
style={"marginBottom": "6px", "color": "#bdc3c7"},
),
dmc.Slider(id="filter-f1", min=0, max=1, step=0.0001, value=0),
],
style={"marginBottom": "12px"},
),
html.Div(
[
html.Div(
[
html.Span("Del. Rate: "),
html.Span("0% - 100%", id="val-del-rate", style={"color": "var(--accent-color)"}),
],
style={"marginBottom": "6px", "color": "#bdc3c7"},
),
dmc.RangeSlider(id="filter-del-rate", min=0, max=100, value=[0, 100], step=1),
],
style={"marginBottom": "12px"},
),
html.Div(
[
html.Div(
[
html.Span("Max Features: "),
html.Span("All", id="val-feats", style={"color": "var(--accent-color)"}),
],
style={"marginBottom": "6px", "color": "#bdc3c7"},
),
dmc.Slider(id="filter-feats", min=1, max=100, step=1, value=100),
],
style={"marginBottom": "12px"},
),
dmc.Select(
id="filter-complexity",
data=complexity_data,
value="all",
clearable=False,
style={"marginBottom": "12px"},
),
dmc.CheckboxGroup(id="filter-algos", children=[], value=[], orientation="vertical"),
]
),
],
),
html.Main(
className="main-content",
children=[
html.Header(
[
html.H1("🏆 Leaderboard Dashboard", style={"color": "var(--secondary-color)", "margin": 0}),
html.Div("Comprehensive benchmark of feature selection algorithms across diverse datasets.", className="subtitle"),
]
),
html.Div(
className="card",
children=[
html.P([
"Feature selection is a critical step in machine learning and data analysis, aimed at ",
html.Strong("identifying the most relevant subset of features"),
" from a high-dimensional dataset. By eliminating irrelevant or redundant features, feature selection not only ",
html.Strong("improves model interpretability"),
" but also ",
html.Strong("enhances predictive performance"),
" and ",
html.Strong("reduces computational cost"),
".",
]),
html.P([
"This leaderboard presents a comprehensive comparison of various feature selection algorithms across multiple benchmark datasets. It includes several ",
html.Strong("information-theoretic and mutual information-based methods"),
", which quantify the statistical dependency between features and the target variable to rank feature relevance. Mutual information approaches are particularly effective in ",
html.Strong("capturing both linear and non-linear relationships"),
", making them suitable for complex datasets where classical correlation-based methods may fail.",
]),
html.P([
"The leaderboard is structured to reflect algorithm performance across different datasets, allowing for an objective assessment of each method’s ability to select informative features. For each method and dataset combination, metrics such as ",
html.Strong("classification accuracy, F1-score, and area under the ROC curve (AUC)"),
" are reported, providing insights into how the selected features contribute to predictive modeling.",
]),
html.P([
"By examining this feature selection leaderboard, researchers and practitioners can gain a better understanding of which methods perform consistently well across diverse domains, helping to guide the choice of feature selection strategies in real-world applications. This serves as a valuable resource for both benchmarking and method development in the field of feature selection.",
]),
],
style={"marginTop": "16px"},
),
dmc.Grid(
id="overview",
gutter="md",
style={"marginTop": "16px"},
children=[
dmc.Col(
span=12,
md=6,
children=html.Div(
className="card",
children=[
html.H3("About This Dataset"),
html.P(["Analyzing performance on ", html.Strong(html.Span("Selected", id="desc-dataset-name")), "."]),
],
),
),
dmc.Col(
span=12,
md=6,
children=html.Div(
className="card",
children=[
html.H3("Dataset Metadata"),
html.Div(["Name: ", html.Span("-", id="meta-name")]),
html.Div(["Samples: ", html.Span("-", id="meta-samples"), " | Features: ", html.Span("-", id="meta-features")]),
html.Div(["Last Updated: ", html.Span("-", id="meta-updated")]),
],
),
),
],
),
html.Div(
id="main-table",
style={"marginTop": "24px"},
children=[
html.H3("📋 Detailed Rankings"),
html.Div(id="custom-table-container", className="custom-table-wrapper"),
],
),
html.Div(
id="charts",
style={"marginTop": "24px"},
children=[
dmc.Grid(
gutter="md",
children=[
dmc.Col(
span=12,
md=6,
children=html.Div(
className="chart-card",
children=[
html.H3("📊 Performance Comparison"),
dcc.Graph(id="score-graph", config={"responsive": True}, style={"height": "100%"}),
],
),
),
dmc.Col(
span=12,
md=6,
children=html.Div(
className="chart-card",
children=[
html.H3("📉 Pareto Frontier (Trade-off)"),
html.Div("X: Selected Features vs Y: F1 Score (Top-Left is better)", style={"fontSize": "0.9em", "color": "#666"}),
dcc.Graph(id="pareto-graph", config={"responsive": True}, style={"height": "100%"}),
],
),
),
],
)
],
),
html.Div(
id="details",
style={"marginTop": "50px", "color": "#999", "textAlign": "center", "borderTop": "1px solid #eee", "paddingTop": "20px"},
children="AutoFS Benchmark Platform © 2026",
),
],
),
],
)
)
# =====================================================
# ⭐ Callback 中确保初始化
# =====================================================
@app.callback(
Output("filter-feats", "max"),
Output("filter-feats", "value"),
Output("filter-f1", "min"),
Output("filter-f1", "max"),
Output("filter-f1", "value"),
Output("filter-algos", "children"),
Output("filter-algos", "value"),
Output("meta-name", "children"),
Output("meta-samples", "children"),
Output("meta-features", "children"),
Output("meta-updated", "children"),
Output("desc-dataset-name", "children"),
Output("stat-updated", "children"),
Output("stat-count", "children"),
Output("stat-best", "children"),
Output("score-graph", "figure"),
Output("pareto-graph", "figure"),
Output("custom-table-container", "children"),
Output("val-f1", "children"),
Output("val-feats", "children"),
Output("val-del-rate", "children"),
Input("dataset-select", "value"),
Input("view-mode", "value"),
Input("filter-f1", "value"),
Input("filter-feats", "value"),
Input("filter-del-rate", "value"),
Input("filter-complexity", "value"),
Input("filter-algos", "value"),
State("filter-f1", "min"),
State("filter-f1", "max"),
State("filter-feats", "max"),
State("filter-algos", "children"),
State("filter-algos", "value"),
)
def update_dashboard_all(
dataset,
view_mode,
min_f1_value,
max_features_value,
del_range,
complexity,
selected_algos,
f1_min_state,
f1_max_state,
feats_max_state,
algo_children_state,
algo_value_state,
):
initialize_once()
triggered_id = callback_context.triggered_id if callback_context.triggered else None
dataset_changed = triggered_id == "dataset-select" or triggered_id is None
selected = dataset or "Authorship"
meta = DATASET_METADATA.get(selected, {"name": selected, "last_updated": "-", "num_samples": None, "total_features": None})
results = get_results_for_dataset(selected)
algo_list = sorted({r.get("algorithm") for r in results if r.get("algorithm")})
if dataset_changed:
f1_scores = [r.get("mean_f1") for r in results if r.get("mean_f1") is not None]
if f1_scores:
min_f1 = min(f1_scores)
safe_min = max(0, math.floor((min_f1 - 0.1) * 10) / 10)
else:
safe_min = 0
max_feats = meta.get("total_features") or 100
f1_min = safe_min
f1_max = 1
f1_value = safe_min
feats_max = max_feats
feats_value = max_feats
algo_children = [dmc.Checkbox(label=a, value=a) for a in algo_list]
algo_value = algo_list
else:
f1_min = f1_min_state if f1_min_state is not None else 0
f1_max = f1_max_state if f1_max_state is not None else 1
f1_value = min_f1_value if min_f1_value is not None else f1_min
feats_max = feats_max_state if feats_max_state is not None else (meta.get("total_features") or 100)
feats_value = max_features_value if max_features_value is not None else feats_max
if algo_children_state:
algo_children = algo_children_state
else:
algo_children = [dmc.Checkbox(label=a, value=a) for a in algo_list]
if selected_algos is not None:
algo_value = selected_algos
else:
algo_value = algo_value_state if algo_value_state is not None else algo_list
filtered = apply_filters(results, meta, f1_value or 0, feats_value, del_range, complexity, algo_value or [])
count = len(filtered)
if filtered:
best = max(filtered, key=lambda r: r.get("mean_f1") or 0)
best_text = f"{best.get('algorithm')} ({(best.get('mean_f1') or 0):.3f})"
else:
best_text = "-"
score_fig = build_score_figure(filtered, view_mode or "overall")
pareto_fig = build_pareto_figure(filtered)
table_component = build_table(filtered, view_mode or "overall")
val_f1 = f"{(f1_value or 0):.4f}"
val_feats = str(int(feats_value)) if isinstance(feats_value, (int, float)) else "All"
del_min = del_range[0] if del_range else 0
del_max = del_range[1] if del_range else 100
val_del = f"{del_min:.0f}% - {del_max:.0f}%"
meta_samples = meta.get("num_samples") if meta.get("num_samples") is not None else "Unavailable"
meta_features = meta.get("total_features") if meta.get("total_features") is not None else "Unavailable"
return (
feats_max,
feats_value,
f1_min,
f1_max,
f1_value,
algo_children,
algo_value,
meta.get("name", "-"),
meta_samples,
meta_features,
meta.get("last_updated", "-"),
meta.get("name", "-"),
meta.get("last_updated", "-"),
count,
best_text,
score_fig,
pareto_fig,
table_component,
val_f1,
val_feats,
val_del,
)
# =====================================================
# ⭐ 只在真正启动时运行
# =====================================================
if __name__ == "__main__":
initialize_once()
port = int(os.environ.get("PORT", 7865))
print(f"Loaded {len(DATASET_METADATA)} datasets from {RESULT_DIR}")
print(f"Dash is running on http://0.0.0.0:{port}/")
app.run(
host="0.0.0.0",
port=port,
debug=False
)