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
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
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
    )