{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport math\nimport gc\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-10T05:19:09.352449Z","iopub.execute_input":"2022-11-10T05:19:09.352883Z","iopub.status.idle":"2022-11-10T05:19:09.359997Z","shell.execute_reply.started":"2022-11-10T05:19:09.352848Z","shell.execute_reply":"2022-11-10T05:19:09.358630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"df_loss = pd.DataFrame()\n\ncolumn_list = list(df.columns)    \ncols_suffix = [k for k in column_list if 'region' in k]\nfor i in range(len(cols_suffix)):\n    cols_suffix[i] = cols_suffix[i].split(sep='_')[-1]\n\nfor ix in cols_suffix:\n    cum_col = 'cum_distance_{}'.format(ix)\n    cum_col = df[cum_col]\n    \n    for i in range(len(cum_col)):\n        if (cum_col[i] >= df.loc[0, 'run_up_distance']):\n            start_idx = i\n            break\n    print('starting_idx is: {}'.format(start_idx))\n    \n    for j in range(len(cum_col)):\n        if (cum_col[j] >= (df.loc[0, 'run_up_distance'] + df.loc[0, 'distance_id'])):\n            stop_idx = j\n            break\n    print('stoping_idx is: {}'.format(stop_idx))\n    \n    region_col = df['region_{}'.format(ix)].loc[start_idx:stop_idx]\n    velocity_col = df['velocity_horizontal_{}'.format(ix)].loc[start_idx:stop_idx]\n    trakus_col = df['trakus_index'].loc[start_idx:stop_idx]\n    prev_head_col = df['prev_heading_diff_{}'.format(ix)].loc[start_idx:stop_idx]\n    prev_head_col = [math.cos(math.radians(x)) for x in prev_head_col] # just a minor correction for jockey action.\n    velocity_col = velocity_col/prev_head_col # correction for jockey events\n    average_velocity = np.mean(velocity_col)\n    \n    #print(region_col)\n    race_regions = []\n    for k in range(start_idx, stop_idx+1):\n        if (k == start_idx):\n            race_regions.append(region_col.loc[k])\n        elif (region_col.loc[k] != race_regions[-1]):\n            race_regions.append(region_col.loc[k])\n    print(race_regions)\n    \n    row_to_app = pd.Series()\n    row_to_app['program_number']=ix\n    row_to_app['average_velocity']= average_velocity\n    for ab in race_regions:\n        for k in range(start_idx, stop_idx+1):\n            if (k == start_idx):\n                slice_idx_1 = start_idx\n\n            if (ab != region_col.loc[k]):\n                slice_idx_2 = k - 1        \n                velocity_loss_ab = list(velocity_col.loc[slice_idx_1:slice_idx_2])\n                #percentage_loss = (velocity_loss_ab[0] - velocity_loss_ab[-1])/velocity_loss_ab[0]\n                percentage_loss = np.mean(velocity_loss_ab)\n                #ratio = (cum_col.loc[slice_idx_2] - cum_col.loc[slice_idx_1]) /df.loc[0, 'distance_id']\n                region_ab = 'region_{}'.format(ab)\n                row_to_app[region_ab] = (percentage_loss) # * ratio) #* 100\n                #row_to_app = {'region': region_col.loc[slice_idx_1], 'loss_percentage': percentage_loss}\n                #df_loss = df_loss.append(row_to_app, ignore_index=True)\n                start_idx = k\n                print(region_col.loc[k-1], percentage_loss, slice_idx_2)\n                break\n\n            elif (k == stop_idx):\n                velocity_loss_ab = list(velocity_col.loc[start_idx:stop_idx])\n                #percentage_loss = (velocity_loss_ab[0] - velocity_loss_ab[-1])/velocity_loss_ab[0]\n                percentage_loss = np.mean(velocity_loss_ab)\n                #ratio = (cum_col.loc[stop_idx] - cum_col.loc[start_idx]) /df.loc[0, 'distance_id']\n                region_ab = 'region_{}'.format(ab)\n                row_to_app[region_ab] = (percentage_loss) # * ratio) #* 100\n                #row_to_app = {'region': region_col.loc[start_idx], 'loss_percentage': percentage_loss}\n                #df_loss = df_loss.append(row_to_app, ignore_index=True)\n                print(region_col.loc[start_idx], percentage_loss, k)\n                \n    #row_to_app['average'] = np.mean(row_to_app[2:])\n    #average_velocity_abs = [abs(x) for x in row_to_app['average']]\n    #row_to_app['race_velocity'] = (1 - row_to_app['average']) * row_to_app['race_start_velocity'] \n    df_loss = df_loss.append(row_to_app, ignore_index=True)\n    #print(row_to_app)\ndf_loss\n   ","metadata":{"execution":{"iopub.status.busy":"2022-11-09T15:25:06.262304Z","iopub.execute_input":"2022-11-09T15:25:06.262711Z","iopub.status.idle":"2022-11-09T15:25:06.469236Z","shell.execute_reply.started":"2022-11-09T15:25:06.262663Z","shell.execute_reply":"2022-11-09T15:25:06.468073Z"},"jupyter":{"source_hidden":true}}},{"cell_type":"code","source":"path_parent_1 = '../input/pvt-tables'\npath_parent_2 = '../input/drafting/draft'\nparent_1_list = os.listdir(path_parent_1)\nparent_2_list = os.listdir(path_parent_2)\n\nsaving_path_1 = './horse_eff'\npearson_df = pd.DataFrame()\n\nfor parent_1 in parent_1_list:\n    parent_1_files = os.listdir(os.path.join(path_parent_1, parent_1))\n    \n    if not os.path.exists(os.path.join(saving_path_1, parent_1)):\n        os.makedirs(os.path.join(saving_path_1, parent_1))\n    \n    for file in parent_1_files:\n        file_path = os.path.join(path_parent_1, parent_1, file)\n        average_velocity_df = pd.DataFrame()\n        df = pd.read_csv(file_path)\n        #print(file_path)\n\n        column_list = list(df.columns)    \n        cols_suffix = [k for k in column_list if 'region' in k]\n        for i in range(len(cols_suffix)):\n            cols_suffix[i] = cols_suffix[i].split(sep='_')[-1]\n\n        for ix in cols_suffix:\n            cum_col = 'cum_distance_{}'.format(ix)\n            cum_col = df[cum_col]\n\n            for i in range(len(cum_col)):\n                if (cum_col[i] >= df.loc[0, 'run_up_distance']):\n                    start_idx = i\n                    break\n            #print('starting_idx is: {}'.format(start_idx))\n\n            for j in range(len(cum_col)):\n                if (cum_col[j] >= (df.loc[0, 'run_up_distance'] + df.loc[0, 'distance_id'])):\n                    stop_idx = j\n                    break\n            #print('stoping_idx is: {}'.format(stop_idx))\n\n            velocity_col = df['velocity_horizontal_{}'.format(ix)].loc[start_idx:stop_idx]\n            average_velocity_before_corr = np.mean(velocity_col)\n\n            velocity_col_2 = df['velocity_horizontal_{}'.format(ix)].loc[0:stop_idx]\n            average_velocity_from_0 = np.mean(velocity_col_2)\n\n            prev_head_col = df['prev_heading_diff_{}'.format(ix)].loc[start_idx:stop_idx]\n            prev_head_col = [math.cos(math.radians(x)) for x in prev_head_col] # just a minor correction for jockey action.\n            velocity_col = velocity_col/prev_head_col # correction for jockey events\n            average_velocity_corr = np.mean(velocity_col)\n            \n            prev_head_col_0 = df['prev_heading_diff_{}'.format(ix)].loc[0:stop_idx]\n            prev_head_col_0 = [math.cos(math.radians(x)) for x in prev_head_col_0] # just a minor correction for jockey action.\n            velocity_col_3 = velocity_col_2/prev_head_col_0 # correction for jockey events\n            average_velocity_corr_0 = np.mean(velocity_col_3)\n\n            row_to_app = pd.Series()\n            row_to_app['program_number']=ix\n            row_to_app['average_velocity_before_corr']= average_velocity_before_corr\n            row_to_app['average_velocity_from_0']= average_velocity_from_0\n            row_to_app['average_velocity_corr']= average_velocity_corr\n            row_to_app['average_velocity_corr_0']= average_velocity_corr_0\n            average_velocity_df = average_velocity_df.append(row_to_app, ignore_index=True)\n        saving_path = os.path.join(saving_path_1, parent_1, file)\n        average_velocity_df.to_csv(saving_path, index=False)  \n        \n        #if ('AQU-2019-01-06-4' in saving_path):\n         #   display(average_velocity_df)\n        \n        draft_path = file_path.replace('../input/pvt-tables', '../input/drafting/draft')\n        draft_eff = pd.read_csv(draft_path)\n        draft_eff['program_number'] = [str(x) for x in draft_eff['program_number']]\n        draft_eff = draft_eff.dropna()\n        merged_df = pd.merge(draft_eff, average_velocity_df, how='left', left_on=['program_number'], right_on = ['program_number'])\n        \n        path_eff_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.path_efficiency)[0,1] * -1\n        draft_eff_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.draft_time_percentage)[0,1] * -1\n        jockey_eff_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.jockey_efficiency)[0,1] * -1\n        velocity_bef_corr_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.average_velocity_before_corr)[0,1] * -1\n        velocity_0_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.average_velocity_from_0)[0,1] * -1\n        velocity_corr_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.average_velocity_corr)[0,1] * -1\n        velocity_corr_0_cc = np.corrcoef(merged_df.true_position_at_finish, merged_df.average_velocity_corr_0)[0,1] * -1\n        # pls note that i multiplied by -1 for the highest ranking ps is 1 and lowest is n. so pearson will result in \n        # -ve high values for highley correleted features. i needed it to be +ve.\n        race = file.split(sep='.')[0]\n        row_to_app_2 = {'race':race, 'path_eff_cc':path_eff_cc, 'draft_eff_cc':draft_eff_cc, 'jockey_eff_cc':jockey_eff_cc, 'velocity_bef_corr_cc':velocity_bef_corr_cc,\n                       'velocity_0_cc':velocity_0_cc, 'velocity_corr_cc':velocity_corr_cc, 'velocity_corr_0_cc':velocity_corr_0_cc}\n        pearson_df = pearson_df.append(row_to_app_2, ignore_index=True)\n        \n        if (velocity_0_cc < 0):\n            print(file_path)\n        \nsaving_path_3 = os.path.join(saving_path_1, 'pearson_all')\nif not os.path.exists(saving_path_3):\n    os.makedirs(saving_path_3)\nsaving_path_3 = os.path.join(saving_path_3, 'pearson_df.csv')\n\npearson_df.to_csv(saving_path_3, index=False)\n\n#display(pearson_df)\n   \n        ","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:14:56.733437Z","iopub.execute_input":"2022-11-10T06:14:56.733911Z","iopub.status.idle":"2022-11-10T06:19:48.507101Z","shell.execute_reply.started":"2022-11-10T06:14:56.733874Z","shell.execute_reply":"2022-11-10T06:19:48.505644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pearson_df.mean(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:05:41.410643Z","iopub.execute_input":"2022-11-10T06:05:41.411099Z","iopub.status.idle":"2022-11-10T06:05:41.425207Z","shell.execute_reply.started":"2022-11-10T06:05:41.411014Z","shell.execute_reply":"2022-11-10T06:05:41.423650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"count = 0\nfor x in range(len(pearson_df.path_eff_cc)):\n    if (math.isnan(pearson_df.path_eff_cc[x])):\n        count+=1\n        print(pearson_df.race[x])\ncount","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:39:19.410902Z","iopub.execute_input":"2022-11-10T05:39:19.411428Z","iopub.status.idle":"2022-11-10T05:39:19.446611Z","shell.execute_reply.started":"2022-11-10T05:39:19.411385Z","shell.execute_reply":"2022-11-10T05:39:19.445124Z"}}},{"cell_type":"code","source":"#pearson_df.race  \n#plt.plot(pearson_df.race, pearson_df.path_eff_cc)\n#plt.plot(pearson_df.race, pearson_df.draft_eff_cc)\n#plt.plot(pearson_df.race,  pearson_df.jockey_eff_cc)\nplt.scatter(pearson_df.race,  pearson_df.velocity_0_cc, s=5.)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:05:59.724749Z","iopub.execute_input":"2022-11-10T06:05:59.725233Z","iopub.status.idle":"2022-11-10T06:06:29.981838Z","shell.execute_reply.started":"2022-11-10T06:05:59.725199Z","shell.execute_reply":"2022-11-10T06:06:29.980709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(pearson_df.race, pearson_df.jockey_eff_cc, s=5.)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:09:11.074627Z","iopub.execute_input":"2022-11-10T06:09:11.075103Z","iopub.status.idle":"2022-11-10T06:09:41.427510Z","shell.execute_reply.started":"2022-11-10T06:09:11.075066Z","shell.execute_reply":"2022-11-10T06:09:41.426301Z"},"trusted":true},"execution_count":null,"outputs":[]}]}