Skip to content

Commit

Permalink
FIX: support different interpolation method signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
theOehrly committed Dec 28, 2023
1 parent 1b6a823 commit 88e5ea4
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions fastf1/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ class Telemetry(pd.DataFrame):
the string ``'original'`` or an integer to specify a frequency in Hz."""

_CHANNELS = {
'X': {'type': 'continuous', 'missing': 'quadratic'},
'Y': {'type': 'continuous', 'missing': 'quadratic'},
'Z': {'type': 'continuous', 'missing': 'quadratic'},
'X': {'type': 'continuous', 'method': 'quadratic'},
'Y': {'type': 'continuous', 'method': 'quadratic'},
'Z': {'type': 'continuous', 'method': 'quadratic'},
'Status': {'type': 'discrete'},
'Speed': {'type': 'continuous', 'missing': 'linear'},
'RPM': {'type': 'continuous', 'missing': 'linear'},
'Throttle': {'type': 'continuous', 'missing': 'linear'},
'Speed': {'type': 'continuous', 'method': 'linear'},
'RPM': {'type': 'continuous', 'method': 'linear'},
'Throttle': {'type': 'continuous', 'method': 'linear'},
# linear is often required as quadratic overshoots on sudden changes
'Brake': {'type': 'discrete'},
'DRS': {'type': 'discrete'},
Expand All @@ -178,11 +178,11 @@ class Telemetry(pd.DataFrame):
'Date': {'type': 'excluded'}, # special, used as index when resampling
'Time': {'type': 'excluded'}, # special, recalculated from 'Date'
'SessionTime': {'type': 'excluded'},
'Distance': {'type': 'continuous', 'missing': 'quadratic'},
'RelativeDistance': {'type': 'continuous', 'missing': 'quadratic'},
'DifferentialDistance': {'type': 'continuous', 'missing': 'quadratic'},
'Distance': {'type': 'continuous', 'method': 'quadratic'},
'RelativeDistance': {'type': 'continuous', 'method': 'quadratic'},
'DifferentialDistance': {'type': 'continuous', 'method': 'quadratic'},
'DriverAhead': {'type': 'discrete'},
'DistanceToDriverAhead': {'type': 'continuous', 'missing': 'linear'}
'DistanceToDriverAhead': {'type': 'continuous', 'method': 'linear'}
}
"""Known telemetry channels which are supported by default"""

Expand Down Expand Up @@ -493,10 +493,19 @@ def merge_channels(
sig_type = self._CHANNELS[ch]['type']

if sig_type == 'continuous':
missing = self._CHANNELS[ch]['missing']
method = self._CHANNELS[ch]['method']
if method in ('nearest', 'zero', 'slinear', 'quadratic',
'cubic', 'barycentric', 'polynomial'):
# interpolation done using scipy.interpolate.interp1d
interp_kwargs = {'fill_value': 'extrapolate'}
elif method in ('pad', 'backfill', 'ffill', 'bfill'):
interp_kwargs = {}
else:
interp_kwargs = {'limit_direction': 'both'}

res = merged.loc[:, ch] \
.resample(frq, origin=ref_date).mean() \
.interpolate(method=missing, fill_value='extrapolate')
.interpolate(method=method, **interp_kwargs)

elif sig_type == 'discrete':
res = merged.loc[:, ch].resample(frq, origin=ref_date) \
Expand Down Expand Up @@ -618,10 +627,19 @@ def fill_missing(self):
if ret[ch].dtype == 'object':
warnings.warn("Interpolation not possible for telemetry "
"channel because dtype is 'object'")
missing = self._CHANNELS[ch]['missing']

method = self._CHANNELS[ch]['method']
if method in ('nearest', 'zero', 'slinear', 'quadratic',
'cubic', 'barycentric', 'polynomial'):
# interpolation done using scipy.interpolate.interp1d
interp_kwargs = {'fill_value': 'extrapolate'}
elif method in ('pad', 'backfill', 'ffill', 'bfill'):
interp_kwargs = {}
else:
interp_kwargs = {'limit_direction': 'both'}

ret.loc[:, ch] = ret.loc[:, ch] \
.interpolate(method=missing, limit_direction='both',
fill_value='extrapolate')
.interpolate(method=method, **interp_kwargs)

elif sig_type == 'discrete':
ret.loc[:, ch] = ret.loc[:, ch].ffill().ffill().bfill()
Expand Down Expand Up @@ -676,7 +694,7 @@ def register_new_channel(
"interpolation_method to be specified.")

cls._CHANNELS[name] = {'type': signal_type,
'missing': interpolation_method}
'method': interpolation_method}

def get_first_non_zero_time_index(self):
"""
Expand Down

0 comments on commit 88e5ea4

Please sign in to comment.