{"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 datetime\nfrom sklearn.metrics.pairwise import haversine_distances \nimport math\nimport gc\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-01T05:54:44.963103Z","iopub.execute_input":"2022-11-01T05:54:44.963692Z","iopub.status.idle":"2022-11-01T05:54:44.969927Z","shell.execute_reply.started":"2022-11-01T05:54:44.963652Z","shell.execute_reply":"2022-11-01T05:54:44.968847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_complete_cols = ['track_id', 'race_date', 'race_number', 'program_number', 'trakus_index', 'latitude', 'longitude', 'distance_id', 'course_type', 'track_condition', 'run_up_distance', 'race_type', 'purse', \n 'post_time', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\nnyra_complete_dtype = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'program_number':'object', 'trakus_index':'int64', 'latitude':'float64', 'longitude':'float64', 'distance_id': 'int64',\n'course_type':'object', 'track_condition':'object', 'run_up_distance':'int64', 'race_type':'object', 'purse':'float64', 'post_time':'object', 'weight_carried':'int64', 'jockey':'object',\n'odds':'float64', 'position_at_finish':'int64'}\nnyra_complete = pd.read_csv('../input/big-data-derby-2022/nyra_2019_complete.csv', names=nyra_complete_cols, dtype=nyra_complete_dtype, parse_dates=True, header=None)\nnyra_complete.odds = nyra_complete.odds/100\n#nyra_complete.post_time.apply(lambda x: x.strftime(r'%H:%M'))","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:54:44.971833Z","iopub.execute_input":"2022-11-01T05:54:44.972157Z","iopub.status.idle":"2022-11-01T05:55:03.440405Z","shell.execute_reply.started":"2022-11-01T05:54:44.972127Z","shell.execute_reply":"2022-11-01T05:55:03.439289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"import datetime\nnyra_complete.post_time.apply(lambda x: datetime.datetime.fromtimestamp(x).strftime('%H:%M'))","metadata":{"execution":{"iopub.status.busy":"2022-09-18T18:37:50.649232Z","iopub.execute_input":"2022-09-18T18:37:50.650103Z","iopub.status.idle":"2022-09-18T18:38:07.130801Z","shell.execute_reply.started":"2022-09-18T18:37:50.650050Z","shell.execute_reply":"2022-09-18T18:38:07.129842Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"nyra_race_table_dtype = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'distance_id': 'int64',\n                        'course_type':'object', 'track_condition':'object', 'run_up_distance':'int64', 'race_type':'object',\n                        'purse':'float64', 'post_time':'object'}\nnyra_race_table = pd.read_csv('../input/big-data-derby-2022/nyra_race_table.csv', dtype=nyra_race_table_dtype, parse_dates=True)\nnyra_race_table.distance_id = nyra_race_table.distance_id/100","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:03.441902Z","iopub.execute_input":"2022-11-01T05:55:03.442334Z","iopub.status.idle":"2022-11-01T05:55:03.472863Z","shell.execute_reply.started":"2022-11-01T05:55:03.442293Z","shell.execute_reply":"2022-11-01T05:55:03.471911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_table_cols = ['track_id', 'race_date', 'race_number', 'program_number', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\nnyra_start_table_dtypes = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'program_number':'object', 'weight_carried':'int64', 'jockey':'object', 'odds':'float64', 'position_at_finish':'int64'}\nnyra_start_table = pd.read_csv('../input/big-data-derby-2022/nyra_start_table.csv', names=start_table_cols, dtype=nyra_start_table_dtypes, parse_dates=True)\nnyra_start_table.odds = nyra_start_table.odds/100\nnyra_start_table['program_number'] = nyra_start_table['program_number'].map(lambda x: x.strip())","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:03.475943Z","iopub.execute_input":"2022-11-01T05:55:03.476690Z","iopub.status.idle":"2022-11-01T05:55:03.513548Z","shell.execute_reply.started":"2022-11-01T05:55:03.476649Z","shell.execute_reply":"2022-11-01T05:55:03.512428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking_table_dtypes = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'program_number':'object', 'trakus_index':'int64', 'latitude':'float64', 'longitude':'float64'}\nnyra_tracking_table = pd.read_csv('../input/big-data-derby-2022/nyra_tracking_table.csv', dtype=nyra_tracking_table_dtypes, parse_dates=True)\nnyra_tracking_table = nyra_tracking_table.assign(lat_radian=nyra_tracking_table.latitude * math.pi/180.)\nnyra_tracking_table = nyra_tracking_table.assign(long_radian=nyra_tracking_table.longitude * math.pi/180.)\nnyra_tracking_table['program_number'] = nyra_tracking_table['program_number'].map(lambda x: x.strip())","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:03.515474Z","iopub.execute_input":"2022-11-01T05:55:03.515845Z","iopub.status.idle":"2022-11-01T05:55:12.892323Z","shell.execute_reply.started":"2022-11-01T05:55:03.515813Z","shell.execute_reply":"2022-11-01T05:55:12.891247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start_table[:100] ","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:12.893850Z","iopub.execute_input":"2022-11-01T05:55:12.894280Z","iopub.status.idle":"2022-11-01T05:55:12.924972Z","shell.execute_reply.started":"2022-11-01T05:55:12.894232Z","shell.execute_reply":"2022-11-01T05:55:12.923804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"nyra_start_table[['track_id', 'race_date']]","metadata":{"execution":{"iopub.status.busy":"2022-10-09T16:03:40.110433Z","iopub.execute_input":"2022-10-09T16:03:40.111168Z","iopub.status.idle":"2022-10-09T16:03:40.129380Z","shell.execute_reply.started":"2022-10-09T16:03:40.111122Z","shell.execute_reply":"2022-10-09T16:03:40.128109Z"}}},{"cell_type":"raw","source":"nyra_race_table[nyra_race_table['track_id']=='AQU'].course_type.unique()","metadata":{"execution":{"iopub.status.busy":"2022-10-09T16:03:40.131127Z","iopub.execute_input":"2022-10-09T16:03:40.131871Z","iopub.status.idle":"2022-10-09T16:03:40.147460Z","shell.execute_reply.started":"2022-10-09T16:03:40.131823Z","shell.execute_reply":"2022-10-09T16:03:40.146112Z"}}},{"cell_type":"code","source":"nyra_race_table[nyra_race_table['track_id']=='AQU'][nyra_race_table['course_type']=='D']","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:12.926200Z","iopub.execute_input":"2022-11-01T05:55:12.926537Z","iopub.status.idle":"2022-11-01T05:55:12.959361Z","shell.execute_reply.started":"2022-11-01T05:55:12.926505Z","shell.execute_reply":"2022-11-01T05:55:12.958189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking_table.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:12.961143Z","iopub.execute_input":"2022-11-01T05:55:12.961608Z","iopub.status.idle":"2022-11-01T05:55:12.978232Z","shell.execute_reply.started":"2022-11-01T05:55:12.961564Z","shell.execute_reply":"2022-11-01T05:55:12.977044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def distance_calculator(list_obj:list): # list of [[lat1,long1],[lat2,long2]]\n    p_1 = list_obj[0]\n    p_2 = list_obj[1]\n    distance_m = float(np.max(haversine_distances([p_1, p_2]) * 6371000)) # in meters\n    distance_ft = float(np.max(haversine_distances([p_1, p_2]) * 20.902e6)) # in feet\n    return distance_m, distance_ft","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:12.981799Z","iopub.execute_input":"2022-11-01T05:55:12.982847Z","iopub.status.idle":"2022-11-01T05:55:12.989001Z","shell.execute_reply.started":"2022-11-01T05:55:12.982807Z","shell.execute_reply":"2022-11-01T05:55:12.987873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def heading_calculator(list_obj:list):\n    p_1 = list_obj[0]\n    p_2 = list_obj[1]\n    p_m = [p_2[0], p_1[1]]\n    ver, _ = distance_calculator([p_1, p_m])\n    hor, _ = distance_calculator([p_m, p_2])\n    hyp = math.sqrt(hor**2 + ver**2)\n    \n    try:\n        theta = math.acos(ver/hyp) * (180./math.pi)\n    except ZeroDivisionError:\n        print(hor, ver, hyp)\n        \n    lat = p_2[0] - p_1[0]\n    long = p_2[1] - p_1[1]\n    \n    if (((hyp==0.) and (ver==0.)) and (hor==0.)):\n        lat == 0.\n        long == 0.\n    \n    if((lat<0.) & (long<0.)):\n        heading = 180. + abs(theta)\n    elif((lat<0.) & (long>0.)):\n        heading = 180. - abs(theta)\n    elif((lat>0.) & (long<0.)):\n        heading = 360. - abs(theta)\n    elif((lat>0.) & (long>0.)):\n        heading = abs(theta)\n    elif((lat==0.) & (long>0.)):\n        heading = 90.\n    elif((lat==0.) & (long<0.)):\n        heading = 270.\n    elif((lat>0.) & (long==0.)):\n        heading = 0.\n    elif((lat<0.) & (long==0.)):\n        heading = 180.    \n    else:    #this will be activated when both lat & long are zeros.\n        heading = float(\"Nan\")\n    \n    heading_rad = heading * math.pi/180.\n    heading_deg = heading\n    return heading_deg, heading_rad","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:12.990452Z","iopub.execute_input":"2022-11-01T05:55:12.990831Z","iopub.status.idle":"2022-11-01T05:55:13.004992Z","shell.execute_reply.started":"2022-11-01T05:55:12.990782Z","shell.execute_reply":"2022-11-01T05:55:13.003803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"# calculating track heading!\np_1a = [40.671363, -73.828386]\np_1b = [40.672329, -73.827942]\np_2a = [40.674348, -73.830598]\np_2b = [40.672855, -73.831285]\nconst = math.pi/180.\np_1a = [x*const for x in p_1a] #multiplied_list = [value * 3 for value in b_list]\np_1b = [x*const for x in p_1b]\np_2a = [x*const for x in p_2a]\np_2b = [x*const for x in p_2b]\nhead1,_ = heading_calculator([p_1a, p_1b])\nhead2,_ = heading_calculator([p_2a, p_2b])\nprint(head1, head2)","metadata":{"execution":{"iopub.status.busy":"2022-10-07T05:43:55.562187Z","iopub.execute_input":"2022-10-07T05:43:55.563479Z","iopub.status.idle":"2022-10-07T05:43:55.573111Z","shell.execute_reply.started":"2022-10-07T05:43:55.563437Z","shell.execute_reply":"2022-10-07T05:43:55.571784Z"}}},{"cell_type":"code","source":"def rotational_heading_diff (stride:float, perp_angle_diff:float, center_pt:float):\n    # given Stride, perp angle diff, center_pt dist,  \n    perp_angle_diff = abs(perp_angle_diff)*math.pi/180. # radians\n    # if the horse travelled in the tangential velocity direction its stride would be\n    tan_velocity_stride = stride / math.cos(perp_angle_diff) # stride in the direction of tangential velocity\n    betha_max = 0.\n    theta_max = 0.\n    stride = 0.\n    count = 0\n    for i in range(0, 90):\n        betha1 = 0.0008726646259971648 # 0.05 deg in radian\n        betha = i*betha1\n        stride_betha = tan_velocity_stride * math.cos(betha)\n        stride_betha_vert = stride_betha*math.cos(betha)\n        stride_betha_horiz = stride_betha*math.sin(betha)\n        center_horiz = center_pt - stride_betha_horiz\n        try:\n            theta1 = math.atan(stride_betha_vert/center_horiz)\n        except ValueError:\n            continue \n        #print(theta1)\n        if (theta1>theta_max):\n            theta_max = theta1\n            betha_max = betha\n            stride = stride_betha\n        if (theta1<theta_max):\n            count+=1\n        if (count>4):\n            break\n    try:\n        center_p2 = (stride/2)/math.sin(theta_max/2)\n    except ZeroDivisionError:\n        center_p2 = center_pt\n    return math.degrees(betha_max), center_p2","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.006468Z","iopub.execute_input":"2022-11-01T05:55:13.006840Z","iopub.status.idle":"2022-11-01T05:55:13.021595Z","shell.execute_reply.started":"2022-11-01T05:55:13.006804Z","shell.execute_reply":"2022-11-01T05:55:13.020423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Heading_diff_track (region:str, heading:float, field_tilt:float, stride=0., curve_center=[0.,0.], pt_1=[0.,0.]):\n    # track headings may be part of the arguments\n    \n    heading_1 = 61.47592139144653 # starting line heading\n    heading_2 = 241.57172692823565 # finish line heading\n    heading_3 = 62.803504620707805\n    \n    # pls note that im considering the races will be held always in the CCW direction.\n    if ((region == 'I') or (region == 'III')):\n        # the expected heading shall be tangent to the curve.\n        center_to_pt = [curve_center, pt_1] # all shall be given in radians\n        _, center_pt_distance = distance_calculator(center_to_pt)\n        heading_central, _ = heading_calculator(center_to_pt) #radial heading\n        if (heading_central>=90.0):\n            perp_heading_expected = heading_central-90.0\n        elif (heading_central<90.0):\n            perp_heading_expected = 360.0+(heading_central-90.0)\n            \n        if ((perp_heading_expected>(270.)) and (heading<(90.))): \n            perp_heading_diff = -((360.0-perp_heading_expected) + heading)\n        elif ((heading>(270.)) and (perp_heading_expected<(90.))):\n            perp_heading_diff = (360.0-heading) + perp_heading_expected\n        else:\n            perp_heading_diff = perp_heading_expected - heading # difference bn tangential velocity heading and heading\n            \n        # perp_heading_expected is the heading of the Tangential Velocity/Force.\n        # lets find the track heaading!\n        beta, center_p2_efficient = rotational_heading_diff (stride=stride, perp_angle_diff = abs(perp_heading_diff), center_pt=center_pt_distance)\n        # rotational_heading_diff func iterates over some beta's and finds the effiecnt betha and it returns it \n        # with center_p2 distance if the horse has kept the esfficient path. i will use this to explain circumscribed circle.\n        \n        track_heading = perp_heading_expected - beta\n        if (track_heading<0.):\n            track_heading = 360.+track_heading        \n                    \n        if ((track_heading>(270.)) and (heading<(90.))): \n            heading_diff = -((360.0-track_heading) + heading)\n        elif ((heading>(270.)) and (track_heading<(90.))):\n            heading_diff = (360.0-heading) + track_heading\n        else:\n            heading_diff = track_heading - heading\n            \n        # we just made sure heading_diff will be -Ve when heading away from the track edge.\n               \n    elif (region == 'II'):\n        if (heading<90.):\n            heading_diff = (360. - heading_2) + heading\n        else:\n            heading_diff = heading_2-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge.\n    \n    elif (region == 'IV'):\n        if (heading>(270.)): # this is if the horse is aproaching perpendicularlly\n            heading_diff = (360.-heading)+heading_1\n        else:\n            heading_diff = heading_1-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge.   \n        \n    elif (region == 'V'): # region IV starting point\n        if (heading>(270.)): # this is if the horse is aproaching perpendicularlly\n            heading_diff = (360.-heading)+heading_3\n        else:\n            heading_diff = heading_3-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge.\n    \n    if ((region!='I') and (region!='III')):\n        beta=0.\n        center_p2_efficient = 0.\n    \n    return heading_diff, beta, center_p2_efficient # its in degree","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.023659Z","iopub.execute_input":"2022-11-01T05:55:13.024124Z","iopub.status.idle":"2022-11-01T05:55:13.041614Z","shell.execute_reply.started":"2022-11-01T05:55:13.024083Z","shell.execute_reply":"2022-11-01T05:55:13.040146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Distance_from_edge (region:str, radius:float, field_tilt:float, p_2:list, tan_pt=[0.,0.], curve_center=[0.,0.]):\n\n    # remember we need this part for drift.\n    if ((region == 'I') or (region == 'III')):\n        _, distance = distance_calculator([curve_center, p_2])\n        abcd = distance - radius\n        edge_distance = abcd\n        #print('section a {} {} {} {}'.format(region, edge_distance, radius, abcd))\n       \n    elif (region=='II'):\n        #if (p_2[0] >= ((-1.3467072620388518*p_2[1]) - 0.9820752870882066)):\n            #_, distance = distance_calculator([curve_center, p_2])\n            #edge_distance = distance - radius\n            #print('section b {} {}'.format(edge_distance, distance, radius))\n        \n        #elif (p_2[0] < ((-1.3467072620388518*p_2[1]) - 0.9820752870882066)):\n        _, heading = heading_calculator([p_2, tan_pt])\n        _, distance = distance_calculator([p_2, tan_pt])\n        theta = heading - field_tilt*(math.pi/180.)\n        edge_distance = (distance*math.sin(theta)) \n            #print('section e {} {}'.format(edge_distance, distance))\n             \n    elif (region=='IV'):\n        # lets try it with tangent point. p_t=[0.,0.]\n        # center_curve shall be curve_center_2 (lower center)  \n        if((p_2[0] <= ((-1.2143077556268673*p_2[1]) - 0.811780650838537)) and (p_2[0] <= ((0.3693621621610286*p_2[1]) + 1.2272706243727403))): # horizontal and axial limits\n            # starting point condistion\n            _, heading = heading_calculator([tan_pt, p_2])\n            _, distance = distance_calculator([tan_pt, p_2])\n            theta = heading-(field_tilt*(math.pi/180.))\n            edge_distance = (distance*math.sin(theta))\n            #print('section l {} {} {} {}'.format(edge_distance, distance, math.degrees(heading), math.degrees(theta)))\n        elif (p_2[0] <= ((-1.3467496831151728*p_2[1]) - 0.982278119738297)): \n            _, distance = distance_calculator([curve_center, p_2])\n            edge_distance = distance-radius\n            #print('section f {} {}'.format(edge_distance, distance))  \n                            \n        elif (p_2[0] >= ((-1.3467496831151728*p_2[1]) - 0.982278119738297)):\n            # above the lower diameter line\n            _, heading = heading_calculator([tan_pt, p_2])\n            _, distance = distance_calculator([tan_pt, p_2])\n            theta = heading - field_tilt*(math.pi/180.)\n            edge_distance = (distance*math.sin(theta))\n            #print('section j {} {}'.format(edge_distance, distance))\n        \n        else: \n            print(\"NO MATCH, EDGE DISTANCE\")\n            print(degrees_func(p_2))\n            \n    #print(distance, [math.degrees(x) for x in p_2], [math.degrees(x) for x in tan_pt])\n            \n    return edge_distance\n\n# p_1[0] =  ((0.3970280742602155*p_1[1]) + 1.262891450830242) # start line equation\n# p_1[0] =  ((0.3954450634384963*p_1[1]) + 1.2609044119293382) # finish line equation\n# p_1[0] =  ((-1.3467496831151728*p_1[1]) - 0.982278119738297) # lower circle equation\n# p_1[0] =  ((-1.3467072620388518*p_1[1]) - 0.9820752870882066) # upper circle equation\n# p_1[0] =  ((0.39623621265256664*p_1[1]) + 1.2618974843227841) # center line\n# p_1[0] =  ((-1.2143077556268673*p_1[1]) - 0.8117806508385371) # starting point line\n# p_1[0] =  ((0.3801731575929377*p_1[1]) + 1.2411901048242218) # limit III\n\n# p_1[0] = ((-1.2143077556268673*p_1[1]) - 0.811780650838537) # Horizontal limit equation\n# p_1[0] = ((0.37538492213489666*p_1[1]) + 1.235025018093613) # axial starting line equation\n# p_1[0] = ((0.3693621621610286*p_1[1]) + 1.2272706243727403) # readjusted axial starting line equation","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.043500Z","iopub.execute_input":"2022-11-01T05:55:13.043974Z","iopub.status.idle":"2022-11-01T05:55:13.061259Z","shell.execute_reply.started":"2022-11-01T05:55:13.043930Z","shell.execute_reply":"2022-11-01T05:55:13.060346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"def effective_distance (region:str, p_1:list, p_2:list, curve_center_1:list, curve_center_2:list, head_diff:float, stride:float, radius:float, field_tilt:float):\n    center1_p1 = [curve_center_1, p_1]\n    center1_p2 = [curve_center_1, p_2]    \n    center2_p1 = [curve_center_2, p_1]\n    center2_p2 = [curve_center_2, p_2]    \n    \n    if (region == 'I'):\n        heading_1, _ = heading_calculator(center1_p1)\n        heading_2, _ = heading_calculator(center1_p2)\n        if ((heading_1>(270.+field_tilt)) and (heading_2<(90.+field_tilt))): # i dont think this has a chance\n            head_diff = (360.-heading_1)+heading_2\n        elif ((heading_2>(270.+field_tilt)) and (heading_1<(90.+field_tilt))): # this has chance\n            head_diff = (360.-heading_2)+heading_1\n        else:\n            head_diff = abs(heading_1-heading_2)\n        eff_distance = (math.pi*radius/180.)*head_diff\n        \n    elif (region == 'III'):\n        heading_1, _ = heading_calculator(center2_p1)\n        heading_2, _ = heading_calculator(center2_p2)\n        if ((heading_1>(270.+field_tilt)) and (heading_2<(90.+field_tilt))): # i dont think this has a chance\n            head_diff = (360.-heading_1)+heading_2\n        elif ((heading_2>(270.+field_tilt)) and (heading_1<(90.+field_tilt))): # this has chance\n            head_diff = (360.-heading_2)+heading_1\n        else:\n            head_diff = abs(heading_1-heading_2)\n        eff_distance = (math.pi*radius/180.)*head_diff\n        \n    elif (region == 'II'): \n        if (p_1[0] > ((-0.26443114502090886*p_1[1]) + 0.36916574415562814)): # upper diameter equation\n            # effective distance for the roll_out part\n            if (p_2[0] >= ((-0.26443114502090886*p_2[1]) + 0.36916574415562814)): # upper diameter equation\n                heading_1, _ = heading_calculator(center1_p1)\n                heading_2, _ = heading_calculator(center1_p2)\n                if ((heading_1>(270.+field_tilt)) and (heading_2<(90.+field_tilt))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.+field_tilt)) and (heading_1<(90.+field_tilt))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n        else:\n            # effective distance for the straight part\n            if (p_2[0] < ((-0.2644464579682782*p_2[1]) + 0.36907043086271923)): # lower diam eq. \n                heading_1, _ = heading_calculator(center2_p1)\n                heading_2, _ = heading_calculator(center2_p2)\n                if ((heading_1>(270.+field_tilt)) and (heading_2<(90.+field_tilt))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.+field_tilt)) and (heading_1<(90.+field_tilt))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius/180.)*head_diff\n            else:                   \n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n    elif (region == 'IV'): \n        if ((p_1[0] < ((-0.2644464579682782*p_1[1]) + 0.36907043086271923)) and (p_2[0] > ((-1.2143077556268673*p_2[1]) - 0.8117806508385371))): # p_2[0]>end of start line # lower diameter equation\n            # effective distance for the roll_out part\n            if (p_2[0] <= ((-0.2644464579682782*p_2[1]) + 0.36907043086271923)):  # lower diameter equation\n                heading_1, _ = heading_calculator(center2_p1)\n                heading_2, _ = heading_calculator(center2_p2)\n                if ((heading_1>(270.+field_tilt)) and (heading_2<(90.+field_tilt))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.+field_tilt)) and (heading_1<(90.+field_tilt))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straight line path\n        elif (p_1[0] >= ((-0.2644464579682782*p_1[1]) + 0.36907043086271923)):\n            # effective distance for the straight part\n            if (p_2[0] > ((-0.26443114502090886*p_2[1]) + 0.36916574415562814)): # upper diam eq., p_1 in IV and p_2 in I\n                heading_1, _ = heading_calculator(center1_p1)\n                heading_2, _ = heading_calculator(center1_p2)\n                if ((heading_1>(270.+field_tilt)) and (heading_2<(90.+field_tilt))): \n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.+field_tilt)) and (heading_1<(90.+field_tilt))): \n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straignt line path\n        else:\n            # starting line condition\n            eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n            \n    return eff_distance","metadata":{"execution":{"iopub.status.busy":"2022-10-15T17:45:34.656614Z","iopub.execute_input":"2022-10-15T17:45:34.657174Z","iopub.status.idle":"2022-10-15T17:45:34.685727Z","shell.execute_reply.started":"2022-10-15T17:45:34.657138Z","shell.execute_reply":"2022-10-15T17:45:34.684774Z"},"jupyter":{"source_hidden":true}}},{"cell_type":"code","source":"const = math.pi/180.\n[[43.06918333*const, -73.77107500*const], [43.06894722*const, -73.77088056*const]]","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.064800Z","iopub.execute_input":"2022-11-01T05:55:13.065139Z","iopub.status.idle":"2022-11-01T05:55:13.079660Z","shell.execute_reply.started":"2022-11-01T05:55:13.065110Z","shell.execute_reply":"2022-11-01T05:55:13.078607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def effective_distance (region:str, p_1:list, p_2:list, curve_center_1:list, curve_center_2:list, head_diff:float, stride:float, radius_1:float, radius_2:float, field_tilt:float):\n    center1_p1 = [curve_center_1, p_1]\n    center1_p2 = [curve_center_1, p_2]    \n    center2_p1 = [curve_center_2, p_1]\n    center2_p2 = [curve_center_2, p_2]    \n    \n    if (region == 'I'):\n        heading_1, _ = heading_calculator(center1_p1)\n        heading_2, _ = heading_calculator(center1_p2)\n        if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n            head_diff = (360.-heading_1)+heading_2\n        elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n            head_diff = (360.-heading_2)+heading_1\n        else:\n            head_diff = abs(heading_1-heading_2)\n        eff_distance = (math.pi*radius_1/180.)*head_diff\n        ang_velocity = (head_diff * math.pi/180.)/0.25 # angular velocity\n        \n    elif (region == 'III'):\n        heading_1, _ = heading_calculator(center2_p1)\n        heading_2, _ = heading_calculator(center2_p2)\n        if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n            head_diff = (360.-heading_1)+heading_2\n        elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n            head_diff = (360.-heading_2)+heading_1\n        else:\n            head_diff = abs(heading_1-heading_2)\n        eff_distance = (math.pi*radius_2/180.)*head_diff\n        ang_velocity = (head_diff * math.pi/180.)/0.25 # angular velocity\n   \n    elif (region == 'II'): \n        if (p_1[0] > ((-1.3467072620388518*p_1[1]) - 0.9820752870882066)): # upper diameter equation\n            # effective distance for the roll_out part\n            if (p_2[0] >= ((-1.3467072620388518*p_2[1]) - 0.9820752870882066)):   # upper diameter equation\n                heading_1, _ = heading_calculator(center1_p1)\n                heading_2, _ = heading_calculator(center1_p2)\n                if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius_1/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n        else:\n            # effective distance for the straight part\n            if (p_2[0] < ((-1.3467496831151728*p_2[1]) - 0.982278119738297)): # lower diam eq. \n                heading_1, _ = heading_calculator(center2_p1)\n                heading_2, _ = heading_calculator(center2_p2)\n                if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius_2/180.)*head_diff\n            else:                   \n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n    elif (region == 'IV'): \n        \n        #if (p_1[0] < ((-1.3467496831151728*p_1[1]) - 0.982278119738297)): # and (p_1[0] > ((-1.2143077556268673*p_1[1]) - 0.8117806508385371))):  # lower diameter equation\n            #if ((p_2[0] <= ((-1.2143077556268673*p_2[1]) - 0.811780650838537)) and (p_2[0] <= ((0.3693621621610286*p_1[1]) + 1.2272706243727403))):\n               # eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n            #elif ()\n            #if (p_2[0] <= ((-1.3467496831151728*p_2[1]) - 0.982278119738297)):  # lower diameter equation\n                #heading_1, _ = heading_calculator(center2_p1)\n                #heading_2, _ = heading_calculator(center2_p2)\n                #if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n                    #head_diff = (360.-heading_1)+heading_2\n                #elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n                    #head_diff = (360.-heading_2)+heading_1\n                #else:\n                    #head_diff = abs(heading_1-heading_2)\n                #eff_distance = (math.pi*radius_2/180.)*head_diff\n            #else:\n                #eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straight line path\n                \n            # this part is already edited. not in use.\n        if (p_1[0] >= ((-1.3467496831151728*p_1[1]) - 0.982278119738297)): # lower diameter eq.\n            # effective distance for the straight part\n            if (p_2[0] > ((-1.3467072620388518*p_2[1]) - 0.9820752870882066)): # upper diam eq., p_1 in IV and p_2 in I\n                heading_1, _ = heading_calculator(center1_p1)\n                heading_2, _ = heading_calculator(center1_p2)\n                if ((heading_1>(270.)) and (heading_2<(90.))): \n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.)) and (heading_1<(90.))): \n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius_1/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straignt line path\n        else:\n            # starting line condition\n            eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n            \n    if ((region!='I') and (region!='III')):\n        ang_velocity = 0.\n        \n    return eff_distance, ang_velocity\n\n# p_1[0] =  ((0.3970280742602155*p_1[1]) + 1.262891450830242) # start line equation\n# p_1[0] =  ((0.3954450634384963*p_1[1]) + 1.2609044119293382) # finish line equation\n# p_1[0] =  ((-1.3467496831151728*p_1[1]) - 0.982278119738297) # lower circle equation\n# p_1[0] =  ((-1.3467072620388518*p_1[1]) - 0.9820752870882066) # upper circle equation\n# p_1[0] =  ((0.39623621265256664*p_1[1]) + 1.2618974843227841) # center line\n# p_1[0] =  ((-1.2143077556268673*p_1[1]) - 0.8117806508385371) # starting point line\n# p_1[0] =  ((0.3801731575929377*p_1[1]) + 1.2411901048242218) # limit III\n# p_1[0] = ((-1.2143077556268673*p_1[1]) - 0.811780650838537) # Horizontal limit equation\n# p_1[0] = ((0.37538492213489666*p_1[1]) + 1.235025018093613) # axial starting line equation \n# p_1[0] = ((0.3693621621610286*p_1[1]) + 1.2272706243727403) # readjusted axial starting line equation","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.080962Z","iopub.execute_input":"2022-11-01T05:55:13.081366Z","iopub.status.idle":"2022-11-01T05:55:13.109263Z","shell.execute_reply.started":"2022-11-01T05:55:13.081333Z","shell.execute_reply":"2022-11-01T05:55:13.108336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prev_heading_diff_calculator (current_heading:float, prev_heading:float, field_tilt:float):\n    if ((current_heading>(270.)) and (prev_heading<(90.))): \n        heading_diff = (360.0-current_heading) + prev_heading\n    elif ((prev_heading>(270.)) and (current_heading<(90.))):\n        heading_diff = -((360.0-prev_heading) + current_heading)\n    else:\n        heading_diff = prev_heading - current_heading\n    return heading_diff","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.110737Z","iopub.execute_input":"2022-11-01T05:55:13.111823Z","iopub.status.idle":"2022-11-01T05:55:13.128227Z","shell.execute_reply.started":"2022-11-01T05:55:13.111774Z","shell.execute_reply":"2022-11-01T05:55:13.127215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking_table.drop_duplicates(inplace=True)\nnyra_tracking_table.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:13.129687Z","iopub.execute_input":"2022-11-01T05:55:13.130694Z","iopub.status.idle":"2022-11-01T05:55:24.654886Z","shell.execute_reply.started":"2022-11-01T05:55:13.130659Z","shell.execute_reply":"2022-11-01T05:55:24.653858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:24.656211Z","iopub.execute_input":"2022-11-01T05:55:24.656637Z","iopub.status.idle":"2022-11-01T05:55:24.782362Z","shell.execute_reply.started":"2022-11-01T05:55:24.656595Z","shell.execute_reply":"2022-11-01T05:55:24.780870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_nyra_tracking_table = nyra_tracking_table.sort_values(['track_id', 'race_date', 'race_number', 'trakus_index', 'program_number'])","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:24.784239Z","iopub.execute_input":"2022-11-01T05:55:24.784766Z","iopub.status.idle":"2022-11-01T05:55:28.484505Z","shell.execute_reply.started":"2022-11-01T05:55:24.784709Z","shell.execute_reply":"2022-11-01T05:55:28.483429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_nyra_tracking_table[sorted_nyra_tracking_table['track_id']=='AQU']","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:28.485616Z","iopub.execute_input":"2022-11-01T05:55:28.485927Z","iopub.status.idle":"2022-11-01T05:55:29.032231Z","shell.execute_reply.started":"2022-11-01T05:55:28.485899Z","shell.execute_reply":"2022-11-01T05:55:29.031195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"sorted_all_tables = pd.merge(sorted_nyra_tracking_table, nyra_race_table[['track_id','race_date', 'race_number',  how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\nsorted_all_tables = pd.merge(sorted_all_tables, nyra_start_table,  how='left', left_on=['track_id','race_date', 'race_number', 'program_number'], right_on = ['track_id','race_date', 'race_number', 'program_number'])","metadata":{"execution":{"iopub.status.busy":"2022-10-07T07:07:41.118304Z","iopub.execute_input":"2022-10-07T07:07:41.118703Z","iopub.status.idle":"2022-10-07T07:07:48.069591Z","shell.execute_reply.started":"2022-10-07T07:07:41.118670Z","shell.execute_reply":"2022-10-07T07:07:48.068708Z"}}},{"cell_type":"raw","source":"sorted_all_tables","metadata":{"execution":{"iopub.status.busy":"2022-10-07T07:07:49.901411Z","iopub.execute_input":"2022-10-07T07:07:49.901852Z","iopub.status.idle":"2022-10-07T07:07:49.932534Z","shell.execute_reply.started":"2022-10-07T07:07:49.901818Z","shell.execute_reply":"2022-10-07T07:07:49.931454Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"sorted_all_tables = pd.merge(sorted_nyra_tracking_table, nyra_race_table[['track_id','race_date', 'race_number', 'course_type']], how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\nsorted_all_tables","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:29.034099Z","iopub.execute_input":"2022-11-01T05:55:29.034567Z","iopub.status.idle":"2022-11-01T05:55:31.352922Z","shell.execute_reply.started":"2022-11-01T05:55:29.034523Z","shell.execute_reply":"2022-11-01T05:55:31.351671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"heading_calculator([[40.67457222*const,-73.82721111*const], [40.67545556*const,-73.82944167*const]])","metadata":{"execution":{"iopub.status.busy":"2022-10-06T11:46:00.159030Z","iopub.execute_input":"2022-10-06T11:46:00.159959Z","iopub.status.idle":"2022-10-06T11:46:00.169315Z","shell.execute_reply.started":"2022-10-06T11:46:00.159912Z","shell.execute_reply":"2022-10-06T11:46:00.167984Z"}}},{"cell_type":"raw","source":"const = math.pi/180.\n_, distance = distance_calculator([[40.67458056*const, -73.82760278*const], [40.67523889*const, -73.82913611*const]])\n_, heading = heading_calculator([[40.67458056*const, -73.82760278*const], [40.67523889*const, -73.82913611*const]])\nimport math\n\nR = 20.902e6 # radius of the earth\nbrng = heading #Bearing is 90 degrees converted to radians.\nd = distance/2 #Distance in km\n\n#lat2  52.20444 - the lat result I'm hoping for\n#lon2  0.36056 - the long result I'm hoping for.\n\nlat1 = 40.67458056*const #Current lat point converted to radians\nlon1 = -73.82760278*const #Current long point converted to radians\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\nprint(\"lat lon of mid point is {} {}\".format(math.degrees(lat2), math.degrees(lon2)))\n########\n\nheading_2 = heading-math.pi/2.\nbrng = heading_2\nd = 240.792\nlat1 = lat2\nlon1 = lon2\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\np_b = [[40.67458056*const, -73.82760278*const], [lat2,lon2]]\np_c = [[40.67523889*const, -73.82913611*const], [lat2,lon2]]\n_, distance2 = distance_calculator(p_b)\n_, distance3 = distance_calculator(p_c)\n\nprint(distance2, distance3)\nlat2 = math.degrees(lat2)\nlon2 = math.degrees(lon2)\n\n#342.6313269375081\nprint(lat2)\nprint(lon2)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-10T16:24:18.169579Z","iopub.execute_input":"2022-10-10T16:24:18.170380Z","iopub.status.idle":"2022-10-10T16:24:18.186029Z","shell.execute_reply.started":"2022-10-10T16:24:18.170336Z","shell.execute_reply":"2022-10-10T16:24:18.184783Z"}}},{"cell_type":"raw","source":"R = 20.902e6 # radius of the earth\nbrng = (math.pi/180.)* (62.803504620707805) #Bearing is 90 degrees converted to radians.\nd = 1000. #Distance in km\n\n#lat2  52.20444 - the lat result I'm hoping for\n#lon2  0.36056 - the long result I'm hoping for.\n\nlat1 = 43.06915833*math.pi/180 #Current lat point converted to radians\nlon1 = -73.77111111*math.pi/180 #Current long point converted to radians\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\nprint([[math.degrees(lat2), math.degrees(lon2)], [lat2, lon2]], lat1, lon1)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:04:35.065394Z","iopub.execute_input":"2022-10-21T18:04:35.066208Z","iopub.status.idle":"2022-10-21T18:04:35.079774Z","shell.execute_reply.started":"2022-10-21T18:04:35.066136Z","shell.execute_reply":"2022-10-21T18:04:35.078530Z"}}},{"cell_type":"raw","source":"const = math.pi/180.\nheading_calculator([[0.7098151331223282, -1.2885626254760563], [0.7516927041749404, -1.2875645382074898]])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:04:35.081407Z","iopub.execute_input":"2022-10-21T18:04:35.081889Z","iopub.status.idle":"2022-10-21T18:04:35.095644Z","shell.execute_reply.started":"2022-10-21T18:04:35.081843Z","shell.execute_reply":"2022-10-21T18:04:35.094588Z"}}},{"cell_type":"raw","source":"heading_calculator([[43.07091111*const, -73.77156111*const], [43.07150833*const, -73.77137500*const]])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:04:35.097127Z","iopub.execute_input":"2022-10-21T18:04:35.097502Z","iopub.status.idle":"2022-10-21T18:04:35.111644Z","shell.execute_reply.started":"2022-10-21T18:04:35.097472Z","shell.execute_reply":"2022-10-21T18:04:35.110382Z"}}},{"cell_type":"raw","source":"math.atan((470-468.76)/1477.1211766199403)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T09:42:59.273314Z","iopub.execute_input":"2022-10-21T09:42:59.273729Z","iopub.status.idle":"2022-10-21T09:42:59.280893Z","shell.execute_reply.started":"2022-10-21T09:42:59.273694Z","shell.execute_reply":"2022-10-21T09:42:59.279495Z"}}},{"cell_type":"raw","source":"center_1 = [43.07259444, -73.76611389], [0.7517585903542032, -1.2874615637816222]\ncenter_2 = [43.07066389, -73.77098611], [0.7517248959003288, -1.2875466000625035]\nradius_1 = 468.76\nradius_2 = 470.\nheading from center 2 to 1 = (61.523446067406084, 1.0737867010493933) 0.0008394705379263456\nheading start line = (61.47592139144653, 1.0729572389779558)\nheading finish line = (241.57172692823565, 4.21622201462636)\ndistance bn center points = (450.2315097237413, 1477.1211766199403)\nstart_line = [[43.06915833, -73.77111111], [43.06881944, -73.77201389]]\nheading_start_line = (62.803504620707805, 1.0961279374228237) # starting pt\nfield_tilt_IV = (61.47592139144653, 1.0729572389779558)\nfield_tilt_II = (61.570972483809896, 1.0746161934973137)\ntangent_point_IV_1 = [[43.071464438507235, -73.76527650290062], [0.7517388681076019, -1.2874469486196243]]\ntangent_point_IV_2 = [[43.06953089933251, -73.77014653426635], [0.7517051214827868, -1.2875319467016315]]\ntangent_point_II_1 = [[43.07372340674531, -73.76695390399716], [0.7517782945410543, -1.2874762247916356]]\ntangent_point_II_2 = [[43.071795843167024, -73.77182831957722], [0.7517446522100717, -1.2875612993926182]]\np_1[0] =  ((0.3970280742602155*p_1[1]) + 1.262891450830242) # start line equation\np_1[0] =  ((0.3954450634384963*p_1[1]) + 1.2609044119293382) # finish line equation\np_1[0] =  ((-1.3467496831151728*p_1[1]) - 0.982278119738297) # lower circle equation\np_1[0] =  ((-1.3467072620388518*p_1[1]) - 0.9820752870882066) # upper circle equation\np_1[0] =  ((0.39623621265256664*p_1[1]) + 1.2618974843227841) # center line\np_1[0] =  ((-1.2143077556268673*p_1[1]) - 0.8117806508385371) # starting point line\np_1[0] =  ((0.3801731575929377*p_1[1]) + 1.2411901048242218) # limit III\n\np_1[0] = ((-1.2143077556268673*p_1[1]) - 0.811780650838537) # Horizontal limit equation\np_1[0] = ((0.37538492213489666*p_1[1]) + 1.235025018093613) # axial starting line equation\np_1[0] = ((0.3693621621610286*p_1[1]) + 1.2272706243727403) # readjusted axial starting line equation","metadata":{"execution":{"iopub.status.busy":"2022-10-06T14:03:49.135313Z","iopub.execute_input":"2022-10-06T14:03:49.135808Z","iopub.status.idle":"2022-10-06T14:03:49.144129Z","shell.execute_reply.started":"2022-10-06T14:03:49.135769Z","shell.execute_reply":"2022-10-06T14:03:49.142848Z"}}},{"cell_type":"code","source":"const = math.pi/180.\n[43.06918333*const, -73.77107500*const], [43.06894722*const, -73.77088056*const]","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:31.354212Z","iopub.execute_input":"2022-11-01T05:55:31.354973Z","iopub.status.idle":"2022-11-01T05:55:31.362612Z","shell.execute_reply.started":"2022-11-01T05:55:31.354937Z","shell.execute_reply":"2022-11-01T05:55:31.361691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pivot_df (df:pd.DataFrame):\n    idxs = []\n    new_tr_idx = []\n    tr_idx = list(df.trakus_index)\n    for x in tr_idx:\n        #print(x)\n        a, b = x.split('_')\n        idxs.append(int(a))\n        idxs.append(int(b))\n    max_idx = max(idxs)\n    min_idx = min(idxs)\n    for i in range(min_idx, max_idx):\n        new_tr_idx.append(\"{}_{}\".format(i, i+1))\n    #new_tr_idx\n    df = df.pivot('trakus_index', 'program_number')\n    df = df.reindex(new_tr_idx)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:31.364498Z","iopub.execute_input":"2022-11-01T05:55:31.365330Z","iopub.status.idle":"2022-11-01T05:55:31.376619Z","shell.execute_reply.started":"2022-11-01T05:55:31.365286Z","shell.execute_reply":"2022-11-01T05:55:31.375244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"# before u run it check for index i of p_'s and tr_idx\n# assumption abt the stride!\nSAR_D_df = sorted_all_tables[sorted_all_tables['track_id']=='SAR'][sorted_all_tables['course_type']=='D']\nunique_track_ids = list(SAR_D_df.track_id.unique())\ntracking_path = \"./tracking_2\"\ntracking_path_1 = \"./tracking_pvt\"\nif not os.path.exists(tracking_path):\n    os.makedirs(tracking_path)\nif not os.path.exists(tracking_path_1):\n    os.makedirs(tracking_path_1)\n#gen_data = pd.DataFrame()\ncount = 0\n\nfor tr_id in unique_track_ids:\n    track_id_df = SAR_D_df[SAR_D_df['track_id']==tr_id]\n    unique_race_date = list(track_id_df.race_date.unique())\n    for rc_date in unique_race_date:\n        race_date_df = track_id_df[track_id_df['race_date']==rc_date]\n        unique_race_number = list(race_date_df.race_number.unique())        \n        for rc_no in unique_race_number: #[:math.floor(len(unique_race_date)/2)]:\n            race_number_df = race_date_df[race_date_df['race_number']==rc_no]            \n            time = 0.25\n            gen_data = pd.DataFrame()\n            unique_program_number = list(race_number_df.program_number.unique())  \n            \n            for pg_no in unique_program_number:\n                program_number_df = race_number_df[race_number_df['program_number']==pg_no].sort_values('trakus_index', ascending=True)\n                len_df = len(program_number_df)\n                for i in range(len_df-1): #range(1:df) and change p_1 and p_2.\n                    p_1 = [program_number_df.iloc[i, program_number_df.columns.get_loc('lat_radian')], program_number_df.iloc[i, program_number_df.columns.get_loc('long_radian')]]\n                    p_2 = [program_number_df.iloc[i+1, program_number_df.columns.get_loc('lat_radian')], program_number_df.iloc[i+1, program_number_df.columns.get_loc('long_radian')]]\n                    \n                    distance_m, distance_ft = distance_calculator([p_1, p_2]) # in meters, in ft\n                    heading_deg, heading_rad = heading_calculator([p_1, p_2]) # in deg, in rad\n                    if (np.isnan(heading_deg)):\n                        print(\"heading is nan at {} {} {} {} {}\".format(rc_date, rc_no, pg_no, p_1, p_2))\n                    const = math.pi/180.                    \n                    radius_1 = 468.76 #feet\n                    radius_2 = 470. #feet                \n                    field_tilt_center = 61.523446067406084\n                    field_tilt_V = 61.47592139144653 # in degrees\n                    field_tilt_IV = 61.47592139144653 # in degrees\n                    field_tilt_II = 61.570972483809896 # in degrees\n                    tan_pt_II = [0.7517782945410543, -1.2874762247916356]\n                    tan_pt_IV = [0.7517051214827868, -1.2875319467016315]\n                    tan_pt_V = [0.7516927041749404, -1.2875645382074898]\n                    #tan_pt_II_2 = [0.7098383663083734, -1.2886084816063457]\n                    \n                    #heading from center 2 to 1 = (19.78284456311878, 0.3452757730366819)\n                    curve_center_1 = [43.07259444*const, -73.76611389*const] #center points in radian\n                    curve_center_2 = [43.07066389*const, -73.77098611*const]                \n                    \n                    tr_idx_1 = program_number_df.iloc[i, program_number_df.columns.get_loc('trakus_index')]\n                    tr_idx_2 = program_number_df.iloc[i+1, program_number_df.columns.get_loc('trakus_index')]\n                    tr_idx = \"{}_{}\".format(tr_idx_1, tr_idx_2)\n                    \n                    if (p_1[0] >= ((-1.3467072620388518*p_1[1]) - 0.9820752870882066)): # upper diameter equation\n                        if (p_1[0] < ((0.3954450634384963*p_1[1]) + 1.2609044119293382)): #finish line equation\n                            #Region I\n                            heading_diff = Heading_diff_track (region='I', heading=heading_deg, field_tilt=field_tilt_center, curve_center=curve_center_1, pt_1=p_1)\n                            edge_distance = Distance_from_edge (region='I', radius=radius_1, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_1)\n                            cum_dist1 = effective_distance (region='I', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                            region='I'\n                        else:\n                            #region II\n                            heading_diff = Heading_diff_track (region='II', heading=heading_deg, field_tilt=field_tilt_center)\n                            edge_distance = Distance_from_edge (region='II', radius=radius_1, field_tilt=field_tilt_II, p_2=p_2, tan_pt=tan_pt_II, curve_center=curve_center_1)\n                            cum_dist1 = effective_distance (region='II', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_II)\n                            region='II'\n                    elif (p_1[0] <= ((-1.3467496831151728*p_1[1]) - 0.982278119738297)): # lower diameter equation\n                        if (p_1[0] > ((0.3970280742602155*p_1[1]) + 1.262891450830242)): # start line equation\n                            #Region III\n                            heading_diff = Heading_diff_track (region='III', heading=heading_deg, field_tilt=field_tilt_center, curve_center=curve_center_2, pt_1=p_1)\n                            edge_distance = Distance_from_edge (region='III', radius=radius_2, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_2)\n                            cum_dist1 = effective_distance (region='III', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                            region='III'\n                        elif (p_1[0] < ((-1.2143077556268673*p_1[1]) - 0.8117806508385371)): #starting pt\n                            # this is just to handle the heading diff.\n                            heading_diff = Heading_diff_track (region='V', heading=heading_deg, field_tilt=field_tilt_center)\n                            edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_V, p_2=p_2, tan_pt=tan_pt_V, curve_center=curve_center_2)\n                            cum_dist1 = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_V)\n                            region='IV'\n                        else:\n                            #region IV\n                            heading_diff = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                            edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                            cum_dist1 = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                            region='IV'\n                    elif (p_1[0] > ((0.39623621265256664*p_1[1]) + 1.2618974843227841)): #center line equation\n                        # region II\n                        heading_diff = Heading_diff_track (region='II', heading=heading_deg, field_tilt=field_tilt_center)\n                        edge_distance = Distance_from_edge (region='II', radius=radius_1, field_tilt=field_tilt_II, p_2=p_2, tan_pt=tan_pt_II, curve_center=curve_center_1)\n                        cum_dist1 = effective_distance (region='II', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_II)\n                        region='II'\n                    elif (p_1[0] < ((0.39623621265256664*p_1[1]) + 1.2618974843227841)): #center line equation\n                        # region IV  \n                        heading_diff = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                        edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                        cum_dist1 = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                        region='IV' \n                                \n                    if (i == 0):\n                        if (distance_ft == 0.):\n                            cum_dist = 0.\n                            heading_deg = 0.\n                            heading_diff = 0.\n                        else:\n                            cum_dist = cum_dist1 # this shall be a line or curve!                            \n                        uncor_cum_dist = distance_ft\n                        prev_heading_diff = 0.\n                        \n                    elif (i > 0):     \n                        if (distance_ft == 0.):\n                            cum_dist = gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                            heading_deg = 0.\n                            heading_diff = np.nan\n                            uncor_cum_dist = gen_data.iloc[-1, gen_data.columns.get_loc('uncor_cum_distance')]\n                            prev_heading_diff = np.nan\n                        else:\n                            cum_dist = cum_dist1 + gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                            uncor_cum_dist = distance_ft + gen_data.iloc[-1, gen_data.columns.get_loc('uncor_cum_distance')]\n                            prev_heading_diff = prev_heading_diff_calculator (heading_deg, gen_data.iloc[-1, gen_data.columns.get_loc('heading')], field_tilt=field_tilt_center)\n\n                    velocity_horizontal = distance_ft/0.25\n                    try:\n                        one_foot_speed = 0.25/distance_ft\n                    except ZeroDivisionError:\n                        one_foot_speed = 0.\n                    stride = distance_ft                     \n                    \n                    row_to_app = {'region':region, 'track_id':tr_id, 'race_date':rc_date, 'race_number':rc_no, 'program_number':pg_no, 'stride':stride, 'velocity_horizontal':velocity_horizontal, 'one_foot_speed':one_foot_speed,                                   \n                                   'cum_distance':cum_dist, 'uncor_cum_distance':uncor_cum_dist, 'heading': heading_deg, 'track_heading_diff':heading_diff, 'prev_heading_diff':prev_heading_diff, 'edge_distance':edge_distance, 'trakus_index':tr_idx}\n                    gen_data = gen_data.append(row_to_app, ignore_index=True)\n                    \n                    if ((edge_distance<0.) or (heading_diff>20.)):\n                        print(\"details are: {} {} {} {} {}\".format(tr_id, rc_date, rc_no, pg_no, tr_idx))\n                        print(\"first point is: {}\".format(p_1))\n                        print(\"second point is: {}\".format(p_2))\n                        print(\"current heading is: {}\".format(heading_deg))\n                        print(\"heading_diff is: {}\".format(heading_diff))\n                        print(\"edge_distance is: {}\".format(edge_distance))\n                        print(\"region is: {} \\n\".format(region))\n                \n            \n            file_path = os.path.join(tracking_path, \"{}-{}-{}.csv\".format(tr_id, rc_date, rc_no))\n            gen_data.to_csv(file_path, index=False) # ??pivot it before writing it.\n        #break\n    #break\n           ","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:57:25.988589Z","iopub.execute_input":"2022-10-21T14:57:25.988996Z","iopub.status.idle":"2022-10-21T14:57:26.054096Z","shell.execute_reply.started":"2022-10-21T14:57:25.988965Z","shell.execute_reply":"2022-10-21T14:57:26.052030Z"},"jupyter":{"source_hidden":true}}},{"cell_type":"code","source":"def degrees_func (list_obj:list):\n    list_obj = [math.degrees(x) for x in list_obj]\n    return list_obj","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:31.380631Z","iopub.execute_input":"2022-11-01T05:55:31.381157Z","iopub.status.idle":"2022-11-01T05:55:31.394511Z","shell.execute_reply.started":"2022-11-01T05:55:31.381123Z","shell.execute_reply":"2022-11-01T05:55:31.393733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# before u run it check for index i of p_'s and tr_idx\n# assumption abt the stride!\nSAR_D_df = sorted_all_tables[sorted_all_tables['track_id']=='SAR'][sorted_all_tables['course_type']=='D']\nunique_track_ids = list(SAR_D_df.track_id.unique())\ntracking_path = \"./tracking_2\"\ntracking_path_1 = \"./tracking_pvt\"\nif not os.path.exists(tracking_path):\n    os.makedirs(tracking_path)\nif not os.path.exists(tracking_path_1):\n    os.makedirs(tracking_path_1)\n#gen_data = pd.DataFrame()\ncount = 0\n\nfor tr_id in unique_track_ids:\n    track_id_df = SAR_D_df[SAR_D_df['track_id']==tr_id]\n    unique_race_date = list(track_id_df.race_date.unique())\n    for rc_date in unique_race_date:\n        race_date_df = track_id_df[track_id_df['race_date']==rc_date]\n        unique_race_number = list(race_date_df.race_number.unique())        \n        for rc_no in unique_race_number: #[:math.floor(len(unique_race_date)/2)]:\n            race_number_df = race_date_df[race_date_df['race_number']==rc_no]            \n            time = 0.25\n            gen_data = pd.DataFrame()\n            unique_program_number = list(race_number_df.program_number.unique())  \n            \n            for pg_no in unique_program_number:\n                program_number_df = race_number_df[race_number_df['program_number']==pg_no].sort_values('trakus_index', ascending=True)\n                len_df = len(program_number_df)\n                for i in range(len_df-1): #range(1:df) and change p_1 and p_2.\n                    p_1 = [program_number_df.iloc[i, program_number_df.columns.get_loc('lat_radian')], program_number_df.iloc[i, program_number_df.columns.get_loc('long_radian')]]\n                    p_2 = [program_number_df.iloc[i+1, program_number_df.columns.get_loc('lat_radian')], program_number_df.iloc[i+1, program_number_df.columns.get_loc('long_radian')]]\n                    \n                    distance_m, distance_ft = distance_calculator([p_1, p_2]) # in meters, in ft\n                    heading_deg, heading_rad = heading_calculator([p_1, p_2]) # in deg, in rad\n                    if (np.isnan(heading_deg)):\n                        print(\"heading is nan at {} {} {} {} {}\".format(rc_date, rc_no, pg_no, p_1, p_2))\n                    const = math.pi/180.                    \n                    radius_1 = 468.76 #feet\n                    radius_2 = 470. #feet                \n                    field_tilt_center = 61.523446067406084\n                    field_tilt_V = 61.47592139144653 # in degrees\n                    field_tilt_IV = 61.47592139144653 # in degrees\n                    field_tilt_II = 61.570972483809896 # in degrees\n                    tan_pt_II = [0.7517782945410543, -1.2874762247916356]\n                    tan_pt_IV = [0.7517051214827868, -1.2875319467016315]\n                    tan_pt_V = [43.06875833*const, -73.77218333*const]\n                    #tan_pt_II_2 = [0.7098383663083734, -1.2886084816063457]\n                    \n                    #heading from center 2 to 1 = (19.78284456311878, 0.3452757730366819)\n                    curve_center_1 = [43.07259444*const, -73.76611389*const] #center points in radian\n                    curve_center_2 = [43.07066389*const, -73.77098611*const]                \n                    \n                    tr_idx_1 = program_number_df.iloc[i, program_number_df.columns.get_loc('trakus_index')]\n                    tr_idx_2 = program_number_df.iloc[i+1, program_number_df.columns.get_loc('trakus_index')]\n                    tr_idx = \"{}_{}\".format(tr_idx_1, tr_idx_2)\n                    \n                    if (p_1[0] >= ((-1.3467072620388518*p_1[1]) - 0.9820752870882066)): # upper diameter equation\n                        #if (p_1[0] < ((0.3954450634384963*p_1[1]) + 1.2609044119293382)): #finish line equation\n                        #Region I\n                        heading_diff, beta, center_p2_efficient = Heading_diff_track (region='I', heading=heading_deg, field_tilt=field_tilt_center, stride=distance_ft, curve_center=curve_center_1, pt_1=p_1)\n                        edge_distance = Distance_from_edge (region='I', radius=radius_1, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_1)\n                        cum_dist1, ang_velocity = effective_distance (region='I', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                        region='I'\n                        _, center_to_pt = distance_calculator([curve_center_1, p_1])\n                        #else:\n                            #region II\n                            #heading_diff = Heading_diff_track (region='II', heading=heading_deg, field_tilt=field_tilt_center)\n                            #edge_distance = Distance_from_edge (region='II', radius=radius_1, field_tilt=field_tilt_II, p_2=p_2, tan_pt=tan_pt_II, curve_center=curve_center_1)\n                            #cum_dist1 = effective_distance (region='II', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_II)\n                            #region='II'\n                            \n                            # p_1[0] =  ((0.3970280742602155*p_1[1]) + 1.262891450830242) # start line equation\n                            # p_1[0] =  ((0.3954450634384963*p_1[1]) + 1.2609044119293382) # finish line equation\n                            # p_1[0] =  ((-1.3467496831151728*p_1[1]) - 0.982278119738297) # lower circle equation\n                            # p_1[0] =  ((-1.3467072620388518*p_1[1]) - 0.9820752870882066) # upper circle equation\n                            # p_1[0] =  ((0.39623621265256664*p_1[1]) + 1.2618974843227841) # center line\n                            # p_1[0] =  ((-1.2143077556268673*p_1[1]) - 0.8117806508385371) # starting point line\n                            # p_1[0] =  ((0.3801731575929377*p_1[1]) + 1.2411901048242218) # limit III\n                            # p_1[0] = ((-1.2143077556268673*p_1[1]) - 0.811780650838537) # Horizontal limit equation\n                            # p_1[0] = ((0.37538492213489666*p_1[1]) + 1.235025018093613) # axial starting line equation \n                            # p_1[0] = ((0.3693621621610286*p_1[1]) + 1.2272706243727403) # readjusted axial starting line equation\n\n                    elif (p_1[0] <= ((-1.3467496831151728*p_1[1]) - 0.982278119738297)): # lower diameter equation\n                        if (i>0):\n                            last_region = gen_data.iloc[-1, gen_data.columns.get_loc('region')]\n                        else:\n                            last_region = 'first'\n                        region_proposal = False\n                        if (last_region=='IV'):\n                            region_proposal = True\n                        \n                        if ((p_2[0] <= ((-1.2143077556268673*p_2[1]) - 0.811780650838537)) and (p_2[0] <= ((0.3693621621610286*p_1[1]) + 1.2272706243727403))): # horizontal and axial limits\n                            # this is just to handle the heading diff.\n                            heading_diff, beta, center_p2_efficient = Heading_diff_track (region='V', heading=heading_deg, field_tilt=field_tilt_center)\n                            edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_V, p_2=p_2, tan_pt=tan_pt_V, curve_center=curve_center_2)\n                            cum_dist1, ang_velocity = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_V)\n                            region='IV'\n                            center_to_pt = 0.\n                            \n                        elif (region_proposal or (i<30)):\n                            #region IV\n                            heading_diff, beta, center_p2_efficient = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                            edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                            cum_dist1, ang_velocity = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                            region='IV'\n                            center_to_pt = 0.\n                            \n                        else: # (p_1[0] > ((0.3801731575929377*p_1[1]) + 1.2411901048242218)):  # limit III                            \n                            #Region III\n                            heading_diff, beta, center_p2_efficient = Heading_diff_track (region='III', heading=heading_deg, field_tilt=field_tilt_center, stride=distance_ft, curve_center=curve_center_2, pt_1=p_1)\n                            edge_distance = Distance_from_edge (region='III', radius=radius_2, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_2)\n                            cum_dist1, ang_velocity = effective_distance (region='III', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                            region='III'\n                            _, center_to_pt = distance_calculator([curve_center_2, p_1])\n                        #else:\n                            #region IV\n                            #heading_diff = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                            #edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                            #cum_dist1 = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                            #region='IV'\n                    elif (p_1[0] > ((0.39623621265256664*p_1[1]) + 1.2618974843227841)): #center line equation\n                        # region II\n                        heading_diff, beta, center_p2_efficient = Heading_diff_track (region='II', heading=heading_deg, field_tilt=field_tilt_center)\n                        edge_distance = Distance_from_edge (region='II', radius=radius_1, field_tilt=field_tilt_II, p_2=p_2, tan_pt=tan_pt_II, curve_center=curve_center_1)\n                        cum_dist1, ang_velocity = effective_distance (region='II', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_II)\n                        region='II'\n                        center_to_pt = 0.\n                    elif (p_1[0] < ((0.39623621265256664*p_1[1]) + 1.2618974843227841)): #center line equation\n                        # region IV  \n                        heading_diff, beta, center_p2_efficient = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                        edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                        cum_dist1, ang_velocity = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                        region='IV' \n                        center_to_pt = 0.\n                                \n                    if (i == 0):\n                        if (distance_ft == 0.):\n                            cum_dist = 0.\n                            heading_deg = 0.\n                            heading_diff = 0.\n                        else:\n                            cum_dist = cum_dist1 # this shall be a line or curve!                            \n                        uncor_cum_dist = distance_ft\n                        prev_heading_diff = 0.\n                        \n                    elif (i > 0):     \n                        if (distance_ft == 0.):\n                            cum_dist = gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                            heading_deg = 0.\n                            heading_diff = np.nan\n                            uncor_cum_dist = gen_data.iloc[-1, gen_data.columns.get_loc('uncor_cum_distance')]\n                            prev_heading_diff = np.nan\n                        else:\n                            cum_dist = cum_dist1 + gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                            uncor_cum_dist = distance_ft + gen_data.iloc[-1, gen_data.columns.get_loc('uncor_cum_distance')]\n                            prev_heading_diff = prev_heading_diff_calculator (heading_deg, gen_data.iloc[-1, gen_data.columns.get_loc('heading')], field_tilt=field_tilt_center)\n\n                    velocity_horizontal = distance_ft/0.25\n                    try:\n                        one_foot_speed = 0.25/distance_ft\n                    except ZeroDivisionError:\n                        one_foot_speed = 0.\n                    stride = distance_ft                     \n                    \n                    row_to_app = {'region':region, 'track_id':tr_id, 'race_date':rc_date, 'race_number':rc_no, 'program_number':pg_no, 'stride':stride, 'velocity_horizontal':velocity_horizontal, 'one_foot_speed':one_foot_speed, 'ang_velocity':ang_velocity, 'cum_distance':cum_dist, 'uncor_cum_distance':uncor_cum_dist,                                 \n                                   'center_to_pt1':center_to_pt, 'center_pt2_efficient':center_p2_efficient, 'heading': heading_deg, 'track_heading_diff':heading_diff, 'beta':beta, 'prev_heading_diff':prev_heading_diff, 'edge_distance':edge_distance, 'trakus_index':tr_idx}\n                    gen_data = gen_data.append(row_to_app, ignore_index=True)\n                    \n                    if ((edge_distance<0.) or (heading_diff>20.)):\n                        print(\"details are: {} {} {} {} {}\".format(tr_id, rc_date, rc_no, pg_no, tr_idx))\n                        print(\"first point is: {}\".format(degrees_func(p_1)))\n                        print(\"second point is: {}\".format(degrees_func(p_2)))\n                        print(\"current heading is: {}\".format(heading_deg))\n                        print(\"heading_diff is: {}\".format(heading_diff))\n                        print(\"edge_distance is: {}\".format(edge_distance))\n                        print(\"region is: {} \\n\".format(region))\n                \n            \n            file_path = os.path.join(tracking_path, \"{}-{}-{}.csv\".format(tr_id, rc_date, rc_no))\n            gen_data.to_csv(file_path, index=False) # ??pivot it before writing it.\n        #break\n    #break\n           ","metadata":{"execution":{"iopub.status.busy":"2022-11-01T05:55:31.396002Z","iopub.execute_input":"2022-11-01T05:55:31.396549Z","iopub.status.idle":"2022-11-01T06:33:34.560764Z","shell.execute_reply.started":"2022-11-01T05:55:31.396518Z","shell.execute_reply":"2022-11-01T06:33:34.559456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"const = math.pi/180.\n[[43.06884167*const, -73.77200000*const], [43.06918333*const, -73.77107500*const]]","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:33:34.562102Z","iopub.status.idle":"2022-11-01T06:33:34.562823Z","shell.execute_reply.started":"2022-11-01T06:33:34.562596Z","shell.execute_reply":"2022-11-01T06:33:34.562618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"[0.7517630100321594*const, -1.2875208372177296*const]\n[0.7517630427778662*const, -1.2875207763658199*const]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:04:35.181284Z","iopub.status.idle":"2022-10-21T18:04:35.181825Z","shell.execute_reply.started":"2022-10-21T18:04:35.181618Z","shell.execute_reply":"2022-10-21T18:04:35.181640Z"}}},{"cell_type":"raw","source":"const = math.pi/180.\nheading_calculator([[43.06875833*const, -73.77218333*const], [43.06860278*const, -73.77202500*const]])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T18:04:35.183833Z","iopub.status.idle":"2022-10-21T18:04:35.184277Z","shell.execute_reply.started":"2022-10-21T18:04:35.184053Z","shell.execute_reply":"2022-10-21T18:04:35.184072Z"}}},{"cell_type":"raw","source":"import shutil\nshutil.rmtree('./tracking_pvt')\nshutil.rmtree('./tracking_2')\nshutil.rmtree(\"./tracking_complete\")","metadata":{"execution":{"iopub.status.busy":"2022-10-10T14:58:49.751074Z","iopub.execute_input":"2022-10-10T14:58:49.751579Z","iopub.status.idle":"2022-10-10T14:58:49.779502Z","shell.execute_reply.started":"2022-10-10T14:58:49.751537Z","shell.execute_reply":"2022-10-10T14:58:49.777832Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"# pivoting\nfor x in os.listdir('./tracking_2'):\n    x_path = os.path.join('./tracking_2', x)\n    x_save = os.path.join('./tracking_pvt', x)\n    df = pd.read_csv(x_path, parse_dates=True)\n    df.race_number = [int(x) for x in df.race_number]\n    df.program_number = [str(x) for x in df.program_number]\n    df = pd.merge(df, nyra_race_table, how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\n    df = pd.merge(df, nyra_start_table[['track_id', 'race_date', 'race_number', 'program_number', 'position_at_finish']],  how='left', left_on=['track_id', 'race_date', 'race_number', 'program_number'], right_on = ['track_id', 'race_date', 'race_number', 'program_number'])\n    df.distance_id = [(x*660) for x in df.distance_id]\n    df = pivot_df (df)\n    df.to_csv(x_save)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:33:34.563960Z","iopub.status.idle":"2022-11-01T06:33:34.564596Z","shell.execute_reply.started":"2022-11-01T06:33:34.564359Z","shell.execute_reply":"2022-11-01T06:33:34.564392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lets create complete tables for each race.\ntracking_path_2 = \"./tracking_complete\"\nif not os.path.exists(tracking_path_2):\n    os.makedirs(tracking_path_2)\nfor x in os.listdir('./tracking_2'):\n    x_path = os.path.join('./tracking_2', x)\n    x_save = os.path.join('./tracking_complete', x)\n    df = pd.read_csv(x_path, parse_dates=True)\n    df.race_number = [int(x) for x in df.race_number]\n    df.program_number = [str(x) for x in df.program_number]\n    df = pd.merge(df, nyra_race_table, how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\n    df = pd.merge(df, nyra_start_table,  how='left', left_on=['track_id', 'race_date', 'race_number', 'program_number'], right_on = ['track_id', 'race_date', 'race_number', 'program_number'])    \n    df.distance_id = [(x*660) for x in df.distance_id]\n    df.to_csv(x_save, index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-01T06:33:34.565724Z","iopub.status.idle":"2022-11-01T06:33:34.566284Z","shell.execute_reply.started":"2022-11-01T06:33:34.566095Z","shell.execute_reply":"2022-11-01T06:33:34.566114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"os.listdir('./tracking_2')","metadata":{"execution":{"iopub.status.busy":"2022-10-10T15:03:21.980204Z","iopub.execute_input":"2022-10-10T15:03:21.981154Z","iopub.status.idle":"2022-10-10T15:03:21.988932Z","shell.execute_reply.started":"2022-10-10T15:03:21.981103Z","shell.execute_reply":"2022-10-10T15:03:21.987721Z"}}},{"cell_type":"raw","source":"file_list = []\n\nfor dirname, _, filenames in os.walk('./'):\n    for filename in filenames:\n        file_list.append(file_name)\n         \nfile_list","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:44:08.326676Z","iopub.execute_input":"2022-10-10T10:44:08.327134Z","iopub.status.idle":"2022-10-10T10:44:08.335952Z","shell.execute_reply.started":"2022-10-10T10:44:08.327100Z","shell.execute_reply":"2022-10-10T10:44:08.334686Z"}}},{"cell_type":"raw","source":"file_path = os.path.join(tracking_path, \"gen_data.csv\")\ngen_data.to_csv(file_path) # ??pivot it before writing it.","metadata":{}},{"cell_type":"raw","source":"const = math.pi/180.\n#print([40.66670833*const, -73.83051944*const], [40.66924444*const, -73.82935278*const])\n[0.7098320194694541/const, -1.2885560343009308/const]\n","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:24:20.728508Z","iopub.execute_input":"2022-10-10T09:24:20.729112Z","iopub.status.idle":"2022-10-10T09:24:20.736897Z","shell.execute_reply.started":"2022-10-10T09:24:20.729061Z","shell.execute_reply":"2022-10-10T09:24:20.735958Z"}}},{"cell_type":"raw","source":"distance_calculator([[0.7097679563067046, -1.28858565268568], [0.7098122197763974, -1.2885652906274285]])","metadata":{"execution":{"iopub.status.busy":"2022-10-09T13:52:59.965807Z","iopub.execute_input":"2022-10-09T13:52:59.966236Z","iopub.status.idle":"2022-10-09T13:52:59.975322Z","shell.execute_reply.started":"2022-10-09T13:52:59.966199Z","shell.execute_reply":"2022-10-09T13:52:59.974041Z"}}},{"cell_type":"raw","source":"math.degrees(0.709815441142775), math.degrees(-1.2885621658617266)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T15:57:44.445499Z","iopub.execute_input":"2022-10-09T15:57:44.445884Z","iopub.status.idle":"2022-10-09T15:57:44.455961Z","shell.execute_reply.started":"2022-10-09T15:57:44.445853Z","shell.execute_reply":"2022-10-09T15:57:44.454914Z"}}},{"cell_type":"raw","source":"details are: AQU 2019-01-01 1 1 10_11\nfirst point is: [0.7098173117289859, -1.28856165616125]\nsecond point is: [0.7098177888404736, -1.2885614463286537]\ncurrent heading is: 18.447571457939528\nheading_diff is: 20.81160473374776\nedge_distance is: 45.45687599277511\nregion is: V ","metadata":{}},{"cell_type":"raw","source":"print(-73.82726389/40.67200833, -73.82957778/40.67255833)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T15:27:50.425881Z","iopub.execute_input":"2022-10-08T15:27:50.426366Z","iopub.status.idle":"2022-10-08T15:27:50.432708Z","shell.execute_reply.started":"2022-10-08T15:27:50.426329Z","shell.execute_reply":"2022-10-08T15:27:50.431711Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.67200833)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:01:14.580118Z","iopub.execute_input":"2022-10-08T16:01:14.580593Z","iopub.status.idle":"2022-10-08T16:01:14.611131Z","shell.execute_reply.started":"2022-10-08T16:01:14.580553Z","shell.execute_reply":"2022-10-08T16:01:14.609384Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.67255833)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:23:37.549358Z","iopub.execute_input":"2022-10-08T16:23:37.549843Z","iopub.status.idle":"2022-10-08T16:23:37.558155Z","shell.execute_reply.started":"2022-10-08T16:23:37.549803Z","shell.execute_reply":"2022-10-08T16:23:37.557009Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.67153333)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:04:18.753383Z","iopub.execute_input":"2022-10-08T16:04:18.754244Z","iopub.status.idle":"2022-10-08T16:04:18.762814Z","shell.execute_reply.started":"2022-10-08T16:04:18.754187Z","shell.execute_reply":"2022-10-08T16:04:18.761539Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.66939167)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:13:06.220156Z","iopub.execute_input":"2022-10-08T16:13:06.220604Z","iopub.status.idle":"2022-10-08T16:13:06.227888Z","shell.execute_reply.started":"2022-10-08T16:13:06.220570Z","shell.execute_reply":"2022-10-08T16:13:06.226884Z"}}},{"cell_type":"raw","source":"So if it is greater than long then it has crossed the line","metadata":{}},{"cell_type":"raw","source":"const = math.pi/180.\n_, dis_r = distance_calculator([[40.67043262317046*const, -73.83059062029287*const], [40.67200833*const, -73.82726389*const]])\n_, dis_l = distance_calculator([[40.67043262317046*const, -73.83059062029287*const], [40.67255833*const, -73.82957778*const]])\nprint(dis_r*-1.81518609287716, dis_l*-1.8152184374776208)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T15:35:33.291255Z","iopub.execute_input":"2022-10-08T15:35:33.291701Z","iopub.status.idle":"2022-10-08T15:35:33.300219Z","shell.execute_reply.started":"2022-10-08T15:35:33.291665Z","shell.execute_reply":"2022-10-08T15:35:33.299158Z"}}},{"cell_type":"raw","source":"file_path = os.path.join(tracking_path, \"aqu_D.csv\")\ngen_data.to_csv(file_path) # ??pivot it before writing it.","metadata":{"execution":{"iopub.status.busy":"2022-10-07T05:49:28.675318Z","iopub.execute_input":"2022-10-07T05:49:28.675824Z","iopub.status.idle":"2022-10-07T05:49:28.684041Z","shell.execute_reply.started":"2022-10-07T05:49:28.675786Z","shell.execute_reply":"2022-10-07T05:49:28.682583Z"}}},{"cell_type":"raw","source":"details are: AQU 2019-01-01 4 5 89_90\nfirst point is: [0.7098095661833876, -1.2885875616806186]\nsecond point is: [0.7098095635787577, -1.288586562348251]\ncurrent heading is: 90.19688355104213\nheading_diff is: -70.94956515364728\nedge_distance is: -303.41272131787963\nregion is: VI \n","metadata":{"execution":{"iopub.status.busy":"2022-10-07T03:59:41.444554Z","iopub.execute_input":"2022-10-07T03:59:41.445074Z","iopub.status.idle":"2022-10-07T03:59:41.455321Z","shell.execute_reply.started":"2022-10-07T03:59:41.445036Z","shell.execute_reply":"2022-10-07T03:59:41.453937Z"}}},{"cell_type":"raw","source":"[0.7098095635787577*180/math.pi, -1.288586562348251*180/math.pi]","metadata":{"execution":{"iopub.status.busy":"2022-10-07T04:32:26.509440Z","iopub.execute_input":"2022-10-07T04:32:26.509873Z","iopub.status.idle":"2022-10-07T04:32:26.519282Z","shell.execute_reply.started":"2022-10-07T04:32:26.509819Z","shell.execute_reply":"2022-10-07T04:32:26.517908Z"}}},{"cell_type":"raw","source":"x = \"hello\"\n\n#if condition returns False, AssertionError is raised:\nassert x == \"goodbye\", \"x should be 'hello'\"","metadata":{"execution":{"iopub.status.busy":"2022-10-07T04:09:33.380766Z","iopub.execute_input":"2022-10-07T04:09:33.381257Z","iopub.status.idle":"2022-10-07T04:09:33.404342Z","shell.execute_reply.started":"2022-10-07T04:09:33.381221Z","shell.execute_reply":"2022-10-07T04:09:33.402784Z"}}},{"cell_type":"raw","source":"path = \"./home\"\nos.path.join(path, \"User/Desktop\", \"file.txt\")","metadata":{}},{"cell_type":"raw","source":"pd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\npd.set_option('display.width', None)\npd.set_option('display.max_colwidth', -1)\n \ndisplay(gen_data)","metadata":{}},{"cell_type":"raw","source":"pd.reset_option('all')\ndisplay(gen_data)","metadata":{}},{"cell_type":"raw","source":"nyra_start_table.shape, nyra_race_table.shape, nyra_tracking_table.shape","metadata":{}},{"cell_type":"raw","source":"nyra_race_table[nyra_race_table.race_date == '2019-01-01']","metadata":{}},{"cell_type":"raw","source":"nyra_race_table.groupby(['race_number']).count()","metadata":{}},{"cell_type":"raw","source":"nyra_start_table[nyra_start_table['position_at_finish']==1].groupby(['weight_carried']).count().sort_values('position_at_finish')\n","metadata":{}},{"cell_type":"raw","source":"nyra_tracking_table.groupby(['race_date', 'race_number', 'trakus_index']).count()","metadata":{}},{"cell_type":"raw","source":"tracking_1st_date_1st_race = nyra_tracking_table[(nyra_tracking_table['race_date'] == '2019-01-01') & (nyra_tracking_table['race_number'] == 1)].sort_values(['trakus_index', 'program_number'])","metadata":{}},{"cell_type":"raw","source":"tracking_1st_date_1st_race.to_csv('./tracking_1st_date_1st_race.csv') ","metadata":{}},{"cell_type":"raw","source":"path = './tracking_2/AQU-2019-01-01-3.csv'\nrace_no_3 = pd.read_csv(path, index_col=0)\n#race_no_3.race_number = [int(x) for x in race_no_3.race_number]\n#race_no_3.program_number = [str(x) for x in race_no_3.program_number]\n#race_no_3_piv = race_no_3.pivot(index='trakus_index', columns='program_number')\nrace_no_3","metadata":{"execution":{"iopub.status.busy":"2022-10-10T13:10:11.656494Z","iopub.execute_input":"2022-10-10T13:10:11.656970Z","iopub.status.idle":"2022-10-10T13:10:11.719208Z","shell.execute_reply.started":"2022-10-10T13:10:11.656932Z","shell.execute_reply":"2022-10-10T13:10:11.717997Z"}}},{"cell_type":"raw","source":"path = './tracking_2/AQU-2019-01-01-3.csv'\nrace_no_9 = pd.read_csv(path, index_col=0)\nrace_no_9.race_number = [int(x) for x in race_no_9.race_number]\nrace_no_9.program_number = [str(x) for x in race_no_9.program_number]\n \n#display(race_no_7) #.sort_values('edge_distance', ascending=True))","metadata":{"execution":{"iopub.status.busy":"2022-10-09T16:31:42.646533Z","iopub.execute_input":"2022-10-09T16:31:42.646946Z","iopub.status.idle":"2022-10-09T16:31:42.669132Z","shell.execute_reply.started":"2022-10-09T16:31:42.646914Z","shell.execute_reply":"2022-10-09T16:31:42.668269Z"}}},{"cell_type":"raw","source":"race_no_3 = pd.merge(race_no_3, nyra_race_table, how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\nrace_no_3 = pd.merge(race_no_3, nyra_start_table[['track_id', 'race_date', 'race_number', 'program_number', 'position_at_finish']],  how='left', left_on=['track_id', 'race_date', 'race_number', 'program_number'], right_on = ['track_id', 'race_date', 'race_number', 'program_number'])\nrace_no_3.distance_id = [(x*660) for x in race_no_3.distance_id]","metadata":{"execution":{"iopub.status.busy":"2022-10-10T12:53:09.088660Z","iopub.execute_input":"2022-10-10T12:53:09.089869Z","iopub.status.idle":"2022-10-10T12:53:09.123562Z","shell.execute_reply.started":"2022-10-10T12:53:09.089808Z","shell.execute_reply":"2022-10-10T12:53:09.122366Z"}}},{"cell_type":"raw","source":"print(sum(np.isnan(race_no_3['track_heading_diff']))) # checking if there is a horse that was at stand still\n#race_no_9","metadata":{"execution":{"iopub.status.busy":"2022-10-10T12:53:11.377717Z","iopub.execute_input":"2022-10-10T12:53:11.378146Z","iopub.status.idle":"2022-10-10T12:53:11.386438Z","shell.execute_reply.started":"2022-10-10T12:53:11.378109Z","shell.execute_reply":"2022-10-10T12:53:11.385009Z"}}},{"cell_type":"raw","source":"idxs = []\nnew_tr_idx = []\ntr_idx = list(race_no_3.trakus_index)\nfor x in tr_idx:\n    a, b = x.split('_')\n    idxs.append(int(a))\n    idxs.append(int(b))\nmax_idx = max(idxs)\nmin_idx = min(idxs)\nfor i in range(min_idx, max_idx):\n    new_tr_idx.append(\"{}_{}\".format(i, i+1))\n#new_tr_idx\nrace_no_3_piv = race_no_3.pivot(index='trakus_index', columns='program_number')\nrace_no_3_piv = race_no_3_piv.reindex(new_tr_idx)\nrace_no_3_piv\n#race_no_9_piv.to_csv('./race_no_9_piv_XX.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-10T12:53:17.186688Z","iopub.execute_input":"2022-10-10T12:53:17.187653Z","iopub.status.idle":"2022-10-10T12:53:17.234426Z","shell.execute_reply.started":"2022-10-10T12:53:17.187611Z","shell.execute_reply":"2022-10-10T12:53:17.232719Z"}}},{"cell_type":"raw","source":"pivoted_distance_df = distance_df.pivot('pg', 'trakus')['distance']\npivoted_distance_df","metadata":{"execution":{"iopub.status.busy":"2022-09-26T14:52:28.887615Z","iopub.execute_input":"2022-09-26T14:52:28.888059Z","iopub.status.idle":"2022-09-26T14:52:29.160079Z","shell.execute_reply.started":"2022-09-26T14:52:28.888020Z","shell.execute_reply":"2022-09-26T14:52:29.159168Z"}}},{"cell_type":"raw","source":"max_distance = np.array(pivoted_distance_df.max())\nmin_distance = np.array(pivoted_distance_df.min())\nlist(max_distance/min_distance).index(min(list(max_distance/min_distance)))\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T14:58:45.267568Z","iopub.execute_input":"2022-09-26T14:58:45.268006Z","iopub.status.idle":"2022-09-26T14:58:45.281038Z","shell.execute_reply.started":"2022-09-26T14:58:45.267972Z","shell.execute_reply":"2022-09-26T14:58:45.279875Z"}}},{"cell_type":"raw","source":"math.sin(30)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T18:04:36.599344Z","iopub.execute_input":"2022-09-26T18:04:36.601431Z","iopub.status.idle":"2022-09-26T18:04:36.610398Z","shell.execute_reply.started":"2022-09-26T18:04:36.601320Z","shell.execute_reply":"2022-09-26T18:04:36.608618Z"}}},{"cell_type":"raw","source":"race_no_3_piv = race_no_3.pivot('trakus_index', 'program_numbers')\nrace_no_3_piv.reindex(new_tr_idx).to_csv('./race_no_7_piv.csv')\nrace_no_3_piv_ridx = race_no_3_piv.reindex(new_tr_idx)\nrace_no_3_piv_ridx","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:34:22.655038Z","iopub.execute_input":"2022-09-28T15:34:22.656240Z","iopub.status.idle":"2022-09-28T15:34:23.664786Z","shell.execute_reply.started":"2022-09-28T15:34:22.656195Z","shell.execute_reply":"2022-09-28T15:34:23.663310Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"def param_calculator1(idx_1:int, idx_2:int, sub_col:int, df:pd.DataFrame):\n    a, b = idx_1, idx_2\n    ratio_1 = (abs(df['acceleration'][sub_col][b])/(abs(df['acceleration'][sub_col][a])+abs(df['acceleration'][sub_col][b])))\n    time_ratio_1 = 0.25*ratio_1   \n    if (horse_raced_data_1['acceleration'][sub_col][b] > horse_raced_data_1['acceleration'][sub_col][b+1]):\n        ratio_2 = (abs(df['acceleration'][sub_col][b])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+time_ratio_2\n        stride = df['distance_ft'][sub_col][b]*ratio_1 + df['distance_ft'][sub_col][b+1]*ratio_2\n        v_int_x = stride / total_time\n        v_int_y = 32.17*total_time*0.5\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = df['cum_distance'][sub_col][b]-(v_int_x*time_ratio_1)\n        end_point = start_point + stride\n    else:\n        ratio_2 = (abs(df['acceleration'][sub_col][b+1])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b+2])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = df['distance_ft'][sub_col][b+1]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*32.17)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = df['cum_distance'][sub_col][b]-(v_int_x*time_ratio_1)\n        end_point = start_point + stride\n    return start_point, end_point, stride, total_time, v_int, theta, time_ratio_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:34:42.188463Z","iopub.execute_input":"2022-09-28T15:34:42.188995Z","iopub.status.idle":"2022-09-28T15:34:42.203364Z","shell.execute_reply.started":"2022-09-28T15:34:42.188954Z","shell.execute_reply":"2022-09-28T15:34:42.202280Z"}}},{"cell_type":"raw","source":"def param_calculator2(idx_1:int, start_point_2:float, time_ratio_1:float, sub_col:int, df:pd.DataFrame):\n    a, b = idx_1-1, idx_1  \n    v_int_x = (df['cum_distance'][sub_col][b]-start_point_2)/time_ratio_1\n    \n    if (horse_raced_data_1['acceleration'][sub_col][b] > horse_raced_data_1['acceleration'][sub_col][b+1]):\n        ratio_2 = (abs(df['acceleration'][sub_col][b])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+time_ratio_2\n        stride = v_int_x*total_time\n        v_int_y = 32.17*total_time*0.5\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        end_point = start_point_2 + stride\n    else:\n        ratio_2 = (abs(df['acceleration'][sub_col][b+1])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b+2])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = df['distance_ft'][sub_col][b+1]\n        stride = v_int_x*total_time\n        v_int_y = 32.17*total_time*0.5 # 32.17m/s2 - gravity\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        end_point = start_point_2 + stride\n    return end_point, stride, total_time, v_int, theta, time_ratio_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:34:43.644708Z","iopub.execute_input":"2022-09-28T15:34:43.645310Z","iopub.status.idle":"2022-09-28T15:34:43.656394Z","shell.execute_reply.started":"2022-09-28T15:34:43.645265Z","shell.execute_reply":"2022-09-28T15:34:43.655169Z"}}},{"cell_type":"raw","source":"horse_raced_data_1 = race_no_3_piv_ridx[['distance_ft', 'acceleration', 'cum_distance']]\nsub_col_list = horse_raced_data_1['distance_ft'].columns\nhorse_raced_data_2 = pd.DataFrame(columns = ['pg_no','launch_pt','land_point','stride','stride_time','launch_speed','launch_angle'])\n\nfor sub_col in sub_col_list:\n    len_df = len(horse_raced_data_1)\n    for i in range(len_df):\n        if(horse_raced_data_1['cum_distance'][sub_col][i]>36.):\n            first_runup_idx = i\n            break\n    #print(horse_raced_data_1['v_final'][sub_col][i-1], horse_raced_data_1['v_final'][sub_col][i], horse_raced_data_1['v_final'][sub_col][i+1],horse_raced_data_1['v_final'][sub_col][i+2])\n    i = first_runup_idx\n    if (horse_raced_data_1['acceleration'][sub_col][i-1] > horse_raced_data_1['acceleration'][sub_col][i]):\n        start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i-1, i, sub_col, horse_raced_data_1)\n        row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n        horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n        print(\"{} is being processed by case_1\".format(sub_col))\n        while ((i+4)<len_df):\n            start_point_2 = end_point\n            time_ratio_1 = 0.25-time_ratio_2\n            for j in range(len_df):\n                if(horse_raced_data_1['cum_distance'][sub_col][j]>start_point_2):\n                    start_idx = j\n                    break\n            i = start_idx\n            end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator2(i, start_point_2, time_ratio_1, sub_col, horse_raced_data_1)            \n            row_to_app = {'pg_no':sub_col,'launch_pt':start_point_2,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n            horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n            print(\"{} is being processed by case_1\".format(sub_col))\n            \n    #break\n                                 \n    elif (horse_raced_data_1['acceleration'][sub_col][i-2] > horse_raced_data_1['acceleration'][sub_col][i-1]):\n        start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i-2, i-1, sub_col, horse_raced_data_1)\n        row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n        horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n        print(\"{} is being processed by case_2\".format(sub_col))\n        while ((i+3)<len_df):\n            i=i+2\n            start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i-2, i-1, sub_col, horse_raced_data_1)\n            row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n            horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)            \n            print(\"{} is being processed by case_2\".format(sub_col))\n    \n        \n    elif (horse_raced_data_1['acceleration'][sub_col][i] > horse_raced_data_1['acceleration'][sub_col][i+1]):\n        start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i, i+1, sub_col, horse_raced_data_1)\n        row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n        horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n        print(\"{} is being processed by case_3\".format(sub_col))\n        while ((i+5)<len_df):\n            i=i+2\n            start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i, i+1, sub_col, horse_raced_data_1)\n            row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n            horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)            \n            print(\"{} is being processed by case_3\".format(sub_col))\n        \n    \n    else:\n        print(\"all conditions are not fulfilled:\", sub_col)\n                                      \n    #row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n    #horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)   \n        \n    #break\nhorse_raced_data_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:35:32.317446Z","iopub.execute_input":"2022-09-28T15:35:32.319298Z","iopub.status.idle":"2022-09-28T15:36:05.726390Z","shell.execute_reply.started":"2022-09-28T15:35:32.319244Z","shell.execute_reply":"2022-09-28T15:36:05.724980Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"horse_raced_data_2.pg_no = [int(x) for x in horse_raced_data_2.pg_no]\n\nhorse_raced_data_2.pivot(index=None, columns='pg_no')\n#horse_raced_data_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:10:46.113807Z","iopub.execute_input":"2022-09-28T12:10:46.114148Z","iopub.status.idle":"2022-09-28T12:10:47.077902Z","shell.execute_reply.started":"2022-09-28T12:10:46.114123Z","shell.execute_reply":"2022-09-28T12:10:47.076353Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"horse_raced_data_1 = race_no_3_piv_ridx[['distance_ft', 'v_final', 'cum_distance']]\nsub_col_list = horse_raced_data_1['distance_ft'].columns\nhorse_raced_data_2 = pd.DataFrame(columns = ['pg_no','launch_pt','land_point','stride','stride_time','launch_speed','launch_angle'])\n\nfor sub_col in sub_col_list:\n    len_df = len(horse_raced_data_1)\n    for i in range(len_df):\n        if(horse_raced_data_1['cum_distance'][sub_col][i]>36.):\n            first_runup_idx = i\n            break\n    #print(horse_raced_data_1['v_final'][sub_col][i-1], horse_raced_data_1['v_final'][sub_col][i], horse_raced_data_1['v_final'][sub_col][i+1],horse_raced_data_1['v_final'][sub_col][i+2])\n    i = first_runup_idx\n    if (horse_raced_data_1['v_final'][sub_col][i-1] > horse_raced_data_1['v_final'][sub_col][i]):\n        ratio_1 = ((horse_raced_data_1['v_final'][sub_col][i])/(horse_raced_data_1['v_final'][sub_col][i-1]+horse_raced_data_1['v_final'][sub_col][i]))\n        time_ratio_1 = 0.25*ratio_1        \n        ratio_2 = ((horse_raced_data_1['v_final'][sub_col][i+1])/(horse_raced_data_1['v_final'][sub_col][i+1]+horse_raced_data_1['v_final'][sub_col][i+2]))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = horse_raced_data_1['distance_ft'][sub_col][i+1]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*9.81)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = horse_raced_data_1['cum_distance'][sub_col][i]-(v_int_x*time_ratio_1)\n        end_point = horse_raced_data_1['cum_distance'][sub_col][i+1]+(v_int_x*time_ratio_2)\n        print(\"{} is being processed by case_1\".format(sub_col))\n                                 \n    elif (horse_raced_data_1['v_final'][sub_col][i-2] > horse_raced_data_1['v_final'][sub_col][i-1]):\n        ratio_1 = ((horse_raced_data_1['v_final'][sub_col][i-1])/(horse_raced_data_1['v_final'][sub_col][i-1]+horse_raced_data_1['v_final'][sub_col][i-2]))\n        time_ratio_1 = 0.25*ratio_1        \n        ratio_2 = ((horse_raced_data_1['v_final'][sub_col][i])/(horse_raced_data_1['v_final'][sub_col][i+1]+horse_raced_data_1['v_final'][sub_col][i]))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = horse_raced_data_1['distance_ft'][sub_col][i]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*9.81)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = horse_raced_data_1['cum_distance'][sub_col][i-1]-(v_int_x*time_ratio_1)\n        end_point = horse_raced_data_1['cum_distance'][sub_col][i]+(v_int_x*time_ratio_2)\n        print(\"{} is being processed by case_2\".format(sub_col))\n        \n    elif (horse_raced_data_1['v_final'][sub_col][i] > horse_raced_data_1['v_final'][sub_col][i+1]):\n        ratio_1 = ((horse_raced_data_1['v_final'][sub_col][i+1])/(horse_raced_data_1['v_final'][sub_col][i+1]+horse_raced_data_1['v_final'][sub_col][i]))\n        time_ratio_1 = 0.25*ratio_1        \n        ratio_2 = ((horse_raced_data_1['v_final'][sub_col][i+2])/(horse_raced_data_1['v_final'][sub_col][i+2]+horse_raced_data_1['v_final'][sub_col][i+3]))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = horse_raced_data_1['distance_ft'][sub_col][i+2]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*9.81)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = horse_raced_data_1['cum_distance'][sub_col][i+1]-(v_int_x*time_ratio_1)\n        end_point = horse_raced_data_1['cum_distance'][sub_col][i+2]+(v_int_x*time_ratio_2)\n        print(\"{} is being processed by case_3\".format(sub_col))\n    \n    else:\n        print(\"all conditions are not fulfilled:\", sub_col)\n                                      \n    row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n    horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)   \n        \n    #break\nhorse_raced_data_2","metadata":{"execution":{"iopub.status.busy":"2022-09-27T20:16:23.525581Z","iopub.execute_input":"2022-09-27T20:16:23.526074Z","iopub.status.idle":"2022-09-27T20:16:23.656878Z","shell.execute_reply.started":"2022-09-27T20:16:23.526038Z","shell.execute_reply":"2022-09-27T20:16:23.655328Z"},"jupyter":{"source_hidden":true}}},{"cell_type":"raw","source":"race_no_7_distance = race_no_3[['trakus_index', 'program_numbers', 'distance_ft', 'cum_distance']].pivot('trakus_index', 'program_numbers').reindex(new_tr_idx)\nrace_no_7_distance.to_csv('./race_no_7_distance.csv')\nrace_no_7_distance[:10] #['distance_ft'][9]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T18:45:54.412424Z","iopub.execute_input":"2022-09-26T18:45:54.412917Z","iopub.status.idle":"2022-09-26T18:45:54.584895Z","shell.execute_reply.started":"2022-09-26T18:45:54.412881Z","shell.execute_reply":"2022-09-26T18:45:54.583638Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"race_no_3_piv = race_no_3.pivot('trakus_index', 'program_numbers')\nrace_no_3_piv.reindex(new_tr_idx)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T13:58:21.339618Z","iopub.execute_input":"2022-09-25T13:58:21.340666Z","iopub.status.idle":"2022-09-25T13:58:22.319503Z","shell.execute_reply.started":"2022-09-25T13:58:21.340620Z","shell.execute_reply":"2022-09-25T13:58:22.318000Z"},"jupyter":{"outputs_hidden":true}}}]}