Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation Options for Trajectory Resampling #441

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 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",
interpolation_options: dict[str, Any] = {},
niclaswue marked this conversation as resolved.
Show resolved Hide resolved
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 interpolation_options: (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
interpolation_options = {"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,10 @@ 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 = (
interpolation_options 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",
interpolation_options={"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