-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualize.py
201 lines (157 loc) · 5.34 KB
/
Visualize.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import argparse
from Graph import *
from Node_Person import Person
import matplotlib.pyplot as plt
import numpy as np
if __name__=="__main__":
args=argparse.ArgumentParser()
args.add_argument("--input","-i",type=str,dest="input")
args.add_argument("--person","-p",type=str,dest="person")
args.add_argument("--output","-o",type=str,dest="output")
args.add_argument("--outputPrefix","-op",type=str,dest="outputPrefix")
args.add_argument("--onlyDetectedTime",type=bool,dest="onlyDetectedTime"\
,default=False)
args.add_argument("--interpolateUndetected",type=bool,\
dest="interpolateUndetected",default=False)
args=args.parse_args()
print("---")
graph=Graph()
graph.init_from_json(args.input)
graph.calculate_standing_locations()
if "," not in args.person:
args.person=int(args.person)
p = graph.getNode(args.person)
if args.interpolateUndetected:
p.interpolate_undetected_timestamps()
else:
p.calculate_detected_time_period()
p = p.params
fig, axs = plt.subplots(2)
fig.suptitle('Information of person : {}'.format(args.person))
# axs[0].plot(x, y)
# axs[1].plot(x, -y)
X=p["X"]
Y=p["Y"]
# print("Before trimming : ",len(X),len(Y))
if args.onlyDetectedTime:
X=X[p["detectionStartT"]:p["detectionEndTExclusive"]]
Y=Y[p["detectionStartT"]:p["detectionEndTExclusive"]]
# print("After trimming : ",len(X),len(Y))
# print(p["detection"][p["detectionStartT"]:p["detectionEndTExclusive"]])
legendWord=[]
axs[0].scatter(X[0],Y[0],color='r',s=40)
legendWord.append("Start")
axs[0].scatter(X[-1],Y[-1],color='b',s=40)
legendWord.append("End")
axs[0].set_xlabel("Spatial dimension (x)")
axs[0].set_ylabel("Spatial dimension (y)")
boolFlagFirstRealPathPlotted=False
boolFlagFirstInterpolatedPathPlotted=False
for dot in range(1,len(X)-1):
# print(p["interpolated"][dot])
if p["detection"][p["detectionStartT"]+ dot]==True:
if not boolFlagFirstRealPathPlotted:
axs[0].scatter(X[dot],Y[dot],color='g',s=10)
boolFlagFirstRealPathPlotted=True
legendWord.append("Path (detected)")
else:
axs[0].scatter(X[dot],Y[dot],color='g',s=10,label='_nolegend_')
else:
if not boolFlagFirstInterpolatedPathPlotted:
axs[0].scatter(X[dot],Y[dot],color='orange',s=10)
boolFlagFirstInterpolatedPathPlotted=True
legendWord.append("Path (interpolated)")
else:
axs[0].scatter(X[dot],Y[dot],color='orange',s=10,label='_nolegend_')
axs[0].legend(legendWord)
for a in range(min(len(X),len(Y))-1):
axs[0].arrow(X[a],Y[a],\
X[a+1]-X[a],Y[a+1]-Y[a],overhang=0)
# plt.show()
# plt.figure()
keys=list(p.keys())
for k in ["X","Y","xMin","xMax","yMin","yMax",\
"handshake","neverDetected","detectionStartT","detectionEndTExclusive"]:
keys.remove(k)
print("Time series to plot: ",keys)
booleanFunctionsToPlot=0
for k in keys:
if type(p[k])==list:
booleanFunctionsToPlot+=1
toPlot=[]
for k in keys:
if type(p[k])==list:
ar=np.array(p[k],dtype=float)
ar=ar-np.min(ar)
ar=ar/np.max(ar)
toPlot.append(ar)
axs[1].plot(np.arange(len(ar)),ar,"-")
axs[1].set_xlabel("time (t)")
axs[1].set_ylabel("parameter f(t)")
axs[1].legend(keys)
else:
pp=list(map(int,args.person.strip().split(",")))
maxLen=0
for p in pp:
p=graph.getNode(p).params
maxLen=max(maxLen,max(len(p["X"]),len(p["Y"])))
locX=np.zeros((len(pp),maxLen),dtype=np.float)
locY=np.zeros((len(pp),maxLen),dtype=np.float)
for p in range(len(pp)):
person=graph.getNode(pp[p]).params
locX[p,:len(person["X"])]=np.array(person["X"],dtype=np.float)
locY[p,:len(person["Y"])]=np.array(person["Y"],dtype=np.float)
cogX=np.mean(locX,axis=0)
cogY=np.mean(locY,axis=0)
print(cogX,cogY)
fig, axs = plt.subplots(2)
legLine=[]
legLine.append(axs[0].scatter(cogX[0],cogY[0],color='r'))
legLine.append(axs[0].scatter(cogX[1:-1],cogY[1:-1],color='g'))
legLine.append(axs[0].scatter(cogX[-1],cogY[-1],color='b'))
for a in range(cogX.shape[0]-1):
axs[0].arrow(cogX[a],cogY[a],\
cogX[a+1]-cogX[a],cogY[a+1]-cogY[a])
legWord=["Start","Path (detected)","Path (interpolated)","End"]
for p in range(len(pp)):
person=graph.getNode(pp[p]).params
x=person["X"]
y=person["Y"]
minLen=min(len(x),len(y))
x=x[:minLen]
y=y[:minLen]
legLine.append(axs[0].scatter(x,y,marker="."))
for a in range(minLen):
axs[0].arrow(cogX[a],cogY[a],x[a]-cogX[a],y[a]-cogY[a],
linestyle="dotted")
legWord.append("P{}".format(p))
axs[0].legend(legLine,legWord)
distX=np.array(locX)
distY=np.array(locY)
dist=distX.fill(0.0)
for n in range(len(graph.nodes)):
distX[n,:]=distX[n,:]-cogX
distY[n,:]=distY[n,:]-cogY
# print(distX.shape)
dist=np.sqrt(np.square(distX)+np.square(distY))
legLine=[]
legWord=[]
axs[1].plot(np.mean(dist,axis=0))
legWord.append("Group dist from COG")
for d in range(dist.shape[0]):
axs[1].plot(dist[d],":")
legWord.append("P {}".format(d))
axs[1].legend(legWord)
print("Dist",dist)
plt.tight_layout()
if False:#args.output==None:
plt.show()
else:
if args.output != None:
plt.savefig(args.output)
elif args.outputPrefix !=None:
fileName="{}-GRAPH-{}-PERSON-{}.png".format(args.outputPrefix,\
args.input.replace(".json","").replace("/","-"),str(args.person).replace(",","-"))
plt.savefig(fileName,dpi=300)
print("Saved figure to {}".format(fileName))
# print(pp)