7.2.3. Event management#

This section shows how to use these methods for event management:

We will use this TimeSeries that contains cyclic kinetic data in wheelchair propulsion:

import kineticstoolkit.lab as ktk
import matplotlib.pyplot as plt

ts = ktk.load(ktk.doc.download("kinetics_wheelchair_propulsion.ktk.zip"))
ts.plot()
_images/c1dff3db6b7eb1cd1b22e0aca097caeb4c0a17d389b3dfa012a24f68e2f942c2.png

7.2.3.1. Adding events#

We already know how to manually add events using ktk.TimeSeries.add_event:

ts = ts.add_event(4.37, "sync")
ts = ts.add_event(8.56, "push")
ts = ts.add_event(9.93, "recovery")
ts = ts.add_event(10.50, "push")
ts = ts.add_event(11.12, "recovery")
ts = ts.add_event(11.78, "push")
ts = ts.add_event(12.33, "recovery")
ts = ts.add_event(13.39, "push")
ts = ts.add_event(13.88, "recovery")
ts = ts.add_event(14.86, "push")
ts = ts.add_event(15.30, "recovery")

ts.plot()
_images/bc4fc6cc88ae6c3dc8ee0d0730cc6457c086ccc0552f6bb34fca75a982b3d7a5.png

7.2.3.2. Counting the number of occurrences of an event#

Each event is defined by its name, and if it is repeated multiple times, by an occurrence. To count these occurrences, we use ktk.TimeSeries.count_events:

print(ts.count_events('sync'), "occurrences of event 'sync'")
print(ts.count_events('push'), "occurrences of event 'push'")
print(ts.count_events('recovery'), "occurrences of event 'recovery'")
1 occurrences of event 'sync'
5 occurrences of event 'push'
5 occurrences of event 'recovery'

7.2.3.3. Renaming events#

Renaming events is performed using ktk.TimeSeries.rename_event:

ts = ts.rename_event("push", "lastpush", occurrence=4)
ts = ts.rename_event("recovery", "lastrecovery", occurrence=4)

ts.plot()
_images/28d14487c9cda7bc4ff4975e214bccdb6d25fb129aead30f9833a0fab206c4c0.png

7.2.3.4. Removing events#

To remove an occurrence of an event, we use ktk.TimeSeries.remove_event. For instance, to remove the second push cycle:

ts = ts.remove_event("push", occurrence=1)
ts = ts.remove_event("recovery", occurrence=1)

ts.plot()
_images/c017d41d2e26b83ac17ece3375e271c134d88b38fef3a9e09dce2e24b5a71fd8.png

7.2.3.5. Removing duplicate events#

It may happen that events get duplicated in a TimeSeries. For instance, we may have added some events twice:

ts = ts.add_event(4.37, "sync")
ts = ts.add_event(8.56, "push")
ts = ts.add_event(9.93, "recovery")
ts = ts.add_event(10.50, "push")
ts = ts.add_event(11.12, "recovery")
ts = ts.add_event(11.78, "push")
ts = ts.add_event(12.33, "recovery")

ts.plot()
_images/e7d362c41b9b6e24f90a87786a90c6ab952270723dd961ffa1a6d418c75dfc77.png

Note the superposition of occurrences on the first events. To remove the duplicate events, we use ktk.TimeSeries.remove_duplicate_events:

ts = ts.remove_duplicate_events()
ts.plot()
_images/28d14487c9cda7bc4ff4975e214bccdb6d25fb129aead30f9833a0fab206c4c0.png

7.2.3.6. Trimming events#

For some reasons, it may happen that a TimeSeries have events outside its time array. Let’s simulate this situation:

ts = ts.add_event(-3.0, "before")
ts = ts.add_event(-1.0, "before")
ts = ts.add_event(23.0, "after")
ts = ts.add_event(25.0, "after")

ts.plot()
_images/bf1dce6200ff9703ec4dc42820a0a94678cb132994f0dd9d5f54d59696e6a6d2.png

To remove any event that is outside the TimeSeries’ time array, we use ktk.TimeSeries.trim_events.

ts = ts.trim_events()
ts.plot()
_images/28d14487c9cda7bc4ff4975e214bccdb6d25fb129aead30f9833a0fab206c4c0.png

7.2.3.7. Editing events interactively#

Finally, it is possible to edit the events interactively, using ktk.TimeSeries.ui_edit_events. This allows to add, remove and move events by clicking on a figure.

Note

Matplotlib must be interactive. See section Installing and configuring Spyder do help.

ts = ts.ui_edit_events()

which creates the interactive windows shown in Fig. 7.1 and Fig. 7.2.

timeseries.ui_edit_events

Fig. 7.1 Interactive figure created by ktk.TimeSeries.ui_edit_events#

timeseries.ui_edit_events

Fig. 7.2 Interactive menu created by ktk.TimeSeries.ui_edit_events#