-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvt_viewer.py
238 lines (171 loc) · 6.7 KB
/
mvt_viewer.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
import pygame
import time
class mvt_viewer(object):
def __init__(self, surface, audio_cues, state, scale_min=None, scale_max=None, fps=25):
"""
:param height: Height of pygame window
:param width: Width of pygame window
:param surface: pygame surface to modify when called upon
:param scale_min: minimum value of range of data values
:param scale_max: maximum value of range of data values
:param fps: approxmate frames per second
"""
self.audio_cues = audio_cues
self.state = state
self.width, self.height = surface.get_size()
self.cache = []
# The MVT match circle is 80% the width of the screen
self.match_circle_perc = .8
self.prev_time = time.time()
self.frame_time = 1.0/fps
if scale_max:
self.scale_max = scale_max
else:
self.scale_max = self.height
if scale_min:
self.scale_min = scale_min
else:
self.scale_min = 0
self.running = True
eighth = self.width // 8
self.screen = surface
'''To be used in mode selection
CLEAR displays a white screen
DISPLAY_MVT just does the regular displaying indefinitely
automation_start()
DISPLAY_MVT_0 does regular display for certain seconds before playing
sound
DISPLAY_MVT_1 collects data for X seconds
DISPLAY_MVT_2 waits until average isn't increasing anymore
"Relax"
Set back to DISPLAY_MVT or CLEAR
'''
self.refrence_time = time.time()
self.internal_mode = "DISPLAY_MVT"
# Just clears the screen
def blank_screen(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
return ("EXIT")
self.screen.fill((255, 255, 255))
pygame.display.update()
return True
def transform(self, value):
"""
Internal method
Does the range mapping to generate the radius of the circle
:param value: The raw datapoint to be mapped into a radius
:return: the transformed value
"""
temp = abs((self.width * self.match_circle_perc) * (value / (self.scale_max-self.scale_min))) / 2
temp = max(temp, 6)
return int(temp)
def mode_process(self, rad):
time_0, time_1 = 2, 5
default = "DISPLAY_MVT"
self.state[0] = self.internal_mode
'''To be used in mode selection
CLEAR displays a white screen
DISPLAY_MVT just does the regular displaying indefinitely
automation_start()
DISPLAY_MVT_0 does regular display for time_0 seconds before playing sound
play sound cue
DISPLAY_MVT_1 collects data for tine_1 seconds
DISPLAY_MVT_2 waits until average isn't increasing anymore
DISPLAY_MVT_3 triggers saving
"Relax"
Set back to DISPLAY_MVT or CLEAR
'''
if self.internal_mode == "CLEAR":
return self.blank_screen
elif self.internal_mode == "DISPLAY_MVT":
return self.one_step(rad, (200, 200, 200))
elif self.internal_mode == "DISPLAY_MVT_0":
if time.time() - self.refrence_time > time_0:
self.internal_mode = "DISPLAY_MVT_1"
self.refrence_time = time.time()
self.audio_cues['pull hard'].play()
return self.one_step(rad)
elif self.internal_mode == "DISPLAY_MVT_1":
if time.time() - self.refrence_time > time_1:
self.refrence_time = time.time()
self.internal_mode = "DISPLAY_MVT_2"
return self.one_step(rad)
elif self.internal_mode == "DISPLAY_MVT_2":
# TODO: Logic for waiting until stabilitzation
self.refrence_time = time.time()
self.internal_mode = "DISPLAY_MVT_3"
return self.one_step(rad)
elif self.internal_mode == "DISPLAY_MVT_3":
self.audio_cues['relax'].play()
self.internal_mode = default
return (f"SAVE,{self.get_max_value()}")
else:
return self.one_step(rad)
def begin_automation(self):
"""
sets the starting refrence time for the MVT collection system
Also sets the internal mode, and plays the "Start cue"
:return: Nothing
"""
self.refrence_time = time.time()
self.internal_mode = "DISPLAY_MVT_0"
self.audio_cues['starting'].play()
def one_step(self, rad, color=(255, 0, 0)):
"""
Does one drawing step for pygame
Also handles pygame events; returns False if the pygame window should be exited
:return: if the program should be quit/ exited
"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
return ("EXIT")
self.screen.fill((255,255,255))
center = (self.width//2, self.height//2)
target_circle_rad = int(self.width*self.match_circle_perc*.5)
pygame.draw.circle(self.screen, (0,0,0), center, target_circle_rad-5, 10)
pygame.draw.circle(self.screen, color, center, rad, 6)
pygame.display.update()
return str(self.running)
def run(self):
"""
testing function
is not used in production
"""
while self.running:
self.one_step()
def process_data(self, data, continue_run=True):
"""
the step that does it all
Takes in a single datapoint, adds it to it's internal cache
If its internal FPS counter indicates so, it sets the circle,
and processes a new frame
:param data: Single numerical datapoint to be correlated with the scale_min/ scale_max
that results in the adjusting of the height. Will be added to internal buffer for eventual
MVT calculation
"""
# If there are adiquate data points, begin a scrolling average
if len(self.cache) > 250:
avg_value = sum(self.cache[-249:]) + data
avg_value /= 250
self.cache.append(avg_value)
else:
self.cache.append(data)
self.running = continue_run
if time.time() - self.prev_time > self.frame_time:
self.prev_time = time.time()
return self.mode_process(self.transform(data))
else:
return "True"
def clear_cache(self):
"""
Clears the data cache
"""
self.cache.clear()
def get_max_value(self):
"""
Returns the maximum value from the internal cache.
"""
return max(self.cache)