File size: 14,915 Bytes
4545835
 
747c55e
 
4545835
 
 
747c55e
4545835
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
 
747c55e
 
 
 
 
 
 
4545835
 
 
747c55e
 
4545835
747c55e
4545835
747c55e
 
 
4545835
747c55e
 
 
4545835
747c55e
4545835
 
747c55e
 
 
4545835
747c55e
 
4545835
 
 
 
747c55e
 
 
 
 
 
4545835
 
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
747c55e
4545835
747c55e
 
 
4545835
 
747c55e
4545835
 
 
747c55e
4545835
747c55e
4545835
747c55e
 
 
 
 
 
 
 
 
 
4545835
747c55e
 
 
 
 
 
 
 
 
 
 
4545835
 
 
 
747c55e
 
4545835
747c55e
 
4545835
 
747c55e
 
4545835
 
747c55e
 
4545835
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
 
747c55e
 
 
4545835
 
747c55e
 
 
 
 
 
 
 
 
4545835
747c55e
 
 
4545835
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
 
747c55e
 
 
4545835
747c55e
4545835
747c55e
 
 
 
 
 
 
 
 
 
4545835
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
747c55e
 
 
4545835
747c55e
 
 
 
4545835
747c55e
 
 
 
 
 
4545835
747c55e
4545835
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
 
 
747c55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4545835
 
747c55e
4545835
747c55e
 
4545835
747c55e
4545835
 
 
 
747c55e
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
import marimo

__generated_with = "0.12.0"
app = marimo.App(width="medium")


@app.cell
def _():
    import marimo as mo
    import pandas as pd
    from svg import SVG, G, Circle, Path, Title, Rect, Line, Polygon, Text
    import numpy as np
    from collections import Counter
    return (
        Circle,
        Counter,
        G,
        Line,
        Path,
        Polygon,
        Rect,
        SVG,
        Text,
        Title,
        mo,
        np,
        pd,
    )


@app.cell
def _(pd):
    #Data import

    stage_data= pd.read_csv("C:/Users/paolo/Desktop/data visualisation project/stage_data.csv")
    tdf_stages= pd.read_csv("C:/Users/paolo/Desktop/data visualisation project/tdf_stages.csv")
    tdf_winners= pd.read_csv("C:/Users/paolo/Desktop/data visualisation project/tdf_winners.csv")
    return stage_data, tdf_stages, tdf_winners


@app.cell
def _(mo, pd, tdf_stages):
    #Text blocks to insert the initial and final date used to filter the database 

    tdf_stages['Date'] = pd.to_datetime(tdf_stages['Date'], errors='coerce')

    start_date_input = mo.ui.text(
        label="Initial Date (YYYY-MM-DD)",
        value=tdf_stages["Date"].min().strftime("%Y-%m-%d")
    )
    end_date_input = mo.ui.text(
        label="Final date (YYYY-MM-DD)",
        value=tdf_stages["Date"].max().strftime("%Y-%m-%d")
    )
    return end_date_input, start_date_input


@app.cell
def _(end_date_input, mo, start_date_input):
    #show the text blocks in the output 

    mo.hstack(
        [start_date_input, end_date_input], justify="start"
    )
    return


@app.cell
def _(mo):
    #Slider to select the circles radius 

    radius =mo.ui.slider(start=1, stop=10, step=1, value=5, label="radius")
    return (radius,)


@app.cell
def _(Counter, end_date_input, start_date_input, tdf_stages):
    # Group the "type" variable in four groups 
    macro_class_mapping = {
        'Flat cobblestone stage': 'Plain',
        'Flat stage': 'Plain',
        'Flat Stage': 'Plain',
        'Half Stage': 'Other',
        'High mountain stage': 'Mountain',
        'Hilly stage': 'Hill',
        'Individual time trial': 'Chrono',
        'Intermediate stage': 'Other',
        'Medium mountain stage': 'Mountain',
        'Mountain stage': 'Mountain',
        'Mountain Stage': 'Mountain',
        'Mountain time trial': 'Chrono',
        'Plain stage': 'Plain',
        'Plain stage with cobblestones': 'Plain',
        'Stage with mountain': 'Mountain',
        'Stage with mountain(s)': 'Mountain',
        'Team time trial': 'Chrono',
        'Transition stage': 'Other'
    }

    tdf_stages['macro_class'] = tdf_stages['Type'].map(macro_class_mapping)

    # Filter the database using the data inserted in the Text Blocks 
    start_date = start_date_input.value
    end_date = end_date_input.value


    filtered_df = tdf_stages[(tdf_stages['Date'] >= start_date) & (tdf_stages['Date'] <= end_date)]



    # Find the 3 countries with most winning for each Type of stage

    all_macro_classes = ['Plain', 'Hill', 'Mountain', 'Chrono']

    macro_class_top3 = {}
    for macro_class in all_macro_classes:
        if macro_class in filtered_df['macro_class'].unique():
            group = filtered_df[filtered_df['macro_class'] == macro_class]
            top_countries = Counter(group['Winner_Country']).most_common(3)
            macro_class_top3[macro_class] = [country for country, _ in top_countries]
            while len(macro_class_top3[macro_class]) < 3:
                macro_class_top3[macro_class].append("NA") 
        else:
            macro_class_top3[macro_class] =[' ', ' ', ' ']

    print(macro_class_top3)
    return (
        all_macro_classes,
        end_date,
        filtered_df,
        group,
        macro_class,
        macro_class_mapping,
        macro_class_top3,
        start_date,
        top_countries,
    )


@app.cell
def _(tdf_winners):
    ##Scale function for flat stages 

    X_MIN, X_MAX = tdf_winners["height"].min(), tdf_winners["height"].max()
    Y_MIN, Y_MAX = tdf_winners["weight"].min(), tdf_winners["weight"].max()


    SVG_X_MIN_P, SVG_X_MAX_P = 60, 150
    SVG_Y_MIN_P, SVG_Y_MAX_P = 280, 470


    def scale_x_plain(x):
        return SVG_X_MIN_P + (x - X_MIN) / (X_MAX - X_MIN) * (SVG_X_MAX_P - SVG_X_MIN_P)

    def scale_y_plain(y):
        return SVG_Y_MAX_P - (y - Y_MIN) / (Y_MAX - Y_MIN) * (SVG_Y_MAX_P - SVG_Y_MIN_P)
    return (
        SVG_X_MAX_P,
        SVG_X_MIN_P,
        SVG_Y_MAX_P,
        SVG_Y_MIN_P,
        X_MAX,
        X_MIN,
        Y_MAX,
        Y_MIN,
        scale_x_plain,
        scale_y_plain,
    )


@app.cell
def _(macro_class_top3, scale_x_plain, scale_y_plain, tdf_winners):
    ## datapoints for plain stages 
    Cplain=str(macro_class_top3['Plain'][0])


    datapoints_plain = []
    for i in range(106):
        if Cplain.capitalize() in tdf_winners["birth_country"][i] :                                                        
            datapoints_plain.append(
            {
            'x': scale_x_plain(tdf_winners["height"][i]),
            'y': scale_y_plain(tdf_winners["weight"][i]),
            'data-id': i
        })

    print(Cplain)
    print(datapoints_plain)
    return Cplain, datapoints_plain, i


@app.cell
def _(X_MAX, X_MIN, Y_MAX, Y_MIN):
    ##Scale function for hilly stages 

    SVG_X_MIN_C, SVG_X_MAX_C = 210, 300
    SVG_Y_MIN_C, SVG_Y_MAX_C = 280, 470

    def scale_x_hill(x):
        return SVG_X_MIN_C + (x - X_MIN) / (X_MAX - X_MIN) * (SVG_X_MAX_C - SVG_X_MIN_C)

    def scale_y_hill(y):
        return SVG_Y_MAX_C - (y - Y_MIN) / (Y_MAX - Y_MIN) * (SVG_Y_MAX_C - SVG_Y_MIN_C)
    return (
        SVG_X_MAX_C,
        SVG_X_MIN_C,
        SVG_Y_MAX_C,
        SVG_Y_MIN_C,
        scale_x_hill,
        scale_y_hill,
    )


@app.cell
def _(i, macro_class_top3, scale_x_hill, scale_y_hill, tdf_winners):
    ## datapoints for hilly stages 
    Chill=str(macro_class_top3['Hill'][0])

    datapoints_hill = []
    for j in range(106):
        if Chill.capitalize() in tdf_winners["birth_country"][j]:           
            datapoints_hill.append(
            {
            'x': scale_x_hill(tdf_winners["height"][j]),
            'y': scale_y_hill(tdf_winners["weight"][j]),
            'data-id': i
        })
    return Chill, datapoints_hill, j


@app.cell
def _(X_MAX, X_MIN, Y_MAX, Y_MIN):
    ##Scale function for Mountain stages

    SVG_X_MIN_M, SVG_X_MAX_M = 360, 450
    SVG_Y_MIN_M, SVG_Y_MAX_M = 280, 470

    def scale_x_Mountain(x):
        return SVG_X_MIN_M + (x - X_MIN) / (X_MAX - X_MIN) * (SVG_X_MAX_M - SVG_X_MIN_M)

    def scale_y_Mountain(y):
        return SVG_Y_MAX_M - (y - Y_MIN) / (Y_MAX - Y_MIN) * (SVG_Y_MAX_M - SVG_Y_MIN_M)
    return (
        SVG_X_MAX_M,
        SVG_X_MIN_M,
        SVG_Y_MAX_M,
        SVG_Y_MIN_M,
        scale_x_Mountain,
        scale_y_Mountain,
    )


@app.cell
def _(macro_class_top3, scale_x_Mountain, scale_y_Mountain, tdf_winners):
    ## datapoints for Mountain stages 

    CMountain=str(macro_class_top3['Mountain'][0])

    datapoints_Mountain = []
    for k in range(106):
        if CMountain.capitalize() in tdf_winners["birth_country"][k]:
            datapoints_Mountain.append(
            {
            'x': scale_x_Mountain(tdf_winners["height"][k]),
            'y': scale_y_Mountain(tdf_winners["weight"][k]),
            'data-id': k
        })
    return CMountain, datapoints_Mountain, k


@app.cell
def _(X_MAX, X_MIN, Y_MAX, Y_MIN):
    ##Scale function for Chrono stages

    SVG_X_MIN_CR, SVG_X_MAX_CR = 520, 600
    SVG_Y_MIN_CR, SVG_Y_MAX_CR = 280, 470

    def scale_x_Chrono(x):
        return SVG_X_MIN_CR + (x - X_MIN) / (X_MAX - X_MIN) * (SVG_X_MAX_CR - SVG_X_MIN_CR)

    def scale_y_Chrono(y):
        return SVG_Y_MAX_CR - (y - Y_MIN) / (Y_MAX - Y_MIN) * (SVG_Y_MAX_CR - SVG_Y_MIN_CR)
    return (
        SVG_X_MAX_CR,
        SVG_X_MIN_CR,
        SVG_Y_MAX_CR,
        SVG_Y_MIN_CR,
        scale_x_Chrono,
        scale_y_Chrono,
    )


@app.cell
def _(macro_class_top3, scale_x_Chrono, scale_y_Chrono, tdf_winners):
    #datapoints for Chrono stages

    CChrono=str(macro_class_top3['Chrono'][0])
    datapoints_Chrono = []
    for z in range(106):
        if CChrono.capitalize() in tdf_winners["birth_country"][z] :

            datapoints_Chrono.append(
            {
            'x': scale_x_Chrono(tdf_winners["height"][z]),
            'y': scale_y_Chrono(tdf_winners["weight"][z]),
            'data-id': z
        })

    return CChrono, datapoints_Chrono, z


@app.cell
def _(
    Circle,
    datapoints_Chrono,
    datapoints_Mountain,
    datapoints_hill,
    datapoints_plain,
    pd,
    radius,
):
    #create circles for each stage type

    circles_plain = []
    for datapoint in datapoints_plain:
        if not (pd.isna(datapoint["x"]) or pd.isna(datapoint["y"])):
            circles_plain.append(
                Circle(
                    cx=datapoint["x"],
                    cy=datapoint["y"],
                    r=radius.value,
                    fill="green",            
                    fill_opacity=0.5,
                    stroke_width=1,
                    stroke="white"
                )
            )

    circles_hill = []
    for datapoint in datapoints_hill:
        if not (pd.isna(datapoint["x"]) or pd.isna(datapoint["y"])):
            circles_hill.append(
                Circle(
                    cx=datapoint["x"],
                    cy=datapoint["y"],
                    r=radius.value,
                    fill="orange",            
                    fill_opacity=0.5,
                    stroke_width=1,
                    stroke="white"
                )
            )

    circles_mountain = []
    for datapoint in datapoints_Mountain:
        if not (pd.isna(datapoint["x"]) or pd.isna(datapoint["y"])):
            circles_mountain.append(
                Circle(
                    cx=datapoint["x"],
                    cy=datapoint["y"],
                    r=radius.value,
                    fill="brown",            
                    fill_opacity=0.5,
                    stroke_width=1,
                    stroke="white"
                )
            )

    circles_chrono= []
    for datapoint in datapoints_Chrono:
        if not (pd.isna(datapoint["x"]) or pd.isna(datapoint["y"])):
            circles_chrono.append(
                Circle(
                    cx=datapoint["x"],
                    cy=datapoint["y"],
                    r=radius.value,
                    fill="darkgray",            
                    fill_opacity=0.5,
                    stroke_width=1,
                    stroke="white"
                )
            )
    return (
        circles_chrono,
        circles_hill,
        circles_mountain,
        circles_plain,
        datapoint,
    )


@app.cell
def _(
    CChrono,
    CMountain,
    Chill,
    Cplain,
    Line,
    Polygon,
    SVG,
    Text,
    circles_chrono,
    circles_hill,
    circles_mountain,
    circles_plain,
    macro_class_top3,
    mo,
    radius,
):
    #complete graph 

    plot = SVG(
        width=700,
        height=800,
        elements= 
        [ 
            # Rettangoli e poligoni
            Polygon(points=[(30, 250), (180, 250), (180, 650), (30, 650)], fill="green", stroke="black"),
            Polygon(points=[(30, 250), (70, 220), (220, 220), (180, 250)], fill="forestgreen", stroke="black"),
            Polygon(points=[(180, 250), (330, 250), (330, 650), (180, 650)], fill="orange", stroke="black"),
            Polygon(points=[(180, 250), (330, 250), (370, 220), (220, 220)], fill="chocolate", stroke="black"),
            Polygon(points=[(180, 250), (330, 250), (330, 200)], fill="goldenrod", stroke="black"),
            Polygon(points=[(180, 250), (220, 220), (370, 170), (330, 200)], fill="gold", stroke="black"),
            Polygon(points=[(330, 250), (480, 250), (420, 130), (330, 200)], fill="maroon", stroke="black"),
            Polygon(points=[(330, 650), (480, 650), (480, 250), (330, 250)], fill="chocolate", stroke="black"),
            Polygon(points=[(330, 200), (370, 170), (450, 100), (520, 220), (480, 250), (420, 130)], fill="maroon", stroke="black"),
            Polygon(points=[(480, 250), (630, 250), (670, 220), (520, 220)], fill="gray", stroke="black"),
            Polygon(points=[(480, 250), (630, 250), (630, 650), (480, 650)], fill="gray", stroke="black"),
            Polygon(points=[(630, 650), (670, 630), (670, 220), (630, 250)], fill="darkgray", stroke="black"),

            # Linee
            Line(x1=30, y1=500, x2=630, y2=500, stroke="black", stroke_width=1),
            Line(x1=630, y1=500, x2=670, y2=480, stroke="black", stroke_width=1),

            Line(x1=30, y1=570, x2=630, y2=570, stroke="black", stroke_width=1),
            Line(x1=630, y1=570, x2=670, y2=550, stroke="black", stroke_width=1),

            #scritte
            Text(x=120, y=490, text="height", font_size=20, fill="darkgreen"),
            Text(x=40, y=270, text="weight", font_size=20, fill="darkgreen"),
            Text(x=270, y=490, text="height", font_size=20, fill="darkorange"),
            Text(x=190, y=270, text="weight", font_size=20, fill="darkorange"),
            Text(x=420, y=490, text="height", font_size=20, fill="brown"),
            Text(x=340, y=270, text="weight", font_size=20, fill="brown"),
            Text(x=570, y=490, text="height", font_size=20, fill="darkgray"),
            Text(x=490, y=270, text="weight", font_size=20, fill="darkgray"),

            Text(x=50, y=400, text=Cplain, font_size=60, fill="darkgreen"),
            Text(x=200, y=400, text=Chill, font_size=60, fill="darkorange"),
            Text(x=350, y=400, text=CMountain, font_size=60, fill="brown"),
            Text(x=500, y=400, text=CChrono, font_size=60, fill="darkgray"),

            Text(x=70, y=550, text=str(macro_class_top3['Plain'][1]), font_size=40, fill="darkgreen"),
            Text(x=220, y=550, text=str(macro_class_top3['Hill'][1]), font_size=40, fill="darkorange"),
            Text(x=370, y=550, text=str(macro_class_top3['Mountain'][1]), font_size=40, fill="brown"),
            Text(x=520, y=550, text=str(macro_class_top3['Chrono'][1]), font_size=40,  fill="darkgray"),

            Text(x=85, y=620, text=str(macro_class_top3['Plain'][2]), font_size=20, fill="darkgreen"),
            Text(x=235, y=620, text=str(macro_class_top3['Hill'][2]), font_size=20, fill="darkorange"),
            Text(x=385, y=620, text=str(macro_class_top3['Mountain'][2]), font_size=20, fill="brown"),
            Text(x=535, y=620, text=str(macro_class_top3['Chrono'][2]), font_size=20,  fill="darkgray"),

        ] + circles_plain + circles_hill + circles_mountain + circles_chrono,
    )


    mo.Html(plot.as_str())

    mo.hstack(
        [mo.Html(plot.as_str()),  radius], justify="start"
    )
    return (plot,)


if __name__ == "__main__":
    app.run()