{"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":"markdown","source":"### The change in speed of each horse over time could provide important information about his/her condition.\n### I'm going to obtain the time-course of speed from latitute/longitude data.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-12T06:32:37.527093Z","iopub.execute_input":"2022-08-12T06:32:37.528139Z","iopub.status.idle":"2022-08-12T06:32:37.563546Z","shell.execute_reply.started":"2022-08-12T06:32:37.527994Z","shell.execute_reply":"2022-08-12T06:32:37.562637Z"}}},{"cell_type":"code","source":"!pip install pymap3d\nimport pymap3d as pm\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nnyra_tracking = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv\")\nnyra_start = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_start_table.csv\")\nnyra_race = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_race_table.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:44:25.978429Z","iopub.execute_input":"2022-08-12T09:44:25.978921Z","iopub.status.idle":"2022-08-12T09:44:49.041397Z","shell.execute_reply.started":"2022-08-12T09:44:25.978829Z","shell.execute_reply":"2022-08-12T09:44:49.039790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = \"AQU\"\nrace_date = \"2019-01-01\"\nrace_number = 1\ntarget_tracking = nyra_tracking.query(\"track_id == @track_id & race_date == @race_date & race_number == @race_number\").sort_values(\"trakus_index\")\ntarget_tracking","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:44:49.043751Z","iopub.execute_input":"2022-08-12T09:44:49.044194Z","iopub.status.idle":"2022-08-12T09:44:49.407580Z","shell.execute_reply.started":"2022-08-12T09:44:49.044157Z","shell.execute_reply":"2022-08-12T09:44:49.406295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Calculate speed from tracking data","metadata":{}},{"cell_type":"code","source":"dtime = 0.25 #0.25 second per trakus_index according to the data description.\n#Elevations obtained from google earth\nif track_id == \"AQU\":\n    elevation = 3\nelif track_id == \"BEL\":\n    elevation = 20\nelif track_id == \"SAR\":\n    elevation = 93\n\ntarget_trackings = []\nfor number, group in target_tracking.groupby(\"program_number\"):\n    ecef = np.array(pm.geodetic2ecef(group[\"latitude\"].values, group[\"longitude\"].values, np.array([elevation]*len(group)))).T\n    #Calculate speed [km/h]\n    v_ecef = np.sqrt(np.sum(np.diff(ecef,axis=0) ** 2, axis = 1)) * 3.6 / dtime\n    group[\"time\"] = group[\"trakus_index\"] * dtime -  dtime\n    group[\"speed\"] = np.insert(v_ecef, 0, 0)\n    target_trackings.append(group)\n    print(\"No.\", number,\" Mean speed : {:.2f} km/h\".format(np.mean(v_ecef)))\ntarget_tracking = pd.concat(target_trackings)\ntarget_tracking","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:45:11.509400Z","iopub.execute_input":"2022-08-12T09:45:11.509852Z","iopub.status.idle":"2022-08-12T09:45:11.553118Z","shell.execute_reply.started":"2022-08-12T09:45:11.509816Z","shell.execute_reply":"2022-08-12T09:45:11.551852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The format of this graph comes from the beautiful notebook by [Matt OP](https://www.kaggle.com/code/mattop/big-data-derby-2022-eda). Thank you very much!","metadata":{}},{"cell_type":"code","source":"plt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.scatterplot(data = target_tracking, x = \"time\", y = \"speed\", hue = \"program_number\", palette = \"Set2\")\nplt.title(\"track_id = {}, race_date = {}, race_number = {}\".format(track_id,race_date,race_number))\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:45:13.365678Z","iopub.execute_input":"2022-08-12T09:45:13.367153Z","iopub.status.idle":"2022-08-12T09:45:13.792207Z","shell.execute_reply.started":"2022-08-12T09:45:13.367101Z","shell.execute_reply":"2022-08-12T09:45:13.790950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Race Information","metadata":{}},{"cell_type":"code","source":"nyra_race.query(\"track_id == @track_id & race_date == @race_date & race_number == @race_number\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:44:49.937719Z","iopub.execute_input":"2022-08-12T09:44:49.938231Z","iopub.status.idle":"2022-08-12T09:44:49.957025Z","shell.execute_reply.started":"2022-08-12T09:44:49.938199Z","shell.execute_reply":"2022-08-12T09:44:49.955467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start.query(\"track_id == @track_id & race_date == @race_date & race_number == @race_number\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:44:49.958378Z","iopub.execute_input":"2022-08-12T09:44:49.958928Z","iopub.status.idle":"2022-08-12T09:44:49.978401Z","shell.execute_reply.started":"2022-08-12T09:44:49.958892Z","shell.execute_reply":"2022-08-12T09:44:49.977026Z"},"trusted":true},"execution_count":null,"outputs":[]}]}