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/5ce977aac62da05eede5e2c4a8cc4ba4cc9162be7720eb3bee3f8ffb0d078045.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/b4bb6eb553fbbfa9a70eb0317fdb032e99336e766674f3fa2faa764e73f5b630.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/19a338c85fa23c1512ab4e0c522a2512fdc0a50fb981d389a9dd1cf7c6680ab5.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/e693f5fc528d66f2abef0211091492744277569c05e7d8439a63df31872ea96d.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/56cccedcf6b0c77af24d0b00d6b358be9a51584eba2d85314a33b716446f755d.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/19a338c85fa23c1512ab4e0c522a2512fdc0a50fb981d389a9dd1cf7c6680ab5.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/0ff9e889183b18f8a31adcbee147fb91a8753ad24301b01399f73428267c2d40.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/19a338c85fa23c1512ab4e0c522a2512fdc0a50fb981d389a9dd1cf7c6680ab5.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 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#