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()
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()
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()
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()