-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnline segmentation.py
145 lines (132 loc) · 5.51 KB
/
Online segmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pandas as pd
import numpy as np
import os, sys, glob
import platform
import time
import matplotlib.pyplot as plt
import yaml
import datetime
from matplotlib.font_manager import FontProperties
from matplotlib.cm import get_cmap
import pickle
from utils.common import *
# framerate = 50.0
EPS = 1e-15
theta=0.005
if __name__ == '__main__':
#All global variables
# if platform.system() == 'Windows':
# path_ = 'Y:'
# elif platform.system() == 'Linux':
# path_ = '/mnt/Y'
# path = path_ + '/Football/soccer_experiments/soccer-kickups/processed_ball_and_pose/'
# file_path = path + '00049_alex_kickups_01_jointsball.csv'
#
# df_ball = pd.read_csv(file_path, usecols=['frame_idx','top','bottom','left','right'], index_col='frame_idx') #ball data
# df_ball_y= pd.DataFrame({'BallY': df_ball['bottom']},
# index=df_ball.index)
# df_ball=df_ball_y.reset_index().drop_duplicates('frame_idx').set_index('frame_idx')
#our time serie
# P = df_ball['BallY']
# Index = P.index
# testing on other time series (synthetic)
#sinusoidal
Fs = 1000
f = 10
x = np.arange(0, 100, 1)
y = np.abs(np.sin(2 * np.pi * f * x / Fs))
#quadratic
# x = np.arange(0, 100, 1)
# y = x** 2 + 5 *x + 10
# y = x ** 2 - 25 * x + 510
P = pd.DataFrame(y, index=x, columns=['Y'])['Y']
Index=P.index
# Initialization
F = ['linear','quadratic'] # Linear , quadtratic candidate funcitons
params=[2,3]
lg = [] # list of approx params of global segments of the time series
ll = [[] for j in range(len(F))] # list of lists of approx params of local segments for each candidate function
lla=[]
p_first = Index[0]
p_start = []
for j in range(len(F)):
p_start.append(Index[0])
FCS = [Polygon() for j in range(len(F))] # feasible coeff space of each candidate func
Fcf = False # Whether a candidate function has been picked as approx func or not
Frv = False # whether the chosen approx func is not longer feasible for further approx
#Finding Segmenting Points
i = 0
last = False
while i < len(Index)-1:
i += 1
p_next = Index[i]
if i==len(Index)-1:
last=True
if not Fcf: # have not chosen an apporx func for the current subsequence
for j in range(len(F)):
fcs = FCSA(F[j], FCS[j], p_start[j], p_next)
if fcs.isempty(): #Look for a better condition
ll[j].append((p_start[j],Index[i-1],FCS[j])) # figure out what to append as local segment (start,end,..)
p_start[j] = Index[i-1]
fcs = FCSA(F[j], FCS[j], p_start[j], p_next)
FCS[j] = fcs
local_seg_for_all_fc = True #all llj!=empty
for j in range(len(F)):
if not ll[j]:
local_seg_for_all_fc = False
if local_seg_for_all_fc or last:
nbp=[0 for j in range(len(F))]
for j in range(len(F)):
# calculate np between p_first and p_next-1
if last: # Changes in here to handle the end of the time series
ll[j].append((p_start[j],p_next,FCS[j]))
p_start[j]=Index[i-1]
nbp[j]=params[j]*len(ll[j]) #not so sure about this
# choose fa with the min np and (error later) lla=(p_start_a,np,cf,p_end_a)
# print (nbp , ll[0], ll[1])
fa=nbp.index(min(nbp))
if (p_start[fa] != Index[i-1]):
Fcf = True
else:
Frv = True
lg.append((ll[fa],F[fa]))
else: # have chosen approx func
FCS[fa] = FCSA(F[fa], FCS[fa], p_start[fa], p_next)
if FCS[fa].isempty():
ll[fa].append((p_start[fa],Index[i-1],FCS[fa]))
lg.append((ll[fa],F[fa]))
Fcf = False
Frv = True
elif last: # Changes in here to handle the end of the time series
ll[fa].append((p_start[fa],p_next,FCS[fa]))
lg.append((ll[fa],F[fa]))
if Frv:
p_first=Index[i-1]
for j in range(len(F)):
ll[j]=[]
p_start[j] = Index[i - 1]
FCS[j]=FCSA(F[j],FCS[j],p_start[j],p_next)
Frv = False
#AA alogorithm ends here
print (len(lg))
for seg in lg:
print (seg)
#Plotting Results
plt.scatter(P.index, P.values, marker='x')
ax=plt.gca()
starts=[[] for j in range(len(F))]
ends = [[] for j in range(len(F))]
for segment in lg:
for local_seg in segment[0]:
if segment[1]=='linear':
starts[0].append(local_seg[0])
ends[0].append(local_seg[1])
elif segment[1]=='quadratic':
starts[1].append(local_seg[0])
ends[1].append(local_seg[1])
ax.vlines(starts[0],ymin=ax.get_ylim()[0],ymax=ax.get_ylim()[1],color='g',label='linear_start')
# ax.vlines(ends[0], ymin=ax.get_ylim()[0], ymax=ax.get_ylim()[1], color='g', label='linear_end',linestyles='--')
ax.vlines(starts[1],ymin=ax.get_ylim()[0], ymax=ax.get_ylim()[1], color='r', label='quadratic_start')
# ax.vlines(ends[0], ymin=ax.get_ylim()[0], ymax=ax.get_ylim()[1], color='r', label='quadratic_end',linestyles='--')
ax.legend(bbox_to_anchor=(1.005, 0.5))
plt.show()