File size: 44,983 Bytes
58cea02
 
 
 
91e473e
d765ee8
0841c51
 
38d3f0b
0841c51
 
 
 
 
 
 
 
 
910ce9f
 
 
 
 
 
0841c51
 
 
910ce9f
69590e2
e04a121
38d3f0b
e04a121
0841c51
dc9501e
869c271
 
 
48da594
869c271
 
 
 
7365d98
629cb6e
ceb4948
869c271
 
629cb6e
 
 
 
 
869c271
 
 
 
39841d9
 
 
 
 
 
 
 
 
de7aa9f
591ec4f
35472d1
39841d9
de7aa9f
39841d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591ec4f
39841d9
35472d1
39841d9
0841c51
58cea02
 
9c7e08b
45a70a9
1748ccd
d18e5a9
8f424e5
0841c51
45351e3
c744629
c6bf99d
31a2d34
58cea02
4ad4038
 
 
 
 
 
8e43993
99e552f
8e43993
d04558f
58cea02
1666aa7
7de18e9
1689df1
7829724
 
4127d38
 
d69a1b2
dc0d88b
f543677
7f07fe7
 
 
c4642dd
7f07fe7
7829724
d69a1b2
7829724
237985f
7829724
c1c18f8
7829724
 
e04a121
7829724
8b08b1a
a692d2e
1666aa7
0841c51
41768e4
 
 
 
a34bb65
 
6e5cc93
996f8cb
a34bb65
 
a692d2e
6a6220a
a692d2e
41768e4
 
 
 
b5afac7
6a6220a
 
 
 
 
 
b5afac7
 
928ec6e
a692d2e
440bba8
a692d2e
 
 
a11ceac
39841d9
 
a11ceac
 
39841d9
 
35472d1
 
cff79f7
a11ceac
cff79f7
 
a692d2e
68d3916
e7e2a49
 
 
5b9c82c
 
 
68d3916
 
1854e4d
0e549ce
3e52c68
a4e3e2e
0e549ce
3597da4
a4e3e2e
e084aa6
1854e4d
28939d0
a4e3e2e
9f87d22
 
 
 
 
28939d0
 
5d76637
59dc088
 
 
d765ee8
0e549ce
a4e3e2e
0e549ce
d765ee8
 
59dc088
d765ee8
0e549ce
a4e3e2e
0e549ce
d765ee8
 
0b97fc2
d91cbaa
 
0295f53
 
 
 
 
 
 
 
 
 
a4e3e2e
76d511e
 
 
59dc088
99e552f
62a6685
 
 
 
 
 
 
 
 
 
 
 
16fbcab
6db62f0
f49d54b
f56fa41
 
 
 
 
a4e3e2e
 
 
f56fa41
 
 
 
a4e3e2e
 
 
f56fa41
 
5370c50
0b97fc2
5370c50
 
 
 
 
 
 
 
 
 
 
 
 
 
f56fa41
a4e3e2e
f56fa41
 
 
 
b36408f
55a782f
 
 
 
 
 
 
b36408f
 
 
 
 
f56fa41
 
 
16fbcab
96d409b
a4e3e2e
1817a5f
a4e3e2e
1cbe27c
1599cba
 
0d01fa6
f344ebf
6e8cffc
886a898
e025a7c
b27b8d0
 
 
 
 
c7440a1
 
 
 
 
 
 
 
 
 
 
 
1cbe27c
c7440a1
 
886a898
6e8cffc
1599cba
4ac617e
b9319bd
 
4ac617e
b9319bd
 
4ac617e
b9319bd
 
4ac617e
b9319bd
 
4ac617e
b9319bd
544cf42
1599cba
 
544cf42
16aefb8
6e8cffc
 
 
 
 
8aaea01
a4e3e2e
 
 
 
 
 
 
 
 
1599cba
3eb2035
 
a4e3e2e
 
f344ebf
3e52c68
6e8cffc
 
 
 
 
 
a4e3e2e
6e8cffc
a19edd8
6e8cffc
 
 
 
 
 
 
 
 
 
 
 
a19edd8
6e8cffc
 
 
a19edd8
 
9da8f46
a19edd8
857c2eb
6e8cffc
 
 
 
a4e3e2e
6e8cffc
 
 
 
 
 
 
3e52c68
 
 
e8a3351
6e8cffc
b36408f
6e8cffc
c9fd1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc0d88b
 
c9fd1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45351e3
00f9a6c
c744629
 
 
 
 
 
 
 
 
 
c9fd1e3
6e8cffc
45351e3
df8ffd8
45351e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e622a8
45351e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e8cffc
c744629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e8cffc
c744629
 
 
 
 
 
 
 
 
 
 
 
 
 
6e8cffc
c6bf99d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e8cffc
c6bf99d
 
 
 
 
 
 
 
 
 
 
 
 
 
8f424e5
 
31a2d34
 
 
 
 
 
 
 
 
 
 
 
 
 
06c79a5
8f424e5
31a2d34
 
 
 
 
 
 
8f424e5
b36408f
d415f18
 
 
 
 
a4e3e2e
d415f18
 
 
 
 
 
fd1c071
 
 
 
 
 
 
31a2d34
d415f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eabaa7d
d415f18
 
 
 
eabaa7d
d415f18
 
 
 
 
 
 
2ceda65
d415f18
e8a3351
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
import streamlit as st
st.set_page_config(layout="wide")
import numpy as np
import pandas as pd
from rapidfuzz import process, fuzz
from collections import Counter
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from datetime import datetime

def init_conn():
        
        uri = st.secrets['mongo_uri']
        client = MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
        db = client['Contest_Information']

        return db
    
def grab_contest_names(db, sport, type):
    if type == 'Classic':
        db_type = 'reg'
    elif type == 'Showdown':
        db_type = 'sd'
    collection = db[f'{sport}_{db_type}_contest_info']
    cursor = collection.find()

    curr_info = pd.DataFrame(list(cursor)).drop('_id', axis=1)
    curr_info['Date'] = pd.to_datetime(curr_info['Contest Date'].sort_values(ascending = False))
    curr_info['Date'] = curr_info['Date'].dt.strftime('%Y-%m-%d')
    contest_names = curr_info['Contest Name'] + ' - ' + curr_info['Date']
    
    return contest_names, curr_info

def grab_contest_player_info(db, sport, type, contest_date, contest_name, contest_id_map):
    if type == 'Classic':
        db_type = 'reg'
    elif type == 'Showdown':
        db_type = 'showdown'
    collection = db[f'{sport}_{db_type}_player_info']
    cursor = collection.find()

    player_info = pd.DataFrame(list(cursor)).drop('_id', axis=1)
    player_info = player_info[player_info['Contest Date'] == contest_date]
    player_info = player_info.rename(columns={'Display Name': 'Player'})
    player_info = player_info.sort_values(by='Salary', ascending=True).drop_duplicates(subset='Player', keep='first')

    info_maps = {
        'position_dict': dict(zip(player_info['Player'], player_info['Position'])),
        'salary_dict': dict(zip(player_info['Player'], player_info['Salary'])),
        'team_dict': dict(zip(player_info['Player'], player_info['Team'])),
        'opp_dict': dict(zip(player_info['Player'], player_info['Opp'])),
        'fpts_avg_dict': dict(zip(player_info['Player'], player_info['Avg FPTS']))
    }
    
    return player_info, info_maps

def export_contest_file(db, sport, type, contest_date, contest_id, contest_data):
    if type == 'Classic':
        db_type = 'reg'
    elif type == 'Showdown':
        db_type = 'showdown'
    collection = db[f'{sport}_{db_type}_contest_data']
    try:
        cursor = collection.find()
        contest_import = pd.DataFrame(list(cursor)).drop('_id', axis=1)
        if contest_id in contest_import['Contest ID'].values:
            return_message = "Data for this contest already exists, no need to upload, but we appreciate the effort!"
            return return_message
    except:
        contest_import = pd.DataFrame(columns = ['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup', 'Player', 'Roster Position', '%Drafted', 'FPTS', 'Contest Date', 'Contest ID'])
    
    contest_data['Contest Date'] = contest_date
    contest_data['Contest ID'] = contest_id
    contest_import = pd.concat([contest_import, contest_data], ignore_index=True)

    chunk_size = 10000
    collection.drop()
    for i in range(0, len(contest_import), chunk_size):
        for _ in range(5):
            try:
                df_chunk = contest_import.iloc[i:i + chunk_size]
                collection.insert_many(df_chunk.to_dict('records'), ordered=False)
                break
            except Exception as e:
                print(f"Retry due to error: {e}")
    return_message = "Contest data uploaded successfully! We appreciate the data!"

    return return_message

db = init_conn()

## import global functions
from global_func.load_contest_file import load_contest_file
from global_func.create_player_exposures import create_player_exposures
from global_func.create_stack_exposures import create_stack_exposures
from global_func.create_stack_size_exposures import create_stack_size_exposures
from global_func.create_general_exposures import create_general_exposures
from global_func.grab_contest_data import grab_contest_data
from global_func.create_player_comparison import create_player_comparison
from global_func.create_stack_comparison import create_stack_comparison
from global_func.create_size_comparison import create_size_comparison
from global_func.create_general_comparison import create_general_comparison

def is_valid_input(file):
    if isinstance(file, pd.DataFrame):
        return not file.empty
    else:
        return file is not None  # For Streamlit uploader objects

player_exposure_format = {'Exposure Overall': '{:.2%}', 'Exposure Top 1%': '{:.2%}', 'Exposure Top 5%': '{:.2%}', 'Exposure Top 10%': '{:.2%}', 'Exposure Top 20%': '{:.2%}'}
dupe_format = {'uniques%': '{:.2%}', 'under_5%': '{:.2%}', 'under_10%': '{:.2%}'}

tab1, tab2 = st.tabs(["Data Load", "Contest Analysis"])
with tab1:
    col1, col2 = st.columns(2)

    with col1:
        if st.button('Clear data', key='reset1'):
            st.session_state.clear()
        sport_options, date_options = st.columns(2)
        parse_type = 'Manual'
        with sport_options:
            sport_select = st.selectbox("Select Sport", ['MLB', 'MMA', 'GOLF', 'NBA', 'NHL', 'WNBA', 'TENNIS'], key='sport_select')
            type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'], key='type_var')
            try:
                contest_names, curr_info = grab_contest_names(db, sport_select, type_var)
            except:
                st.error("No contests found for this sport and/or game type")
                st.stop()
            
        with date_options:
            date_list = curr_info['Date'].sort_values(ascending=False).unique()
            # date_list = date_list[date_list != pd.Timestamp.today().strftime('%Y-%m-%d')]
            date_select = st.selectbox("Select Date", date_list, key='date_select')
            date_select2 = (pd.to_datetime(date_select) + pd.Timedelta(days=1)).strftime('%Y-%m-%d')
            
            name_parse = curr_info[curr_info['Date'] == date_select]['Contest Name'].reset_index(drop=True)
            contest_id_map = dict(zip(name_parse, curr_info[curr_info['Date'] == date_select]['Contest ID']))
            date_select = date_select.replace('-', '')
            date_select2 = date_select2.replace('-', '')

        contest_name_var = st.selectbox("Select Contest to load", name_parse)
        if parse_type == 'DB Search':
            if 'Contest_file_helper' in st.session_state:
                del st.session_state['Contest_file_helper']
            if 'Contest_file' in st.session_state:
                del st.session_state['Contest_file']
            if 'Contest_file' not in st.session_state:
                if st.button('Load Contest Data', key='load_contest_data'):
                    st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select, contest_name_var, contest_id_map)
                    st.session_state['Contest_file'] = grab_contest_data(sport_select, contest_name_var, contest_id_map, date_select, date_select2)
            else:
                pass
    with col2:
        st.info(f"If you are manually loading and do not have the results CSV for the contest you selected, you can find it here: https://www.draftkings.com/contest/gamecenter/{contest_id_map[contest_name_var]}#/, or you can initiate a download with this link: https://www.draftkings.com/contest/exportfullstandingscsv/{contest_id_map[contest_name_var]}")
        if parse_type == 'Manual':
            if 'Contest_file_helper' in st.session_state:
                del st.session_state['Contest_file_helper']
            if 'Contest_file' in st.session_state:
                del st.session_state['Contest_file']
            if 'Contest_file' not in st.session_state:
                st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
                st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select, contest_name_var, contest_id_map)
                try:
                    st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
                except:
                    st.warning('Please upload a Contest CSV')
            else:
                pass

    if 'Contest_file' in st.session_state:
        st.session_state['Contest'], st.session_state['ownership_df'], st.session_state['actual_df'], st.session_state['entry_list'], check_lineups = load_contest_file(st.session_state['Contest_file'], type_var, st.session_state['player_info'], sport_select)
        st.session_state['Contest'] = st.session_state['Contest'].dropna(how='all')
        st.session_state['Contest'] = st.session_state['Contest'].reset_index(drop=True)
        if st.session_state['Contest'] is not None:
            success_col, info_col, upload_col, message_col = st.columns([1, 3, 1, 2])
            with success_col:
                st.success('Contest file loaded successfully!')
            with info_col:
                st.warning("If you have confirmed that the data is correct, you can send the CSV to the database to enrich Paydirt's sources and help us create actionable tools and algorithms >>")
            with upload_col:
                if st.button('Send file to Database?', key='export_contest_file'):
                    return_message = export_contest_file(db, sport_select, type_var, date_select, contest_id_map[contest_name_var], st.session_state['Contest_file'])
            with message_col:
                try:
                    st.info(return_message)
                except:
                    pass
            st.dataframe(st.session_state['Contest'].head(100))
        
    if 'Contest_file' in st.session_state:
        st.session_state['ownership_dict'] = dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own']))
        st.session_state['actual_dict'] = dict(zip(st.session_state['actual_df']['Player'], st.session_state['actual_df']['FPTS']))
        st.session_state['salary_dict'] = st.session_state['info_maps']['salary_dict']
        st.session_state['team_dict'] = st.session_state['info_maps']['team_dict']
        st.session_state['pos_dict'] = st.session_state['info_maps']['position_dict']

with tab2:
    excluded_cols = ['BaseName', 'EntryCount']
    exclude_stacks = ['BaseName', 'EntryCount', 'SP', 'SP1', 'SP2']
    if 'Contest' in st.session_state and 'display_contest_info' not in st.session_state:
        st.session_state['player_columns'] = [col for col in st.session_state['Contest'].columns if col not in excluded_cols]
        st.session_state['stack_columns'] = [col for col in st.session_state['Contest'].columns if col not in exclude_stacks]
        print(st.session_state['player_columns'])
        for col in st.session_state['player_columns']:
            st.session_state['Contest'][col] = st.session_state['Contest'][col].astype(str).str.strip()
    
        # Create mapping dictionaries
        st.session_state['map_dict'] = {
            'pos_map': st.session_state['pos_dict'],
            'team_map': st.session_state['team_dict'],
            'salary_map': st.session_state['salary_dict'],
            'own_map': st.session_state['ownership_dict'],
            'own_percent_rank': dict(zip(st.session_state['ownership_df']['Player'], st.session_state['ownership_df']['Own'].rank(pct=True)))
        }
        # Create a copy of the dataframe for calculations
        working_df = st.session_state['Contest'].copy()

        if type_var == 'Classic':
            working_df['stack'] = working_df.apply(
                lambda row: Counter(
                    st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']]
                    if st.session_state['map_dict']['team_map'].get(player, '') != ''
                ).most_common(1)[0][0] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']]) else '',
                axis=1
            )
            working_df['stack_size'] = working_df.apply(
                lambda row: Counter(
                    st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']]
                    if st.session_state['map_dict']['team_map'].get(player, '') != ''
                ).most_common(1)[0][1] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[st.session_state['stack_columns']]) else '',
                axis=1
            )
            working_df['salary'] = working_df.apply(lambda row: sum(st.session_state['salary_dict'].get(player, 0) for player in row[st.session_state['player_columns']]), axis=1)
            working_df['actual_fpts'] = working_df.apply(lambda row: sum(st.session_state['actual_dict'].get(player, 0) for player in row[st.session_state['player_columns']]), axis=1)
            working_df['actual_own'] = working_df.apply(lambda row: sum(st.session_state['ownership_dict'].get(player, 0) for player in row[st.session_state['player_columns']]), axis=1)
            print("Sample row values:")
            print(working_df.iloc[0][st.session_state['player_columns']])
            print("Sample salary calculation:")
            sample_row = working_df.iloc[0]
            sample_salary = sum(st.session_state['salary_dict'].get(player, 0) for player in sample_row[st.session_state['player_columns']])
            print(f"Sample salary: {sample_salary}")
            print("Individual player salaries:")
            for player in sample_row[st.session_state['player_columns']]:
                salary = st.session_state['salary_dict'].get(player, 0)
                print(f"  {player}: {salary}")
            working_df['sorted'] = working_df[st.session_state['player_columns']].apply(
                lambda row: ','.join(sorted(row.values)),
                axis=1
            )
            working_df['dupes'] = working_df.groupby('sorted').transform('size')

            working_df['uniques'] = working_df.groupby('BaseName').apply(
                lambda x: (x['dupes'] == 1).sum()
            ).reindex(working_df['BaseName']).values

            working_df['under_5'] = working_df.groupby('BaseName').apply(
                lambda x: (x['dupes'] <= 5).sum()
            ).reindex(working_df['BaseName']).values

            working_df['under_10'] = working_df.groupby('BaseName').apply(
                lambda x: (x['dupes'] <= 10).sum()
            ).reindex(working_df['BaseName']).values

            working_df = working_df.reset_index()
            working_df['percentile_finish'] = working_df['index'].rank(pct=True)
            working_df['finish'] = working_df['index']
            working_df = working_df.drop(['sorted', 'index'], axis=1)
        
        elif type_var == 'Showdown':
            working_df['stack'] = working_df.apply(
                lambda row: Counter(
                    st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:]
                    if st.session_state['map_dict']['team_map'].get(player, '') != ''
                ).most_common(1)[0][0] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:]) else '',
                axis=1
            )
            working_df['stack_size'] = working_df.apply(
                lambda row: Counter(
                    st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:]
                    if st.session_state['map_dict']['team_map'].get(player, '') != ''
                ).most_common(1)[0][1] if any(st.session_state['map_dict']['team_map'].get(player, '') for player in row[2:]) else '',
                axis=1
            )
            if sport_select == 'GOLF':
                working_df['salary'] = working_df.apply(lambda row: sum(st.session_state['salary_dict'].get(player, 0) for player in row), axis=1)
                working_df['actual_fpts'] = working_df.apply(lambda row: sum(st.session_state['actual_dict'].get(player, 0) for player in row), axis=1)
            else:
                # Modified salary calculation with 1.5x multiplier for first player
                working_df['salary'] = working_df.apply(
                    lambda row: (st.session_state['map_dict']['salary_map'].get(row[2], 0) * 1.5) + 
                            sum(st.session_state['map_dict']['salary_map'].get(player, 0) for player in row[3:]),
                    axis=1
                )
                # Modified actual_fpts calculation with 1.5x multiplier for first player
                working_df['actual_fpts'] = working_df.apply(
                    lambda row: (st.session_state['actual_dict'].get(row[2], 0) * 1.5) + 
                            sum(st.session_state['actual_dict'].get(player, 0) for player in row[3:]),
                    axis=1
                )
            working_df['actual_own'] = working_df.apply(lambda row: sum(st.session_state['ownership_dict'].get(player, 0) for player in row), axis=1)
            working_df['sorted'] = working_df[st.session_state['player_columns']].apply(
                lambda row: ','.join(sorted(row.values)),
                axis=1
            )
            working_df['dupes'] = working_df.groupby('sorted').transform('size')

            working_df['uniques'] = working_df.groupby('BaseName').apply(
                lambda x: (x['dupes'] == 1).sum()
            ).reindex(working_df['BaseName']).values

            working_df['under_5'] = working_df.groupby('BaseName').apply(
                lambda x: (x['dupes'] <= 5).sum()
            ).reindex(working_df['BaseName']).values

            working_df['under_10'] = working_df.groupby('BaseName').apply(
                lambda x: (x['dupes'] <= 10).sum()
            ).reindex(working_df['BaseName']).values

            working_df = working_df.reset_index()
            working_df['percentile_finish'] = working_df['index'].rank(pct=True)
            working_df['finish'] = working_df['index']
            working_df = working_df.drop(['sorted', 'index'], axis=1)
        # working_df['stack_size'] = working_df['stack_size'].fillna(1).astype(int)
        st.session_state['field_player_frame'] = create_player_exposures(working_df, st.session_state['player_columns'])
        st.session_state['field_stack_frame'] = create_stack_exposures(working_df)
        st.session_state['display_contest_info'] = working_df.copy()
        st.session_state['contest_info_reset'] = working_df.copy()
        st.session_state['unique_players'] = pd.unique(st.session_state['display_contest_info'][st.session_state['player_columns']].values.ravel('K'))
        st.session_state['unique_players'] = [p for p in st.session_state['unique_players'] if p != 'nan']  # Remove any NaN values

    if 'display_contest_info' in st.session_state:
        with st.expander("Info and filters"):
            st.info("Note that any filtering here needs to be reset manually, i.e. if you parse down the specific users and want to reset the table, just backtrack your filtering by setting it back to 'All'")
            clear_col, reset_col, blank_col = st.columns([1, 1, 7])
            with clear_col:
                if st.button('Clear data', key='reset3'):
                    st.session_state.clear()
            with reset_col:
                if st.button('Reset filters', key='reset4'):
                    st.session_state['entry_parse_var'] = 'All'
                    st.session_state['entry_names'] = []
                    st.session_state['low_entries_var'] = 1
                    st.session_state['high_entries_var'] = 150
                    st.session_state['stack_parse_var'] = 'All'
                    st.session_state['stack_names'] = []
                    st.session_state['stack_size_parse_var'] = 'All'
                    st.session_state['stack_size_names'] = []
                    st.session_state['player_parse_var'] = 'All'
                    st.session_state['player_names'] = []
                    st.session_state['remove_var'] = 'No'
                    st.session_state['remove_names'] = []
                    st.session_state['display_contest_info'] = st.session_state['contest_info_reset'].copy()
                    st.session_state['unique_players'] = pd.unique(st.session_state['display_contest_info'][st.session_state['player_columns']].values.ravel('K'))
                    st.session_state['unique_players'] = [p for p in st.session_state['unique_players'] if p != 'nan']  # Remove any NaN values

            with st.form(key='filter_form'):
                users_var, entries_var, stack_var, stack_size_var, player_var, remove_var = st.columns(6)
                with users_var:
                    st.session_state['entry_parse_var'] = st.selectbox("Do you want to view a specific user(s)?", ['All', 'Specific'])
                    st.session_state['entry_names'] = st.multiselect("Select players", options=st.session_state['entry_list'], default=[])
                with entries_var:
                    st.session_state['low_entries_var'] = st.number_input("Low end of entries range", min_value=0, max_value=150, value=1)
                    st.session_state['high_entries_var'] = st.number_input("High end of entries range", min_value=0, max_value=150, value=150)
                with stack_var:
                    st.session_state['stack_parse_var'] = st.selectbox("Do you want to view lineups with specific team(s)?", ['All', 'Specific'])
                    st.session_state['stack_names'] = st.multiselect("Select teams", options=st.session_state['display_contest_info']['stack'].unique(), default=[])
                with stack_size_var:
                    st.session_state['stack_size_parse_var'] = st.selectbox("Do you want to view a specific stack size(s)?", ['All', 'Specific'])
                    st.session_state['stack_size_names'] = st.multiselect("Select stack sizes", options=st.session_state['display_contest_info']['stack_size'].unique(), default=[])
                with player_var:
                    st.session_state['player_parse_var'] = st.selectbox("Do you want to view lineups with specific player(s)?", ['All', 'Specific'])
                    st.session_state['player_names'] = st.multiselect("Select players to lock", options=st.session_state['unique_players'], default=[])
                with remove_var:
                    st.session_state['remove_var'] = st.selectbox("Do you want to remove a specific player(s)?", ['No', 'Yes'])
                    st.session_state['remove_names'] = st.multiselect("Select players to remove", options=st.session_state['unique_players'], default=[])
                submitted = st.form_submit_button("Submit")
                if submitted:
                    if 'player_frame' in st.session_state:
                        del st.session_state['player_frame']
                    if 'stack_frame' in st.session_state:
                        del st.session_state['stack_frame']

                    if st.session_state['entry_parse_var'] == 'Specific' and st.session_state['entry_names']:
                        st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['BaseName'].isin(st.session_state['entry_names'])]
                    if st.session_state['stack_parse_var'] == 'Specific' and st.session_state['stack_names']:
                        st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['stack'].isin(st.session_state['stack_names'])]
                    if st.session_state['stack_size_parse_var'] == 'Specific' and st.session_state['stack_size_names']:
                        st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['stack_size'].isin(st.session_state['stack_size_names'])]
                    if st.session_state['player_parse_var'] == 'Specific' and st.session_state['player_names']:
                        mask = st.session_state['display_contest_info'][st.session_state['player_columns']].apply(lambda row: all(player in row.values for player in st.session_state['player_names']), axis=1)
                        st.session_state['display_contest_info'] = st.session_state['display_contest_info'][mask]
                    if st.session_state['remove_var'] == 'Yes' and st.session_state['remove_names']:
                        mask = st.session_state['display_contest_info'][st.session_state['player_columns']].apply(lambda row: any(player in row.values for player in st.session_state['remove_names']), axis=1)
                        st.session_state['display_contest_info'] = st.session_state['display_contest_info'][~mask]
                    if st.session_state['low_entries_var'] and st.session_state['high_entries_var']:
                        st.session_state['display_contest_info'] = st.session_state['display_contest_info'][st.session_state['display_contest_info']['EntryCount'].between(st.session_state['low_entries_var'], st.session_state['high_entries_var'])]
    
    if 'display_contest_info' in st.session_state:
        # Initialize pagination in session state if not exists
        if 'current_page' not in st.session_state:
            st.session_state.current_page = 1

        # Calculate total pages
        rows_per_page = 500
        total_rows = len(st.session_state['display_contest_info'])
        total_pages = (total_rows + rows_per_page - 1) // rows_per_page

        # Create pagination controls in a single row
        pagination_cols = st.columns([4, 1, 1, 1, 4])
        with pagination_cols[1]:
            if st.button(f"Previous Page"):
                if st.session_state['current_page'] > 1:
                    st.session_state.current_page -= 1
                else:
                    st.session_state.current_page = 1
                    if 'player_frame' in st.session_state:
                        del st.session_state['player_frame']
                    if 'stack_frame' in st.session_state:
                        del st.session_state['stack_frame']

        with pagination_cols[3]:
            if st.button(f"Next Page"):
                st.session_state.current_page += 1
                if 'player_frame' in st.session_state:
                    del st.session_state['player_frame']
                if 'stack_frame' in st.session_state:
                    del st.session_state['stack_frame']

        # Calculate start and end indices for current page
        start_idx = (st.session_state.current_page - 1) * rows_per_page
        end_idx = min((st.session_state.current_page) * rows_per_page, total_rows)
        st.dataframe(
            st.session_state['display_contest_info'].iloc[start_idx:end_idx].style
            .background_gradient(axis=0)
            .background_gradient(cmap='RdYlGn')
            .format(precision=2), 
            height=500,
            use_container_width=True,
            hide_index=True
        )
    else:
        st.stop()
    
    if 'Contest' in st.session_state:
        with st.container():
            tab1, tab2, tab3, tab4, tab5 = st.tabs(['Player Used Info', 'Stack Used Info', 'Stack Size Info', 'General Info', 'Duplication Info'])
            with tab1:
                player_pos_form_col, player_comp_form_col = st.columns(2)
                with player_pos_form_col:
                    with st.form(key='player_info_pos_form'):
                        col1, col2 = st.columns(2)
                        with col1:
                            pos_var = st.selectbox("Which position(s) would you like to view?",  ['All', 'Specific'], key='pos_var')
                        with col2:
                            if sport_select == 'MLB':
                                pos_select = st.multiselect("Select your position(s)", ['P', 'C', '1B', '2B', '3B', 'SS', 'OF'], key='pos_select')
                            elif sport_select == 'NBA':
                                pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF', 'C'], key='pos_select')
                            elif sport_select == 'WNBA':
                                pos_select = st.multiselect("Select your position(s)", ['PG', 'SG', 'SF', 'PF'], key='pos_select')
                            elif sport_select == 'NFL':
                                pos_select = st.multiselect("Select your position(s)", ['QB', 'RB', 'WR', 'TE', 'DST'], key='pos_select')
                            elif sport_select == 'NHL':
                                pos_select = st.multiselect("Select your position(s)", ['W', 'C', 'D', 'G'], key='pos_select')
                            elif sport_select == 'MMA':
                                pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select')
                            elif sport_select == 'GOLF':
                                pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select')
                            elif sport_select == 'TENNIS':
                                pos_select = st.multiselect("Select your position(s)", ['All the same position', 'So', 'Yeah', 'Idk'], key='pos_select')
                        submitted = st.form_submit_button("Submit")
                        if submitted:
                            if pos_var == 'Specific':
                                pos_select = pos_select
                            else:
                                pos_select = None
                with player_comp_form_col:
                    with st.form(key='player_exp_comp_form'):
                        col1, col2 = st.columns(2)
                        with col1:
                            comp_player_var = st.selectbox("Would you like to compare with anyone?",  ['No', 'Yes'], key='comp_player_var')
                        with col2:
                            comp_player_select = st.multiselect("Select players to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_player_select')
                        submitted = st.form_submit_button("Submit")
                        if submitted:
                            if comp_player_var == 'No':
                                comp_player_select = None
                            else:
                                comp_player_select = comp_player_select
                if comp_player_var == 'Yes':
                    player_exp_comp = create_player_comparison(st.session_state['display_contest_info'], st.session_state['player_columns'], comp_player_select)
                    hold_frame = player_exp_comp.copy()
                    if sport_select == 'GOLF':
                        hold_frame['Pos'] = 'G'
                    else:
                        hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map'])
                    player_exp_comp.insert(1, 'Pos', hold_frame['Pos'])
                    player_exp_comp = player_exp_comp.dropna(subset=['Pos'])
                    if pos_select:
                        position_mask = player_exp_comp['Pos'].apply(lambda x: any(pos in x for pos in pos_select))
                        player_exp_comp = player_exp_comp[position_mask]
                    st.dataframe(player_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=player_exp_comp.select_dtypes(include=['number']).columns), hide_index=True)
                else:
                    if st.session_state['entry_parse_var'] == 'All':

                        st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'])
                        hold_frame = st.session_state['player_frame'].copy()
                        if sport_select == 'GOLF':
                            hold_frame['Pos'] = 'G'
                        else:
                            hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map'])
                        st.session_state['player_frame'].insert(1, 'Pos', hold_frame['Pos'])
                        st.session_state['player_frame'] = st.session_state['player_frame'].dropna(subset=['Pos'])
                        if pos_select:
                            position_mask = st.session_state['player_frame']['Pos'].apply(lambda x: any(pos in x for pos in pos_select))
                            st.session_state['player_frame'] = st.session_state['player_frame'][position_mask]
                        st.dataframe(st.session_state['player_frame'].
                            sort_values(by='Exposure Overall', ascending=False).
                            style.background_gradient(cmap='RdYlGn').
                            format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns),
                            hide_index=True)
                    else:
                        st.session_state['player_frame'] = create_player_exposures(st.session_state['display_contest_info'], st.session_state['player_columns'], st.session_state['entry_names'])
                        hold_frame = st.session_state['player_frame'].copy()
                        if sport_select == 'GOLF':
                            hold_frame['Pos'] = 'G'
                        else:
                            hold_frame['Pos'] = hold_frame['Player'].map(st.session_state['map_dict']['pos_map'])
                        st.session_state['player_frame'].insert(1, 'Pos', hold_frame['Pos'])
                        st.session_state['player_frame'] = st.session_state['player_frame'].dropna(subset=['Pos'])
                        if pos_select:
                            position_mask = st.session_state['player_frame']['Pos'].apply(lambda x: any(pos in x for pos in pos_select))
                            st.session_state['player_frame'] = st.session_state['player_frame'][position_mask]
                        st.dataframe(st.session_state['player_frame'].
                            sort_values(by='Exposure Overall', ascending=False).
                            style.background_gradient(cmap='RdYlGn').
                            format(formatter='{:.2%}', subset=st.session_state['player_frame'].iloc[:, 2:].select_dtypes(include=['number']).columns),
                            hide_index=True)
            with tab2:
                with st.form(key='stack_exp_comp_form'):
                    col1, col2 = st.columns(2)
                    with col1:
                        comp_stack_var = st.selectbox("Would you like to compare with anyone?",  ['No', 'Yes'], key='comp_stack_var')
                    with col2:
                        comp_stack_select = st.multiselect("Select stacks to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_stack_select')
                    submitted = st.form_submit_button("Submit")
                    if submitted:
                        if comp_stack_var == 'No':
                            comp_stack_select = None
                        else:
                            comp_stack_select = comp_stack_select
                if comp_stack_var == 'Yes':
                    stack_exp_comp = create_stack_comparison(st.session_state['display_contest_info'], comp_stack_select)
                    st.dataframe(stack_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=stack_exp_comp.select_dtypes(include=['number']).columns), hide_index=True)
                else:
                    if st.session_state['entry_parse_var'] == 'All':
                        st.session_state['stack_frame'] = create_stack_exposures(st.session_state['display_contest_info'])
                        st.dataframe(st.session_state['stack_frame'].
                            sort_values(by='Exposure Overall', ascending=False).
                            style.background_gradient(cmap='RdYlGn').
                            format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
                            hide_index=True)
                    else:
                        st.session_state['stack_frame'] = create_stack_exposures(st.session_state['display_contest_info'], st.session_state['entry_names'])
                        st.dataframe(st.session_state['stack_frame'].
                            sort_values(by='Exposure Overall', ascending=False).
                            style.background_gradient(cmap='RdYlGn').
                            format(formatter='{:.2%}', subset=st.session_state['stack_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
                            hide_index=True)
            with tab3:
                with st.form(key='size_exp_comp_form'):
                    col1, col2 = st.columns(2)
                    with col1:
                        comp_size_var = st.selectbox("Would you like to compare with anyone?",  ['No', 'Yes'], key='comp_size_var')
                    with col2:
                        comp_size_select = st.multiselect("Select sizes to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_size_select')
                    submitted = st.form_submit_button("Submit")
                    if submitted:
                        if comp_size_var == 'No':
                            comp_size_select = None
                        else:
                            comp_size_select = comp_size_select
                if comp_size_var == 'Yes':
                    size_exp_comp = create_size_comparison(st.session_state['display_contest_info'], comp_size_select)
                    st.dataframe(size_exp_comp.style.background_gradient(cmap='RdYlGn', axis=0).format(formatter='{:.2%}', subset=size_exp_comp.select_dtypes(include=['number']).columns), hide_index=True)
                else:
                    if st.session_state['entry_parse_var'] == 'All':
                        st.session_state['stack_size_frame'] = create_stack_size_exposures(st.session_state['display_contest_info'])
                        st.dataframe(st.session_state['stack_size_frame'].
                            sort_values(by='Exposure Overall', ascending=False).
                            style.background_gradient(cmap='RdYlGn').
                            format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
                            hide_index=True)
                    else:
                        st.session_state['stack_size_frame'] = create_stack_size_exposures(st.session_state['display_contest_info'], st.session_state['entry_names'])
                        st.dataframe(st.session_state['stack_size_frame'].
                            sort_values(by='Exposure Overall', ascending=False).
                            style.background_gradient(cmap='RdYlGn').
                            format(formatter='{:.2%}', subset=st.session_state['stack_size_frame'].iloc[:, 1:].select_dtypes(include=['number']).columns),
                            hide_index=True)
            
            with tab4:
                with st.form(key='general_comp_form'):
                    col1, col2 = st.columns(2)
                    with col1:
                        comp_general_var = st.selectbox("Would you like to compare with anyone?",  ['No', 'Yes'], key='comp_general_var')
                    with col2:
                        comp_general_select = st.multiselect("Select generals to compare with:", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='comp_general_select')
                    submitted = st.form_submit_button("Submit")
                    if submitted:
                        if comp_general_var == 'No':
                            comp_general_select = None
                        else:
                            comp_general_select = comp_general_select
                if comp_general_var == 'Yes':
                    general_comp = create_general_comparison(st.session_state['display_contest_info'], comp_general_select)
                    st.dataframe(general_comp.style.background_gradient(cmap='RdYlGn', axis=1).format(precision=2))
                else:
                    if st.session_state['entry_parse_var'] == 'All':
                        st.session_state['general_frame'] = create_general_exposures(st.session_state['display_contest_info'])
                        st.dataframe(st.session_state['general_frame'].style.background_gradient(cmap='RdYlGn', axis=1).format(precision=2), hide_index=True)
                        
                    else:
                        st.session_state['general_frame'] = create_general_exposures(st.session_state['display_contest_info'], st.session_state['entry_names'])
                        st.dataframe(st.session_state['general_frame'].style.background_gradient(cmap='RdYlGn', axis=1).format(precision=2), hide_index=True)

            with tab5:
                with st.form(key='dupe_form'):
                    col1, col2 = st.columns(2)
                    with col1:
                        user_dupe_var = st.selectbox("Which usage(s) would you like to view?",  ['All', 'Specific'], key='user_dupe_var')
                    with col2:
                        user_dupe_select = st.multiselect("Select your user(s)", st.session_state['display_contest_info']['BaseName'].sort_values().unique(), key='user_dupe_select')
                    submitted = st.form_submit_button("Submit")
                    if submitted:
                        if user_dupe_var == 'Specific':
                            user_dupe_select = user_dupe_select
                        else:
                            user_dupe_select = None
                if 'duplication_frame' not in st.session_state:
                    dupe_frame = st.session_state['display_contest_info'][['BaseName', 'EntryCount', 'dupes', 'uniques', 'under_5', 'under_10']]
                    dupe_frame['average_dupes'] = dupe_frame['dupes'].mean()
                    dupe_frame['uniques%'] = dupe_frame['uniques'] / dupe_frame['EntryCount']
                    dupe_frame['under_5%'] = dupe_frame['under_5'] / dupe_frame['EntryCount']
                    dupe_frame['under_10%'] = dupe_frame['under_10'] / dupe_frame['EntryCount']
                    dupe_frame = dupe_frame[['BaseName', 'EntryCount', 'average_dupes', 'uniques', 'uniques%', 'under_5', 'under_5%', 'under_10', 'under_10%']].drop_duplicates(subset='BaseName', keep='first')
                    st.session_state['duplication_frame'] = dupe_frame.sort_values(by='uniques%', ascending=False)
                if user_dupe_var == 'Specific':
                    st.session_state['duplication_frame'] = st.session_state['duplication_frame'][st.session_state['duplication_frame']['BaseName'].isin(user_dupe_select)]
                
                # Initialize pagination in session state if not exists
                if 'dupe_page' not in st.session_state:
                    st.session_state.dupe_page = 1

                # Calculate total pages
                rows_per_page = 50
                total_rows = len(st.session_state['duplication_frame'])
                total_pages = (total_rows + rows_per_page - 1) // rows_per_page

                # Create pagination controls in a single row
                pagination_cols = st.columns([4, 1, 1, 1, 4])
                with pagination_cols[1]:
                    if st.button(f"Previous Dupes Page"):
                        if st.session_state['dupe_page'] > 1:
                            st.session_state.dupe_page -= 1

                with pagination_cols[3]:
                    if st.button(f"Next Dupes Page"):
                        st.session_state.dupe_page += 1

                # Calculate start and end indices for current page
                start_dupe_idx = (st.session_state.dupe_page - 1) * rows_per_page
                end_dupe_idx = min((st.session_state.dupe_page) * rows_per_page, total_rows)

                st.dataframe(st.session_state['duplication_frame'].iloc[start_dupe_idx:end_dupe_idx].style.
                             background_gradient(cmap='RdYlGn', subset=['uniques%', 'under_5%', 'under_10%'], axis=0).
                             background_gradient(cmap='RdYlGn', subset=['uniques', 'under_5', 'under_10'], axis=0).
                             format(dupe_format, precision=2), hide_index=True)