File size: 60,914 Bytes
c1e71a4
 
 
 
 
2effe9f
c65f61f
c1e71a4
d1feee2
 
 
 
 
 
e0c3b64
0614531
99e9056
c1e71a4
d1feee2
ccc36fc
 
c1e71a4
36a4f29
69e4fc3
bd18063
e1747ca
69e4fc3
feb3149
 
69e4fc3
feb3149
 
 
e2f3e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0614531
00d1795
fd4e583
00d1795
 
feb3149
 
b0b9555
feb3149
5d94e8f
feb3149
 
69e4fc3
1b67fda
f32a335
5ae9ba3
f32a335
5ae9ba3
 
53e1672
 
 
 
5ae9ba3
f32a335
 
53e1672
 
 
 
 
e1747ca
f32a335
3a7ae3a
895faba
 
 
 
20e7035
 
895faba
 
 
 
1b67fda
 
 
 
 
70f0159
1b67fda
 
 
e1747ca
 
ccc36fc
e1747ca
69e4fc3
feb3149
69e4fc3
1b67fda
f08ff90
 
 
 
 
 
53e1672
 
f08ff90
 
 
 
 
 
 
 
 
 
 
 
e1747ca
 
 
 
ccc36fc
8e35500
 
 
 
 
 
 
 
eaa851e
8e35500
 
 
249be69
9af0cf7
8e35500
 
 
 
 
 
 
27e6d5b
8e35500
 
9af0cf7
 
 
 
 
 
 
 
 
 
8e35500
69e4fc3
 
36a4f29
 
0231bd5
 
2cb4bd9
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e43c7
 
 
 
 
 
 
 
0231bd5
10eb31b
 
0231bd5
60e43c7
 
 
 
 
 
 
 
 
 
 
 
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703c0bc
0231bd5
 
703c0bc
0231bd5
 
 
0562483
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703c0bc
0231bd5
 
 
 
 
 
 
 
 
00d1795
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703c0bc
0231bd5
 
 
edfc7a7
 
 
 
0231bd5
 
 
 
 
 
edfc7a7
 
0231bd5
 
edfc7a7
 
0231bd5
edfc7a7
 
 
 
0231bd5
 
edfc7a7
0231bd5
edfc7a7
 
0231bd5
edfc7a7
 
 
 
 
 
 
0231bd5
 
edfc7a7
 
 
 
 
 
 
 
 
 
 
 
 
0231bd5
703c0bc
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703c0bc
0231bd5
 
 
00d1795
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00d1795
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c65f61f
0231bd5
 
c65f61f
0231bd5
 
 
 
 
 
 
 
 
 
 
 
1807cfe
0231bd5
 
703c0bc
0231bd5
 
 
 
 
 
 
 
 
 
 
 
1807cfe
59c4c13
0231bd5
 
 
59c4c13
0231bd5
 
 
 
 
 
 
e3e18b5
0231bd5
 
 
 
 
 
 
 
 
703c0bc
0231bd5
 
1807cfe
59c4c13
0231bd5
 
 
59c4c13
0231bd5
 
 
 
 
 
 
c9b59bb
0231bd5
 
 
 
 
 
 
 
 
c9b59bb
0231bd5
 
c9b59bb
59c4c13
0231bd5
 
 
59c4c13
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00d1795
0231bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b40022b
0231bd5
 
 
 
 
 
 
 
 
 
 
36a4f29
0231bd5
 
 
 
 
 
 
 
 
 
36a4f29
 
7068b30
e1747ca
8b78599
15bba05
d3aba2f
b645a0a
 
10a7ad2
68d55bd
b645a0a
e4b8b07
60802c7
e4b8b07
9902a0c
 
bd18063
 
9902a0c
 
 
06422c3
9902a0c
 
 
 
 
43fe690
 
 
 
1efa340
43fe690
 
 
 
 
 
 
1efa340
43fe690
 
 
1efa340
43fe690
 
 
 
 
 
 
e4b8b07
 
bd18063
 
7fbfe19
 
 
06422c3
7fbfe19
 
 
e4b8b07
 
 
bd18063
 
 
37f8164
 
e4b8b07
 
bd18063
e4b8b07
 
 
 
 
 
bd18063
e4b8b07
 
 
 
 
 
bd18063
e4b8b07
 
 
 
bd18063
8ac7d89
 
0924d35
 
8ac7d89
 
 
 
 
 
 
 
 
c952d2d
cf5c56f
8ac7d89
 
161f2cc
 
40c567e
9054585
0924d35
9054585
 
59d0d43
0924d35
 
0ca6fd0
0924d35
0e52b5a
cf5c56f
9054585
 
 
 
 
 
 
8e17cb4
9054585
 
 
5fb9559
9054585
 
 
 
ddcaf58
d3aba2f
04ae2dd
ba359a4
04ae2dd
c952d2d
3852a15
 
 
439ae31
 
3852a15
439ae31
 
 
bad9e99
3852a15
 
bad9e99
3852a15
 
68a04df
bad9e99
3852a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2daa4c0
3852a15
 
 
bad9e99
3852a15
 
e184997
3852a15
 
97f924a
3852a15
 
 
 
 
 
 
439ae31
3852a15
97f924a
3852a15
 
e184997
3852a15
 
1f8d35a
 
 
bad9e99
3852a15
2dfa83b
 
e184997
8c43ae6
4d2caf3
 
 
 
 
 
 
 
 
 
 
 
68d55bd
 
 
 
 
 
 
 
4d2caf3
 
 
 
 
 
 
 
 
 
 
 
 
68d55bd
 
 
 
 
 
 
36a4f29
fcc49f0
 
 
 
e029672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc49f0
 
 
 
 
 
 
52b557b
fcc49f0
 
 
 
e029672
64951e0
fcc49f0
 
 
0bb8be3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
import streamlit as st
st.set_page_config(layout="wide")
import numpy as np
import pandas as pd
import time
from fuzzywuzzy import process
import random

## import global functions
from global_func.clean_player_name import clean_player_name
from global_func.load_file import load_file
from global_func.load_ss_file import load_ss_file
from global_func.find_name_mismatches import find_name_mismatches
from global_func.predict_dupes import predict_dupes
from global_func.highlight_rows import highlight_changes, highlight_changes_winners, highlight_changes_losers
from global_func.load_csv import load_csv
from global_func.find_csv_mismatches import find_csv_mismatches

freq_format = {'Finish_percentile': '{:.2%}', 'Lineup Edge': '{:.2%}', 'Win%': '{:.2%}'}
player_wrong_names_mlb = ['Enrique Hernandez']
player_right_names_mlb = ['Kike Hernandez']

tab1, tab2, tab3 = st.tabs(["Data Load", "Late Swap", "Manage Portfolio"])
with tab1:
    if st.button('Clear data', key='reset1'):
        st.session_state.clear()
    # Add file uploaders to your app
    col1, col2, col3 = st.columns(3)

    with col1:
        st.subheader("Draftkings/Fanduel CSV")
        st.info("Upload the player pricing CSV from the site you are playing on. This is used in late swap exporting and/or with SaberSim portfolios, but is not necessary for the portfolio management functions.")

        upload_csv_col, csv_template_col = st.columns([3, 1])
        with upload_csv_col:
            csv_file = st.file_uploader("Upload CSV File", type=['csv'])
            if 'csv_file' in st.session_state:
                del st.session_state['csv_file']
        with csv_template_col:

            csv_template_df = pd.DataFrame(columns=['Name', 'ID', 'Roster Position', 'Salary'])

            st.download_button(
                label="CSV Template",
                data=csv_template_df.to_csv(index=False),
                file_name="csv_template.csv",
                mime="text/csv"
            )
        st.session_state['csv_file'] = load_csv(csv_file)
        try:
            st.session_state['csv_file']['Salary'] = st.session_state['csv_file']['Salary'].astype(str).str.replace(',', '').astype(int)
        except:
            pass
            
        if csv_file:
            st.session_state['csv_file'] = st.session_state['csv_file'].drop_duplicates(subset=['Name'])
            st.success('Projections file loaded successfully!')
            st.dataframe(st.session_state['csv_file'].head(10))
    
    with col2:
        st.subheader("Portfolio File")
        st.info("Go ahead and upload a portfolio file here. Only include player columns and an optional 'Stack' column if you are playing MLB.")
        saber_toggle = st.radio("Are you uploading from SaberSim?", options=['No', 'Yes'])
        st.info("If you are uploading from SaberSim, you will need to upload a CSV file for the slate for name matching.")
        if saber_toggle == 'Yes':
            if csv_file is not None:
                portfolio_file = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
                if 'portfolio' in st.session_state:
                    del st.session_state['portfolio']
                if 'export_portfolio' in st.session_state:
                    del st.session_state['export_portfolio']

        else:
            portfolio_file = st.file_uploader("Upload Portfolio File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
            if 'portfolio' in st.session_state:
                del st.session_state['portfolio']
            if 'export_portfolio' in st.session_state:
                del st.session_state['export_portfolio']

        if portfolio_file:
            if saber_toggle == 'Yes':
                st.session_state['export_portfolio'], st.session_state['portfolio'] = load_ss_file(portfolio_file, st.session_state['csv_file'])
                st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all')
                st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True)
                st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all')
                st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True)
            else:
                st.session_state['export_portfolio'], st.session_state['portfolio'] = load_file(portfolio_file)
                st.session_state['export_portfolio'] = st.session_state['export_portfolio'].dropna(how='all')
                st.session_state['export_portfolio'] = st.session_state['export_portfolio'].reset_index(drop=True)
                st.session_state['portfolio'] = st.session_state['portfolio'].dropna(how='all')
                st.session_state['portfolio'] = st.session_state['portfolio'].reset_index(drop=True)
            # Check if Stack column exists in the portfolio
            if 'Stack' in st.session_state['portfolio'].columns:
                # Create dictionary mapping index to Stack values
                stack_dict = dict(zip(st.session_state['portfolio'].index, st.session_state['portfolio']['Stack']))
                st.write(f"Found {len(stack_dict)} stack assignments")
                st.session_state['portfolio'] = st.session_state['portfolio'].drop(columns=['Stack'])
            else:
                stack_dict = None
                st.info("No Stack column found in portfolio")
            if st.session_state['portfolio'] is not None:
                st.success('Portfolio file loaded successfully!')
                st.session_state['portfolio'] = st.session_state['portfolio'].apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb))
                st.dataframe(st.session_state['portfolio'].head(10))

    with col3:
        st.subheader("Projections File")
        st.info("upload a projections file that has 'player_names', 'salary', 'median', 'ownership', and 'captain ownership' (Needed for Showdown) columns. Note that the salary for showdown needs to be the FLEX salary, not the captain salary.")
        
        # Create two columns for the uploader and template button
        upload_col, template_col = st.columns([3, 1])
        
        with upload_col:
            projections_file = st.file_uploader("Upload Projections File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
            if 'projections_df' in st.session_state:
                del st.session_state['projections_df']
        
        with template_col:
            # Create empty DataFrame with required columns
            template_df = pd.DataFrame(columns=['player_names', 'position', 'team', 'salary', 'median', 'ownership', 'captain ownership'])
            # Add download button for template
            st.download_button(
                label="Template",
                data=template_df.to_csv(index=False),
                file_name="projections_template.csv",
                mime="text/csv"
            )
            
        if projections_file:
            export_projections, projections = load_file(projections_file)
            if projections is not None:
                st.success('Projections file loaded successfully!')
                projections = projections.apply(lambda x: x.replace(player_wrong_names_mlb, player_right_names_mlb))
                st.dataframe(projections.head(10))

    if portfolio_file and projections_file:
        if st.session_state['portfolio'] is not None and projections is not None:
            st.subheader("Name Matching Analysis")
            # Initialize projections_df in session state if it doesn't exist
            if 'projections_df' not in st.session_state:
                st.session_state['projections_df'] = projections.copy()
                st.session_state['projections_df']['salary'] = (st.session_state['projections_df']['salary'].astype(str).str.replace(',', '').astype(float).astype(int))
            
            # Update projections_df with any new matches
            st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
            if csv_file is not None and 'export_dict' not in st.session_state:
                    # Create a dictionary of Name to Name+ID from csv_file
                    try:
                        name_id_map = dict(zip(
                            st.session_state['csv_file']['Name'], 
                            st.session_state['csv_file']['Name + ID']
                        ))
                    except:
                        name_id_map = dict(zip(
                            st.session_state['csv_file']['Nickname'], 
                            st.session_state['csv_file']['Id']
                        ))
                    
                    # Function to find best match
                    def find_best_match(name):
                        best_match = process.extractOne(name, name_id_map.keys())
                        if best_match and best_match[1] >= 85:  # 85% match threshold
                            return name_id_map[best_match[0]]
                        return name  # Return original name if no good match found
                    
                    # Apply the matching
                    projections['upload_match'] = projections['player_names'].apply(find_best_match)
                    st.session_state['export_dict'] = dict(zip(projections['player_names'], projections['upload_match']))

with tab2:
    if st.button('Clear data', key='reset2'):
        st.session_state.clear()
    
    if 'portfolio' in st.session_state and 'projections_df' in st.session_state:

        optimized_df = None

        map_dict = {
                    'pos_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                    st.session_state['projections_df']['position'])),
                    'salary_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                        st.session_state['projections_df']['salary'])),
                    'proj_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                    st.session_state['projections_df']['median'])),
                    'own_map': dict(zip(st.session_state['projections_df']['player_names'],
                                    st.session_state['projections_df']['ownership'])),
                    'team_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                        st.session_state['projections_df']['team']))
                }
        # Calculate new stats for optimized lineups
        st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(
            lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row if player in map_dict['salary_map']), axis=1
        )
        st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(
            lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row if player in map_dict['proj_map']), axis=1
        )

        st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(
            lambda row: sum(map_dict['own_map'].get(player, 0) for player in row if player in map_dict['own_map']), axis=1
        )

        options_container = st.container()
        with options_container:
            col1, col2, col3, col4, col5, col6 = st.columns(6)
            with col1:
                curr_site_var = st.selectbox("Select your current site", options=['DraftKings', 'FanDuel'])
            with col2:
                curr_sport_var = st.selectbox("Select your current sport", options=['NBA', 'MLB', 'NFL', 'NHL', 'MMA'])
            with col3:
                swap_var = st.multiselect("Select late swap strategy", options=['Optimize', 'Increase volatility', 'Decrease volatility'])
            with col4:
                remove_teams_var = st.multiselect("What teams have already played?", options=st.session_state['projections_df']['team'].unique())
            with col5:
                winners_var = st.multiselect("Are there any players doing exceptionally well?", options=st.session_state['projections_df']['player_names'].unique(), max_selections=3)
            with col6:
                losers_var = st.multiselect("Are there any players doing exceptionally poorly?", options=st.session_state['projections_df']['player_names'].unique(), max_selections=3)
        if st.button('Clear Late Swap'):
            if 'optimized_df' in st.session_state:
                del st.session_state['optimized_df']

            map_dict = {
                        'pos_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                        st.session_state['projections_df']['position'])),
                        'salary_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                            st.session_state['projections_df']['salary'])),
                        'proj_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                        st.session_state['projections_df']['median'])),
                        'own_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                        st.session_state['projections_df']['ownership'])),
                        'team_map': dict(zip(st.session_state['projections_df']['player_names'], 
                                        st.session_state['projections_df']['team']))
                    }
            # Calculate new stats for optimized lineups
            st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(
                lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row if player in map_dict['salary_map']), axis=1
            )
            st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(
                lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row if player in map_dict['proj_map']), axis=1
            )
            st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(
                lambda row: sum(map_dict['own_map'].get(player, 0) for player in row if player in map_dict['own_map']), axis=1
            )

        if st.button('Run Late Swap'):
            st.session_state['portfolio'] = st.session_state['portfolio'].drop(columns=['salary', 'median', 'Own'])
            if curr_sport_var == 'NBA':
                if curr_site_var == 'DraftKings':
                    st.session_state['portfolio'] = st.session_state['portfolio'].set_axis(['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'UTIL'], axis=1)
                else:
                    st.session_state['portfolio'] = st.session_state['portfolio'].set_axis(['PG', 'PG', 'SG', 'SG', 'SF', 'SF', 'PF', 'PF', 'C'], axis=1)
            
            # Define roster position rules
            if curr_site_var == 'DraftKings':
                position_rules = {
                    'PG': ['PG'],
                    'SG': ['SG'],
                    'SF': ['SF'],
                    'PF': ['PF'],
                    'C': ['C'],
                    'G': ['PG', 'SG'],
                    'F': ['SF', 'PF'],
                    'UTIL': ['PG', 'SG', 'SF', 'PF', 'C']
                    }
            else:
                position_rules = {
                    'PG': ['PG'],
                    'SG': ['SG'],
                    'SF': ['SF'],
                    'PF': ['PF'],
                    'C': ['C'],
                }
            # Create position groups from projections data
            position_groups = {}
            for _, player in st.session_state['projections_df'].iterrows():
                positions = player['position'].split('/')
                for pos in positions:
                    if pos not in position_groups:
                        position_groups[pos] = []
                    position_groups[pos].append({
                        'player_names': player['player_names'],
                        'salary': player['salary'],
                        'median': player['median'],
                        'ownership': player['ownership'],
                        'positions': positions  # Store all eligible positions
                    })

            def optimize_lineup(row):
                current_lineup = []
                total_salary = 0
                if curr_site_var == 'DraftKings':
                    salary_cap = 50000
                else:
                    salary_cap = 60000
                used_players = set()

                # Convert row to dictionary with roster positions
                roster = {}
                for col, player in zip(row.index, row):
                    if col not in ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Lineup Edge']:
                        roster[col] = {
                            'name': player,
                            'position': map_dict['pos_map'].get(player, '').split('/'),
                            'team': map_dict['team_map'].get(player, ''),
                            'salary': map_dict['salary_map'].get(player, 0),
                            'median': map_dict['proj_map'].get(player, 0),
                            'ownership': map_dict['own_map'].get(player, 0)
                        }
                        total_salary += roster[col]['salary']
                        used_players.add(player)

                # Optimize each roster position in random order
                roster_positions = list(roster.items())
                random.shuffle(roster_positions)
                
                for roster_pos, current in roster_positions:
                    # Skip optimization for players from removed teams
                    if current['team'] in remove_teams_var:
                        continue
                    
                    valid_positions = position_rules[roster_pos]
                    better_options = []

                    # Find valid replacements for this roster position
                    for pos in valid_positions:
                        if pos in position_groups:
                            pos_options = [
                                p for p in position_groups[pos]
                                if p['median'] > current['median']
                                and (total_salary - current['salary'] + p['salary']) <= salary_cap
                                and p['player_names'] not in used_players
                                and any(valid_pos in p['positions'] for valid_pos in valid_positions)
                                and map_dict['team_map'].get(p['player_names']) not in remove_teams_var  # Check team restriction
                            ]
                            better_options.extend(pos_options)

                    if better_options:
                        # Remove duplicates
                        better_options = {opt['player_names']: opt for opt in better_options}.values()
                        
                        # Sort by median projection and take the best one
                        best_replacement = max(better_options, key=lambda x: x['median'])
                        
                        # Update the lineup and tracking variables
                        used_players.remove(current['name'])
                        used_players.add(best_replacement['player_names'])
                        total_salary = total_salary - current['salary'] + best_replacement['salary']
                        roster[roster_pos] = {
                            'name': best_replacement['player_names'],
                            'position': map_dict['pos_map'][best_replacement['player_names']].split('/'),
                            'team': map_dict['team_map'][best_replacement['player_names']],
                            'salary': best_replacement['salary'],
                            'median': best_replacement['median'],
                            'ownership': best_replacement['ownership']
                        }

                # Return optimized lineup maintaining original column order
                return [roster[pos]['name'] for pos in row.index if pos in roster]

            def optimize_lineup_winners(row):
                current_lineup = []
                total_salary = 0
                if curr_site_var == 'DraftKings':
                    salary_cap = 50000
                else:
                    salary_cap = 60000
                used_players = set()

                # Check if any winners are in the lineup and count them
                winners_in_lineup = sum(1 for player in row if player in winners_var)
                changes_needed = min(winners_in_lineup, 3) if winners_in_lineup > 0 else 0
                changes_made = 0

                # Convert row to dictionary with roster positions
                roster = {}
                for col, player in zip(row.index, row):
                    if col not in ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Lineup Edge']:
                        roster[col] = {
                            'name': player,
                            'position': map_dict['pos_map'].get(player, '').split('/'),
                            'team': map_dict['team_map'].get(player, ''),
                            'salary': map_dict['salary_map'].get(player, 0),
                            'median': map_dict['proj_map'].get(player, 0),
                            'ownership': map_dict['own_map'].get(player, 0)
                        }
                        total_salary += roster[col]['salary']
                        used_players.add(player)

                # Only proceed with ownership-based optimization if we have winners in the lineup
                if changes_needed > 0:
                    # Randomize the order of positions to optimize
                    roster_positions = list(roster.items())
                    random.shuffle(roster_positions)
                    
                    for roster_pos, current in roster_positions:
                        # Stop if we've made enough changes
                        if changes_made >= changes_needed:
                            break
                            
                        # Skip optimization for players from removed teams or if the current player is a winner
                        if current['team'] in remove_teams_var or current['name'] in winners_var:
                            continue
                        
                        valid_positions = list(position_rules[roster_pos])
                        random.shuffle(valid_positions)
                        better_options = []

                        # Find valid replacements with higher ownership
                        for pos in valid_positions:
                            if pos in position_groups:
                                pos_options = [
                                    p for p in position_groups[pos]
                                    if p['ownership'] > current['ownership']
                                    and p['median'] >= current['median'] - 3
                                    and (total_salary - current['salary'] + p['salary']) <= salary_cap
                                    and (total_salary - current['salary'] + p['salary']) >= salary_cap - 1000
                                    and p['player_names'] not in used_players
                                    and any(valid_pos in p['positions'] for valid_pos in valid_positions)
                                    and map_dict['team_map'].get(p['player_names']) not in remove_teams_var
                                ]
                                better_options.extend(pos_options)

                        if better_options:
                            # Remove duplicates
                            better_options = {opt['player_names']: opt for opt in better_options}.values()
                            
                            # Sort by ownership and take the highest owned option
                            best_replacement = max(better_options, key=lambda x: x['ownership'])
                            
                            # Update the lineup and tracking variables
                            used_players.remove(current['name'])
                            used_players.add(best_replacement['player_names'])
                            total_salary = total_salary - current['salary'] + best_replacement['salary']
                            roster[roster_pos] = {
                                'name': best_replacement['player_names'],
                                'position': map_dict['pos_map'][best_replacement['player_names']].split('/'),
                                'team': map_dict['team_map'][best_replacement['player_names']],
                                'salary': best_replacement['salary'],
                                'median': best_replacement['median'],
                                'ownership': best_replacement['ownership']
                            }
                            changes_made += 1

                # Return optimized lineup maintaining original column order
                return [roster[pos]['name'] for pos in row.index if pos in roster]
            
            def optimize_lineup_losers(row):
                current_lineup = []
                total_salary = 0
                if curr_site_var == 'DraftKings':
                    salary_cap = 50000
                else:
                    salary_cap = 60000
                used_players = set()

                # Check if any winners are in the lineup and count them
                losers_in_lineup = sum(1 for player in row if player in losers_var)
                changes_needed = min(losers_in_lineup, 3) if losers_in_lineup > 0 else 0
                changes_made = 0

                # Convert row to dictionary with roster positions
                roster = {}
                for col, player in zip(row.index, row):
                    if col not in ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Lineup Edge']:
                        roster[col] = {
                            'name': player,
                            'position': map_dict['pos_map'].get(player, '').split('/'),
                            'team': map_dict['team_map'].get(player, ''),
                            'salary': map_dict['salary_map'].get(player, 0),
                            'median': map_dict['proj_map'].get(player, 0),
                            'ownership': map_dict['own_map'].get(player, 0)
                        }
                        total_salary += roster[col]['salary']
                        used_players.add(player)

                # Only proceed with ownership-based optimization if we have winners in the lineup
                if changes_needed > 0:
                    # Randomize the order of positions to optimize
                    roster_positions = list(roster.items())
                    random.shuffle(roster_positions)
                    
                    for roster_pos, current in roster_positions:
                        # Stop if we've made enough changes
                        if changes_made >= changes_needed:
                            break
                            
                        # Skip optimization for players from removed teams or if the current player is a winner
                        if current['team'] in remove_teams_var or current['name'] in losers_var:
                            continue
                        
                        valid_positions = list(position_rules[roster_pos])
                        random.shuffle(valid_positions)
                        better_options = []

                        # Find valid replacements with higher ownership
                        for pos in valid_positions:
                            if pos in position_groups:
                                pos_options = [
                                    p for p in position_groups[pos]
                                    if p['ownership'] < current['ownership']
                                    and p['median'] >= current['median'] - 3
                                    and (total_salary - current['salary'] + p['salary']) <= salary_cap
                                    and (total_salary - current['salary'] + p['salary']) >= salary_cap - 1000
                                    and p['player_names'] not in used_players
                                    and any(valid_pos in p['positions'] for valid_pos in valid_positions)
                                    and map_dict['team_map'].get(p['player_names']) not in remove_teams_var
                                ]
                                better_options.extend(pos_options)

                        if better_options:
                            # Remove duplicates
                            better_options = {opt['player_names']: opt for opt in better_options}.values()
                            
                            # Sort by ownership and take the highest owned option
                            best_replacement = max(better_options, key=lambda x: x['ownership'])
                            
                            # Update the lineup and tracking variables
                            used_players.remove(current['name'])
                            used_players.add(best_replacement['player_names'])
                            total_salary = total_salary - current['salary'] + best_replacement['salary']
                            roster[roster_pos] = {
                                'name': best_replacement['player_names'],
                                'position': map_dict['pos_map'][best_replacement['player_names']].split('/'),
                                'team': map_dict['team_map'][best_replacement['player_names']],
                                'salary': best_replacement['salary'],
                                'median': best_replacement['median'],
                                'ownership': best_replacement['ownership']
                            }
                            changes_made += 1

                # Return optimized lineup maintaining original column order
                return [roster[pos]['name'] for pos in row.index if pos in roster]

            # Create a progress bar
            progress_bar = st.progress(0)
            status_text = st.empty()
            
            # Process each lineup
            optimized_lineups = []
            total_lineups = len(st.session_state['portfolio'])
            
            for idx, row in st.session_state['portfolio'].iterrows():
                # First optimization pass
                first_pass = optimize_lineup(row)
                first_pass_series = pd.Series(first_pass, index=row.index)

                second_pass = optimize_lineup(first_pass_series)
                second_pass_series = pd.Series(second_pass, index=row.index)

                third_pass = optimize_lineup(second_pass_series)
                third_pass_series = pd.Series(third_pass, index=row.index)

                fourth_pass = optimize_lineup(third_pass_series)
                fourth_pass_series = pd.Series(fourth_pass, index=row.index)

                fifth_pass = optimize_lineup(fourth_pass_series)
                fifth_pass_series = pd.Series(fifth_pass, index=row.index)
                
                # Second optimization pass
                final_lineup = optimize_lineup(fifth_pass_series)
                optimized_lineups.append(final_lineup)
                
                if 'Optimize' in swap_var:
                    progress = (idx + 1) / total_lineups
                    progress_bar.progress(progress)
                    status_text.text(f'Optimizing Lineups {idx + 1} of {total_lineups}')
                else:
                    pass
            
            # Create new dataframe with optimized lineups
            if 'Optimize' in swap_var:
                st.session_state['optimized_df_medians'] = pd.DataFrame(optimized_lineups, columns=st.session_state['portfolio'].columns)
            else:
                st.session_state['optimized_df_medians'] = st.session_state['portfolio']

            # Create a progress bar
            progress_bar_winners = st.progress(0)
            status_text_winners = st.empty()
            
            # Process each lineup
            optimized_lineups_winners = []
            total_lineups = len(st.session_state['optimized_df_medians'])
            
            for idx, row in st.session_state['optimized_df_medians'].iterrows():

                final_lineup = optimize_lineup_winners(row)
                optimized_lineups_winners.append(final_lineup)
                
                if 'Decrease volatility' in swap_var:
                    progress_winners = (idx + 1) / total_lineups
                    progress_bar_winners.progress(progress_winners)
                    status_text_winners.text(f'Lowering Volatility around Winners {idx + 1} of {total_lineups}')
                else:
                    pass
            
            # Create new dataframe with optimized lineups
            if 'Decrease volatility' in swap_var:
                st.session_state['optimized_df_winners'] = pd.DataFrame(optimized_lineups_winners, columns=st.session_state['optimized_df_medians'].columns)
            else:
                st.session_state['optimized_df_winners'] = st.session_state['optimized_df_medians']

            # Create a progress bar
            progress_bar_losers = st.progress(0)
            status_text_losers = st.empty()
            
            # Process each lineup
            optimized_lineups_losers = []
            total_lineups = len(st.session_state['optimized_df_winners'])
            
            for idx, row in st.session_state['optimized_df_winners'].iterrows():

                final_lineup = optimize_lineup_losers(row)
                optimized_lineups_losers.append(final_lineup)
                
                if 'Increase volatility' in swap_var:
                    progress_losers = (idx + 1) / total_lineups
                    progress_bar_losers.progress(progress_losers)
                    status_text_losers.text(f'Increasing Volatility around Losers {idx + 1} of {total_lineups}')
                else:
                    pass
            
            # Create new dataframe with optimized lineups
            if 'Increase volatility' in swap_var:
                st.session_state['optimized_df'] = pd.DataFrame(optimized_lineups_losers, columns=st.session_state['optimized_df_winners'].columns)
            else:
                st.session_state['optimized_df'] = st.session_state['optimized_df_winners']
            
            # Calculate new stats for optimized lineups
            st.session_state['optimized_df']['salary'] = st.session_state['optimized_df'].apply(
                lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row if player in map_dict['salary_map']), axis=1
            )
            st.session_state['optimized_df']['median'] = st.session_state['optimized_df'].apply(
                lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row if player in map_dict['proj_map']), axis=1
            )
            st.session_state['optimized_df']['Own'] = st.session_state['optimized_df'].apply(
                lambda row: sum(map_dict['own_map'].get(player, 0) for player in row if player in map_dict['own_map']), axis=1
            )

            # Display results
            st.success('Optimization complete!')

        if 'optimized_df' in st.session_state:
            st.write("Increase in median highlighted in yellow, descrease in volatility highlighted in blue, increase in volatility highlighted in red:")
            st.dataframe(
                st.session_state['optimized_df'].style
                .apply(highlight_changes, axis=1)
                .apply(highlight_changes_winners, axis=1)
                .apply(highlight_changes_losers, axis=1)
                .background_gradient(axis=0)
                .background_gradient(cmap='RdYlGn')
                .format(precision=2),
                height=1000,
                use_container_width=True
            )
            
            # Option to download optimized lineups
            if st.button('Prepare Late Swap Export'):
                export_df = st.session_state['optimized_df'].copy()
                
                # Map player names to their export IDs for all player columns
                for col in export_df.columns:
                    if col not in ['salary', 'median', 'Own']:
                        export_df[col] = export_df[col].map(st.session_state['export_dict'])
                
                csv = export_df.to_csv(index=False)
                st.download_button(
                    label="Download CSV",
                    data=csv,
                    file_name="optimized_lineups.csv",
                    mime="text/csv"
                )
        else:
            st.write("Current Portfolio")
            st.dataframe(
                st.session_state['portfolio'].style
                .background_gradient(axis=0)
                .background_gradient(cmap='RdYlGn')
                .format(precision=2),
                height=1000,
                use_container_width=True
            )

with tab3:
    if st.button('Clear data', key='reset3'):
        st.session_state.clear()
    if 'portfolio' in st.session_state and 'projections_df' in st.session_state:
        col1, col2, col3 = st.columns([1, 8, 1])
        excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Win%', 'Lineup Edge']
        with col1:
            site_var = st.selectbox("Select Site", ['Draftkings', 'Fanduel'])
            sport_var = st.selectbox("Select Sport", ['NFL', 'MLB', 'NBA', 'NHL', 'MMA'])
            st.info("It currently does not matter what sport you select, it may matter in the future.")
            type_var = st.selectbox("Select Game Type", ['Classic', 'Showdown'])
            Contest_Size = st.number_input("Enter Contest Size", value=25000, min_value=1, step=1)
            strength_var = st.selectbox("Select field strength", ['Average', 'Sharp', 'Weak'])
        if site_var == 'Draftkings':
            if type_var == 'Classic':
                map_dict = {
                    'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
                    'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
                    'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
                    'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
                    'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
                    'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
                    'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
                    'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
                    'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
                }
            elif type_var == 'Showdown':
                if sport_var == 'NFL':
                    map_dict = {
                        'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
                        'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
                        'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
                        'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
                        'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
                        'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
                        'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'] * 1.5)),
                        'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
                        'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
                    }
                elif sport_var != 'NFL':
                    map_dict = {
                        'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
                        'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
                        'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'] / 1.5)),
                        'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
                        'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
                        'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
                        'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
                        'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
                        'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
                    }
        elif site_var == 'Fanduel':
            map_dict = {
                'pos_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['position'])),
                'team_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['team'])),
                'salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
                'proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'])),
                'own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'])),
                'own_percent_rank':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['ownership'].rank(pct=True))),
                'cpt_salary_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['salary'])),
                'cpt_proj_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['median'] * 1.5)),
                'cpt_own_map':dict(zip(st.session_state['projections_df']['player_names'], st.session_state['projections_df']['captain ownership']))
            }
        
        if type_var == 'Classic':
            st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['salary_map'].get(player, 0) for player in row), axis=1)
            st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['proj_map'].get(player, 0) for player in row), axis=1)
            st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(lambda row: sum(map_dict['own_map'].get(player, 0) for player in row), axis=1)
            if stack_dict is not None:
                st.session_state['portfolio']['Stack'] = st.session_state['portfolio'].index.map(stack_dict)
        elif type_var == 'Showdown':
            # Calculate salary (CPT uses cpt_salary_map, others use salary_map)
            st.session_state['portfolio']['salary'] = st.session_state['portfolio'].apply(
                lambda row: map_dict['cpt_salary_map'].get(row.iloc[0], 0) + 
                          sum(map_dict['salary_map'].get(player, 0) for player in row.iloc[1:]),
                axis=1
            )
            
            # Calculate median (CPT uses cpt_proj_map, others use proj_map)
            st.session_state['portfolio']['median'] = st.session_state['portfolio'].apply(
                lambda row: map_dict['cpt_proj_map'].get(row.iloc[0], 0) + 
                          sum(map_dict['proj_map'].get(player, 0) for player in row.iloc[1:]),
                axis=1
            )
            
            # Calculate ownership (CPT uses cpt_own_map, others use own_map)
            st.session_state['portfolio']['Own'] = st.session_state['portfolio'].apply(
                lambda row: map_dict['cpt_own_map'].get(row.iloc[0], 0) + 
                          sum(map_dict['own_map'].get(player, 0) for player in row.iloc[1:]),
                axis=1
            )
        with col3:
            with st.form(key='filter_form'):
                max_dupes = st.number_input("Max acceptable dupes?", value=1000, min_value=1, step=1)
                min_salary = st.number_input("Min acceptable salary?", value=1000, min_value=1000, step=100)
                max_salary = st.number_input("Max acceptable salary?", value=60000, min_value=1000, step=100)
                max_finish_percentile = st.number_input("Max acceptable finish percentile?", value=.50, min_value=0.005, step=.001)
                player_names = set()
                for col in st.session_state['portfolio'].columns:
                    if col not in excluded_cols:
                        player_names.update(st.session_state['portfolio'][col].unique())
                player_lock = st.multiselect("Lock players?", options=sorted(list(player_names)), default=[])
                player_remove = st.multiselect("Remove players?", options=sorted(list(player_names)), default=[])
                if stack_dict is not None:
                    stack_toggle = st.selectbox("Include specific stacks?", options=['All Stacks', 'Specific Stacks'], index=0)
                    stack_selections = st.multiselect("If Specific Stacks, Which to include?", options=sorted(list(set(stack_dict.values()))), default=[])
                    stack_remove = st.multiselect("If Specific Stacks, Which to remove?", options=sorted(list(set(stack_dict.values()))), default=[])
                
                submitted = st.form_submit_button("Submit")

        with col2:
            st.session_state['portfolio'] = predict_dupes(st.session_state['portfolio'], map_dict, site_var, type_var, Contest_Size, strength_var)
            st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['Dupes'] <= max_dupes]
            st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['salary'] >= min_salary]
            st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['salary'] <= max_salary]
            st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['Finish_percentile'] <= max_finish_percentile]
            if stack_dict is not None:
                if stack_toggle == 'All Stacks':
                    st.session_state['portfolio'] = st.session_state['portfolio']
                    st.session_state['portfolio'] = st.session_state['portfolio'][~st.session_state['portfolio']['Stack'].isin(stack_remove)]
                else:
                    st.session_state['portfolio'] = st.session_state['portfolio'][st.session_state['portfolio']['Stack'].isin(stack_selections)]
                    st.session_state['portfolio'] = st.session_state['portfolio'][~st.session_state['portfolio']['Stack'].isin(stack_remove)]
            if player_remove:
                # Create mask for lineups that contain any of the removed players
                player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols]
                remove_mask = st.session_state['portfolio'][player_columns].apply(
                    lambda row: not any(player in list(row) for player in player_remove), axis=1
                )
                st.session_state['portfolio'] = st.session_state['portfolio'][remove_mask]
            
            if player_lock:
                # Create mask for lineups that contain all locked players
                player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols]
                
                lock_mask = st.session_state['portfolio'][player_columns].apply(
                    lambda row: all(player in list(row) for player in player_lock), axis=1
                )
                st.session_state['portfolio'] = st.session_state['portfolio'][lock_mask]
            export_file = st.session_state['portfolio'].copy()
            st.session_state['portfolio'] = st.session_state['portfolio'].sort_values(by='median', ascending=False)
            if csv_file is not None:
                player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols]
                for col in player_columns:
                    export_file[col] = export_file[col].map(st.session_state['export_dict'])
            with st.expander("Download options"):
                if stack_dict is not None:
                    with st.form(key='stack_form'):
                        st.subheader("Stack Count Adjustments")
                        st.info("This allows you to fine tune the stacks that you wish to export. If you want to make sure you don't export any of a specific stack you can 0 it out.")
                        # Create a container for stack value inputs
                        sort_container = st.container()
                        with sort_container:
                            sort_var = st.selectbox("Sort export portfolio by:", options=['median', 'Lineup Edge', 'Own'])
                        
                        # Get unique stack values
                        unique_stacks = sorted(list(set(stack_dict.values())))
                        
                        # Create a dictionary to store stack multipliers
                        if 'stack_multipliers' not in st.session_state:
                            st.session_state.stack_multipliers = {stack: 0.0 for stack in unique_stacks}
                        
                        # Create columns for the stack inputs
                        num_cols = 6  # Number of columns to display
                        for i in range(0, len(unique_stacks), num_cols):
                            cols = st.columns(num_cols)
                            for j, stack in enumerate(unique_stacks[i:i+num_cols]):
                                with cols[j]:
                                    # Create a unique key for each number input
                                    key = f"stack_count_{stack}"
                                    # Get the current count of this stack in the portfolio
                                    current_stack_count = len(st.session_state['portfolio'][st.session_state['portfolio']['Stack'] == stack])
                                    # Create number input with current value and max value based on actual count
                                    st.session_state.stack_multipliers[stack] = st.number_input(
                                        f"{stack} count",
                                        min_value=0.0,
                                        max_value=float(current_stack_count),
                                        value=0.0,
                                        step=1.0,
                                        key=key
                                    )
                        
                        # Create a copy of the portfolio
                        portfolio_copy = st.session_state['portfolio'].copy()
                        
                        # Create a list to store selected rows
                        selected_rows = []
                        
                        # For each stack, select the top N rows based on the count value
                        for stack in unique_stacks:
                            if stack in st.session_state.stack_multipliers:
                                count = int(st.session_state.stack_multipliers[stack])
                                # Get rows for this stack
                                stack_rows = portfolio_copy[portfolio_copy['Stack'] == stack]
                                # Sort by median and take top N rows
                                top_rows = stack_rows.nlargest(count, sort_var)
                                selected_rows.append(top_rows)
                        
                        # Combine all selected rows
                        portfolio_copy = pd.concat(selected_rows)
                        
                        # Update export_file with filtered data
                        export_file = portfolio_copy.copy()
                        for col in export_file.columns:
                            if col not in excluded_cols:
                                export_file[col] = export_file[col].map(st.session_state['export_dict'])
                        
                        submitted = st.form_submit_button("Submit")
                        if submitted:
                            st.write('Export portfolio updated!')
                
            st.download_button(label="Download Portfolio", data=export_file.to_csv(index=False), file_name="portfolio.csv", mime="text/csv")
            # Display the paginated dataframe first
            st.dataframe(
                st.session_state['portfolio'].style
                .background_gradient(axis=0)
                .background_gradient(cmap='RdYlGn')
                .background_gradient(cmap='RdYlGn_r', subset=['Finish_percentile', 'Own', 'Dupes'])
                .format(freq_format, precision=2), 
                height=1000,
                use_container_width=True
            )

            # Add pagination controls below the dataframe
            total_rows = len(st.session_state['portfolio'])
            rows_per_page = 500
            total_pages = (total_rows + rows_per_page - 1) // rows_per_page  # Ceiling division

            # Initialize page number in session state if not exists
            if 'current_page' not in st.session_state:
                st.session_state.current_page = 1

            # Display current page range info and pagination control in a single line
            st.write(
                f"Showing rows {(st.session_state.current_page - 1) * rows_per_page + 1} "
                f"to {min(st.session_state.current_page * rows_per_page, total_rows)} of {total_rows}"
            )
            
            # Add page number input
            st.session_state.current_page = st.number_input(
                f"Page (1-{total_pages})", 
                min_value=1, 
                max_value=total_pages,
                value=st.session_state.current_page
            )

            # Calculate start and end indices for current page
            start_idx = (st.session_state.current_page - 1) * rows_per_page
            end_idx = min(start_idx + rows_per_page, total_rows)

            # Get the subset of data for the current page
            current_page_data = st.session_state['portfolio'].iloc[start_idx:end_idx]
            
            # Create player summary dataframe
            player_stats = []
            player_columns = [col for col in st.session_state['portfolio'].columns if col not in excluded_cols]
            
            if type_var == 'Showdown':
                # Handle Captain positions
                for player in player_names:
                    # Create mask for lineups where this player is Captain (first column)
                    cpt_mask = st.session_state['portfolio'][player_columns[0]] == player
                    
                    if cpt_mask.any():
                        player_stats.append({
                            'Player': f"{player} (CPT)",
                            'Lineup Count': cpt_mask.sum(),
                            'Avg Median': st.session_state['portfolio'][cpt_mask]['median'].mean(),
                            'Avg Own': st.session_state['portfolio'][cpt_mask]['Own'].mean(),
                            'Avg Dupes': st.session_state['portfolio'][cpt_mask]['Dupes'].mean(),
                            'Avg Finish %': st.session_state['portfolio'][cpt_mask]['Finish_percentile'].mean(),
                            'Avg Lineup Edge': st.session_state['portfolio'][cpt_mask]['Lineup Edge'].mean(),
                        })
                    
                    # Create mask for lineups where this player is FLEX (other columns)
                    flex_mask = st.session_state['portfolio'][player_columns[1:]].apply(
                        lambda row: player in list(row), axis=1
                    )
                    
                    if flex_mask.any():
                        player_stats.append({
                            'Player': f"{player} (FLEX)",
                            'Lineup Count': flex_mask.sum(),
                            'Avg Median': st.session_state['portfolio'][flex_mask]['median'].mean(),
                            'Avg Own': st.session_state['portfolio'][flex_mask]['Own'].mean(),
                            'Avg Dupes': st.session_state['portfolio'][flex_mask]['Dupes'].mean(),
                            'Avg Finish %': st.session_state['portfolio'][flex_mask]['Finish_percentile'].mean(),
                            'Avg Lineup Edge': st.session_state['portfolio'][flex_mask]['Lineup Edge'].mean(),
                        })
            else:
                # Original Classic format processing
                for player in player_names:
                    player_mask = st.session_state['portfolio'][player_columns].apply(
                        lambda row: player in list(row), axis=1
                    )
                    
                    if player_mask.any():
                        player_stats.append({
                            'Player': player,
                            'Lineup Count': player_mask.sum(),
                            'Avg Median': st.session_state['portfolio'][player_mask]['median'].mean(),
                            'Avg Own': st.session_state['portfolio'][player_mask]['Own'].mean(),
                            'Avg Dupes': st.session_state['portfolio'][player_mask]['Dupes'].mean(),
                            'Avg Finish %': st.session_state['portfolio'][player_mask]['Finish_percentile'].mean(),
                            'Avg Lineup Edge': st.session_state['portfolio'][player_mask]['Lineup Edge'].mean(),
                        })
            
            player_summary = pd.DataFrame(player_stats)
            player_summary = player_summary.sort_values('Lineup Count', ascending=False)
            
            st.subheader("Player Summary")
            st.dataframe(
                player_summary.style
                .background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Avg Finish %', 'Avg Own', 'Avg Dupes'])
                .format({
                    'Avg Median': '{:.2f}',
                    'Avg Own': '{:.2f}',
                    'Avg Dupes': '{:.2f}',
                    'Avg Finish %': '{:.2%}',
                    'Avg Lineup Edge': '{:.2%}'
                }),
                height=400,
                use_container_width=True
            )