Skip to content

Commit

Permalink
Merge pull request #360 from druzsan/main
Browse files Browse the repository at this point in the history
Test PR
  • Loading branch information
druzsan authored Nov 15, 2023
2 parents fdadd69 + 5dc96af commit c4a68d1
Showing 1 changed file with 88 additions and 60 deletions.
148 changes: 88 additions & 60 deletions playbook/stories/fastf1_spotlight_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"source": [
"import fastf1\n",
"\n",
"session = fastf1.get_session(2023, 'Montreal', 'Race')\n",
"session = fastf1.get_session(2023, \"Montreal\", \"Race\")\n",
"\n",
"session.load(telemetry=True, laps=True)\n",
"\n",
Expand All @@ -41,20 +41,36 @@
"import pandas as pd\n",
"from tqdm import tqdm\n",
"\n",
"\n",
"def extract_telemetry(laps, columns):\n",
" df_telemetry = pd.DataFrame(columns=columns)\n",
" row_dict = {}\n",
"\n",
" for index, lap in tqdm(laps.iterlaps(), total=laps.shape[0]): \n",
" telemetry = lap.get_telemetry() \n",
" for index, lap in tqdm(laps.iterlaps(), total=laps.shape[0]):\n",
" telemetry = lap.get_telemetry()\n",
" for column in columns:\n",
" row_dict[column] = [telemetry['Distance'].tolist(), telemetry[column].tolist()]\n",
" df_telemetry.loc[index]=row_dict\n",
" \n",
" row_dict[column] = [\n",
" telemetry[\"Distance\"].tolist(),\n",
" telemetry[column].tolist(),\n",
" ]\n",
" df_telemetry.loc[index] = row_dict\n",
"\n",
" return df_telemetry\n",
"\n",
"columns = [\"DistanceToDriverAhead\", \"RPM\", \"Speed\", \"nGear\", \"Throttle\", \"Brake\", \"DRS\", \"X\", \"Y\", \"Z\"]\n",
"df_telemetry = extract_telemetry(laps, columns)\n"
"\n",
"columns = [\n",
" \"DistanceToDriverAhead\",\n",
" \"RPM\",\n",
" \"Speed\",\n",
" \"nGear\",\n",
" \"Throttle\",\n",
" \"Brake\",\n",
" \"DRS\",\n",
" \"X\",\n",
" \"Y\",\n",
" \"Z\",\n",
"]\n",
"df_telemetry = extract_telemetry(laps, columns)"
]
},
{
Expand All @@ -80,24 +96,29 @@
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"dist_index = np.array(list(range(-10, 4400, 5)))\n",
"\n",
"\n",
"def extract_embeddings(laps, columns):\n",
" \n",
" column_names= []\n",
" column_names = []\n",
" for column in columns:\n",
" column_names.append(column + \"_emb\")\n",
" \n",
"\n",
" df_embedding = pd.DataFrame(columns=column_names)\n",
" row_dict = {}\n",
"\n",
" for index, lap in tqdm(laps.iterlaps(), total=laps.shape[0]): \n",
" telemetry = lap.get_telemetry() \n",
" for index, lap in tqdm(laps.iterlaps(), total=laps.shape[0]):\n",
" telemetry = lap.get_telemetry()\n",
" for column in columns:\n",
" column_name = column + \"_emb\"\n",
" row_dict[column_name] = np.interp(x=dist_index, xp=telemetry['Distance'].to_numpy(), fp=telemetry[column].to_numpy()).tolist() \n",
" df_embedding.loc[index]=row_dict\n",
" \n",
" row_dict[column_name] = np.interp(\n",
" x=dist_index,\n",
" xp=telemetry[\"Distance\"].to_numpy(),\n",
" fp=telemetry[column].to_numpy(),\n",
" ).tolist()\n",
" df_embedding.loc[index] = row_dict\n",
"\n",
" return df_embedding\n",
"\n",
"\n",
Expand Down Expand Up @@ -130,112 +151,110 @@
"from os import path\n",
"\n",
"\n",
"#function to print the gear and speed map\n",
"# function to print the gear and speed map\n",
"\n",
"\n",
"def create_speed_image(lapnumber, tel):\n",
" filename = \"imgs/speed/speed_vis_\" + str(lapnumber) + \".png\"\n",
"\n",
" filename = 'imgs/speed/speed_vis_' + str(lapnumber) +'.png'\n",
" \n",
" if path.isfile(filename):\n",
" return filename\n",
" \n",
"\n",
" colormap = mpl.cm.plasma\n",
" # Get telemetry data\n",
" x = np.array(tel['X'].values)\n",
" y = np.array(tel['Y'].values)\n",
" color = tel['Speed'] # value to base color gradient on\n",
" x = np.array(tel[\"X\"].values)\n",
" y = np.array(tel[\"Y\"].values)\n",
" color = tel[\"Speed\"] # value to base color gradient on\n",
"\n",
" points = np.array([x, y]).T.reshape(-1, 1, 2)\n",
" segments = np.concatenate([points[:-1], points[1:]], axis=1)\n",
"\n",
" # We create a plot with title and adjust some setting to make it look good.\n",
" fig, ax = plt.subplots(sharex=True, sharey=True, figsize=(12, 6.75)) \n",
" fig, ax = plt.subplots(sharex=True, sharey=True, figsize=(12, 6.75))\n",
"\n",
" # Adjust margins and turn of axis\n",
" plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.12)\n",
" ax.axis('off')\n",
"\n",
" ax.axis(\"off\")\n",
"\n",
" # After this, we plot the data itself.\n",
" # Create background track line\n",
" ax.plot(tel['X'], tel['Y'], color='black', linestyle='-', linewidth=16, zorder=0)\n",
" ax.plot(tel[\"X\"], tel[\"Y\"], color=\"black\", linestyle=\"-\", linewidth=16, zorder=0)\n",
"\n",
" # Create a continuous norm to map from data points to colors\n",
" norm = plt.Normalize(color.min(), color.max())\n",
" lc = LineCollection(segments, cmap=colormap, norm=norm, linestyle='-', linewidth=5)\n",
" lc = LineCollection(segments, cmap=colormap, norm=norm, linestyle=\"-\", linewidth=5)\n",
"\n",
" # Set the values used for colormapping\n",
" lc.set_array(color)\n",
"\n",
" # Merge all line segments together\n",
" line = ax.add_collection(lc)\n",
"\n",
"\n",
" # Finally, we create a color bar as a legend.\n",
" cbaxes = fig.add_axes([0.25, 0.05, 0.5, 0.05])\n",
" normlegend = mpl.colors.Normalize(vmin=color.min(), vmax=color.max())\n",
" legend = mpl.colorbar.ColorbarBase(cbaxes, norm=normlegend, cmap=colormap, orientation=\"horizontal\")\n",
" legend = mpl.colorbar.ColorbarBase(\n",
" cbaxes, norm=normlegend, cmap=colormap, orientation=\"horizontal\"\n",
" )\n",
"\n",
"\n",
" \n",
" plt.savefig(filename, format='png')\n",
" plt.savefig(filename, format=\"png\")\n",
"\n",
" plt.clf()\n",
"\n",
" plt.close('all')\n",
" plt.close(\"all\")\n",
"\n",
" return filename\n",
"\n",
"\n",
"def create_gear_image(lapnumber, tel):\n",
"\n",
" filename = 'imgs/gears/gear_shift_vis_' + str(lapnumber) +'.png'\n",
" filename = \"imgs/gears/gear_shift_vis_\" + str(lapnumber) + \".png\"\n",
"\n",
" if path.isfile(filename):\n",
" return filename\n",
"\n",
" x = np.array(tel['X'].values)\n",
" y = np.array(tel['Y'].values)\n",
" x = np.array(tel[\"X\"].values)\n",
" y = np.array(tel[\"Y\"].values)\n",
"\n",
" points = np.array([x, y]).T.reshape(-1, 1, 2)\n",
" segments = np.concatenate([points[:-1], points[1:]], axis=1)\n",
" gear = tel['nGear'].to_numpy().astype(float)\n",
" gear = tel[\"nGear\"].to_numpy().astype(float)\n",
"\n",
" cmap = cm.get_cmap('Paired')\n",
" lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)\n",
" cmap = cm.get_cmap(\"Paired\")\n",
" lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N + 1), cmap=cmap)\n",
" lc_comp.set_array(gear)\n",
" lc_comp.set_linewidth(4)\n",
"\n",
" plt.gca().add_collection(lc_comp)\n",
" plt.axis('equal')\n",
" plt.axis(\"equal\")\n",
" plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)\n",
"\n",
" cbar = plt.colorbar(mappable=lc_comp, label=\"Gear\", boundaries=np.arange(1, 10))\n",
" cbar.set_ticks(np.arange(1.5, 9.5))\n",
" cbar.set_ticklabels(np.arange(1, 9))\n",
"\n",
" filename = 'imgs/gears/gear_shift_vis_' + str(lapnumber) +'.png'\n",
" plt.savefig(filename, format='png')\n",
" filename = \"imgs/gears/gear_shift_vis_\" + str(lapnumber) + \".png\"\n",
" plt.savefig(filename, format=\"png\")\n",
"\n",
" plt.clf()\n",
"\n",
" plt.close('all')\n",
" plt.close(\"all\")\n",
"\n",
" return filename\n",
"\n",
"\n",
"def extract_images(laps):\n",
" df_images = pd.DataFrame(columns=['gear_vis','speed_vis'])\n",
" df_images = pd.DataFrame(columns=[\"gear_vis\", \"speed_vis\"])\n",
" row_dict = {}\n",
"\n",
" for index, lap in tqdm(laps.iterlaps(), total=laps.shape[0]): \n",
" telemetry = lap.get_telemetry() \n",
" row_dict['gear_vis']=create_gear_image(index, telemetry)\n",
" row_dict['speed_vis']=create_speed_image(index, telemetry) \n",
" df_images.loc[index]=row_dict\n",
" \n",
" for index, lap in tqdm(laps.iterlaps(), total=laps.shape[0]):\n",
" telemetry = lap.get_telemetry()\n",
" row_dict[\"gear_vis\"] = create_gear_image(index, telemetry)\n",
" row_dict[\"speed_vis\"] = create_speed_image(index, telemetry)\n",
" df_images.loc[index] = row_dict\n",
"\n",
" return df_images\n",
"\n",
"\n",
"df_images = extract_images(laps)"
]
},
Expand All @@ -252,7 +271,7 @@
"metadata": {},
"outputs": [],
"source": [
"#concat the dataframes\n",
"# concat the dataframes\n",
"\n",
"df_metadata = pd.DataFrame(laps)\n",
"df = pd.concat([df_metadata, df_telemetry, df_images, df_embedding], axis=1)"
Expand All @@ -267,10 +286,10 @@
"from renumics import spotlight\n",
"from renumics.spotlight import dtypes\n",
"\n",
"#dtypes = {\"DistanceToDriverAhead\": spotlight.Sequence1D, \"RPM\": spotlight.Sequence1D, \"Speed\": spotlight.Sequence1D, \"nGear\": spotlight.Sequence1D,\n",
"# dtypes = {\"DistanceToDriverAhead\": spotlight.Sequence1D, \"RPM\": spotlight.Sequence1D, \"Speed\": spotlight.Sequence1D, \"nGear\": spotlight.Sequence1D,\n",
"# \"Throttle\": spotlight.Sequence1D, \"Brake\": spotlight.Sequence1D, \"DRS\": spotlight.Sequence1D, \"X\": spotlight.Sequence1D, \"Y\": spotlight.Sequence1D, \"Z\": spotlight.Sequence1D}\n",
"\n",
"spotlight.show(df)\n"
"spotlight.show(df)"
]
},
{
Expand All @@ -290,7 +309,7 @@
"\n",
"ds = datasets.Dataset.from_pandas(df)\n",
"\n",
"ds.save_to_disk('telemetry_test')"
"ds.save_to_disk(\"telemetry_test\")"
]
},
{
Expand All @@ -299,8 +318,18 @@
"metadata": {},
"outputs": [],
"source": [
"dtypes = {\"DistanceToDriverAhead\": spotlight.Sequence1D, \"RPM\": spotlight.Sequence1D, \"Speed\": spotlight.Sequence1D, \"nGear\": spotlight.Sequence1D,\n",
" \"Throttle\": spotlight.Sequence1D, \"Brake\": spotlight.Sequence1D, \"DRS\": spotlight.Sequence1D, \"X\": spotlight.Sequence1D, \"Y\": spotlight.Sequence1D, \"Z\": spotlight.Sequence1D}\n",
"dtypes = {\n",
" \"DistanceToDriverAhead\": spotlight.Sequence1D,\n",
" \"RPM\": spotlight.Sequence1D,\n",
" \"Speed\": spotlight.Sequence1D,\n",
" \"nGear\": spotlight.Sequence1D,\n",
" \"Throttle\": spotlight.Sequence1D,\n",
" \"Brake\": spotlight.Sequence1D,\n",
" \"DRS\": spotlight.Sequence1D,\n",
" \"X\": spotlight.Sequence1D,\n",
" \"Y\": spotlight.Sequence1D,\n",
" \"Z\": spotlight.Sequence1D,\n",
"}\n",
"\n",
"spotlight.show(df, dtype=dtypes)"
]
Expand All @@ -323,8 +352,7 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
},
"orig_nbformat": 4
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down

0 comments on commit c4a68d1

Please sign in to comment.