-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
2084 lines (1734 loc) · 79.3 KB
/
Copy pathfeatures.py
File metadata and controls
2084 lines (1734 loc) · 79.3 KB
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Pure pandas feature-engineering helpers for the War Thunder Stats app.
This module intentionally has **no Streamlit imports**. It is the single source
of truth for:
* cleaning / typing the raw ThunderSkill CSV -> clean_daily()
* safe metadata fallbacks (country / type / rank) -> apply_metadata_fallbacks()
* manual War Thunder Wiki BR overrides -> apply_wiki_br_overrides()
* manual War Thunder Wiki premium-status overrides -> apply_wiki_premium_overrides()
* the recent 30-day window -> recent_window()
* the one-row-per-vehicle aggregate -> build_vehicle_agg()
* BR-relative Combat Effectiveness Score -> add_combat_effectiveness()
* broad lineup-style BR range fields -> add_br_ranges()
* data-quality flags -> add_quality_flags()
Keeping these as plain functions means the offline clustering / precompute
scripts (future phases) can reuse the exact same logic. Streamlit-specific
caching wrappers live in streamlit_app.py.
Two canonical dataframes are produced from the raw CSV:
cleaned_daily_df : one row per vehicle per day (typed, backfilled).
vehicle_agg_df : one row per vehicle (aggregated 30-day window + features).
"""
from __future__ import annotations
import numpy as np
import pandas as pd
# ============================================================
# Config / thresholds (named so they are easy to tune later)
# ============================================================
RECENT_WINDOW_DAYS = 30
# Quality-flag thresholds (see add_quality_flags).
MIN_SUFFICIENT_DAYS = 5
MIN_SUFFICIENT_BATTLES = 20
# Sentinel strings that should be treated as missing in text columns.
_NULL_TOKENS = {"", "nan", "none", "null"}
# index_country is UPPERCASE (USSR / USA / SWEDEN / BRITAIN ...).
# country (from the page scrape) is title-case (Sweden / Britain ...).
# Map both to one canonical spelling before backfilling.
COUNTRY_CANONICAL = {
"USA": "USA",
"USSR": "USSR",
"BRITAIN": "Britain",
"GERMANY": "Germany",
"JAPAN": "Japan",
"ITALY": "Italy",
"FRANCE": "France",
"SWEDEN": "Sweden",
"CHINA": "China",
"ISRAEL": "Israel",
}
NUMERIC_COLS = [
"realistic_br",
"arcade_br",
"simulator_br",
"rank",
"battles",
"win_rate",
"efficiency",
"air_frags_per_battle",
"air_frags_per_death",
"ground_frags_per_battle",
"ground_frags_per_death",
"index_rank",
"index_battles",
"index_win_rate",
"index_efficiency",
]
BOOL_COLS = [
"is_premium",
"is_squadron",
"is_pack",
"on_marketplace",
]
TEXT_COLS = [
"vehicle_slug",
"vehicle_name",
"vehicle_url",
"pic",
"country",
"vehicle_type",
"mode",
"index_role",
"index_type",
"index_country",
]
# Core per-battle performance metrics (battle-weighted on aggregation).
CORE_PERF_COLS = [
"win_rate",
"efficiency",
"ground_frags_per_battle",
"ground_frags_per_death",
"air_frags_per_battle",
"air_frags_per_death",
]
# Stable per-vehicle metadata carried into the aggregate (first non-null value).
META_COLS = [
"vehicle_name",
"vehicle_url",
"pic",
"country",
"vehicle_type",
"rank",
"realistic_br",
"is_premium",
"is_squadron",
"is_pack",
"on_marketplace",
"release_date_raw",
"vehicle_id",
"mode",
]
# ------------------------------------------------------------
# Combat Effectiveness Score (BR-relative, empirical-Bayes)
# ------------------------------------------------------------
# The official score is computed within each EXACT realistic_br peer group.
# Metric weights (must sum with CE_CONFIDENCE_WEIGHT to 1.0). efficiency is
# deliberately excluded -- it is already a ThunderSkill-style composite.
CE_METRIC_WEIGHTS = {
"ground_frags_per_death": 0.40, # K/D
"ground_frags_per_battle": 0.40,
"win_rate": 0.15,
}
CE_CONFIDENCE_WEIGHT = 0.05 # within-BR standardized log1p(battles)
# Metrics that get a log1p transform before smoothing/standardizing (skewed).
CE_LOG1P_METRICS = {"ground_frags_per_death", "ground_frags_per_battle"}
# Empirical-Bayes shrinkage: reliability = battles / (battles + PRIOR_BATTLES).
PRIOR_BATTLES = 100
# 0-100 mapping: score = SCORE_CENTER + SCORE_SLOPE * z_total (clipped 0-100).
SCORE_CENTER = 50.0
SCORE_SLOPE = 15.0
# Robustness guards for the within-BR standardization.
MIN_SCORE_PEERS = 8 # BRs with fewer scoreable vehicles -> NaN score
ROBUST_SCALE_FLOOR = 1e-6 # avoids divide-by-near-zero when a BR is flat
CONFIDENCE_Z_CLIP = 3.0 # bound the confidence term so big samples can't run away
# Legacy global percentile formula (kept only for combat_effectiveness_legacy).
CE_LEGACY_WEIGHTS = {
"ground_frags_per_death": 0.35,
"ground_frags_per_battle": 0.35,
"win_rate": 0.20,
"efficiency": 0.10,
}
# ============================================================
# Small helpers
# ============================================================
def _clean_text_series(s: pd.Series) -> pd.Series:
"""Strip whitespace and convert sentinel strings ('nan', '', ...) to NaN.
The previous loader used ``astype(str).str.strip()`` which turned real NaN
into the literal string ``"nan"`` -- that leaked a phantom 'nan' nation into
filters. This nullifies those sentinels instead.
"""
s = s.astype(str).str.strip()
return s.mask(s.str.lower().isin(_NULL_TOKENS), np.nan)
def _canon_country(val) -> object:
"""Return a canonical, title-cased country name (or NaN)."""
if pd.isna(val):
return np.nan
s = str(val).strip()
if s.lower() in _NULL_TOKENS:
return np.nan
return COUNTRY_CANONICAL.get(s.upper(), s.title())
def _first_valid(s: pd.Series):
"""First non-null value in a group (NaN if the group is all-null)."""
s = s.dropna()
return s.iloc[0] if len(s) else np.nan
def weighted_average(values: pd.Series, weights: pd.Series) -> float:
"""Battle-weighted mean.
Unlike the original helper, this returns NaN when there is no valid weight
rather than silently falling back to an unweighted mean (which fabricated a
value for near-empty vehicles).
"""
valid = values.notna() & weights.notna() & weights.gt(0)
if valid.any():
return float(np.average(values.loc[valid], weights=weights.loc[valid]))
return float("nan")
# ============================================================
# Cleaning + metadata fallbacks -> cleaned_daily_df
# ============================================================
def apply_metadata_fallbacks(df: pd.DataFrame) -> pd.DataFrame:
"""Backfill stable metadata from the index columns when the page scrape
left them missing. Existing (non-null) values are never overwritten.
country <- normalize(index_country) [casing-normalized]
vehicle_type <- index_role [NOT index_type, which is "Ground forces"]
rank <- index_rank [rank stays source of truth]
realistic_br is intentionally NOT backfilled: the index carries no BR.
"""
out = df.copy()
if "country" in out.columns:
out["country"] = out["country"].map(_canon_country)
if "index_country" in out.columns:
idx_country = out["index_country"].map(_canon_country)
out["country"] = out["country"].fillna(idx_country)
if "vehicle_type" in out.columns and "index_role" in out.columns:
out["vehicle_type"] = out["vehicle_type"].fillna(out["index_role"])
if "rank" in out.columns and "index_rank" in out.columns:
out["rank"] = out["rank"].fillna(out["index_rank"])
return out
def clean_daily(raw_df: pd.DataFrame) -> pd.DataFrame:
"""Build cleaned_daily_df: one typed, backfilled row per vehicle per day.
Preserves the daily-row structure of the CSV (no rows dropped here).
"""
df = raw_df.copy()
if "date" in df.columns:
df["date"] = pd.to_datetime(df["date"], errors="coerce")
for col in NUMERIC_COLS:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors="coerce")
for col in BOOL_COLS:
if col in df.columns:
if df[col].dtype == "object":
df[col] = (
df[col]
.astype(str)
.str.lower()
.map({"true": True, "false": False})
.fillna(False)
)
else:
df[col] = df[col].fillna(False).astype(bool)
for col in TEXT_COLS:
if col in df.columns:
df[col] = _clean_text_series(df[col])
# Clean vehicle image URLs. Do NOT invent image URLs from vehicle_slug;
# ThunderSkill filenames are not guaranteed to match the slug.
if "pic" in df.columns:
relative_pic_mask = df["pic"].notna() & df["pic"].str.startswith("/")
df.loc[relative_pic_mask, "pic"] = (
"https://thunderskill.com" + df.loc[relative_pic_mask, "pic"]
)
df = apply_metadata_fallbacks(df)
return df
# ============================================================
# Manual War Thunder Wiki BR overrides
# ============================================================
# ThunderSkill occasionally exposes stale realistic_br / arcade_br /
# simulator_br metadata after War Thunder changes a vehicle's BR (e.g.
# ussr_kv_1s: ThunderSkill reports 4.0, the official Wiki reports 4.3). This
# lets a small, manually-maintained lookup CSV correct those columns before
# CE / clustering / any other BR-relative feature is computed.
WIKI_BR_LOOKUP_COLS = ["wiki_arcade_br", "wiki_realistic_br", "wiki_simulator_br"]
# ThunderSkill BR column -> the wiki lookup column that can override it.
_BR_OVERRIDE_MAP = {
"arcade_br": "wiki_arcade_br",
"realistic_br": "wiki_realistic_br",
"simulator_br": "wiki_simulator_br",
}
def apply_wiki_br_overrides(df: pd.DataFrame, lookup_df: pd.DataFrame | None) -> pd.DataFrame:
"""Override stale ThunderSkill BR columns with trusted War Thunder Wiki values.
Merges ``lookup_df`` onto ``df`` by vehicle_slug and prefers each wiki_*_br
column wherever it is non-null; ThunderSkill values are kept for rows the
lookup doesn't cover. Original ThunderSkill BRs are preserved as
thunderskill_arcade_br / thunderskill_realistic_br / thunderskill_simulator_br
so both are inspectable. Adds br_overridden (bool) and br_source
("war_thunder_wiki" or "thunderskill").
Safe no-op (returns ``df`` unchanged) if ``df`` has no vehicle_slug column,
or ``lookup_df`` is None/empty, has no vehicle_slug column, or has none of
the wiki_*_br columns. Supports any number of lookup rows without code
changes.
"""
if "vehicle_slug" not in df.columns:
return df
if lookup_df is None or lookup_df.empty or "vehicle_slug" not in lookup_df.columns:
return df
wiki_cols = [c for c in WIKI_BR_LOOKUP_COLS if c in lookup_df.columns]
if not wiki_cols:
return df
lookup = lookup_df[["vehicle_slug", *wiki_cols]].copy()
lookup["vehicle_slug"] = _clean_text_series(lookup["vehicle_slug"])
lookup = lookup.dropna(subset=["vehicle_slug"]).drop_duplicates(
subset="vehicle_slug", keep="last"
)
for col in wiki_cols:
lookup[col] = pd.to_numeric(lookup[col], errors="coerce")
out = df.copy()
for br_col, saved_col in [
("arcade_br", "thunderskill_arcade_br"),
("realistic_br", "thunderskill_realistic_br"),
("simulator_br", "thunderskill_simulator_br"),
]:
if br_col in out.columns:
out[saved_col] = out[br_col]
out = out.merge(lookup, on="vehicle_slug", how="left")
overridden = pd.Series(False, index=out.index)
for br_col, wiki_col in _BR_OVERRIDE_MAP.items():
if br_col not in out.columns or wiki_col not in out.columns:
continue
has_override = out[wiki_col].notna()
out[br_col] = out[wiki_col].where(has_override, out[br_col])
overridden = overridden | has_override
out["br_overridden"] = overridden
out["br_source"] = np.where(overridden, "war_thunder_wiki", "thunderskill")
out = out.drop(columns=wiki_cols)
return out
# ============================================================
# Manual War Thunder Wiki premium-status overrides
# ============================================================
# ThunderSkill occasionally has stale premium status (e.g. Sherman Hell is
# not marked premium in ThunderSkill, but the Wiki marks it premium). This
# uses the same wiki_ground_br_lookup.csv (its wiki_is_premium column) to
# correct is_premium before it flows into filters, CE, or anything else.
def _coerce_bool(value):
"""Best-effort parse of a boolean-like value (True/False, 'true'/'false',
1/0, ...). Returns pd.NA for anything ambiguous or unparsable -- never
guesses."""
if isinstance(value, bool):
return value
if pd.isna(value):
return pd.NA
s = str(value).strip().lower()
if s in ("true", "1", "1.0"):
return True
if s in ("false", "0", "0.0"):
return False
return pd.NA
def apply_wiki_premium_overrides(df: pd.DataFrame, lookup_df: pd.DataFrame | None) -> pd.DataFrame:
"""Override stale ThunderSkill is_premium with trusted War Thunder Wiki values.
Uses the same lookup CSV as apply_wiki_br_overrides (its wiki_is_premium
column). Preserves the original ThunderSkill value as
thunderskill_is_premium so both are inspectable. Adds premium_overridden
(bool) and premium_source ("war_thunder_wiki" or "thunderskill").
Safe no-op (returns ``df`` unchanged) if ``df`` is empty, has no
vehicle_slug or is_premium column, or ``lookup_df`` is None/empty, has no
vehicle_slug column, or has no wiki_is_premium column. Wiki values that
don't parse as a clean boolean are treated as missing -- the ThunderSkill
value is kept and premium_overridden stays False for that row.
premium_overridden is True only when a valid Wiki value exists AND it
disagrees with the original ThunderSkill value -- a valid Wiki value that
simply confirms ThunderSkill is not counted as an override.
"""
if df.empty:
return df
if "vehicle_slug" not in df.columns or "is_premium" not in df.columns:
return df
if lookup_df is None or lookup_df.empty or "vehicle_slug" not in lookup_df.columns:
return df
if "wiki_is_premium" not in lookup_df.columns:
return df
lookup = lookup_df[["vehicle_slug", "wiki_is_premium"]].copy()
lookup["vehicle_slug"] = _clean_text_series(lookup["vehicle_slug"])
lookup = lookup.dropna(subset=["vehicle_slug"]).drop_duplicates(
subset="vehicle_slug", keep="last"
)
lookup["wiki_is_premium"] = lookup["wiki_is_premium"].apply(_coerce_bool)
out = df.copy()
out["thunderskill_is_premium"] = out["is_premium"]
out = out.merge(lookup, on="vehicle_slug", how="left")
has_wiki_value = out["wiki_is_premium"].notna()
disagrees = pd.Series(False, index=out.index)
disagrees.loc[has_wiki_value] = (
out.loc[has_wiki_value, "wiki_is_premium"].astype(bool)
!= out.loc[has_wiki_value, "thunderskill_is_premium"].astype(bool)
)
out["is_premium"] = out["wiki_is_premium"].where(has_wiki_value, out["is_premium"]).astype(bool)
out["premium_overridden"] = disagrees
out["premium_source"] = np.where(has_wiki_value, "war_thunder_wiki", "thunderskill")
out = out.drop(columns=["wiki_is_premium"])
return out
def recent_window(df: pd.DataFrame, days: int = RECENT_WINDOW_DAYS) -> pd.DataFrame:
"""Keep observations within the most recent global date window.
The pipeline pulls up to 30 chart observations per vehicle. Most vehicles
have recent daily data, but sparse event vehicles may carry very old
observations; this drops those from the app's default views.
"""
out = df.copy()
if "date" not in out.columns or out["date"].isna().all():
return out
max_date = out["date"].max()
min_date = max_date - pd.Timedelta(days=days - 1)
return out[(out["date"] >= min_date) & (out["date"] <= max_date)].copy()
# ============================================================
# Vehicle aggregation -> vehicle_agg_df
# ============================================================
def _weighted_means(
df: pd.DataFrame,
metrics: list[str],
weight_col: str = "battles_weight",
group_col: str = "vehicle_slug",
) -> pd.DataFrame:
"""Battle-weighted mean per vehicle for each metric.
Vectorized: sum(metric * weight) / sum(weight) over rows where the metric is
present and weight > 0. Returns NaN when a vehicle has no valid weighted
rows (e.g. zero total battles), so we never emit a misleading 0.
"""
w = df[weight_col].fillna(0).clip(lower=0)
grp = df[group_col]
result = {}
for m in metrics:
valid = df[m].notna() & (w > 0)
num = (df[m] * w).where(valid, 0.0).groupby(grp).sum()
den = w.where(valid, 0.0).groupby(grp).sum()
result[m] = num / den.replace(0, np.nan)
return pd.DataFrame(result)
def build_vehicle_agg(recent_df: pd.DataFrame) -> pd.DataFrame:
"""Build vehicle_agg_df: one row per vehicle from the recent window.
Grouped by vehicle_slug only (not volatile columns like pic /
release_date_raw). Battles are summed; performance metrics are
battle-weighted averages; metadata is the first non-null value.
"""
if recent_df.empty:
return recent_df.copy()
df = recent_df.copy()
df["battles_weight"] = df["battles"].fillna(0).clip(lower=0)
meta_cols = [c for c in META_COLS if c in df.columns]
perf_cols = [c for c in CORE_PERF_COLS if c in df.columns]
grp = df.groupby("vehicle_slug", dropna=False)
meta = grp[meta_cols].agg(_first_valid)
agg = pd.DataFrame(
{
"battles": grp["battles"].sum(), # NaN treated as 0
"observations": grp.size(),
}
)
if "date" in df.columns:
agg["first_observed"] = grp["date"].min()
agg["last_observed"] = grp["date"].max()
agg["days_observed"] = grp["date"].nunique()
wmeans = _weighted_means(df, perf_cols)
out = meta.join(agg).join(wmeans).reset_index()
return out
# ============================================================
# Scoring
# ============================================================
# Short debug-column names for the per-metric within-BR z-scores.
_CE_METRIC_SHORT = {
"ground_frags_per_death": "kd",
"ground_frags_per_battle": "fpb",
"win_rate": "wr",
}
def _within_br_robust_z(
values: pd.Series,
br: pd.Series,
clip: float | None = None,
) -> pd.Series:
"""Robust z-score of ``values`` within each exact-BR peer group.
Uses median + MAD (scaled by 1.4826 to be normal-consistent). Guards:
* BRs with fewer than MIN_SCORE_PEERS non-null values -> NaN.
* A flat BR (scale below ROBUST_SCALE_FLOOR) -> z = 0 (everyone average).
* Rows with a NaN value or NaN BR -> NaN.
"""
med = values.groupby(br).transform("median")
mad = (values - med).abs().groupby(br).transform("median")
scale = 1.4826 * mad
count = values.groupby(br).transform("count")
has_spread = scale >= ROBUST_SCALE_FLOOR
z = (values - med) / scale.where(has_spread)
z = z.where(has_spread, 0.0) # flat BR -> everyone == average
z = z.where(values.notna(), np.nan) # missing value -> NaN
z = z.where(count >= MIN_SCORE_PEERS, np.nan) # too few peers -> NaN
if clip is not None:
z = z.clip(-clip, clip)
return z
def add_combat_effectiveness(df: pd.DataFrame) -> pd.DataFrame:
"""Combat Effectiveness Score: BR-relative overperformance, 0-100.
Computed within each EXACT realistic_br peer group:
1. transform skewed metrics with log1p (K/D, frags per battle);
win rate is used as-is.
2. empirical-Bayes shrinkage toward the exact-BR center, with
reliability = battles / (battles + PRIOR_BATTLES). Low-battle vehicles
are pulled strongly toward the BR average; high-battle vehicles keep
more of their observed value.
3. robust within-BR standardization (median + MAD) of the smoothed metric.
4. weighted combine (40% K/D, 40% frags/battle, 15% win rate, 5% a
within-BR confidence term = standardized log1p(battles), clipped).
5. score = 50 + 15 * z_total, clipped to 0-100.
50 = roughly BR-average; ~65 = a strong step above; ~95+ = exceptional.
Because it is robust-z (not percentile / min-max), the top vehicle in a BR
is NOT automatically 100. efficiency is intentionally excluded.
Missing-BR or signal-less (no battles) vehicles get NaN (not scoreable).
Adds debug columns: reliability, confidence_z, {kd,fpb,wr}_z_br.
"""
out = df.copy()
if "realistic_br" not in out.columns:
out["combat_effectiveness"] = np.nan
out["meta_score"] = np.nan
out["reliability"] = np.nan
return out
br = out["realistic_br"]
battles = out["total_battles_30d"] if "total_battles_30d" in out.columns else out.get("battles")
battles = pd.to_numeric(battles, errors="coerce").fillna(0).clip(lower=0)
reliability = battles / (battles + PRIOR_BATTLES)
out["reliability"] = reliability.round(4)
# --- per-metric smoothed, standardized z within exact BR ---
metric_terms = {} # metric -> (weight, z Series)
for metric, weight in CE_METRIC_WEIGHTS.items():
if metric not in out.columns:
continue
raw = pd.to_numeric(out[metric], errors="coerce")
t = np.log1p(raw) if metric in CE_LOG1P_METRICS else raw
center = t.groupby(br).transform("median") # exact-BR prior center
t_smooth = reliability * t + (1.0 - reliability) * center
z = _within_br_robust_z(t_smooth, br)
out[f"{_CE_METRIC_SHORT[metric]}_z_br"] = z
metric_terms[metric] = (weight, z)
# --- confidence / stability term (within-BR standardized log1p battles) ---
conf_z = _within_br_robust_z(np.log1p(battles), br, clip=CONFIDENCE_Z_CLIP)
out["confidence_z"] = conf_z
# --- weighted combine with per-row renormalization over available terms ---
num = pd.Series(0.0, index=out.index)
den = pd.Series(0.0, index=out.index)
n_metric = pd.Series(0, index=out.index)
for _, (weight, z) in metric_terms.items():
avail = z.notna()
num = num + (weight * z).where(avail, 0.0)
den = den + pd.Series(weight, index=out.index).where(avail, 0.0)
n_metric = n_metric + avail.astype(int)
# Scoreable requires a real BR and at least one usable metric (so a 0-battle
# vehicle is never scored "average" off the confidence term alone).
scoreable = br.notna() & (n_metric >= 1)
conf_avail = conf_z.notna() & scoreable
num = num + (CE_CONFIDENCE_WEIGHT * conf_z).where(conf_avail, 0.0)
den = den + pd.Series(CE_CONFIDENCE_WEIGHT, index=out.index).where(conf_avail, 0.0)
z_total = (num / den.where(den > 0)).where(scoreable, np.nan)
score = (SCORE_CENTER + SCORE_SLOPE * z_total).clip(0, 100).round(1)
out["combat_effectiveness"] = score.where(scoreable, np.nan)
out["meta_score"] = out["combat_effectiveness"] # vestigial alias
return out
def add_combat_effectiveness_legacy(df: pd.DataFrame) -> pd.DataFrame:
"""Legacy global-percentile score, kept only as combat_effectiveness_legacy
for validation/debug. This is the pre-change formula (incl. efficiency)."""
out = df.copy()
available = [c for c in CE_LEGACY_WEIGHTS if c in out.columns]
if not available:
out["combat_effectiveness_legacy"] = np.nan
return out
weight_sum = sum(CE_LEGACY_WEIGHTS[c] for c in available)
legacy = sum(
out[c].rank(pct=True) * CE_LEGACY_WEIGHTS[c] for c in available
) / weight_sum
out["combat_effectiveness_legacy"] = (legacy * 100).round(1)
return out
def assign_br_bracket(br: pd.Series) -> pd.Series:
"""Map BR -> a 1.0-wide bracket key (floor). NaN BR stays NaN."""
return np.floor(br)
def add_br_ranges(df: pd.DataFrame) -> pd.DataFrame:
"""Add broad lineup-style BR range fields (future-ready: HDBSCAN, sleepers,
lineup ranker, meta views). These are NOT the official scoring peer group --
the score uses exact realistic_br. Each range spans X.0 to X.7.
Adds: br_bracket (floor), br_range_min, br_range_max, br_range_label.
"""
out = df.copy()
if "realistic_br" in out.columns:
floor = assign_br_bracket(out["realistic_br"])
else:
floor = pd.Series(np.nan, index=out.index)
out["br_bracket"] = floor
out["br_range_min"] = floor
out["br_range_max"] = floor + 0.7
out["br_range_label"] = floor.apply(
lambda x: f"{x:.1f}–{x + 0.7:.1f}" if pd.notna(x) else np.nan
)
return out
# ============================================================
# Quality flags
# ============================================================
def add_quality_flags(df: pd.DataFrame) -> pd.DataFrame:
"""Add data-quality flags. These are informational for now; no global
filtering is applied by this module."""
out = df.copy()
out["observed_days"] = out["days_observed"] if "days_observed" in out.columns else np.nan
out["total_battles_30d"] = out["battles"] if "battles" in out.columns else np.nan
if "realistic_br" in out.columns:
out["has_realistic_br"] = out["realistic_br"].notna()
else:
out["has_realistic_br"] = False
out["has_sufficient_dates"] = out["observed_days"].fillna(0) >= MIN_SUFFICIENT_DAYS
out["has_sufficient_battles"] = out["total_battles_30d"].fillna(0) >= MIN_SUFFICIENT_BATTLES
perf_present = [c for c in CORE_PERF_COLS if c in out.columns]
if perf_present:
out["has_performance_data"] = out[perf_present].notna().any(axis=1)
else:
out["has_performance_data"] = False
out["is_analysis_ready"] = (
out["has_realistic_br"]
& out["has_sufficient_dates"]
& out["has_sufficient_battles"]
)
return out
# ============================================================
# Group aggregates (nation / BR) -- unchanged behavior
# ============================================================
def build_nation_aggregate(vehicle_df: pd.DataFrame) -> pd.DataFrame:
if vehicle_df.empty:
return vehicle_df.copy()
return (
vehicle_df
.groupby("country", as_index=False)
.agg(
vehicles=("vehicle_slug", "nunique"),
battles=("battles", "sum"),
avg_win_rate=("win_rate", "mean"),
avg_frags_per_battle=("ground_frags_per_battle", "mean"),
avg_frags_per_death=("ground_frags_per_death", "mean"),
avg_efficiency=("efficiency", "mean"),
avg_combat_effectiveness=("combat_effectiveness", "mean"),
)
.sort_values("avg_combat_effectiveness", ascending=False)
)
def build_br_aggregate(vehicle_df: pd.DataFrame) -> pd.DataFrame:
if vehicle_df.empty:
return vehicle_df.copy()
return (
vehicle_df
.groupby("realistic_br", as_index=False)
.agg(
vehicles=("vehicle_slug", "nunique"),
battles=("battles", "sum"),
avg_win_rate=("win_rate", "mean"),
avg_frags_per_battle=("ground_frags_per_battle", "mean"),
avg_frags_per_death=("ground_frags_per_death", "mean"),
avg_efficiency=("efficiency", "mean"),
avg_combat_effectiveness=("combat_effectiveness", "mean"),
)
.sort_values("realistic_br")
)
# ============================================================
# Nation Meta aggregates (for the redesigned Nation Meta tab)
# ============================================================
def build_nation_br_heatmap(vehicle_df: pd.DataFrame) -> pd.DataFrame:
"""One row per (country, br_range_label) for the Nation x BR Range heatmap.
win_rate_bw is the battle-weighted win rate over the 30-day window; the
avg_* fields and counts are for hover. Requires br_range_label / br_range_min
(from add_br_ranges) on the vehicle-level frame.
"""
if vehicle_df.empty or "br_range_label" not in vehicle_df.columns:
return pd.DataFrame()
df = vehicle_df.dropna(subset=["country", "br_range_label"]).copy()
if df.empty:
return pd.DataFrame()
w = df["battles"].fillna(0).clip(lower=0)
df["_wr_num"] = (df["win_rate"] * w).where(df["win_rate"].notna(), 0.0)
df["_wr_den"] = w.where(df["win_rate"].notna(), 0.0)
out = df.groupby(["country", "br_range_label"], as_index=False).agg(
br_range_min=("br_range_min", "min"),
battles=("battles", "sum"),
vehicles=("vehicle_slug", "nunique"),
avg_kd=("ground_frags_per_death", "mean"),
avg_fpb=("ground_frags_per_battle", "mean"),
avg_ce=("combat_effectiveness", "mean"),
_wr_num=("_wr_num", "sum"),
_wr_den=("_wr_den", "sum"),
)
out["win_rate_bw"] = out["_wr_num"] / out["_wr_den"].where(out["_wr_den"] > 0)
out = out.drop(columns=["_wr_num", "_wr_den"])
return out.sort_values(["country", "br_range_min"]).reset_index(drop=True)
def build_nation_daily_trend(recent_df: pd.DataFrame) -> pd.DataFrame:
"""One row per (date, country) with battle-weighted daily metrics.
Metrics are battle-weighted across that nation's vehicles on each day:
win_rate, ground_frags_per_death, ground_frags_per_battle; battles is summed.
CE Score is a window-level vehicle score and is deliberately not produced
here. Rolling smoothing is applied in the app layer.
"""
if recent_df.empty:
return pd.DataFrame()
df = recent_df.dropna(subset=["date", "country"]).copy()
if df.empty:
return pd.DataFrame()
w = df["battles"].fillna(0).clip(lower=0)
metrics = ["win_rate", "ground_frags_per_death", "ground_frags_per_battle"]
agg_kwargs = {"battles": ("battles", "sum")}
for m in metrics:
df[f"_{m}_num"] = (df[m] * w).where(df[m].notna(), 0.0)
df[f"_{m}_den"] = w.where(df[m].notna(), 0.0)
agg_kwargs[f"_{m}_num"] = (f"_{m}_num", "sum")
agg_kwargs[f"_{m}_den"] = (f"_{m}_den", "sum")
out = df.groupby(["date", "country"], as_index=False).agg(**agg_kwargs)
for m in metrics:
out[m] = out[f"_{m}_num"] / out[f"_{m}_den"].where(out[f"_{m}_den"] > 0)
keep = ["date", "country", "battles"] + metrics
return out[keep].sort_values(["country", "date"]).reset_index(drop=True)
def build_nation_summary(vehicle_df: pd.DataFrame) -> pd.DataFrame:
"""Nation-level summary cards/table: CE central tendency, totals, top BR
range (by battles), and the nation's best vehicle by CE Score."""
if vehicle_df.empty:
return pd.DataFrame()
df = vehicle_df.dropna(subset=["country"]).copy()
if df.empty:
return pd.DataFrame()
rows = []
for country, g in df.groupby("country"):
ce = g["combat_effectiveness"]
scored = g.dropna(subset=["combat_effectiveness"])
if not scored.empty:
best = scored.loc[scored["combat_effectiveness"].idxmax()]
best_vehicle = best.get("vehicle_name", np.nan)
best_vehicle_ce = best.get("combat_effectiveness", np.nan)
else:
best_vehicle = np.nan
best_vehicle_ce = np.nan
top_br_range = np.nan
if "br_range_label" in g.columns and g["br_range_label"].notna().any():
by_range = (
g.dropna(subset=["br_range_label"])
.groupby("br_range_label")["battles"].sum()
)
if not by_range.empty:
top_br_range = by_range.idxmax()
rows.append(
{
"country": country,
"vehicles": g["vehicle_slug"].nunique(),
"battles": g["battles"].sum(),
"avg_ce": ce.mean(),
"median_ce": ce.median(),
"top_br_range": top_br_range,
"best_vehicle": best_vehicle,
"best_vehicle_ce": best_vehicle_ce,
}
)
return pd.DataFrame(rows).sort_values("avg_ce", ascending=False).reset_index(drop=True)
# ============================================================
# Performance clustering (v1)
# ============================================================
# Clustering intentionally uses only three standardized features:
# CE Score, K/D (ground frags per death), and log1p(sample battles).
# Win rate / frags-per-battle / nation / vehicle type are context only.
CLUSTER_MIN_SAMPLE_BATTLES = 50 # default evidence floor (user-adjustable in UI)
CLUSTER_MIN_VEHICLES = 15 # below this we do not attempt clustering
CLUSTER_Z_HIGH = 0.4 # z-median above this counts as "high"
CLUSTER_Z_LOW = -0.4 # z-median below this counts as "low"
def build_vehicle_clusters(
df: pd.DataFrame,
min_sample_battles: int = CLUSTER_MIN_SAMPLE_BATTLES,
min_cluster_size=None,
min_samples=None,
):
"""Cluster vehicles with HDBSCAN on standardized
[combat_effectiveness, ground_frags_per_death, log1p(total_battles_30d)].
Missing-BR vehicles and rows lacking CE / K/D are excluded, as are vehicles
below ``min_sample_battles``. Returns ``(clustered_df, meta)`` where
clustered_df has: log1p_sample_battles, z_ce, z_kd, z_log_battles, cluster_id
(-1 = HDBSCAN noise). ``meta`` describes counts / quality. sklearn is imported
lazily so this module still imports if scikit-learn is absent.
"""
meta = {
"available": True,
"n_vehicles": 0,
"n_clusters": 0,
"noise_pct": float("nan"),
"silhouette": None,
"quality_label": "Weak",
"min_cluster_size": None,
"reason": None,
}
if df.empty:
return df.iloc[0:0].copy(), {**meta, "reason": "empty"}
work = df.copy()
has_br = (
work["has_realistic_br"].fillna(False)
if "has_realistic_br" in work.columns
else work["realistic_br"].notna()
)
mask = (
has_br
& work["combat_effectiveness"].notna()
& work["ground_frags_per_death"].notna()
& (work["total_battles_30d"].fillna(0) >= min_sample_battles)
)
cand = work[mask].copy()
n = len(cand)
meta["n_vehicles"] = n
if n < CLUSTER_MIN_VEHICLES:
return cand, {**meta, "reason": "too_few"}
try:
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import HDBSCAN
from sklearn.metrics import silhouette_score
except ImportError:
return cand, {**meta, "available": False, "reason": "sklearn_missing"}
cand["log1p_sample_battles"] = np.log1p(cand["total_battles_30d"].fillna(0))
feats = cand[
["combat_effectiveness", "ground_frags_per_death", "log1p_sample_battles"]
].to_numpy(dtype=float)
z = StandardScaler().fit_transform(feats)
cand["z_ce"] = z[:, 0]
cand["z_kd"] = z[:, 1]
cand["z_log_battles"] = z[:, 2]
# WT BR slices are small and standout groups may be only ~3 vehicles, so use
# a low min_cluster_size / min_samples. (Larger values marked almost
# everything as noise on these spread-out performance clouds.)
if min_cluster_size is None:
min_cluster_size = 3
if min_samples is None:
min_samples = 2
model = HDBSCAN(
min_cluster_size=int(min_cluster_size),
min_samples=int(min_samples),
copy=True,
)
labels = model.fit_predict(z)
cand["cluster_id"] = labels
non_noise = labels != -1
n_clusters = int(len({c for c in labels if c != -1}))
noise_pct = float((~non_noise).mean() * 100.0)