-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-plot.py
executable file
·280 lines (241 loc) · 8.45 KB
/
js-plot.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/python
# File: js-plot.py
# Description: plot joystick event recordings
# Created: 2017-06-05
import js
import argparse
import sys
from common import ArgvError
class PlotHandler(js.Handler):
lasttime = None
def __init__(self, evs, allstates, adapters):
js.Handler.__init__(self, evs)
self.allstates = allstates
self.origin_to_adapters = origin_to_adapters = {}
self.recorded_events = ev = {}
self.directional = set()
for adapter in adapters:
ev[adapter] = []
for o in adapter.origin:
try:
olist = origin_to_adapters[o]
except KeyError:
origin_to_adapters[o] = olist = []
olist.append(adapter)
def initevents(self, time):
for adapter, events in self.recorded_events.items():
value = adapter(self.allstates)
if isinstance(value, (tuple, list)):
self.directional.add(adapter)
events.append((time, value))
def handle_event(self, event):
type = event.type & ~js.TY_INIT_BIT
num = event.number
spec = (type, num)
time = event.time
self.lasttime = time
for adapter in self.origin_to_adapters.get(spec, ()):
events = self.recorded_events[adapter]
prevevent = events[-1]
if prevevent[0] < time - 10:
events.append((time - 10, *prevevent[1:]))
events.append((time, adapter(self.allstates)))
def calculate_directions(events):
import numpy as np
array = np.empty((len(events), 3), dtype=np.float64)
for i, (time, values) in enumerate(events):
array[i, 0] = time
array[i, 1:] = values[0], values[1]
times = array[:, 0]
magnitudes = np.hypot(array[:, 1], array[:, 2])
directions = np.arctan2(array[:, 2], array[:, 1])
return times, magnitudes, directions
def calculate_major_directions(times, magnitudes, directions):
import numpy as np
result = []
curstart = None
dirmin = 0
dirmax = 0
maxmag = 0
dir_threshold = np.pi * .3
def endit():
nonlocal curstart
if curstart is not None:
result.append(((curstart + time) * .5,
(dirmin + dirmax) * .5,
maxmag))
curstart = None
def startit():
nonlocal curstart, dirmin, dirmax, maxmag
curstart = time
dirmin = dir
dirmax = dir
maxmag = mag
for time, mag, dir in zip(times, magnitudes, directions):
if mag < .05:
endit()
continue
if curstart is None:
startit()
continue
d = dir
if d < dirmax - np.pi:
d += np.pi * 2
elif d > dirmin + np.pi:
d += np.pi * 2
if d > dirmax:
if d - dirmin > dir_threshold:
endit()
startit()
continue
dirmax = d
elif d < dirmin:
if dirmax - d > dir_threshold:
endit()
startit()
continue
dirmin = d
if mag > maxmag:
maxmag = mag
endit()
return result
COLORS = {
'A': '#3dba41',
'B': '#ff5353',
'X': '#535fff',
'Y': '#ffc653',
'BACK': '#00c2ca',
'START': '#ca0098',
'GUIDE': '#cfaeff',
'RB': '#30ff30',
'LB': '#3030ff',
'RT': '#ff3030',
'LT': '#ff880a',
'STL': '#5b5b99',
'STL_X': '#5b5b99',
'STL_Y': '#5b5b99',
'STR': '#995b5b',
'STR_X': '#995b5b',
'STR_Y': '#995b5b',
}
PLOT_IDS = {
'STL': 2,
'STL_X': 2,
'STL_Y': 2,
'STR': 2,
'STR_X': 2,
'STR_Y': 2,
'RB': 3,
'LB': 3,
'RT': 3,
'LT': 3,
}
def convert_timearg(s, default=None):
if s is not None:
return int(float(s) * 1000)
else:
return default
def main(argv):
progname = argv.pop(0).rpartition('/')[2]
parser = argparse.ArgumentParser(prog=progname)
parser.add_argument('-d', '--delay', default=None, help="Additional start delay in seconds")
parser.add_argument('-s', '-ss', '--start', default=None, help="Start time in seconds (opposite of --delay)")
parser.add_argument('-S', '--absolute-start', default=None, help="Absolute start time (additional to -s)")
parser.add_argument('-T', '--type', default='auto', help="Specify the controller type to use")
parser.add_argument('-i', '--inputs', nargs='*',
default=['STL_X', 'STL_Y', 'STR_X', 'STR_Y', 'LT', 'RT', 'LB', 'RB', 'BACK', 'START', 'GUIDE', 'A', 'B', 'X', 'Y'],
help="Specify controller inputs to plot")
endgroup = parser.add_mutually_exclusive_group()
endgroup.add_argument('-to', '--until', default=None, help="End time in seconds after --delay")
endgroup.add_argument('-t', '--duration', default=None, help="Duration in seconds after the actual start time")
args = parser.parse_args(argv)
args.start = convert_timearg(args.start, 0)
if args.absolute_start is not None:
args.absstart = int(args.absolute_start)
else:
args.absstart = 0
args.delay = convert_timearg(args.delay, 0)
args.until = convert_timearg(args.until, None)
args.duration = convert_timearg(args.duration, None)
import overlayapi as api
api.import_all_config()
try:
ctype = api.CONTROLLER_TYPES[args.type]()
except KeyError:
raise ArgvError("no such controller type: %r" % (args.type,), parser)
adapters = [api.to_adapter(getattr(ctype, name)) for name in args.inputs]
evs = js.HandlerJsEvents(sys.stdin)
ctype.attach_events(evs)
allstates = js.AllstatesHandler(evs)
allstates.attach()
evs.work_all(until='initialized')
evs.work_all(until=args.absstart)
firsttime = evs.previous_event.time
# read until we reach our start time
starttime = firsttime + args.start - args.delay
evs.work_all(until=starttime)
endtime = None
if args.until is not None:
endtime = firsttime + args.until - args.delay
elif args.duration is not None:
endtime = firsttime + args.duration + args.start - args.delay
plotter = PlotHandler(evs, allstates, adapters)
plotter.initevents(starttime)
plotter.attach()
evs.work_all(until=endtime)
import matplotlib.pyplot as plt
import numpy as np
plotnums = set()
for name in args.inputs:
plotnums.add(PLOT_IDS.get(name, 1))
plotmap = {num: i for i, num in enumerate(sorted(list(plotnums)), 1)}
firstplot = None
plots = {}
for num, i in plotmap.items():
plot = plt.subplot(len(plotmap), 1, i, sharex=firstplot,
xlabel='time (seconds)', ylabel="value")
if firstplot is None:
firstplot = plot
plots[num] = (plot, [0, 0])
for name, adapter in zip(args.inputs, adapters):
num = PLOT_IDS.get(name, 1)
events = plotter.recorded_events[adapter]
if adapter in plotter.directional:
times, values, directions = calculate_directions(events)
major_directions = calculate_major_directions(
(times - starttime) * .001, values, directions)
else:
directions = ()
events = np.array(events)
times = events[:,0]
values = events[:,1]
times = (times - starttime) * .001
color = COLORS.get(name, '#000000')
plot, ranges = plots[num]
if any(values < 0):
ranges[0] = -1
if any(values > 0):
ranges[1] = 1
if len(directions):
for time, angle, maxmag in major_directions:
plot.text(time, maxmag + .1, "→", ha='center', va='center',
color=color, rotation=(- angle * 180 / np.pi))
plot.fill_between(times, values, 0,
where=abs(values)>.01, alpha=0.5,
color=color, label=name)
if plotter.lasttime is None:
print("no events")
else:
end = plotter.lasttime - starttime
end *= .001
for plot, ranges in plots.values():
plot.vlines([end], ranges[0], ranges[1], '#000000',
label='end', linewidth=1, linestyle='-.')
for plot, ranges in plots.values():
plot.legend()
plt.show()
return 0
if __name__ == '__main__':
from common import run_main
run_main(main)
# vim:set sw=4 ts=8 sts=4 et sr ft=python fdm=marker tw=0: