@@ -40,9 +40,7 @@ def sector_lap_time(self, sector_df):
40
40
# print(f"section_lap_time: {section_lap_time}")
41
41
return section_lap_time
42
42
43
- def split_sectors (
44
- self , df , threshold = None , min_length_throttle_below_threshold = 50 , min_distance_between_sectors = 50
45
- ):
43
+ def split_sectors (self , df , threshold = None , min_length_throttle_below_threshold = 50 , min_distance_between_sectors = 50 ):
46
44
logging .debug (f"split_sectors: min_length_throttle_below_threshold: { min_length_throttle_below_threshold } " )
47
45
logging .debug (f"split_sectors: min_distance_between_sectors: { min_distance_between_sectors } " )
48
46
if threshold is None :
@@ -63,7 +61,7 @@ def split_sectors(
63
61
one_sector = [
64
62
{"start" : 0 , "end" : max_distance , "length" : max_distance },
65
63
]
66
-
64
+
67
65
# Return one sector if we don't have valid start/end points
68
66
if len (start ) == 0 or len (end ) == 0 :
69
67
logging .error (f"No valid sectors found - threshold: { threshold } min_length: { min_length_throttle_below_threshold } " )
@@ -115,9 +113,7 @@ def split_sectors(
115
113
# sector["start"] = prev_sector["start"]
116
114
prev_sector ["end" ] = sector ["end" ]
117
115
remove_indices .append (i )
118
- logging .debug (
119
- f"remove sector { i } - { sector ['start' ]} m " + f"too close to previous sector: { distance_between } "
120
- )
116
+ logging .debug (f"remove sector { i } - { sector ['start' ]} m " + f"too close to previous sector: { distance_between } " )
121
117
122
118
new_sectors = []
123
119
for i , sector in enumerate (sectors ):
@@ -174,15 +170,11 @@ def section_df(self, track_df, start, end):
174
170
175
171
if end < start :
176
172
# Wrap around the max_distance
177
- first_part = track_df [
178
- (track_df ["DistanceRoundTrack" ] >= start ) & (track_df ["DistanceRoundTrack" ] <= max_distance )
179
- ]
173
+ first_part = track_df [(track_df ["DistanceRoundTrack" ] >= start ) & (track_df ["DistanceRoundTrack" ] <= max_distance )]
180
174
second_part = track_df [(track_df ["DistanceRoundTrack" ] >= 0 ) & (track_df ["DistanceRoundTrack" ] <= end )]
181
175
sector_df = pd .concat ([first_part , second_part ], axis = 0 ).reset_index (drop = True )
182
176
else :
183
- sector_df = track_df [
184
- (track_df ["DistanceRoundTrack" ] >= start ) & (track_df ["DistanceRoundTrack" ] <= end )
185
- ].reset_index (drop = True )
177
+ sector_df = track_df [(track_df ["DistanceRoundTrack" ] >= start ) & (track_df ["DistanceRoundTrack" ] <= end )].reset_index (drop = True )
186
178
187
179
return sector_df
188
180
@@ -253,9 +245,7 @@ def extract_window_start_end(self, sector_df, threshold, comparison_operator):
253
245
window_start = None
254
246
255
247
if window_start is not None and not below_threshold .empty :
256
- window_end = below_threshold [below_threshold ["DistanceRoundTrack" ] > window_start ].iloc [0 ][
257
- "DistanceRoundTrack"
258
- ]
248
+ window_end = below_threshold [below_threshold ["DistanceRoundTrack" ] > window_start ].iloc [0 ]["DistanceRoundTrack" ]
259
249
else :
260
250
window_end = None
261
251
@@ -357,7 +347,7 @@ def resample_channels(self, lap_df, columns=["Brake", "SpeedMs"], freq=1, max_di
357
347
# Early return if DataFrame is empty
358
348
if len (lap_df ) == 0 :
359
349
return lap_df
360
-
350
+
361
351
# Check if DistanceRoundTrack exists
362
352
if "DistanceRoundTrack" not in lap_df .columns :
363
353
logging .error ("DistanceRoundTrack column not found in DataFrame" )
@@ -406,23 +396,13 @@ def resample_channels(self, lap_df, columns=["Brake", "SpeedMs"], freq=1, max_di
406
396
# Handle interpolated columns
407
397
if df_interpolate_columns :
408
398
source_df = lap_df [df_interpolate_columns + ["DistanceRoundTrack" ]]
409
- interp_df = pd .merge_asof (
410
- result_df ,
411
- source_df ,
412
- on = "DistanceRoundTrack" ,
413
- direction = "nearest"
414
- ).interpolate ("linear" )
399
+ interp_df = pd .merge_asof (result_df , source_df , on = "DistanceRoundTrack" , direction = "nearest" ).interpolate ("linear" )
415
400
result_df [df_interpolate_columns ] = interp_df [df_interpolate_columns ]
416
401
417
402
# Handle backfill columns
418
403
if df_backfill_columns :
419
404
source_df = lap_df [df_backfill_columns + ["DistanceRoundTrack" ]]
420
- backfill_df = pd .merge_asof (
421
- result_df ,
422
- source_df ,
423
- on = "DistanceRoundTrack" ,
424
- direction = "nearest"
425
- ).bfill ()
405
+ backfill_df = pd .merge_asof (result_df , source_df , on = "DistanceRoundTrack" , direction = "nearest" ).bfill ()
426
406
result_df [df_backfill_columns ] = backfill_df [df_backfill_columns ]
427
407
428
408
new_df = result_df
@@ -442,18 +422,24 @@ def resample(self, input_df, columns=["Brake", "SpeedMs"], method="nearest", fre
442
422
max_distance = int (np .floor (df ["DistanceRoundTrack" ].max ()))
443
423
target_rows = int (max_distance / freq )
444
424
425
+ if target_rows <= 0 :
426
+ logging .error (f"Invalid target rows: { target_rows } (max_distance: { max_distance } , freq: { freq } )" )
427
+ return input_df
428
+
445
429
new_distance_round_track = np .linspace (min_distance , max_distance , target_rows )
430
+ if len (new_distance_round_track ) == 0 :
431
+ logging .error ("Empty distance track array generated" )
432
+ return input_df
446
433
447
434
new_distance_round_track = np .round (new_distance_round_track , decimals = 2 )
448
- new_distance_round_track [0 ] = max (new_distance_round_track [0 ], min_distance )
449
- new_distance_round_track [- 1 ] = min (new_distance_round_track [- 1 ], max_distance )
435
+ if len (new_distance_round_track ) > 0 :
436
+ new_distance_round_track [0 ] = max (new_distance_round_track [0 ], min_distance )
437
+ new_distance_round_track [- 1 ] = min (new_distance_round_track [- 1 ], max_distance )
450
438
451
439
resampled_df = pd .DataFrame ({"DistanceRoundTrack" : new_distance_round_track })
452
440
453
441
for column in columns :
454
- interp = interp1d (
455
- df ["DistanceRoundTrack" ], df [column ], kind = method , bounds_error = False , fill_value = "extrapolate"
456
- )
442
+ interp = interp1d (df ["DistanceRoundTrack" ], df [column ], kind = method , bounds_error = False , fill_value = "extrapolate" )
457
443
interpolated_values = interp (new_distance_round_track )
458
444
459
445
if np .issubdtype (df [column ].dtype , np .integer ):
@@ -495,9 +481,7 @@ def distance_speed_lookup_table_non_lin(self, lap):
495
481
lap = lap [["DistanceRoundTrack" , "CurrentLapTime" , "SpeedMs" ]].copy ()
496
482
lap_start = lap ["CurrentLapTime" ].idxmin ()
497
483
498
- lap .loc [:lap_start , "CurrentLapTime" ] = (
499
- lap .loc [:lap_start , "DistanceRoundTrack" ] / lap .loc [:lap_start , "SpeedMs" ]
500
- )
484
+ lap .loc [:lap_start , "CurrentLapTime" ] = lap .loc [:lap_start , "DistanceRoundTrack" ] / lap .loc [:lap_start , "SpeedMs" ]
501
485
lap = lap [["DistanceRoundTrack" , "CurrentLapTime" , "SpeedMs" ]]
502
486
lap ["DistanceRoundTrack" ] = lap ["DistanceRoundTrack" ].round (1 )
503
487
lap ["CurrentLapTime" ] = lap ["CurrentLapTime" ].round (3 )
@@ -617,18 +601,14 @@ def extract_sector_start_end(self, sectors, threshold=0.98, track_length=0, min_
617
601
if i == len (sectors ) - 1 :
618
602
end = int ((sectors [0 ]["DistanceRoundTrack" ].iloc [0 ] - delta - 1 ) % track_length )
619
603
else :
620
- end = int (
621
- (sectors [i + 1 ]["DistanceRoundTrack" ].iloc [0 ] - delta - 1 ) % track_length
622
- ) # Subtract 11 to make the boundaries exactly one meter apart
604
+ end = int ((sectors [i + 1 ]["DistanceRoundTrack" ].iloc [0 ] - delta - 1 ) % track_length ) # Subtract 11 to make the boundaries exactly one meter apart
623
605
624
606
length = int ((end - start ) % track_length )
625
607
626
608
# Merge sectors shorter than the threshold with the previous sector
627
609
if i > 0 and length < min_length :
628
610
sector_start_end [- 1 ]["end" ] = end
629
- sector_start_end [- 1 ]["length" ] = (
630
- sector_start_end [- 1 ]["end" ] - sector_start_end [- 1 ]["start" ]
631
- ) % track_length
611
+ sector_start_end [- 1 ]["length" ] = (sector_start_end [- 1 ]["end" ] - sector_start_end [- 1 ]["start" ]) % track_length
632
612
else :
633
613
sector_start_end .append ({"start" : start , "end" : end , "length" : length })
634
614
return sector_start_end
@@ -659,14 +639,10 @@ def local_extrema(self, df, column="Gear", mode="min", points=50):
659
639
660
640
if mode == "min" :
661
641
# now find the local minima again
662
- real = min_max [column ][
663
- (min_max [column ].shift (1 ) >= min_max [column ]) & (min_max [column ].shift (- 1 ) > min_max [column ])
664
- ]
642
+ real = min_max [column ][(min_max [column ].shift (1 ) >= min_max [column ]) & (min_max [column ].shift (- 1 ) > min_max [column ])]
665
643
else :
666
644
# now find the local maxima again
667
- real = min_max [column ][
668
- (min_max [column ].shift (1 ) <= min_max [column ]) & (min_max [column ].shift (- 1 ) < min_max [column ])
669
- ]
645
+ real = min_max [column ][(min_max [column ].shift (1 ) <= min_max [column ]) & (min_max [column ].shift (- 1 ) < min_max [column ])]
670
646
671
647
return df .loc [real .index ]
672
648
@@ -685,9 +661,7 @@ def local_minima_off(self, df, column="Gear"):
685
661
min_max = pd .concat ([min_max , df .iloc [[0 , - 1 ]]])
686
662
687
663
# now find the local minima again
688
- real_min = min_max [column ][
689
- (min_max [column ].shift (1 ) >= min_max [column ]) & (min_max [column ].shift (- 1 ) > min_max [column ])
690
- ]
664
+ real_min = min_max [column ][(min_max [column ].shift (1 ) >= min_max [column ]) & (min_max [column ].shift (- 1 ) > min_max [column ])]
691
665
return df .loc [real_min .index ]
692
666
693
667
def extend_lap (self , df , count = 2 ):
0 commit comments