File size: 11,796 Bytes
666100e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
991f67f
666100e
5c1f02b
666100e
 
bc57db2
 
 
 
 
 
 
 
 
 
 
 
 
c7a9602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0402531
0a818cb
 
 
 
 
 
c7a9602
0402531
4b1d079
12c5542
 
 
 
 
 
 
c7a9602
12c5542
 
 
 
 
3ed3e6b
 
 
c7a9602
3ed3e6b
c7a9602
3ed3e6b
 
 
c7a9602
3ed3e6b
c7a9602
3ed3e6b
12c5542
 
121ea11
3ed3e6b
 
c7a9602
3ed3e6b
c7a9602
3ed3e6b
 
 
c7a9602
3ed3e6b
c7a9602
3ed3e6b
12c5542
c7a9602
 
016be9e
c7a9602
 
 
016be9e
 
e6b4d5e
 
 
c7a9602
 
e6b4d5e
c7a9602
 
 
 
0a818cb
c7a9602
 
 
016be9e
e6b4d5e
016be9e
 
e6b4d5e
016be9e
 
c7a9602
e6b4d5e
c7a9602
 
e6b4d5e
016be9e
e6b4d5e
 
 
c7a9602
 
e6b4d5e
c7a9602
 
 
 
0a818cb
c7a9602
 
 
016be9e
e6b4d5e
016be9e
 
e6b4d5e
016be9e
12c5542
 
 
 
 
 
 
 
 
 
 
ab169e9
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
import streamlit as st
st.set_page_config(layout="wide")

for name in dir():
    if not name.startswith('_'):
        del globals()[name]
        
import numpy as np
import pandas as pd
import streamlit as st
import gspread
import pymongo

@st.cache_resource
def init_conn():
        scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

        credentials = {
          "type": "service_account",
          "project_id": "model-sheets-connect",
          "private_key_id": "0e0bc2fdef04e771172fe5807392b9d6639d945e",
          "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\ncx0KQ23nZK3ZVvADYy8u/RUn/EDI82QKxTd/DizRLIV81JiNQxDJXSzgkbwKYEDm\n48E8zGvupU8+Nk76xNPakrQKy2Y8+VJlq5psBtGchJTuUSHcXU5Mg2JhQsB376PJ\nsCw552K6Pw8fpeMDJDZuxpKSkaJR6k9G5Dhf5q8HDXnC5Rh/PRFuKJ2GGRpX7n+2\nhT/sCax0J8jfdTy/MDGiDfJqfQrOPrMKELtsGHR9Iv6F4vKiDqXpKfqH+02E9ptz\nBk+MNcbZ3m90M8ShfRu28ebebsASfarNMzc3dk7tb3utHOGXKCf4tF8yYKo7x8BZ\noO9X4gSfAgMBAAECggEAU8ByyMpSKlTCF32TJhXnVJi/kS+IhC/Qn5JUDMuk4LXr\naAEWsWO6kV/ZRVXArjmuSzuUVrXumISapM9Ps5Ytbl95CJmGDiLDwRL815nvv6k3\nUyAS8EGKjz74RpoIoH6E7EWCAzxlnUgTn+5oP9Flije97epYk3H+e2f1f5e1Nn1d\nYNe8U+1HqJgILcxA1TAUsARBfoD7+K3z/8DVPHI8IpzAh6kTHqhqC23Rram4XoQ6\nzj/ZdVBjvnKuazETfsD+Vl3jGLQA8cKQVV70xdz3xwLcNeHsbPbpGBpZUoF73c65\nkAXOrjYl0JD5yAk+hmYhXr6H9c6z5AieuZGDrhmlFQKBgQDzV6LRXmjn4854DP/J\nI82oX2GcI4eioDZPRukhiQLzYerMQBmyqZIRC+/LTCAhYQSjNgMa+ZKyvLqv48M0\n/x398op/+n3xTs+8L49SPI48/iV+mnH7k0WI/ycd4OOKh8rrmhl/0EWb9iitwJYe\nMjTV/QxNEpPBEXfR1/mvrN/lVQKBgQDuhomOxUhWVRVH6x03slmyRBn0Oiw4MW+r\nrt1hlNgtVmTc5Mu+4G0USMZwYuOB7F8xG4Foc7rIlwS7Ic83jMJxemtqAelwOLdV\nXRLrLWJfX8+O1z/UE15l2q3SUEnQ4esPHbQnZowHLm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
          "client_email": "[email protected]",
          "client_id": "100369174533302798535",
          "auth_uri": "https://accounts.google.com/o/oauth2/auth",
          "token_uri": "https://oauth2.googleapis.com/token",
          "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
          "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
        }
        uri = "mongodb+srv://multichem:[email protected]/?retryWrites=true&w=majority&appName=TestCluster"
        client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=100000)
        db = client["testing_db"]

        gc_con = gspread.service_account_from_dict(credentials, scope)
      
        return gc_con, client, db

gcservice_account, client, db = init_conn()

MLB_Data = 'https://docs.google.com/spreadsheets/d/1f42Ergav8K1VsOLOK9MUn7DM_MLMvv4GR2Fy7EfnZTc/edit#gid=340831852'

percentages_format = {'Exposure': '{:.2%}'}

@st.cache_data(ttl = 600)
def init_baselines():
    sh = gcservice_account.open_by_url(MLB_Data)
    
    worksheet = sh.worksheet('DK_Projections')
    load_display = pd.DataFrame(worksheet.get_all_records())
    load_display.replace('', np.nan, inplace=True)
    
    dk_raw = load_display.dropna(subset=['Median'])
    
    worksheet = sh.worksheet('FD_Projections')
    load_display = pd.DataFrame(worksheet.get_all_records())
    load_display.replace('', np.nan, inplace=True)
    
    fd_raw = load_display.dropna(subset=['Median'])

    return dk_raw, fd_raw

@st.cache_data(ttl = 600)
def init_DK_seed_frame():
    collection = db["DK_MLB_seed_frame"] 
    cursor = collection.find()
    
    raw_display = pd.DataFrame(list(cursor))
    DK_seed = raw_display[['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'salary', 'proj']]

    return DK_seed

@st.cache_data(ttl = 600)
def init_FD_seed_frame():
    collection = db["FD_MLB_seed_frame"] 
    cursor = collection.find()
    
    raw_display = pd.DataFrame(list(cursor))
    FD_seed = raw_display[['P', 'C_1B', '2B', '3B', 'SS', 'OF1', 'OF2', 'OF3', 'UTIL', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'salary', 'proj']]

    return FD_seed

@st.cache_data
def convert_df_from_parquet(df):
    # IMPORTANT: Cache the conversion to prevent computation on every rerun
    working_file = df.read_parquet()
    return working_file.to_csv().encode('utf-8')

dk_raw, fd_raw = init_baselines()

tab1, tab2 = st.tabs(['Data Export', 'Contest Sims'])
with tab1:
    col1, col2 = st.columns([1, 7])
    with col1:
        if st.button("Load/Reset Data", key='reset1'):
              st.cache_data.clear()
              for key in st.session_state.keys():
                  del st.session_state[key]
              dk_raw, fd_raw = init_baselines()
              
        slate_var1 = st.radio("Which data are you loading?", ('Main Slate', 'Other Main Slate'))
        site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'))
        if site_var1 == 'Draftkings':
            raw_baselines = dk_raw
            
            team_var1 = st.radio("Do you want a frame with specific teams?", ('Full Slate', 'Specific Teams'), key='team_var1')
            if team_var1 == 'Specific Teams':
                    team_var2 = st.multiselect('Which teams do you want?', options = dk_raw['Team'].unique())
            elif team_var1 == 'Full Slate':
                    team_var2 = dk_raw.Team.values.tolist()
            
            stack_var1 = st.radio("Do you want a frame with specific stack sizes?", ('Full Slate', 'Specific Stack Sizes'), key='stack_var1')
            if stack_var1 == 'Specific Stack Sizes':
                    stack_var2 = st.multiselect('Which stack sizes do you want?', options = [5, 4, 3, 2, 1, 0])
            elif stack_var1 == 'Full Slate':
                    stack_var2 = [5, 4, 3, 2, 1, 0]
                    
        elif site_var1 == 'Fanduel':
            raw_baselines = fd_raw
            
            team_var1 = st.radio("Do you want a frame with specific teams?", ('Full Slate', 'Specific Teams'), key='team_var1')
            if team_var1 == 'Specific Teams':
                    team_var2 = st.multiselect('Which teams do you want?', options = fd_raw['Team'].unique())
            elif team_var1 == 'Full Slate':
                    team_var2 = fd_raw.Team.values.tolist()
            
            stack_var1 = st.radio("Do you want a frame with specific stack sizes?", ('Full Slate', 'Specific Stack Sizes'), key='stack_var1')
            if stack_var1 == 'Specific Stack Sizes':
                    stack_var2 = st.multiselect('Which stack sizes do you want?', options = [4, 3, 2, 1, 0])
            elif stack_var1 == 'Full Slate':
                    stack_var2 = [4, 3, 2, 1, 0]
            
    with col2:
        if st.button("Load Seed Frame", key='seed_frame_load'):
            if site_var1 == 'Draftkings':
                
                DK_seed = init_DK_seed_frame()
                DK_seed_parse = DK_seed[DK_seed['Team'].isin(team_var2)]
                DK_seed_parse = DK_seed_parse[DK_seed_parse['Team_count'].isin(stack_var2)]
                data_export_display = DK_seed_parse.head(1000)
                st.session_state.data_export_display = data_export_display.copy()
                st.session_state.data_export_expo = DK_seed.copy()
                st.session_state.data_export = DK_seed.to_parquet()
                st.session_state.data_export_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.data_export_expo.iloc[:,0:9].values, return_counts=True)),
                                            columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
                st.session_state.data_export_freq['Freq'] = st.session_state.data_export_freq['Freq'].astype(int)
                st.session_state.data_export_freq['Exposure'] = st.session_state.data_export_freq['Freq']/(len(st.session_state.data_export_expo['Team']))
                
                if 'data_export' in st.session_state:
                        st.download_button(
                            label="Export optimals set",
                            data=convert_df_from_parquet(st.session_state.data_export),
                            file_name='MLB_optimals_export.csv',
                            mime='text/csv',
                        )
                with st.container():
                    if 'data_export_display' in st.session.state:
                        st.dataframe(st.session_state.data_export_display.style.format(precision=2), height=500, use_container_width=True)
                with st.container():
                    if 'data_export_freq' in st.session.state:
                        st.dataframe(st.session_state.data_export_freq.style.format(percentages_format, precision=2), height=500, use_container_width=True)
            
            elif site_var1 == 'Fanduel':
                FD_seed = init_FD_seed_frame()
                FD_seed_parse = FD_seed[FD_seed['Team'].isin(team_var2)]
                FD_seed_parse = FD_seed_parse[FD_seed_parse['Team_count'].isin(stack_var2)]
                data_export_display = FD_seed_parse.head(1000)
                st.session_state.data_export_display = data_export_display.copy()
                st.session_state.data_export_expo = FD_seed.copy()
                st.session_state.data_export = FD_seed.to_parquet()
                st.session_state.data_export_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.data_export_expo.iloc[:,0:8].values, return_counts=True)),
                                            columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
                st.session_state.data_export_freq['Freq'] = st.session_state.data_export_freq['Freq'].astype(int)
                st.session_state.data_export_freq['Exposure'] = st.session_state.data_export_freq['Freq']/(len(st.session_state.data_export_expo['Team']))
                
                if 'data_export' in st.session_state:
                        st.download_button(
                            label="Export optimals set",
                            data=convert_df_from_parquet(st.session_state.data_export),
                            file_name='MLB_optimals_export.csv',
                            mime='text/csv',
                        )
                with st.container():
                    if 'data_export_display' in st.session.state:
                        st.dataframe(st.session_state.data_export_display.style.format(precision=2), height=500, use_container_width=True)
                with st.container():
                    if 'data_export_freq' in st.session.state:
                        st.dataframe(st.session_state.data_export_freq.style.format(percentages_format, precision=2), height=500, use_container_width=True)
            
with tab2:
    col1, col2 = st.columns([1, 7])
    with col1:
        if st.button("Load/Reset Data", key='reset2'):
              st.cache_data.clear()
              for key in st.session_state.keys():
                  del st.session_state[key]
              DK_seed, FD_seed, dk_raw, fd_raw = init_baselines()
    
    with col2:
        st.write("Things will go here")