File size: 12,237 Bytes
1e2f286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Soccer Feature Engineering Pipeline - Extended Edition
======================================================
Adds temporal decomposition (1st half / 2nd half), tactical clustering,
similarity analysis, and visualizations.

Outputs:
  - features.csv                  - full-match 33 features (18 rows)
  - features_first_half.csv       - 1st-half features (18 rows)
  - features_second_half.csv      - 2nd-half features (18 rows)
  - features_halves_diff.csv      - 2nd-half minus 1st-half deltas (18 rows)
  - behavioral_fingerprint.csv    - 16-dim fingerprint per team-match (18 rows)
  - cluster_labels.csv            - KMeans cluster assignments per row
  - similarity_matrix.csv         - pairwise team similarity (18x18)
  - archetype_profiles.csv        - centroid profiles per cluster
  - analysis.png                  - radar chart grid of archetypes
"""

import glob
import json
import os
import warnings

import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.decomposition import PCA

warnings.filterwarnings("ignore")


def discover_dynamic_events_files(data_root="/app/opendata/data/matches"):
    pattern = os.path.join(data_root, "*", "*_dynamic_events.csv")
    files = glob.glob(pattern, recursive=True)
    files.sort()
    return files


def compute_features_for_match(dynamic_events_path, period_filter=None):
    """
    Compute all 33 features for a single match.
    If period_filter is set (1 or 2), only events from that period are used.
    """
    df = pd.read_csv(dynamic_events_path, low_memory=False)
    match_id = df["match_id"].iloc[0]

    if period_filter is not None:
        df = df[df["period"] == period_filter]

    team_ids = sorted(df["team_id"].unique().tolist())

    records = []
    for team_id in team_ids:
        rec = {"match_id": match_id, "team_id": int(team_id)}

        pp = df[(df["event_type"] == "player_possession") & (df["team_id"] == team_id)].copy()
        obe = df[(df["event_type"] == "on_ball_engagement") & (df["team_id"] == team_id)].copy()
        obr = df[(df["event_type"] == "off_ball_run") & (df["team_id"] == team_id)].copy()

        # DIMENSION 1 - ATTACKING STRUCTURE (att1-att5)
        passes = pp[pp["pass_outcome"].notna()]
        rec["att1"] = int((passes["third_end"] == "attacking_third").sum())
        rec["att2"] = int(((pp["carry"] == True) & (pp["third_end"] == "attacking_third")).sum())
        pass_opp_bypassed = passes["n_opponents_bypassed"].fillna(0).clip(lower=0)
        rec["att3"] = float(pass_opp_bypassed.sum())
        rec["att4"] = int((passes["last_line_break"] == True).sum())
        rec["att5"] = int((passes["third_start"] == "attacking_third").sum())

        # DIMENSION 2 - BUILD-UP PROFILE (att6-att10)
        phase_counts = pp["team_in_possession_phase_type"].value_counts()
        rec["att6"] = int(phase_counts.get("build_up", 0))
        rec["att7"] = int(phase_counts.get("direct", 0))
        rec["att8"] = int(phase_counts.get("set_play", 0))
        rec["att9"] = int(phase_counts.get("quick_break", 0))
        rec["att10"] = int(phase_counts.get("transition", 0))

        # DIMENSION 3 - POSSESSION QUALITY (att11-att20)
        rec["att11"] = int((pp["one_touch"] == True).sum())
        rec["att12"] = int((pp["quick_pass"] == True).sum())
        rec["att13"] = int(pp["lead_to_shot"].sum())
        rec["att14"] = int(pp["lead_to_goal"].sum())
        rec["att15"] = float(pp["delta_to_last_defensive_line_gain"].fillna(0).clip(lower=0).sum())
        rec["att16"] = float(pp["last_defensive_line_height_gain"].fillna(0).clip(lower=0).sum())
        rec["att17"] = int((pp["forward_momentum"] == True).sum())
        rec["att18"] = float(pp["n_passing_options"].fillna(0).sum())
        rec["att19"] = float(pp["n_passing_options_dangerous_difficult"].fillna(0).sum())
        rec["att20"] = int((obr["event_subtype"] == "run_ahead_of_the_ball").sum())

        # DIMENSION 4 - PRESSING & DEFENSIVE SHAPE (def1-def7)
        rec["def1"] = int(len(obe))
        rec["def2"] = int((obe["event_subtype"] == "counter_press").sum())
        rec["def3"] = int((obe["event_subtype"] == "recovery_press").sum())
        chain_starts = obe[obe["index_in_pressing_chain"] == 1.0]
        rec["def4"] = float(chain_starts["pressing_chain_length"].fillna(0).sum())
        rec["def5"] = int(len(chain_starts))
        rec["def6"] = float(obe["pressing_chain_length"].max() if len(obe) > 0 else 0)
        rec["def7"] = int((obe["stop_possession_danger"] == True).sum())

        # DIMENSION 5 - OFF-BALL MOVEMENT INTELLIGENCE (run1-run5 + gng)
        rec["run1"] = int((obr["break_defensive_line"] == True).sum())
        rec["run2"] = int((obr["push_defensive_line"] == True).sum())
        rec["run3"] = int((obr["event_subtype"] == "behind").sum())
        rec["run4"] = int((obr["event_subtype"] == "overlap").sum())
        rec["run5"] = int((obr["third_start"] == "attacking_third").sum())
        rec["att_give_and_go_initiated"] = int((pp["initiate_give_and_go"] == True).sum())

        records.append(rec)

    return pd.DataFrame(records)


def build_full_match_features(files, output_path="/app/features.csv"):
    all_dfs = []
    for f in files:
        try:
            match_df = compute_features_for_match(f)
            all_dfs.append(match_df)
        except Exception as e:
            print(f"  ERROR: {os.path.basename(f)} -> {e}")

    features_df = pd.concat(all_dfs, ignore_index=True)
    feature_cols = (
        [f"att{i}" for i in range(1, 21)] +
        [f"def{i}" for i in range(1, 8)] +
        [f"run{i}" for i in range(1, 6)] +
        ["att_give_and_go_initiated"]
    )
    ordered_cols = ["match_id", "team_id"] + feature_cols
    features_df = features_df[[c for c in ordered_cols if c in features_df.columns]]

    for col in features_df.columns:
        if features_df[col].dtype == float:
            rounded = features_df[col].round(2)
            if (rounded == rounded.astype(int)).all():
                features_df[col] = rounded.astype(int)
            else:
                features_df[col] = rounded

    features_df.to_csv(output_path, index=False)
    print(f"Wrote full-match features: {features_df.shape}")
    return features_df


def build_halves_features(files):
    halves = {1: [], 2: []}
    for f in files:
        for period in [1, 2]:
            try:
                match_df = compute_features_for_match(f, period_filter=period)
                match_df["period"] = period
                halves[period].append(match_df)
            except Exception as e:
                print(f"  ERROR: {os.path.basename(f)} period {period} -> {e}")

    first_half = pd.concat(halves[1], ignore_index=True)
    second_half = pd.concat(halves[2], ignore_index=True)

    feature_cols = (
        [f"att{i}" for i in range(1, 21)] +
        [f"def{i}" for i in range(1, 8)] +
        [f"run{i}" for i in range(1, 6)] +
        ["att_give_and_go_initiated"]
    )

    first_half = first_half.sort_values(["match_id", "team_id"]).reset_index(drop=True)
    second_half = second_half.sort_values(["match_id", "team_id"]).reset_index(drop=True)

    delta = first_half[["match_id", "team_id"]].copy()
    for col in feature_cols:
        delta[col] = second_half[col] - first_half[col]

    for df in [first_half, second_half, delta]:
        for col in feature_cols:
            if df[col].dtype == float:
                rounded = df[col].round(2)
                if (rounded == rounded.astype(int)).all():
                    df[col] = rounded.astype(int)
                else:
                    df[col] = rounded

    first_half.to_csv("/app/features_first_half.csv", index=False)
    second_half.to_csv("/app/features_second_half.csv", index=False)
    delta.to_csv("/app/features_halves_diff.csv", index=False)

    print(f"Wrote 1st-half features: {first_half.shape}")
    print(f"Wrote 2nd-half features: {second_half.shape}")
    print(f"Wrote half-to-half deltas: {delta.shape}")
    return first_half, second_half, delta


def build_behavioral_fingerprint(features_df, output_path="/app/behavioral_fingerprint.csv"):
    fingerprint_cols = (
        [f"att{i}" for i in range(6, 11)] +
        [f"def{i}" for i in range(4, 7)] +
        [f"run{i}" for i in range(1, 6)] +
        ["att1", "att3", "att15", "att16"]
    )
    fp = features_df[["match_id", "team_id"] + fingerprint_cols].copy()
    fp.to_csv(output_path, index=False)
    print(f"Wrote behavioral fingerprint: {fp.shape}")
    return fp, fingerprint_cols


def run_clustering(fp_df, feature_cols, n_clusters=4, output_labels="/app/cluster_labels.csv",
                   output_archetypes="/app/archetype_profiles.csv"):
    X = fp_df[feature_cols].values
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X)

    kmeans = KMeans(n_clusters=n_clusters, random_state=42, n_init=10)
    labels = kmeans.fit_predict(X_scaled)

    fp_df["cluster"] = labels
    fp_df.to_csv(output_labels, index=False)

    archetypes = fp_df.groupby("cluster")[feature_cols].mean().round(2)
    archetypes.to_csv(output_archetypes)
    print(f"Wrote cluster labels: {fp_df.shape}")
    print(f"Wrote archetype profiles: {archetypes.shape}")
    print(f"\nCluster counts:\n{fp_df['cluster'].value_counts().sort_index()}")
    print(f"\nArchetype Profiles:\n{archetypes}")
    return labels, archetypes, kmeans, scaler


def build_similarity_matrix(fp_df, feature_cols, output_path="/app/similarity_matrix.csv"):
    X = fp_df[feature_cols].values
    sim = cosine_similarity(X)
    labels = fp_df.apply(lambda r: f"{r['match_id']}_{r['team_id']}", axis=1)
    sim_df = pd.DataFrame(sim, index=labels, columns=labels)
    sim_df.to_csv(output_path)
    print(f"Wrote similarity matrix: {sim_df.shape}")
    return sim_df


def generate_radar_chart(archetypes, output_path="/app/analysis.png"):
    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt

    n_clusters = len(archetypes)
    categories = archetypes.columns.tolist()
    N = len(categories)

    archetypes_norm = (archetypes - archetypes.min()) / (archetypes.max() - archetypes.min())
    archetypes_norm = archetypes_norm.fillna(0)

    angles = [n / float(N) * 2 * np.pi for n in range(N)]
    angles += angles[:1]

    fig, axs = plt.subplots(1, n_clusters, figsize=(5 * n_clusters, 5), subplot_kw=dict(polar=True))
    if n_clusters == 1:
        axs = [axs]

    colors = plt.cm.tab10(np.linspace(0, 1, n_clusters))

    for idx, (cluster_id, row) in enumerate(archetypes_norm.iterrows()):
        ax = axs[idx]
        values = row.values.tolist()
        values += values[:1]
        ax.plot(angles, values, color=colors[idx], linewidth=2, label=f"Cluster {cluster_id}")
        ax.fill(angles, values, color=colors[idx], alpha=0.25)
        ax.set_xticks(angles[:-1])
        ax.set_xticklabels(categories, fontsize=7)
        ax.set_title(f"Archetype {cluster_id}", fontsize=12, fontweight="bold")

    plt.tight_layout()
    plt.savefig(output_path, dpi=150)
    plt.close()
    print(f"Saved radar chart: {output_path}")


def main():
    files = discover_dynamic_events_files()
    print(f"Discovered {len(files)} dynamic_events.csv files\n")

    features_df = build_full_match_features(files)
    first_half, second_half, delta = build_halves_features(files)
    fp_df, fp_cols = build_behavioral_fingerprint(features_df)
    labels, archetypes, kmeans, scaler = run_clustering(fp_df, fp_cols, n_clusters=4)
    sim_matrix = build_similarity_matrix(fp_df, fp_cols)
    generate_radar_chart(archetypes)

    print("\n=== ALL OUTPUTS GENERATED ===")
    for f in [
        "/app/features.csv",
        "/app/features_first_half.csv",
        "/app/features_second_half.csv",
        "/app/features_halves_diff.csv",
        "/app/behavioral_fingerprint.csv",
        "/app/cluster_labels.csv",
        "/app/archetype_profiles.csv",
        "/app/similarity_matrix.csv",
        "/app/analysis.png",
    ]:
        if os.path.exists(f):
            sz = os.path.getsize(f)
            print(f"  {f} ({sz} bytes)")


if __name__ == "__main__":
    main()