Skip to content

Commit

Permalink
Interpolation Options for Trajectory Resampling (#441)
Browse files Browse the repository at this point in the history
Co-authored-by: Niclas Wüstenbecker <[email protected]>
  • Loading branch information
niclaswue and Niclas Wüstenbecker committed May 27, 2024
1 parent 8e19aa9 commit ecc34ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/traffic/core/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,7 @@ def resample(
self,
rule: str | int = "1s",
how: None | str | dict[str, Iterable[str]] = "interpolate",
interpolate_kw: dict[str, Any] = {},
projection: None | str | pyproj.Proj | "crs.Projection" = None,
) -> Flight:
"""Resample the trajectory at a given frequency or for a target number
Expand All @@ -1680,6 +1681,18 @@ def resample(
``"interpolate"``, ``"ffill"``) and names of columns as values.
Columns not included in any value are left as is.
:param interpolate_kw: (default: ``{}``)
- A dictionary with keyword arguments that will be passed to the
pandas :py:method:`pandas.Series.interpolate` method.
Example usage:
To specify a fifth-degree polynomial interpolation, you can
pass the following dictionary:
.. code-block:: python
interpolate_kw = {"method": "polynomial", "order": 5}
:param projection: (default: ``None``)
- By default, lat/lon are resampled with a linear interpolation;
Expand Down Expand Up @@ -1729,7 +1742,8 @@ def resample(
for meth, columns in how.items():
if meth is not None:
idx = data.columns.get_indexer(columns)
value = getattr(data.iloc[:, idx], meth)()
kwargs = interpolate_kw if meth == "interpolate" else {}
value = getattr(data.iloc[:, idx], meth)(**kwargs)
data[data.columns[idx]] = value

elif isinstance(rule, int):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,36 @@ def test_resample_how_argument() -> None:
check_dtype=False,
)

altitude_interpolate_quadratic = [
30000.0,
28440.0,
27160.0,
26160.0,
25440.0,
25000.0,
24840.0,
24960.0,
25360.0,
26040.0,
27000.0,
]
resampled_interpolate_quadratic = Flight(df).resample(
"1s",
how="interpolate",
interpolate_kw={"method": "polynomial", "order": 2},
)
pd.testing.assert_frame_equal(
resampled_interpolate_quadratic.data[["altitude", "fake"]],
pd.DataFrame(
{
"altitude": altitude_interpolate_quadratic,
"fake": fake_interpolate,
}
),
check_dtype=False,
check_exact=False, # accomodate for small rounding errors
)

resampled_ffill = Flight(df).resample("1s", how="ffill")
pd.testing.assert_frame_equal(
resampled_ffill.data[["altitude", "fake"]],
Expand Down

0 comments on commit ecc34ec

Please sign in to comment.