Fancy-yousa's picture
Rename Webapp/app.py to Webapp/app00.py
26d150c verified
import os
import sys
import json
import datetime
import math
try:
import scipy.io
except ImportError:
scipy = None
try:
import numpy as np
except ImportError:
np = None
try:
import pandas as pd
except ImportError:
pd = None
from flask import Flask, jsonify, request, render_template, send_from_directory
from dash import Dash, html, dcc, dash_table, Input, Output, State, callback_context
import dash_mantine_components as dmc
import plotly.graph_objects as go
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from leaderboard import rank_results
try:
from complex_com import algorithms as ALGO_COMPLEXITY
except ImportError:
ALGO_COMPLEXITY = {}
base_dir = os.getcwd()
if not os.path.isdir(os.path.join(base_dir, "results")):
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
PROJECT_ROOT = base_dir
RESULT_DIR = os.path.join(PROJECT_ROOT, "results")
DATA_DIR = os.path.join(PROJECT_ROOT, "data")
PDF_DIR = os.path.join(PROJECT_ROOT, "pdf")
os.makedirs(RESULT_DIR, exist_ok=True)
server = Flask(__name__)
RESULT_CACHE = {}
def save_result_json(dataset, results):
path = os.path.join(RESULT_DIR, f"{dataset}.json")
with open(path, "w", encoding="utf-8") as f:
json.dump(results, f, indent=4)
def load_result_json(dataset):
path = os.path.join(RESULT_DIR, f"{dataset}.json")
if not os.path.exists(path):
return None
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def list_available_datasets():
datasets = set()
for f in os.listdir(RESULT_DIR):
if f.endswith(".json"):
datasets.add(f.replace(".json", ""))
datasets.add("Authorship")
return sorted(datasets)
def run_agent_for_dataset(dataset):
return []
def build_dataset_metadata():
datasets = {}
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
datasets[name] = {
"name": name,
"last_updated": last_updated,
"num_samples": num_samples,
"total_features": total_features,
}
return datasets
DATASET_METADATA = build_dataset_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
DISPLAY_COMPLEXITY = build_complexity_display()
VIEW_CONFIG = {
"overall": [
{"key": "mean_f1", "label": "Mean F1"},
{"key": "mean_auc", "label": "Mean AUC"},
],
"classifiers-f1": [
{"key": "metrics.nb.f1", "label": "NB F1"},
{"key": "metrics.svm.f1", "label": "SVM F1"},
{"key": "metrics.rf.f1", "label": "RF F1"},
],
"classifiers-auc": [
{"key": "metrics.nb.auc", "label": "NB AUC"},
{"key": "metrics.svm.auc", "label": "SVM AUC"},
{"key": "metrics.rf.auc", "label": "RF AUC"},
],
}
def get_results_for_dataset(dataset):
if dataset in RESULT_CACHE:
leaderboard = rank_results(RESULT_CACHE[dataset])
else:
results = load_result_json(dataset)
if results is None:
results = run_agent_for_dataset(dataset)
if results:
save_result_json(dataset, results)
RESULT_CACHE[dataset] = results or []
leaderboard = rank_results(results or [])
if not isinstance(leaderboard, list):
if hasattr(leaderboard, "to_dict"):
leaderboard = leaderboard.to_dict(orient="records")
else:
leaderboard = list(leaderboard)
return leaderboard
def get_metric_value(row, key):
value = row
for part in key.split("."):
if isinstance(value, dict):
value = value.get(part)
else:
return None
return value
def get_feature_count(row):
num_features = row.get("num_features")
if isinstance(num_features, (int, float)):
return int(num_features)
selected = row.get("selected_features")
if isinstance(selected, list):
return len(selected)
return 0
def apply_filters(results, dataset_meta, min_f1, max_features, del_range, complexity, selected_algos):
total_features = dataset_meta.get("total_features") if dataset_meta else None
filtered = []
min_del = (del_range[0] if del_range else 0) / 100
max_del = (del_range[1] if del_range else 100) / 100
min_f1 = min_f1 if min_f1 is not None else 0
max_features = max_features if max_features is not None else float("inf")
selected_algos = selected_algos if selected_algos else None
for r in results:
algo = r.get("algorithm")
if selected_algos and algo not in selected_algos:
continue
raw_f1 = r.get("mean_f1")
try:
f1 = float(raw_f1) if raw_f1 is not None else 0
except (TypeError, ValueError):
f1 = 0
if f1 < min_f1:
continue
feats = get_feature_count(r)
if feats > max_features:
continue
if isinstance(total_features, (int, float)) and total_features > 0:
del_rate = 1 - (feats / total_features)
if del_rate < min_del or del_rate > max_del:
continue
if complexity and complexity != "all":
comp = DISPLAY_COMPLEXITY.get(algo, {}).get("time")
if comp != complexity:
continue
filtered.append(r)
return filtered
def build_score_figure(results, view_mode):
if not results:
fig = go.Figure()
fig.add_annotation(
text="No Data Available",
x=0.5,
y=0.5,
xref="paper",
yref="paper",
showarrow=False,
font=dict(size=16, color="#999"),
)
fig.update_layout(
xaxis=dict(visible=False),
yaxis=dict(visible=False),
margin=dict(l=20, r=20, t=20, b=20),
)
return fig
top = results[:20]
labels = [r.get("algorithm") for r in top]
fig = go.Figure()
if view_mode == "overall":
fig.add_trace(go.Bar(
name="Mean F1",
y=labels,
x=[r.get("mean_f1") for r in top],
orientation="h",
marker_color="rgba(52, 152, 219, 0.7)",
))
fig.add_trace(go.Bar(
name="Mean AUC",
y=labels,
x=[r.get("mean_auc") for r in top],
orientation="h",
marker_color="rgba(46, 204, 113, 0.7)",
))
elif view_mode == "classifiers-f1":
for idx, clf in enumerate(["nb", "svm", "rf"]):
fig.add_trace(go.Bar(
name=clf.upper(),
y=labels,
x=[get_metric_value(r, f"metrics.{clf}.f1") for r in top],
orientation="h",
marker_color=f"hsla({200 + idx * 40}, 70%, 60%, 0.7)",
))
else:
for idx, clf in enumerate(["nb", "svm", "rf"]):
fig.add_trace(go.Bar(
name=clf.upper(),
y=labels,
x=[get_metric_value(r, f"metrics.{clf}.auc") for r in top],
orientation="h",
marker_color=f"hsla({100 + idx * 40}, 70%, 60%, 0.7)",
))
fig.update_layout(
barmode="group",
margin=dict(l=20, r=20, t=20, b=20),
legend=dict(orientation="h"),
yaxis=dict(autorange="reversed"),
)
return fig
def build_pareto_figure(results):
x_vals = [get_feature_count(r) for r in results]
y_vals = [r.get("mean_f1") for r in results]
labels = [r.get("algorithm") for r in results]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x_vals,
y=y_vals,
mode="markers",
text=labels,
marker=dict(color="rgba(230, 126, 34, 0.7)", size=10),
))
fig.update_layout(
margin=dict(l=20, r=20, t=20, b=20),
xaxis_title="Num Features",
yaxis_title="Mean F1",
)
return fig
def build_table(results, view_mode):
config = VIEW_CONFIG[view_mode]
headers = ["Rank", "Algorithm"] + [c["label"] for c in config] + ["Selected Features"]
col_keys = [c["key"] for c in config]
max_map = {}
for key in col_keys:
vals = []
for r in results:
v = get_metric_value(r, key)
try:
v = float(v) if v is not None else None
except Exception:
v = None
if v is not None:
vals.append(v)
max_map[key] = max(vals) if vals else 0
thead = html.Thead(
html.Tr([html.Th(h, className="cth") for h in headers], className="chead")
)
rows = []
if not results:
empty_cells = [html.Td("", className="ctd")] * (len(headers) - 2)
rows.append(
html.Tr(
[html.Td("", className="ctd"), html.Td("No Data Available", className="ctd")]+empty_cells,
className="crow"
)
)
else:
for idx, r in enumerate(results):
rank = idx + 1
medal = {1: "🥇", 2: "🥈", 3: "🥉"}.get(rank, str(rank))
row_class = (
"crow crow-gold" if rank == 1 else
"crow crow-silver" if rank == 2 else
"crow crow-bronze" if rank == 3 else
"crow"
)
algo = r.get("algorithm") or "Unknown"
metric_tds = []
for c in config:
key = c["key"]
raw = get_metric_value(r, key)
try:
val = float(raw) if raw is not None else 0.0
except Exception:
val = 0.0
m = max_map.get(key) or 0.0
pct = (val / m * 100.0) if m > 0 else 0
is_max = (m > 0 and abs(val - m) < 1e-12)
bar = html.Div(
[
html.Div(className="bar-track", children=html.Div(className="bar-fill", style={"width": f"{pct:.2f}%"})),
html.Span(f"{val:.4f}", className=("bar-text is-max" if is_max else "bar-text")),
],
className="bar-cell",
title=f"max={m:.4f}" if is_max else None,
)
cell = html.Td(bar, className="ctd cnum")
metric_tds.append(cell)
selected = r.get("selected_features")
feat_count = get_feature_count(r)
if isinstance(selected, list):
features_title = ", ".join(str(s) for s in selected)
else:
features_title = "N/A"
feature_td = html.Td(
f"{feat_count} features",
className="ctd cfeat",
title=features_title,
style={"whiteSpace": "nowrap"},
)
row = html.Tr(
[html.Td(medal, className="ctd crank"), html.Td(algo, className="ctd calgo")] + metric_tds + [feature_td],
className=row_class,
)
rows.append(row)
tbody = html.Tbody(rows)
table = html.Table([thead, tbody], className="custom-table")
return html.Div(className="table-container", children=table)
dataset_options = [{"label": name, "value": name} for name in sorted(DATASET_METADATA.keys())]
default_dataset = "Authorship" if "Authorship" in DATASET_METADATA else (dataset_options[0]["value"] if dataset_options else "Authorship")
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
]
dash_app = Dash(__name__, server=server, url_base_pathname="/")
app = dash_app
css = """
:root {
--primary-color: #3498db;
--secondary-color: #2c3e50;
--background-color: #f8f9fa;
--text-color: #333;
--border-color: #dee2e6;
--hover-color: #f1f1f1;
--accent-color: #e67e22;
--sidebar-width: 280px;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background-color: var(--background-color);
color: var(--text-color);
}
.app-shell {
display: flex;
min-height: 100vh;
}
.sidebar {
width: var(--sidebar-width);
background-color: var(--secondary-color);
color: white;
position: fixed;
height: 100vh;
overflow-y: auto;
padding: 20px;
box-sizing: border-box;
left: 0;
top: 0;
z-index: 100;
display: flex;
flex-direction: column;
gap: 20px;
}
.sidebar h2 {
font-size: 1.1em;
margin-bottom: 10px;
color: #ecf0f1;
border-bottom: 1px solid #34495e;
padding-bottom: 5px;
}
.main-content {
margin-left: var(--sidebar-width);
padding: 24px;
width: calc(100% - var(--sidebar-width));
box-sizing: border-box;
}
.stats-grid {
display: grid;
grid-template-columns: 1fr;
gap: 10px;
}
.stat-card {
background: rgba(255,255,255,0.1);
padding: 10px;
border-radius: 6px;
text-align: center;
}
.stat-value {
font-size: 1.2em;
font-weight: 600;
color: var(--accent-color);
}
.stat-label {
font-size: 0.8em;
color: #bdc3c7;
}
.card {
background: white;
padding: 16px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.chart-card {
background: white;
padding: 16px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
height: 420px;
display: flex;
flex-direction: column;
}
.chart-card .dash-graph {
flex: 1;
}
.table-container {
background: white;
padding: 12px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.nav-links {
list-style: none;
padding: 0;
margin: 0;
}
.nav-links li a {
display: block;
padding: 8px;
color: #bdc3c7;
text-decoration: none;
border-radius: 4px;
}
.nav-links li a:hover {
background: rgba(255,255,255,0.1);
color: white;
}
/* Custom Table Styles */
.custom-table-wrapper {
overflow-x: auto;
overflow-y: auto;
max-height: 520px;
background: #fff;
border: 1px solid #eee;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.custom-table {
width: 100%;
border-collapse: separate;
border-spacing: 0;
table-layout: fixed;
}
.custom-table thead th {
text-align: left;
border-bottom: 2px solid var(--border-color);
padding: 10px;
}
.custom-table tbody td {
padding: 8px 10px;
border-bottom: 1px solid #eee;
vertical-align: middle;
}
.custom-table tbody tr:hover {
background: #fafafa;
transition: background 0.2s ease;
}
.custom-table tbody tr:nth-child(even) { background: #fcfcfc; }
.custom-table thead th {
position: sticky;
top: 0;
background: #fff;
z-index: 1;
}
.cnum { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; text-align: right; }
.is-max { font-weight: 600; color: var(--accent-color); }
.crow-gold { background: rgba(255,215,0,0.12); }
.crow-silver { background: rgba(192,192,192,0.12); }
.crow-bronze { background: rgba(205,127,50,0.12); }
.crank { width: 56px; text-align: center; }
.calgo { font-weight: 500; }
.cth { background: var(--background-color); }
.cfeat { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* In-cell Data Bars */
.bar-cell {
position: relative;
height: 26px;
display: flex;
align-items: center;
justify-content: flex-end;
}
.bar-track {
position: absolute;
left: 6px;
right: 6px;
height: 60%;
background: #f4f7fb;
border: 1px solid #e6eef7;
border-radius: 6px;
overflow: hidden;
}
.bar-fill {
height: 100%;
background: linear-gradient(90deg, #7db9e8 0%, #3498db 100%);
opacity: 0.35;
transition: width 200ms ease;
}
.bar-text {
position: relative;
z-index: 1;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
padding: 0 6px;
line-height: 1;
}
"""
dash_app.index_string = f"""
<!DOCTYPE html>
<html>
<head>
{{%metas%}}
<title>AutoFS Leaderboard</title>
{{%favicon%}}
{{%css%}}
<style>{css}</style>
</head>
<body>
{{%app_entry%}}
<footer>
{{%config%}}
{{%scripts%}}
{{%renderer%}}
</footer>
</body>
</html>
"""
dash_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",
),
],
),
],
)
)
@dash_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,
):
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,
)
def sanitize_json(value):
if value is None or isinstance(value, (str, int, float, bool)):
return value
if np and isinstance(value, np.generic):
return value.item()
if np and isinstance(value, np.ndarray):
return value.tolist()
if pd and isinstance(value, (pd.DataFrame, pd.Series)):
if isinstance(value, pd.DataFrame):
return value.to_dict(orient="records")
return value.to_dict()
if isinstance(value, (datetime.datetime, datetime.date)):
return value.isoformat()
if isinstance(value, dict):
return {str(k): sanitize_json(v) for k, v in value.items()}
if isinstance(value, (list, tuple, set)):
return [sanitize_json(v) for v in value]
if hasattr(value, "to_dict"):
return sanitize_json(value.to_dict())
return str(value)
@server.route("/global")
def global_view():
return render_template("global.html")
@server.route("/submit")
def submit_view():
return render_template("submit.html")
@server.route("/api/results")
def get_results_api():
try:
dataset = request.args.get("dataset") or "Authorship"
leaderboard = get_results_for_dataset(dataset)
return jsonify(sanitize_json(leaderboard))
except Exception as e:
print(e)
return jsonify({"error": str(e)})
@server.route("/api/datasets")
def api_datasets():
try:
datasets = []
for name, meta in DATASET_METADATA.items():
datasets.append({
"name": name,
"last_updated": meta.get("last_updated"),
"num_samples": meta.get("num_samples") if meta.get("num_samples") is not None else "Unavailable",
"total_features": meta.get("total_features") if meta.get("total_features") is not None else "Unavailable",
})
return jsonify(sanitize_json(datasets))
except Exception as e:
print(e)
return jsonify({"error": str(e)})
@server.route("/api/global_stats")
def api_global_stats():
try:
algo_totals = {}
algo_counts = {}
for dataset in DATASET_METADATA.keys():
results = get_results_for_dataset(dataset) or []
for row in results:
algo = row.get("algorithm") or "Unknown"
mean_f1 = row.get("mean_f1")
mean_auc = row.get("mean_auc")
if mean_f1 is None and mean_auc is None:
continue
totals = algo_totals.get(algo, {"f1": 0.0, "auc": 0.0})
counts = algo_counts.get(algo, {"f1": 0, "auc": 0})
if mean_f1 is not None:
totals["f1"] += float(mean_f1)
counts["f1"] += 1
if mean_auc is not None:
totals["auc"] += float(mean_auc)
counts["auc"] += 1
algo_totals[algo] = totals
algo_counts[algo] = counts
global_stats = []
for algo, totals in algo_totals.items():
counts = algo_counts.get(algo, {"f1": 0, "auc": 0})
mean_f1_global = totals["f1"] / counts["f1"] if counts["f1"] else None
mean_auc_global = totals["auc"] / counts["auc"] if counts["auc"] else None
global_stats.append({
"algorithm": algo,
"mean_f1_global": mean_f1_global,
"mean_auc_global": mean_auc_global,
})
return jsonify(sanitize_json(global_stats))
except Exception as e:
print(e)
return jsonify({"error": str(e)})
@server.route("/api/algos")
def api_algorithms():
return jsonify(DISPLAY_COMPLEXITY)
@server.route("/pdfs/<path:filename>")
def serve_pdf(filename):
return send_from_directory(PDF_DIR, filename)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 7860))
print(f"Loaded {len(DATASET_METADATA)} datasets from {RESULT_DIR}")
app.run(host="0.0.0.0", port=port, debug=False)