1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | # Copyright Puzzlebox Productions, LLC (2010-2014) |
---|
4 | # |
---|
5 | # This code is released under the GNU Pulic License (GPL) version 2 |
---|
6 | # For more information please refer to http://www.gnu.org/copyleft/gpl.html |
---|
7 | |
---|
8 | __changelog__ = """\ |
---|
9 | Last Update: 2014.02.23 |
---|
10 | """ |
---|
11 | |
---|
12 | __todo__ = """ |
---|
13 | """ |
---|
14 | |
---|
15 | ### IMPORTS ### |
---|
16 | import os, sys, time |
---|
17 | |
---|
18 | import Puzzlebox.Synapse.Configuration as configuration |
---|
19 | |
---|
20 | if configuration.ENABLE_PYSIDE: |
---|
21 | try: |
---|
22 | import PySide as PyQt4 |
---|
23 | from PySide import QtCore, QtGui |
---|
24 | except Exception, e: |
---|
25 | print "ERROR: [Synapse:Session] Exception importing PySide:", |
---|
26 | print e |
---|
27 | configuration.ENABLE_PYSIDE = False |
---|
28 | else: |
---|
29 | print "INFO: [Synapse:Session] Using PySide module" |
---|
30 | |
---|
31 | if not configuration.ENABLE_PYSIDE: |
---|
32 | print "INFO: [Synapse:Session] Using PyQt4 module" |
---|
33 | from PyQt4 import QtCore, QtGui |
---|
34 | |
---|
35 | |
---|
36 | try: |
---|
37 | import cPickle as pickle |
---|
38 | except: |
---|
39 | import pickle |
---|
40 | |
---|
41 | |
---|
42 | ##################################################################### |
---|
43 | # Globals |
---|
44 | ##################################################################### |
---|
45 | |
---|
46 | DEBUG = configuration.DEBUG |
---|
47 | |
---|
48 | DEFAULT_SIGNAL_LEVEL_MESSAGE = \ |
---|
49 | {"poorSignalLevel": 0} |
---|
50 | # A quantifier of the quality of the brainwave signal. |
---|
51 | # This is an integer value that is generally in the |
---|
52 | # range of 0 to 200, with 0 indicating a |
---|
53 | # good signal and 200 indicating an off-head state. |
---|
54 | |
---|
55 | DEFAULT_EEG_POWER_MESSAGE = \ |
---|
56 | {"eegPower": { \ |
---|
57 | 'delta': 0, \ |
---|
58 | 'theta': 0, \ |
---|
59 | 'lowAlpha': 0, \ |
---|
60 | 'highAlpha': 0, \ |
---|
61 | 'lowBeta': 0, \ |
---|
62 | 'highBeta': 0, \ |
---|
63 | 'lowGamma': 0, \ |
---|
64 | 'highGamma': 0, \ |
---|
65 | }, \ |
---|
66 | } # A container for the EEG powers. These may |
---|
67 | # be either integer or floating-point values. |
---|
68 | # Maximum values are undocumented but assumed to be 65535 |
---|
69 | |
---|
70 | DEFAULT_ESENSE_MESSAGE = \ |
---|
71 | {"eSense": { \ |
---|
72 | 'attention': 0, \ |
---|
73 | 'meditation': 0, \ |
---|
74 | }, \ |
---|
75 | } # A container for the eSense⢠attributes. |
---|
76 | # These are integer values between 0 and 100, |
---|
77 | # where 0 is perceived as a lack of that attribute |
---|
78 | # and 100 is an excess of that attribute. |
---|
79 | |
---|
80 | DEFAULT_BLINK_MESSAGE = {"blinkStrength": 255} |
---|
81 | # The strength of a detected blink. This is |
---|
82 | # an integer in the range of 0-255. |
---|
83 | |
---|
84 | DEFAULT_RAWEEG_MESSAGE = {"rawEeg": 255} |
---|
85 | # The raw data reading off the forehead sensor. |
---|
86 | # This may be either an integer or a floating-point value. |
---|
87 | |
---|
88 | DEFAULT_PACKET = {} |
---|
89 | DEFAULT_PACKET.update(DEFAULT_EEG_POWER_MESSAGE) |
---|
90 | DEFAULT_PACKET.update(DEFAULT_SIGNAL_LEVEL_MESSAGE) |
---|
91 | DEFAULT_PACKET.update(DEFAULT_ESENSE_MESSAGE) |
---|
92 | |
---|
93 | PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD = 0.75 |
---|
94 | |
---|
95 | ##################################################################### |
---|
96 | # Classes |
---|
97 | ##################################################################### |
---|
98 | |
---|
99 | class puzzlebox_synapse_session(QtGui.QWidget): |
---|
100 | |
---|
101 | def __init__(self, log, \ |
---|
102 | DEBUG=DEBUG, \ |
---|
103 | parent=None, \ |
---|
104 | ): |
---|
105 | |
---|
106 | self.log = log |
---|
107 | self.DEBUG = DEBUG |
---|
108 | self.parent=parent |
---|
109 | |
---|
110 | if self.parent == None: |
---|
111 | QtGui.QWidget.__init__(self, parent) |
---|
112 | #self.setupUi(self) |
---|
113 | |
---|
114 | self.configureSettings() |
---|
115 | self.connectWidgets() |
---|
116 | |
---|
117 | self.name = "Synapse:Session" |
---|
118 | |
---|
119 | |
---|
120 | if (sys.platform == 'win32'): |
---|
121 | self.homepath = os.path.join( \ |
---|
122 | os.environ['HOMEDRIVE'], \ |
---|
123 | os.environ['HOMEPATH'], \ |
---|
124 | 'Desktop') |
---|
125 | elif (sys.platform == 'darwin'): |
---|
126 | desktop = os.path.join(os.environ['HOME'], 'Documents') |
---|
127 | if os.path.exists(desktop): |
---|
128 | self.homepath = desktop |
---|
129 | else: |
---|
130 | self.homepath = os.environ['HOME'] |
---|
131 | else: |
---|
132 | desktop = os.path.join(os.environ['HOME'], 'Desktop') |
---|
133 | if os.path.exists(desktop): |
---|
134 | self.homepath = desktop |
---|
135 | else: |
---|
136 | self.homepath = os.environ['HOME'] |
---|
137 | |
---|
138 | |
---|
139 | if not os.path.exists(self.homepath): |
---|
140 | if self.DEBUG: |
---|
141 | print "WARNING: [Synapse:Session] User default path not found" |
---|
142 | self.homepath = os.getcwd() |
---|
143 | |
---|
144 | |
---|
145 | ################################################################## |
---|
146 | |
---|
147 | def configureSettings(self): |
---|
148 | |
---|
149 | pass |
---|
150 | |
---|
151 | |
---|
152 | ################################################################## |
---|
153 | |
---|
154 | def connectWidgets(self): |
---|
155 | |
---|
156 | pass |
---|
157 | |
---|
158 | |
---|
159 | ################################################################## |
---|
160 | |
---|
161 | def updateProfileSessionStatus(self, source=None, target=None): |
---|
162 | |
---|
163 | session_time = self.calculateSessionTime() |
---|
164 | |
---|
165 | #if source == None: |
---|
166 | #if self.parent == None: |
---|
167 | #source = self |
---|
168 | #else: |
---|
169 | #source = self.parent |
---|
170 | |
---|
171 | #if target == None: |
---|
172 | #if self.parent == None: |
---|
173 | #target = self |
---|
174 | #else: |
---|
175 | #target = self.parent |
---|
176 | |
---|
177 | #target.textLabelSessionTime.setText(session_time) |
---|
178 | self.textLabelSessionTime.setText(session_time) |
---|
179 | |
---|
180 | #self.parent.packet_count) |
---|
181 | #self.synapseServer.protocol.packet_count) |
---|
182 | |
---|
183 | try: |
---|
184 | packet_count = self.parent.plugin_eeg.getPacketCount() |
---|
185 | except: |
---|
186 | try: |
---|
187 | packet_count = self.synapseServer.protocol.packet_count |
---|
188 | except: |
---|
189 | packet_count = 0 |
---|
190 | |
---|
191 | self.textLabelPacketsReceived.setText( "%i" % packet_count) |
---|
192 | |
---|
193 | |
---|
194 | try: |
---|
195 | bad_packets = self.parent.plugin_eeg.getBadPackets() |
---|
196 | except: |
---|
197 | try: |
---|
198 | bad_packets = self.synapseServer.protocol.bad_packets |
---|
199 | except: |
---|
200 | bad_packets = 0 |
---|
201 | |
---|
202 | self.textLabelPacketsDropped.setText( "%i" % bad_packets) |
---|
203 | |
---|
204 | |
---|
205 | ################################################################## |
---|
206 | |
---|
207 | def calculateSessionTime(self): |
---|
208 | |
---|
209 | session_time = self.getSessionTime() |
---|
210 | |
---|
211 | session_time = time.time() - session_time |
---|
212 | session_time = int(session_time) |
---|
213 | session_time = self.convert_seconds_to_datetime(session_time) |
---|
214 | |
---|
215 | return (session_time) |
---|
216 | |
---|
217 | |
---|
218 | ################################################################## |
---|
219 | |
---|
220 | def getSessionTime(self): |
---|
221 | |
---|
222 | return (self.synapseServer.session_start_timestamp) |
---|
223 | |
---|
224 | |
---|
225 | ################################################################## |
---|
226 | |
---|
227 | def collectData(self, source=None, target=None): |
---|
228 | |
---|
229 | if source == None: |
---|
230 | if self.parent == None: |
---|
231 | source = self |
---|
232 | else: |
---|
233 | source = self.parent |
---|
234 | |
---|
235 | if target == None: |
---|
236 | if self.parent == None: |
---|
237 | target = self |
---|
238 | else: |
---|
239 | target = self.parent |
---|
240 | |
---|
241 | data = {} |
---|
242 | |
---|
243 | data['rawEeg'] = source.packets['rawEeg'] |
---|
244 | data['signals'] = source.packets['signals'] |
---|
245 | |
---|
246 | data['sessionTime'] = self.calculateSessionTime() |
---|
247 | |
---|
248 | data['profileName'] = str(target.lineEditSessionProfile.text()) |
---|
249 | |
---|
250 | return(data) |
---|
251 | |
---|
252 | |
---|
253 | ################################################################## |
---|
254 | |
---|
255 | def parseTimeStamp(self, timestamp, local_version=False, truncate_time_zone=False): |
---|
256 | |
---|
257 | try: |
---|
258 | decimal = '%f' % timestamp |
---|
259 | decimal = decimal.split('.')[1] |
---|
260 | except: |
---|
261 | decimal = '0' |
---|
262 | |
---|
263 | localtime = time.localtime(timestamp) |
---|
264 | |
---|
265 | if local_version: |
---|
266 | date = time.strftime('%x', localtime) |
---|
267 | localtime = time.strftime('%X', localtime) |
---|
268 | |
---|
269 | elif truncate_time_zone: |
---|
270 | date = time.strftime('%Y-%m-%d', localtime) |
---|
271 | localtime = time.strftime('%H:%M:%S', localtime) |
---|
272 | localtime = '%s.%s' % (localtime, decimal[:3]) |
---|
273 | |
---|
274 | else: |
---|
275 | date = time.strftime('%Y-%m-%d', localtime) |
---|
276 | localtime = time.strftime('%H:%M:%S', localtime) |
---|
277 | localtime = '%s.%s %s' % (localtime, decimal, \ |
---|
278 | time.strftime('%Z', time.localtime(timestamp))) |
---|
279 | |
---|
280 | |
---|
281 | return(date, localtime) |
---|
282 | |
---|
283 | |
---|
284 | ################################################################## |
---|
285 | |
---|
286 | def saveData(self, source=None, target=None, output_file=None, use_default=False): |
---|
287 | |
---|
288 | if source == None: |
---|
289 | if self.parent == None: |
---|
290 | source = self |
---|
291 | else: |
---|
292 | source = self.parent |
---|
293 | |
---|
294 | if target == None: |
---|
295 | if self.parent == None: |
---|
296 | target = self |
---|
297 | else: |
---|
298 | target = self.parent |
---|
299 | |
---|
300 | data = self.collectData(source=source, target=target) |
---|
301 | |
---|
302 | (date, localtime) = self.parseTimeStamp(time.time()) |
---|
303 | |
---|
304 | default_filename = '%s %s.synapse' % (date, \ |
---|
305 | target.lineEditSessionProfile.text()) |
---|
306 | |
---|
307 | default_filename = os.path.join(self.homepath, default_filename) |
---|
308 | |
---|
309 | |
---|
310 | if output_file == None: |
---|
311 | |
---|
312 | # use_default controls whether or not a file is automatically saves using the |
---|
313 | # default name and path (as opposed to raising a GUI file selection menu) |
---|
314 | # whenever an explicit filepath is not defined |
---|
315 | if use_default: |
---|
316 | |
---|
317 | output_file = default_filename |
---|
318 | |
---|
319 | else: |
---|
320 | |
---|
321 | output_file = QtGui.QFileDialog.getSaveFileName(parent=target, \ |
---|
322 | caption="Save Session Data to File", \ |
---|
323 | directory=default_filename, \ |
---|
324 | filter="Puzzlebox Synapse Data File (*.synapse)") |
---|
325 | |
---|
326 | # TODO 2014-02-09 Disabling due to failure with Puzzlebox Orbit |
---|
327 | # TODO 2014-02-23 Re-enabled due to failure to write files |
---|
328 | try: |
---|
329 | output_file = output_file[0] |
---|
330 | except: |
---|
331 | #output_file = '' |
---|
332 | # TODO 2014-02-23 Attempted compromise |
---|
333 | pass |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | if output_file == '': |
---|
338 | return |
---|
339 | |
---|
340 | |
---|
341 | file = open(str(output_file), 'w') |
---|
342 | pickle.dump(data, file) |
---|
343 | file.close() |
---|
344 | |
---|
345 | |
---|
346 | ################################################################## |
---|
347 | |
---|
348 | def exportData(self, parent=None, source=None, target=None, output_file=None, use_default=False): |
---|
349 | |
---|
350 | if parent == None: |
---|
351 | if self.parent == None: |
---|
352 | parent = self |
---|
353 | else: |
---|
354 | parent = self.parent |
---|
355 | |
---|
356 | if source == None: |
---|
357 | if self.parent == None: |
---|
358 | source = self |
---|
359 | else: |
---|
360 | source = self.parent |
---|
361 | |
---|
362 | if target == None: |
---|
363 | if self.parent == None: |
---|
364 | target = self |
---|
365 | else: |
---|
366 | target = self.parent |
---|
367 | |
---|
368 | |
---|
369 | try: |
---|
370 | export_csv_raw = target.configuration.EXPORT_CSV_RAW_DATA |
---|
371 | except: |
---|
372 | export_csv_raw = False |
---|
373 | |
---|
374 | |
---|
375 | (date, localtime) = self.parseTimeStamp(time.time()) |
---|
376 | |
---|
377 | default_filename = '%s %s.csv' % (date, \ |
---|
378 | target.lineEditSessionProfile.text()) |
---|
379 | |
---|
380 | default_filename = os.path.join(target.homepath, default_filename) |
---|
381 | |
---|
382 | |
---|
383 | if output_file == None: |
---|
384 | |
---|
385 | # use_default controls whether or not a file is automatically saves using the |
---|
386 | # default name and path (as opposed to raising a GUI file selection menu) |
---|
387 | # whenever an explicit filepath is not defined |
---|
388 | if use_default: |
---|
389 | |
---|
390 | output_file = default_filename |
---|
391 | |
---|
392 | else: |
---|
393 | output_file = QtGui.QFileDialog.getSaveFileName(parent=target, \ |
---|
394 | caption="Export Session Data to File", \ |
---|
395 | directory=default_filename, \ |
---|
396 | filter="CSV File (*.csv);;Text File (*.txt)") |
---|
397 | |
---|
398 | # TODO 2014-02-09 Disabling due to failure with Puzzlebox Orbit |
---|
399 | # TODO 2014-02-23 Re-enabled due to failure to write files |
---|
400 | try: |
---|
401 | output_file = output_file[0] |
---|
402 | except: |
---|
403 | #output_file = '' |
---|
404 | # TODO 2014-02-23 Attempted compromise |
---|
405 | pass |
---|
406 | |
---|
407 | |
---|
408 | if output_file == '': |
---|
409 | return |
---|
410 | |
---|
411 | |
---|
412 | if str(output_file).endswith('.csv'): |
---|
413 | |
---|
414 | outputData = self.exportDataToCSV(parent=parent, source=source, target=target) |
---|
415 | |
---|
416 | |
---|
417 | else: |
---|
418 | |
---|
419 | try: |
---|
420 | outputData = self.textEditDebugConsole.toPlainText() |
---|
421 | except: |
---|
422 | outputData = self.exportDataToCSV(parent=parent, source=source, target=target) |
---|
423 | |
---|
424 | |
---|
425 | if self.DEBUG: |
---|
426 | print "Writing file:", |
---|
427 | print output_file |
---|
428 | |
---|
429 | |
---|
430 | file = open(os.path.join(str(output_file)), 'w') |
---|
431 | file.write(outputData) |
---|
432 | file.close() |
---|
433 | |
---|
434 | |
---|
435 | if export_csv_raw: |
---|
436 | |
---|
437 | output_file = output_file.replace('.csv', '-rawEeg.csv') |
---|
438 | |
---|
439 | outputData = self.exportRawDataToCSV(parent=parent, source=source, target=target) |
---|
440 | |
---|
441 | if outputData != None: |
---|
442 | |
---|
443 | file = open(str(output_file), 'w') |
---|
444 | file.write(outputData) |
---|
445 | file.close() |
---|
446 | |
---|
447 | |
---|
448 | ################################################################## |
---|
449 | |
---|
450 | def exportDataToCSV(self, parent=None, source=None, target=None): |
---|
451 | |
---|
452 | # handle importing class from multiple sources |
---|
453 | if parent == None: |
---|
454 | if self.parent == None: |
---|
455 | parent = self |
---|
456 | else: |
---|
457 | parent = self.parent |
---|
458 | |
---|
459 | if source == None: |
---|
460 | if self.parent == None: |
---|
461 | source = self |
---|
462 | else: |
---|
463 | source = self.parent |
---|
464 | |
---|
465 | if target == None: |
---|
466 | if self.parent == None: |
---|
467 | target = self |
---|
468 | else: |
---|
469 | target = self.parent |
---|
470 | |
---|
471 | try: |
---|
472 | truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE |
---|
473 | except: |
---|
474 | truncate_csv_timezone = False |
---|
475 | |
---|
476 | |
---|
477 | #print source.name |
---|
478 | #print source.packets['signals'] |
---|
479 | |
---|
480 | |
---|
481 | # NOTE: no need to scrub emulated data |
---|
482 | try: |
---|
483 | scrub_data = target.configuration.EXPORT_CSV_SCRUB_DATA |
---|
484 | except: |
---|
485 | scrub_data = False |
---|
486 | |
---|
487 | try: |
---|
488 | if self.parent.plugin_eeg.emulate_headset_data: |
---|
489 | scrub_data = False |
---|
490 | except: |
---|
491 | pass |
---|
492 | |
---|
493 | |
---|
494 | headers = 'Date,Time' |
---|
495 | |
---|
496 | |
---|
497 | customDataHeaders = [] |
---|
498 | for header in parent.customDataHeaders: |
---|
499 | customDataHeaders.append(header) |
---|
500 | for plugin in parent.activePlugins: |
---|
501 | #print plugin.name |
---|
502 | for header in plugin.customDataHeaders: |
---|
503 | customDataHeaders.append(header) |
---|
504 | |
---|
505 | for each in customDataHeaders: |
---|
506 | headers = headers + ',%s' % each |
---|
507 | |
---|
508 | headers = headers + '\n' |
---|
509 | |
---|
510 | |
---|
511 | csv = {} |
---|
512 | |
---|
513 | |
---|
514 | for packet in source.packets['signals']: |
---|
515 | |
---|
516 | |
---|
517 | # NOTE: Move this to ThinkGear Server object |
---|
518 | #if 'rawEeg' in packet.keys(): |
---|
519 | #continue |
---|
520 | |
---|
521 | |
---|
522 | if 'timestamp' not in packet.keys() and len(packet.keys()) == 1: |
---|
523 | if self.DEBUG: |
---|
524 | print "WARN: Skipping empty packet:", |
---|
525 | print packet |
---|
526 | # skip empty packets |
---|
527 | continue |
---|
528 | |
---|
529 | |
---|
530 | print "packet:", |
---|
531 | print packet |
---|
532 | |
---|
533 | timestamp = packet['timestamp'] |
---|
534 | #(date, localtime) = self.parseTimeStamp(timestamp, \ |
---|
535 | #truncate_time_zone=truncate_csv_timezone) |
---|
536 | (date, localtime) = source.parseTimeStamp(timestamp, \ |
---|
537 | truncate_time_zone=truncate_csv_timezone) |
---|
538 | |
---|
539 | if timestamp not in csv.keys(): |
---|
540 | |
---|
541 | #if 'blinkStrength' in packet.keys(): |
---|
542 | ## Skip any blink packets from log |
---|
543 | #continue |
---|
544 | |
---|
545 | |
---|
546 | #timestamp = packet['timestamp'] |
---|
547 | ##(date, localtime) = self.parseTimeStamp(timestamp, \ |
---|
548 | ##truncate_time_zone=truncate_csv_timezone) |
---|
549 | #(date, localtime) = source.parseTimeStamp(timestamp, \ |
---|
550 | #truncate_time_zone=truncate_csv_timezone) |
---|
551 | |
---|
552 | csv[timestamp] = {} |
---|
553 | csv[timestamp]['Date'] = date |
---|
554 | csv[timestamp]['Time'] = localtime |
---|
555 | |
---|
556 | |
---|
557 | for plugin in parent.activePlugins: |
---|
558 | if plugin.customDataHeaders != []: |
---|
559 | if self.DEBUG > 2: |
---|
560 | print "INFO: [Synapse:Session] Exporting:", |
---|
561 | print plugin.name |
---|
562 | try: |
---|
563 | csv[timestamp] = plugin.processPacketForExport(packet=packet, output=csv[timestamp]) |
---|
564 | if self.DEBUG > 2: |
---|
565 | print "INFO [Synapse:Session]: Export Successful" |
---|
566 | print plugin.name |
---|
567 | except Exception, e: |
---|
568 | if self.DEBUG: |
---|
569 | print "ERROR: [Synapse:Session] Exception calling processPacketForExport on", |
---|
570 | print plugin.name |
---|
571 | |
---|
572 | |
---|
573 | for header in customDataHeaders: |
---|
574 | |
---|
575 | if 'custom' in packet.keys() and \ |
---|
576 | header in packet['custom'].keys(): |
---|
577 | |
---|
578 | timestamp = packet['timestamp'] |
---|
579 | (date, localtime) = source.parseTimeStamp(timestamp, \ |
---|
580 | truncate_time_zone=truncate_csv_timezone) |
---|
581 | |
---|
582 | if timestamp not in csv.keys(): |
---|
583 | csv[timestamp] = {} |
---|
584 | csv[timestamp]['Date'] = date |
---|
585 | csv[timestamp]['Time'] = localtime |
---|
586 | if self.DEBUG: |
---|
587 | print "WARN: Unmatched custom packet:", |
---|
588 | print packet |
---|
589 | |
---|
590 | csv[timestamp][header] = packet['custom'][header] |
---|
591 | |
---|
592 | |
---|
593 | if scrub_data: |
---|
594 | csv = self.scrubData(csv, truncate_csv_timezone, source=source) |
---|
595 | |
---|
596 | |
---|
597 | output = headers |
---|
598 | |
---|
599 | timestamps = csv.keys() |
---|
600 | timestamps.sort() |
---|
601 | |
---|
602 | for timestamp in timestamps: |
---|
603 | |
---|
604 | row = '%s,%s' % \ |
---|
605 | (csv[timestamp]['Date'], \ |
---|
606 | csv[timestamp]['Time']) |
---|
607 | |
---|
608 | for header in customDataHeaders: |
---|
609 | if header in csv[timestamp].keys(): |
---|
610 | row = row + ',%s' % csv[timestamp][header] |
---|
611 | else: |
---|
612 | #row = row + ',' |
---|
613 | row = '' |
---|
614 | if self.DEBUG > 1: |
---|
615 | print "WARN: empty signals packet:", |
---|
616 | print csv[timestamp] |
---|
617 | break |
---|
618 | |
---|
619 | if row != '': |
---|
620 | row = row + '\n' |
---|
621 | |
---|
622 | output = output + row |
---|
623 | |
---|
624 | |
---|
625 | return(output) |
---|
626 | |
---|
627 | |
---|
628 | ################################################################## |
---|
629 | |
---|
630 | def exportRawDataToCSV(self, parent=None, source=None, target=None): |
---|
631 | |
---|
632 | # handle importing class from multiple sources |
---|
633 | if parent == None: |
---|
634 | if self.parent == None: |
---|
635 | parent = self |
---|
636 | else: |
---|
637 | parent = self.parent |
---|
638 | |
---|
639 | if source == None: |
---|
640 | if self.parent == None: |
---|
641 | source = self |
---|
642 | else: |
---|
643 | source = self.parent |
---|
644 | |
---|
645 | if target == None: |
---|
646 | if self.parent == None: |
---|
647 | target = self |
---|
648 | else: |
---|
649 | target = self.parent |
---|
650 | |
---|
651 | |
---|
652 | try: |
---|
653 | truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE |
---|
654 | except: |
---|
655 | truncate_csv_timezone = False |
---|
656 | |
---|
657 | |
---|
658 | if source.packets['rawEeg'] == []: |
---|
659 | return(None) |
---|
660 | |
---|
661 | |
---|
662 | headers = 'Date,Time,Raw EEG' |
---|
663 | headers = headers + '\n' |
---|
664 | |
---|
665 | csv = {} |
---|
666 | |
---|
667 | for packet in source.packets['rawEeg']: |
---|
668 | |
---|
669 | # NOTE: Move this to ThinkGear Server object |
---|
670 | if 'rawEeg' in packet.keys(): |
---|
671 | |
---|
672 | if packet['timestamp'] not in csv.keys(): |
---|
673 | |
---|
674 | timestamp = packet['timestamp'] |
---|
675 | |
---|
676 | (date, localtime) = source.parseTimeStamp(timestamp, \ |
---|
677 | truncate_time_zone=truncate_csv_timezone) |
---|
678 | |
---|
679 | csv[timestamp] = {} |
---|
680 | csv[timestamp]['Date'] = date |
---|
681 | csv[timestamp]['Time'] = localtime |
---|
682 | csv[timestamp]['rawEeg'] = packet['rawEeg'] |
---|
683 | |
---|
684 | |
---|
685 | output = headers |
---|
686 | |
---|
687 | timestamps = csv.keys() |
---|
688 | |
---|
689 | # Don't sort timestamps in order to better preserve the original raw signal |
---|
690 | #timestamps.sort() |
---|
691 | |
---|
692 | for timestamp in timestamps: |
---|
693 | |
---|
694 | row = '%s,%s,%s' % \ |
---|
695 | (csv[timestamp]['Date'], \ |
---|
696 | csv[timestamp]['Time'], \ |
---|
697 | csv[timestamp]['rawEeg']) |
---|
698 | |
---|
699 | row = row + '\n' |
---|
700 | |
---|
701 | output = output + row |
---|
702 | |
---|
703 | |
---|
704 | return(output) |
---|
705 | |
---|
706 | |
---|
707 | ################################################################# |
---|
708 | |
---|
709 | def scrubData(self, csv, truncate_csv_timezone=False, source=None): |
---|
710 | |
---|
711 | # If there are missing packets, repeat a given packet once per missing |
---|
712 | # second until there is a gap between 1 and 2 seconds, in which case |
---|
713 | # produce a final duplicate packet at the mid-point between the packets |
---|
714 | |
---|
715 | if self.DEBUG: |
---|
716 | print "INFO: Scrubbing Data" |
---|
717 | |
---|
718 | if source == None: |
---|
719 | if self.parent == None: |
---|
720 | source = self |
---|
721 | else: |
---|
722 | source = self.parent |
---|
723 | |
---|
724 | last_time = None |
---|
725 | last_recorded_time = None |
---|
726 | |
---|
727 | output = {} |
---|
728 | |
---|
729 | csv_keys = csv.keys() |
---|
730 | csv_keys.sort() |
---|
731 | |
---|
732 | for key in csv_keys: |
---|
733 | |
---|
734 | timestamp = key |
---|
735 | |
---|
736 | if last_time == None: |
---|
737 | # First entry in log |
---|
738 | last_time = timestamp |
---|
739 | last_recorded_time = timestamp |
---|
740 | #output[key] = csv[key] |
---|
741 | if key not in output.keys(): |
---|
742 | output[key] = DEFAULT_PACKET.copy() |
---|
743 | output[key].update(csv[key]) |
---|
744 | continue |
---|
745 | |
---|
746 | else: |
---|
747 | |
---|
748 | #time_difference = timestamp - last_time |
---|
749 | #time_difference = timestamp - last_recorded_time |
---|
750 | time_difference = abs(timestamp - last_recorded_time) |
---|
751 | |
---|
752 | if (time_difference <= 1) and \ |
---|
753 | (time_difference >= PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD): |
---|
754 | # Skip packets within the correct time threshold |
---|
755 | last_time = timestamp |
---|
756 | last_recorded_time = timestamp |
---|
757 | #output[key] = csv[key] |
---|
758 | if key not in output.keys(): |
---|
759 | output[key] = DEFAULT_PACKET.copy() |
---|
760 | output[key].update(csv[key]) |
---|
761 | |
---|
762 | #print "<=1 and >=min" |
---|
763 | continue |
---|
764 | |
---|
765 | else: |
---|
766 | |
---|
767 | if self.DEBUG > 1: |
---|
768 | print "time_difference:", |
---|
769 | print time_difference |
---|
770 | print "timestamp:", |
---|
771 | print source.parseTimeStamp(timestamp)[-1].split(' ')[0] |
---|
772 | print "last_time:", |
---|
773 | print source.parseTimeStamp(last_time)[-1].split(' ')[0] |
---|
774 | print "last_recorded_time:", |
---|
775 | print source.parseTimeStamp(last_recorded_time)[-1].split(' ')[0] |
---|
776 | |
---|
777 | |
---|
778 | #new_packet = csv[key].copy() |
---|
779 | if key not in output.keys(): |
---|
780 | new_packet = DEFAULT_PACKET.copy() |
---|
781 | new_packet.update(csv[key]) |
---|
782 | |
---|
783 | if time_difference >= 2: |
---|
784 | |
---|
785 | ##new_time = last_time + 1 |
---|
786 | #new_time = last_recorded_time + 1 |
---|
787 | |
---|
788 | count = int(time_difference) |
---|
789 | while count >= 1: |
---|
790 | #new_packet = csv[key].copy() |
---|
791 | if key not in output.keys(): |
---|
792 | new_packet = DEFAULT_PACKET.copy() |
---|
793 | new_packet.update(csv[key]) |
---|
794 | |
---|
795 | new_time = last_recorded_time + 1 |
---|
796 | (date, formatted_new_time) = source.parseTimeStamp(new_time, \ |
---|
797 | truncate_time_zone=truncate_csv_timezone) |
---|
798 | new_packet['Time'] = formatted_new_time |
---|
799 | last_recorded_time = new_time |
---|
800 | last_time = timestamp |
---|
801 | if key not in output.keys(): |
---|
802 | output[new_time] = new_packet |
---|
803 | else: |
---|
804 | output[new_time].update(new_packet) |
---|
805 | count = count - 1 |
---|
806 | continue |
---|
807 | |
---|
808 | #print ">=2" |
---|
809 | |
---|
810 | |
---|
811 | elif time_difference < PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD: |
---|
812 | # Spread out "bunched up" packets |
---|
813 | #new_time = last_time + 1 |
---|
814 | new_time = last_recorded_time + 1 |
---|
815 | #new_time = last_recorded_time |
---|
816 | #print "<min" |
---|
817 | |
---|
818 | |
---|
819 | elif (time_difference < 2) and (time_difference > 1): |
---|
820 | |
---|
821 | #new_time = last_time + ((last_time - timestamp) / 2) |
---|
822 | #new_time = last_recorded_time + ((last_recorded_time - timestamp) / 2) |
---|
823 | #new_time = last_time + 1 |
---|
824 | #new_time = last_recorded_time + 1 |
---|
825 | new_time = last_recorded_time |
---|
826 | #print "<2" |
---|
827 | |
---|
828 | |
---|
829 | (date, formatted_new_time) = source.parseTimeStamp(new_time, \ |
---|
830 | truncate_time_zone=truncate_csv_timezone) |
---|
831 | |
---|
832 | new_packet['Time'] = formatted_new_time |
---|
833 | |
---|
834 | #last_time = new_time |
---|
835 | last_recorded_time = new_time |
---|
836 | #last_time = timestamp |
---|
837 | last_time = new_time |
---|
838 | try: |
---|
839 | output[new_time].update(new_packet) |
---|
840 | except Exception, e: |
---|
841 | output[new_time] = new_packet |
---|
842 | #print e |
---|
843 | |
---|
844 | if self.DEBUG > 1: |
---|
845 | print "WARN: Scrubbing new packet:", |
---|
846 | print new_packet |
---|
847 | print |
---|
848 | |
---|
849 | |
---|
850 | return(output) |
---|
851 | |
---|
852 | |
---|
853 | ################################################################## |
---|
854 | |
---|
855 | def resetData(self, source=None): |
---|
856 | |
---|
857 | if source == None: |
---|
858 | if self.parent == None: |
---|
859 | source = self |
---|
860 | else: |
---|
861 | source = self.parent |
---|
862 | |
---|
863 | source.packets['rawEeg'] = [] |
---|
864 | source.packets['signals'] = [] |
---|
865 | |
---|
866 | if self.synapseServer != None: |
---|
867 | self.synapseServer.protocol.resetSessionStartTime() |
---|
868 | else: |
---|
869 | self.resetSessionStartTime() |
---|
870 | |
---|
871 | if self.synapseServer != None: |
---|
872 | source.synapseServer.protocol.packet_count = 0 |
---|
873 | source.synapseServer.protocol.bad_packets = 0 |
---|
874 | else: |
---|
875 | source.packet_count = 0 |
---|
876 | source.bad_packets = 0 |
---|
877 | |
---|
878 | self.updateProfileSessionStatus() |
---|
879 | |
---|
880 | try: |
---|
881 | source.textEditDebugConsole.setText("") |
---|
882 | except: |
---|
883 | pass |
---|
884 | |
---|
885 | |
---|
886 | ##################################################################### |
---|
887 | |
---|
888 | def resetSessionStartTime(self, source=None): |
---|
889 | |
---|
890 | self.session_start_timestamp = time.time() |
---|
891 | |
---|
892 | |
---|
893 | ##################################################################### |
---|
894 | |
---|
895 | def convert_seconds_to_datetime(self, duration): |
---|
896 | |
---|
897 | duration_hours = duration / (60 * 60) |
---|
898 | duration_minutes = (duration - (duration_hours * (60 * 60))) / 60 |
---|
899 | duration_seconds = (duration - (duration_hours * (60 * 60)) - (duration_minutes * 60)) |
---|
900 | |
---|
901 | duration_hours = '%i' % duration_hours |
---|
902 | if (len(duration_hours) == 1): |
---|
903 | duration_hours = "0%s" % duration_hours |
---|
904 | |
---|
905 | duration_minutes = '%i' % duration_minutes |
---|
906 | if (len(duration_minutes) == 1): |
---|
907 | duration_minutes = "0%s" % duration_minutes |
---|
908 | |
---|
909 | duration_seconds = '%i' % duration_seconds |
---|
910 | if (len(duration_seconds) == 1): |
---|
911 | duration_seconds = "0%s" % duration_seconds |
---|
912 | |
---|
913 | datetime = '%s:%s:%s' % (duration_hours, duration_minutes, duration_seconds) |
---|
914 | |
---|
915 | return(datetime) |
---|
916 | |
---|