8.4. Writing CSV files#
To export a TimeSeries to a CSV file, we do the opposite sequence. We first convert the TimeSeries to a DataFrame, then we use the DataFrame’s to_csv method to create a CSV file.
In this example, we read some 3D marker positions from a sample C3D file and export these positions as a CSV file. Let’s first download and read a sample C3D file. This file is provided by https://www.c3d.org as a test suite for C3D software development. For conciseness, we will only keep the following markers: “RFT1” and “RFT2”.
import kineticstoolkit.lab as ktk
import pandas as pd
filename = ktk.doc.download("c3d_test_suite_sample.c3d")
c3d_contents = ktk.read_c3d(filename, convert_point_unit=True)
markers = c3d_contents["Points"].get_subset(["RFT1", "RFT2"])
markers
TimeSeries with attributes:
time: <array of shape (450,)>
data: <dict with 2 entries>
time_info: {'Unit': 's'}
data_info: {'RFT1': {'Unit': 'm'}, 'RFT2': {'Unit': 'm'}}
events: []
markers.data
{
'RFT1': <array of shape (450, 4)>
'RFT2': <array of shape (450, 4)>
}
To convert this TimeSeries to a CSV file, we first create a DataFrame:
df = markers.to_dataframe()
df
RFT1[0] | RFT1[1] | RFT1[2] | RFT1[3] | RFT2[0] | RFT2[1] | RFT2[2] | RFT2[3] | |
---|---|---|---|---|---|---|---|---|
0.00 | 0.248583 | 0.226833 | 0.037417 | 1.0 | 0.212667 | 0.218333 | 0.088917 | 1.0 |
0.02 | 0.249000 | 0.226750 | 0.037000 | 1.0 | 0.213167 | 0.218500 | 0.088250 | 1.0 |
0.04 | 0.249333 | 0.226333 | 0.036667 | 1.0 | 0.213333 | 0.216917 | 0.088250 | 1.0 |
0.06 | 0.248833 | 0.226667 | 0.036583 | 1.0 | 0.213000 | 0.216667 | 0.088167 | 1.0 |
0.08 | 0.249917 | 0.226250 | 0.036833 | 1.0 | 0.214167 | 0.216417 | 0.087833 | 1.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
8.90 | 0.325083 | 2.243583 | 0.034333 | 1.0 | 0.294083 | 2.237917 | 0.086333 | 1.0 |
8.92 | 0.323583 | 2.249417 | 0.032583 | 1.0 | 0.293250 | 2.238667 | 0.086167 | 1.0 |
8.94 | 0.324250 | 2.247750 | 0.033250 | 1.0 | 0.290917 | 2.238750 | 0.086833 | 1.0 |
8.96 | 0.325250 | 2.247833 | 0.034000 | 1.0 | 0.291750 | 2.239000 | 0.086250 | 1.0 |
8.98 | 0.324583 | 2.248000 | 0.033750 | 1.0 | 0.291000 | 2.238750 | 0.087083 | 1.0 |
450 rows × 8 columns
Then we export this DataFrame to a CSV file. Let’s print the first 3 lines of this file:
df.to_csv("output.csv", index_label="Time")
Here are the first lines of the exported CSV file, ready to be processed in another software:
Time,RFT1[0],RFT1[1],RFT1[2],RFT1[3],RFT2[0],RFT2[1],RFT2[2],RFT2[3]
0.0,0.24858334074169397,0.22683334009349346,0.03741666778177023,1.0,0.21266667300462724,0.21833333984017372,0.08891666931658983,1.0
0.02,0.24900000742077827,0.22675000675767662,0.03700000110268593,1.0,0.21316667301952838,0.21850000651180745,0.08825000263005495,1.0
0.04,0.24933334076404573,0.2263333400785923,0.036666667759418486,1.0,0.2133333396911621,0.2169166731312871,0.08825000263005495,1.0
0.06,0.24883334074914457,0.22666667342185975,0.03658333442360163,1.0,0.21300000634789468,0.21666667312383653,0.0881666692942381,1.0