File size: 16,376 Bytes
fd38574
 
8206722
fd38574
7007406
 
b466419
 
fd38574
8206722
fd38574
 
8206722
fd38574
8206722
 
 
 
 
 
 
 
 
fd38574
 
b466419
8206722
fd38574
8206722
 
 
 
 
 
 
 
 
fd38574
 
 
 
 
 
8206722
b466419
8206722
b466419
8206722
 
 
 
fd38574
8206722
fd38574
8206722
fd38574
 
b466419
8206722
 
 
 
fd38574
 
8206722
 
 
 
 
 
 
 
 
fd38574
8206722
fd38574
8206722
fd38574
b466419
8206722
 
 
 
fd38574
 
8206722
b466419
8206722
 
 
 
 
 
 
fd38574
8206722
fd38574
8206722
fd38574
b466419
fd38574
 
8206722
 
 
 
fd38574
8206722
fd38574
8206722
fd38574
 
 
 
 
 
 
 
8206722
fd38574
8206722
fd38574
4214624
fd38574
b466419
fd38574
8206722
 
fd38574
8206722
 
 
 
 
 
 
 
 
 
fd38574
 
 
8206722
 
 
fd38574
8206722
fd38574
 
 
 
8206722
fd38574
8206722
fd38574
8206722
fd38574
 
8206722
fd38574
8206722
 
 
 
b466419
8206722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
 
8206722
 
b466419
8206722
b466419
8206722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
8206722
 
 
 
 
b466419
8206722
 
 
 
 
 
 
 
 
 
b466419
8206722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
8206722
 
 
 
b466419
 
8206722
 
 
 
 
 
 
b466419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7007406
8206722
 
 
77e9502
8206722
b466419
8206722
 
 
 
 
 
 
 
 
 
 
 
b466419
 
 
8206722
 
 
 
 
b466419
8206722
 
 
 
 
 
 
 
 
 
b466419
8206722
 
 
 
 
 
 
 
 
 
 
 
 
b466419
8206722
b466419
8206722
 
 
 
7007406
b466419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8206722
7007406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8206722
7007406
 
 
 
 
 
 
 
 
 
8206722
7007406
 
8206722
b466419
7007406
 
 
 
 
 
 
 
 
 
 
 
 
 
b466419
7007406
 
b466419
7007406
 
 
 
 
 
 
 
b466419
 
7007406
 
 
 
8206722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77e9502
8206722
 
fd38574
8206722
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
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

filtered_df = pd.read_pickle("data_frames/filtered_df.pkl")

def create_stacked_area_chart(
    topk_df, gini_df, hhi_df, events, palette, start_time=None, end_time=None
):

    # Create subplot with secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    # Define metric order
    metric_order = [
        "Top 1",
        "Top 1 - 10",
        "Top 10 - 100",
        "Top 100 - 1000",
        "Top 1000 - 10000",
        "Rest",
    ]

    # Create stacked area traces
    for i, metric in enumerate(metric_order):
        metric_data = topk_df[topk_df["metric"] == metric]

        # Sort by time and get values
        metric_data = metric_data.sort_values("time")
        if start_time:
            metric_data = metric_data[metric_data["time"] >= start_time]
        if end_time:
            metric_data = metric_data[metric_data["time"] <= end_time]
            
        x_vals = metric_data["time"]
        y_vals = metric_data["value"]

        # Add area trace
        fig.add_trace(
            go.Scatter(
                x=x_vals,
                y=y_vals,
                name=metric,
                mode="lines",
                line=dict(width=0, color=palette[i % len(palette)]),
                fill="tonexty" if i > 0 else "tozeroy",
                fillcolor=palette[i % len(palette)],
                stackgroup="one",
                hovertemplate="<b>%{fullData.name}</b><br>"
                + "Time: %{x}<br>"
                + "Value: %{y}<extra></extra>",
            ),
            secondary_y=False,
        )

    # Add overlay lines
    # Gini Coefficient
    gini_data = gini_df.sort_values("time")
    if start_time:
        gini_data = gini_data[gini_data["time"] >= start_time]
    if end_time:
        gini_data = gini_data[gini_data["time"] <= end_time]
    fig.add_trace(
        go.Scatter(
            x=gini_data["time"],
            y=gini_data["value"],
            name="Gini Coefficient",
            mode="lines",
            line=dict(color="#6b46c1", width=3),
            yaxis="y2",
            hovertemplate="<b>Gini Coefficient</b><br>"
            + "Time: %{x}<br>"
            + "Value: %{y:.3f}<extra></extra>",
        ),
        secondary_y=True,
    )

    # HHI (ร—10)
    hhi_data = hhi_df.sort_values("time")
    if start_time:
        hhi_data = hhi_data[hhi_data["time"] >= start_time]
    if end_time:
        hhi_data = hhi_data[hhi_data["time"] <= end_time]
    fig.add_trace(
        go.Scatter(
            x=hhi_data["time"],
            y=hhi_data["value"] * 10,
            name="HHI (ร—10)",
            mode="lines",
            line=dict(color="#ec4899", width=3),
            yaxis="y2",
            hovertemplate="<b>HHI (ร—10)</b><br>"
            + "Time: %{x}<br>"
            + "Value: %{y:.3f}<extra></extra>",
        ),
        secondary_y=True,
    )

    # Add vertical lines for events
    for event_name, event_date in events.items():
        fig.add_shape(
            type="line",
            x0=event_date,
            x1=event_date,
            y0=0,
            y1=1,
            yref="paper",
            line=dict(color="#333333", width=2, dash="dash"),
        )

        # Add annotation for the event
        fig.add_annotation(
            x=event_date,
            y=1,
            yref="paper",
            text=event_name,
            showarrow=False,
            yshift=10,
            font=dict(size=12),
        )

    fig.update_layout(
        autosize=True,
        font_size=14,
        showlegend=True,
        margin=dict(l=60, r=60, t=40, b=60),
        plot_bgcolor="white",
        hovermode="x unified",
    )

    # Update x-axis to be governed by start_time/end_time
    xaxis_range = None
    if start_time is not None and end_time is not None:
        xaxis_range = [start_time, end_time]
    elif start_time is not None:
        xaxis_range = [start_time, None]
    elif end_time is not None:
        xaxis_range = [None, end_time]

    fig.update_xaxes(
        title_text="",
        showgrid=True,
        gridcolor="lightgray",
        gridwidth=1,
        range=xaxis_range,
    )

    # Update primary y-axis (left)
    fig.update_yaxes(
        title_text="Model Market Share",
        showgrid=True,
        gridcolor="lightgray",
        gridwidth=1,
        secondary_y=False,
    )

    # Update secondary y-axis (right)
    fig.update_yaxes(
        title_text="Concentration Indices", showgrid=False, secondary_y=True
    )

    return fig


def create_world_map(
    df, time_col="time", metric_col="metric", value_col="value", top_n_labels=10, start_time=None, end_time=None
):
    # Get all unique times and sort them
    times = sorted(df[time_col].unique())

    # Country code mapping
    country_code_map = {
        "Germany": "DEU",
        "United States of America": "USA",
        "China": "CHN",
        "France": "FRA",
        "India": "IND",
        "Israel": "ISR",
        "South Korea": "KOR",
        "United Kingdom": "GBR",
        "Switzerland": "CHE",
        "United Arab Emirates": "ARE",
        "Vietnam": "VNM",
        "Singapore": "SGP",
        "Chile": "CHL",
        "Hong Kong": "HKG",
        "Japan": "JPN",
        "Canada": "CAN",
        "Spain": "ESP",
        "Finland": "FIN",
        "Indonesia": "IDN",
        "Russia": "RUS",
        "Iran": "IRN",
        "Belarus": "BLR",
        "Thailand": "THA",
        "UAE": "ARE",
        "Argentina": "ARG",
        "Iceland": "ISL",
        "Poland": "POL",
        "Sweden": "SWE",
        "Taiwan": "TWN",
        "Lebanon": "LBN",
        "Algeria": "DZA",
        "Bulgaria": "BGR",
        "Norway": "NOR",
        "Netherlands": "NLD",
        "Hungary": "HUN",
        "Estonia": "EST",
        "Qatar": "QAT",
        "Brazil": "BRA",
        "Morocco": "MAR",
        "Slovenia": "SVN",
        "Ghana": "GHA",
        "Uganda": "UGA",
        "Turkey": "TUR",
    }

    df["country_code"] = df[metric_col].map(country_code_map)
    mapped_data = df.dropna(subset=["country_code"])

    fig = make_subplots(
        rows=1,
        cols=1,
        specs=[[{"type": "geo"}]],
    )

    # Function to aggregate data for time range
    def aggregate_time_range(start_time, end_time):
        range_data = mapped_data[
            (mapped_data[time_col] >= start_time) & (mapped_data[time_col] <= end_time)
        ]
        # Average values across time range
        agg_data = (
            range_data.groupby([metric_col, "country_code"])[value_col]
            .mean()
            .reset_index()
        )
        agg_data["percentage"] = agg_data[value_col] * 100
        return agg_data.sort_values("percentage", ascending=False)

    # Initial data if start or end time are not set (full range)
    if start_time is None:
        start_time = times[0]
    if end_time is None:
        end_time = times[-1]
    initial_data = aggregate_time_range(start_time, end_time)
    # top_countries = initial_data.head(top_n_labels)

    # Create hover text
    hover_text = []
    for _, row in initial_data.iterrows():
        hover_text.append(
            f"<b>{row[metric_col]}</b><br>"
            f"Avg Downloads: {row['percentage']:.1f}% of total<br>"
            f"Avg Value: {row[value_col]:.6f}"
        )

    # Add choropleth to plot
    fig.add_trace(
        go.Choropleth(
            locations=initial_data["country_code"],
            z=initial_data["percentage"],
            text=hover_text,
            hovertemplate="%{text}<extra></extra>",
            colorscale=[
                "#001219",
                "#0a9396",
                "#94d2bd",
                "#e9d8a6",
                "#ee9b00",
                "#ca6702",
                "#bb3e03",
                "#9b2226",
            ],
            colorbar=dict(
                title="Avg % of Total Downloads",
                tickfont=dict(size=12),
                len=0.6,
                x=1.02,
                y=0.7,
            ),
            marker_line_color="#ffffff",
            marker_line_width=1.5,
            geo="geo",
        ),
        row=1,
        col=1,
    )

    # Country center coordinates for labels
    # country_centers = {
    #     "USA": {"lat": 39.8, "lon": -98.5},
    #     "CHN": {"lat": 35.8, "lon": 104.2},
    #     "DEU": {"lat": 51.2, "lon": 10.4},
    #     "GBR": {"lat": 55.4, "lon": -3.4},
    #     "FRA": {"lat": 46.6, "lon": 2.2},
    #     "JPN": {"lat": 36.2, "lon": 138.3},
    #     "IND": {"lat": 20.6, "lon": 78.9},
    #     "CAN": {"lat": 56.1, "lon": -106.3},
    #     "RUS": {"lat": 61.5, "lon": 105.3},
    #     "BRA": {"lat": -14.2, "lon": -51.9},
    #     "AUS": {"lat": -25.3, "lon": 133.8},
    #     "KOR": {"lat": 35.9, "lon": 127.8},
    # }

    # # Add initial labels using scattergeo instead of annotations
    # label_lons = []
    # label_lats = []
    # label_texts = []

    # for _, country in top_countries.iterrows():
    #     country_code = country["country_code"]
    #     if country_code in country_centers:
    #         center = country_centers[country_code]
    #         label_lons.append(center["lon"])
    #         label_lats.append(center["lat"])
    #         label_texts.append(f"{country['percentage']:.1f}%")

    # # Add text labels as a scattergeo trace
    # fig.add_trace(
    #     go.Scattergeo(
    #         lon=label_lons,
    #         lat=label_lats,
    #         text=label_texts,
    #         mode="text",
    #         textfont=dict(
    #             color="#ffffff", size=13, family="Inter, system-ui, sans-serif"
    #         ),
    #         textposition="middle center",
    #         showlegend=False,
    #         hoverinfo="skip",
    #         geo="geo",
    #     ),
    #     row=1,
    #     col=1,
    # )
    
    # Update layout
    fig.update_layout(
        title=dict(
            text="Model Downloads by Country",
            x=0.5,
            font=dict(size=20),
        ),
        width=1200,
        height=800,
        plot_bgcolor="#ffffff",
        paper_bgcolor="#ffffff",
        margin=dict(l=0, r=120, t=100, b=60),
    )

    # Update geo layout
    fig.update_geos(
        showframe=False,
        showland=True,
        landcolor="#d0cfcf",
        coastlinecolor="#b8b8b8",
        projection_type="natural earth",
        bgcolor="#ffffff",
    )

    return fig

def create_range_slider(df):
    if df.empty or "time" not in df.columns:
        return go.Figure()

    times = sorted(df["time"].unique())
    fig = go.Figure()

    # Invisible trace just to attach slider to the x-axis
    fig.add_trace(
        go.Scatter(
            x=times,
            y=[0] * len(times),
            mode="lines",
            line=dict(color="rgba(0,0,0,0)"),  # Invisible line
            hoverinfo="skip",
            showlegend=False
        )
    )

    # Enable range slider
    fig.update_layout(
        xaxis=dict(
            rangeslider=dict(visible=False),
            type="date"
        ),
        yaxis=dict(visible=False),
        margin=dict(t=20, b=20, l=20, r=20),
        height=100
    )

    return fig

def create_leaderboard(country_df, developer_df, model_df, start_time=None, end_time=None, top_n=10):
    # Country -> Emoji mapping
    country_emoji_map = {
        "United States of America": "๐Ÿ‡บ๐Ÿ‡ธ",
        "China": "๐Ÿ‡จ๐Ÿ‡ณ",
        "Germany": "๐Ÿ‡ฉ๐Ÿ‡ช",
        "France": "๐Ÿ‡ซ๐Ÿ‡ท",
        "India": "๐Ÿ‡ฎ๐Ÿ‡ณ",
        "Italy": "๐Ÿ‡ฎ๐Ÿ‡น",
        "Japan": "๐Ÿ‡ฏ๐Ÿ‡ต",
        "South Korea": "๐Ÿ‡ฐ๐Ÿ‡ท",
        "United Kingdom": "๐Ÿ‡ฌ๐Ÿ‡ง",
        "Canada": "๐Ÿ‡จ๐Ÿ‡ฆ",
        "Brazil": "๐Ÿ‡ง๐Ÿ‡ท",
        "Australia": "๐Ÿ‡ฆ๐Ÿ‡บ",
        "Unknown": "โ“",
        "Finland": "๐Ÿ‡ซ๐Ÿ‡ฎ",
        "Lebanon": "๐Ÿ‡ฑ๐Ÿ‡ง ",
    }
    
    # Ensure datetime
    country_df["time"] = pd.to_datetime(country_df["time"])
    developer_df["time"] = pd.to_datetime(developer_df["time"])
    model_df["time"] = pd.to_datetime(model_df["time"])

    # Add corresponding country info to developer_df and model_df, mapping "metric" to "author" and "metric" to "model"
    # Merge with filtered_df to get country info
    developer_df = developer_df.merge(
        filtered_df[["author", "country"]].drop_duplicates(),
        left_on="metric",
        right_on="author",
        how="left"
    ).rename(columns={"country": "country_metric"}).drop(columns=["author"])
    model_df = model_df.merge(
        filtered_df[["model", "country"]].drop_duplicates(),
        left_on="metric",
        right_on="model",
        how="left"
    ).rename(columns={"country": "country_metric"}).drop(columns=["model"])

    if start_time is None:
        start_time = country_df["time"].min()
    if end_time is None:
        end_time = country_df["time"].max()

    # Filter time range
    country_df_filtered = country_df[
        (country_df["time"] >= start_time) & (country_df["time"] <= end_time)
    ]
    developer_df_filtered = developer_df[
        (developer_df["time"] >= start_time) & (developer_df["time"] <= end_time)
    ]
    model_df_filtered = model_df[
        (model_df["time"] >= start_time) & (model_df["time"] <= end_time)
    ]

    if country_df_filtered.empty and developer_df_filtered.empty and model_df_filtered.empty:
        return go.Figure()

    # Function to get top N leaderboard with percentage
    def get_top_n_leaderboard(df, group_col, label, top_n=10):
        top = (
            df.groupby(group_col)["value"]
            .sum()
            .sort_values(ascending=False)
            .head(top_n)
            .reset_index()
            .rename(columns={group_col: label, "value": "Total Value"})
        )
        total_value = top["Total Value"].sum()
        if total_value > 0:
            top["% of total"] = top["Total Value"] / total_value * 100
        else:
            top["% of total"] = 0

        # add column with metadata (country emoji for country, country for developer/model)
        if label == "Country":
            top["Attributes"] = top[label].map(country_emoji_map).fillna("")
        else:
            # Get the country_metric for each developer/model with the already merged info
            top = top.merge(
                df[[group_col, "country_metric"]].drop_duplicates(),
                left_on=label,
                right_on=group_col,
                how="left"
            ).drop(columns=[group_col])
            top["Attributes"] = top["country_metric"].map(country_emoji_map).fillna("")
        return top[[label, "Attributes", "% of total"]]

    top_countries = get_top_n_leaderboard(country_df_filtered, "metric", "Country", top_n=top_n)
    top_developers = get_top_n_leaderboard(developer_df_filtered, "metric", "Developer", top_n=top_n)
    top_models = get_top_n_leaderboard(model_df_filtered, "metric", "Model", top_n=top_n)

    # Create subplot grid with 3 columns
    fig = make_subplots(
        rows=1, cols=3,
        subplot_titles=("Top Countries", "Top Developers", "Top Models"),
        specs=[[{"type": "table"}, {"type": "table"}, {"type": "table"}]]
    )

    # Add country table
    fig.add_trace(
        go.Table(
            header=dict(values=list(top_countries.columns),
                        fill_color="lightgrey", align="left"),
            cells=dict(values=[top_countries[col] for col in top_countries.columns],
                       fill_color="white", align="left"),
        ),
        row=1, col=1
    )

    # Add developer table
    fig.add_trace(
        go.Table(
            header=dict(values=list(top_developers.columns),
                        fill_color="lightgrey", align="left"),
            cells=dict(values=[top_developers[col] for col in top_developers.columns],
                       fill_color="white", align="left"),
        ),
        row=1, col=2
    )

    # Add model table
    fig.add_trace(
        go.Table(
            header=dict(values=list(top_models.columns),
                        fill_color="lightgrey", align="left"),
            cells=dict(values=[top_models[col] for col in top_models.columns],
                       fill_color="white", align="left"),
        ),
        row=1, col=3
    )

    fig.update_layout(
        height=400,
        showlegend=False,
        title_text="Leaderboards"
    )

    return fig