# %% [code]

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import csv #file manipulation
import time
import sklearn

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

#import os
#for dirname, _, filenames in os.walk('/kaggle/input'):
 #   for filename in filenames:
  #      print(os.path.join(dirname, filename))

# Open files and scope data

tmp_season = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')
tmp_tourn = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
tourn_seed = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv')
kp_all = pd.read_csv('/kaggle/input/kenpom-all/kenpom_all.csv')
five_data = pd.read_csv('/kaggle/input/ncaa-men-538-team-ratings/538ratingsMen.csv')

tmp_season = tmp_season[['Season', 'WTeamID','WScore','LTeamID', 'LScore','WLoc']]
tmp_season.columns = ['Season', 'HTeamID','HScore','ATeamID', 'AScore','WLoc']

tmp_tourn = tmp_tourn[['Season', 'WTeamID','WScore','LTeamID', 'LScore','WLoc']]
tmp_tourn.columns = ['Season', 'HTeamID','HScore','ATeamID', 'AScore','WLoc']

# Swap data to indicate Home or Away for the regular season

mask = tmp_season['WLoc'] == 'A'
tmp_team = tmp_season.loc[mask, ['HTeamID', 'HScore']].values
tmp_season.loc[mask, ['HTeamID', 'HScore']] = tmp_season.loc[mask, ['ATeamID', 'AScore']].values
tmp_season.loc[mask, ['ATeamID', 'AScore']] = tmp_team


mask = tmp_tourn['WLoc'] == 'A'
tmp_team = tmp_tourn.loc[mask, ['HTeamID', 'HScore']].values
tmp_tourn.loc[mask, ['HTeamID', 'HScore']] = tmp_tourn.loc[mask, ['ATeamID', 'AScore']].values
tmp_tourn.loc[mask, ['ATeamID', 'AScore']] = tmp_team

# Drop WLoc, Remove 1985 to 2002, and sort ascending

tmp_season.drop('WLoc', axis = 1, inplace = True)
tmp_tourn.drop('WLoc', axis = 1, inplace = True)

for x in range(1985,2002):                            
    tmp_season = tmp_season[(tmp_season.Season != x)]
    tmp_tourn = tmp_tourn[(tmp_tourn.Season != x)]
    tourn_seed = tourn_seed[(tourn_seed.Season != x)]

tmp_season.sort_values(by='Season', ascending = False, inplace = True)  
tmp_tourn.sort_values(by='Season', ascending = False, inplace = True)   
tourn_seed.sort_values(by='Season', ascending = False, inplace = True)   

# Merge seed data with tournament and season data
                 
tmp_tourn = pd.merge(tmp_tourn, tourn_seed, left_on = ['Season', 'HTeamID'], right_on = ['Season', 'TeamID'], how='left')  
tmp_tourn = pd.merge(tmp_tourn, tourn_seed, left_on = ['Season', 'ATeamID'], right_on = ['Season', 'TeamID'], how='left')  
tmp_tourn.drop('TeamID_x', axis = 1, inplace = True)
tmp_tourn.drop('TeamID_y', axis = 1, inplace = True)
tmp_tourn.rename(columns = {'Seed_x' : 'HSeed', 'Seed_y' : 'ASeed'}, inplace = True)

tmp_season = pd.merge(tmp_season, tourn_seed, left_on = ['Season', 'HTeamID'], right_on = ['Season', 'TeamID'], how='left')  
tmp_season = pd.merge(tmp_season, tourn_seed, left_on = ['Season', 'ATeamID'], right_on = ['Season', 'TeamID'], how='left')  
tmp_season.drop('TeamID_x', axis = 1, inplace = True)
tmp_season.drop('TeamID_y', axis = 1, inplace = True)
tmp_season.rename(columns = {'Seed_x' : 'HSeed', 'Seed_y' : 'ASeed'}, inplace = True)

# Add binary indicator for a season or tournament game

tmp_season['is_tourn'] = 0
tmp_tourn['is_tourn'] = 1

# Merge and sort the data

all_data = tmp_season.append(tmp_tourn)
all_data.sort_values(by='Season', ascending = False, inplace = True)

# Merge KenPom data

kp_all = kp_all[['Season' , 'TeamID', 'adjem', 'adjo', 'adjd', 'luck']]
kp_merge = pd.merge(all_data, kp_all, left_on = ['Season', 'HTeamID'], right_on = ['Season', 'TeamID'], how='left')
kp_merge.rename(columns = {'adjem' : 'HAdjem', 'adjo' : 'HAdjo', 'adjd' : 'HAdjd', 'luck' : 'HLuck'}, inplace = True)
kp_merge.drop('TeamID', axis = 1, inplace = True)

kp_finish = pd.merge(kp_merge, kp_all, left_on = ['Season', 'ATeamID'], right_on = ['Season', 'TeamID'], how='left')
kp_finish.rename(columns = {'adjem' : 'AAdjem', 'adjo' : 'AAdjo', 'adjd' : 'AAdjd', 'luck' : 'ALuck'}, inplace = True)
kp_finish.drop('TeamID', axis = 1, inplace = True)

# add 538 Data

five_data.sort_values(by='Season', ascending = False, inplace = True)
five_merge = pd.merge(kp_finish, five_data, left_on = ['Season', 'HTeamID'], right_on = ['Season', 'TeamID'], how='left')
five_merge.rename(columns = {'538rating' : 'H538'}, inplace = True)
five_merge.drop(['TeamID', 'TeamName'], axis = 1, inplace = True)


final_data = pd.merge(five_merge, five_data, left_on = ['Season', 'ATeamID'], right_on = ['Season', 'TeamID'], how='left')
final_data.rename(columns = {'538rating' : 'A538'}, inplace = True)
final_data.drop(['TeamID', 'TeamName'], axis = 1, inplace = True)

final_data['Hwin'] = 0
final_data.loc[final_data['HScore'] > final_data['AScore'], 'Hwin'] = 1


# Create data for test set of tournament data from 2018, 2019 data

test_set = final_data

test_set = test_set.sort_values(by=['HTeamID'], ascending = True)

test_set['ID'] = test_set['Season'].astype(str) + '_' \
              + test_set['HTeamID'].astype(str) + '_' \
              + test_set['ATeamID'].astype(str)

test_set = test_set[['Season', 'ID',
 'HTeamID',
 'HScore',
 'ATeamID',
 'AScore',
 'HSeed',
 'ASeed',
 'is_tourn',
 'HAdjem',
 'HAdjo',
 'HAdjd',
 'HLuck',
 'AAdjem',
 'AAdjo',
 'AAdjd',
 'ALuck',
 'H538',
 'A538', 'Hwin']]

sel_variables = ['Season', 'ID',
 'HTeamID',
 'HScore',
 'ATeamID',
 'AScore',
 'HSeed',
 'ASeed',
 'is_tourn',
 'HAdjem',
 'HAdjo',
 'HAdjd',
 'HLuck',
 'AAdjem',
 'AAdjo',
 'AAdjd',
 'ALuck',
 'H538',
 'A538', 'Hwin']


test_set = test_set[test_set['Season'] > 2017]
test_set = test_set[test_set['is_tourn'] == 1]
X_valid = test_set[sel_variables].reset_index(drop=True)
y_valid = test_set['Hwin'].reset_index(drop=True)

#Train on everything else:
#temp_df1 = all_games_df[all_games_df['Season']>2016]
#temp_df1 = temp_df1[temp_df1['is_tourney']==0]
#temp_df2 = all_games_df[all_games_df['Season']<2017]
#combined_temp_df = temp_df1.append(temp_df2)


test_set.tail(50)

# Stopped due to Pandemic




