7.2.5. Segmenting TimeSeries#

This section shows how to use these methods to segment TimeSeries:

All these methods do the same: they extract a new TimeSeries using a criteria based on index, time or event. We will once again use kinetic data of wheelchair propulsion:

import matplotlib.pyplot as plt

import kineticstoolkit.lab as ktk

ts = ktk.load(ktk.doc.download("kinetics_wheelchair_propulsion_events.ktk.zip"))
ts.plot()
_images/3affa6def59f4dcc067528e44341f1c321bb6e3a9c2f10b359457f8a76d926c2.png

7.2.5.1. Using indexes#

If we know that the sync event happened at index 1049 (we could know it using index = ts.get_index_at_event("sync")), we can discard any data that happened before this index:

new_ts = ts.get_ts_after_index(1049)
new_ts.plot()
_images/aeec338d13ed42d7a28f0a854e39bf1e40ac5a4dea69e54f61d1091e77774f4b.png

7.2.5.2. Using time#

If we know that the sync event happened at time 4.37 (we could know it using time = ts.time[ts.get_index_at_event("sync")]), we could do the same using time:

new_ts = ts.get_ts_after_time(4.37)

7.2.5.3. Using events#

Or, we could simply use the event itself:

new_ts = ts.get_ts_after_event("sync")

These methods are very helpful to analyze only specific sections of a TimeSeries. For instance, to analyze only the four first pushes and get rid of any other data, we would do:

# Extract the four pushes
first_four_pushes = ts.get_ts_between_events("push", "push", 0, 4, inclusive=True)

# Remove the events outside the resulting TimeSeries
first_four_pushes = first_four_pushes.trim_events()

# Plot the result
first_four_pushes.plot()
_images/914e6ff4ceb973ee1f5da8899d6b55be1863842c8a381a9a002ccb4ca362158c.png

Note the use of inclusive=True in this example. This optional parameter indicates wether the “before”, “after” or “between” term in the method name is inclusive (<=, >=) or strict (<, >). Different combinations lead to different results:

plt.subplot(2, 2, 1)
ts1 = ts.get_ts_between_events("push", "push", 0, 4, inclusive=False)
ts1 = ts1.trim_events()
ts1.plot(legend=False)
plt.xlabel("Inclusive = False")

plt.subplot(2, 2, 2)
ts2 = ts.get_ts_between_events("push", "push", 0, 4, inclusive=True)
ts2 = ts2.trim_events()
ts2.plot(legend=False)
plt.xlabel("Inclusive = True")

plt.subplot(2, 2, 3)
ts3 = ts.get_ts_between_events("push", "push", 0, 4, inclusive=[False, True])
ts3 = ts3.trim_events()
ts3.plot(legend=False)
plt.xlabel("Inclusive = [False, True]")

plt.subplot(2, 2, 4)
ts4 = ts.get_ts_between_events("push", "push", 0, 4, inclusive=[True, False])
ts4 = ts4.trim_events()
ts4.plot(legend=False)
plt.xlabel("Inclusive = [True, False]")

plt.tight_layout()
_images/b30c63cee7b651e855fa701c71c31fa1699c90b49cb77a31ca917c9cecf2356d.png