Skip to content

Commit

Permalink
update relevant examples
Browse files Browse the repository at this point in the history
  • Loading branch information
niksirbi committed Jan 21, 2025
1 parent 6b92e6b commit c1e1726
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
37 changes: 26 additions & 11 deletions examples/filter_and_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@
# %%
# Filter out points with low confidence
# -------------------------------------
# Using the :func:`movement.filtering.filter_by_confidence` function from the
# :mod:`movement.filtering` module, we can filter out points with confidence
# scores below a certain threshold. This function takes ``position`` and
# ``confidence`` as required arguments, and accepts an optional ``threshold``
# parameter, which defaults to ``threshold=0.6`` unless specified otherwise.
# Using the
# :func:`filter_by_confidence()<movement.filtering.filter_by_confidence>`
# function from the :mod:`movement.filtering` module,
# we can filter out points with confidence scores below a certain threshold.
# This function takes ``position`` and ``confidence`` as required arguments,
# and accepts an optional ``threshold`` parameter,
# which defaults to ``threshold=0.6`` unless specified otherwise.
# The function will also report the number of NaN values in the dataset before
# and after the filtering operation by default, but you can disable this
# by passing ``print_report=False``.
Expand All @@ -106,10 +108,11 @@
# %%
# Interpolate over missing values
# -------------------------------
# Using the :func:`movement.filtering.interpolate_over_time` function from the
# :mod:`movement.filtering` module, we can interpolate over gaps
# we've introduced in the pose tracks.
# Here we use the default linear interpolation method (``method=linear``)
# Using the
# :func:`interpolate_over_time()<movement.filtering.interpolate_over_time>`
# function from the :mod:`movement.filtering` module, we can interpolate over
# gaps we've introduced in the pose tracks.
# Here we use the default linear interpolation method (``method="linear"``)
# and interpolate over gaps of 40 frames or less (``max_gap=40``).
# The default ``max_gap=None`` would interpolate over all gaps, regardless of
# their length, but this should be used with caution as it can introduce
Expand All @@ -118,14 +121,26 @@
ds.update({"position": interpolate_over_time(ds.position, max_gap=40)})

# %%
# We see that all NaN values have disappeared, meaning that all gaps were
# indeed shorter than 40 frames.
# We see that most, but not all, NaN values have disappeared, meaning that
# most gaps were indeed shorter than 40 frames.
# Let's visualise the interpolated pose tracks.

ds.position.squeeze().plot.line(
x="time", row="keypoints", hue="space", aspect=2, size=2.5
)

# %%
# .. note::
# By default, interpolation does not apply to gaps at either end of the time
# series. In our case, the last few frames removed by filtering were not
# replaced by interpolation. Passing ``fill_value="extrapolate"`` to the
# :func:`interpolate_over_time()<movement.filtering.interpolate_over_time>`
# function would extrapolate the data to also fill the gaps at either end.
#
# In general, you may pass any keyword arguments that are accepted by
# :meth:`xarray.DataArray.interpolate_na` and its underlying methods.


# %%
# Log of processing steps
# -----------------------
Expand Down
4 changes: 3 additions & 1 deletion examples/smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def plot_raw_and_smooth_timeseries_and_psd(
)

# interpolate data to remove NaNs in the PSD calculation
pos_interp = interpolate_over_time(pos, print_report=False)
pos_interp = interpolate_over_time(
pos, print_report=False, fill_value="extrapolate"
)

# compute and plot the PSD
freq, psd = welch(pos_interp, fs=ds.fps, nperseg=256)
Expand Down

0 comments on commit c1e1726

Please sign in to comment.