ktk.TimeSeries#
- class TimeSeries(src=None, *, time=[], time_info={'Unit': 's'}, data={}, data_info={}, events=[])[source]#
Bases:
object
A class that holds time, data series, events and metadata.
- Parameters:
src (None | TimeSeries | pd.DataFrame | ArrayLike)
time (ArrayLike)
time_info (dict[str, Any])
data (dict[str, ArrayLike])
data_info (dict[str, dict[str, Any]])
events (list[TimeSeriesEvent])
- time#
Time attribute as 1-dimension np.array.
- Type:
np.ndarray
- data#
Contains the data, where each element contains a np.array which first dimension corresponds to time.
- Type:
dict[str, np.ndarray]
- time_info#
Contains metadata relative to time. The default is {“Unit”: “s”}
- Type:
dict[str, Any]
- data_info#
Contains facultative metadata relative to data. For example, the data_info attribute could indicate the unit of data[“Forces”]:
data["Forces"] = {"Unit": "N"}
To facilitate the management of data_info, please use ktk.TimeSeries.add_data_info.
- Type:
dict[str, dict[str, Any]]
- events#
list of events.
- Type:
list[TimeSeriesEvent]
Examples
A TimeSeries can be constructed from another TimeSeries, a Pandas DataFrame or any array with at least one dimension.
Creating an empty TimeSeries:
>>> ktk.TimeSeries() TimeSeries with attributes: time: array([], dtype=float64) data: {} time_info: {'Unit': 's'} data_info: {} events: []
Creating a TimeSeries and setting time and data:
>>> ktk.TimeSeries(time=np.arange(0, 10), data={"test":np.arange(0, 10)}) TimeSeries with attributes: time: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) data: {'test': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])} time_info: {'Unit': 's'} data_info: {} events: []
Creating a TimeSeries as a copy of another TimeSeries:
>>> ts1 = ktk.TimeSeries(time=np.arange(0, 10), data={"test":np.arange(0, 10)}) >>> ts2 = ktk.TimeSeries(ts1) >>> ts2 TimeSeries with attributes: time: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) data: {'test': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])} time_info: {'Unit': 's'} data_info: {} events: []
See Also: TimeSeries.copy
Creating a TimeSeries from a Pandas DataFrame:
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["x"] = [0., 1., 2., 3., 4.] >>> df["y"] = [5., 6., 7., 8., 9.] >>> df["z"] = [0., 0., 0., 0., 0.] >>> df x y z 0.0 0.0 5.0 0.0 0.1 1.0 6.0 0.0 0.2 2.0 7.0 0.0 0.3 3.0 8.0 0.0 0.4 4.0 9.0 0.0
>>> ts = ktk.TimeSeries(df) >>> ts TimeSeries with attributes: time: array([0. , 0.1, 0.2, 0.3, 0.4]) data: <dict with 3 entries> time_info: {'Unit': 's'} data_info: {} events: []
>>> ts.data {'x': array([0., 1., 2., 3., 4.]), 'y': array([5., 6., 7., 8., 9.]), 'z': array([0., 0., 0., 0., 0.])}
See Also: TimeSeries.from_dataframe
5. Creating a multidimensional TimeSeries from a Pandas DataFrame (using brackets in column names):
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["point[0]"] = [0., 1., 2., 3., 4.] >>> df["point[1]"] = [5., 6., 7., 8., 9.] >>> df["point[2]"] = [0., 0., 0., 0., 0.] >>> df point[0] point[1] point[2] 0.0 0.0 5.0 0.0 0.1 1.0 6.0 0.0 0.2 2.0 7.0 0.0 0.3 3.0 8.0 0.0 0.4 4.0 9.0 0.0
>>> ts = ktk.TimeSeries(df) >>> ts.data {'point': array([[0., 5., 0.], [1., 6., 0.], [2., 7., 0.], [3., 8., 0.], [4., 9., 0.]])}
See Also: TimeSeries.from_dataframe
6. Creating a multidimensional TimeSeries of higher order from a Pandas DataFrame (using brackets and commas in column names):
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["rot[0,0]"] = np.cos([0., 0.1, 0.2, 0.3, 0.4]) >>> df["rot[0,1]"] = -np.sin([0., 0.1, 0.2, 0.3, 0.4]) >>> df["rot[1,0]"] = np.sin([0., 0.1, 0.2, 0.3, 0.4]) >>> df["rot[1,1]"] = np.cos([0., 0.1, 0.2, 0.3, 0.4]) >>> df["trans[0]"] = [0., 0.1, 0.2, 0.3, 0.4] >>> df["trans[1]"] = [5., 6., 7., 8., 9.] >>> df rot[0,0] rot[0,1] rot[1,0] rot[1,1] trans[0] trans[1] 0.0 1.000000 -0.000000 0.000000 1.000000 0.0 5.0 0.1 0.995004 -0.099833 0.099833 0.995004 0.1 6.0 0.2 0.980067 -0.198669 0.198669 0.980067 0.2 7.0 0.3 0.955336 -0.295520 0.295520 0.955336 0.3 8.0 0.4 0.921061 -0.389418 0.389418 0.921061 0.4 9.0
>>> ts = ktk.TimeSeries(df) >>> ts.data {'rot': array([[[ 1. , -0. ], [ 0. , 1. ]], [[ 0.99500417, -0.09983342], [ 0.09983342, 0.99500417]], [[ 0.98006658, -0.19866933], [ 0.19866933, 0.98006658]], [[ 0.95533649, -0.29552021], [ 0.29552021, 0.95533649]], [[ 0.92106099, -0.38941834], [ 0.38941834, 0.92106099]]]), 'trans': array([[0. , 5. ], [0.1, 6. ], [0.2, 7. ], [0.3, 8. ], [0.4, 9. ]])}
See Also: TimeSeries.from_dataframe
7. Creating a TimeSeries from any array (results in a TimeSeries with a single data key named “data” and with a matching time property with a period of 1 second - unless time attribute is also defined):
>>> ktk.TimeSeries([0.1, 0.2, 0.3, 0.4, 0.5]) TimeSeries with attributes: time: array([0., 1., 2., 3., 4.]) data: {'data': array([0.1, 0.2, 0.3, 0.4, 0.5])} time_info: {'Unit': 's'} data_info: {} events: []
>>> ktk.TimeSeries([0.1, 0.2, 0.3, 0.4, 0.5], time=[0.1, 0.2, 0.3, 0.4, 0.5]) TimeSeries with attributes: time: array([0.1, 0.2, 0.3, 0.4, 0.5]) data: {'data': array([0.1, 0.2, 0.3, 0.4, 0.5])} time_info: {'Unit': 's'} data_info: {} events: []
See Also: TimeSerise.from_array
Methods
Add new data to the TimeSeries.
Add metadata to TimeSeries' data.
Add an event to the TimeSeries.
Deep copy of a TimeSeries.
Count the number of occurrence of a given event name.
Fill missing samples using a given method.
Create a new TimeSeries from an array.
Create a new TimeSeries from a Pandas Dataframe.
Get the time index that is just after the specified event occurrence.
Get the time index that is just after the specified time.
Get the time index that is closest to the specified event occurrence.
Get the time index that is closest to the specified time.
Get the time index that is just before the specified event occurrence.
Get the time index that is just before the specified time.
Get the sample rate in samples/s.
Return a subset of the TimeSeries.
Get a TimeSeries after the specified event.
Get a TimeSeries after the specified time index.
Get a TimeSeries after the specified time.
Get a TimeSeries before the specified event.
Get a TimeSeries before the specified time index.
Get a TimeSeries before the specified time.
Get a TimeSeries between two specified events.
Get a TimeSeries between two specified time indexes.
Get a TimeSeries between two specified times.
Return a boolean array of missing samples.
Merge the TimeSeries with another TimeSeries.
Plot the TimeSeries in the current matplotlib figure.
Remove a data key and its associated metadata.
Remove metadata from a TimeSeries' data.
Remove events with same name and time so that each event gets unique.
Remove an event occurrence or all events of a same name.
Rename a key in data and data_info.
Rename an event occurrence or all events of a same name.
Resample the TimeSeries.
Shift time and events.time.
Deprecated.
Create a DataFrame by reshaping all data to one bidimensional table.
Delete the events that are outside the TimeSeries' time attribute.
Edit events interactively.
Synchronize one or two TimeSeries by shifting their time.
Attributes