I have a time series data with a few timestamps, and each timestamps has been detected with one label (action, discard, cut). Depending on these key words, the segments between these timestamps would be processed differently (keep, discard, check).
time_stamps = [[1.12, 'action'], [29.09, 'cut'], [52.36, 'action'], [87.34, 'cut'], [103.8, 'action'], [171.32, 'cut'], [191.46, 'action'], [238.56, 'action'], [257.1, 'cut']...]
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# import datetime
from datetime import date, datetime, timedelta
import time
tz = 'Europe/London'
start = pd.Timestamp(date.today(), tz=tz) # today's date
df = pd.DataFrame(columns=['Task','Start','End', "Key1", "Key2"])
not_finsh = True
i = 0
while not_finsh:
stamp_curr, stamp_next = time_stamps[i], time_stamps[i+1]
if stamp_curr[1] == "action" and stamp_next[1] == "cut":
i += 2
task = "keep"
elif stamp_curr[1] == "action" and stamp_next[1] == "discard":
i += 2
task = "discard"
else:
i += 1
task = "check"
not_finsh = i<len(time_stamps)-1
time_start = start+timedelta(seconds=stamp_curr[0])
time_end = start+timedelta(seconds=stamp_next[0])
df2 = pd.DataFrame({"Task": [task],
"Start": [time_start],
"End": [time_end],
"Key1": [stamp_curr[1]], # the starting key word
"Key2": [stamp_next[1]]}) # the ending key word
df = pd.concat([df, df2], ignore_index=True)
fig = px.timeline(df, x_start='Start', x_end='End', y='Task', color='Task')
fig.update_layout(xaxis=dict(title='Timestamp', tickformat='%M:%S.%f',))
fig.show()
a = 0