7.2.4. Indexing TimeSeries#
This section shows how to use these methods for indexing TimeSeries:
For this section, we will continue to use kinetic data from wheelchair propulsion:
import kineticstoolkit.lab as ktk
import matplotlib.pyplot as plt
ts = ktk.load(ktk.doc.download("kinetics_wheelchair_propulsion_events.ktk.zip"))
ts.plot()
Despite the title of this section, we do not index or slice a TimeSeries. Instead, we address its time or data arrays, as for any NumPy array. For instance, to get the time and force data at the 100th sample of the TimeSeries, we would do:
print("Time is", ts.time[99])
print("Force is", ts.data["Forces"][99])
Time is 0.4125
Force is [ 0.27 -1.93 0.41 0. ]
We may however want to know search which index corresponds to a given time. To this effect, we can use ktk.TimeSeries.get_index_at_time, ktk.TimeSeries.get_index_before_time and ktk.TimeSeries.get_index_after_time:
print("The index that is nearest to 15 seconds is:")
print(ts.get_index_at_time(15.0))
print("The index that is just before 15 seconds is:")
print(ts.get_index_before_time(15.0))
print("The index that is just after 15 seconds is:")
print(ts.get_index_after_time(15.0))
The index that is nearest to 15 seconds is:
3600
The index that is just before 15 seconds is:
3599
The index that is just after 15 seconds is:
3601
Or, we may want to know which index corresponds to a given event, using ktk.TimeSeries.get_index_at_event, ktk.TimeSeries.get_index_before_event or ktk.TimeSeries.get_index_after_event:
print("The index that is nearest to 2nd push is:")
print(ts.get_index_at_event("push", occurrence=1))
print("The index that is just before 2nd push is:")
print(ts.get_index_before_event("push", occurrence=1))
print("The index that is just after 2nd push is:")
print(ts.get_index_after_event("push", occurrence=1))
The index that is nearest to 2nd push is:
2520
The index that is just before 2nd push is:
2519
The index that is just after 2nd push is:
2521
The get_index_before_...
and get_index_after_...
methods have a parameter inclusive
that selects if the selection is inclusive (<=
, >=
) or exclusive (<
, >
):
Method |
Returns |
---|---|
get_index_before_time(t, inclusive=False) |
index for nearest time \(<\) t |
get_index_before_time(t, inclusive=True) |
index for nearest time \(<=\) t |
get_index_after_time(t, inclusive=False) |
index for nearest time \(>\) t |
get_index_after_time(t, inclusive=True) |
index for nearest time \(>=\) t |
get_index_before_event(name, occurrence, inclusive=False) |
index for nearest time \(<\) event time |
get_index_before_event(name, occurrence, inclusive=True) |
index for nearest time \(<=\) event time |
get_index_after_event(name, occurrence, inclusive=False) |
index for nearest time \(>\) event time |
get_index_after_event(name, occurrence, inclusive=True) |
index for nearest time \(>=\) event time |