visualizer.py
1 import plotly.graph_objects as go 2 3 4 def telemetry_for_fig(segment, track_length=None): 5 if segment.start > segment.end: 6 # add track_length to all distances that are less than start 7 df = segment.telemetry.copy() 8 if track_length is None: 9 track_length = df["DistanceRoundTrack"].max() 10 print(f"track_length: {track_length}") 11 df["DistanceRoundTrack"] = df["DistanceRoundTrack"].apply( 12 lambda x: x + track_length if x < segment.start else x 13 ) 14 return df 15 return segment.telemetry 16 17 18 def features_for_fig(segment, track_length, features): 19 if segment.start > segment.end: 20 features = features.copy() 21 for key in ["start", "end", "max_start", "max_end"]: 22 value = features[key] 23 if value < segment.start: 24 # print(f"adding track_length to {key} {value} -> {value + track_length}") 25 features[key] = value + track_length 26 return features 27 28 29 def lap_fig(df, mode=None, columns=["Throttle", "Brake"], fig=None, full_range=False): 30 fig = fig or go.Figure() 31 # fig = fig or go.Figure(layout=go.Layout( 32 # autosize=False, 33 # width=800, # specify the width in pixels 34 # height=600 # specify the height in pixels 35 # )) 36 37 for column in columns: 38 color = "red" 39 if column == "Throttle": 40 color = "green" 41 fig.add_scatter( 42 x=df["DistanceRoundTrack"], 43 y=df[column], 44 marker=dict(size=1), 45 mode=mode, 46 name=column, 47 line=dict(color=color), 48 showlegend=True, 49 ) 50 51 # Set the range of the x-axis and the distance between tick marks 52 # set start to the nearest 100 meters 53 if not full_range: 54 start = df["DistanceRoundTrack"].min() 55 start = start - (start % 100) 56 # end = df["DistanceRoundTrack"].max() 57 # if end - start < 400: 58 # end = start + 400 59 end = start + 1000 60 x_range = [start, end] 61 fig.update_xaxes(range=x_range, dtick=100) 62 63 return fig 64 65 66 def fig_add_shape(fig, color="black", **kwargs): 67 default = dict( 68 type="rect", 69 xref="x", 70 yref="y", 71 x0=0, 72 y0=0, 73 x1=0, 74 y1=1, 75 line=dict(color=color, width=2, dash="dot"), 76 ) 77 args = {**default, **kwargs} 78 fig.add_shape(**args) 79 return fig 80 81 82 def fig_add_features(fig, features, color="red"): 83 fig_add_shape(fig, x0=features["start"], x1=features["end"], color=color) 84 fig_add_shape( 85 fig, 86 x0=features["max_start"], 87 y0=features["max_low"], 88 x1=features["max_end"], 89 y1=features["max_high"], 90 color=color, 91 ) 92 fig_add_shape( 93 fig, 94 type="line", 95 x0=features["max_start"], 96 y0=features["force"], 97 x1=features["max_end"], 98 y1=features["force"], 99 line=dict(color="yellow", width=2), 100 )