Changeset 386
- Timestamp:
- 04/09/12 15:16:20 (9 years ago)
- Location:
- trunk/Puzzlebox/Synapse
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Puzzlebox/Synapse/Emotiv/Server.py
r384 r386 7 7 8 8 __changelog__ = """\ 9 Last Update: 2012.04.0 59 Last Update: 2012.04.09 10 10 """ 11 11 … … 87 87 # either true or false. 88 88 89 DEFAULT_SIGNAL_LEVEL_MESSAGE = \90 {"poorSignalLevel": 0}91 # A quantifier of the quality of the brainwave signal.92 # This is an integer value that is generally in the93 # range of 0 to 200, with 0 indicating a94 # good signal and 200 indicating an off-head state.95 96 DEFAULT_EEG_POWER_MESSAGE = \97 {"eegPower": { \98 'delta': 0, \99 'theta': 0, \100 'lowAlpha': 0, \101 'highAlpha': 0, \102 'lowBeta': 0, \103 'highBeta': 0, \104 'lowGamma': 0, \105 'highGamma': 0, \106 }, \107 } # A container for the EEG powers. These may108 # be either integer or floating-point values.109 # Maximum values are undocumented but assumed to be 65535110 111 DEFAULT_ESENSE_MESSAGE = \112 {"eSense": { \113 'attention': 0, \114 'meditation': 0, \115 }, \116 } # A container for the eSense⢠attributes.117 # These are integer values between 0 and 100,118 # where 0 is perceived as a lack of that attribute119 # and 100 is an excess of that attribute.89 #DEFAULT_SIGNAL_LEVEL_MESSAGE = \ 90 #{"poorSignalLevel": 0} 91 ## A quantifier of the quality of the brainwave signal. 92 ## This is an integer value that is generally in the 93 ## range of 0 to 200, with 0 indicating a 94 ## good signal and 200 indicating an off-head state. 95 96 #DEFAULT_EEG_POWER_MESSAGE = \ 97 #{"eegPower": { \ 98 #'delta': 0, \ 99 #'theta': 0, \ 100 #'lowAlpha': 0, \ 101 #'highAlpha': 0, \ 102 #'lowBeta': 0, \ 103 #'highBeta': 0, \ 104 #'lowGamma': 0, \ 105 #'highGamma': 0, \ 106 #}, \ 107 #} # A container for the EEG powers. These may 108 ## be either integer or floating-point values. 109 ## Maximum values are undocumented but assumed to be 65535 110 111 #DEFAULT_ESENSE_MESSAGE = \ 112 #{"eSense": { \ 113 #'attention': 0, \ 114 #'meditation': 0, \ 115 #}, \ 116 #} # A container for the eSense⢠attributes. 117 ## These are integer values between 0 and 100, 118 ## where 0 is perceived as a lack of that attribute 119 ## and 100 is an excess of that attribute. 120 120 121 121 DEFAULT_BLINK_MESSAGE = {"blinkStrength": 255} … … 123 123 # an integer in the range of 0-255. 124 124 125 DEFAULT_RAWEEG_MESSAGE = {"rawEeg": 255}126 # The raw data reading off the forehead sensor.127 # This may be either an integer or a floating-point value.125 #DEFAULT_RAWEEG_MESSAGE = {"rawEeg": 255} 126 ## The raw data reading off the forehead sensor. 127 ## This may be either an integer or a floating-point value. 128 128 129 129 DEFAULT_PACKET = {} 130 DEFAULT_PACKET.update(DEFAULT_EEG_POWER_MESSAGE)131 DEFAULT_PACKET.update(DEFAULT_SIGNAL_LEVEL_MESSAGE)132 DEFAULT_PACKET.update(DEFAULT_ESENSE_MESSAGE)130 #DEFAULT_PACKET.update(DEFAULT_EEG_POWER_MESSAGE) 131 #DEFAULT_PACKET.update(DEFAULT_SIGNAL_LEVEL_MESSAGE) 132 #DEFAULT_PACKET.update(DEFAULT_ESENSE_MESSAGE) 133 133 134 134 #DEFAULT_RESPONSE_MESSAGE = DEFAULT_SIGNAL_LEVEL_MESSAGE … … 182 182 #self.blink_frequency_timer = BLINK_FREQUENCY_TIMER 183 183 #self.blink_timestamp = time.time() 184 185 186 self.customDataHeaders = 'Time From Start,Headset On,Contact Number Of Quality Channels,Wireless,Excitement,Long Term Excitement,Meditation,Frustration,Engagement/Boredom,Current Action,Current Action Power' 187 184 188 185 189 self.configureEEG() … … 309 313 ################################################################## 310 314 315 def exportDataToCSV(self, parent=None, source=None, target=None): 316 317 if parent == None: 318 if self.parent == None: 319 parent = self 320 else: 321 parent = self.parent 322 323 if source == None: 324 if self.parent == None: 325 source = self 326 else: 327 source = self.parent 328 329 if target == None: 330 if self.parent == None: 331 target = self 332 else: 333 target = self.parent 334 335 try: 336 truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE 337 except: 338 truncate_csv_timezone = False 339 340 #try: 341 #scrub_data = target.configuration.EXPORT_CSV_SCRUB_DATA 342 #except: 343 #scrub_data = False 344 345 346 #headers = 'Date,Time' 347 #headers = 'Date,Time,Attention,Meditation,Signal Level,Delta,Theta,Low Alpha,High Alpha,Low Beta,High Beta,Low Gamma,Mid Gamma' 348 headers = self.customDataHeaders 349 350 customDataHeaders = [] 351 for header in parent.customDataHeaders: 352 customDataHeaders.append(header) 353 for plugin in parent.activePlugins: 354 for header in plugin.customDataHeaders: 355 customDataHeaders.append(header) 356 357 for each in customDataHeaders: 358 headers = headers + ',%s' % each 359 360 headers = headers + '\n' 361 362 csv = {} 363 364 for packet in source.packets['signals']: 365 366 367 if 'rawEeg' in packet.keys(): 368 continue 369 370 if packet['timestamp'] not in csv.keys(): 371 372 if 'blinkStrength' in packet.keys(): 373 # Skip any blink packets from log 374 continue 375 376 377 #print packet 378 timestamp = packet['timestamp'] 379 (date, localtime) = self.parseTimeStamp(timestamp, \ 380 truncate_time_zone=truncate_csv_timezone) 381 382 csv[timestamp] = {} 383 csv[timestamp]['Date'] = date 384 csv[timestamp]['Time'] = localtime 385 csv[timestamp]['Attention'] = '' 386 csv[timestamp]['Meditation'] = '' 387 csv[timestamp]['Signal Level'] = '' 388 csv[timestamp]['Delta'] = '' 389 csv[timestamp]['Theta'] = '' 390 csv[timestamp]['Low Alpha'] = '' 391 csv[timestamp]['High Alpha'] = '' 392 csv[timestamp]['Low Beta'] = '' 393 csv[timestamp]['High Beta'] = '' 394 csv[timestamp]['Low Gamma'] = '' 395 csv[timestamp]['Mid Gamma'] = '' 396 397 for header in customDataHeaders: 398 csv[timestamp][header] = '' 399 400 401 if 'eSense' in packet.keys(): 402 if 'attention' in packet['eSense'].keys(): 403 csv[timestamp]['Attention'] = packet['eSense']['attention'] 404 if 'meditation' in packet['eSense'].keys(): 405 csv[timestamp]['Meditation'] = packet['eSense']['meditation'] 406 407 if 'eegPower' in packet.keys(): 408 if 'delta' in packet['eegPower'].keys(): 409 csv[timestamp]['Delta'] = packet['eegPower']['delta'] 410 if 'theta' in packet['eegPower'].keys(): 411 csv[timestamp]['Theta'] = packet['eegPower']['theta'] 412 if 'lowAlpha' in packet['eegPower'].keys(): 413 csv[timestamp]['Low Alpha'] = packet['eegPower']['lowAlpha'] 414 if 'highAlpha' in packet['eegPower'].keys(): 415 csv[timestamp]['High Alpha'] = packet['eegPower']['highAlpha'] 416 if 'lowBeta' in packet['eegPower'].keys(): 417 csv[timestamp]['Low Beta'] = packet['eegPower']['lowBeta'] 418 if 'highBeta' in packet['eegPower'].keys(): 419 csv[timestamp]['High Beta'] = packet['eegPower']['highBeta'] 420 if 'lowGamma' in packet['eegPower'].keys(): 421 csv[timestamp]['Low Gamma'] = packet['eegPower']['lowGamma'] 422 if 'highGamma' in packet['eegPower'].keys(): 423 csv[timestamp]['Mid Gamma'] = packet['eegPower']['highGamma'] 424 425 if 'poorSignalLevel' in packet.keys(): 426 csv[timestamp]['Signal Level'] = packet['poorSignalLevel'] 427 428 for header in customDataHeaders: 429 if 'custom' in packet.keys() and \ 430 header in packet['custom'].keys(): 431 csv[timestamp][header] = packet['custom'][header] 432 433 434 #if scrub_data: 435 #csv = self.scrubData(csv, truncate_csv_timezone) 436 437 438 output = headers 439 440 csv_keys = csv.keys() 441 csv_keys.sort() 442 443 for key in csv_keys: 444 445 row = '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % \ 446 (csv[key]['Date'], \ 447 csv[key]['Time'], \ 448 csv[key]['Attention'], \ 449 csv[key]['Meditation'], \ 450 csv[key]['Signal Level'], \ 451 csv[key]['Delta'], \ 452 csv[key]['Theta'], \ 453 csv[key]['Low Alpha'], \ 454 csv[key]['High Alpha'], \ 455 csv[key]['Low Beta'], \ 456 csv[key]['High Beta'], \ 457 csv[key]['Low Gamma'], \ 458 csv[key]['Mid Gamma']) 459 460 for header in customDataHeaders: 461 row = row + ',%s' % csv[key][header] 462 463 row = row + '\n' 464 465 output = output + row 466 467 468 return(output) 469 470 471 ################################################################## 472 311 473 def exitThread(self, callThreadQuit=True): 312 474 -
trunk/Puzzlebox/Synapse/Session.py
r382 r386 7 7 8 8 __changelog__ = """\ 9 Last Update: 2012.04.0 19 Last Update: 2012.04.09 10 10 """ 11 11 … … 46 46 DEBUG = configuration.DEBUG 47 47 48 PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD = 0.7548 #PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD = 0.75 49 49 50 50 … … 364 364 def exportDataToCSV(self, parent=None, source=None, target=None): 365 365 366 if parent == None: 367 if self.parent == None: 368 parent = self 369 else: 370 parent = self.parent 371 372 if source == None: 373 if self.parent == None: 374 source = self 375 else: 376 source = self.parent 377 378 if target == None: 379 if self.parent == None: 380 target = self 381 else: 382 target = self.parent 383 384 try: 385 truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE 386 except: 387 truncate_csv_timezone = False 388 389 try: 390 scrub_data = target.configuration.EXPORT_CSV_SCRUB_DATA 391 except: 392 scrub_data = False 393 394 395 headers = 'Date,Time,Attention,Meditation,Signal Level,Delta,Theta,Low Alpha,High Alpha,Low Beta,High Beta,Low Gamma,Mid Gamma' 396 397 customDataHeaders = [] 398 for header in parent.customDataHeaders: 399 customDataHeaders.append(header) 400 for plugin in parent.activePlugins: 401 for header in plugin.customDataHeaders: 402 customDataHeaders.append(header) 403 404 for each in customDataHeaders: 405 headers = headers + ',%s' % each 406 407 headers = headers + '\n' 408 409 csv = {} 410 411 for packet in source.packets['signals']: 412 413 414 if 'rawEeg' in packet.keys(): 415 continue 416 417 if packet['timestamp'] not in csv.keys(): 418 419 if 'blinkStrength' in packet.keys(): 420 # Skip any blink packets from log 421 continue 422 423 424 #print packet 425 timestamp = packet['timestamp'] 426 (date, localtime) = self.parseTimeStamp(timestamp, \ 427 truncate_time_zone=truncate_csv_timezone) 428 429 csv[timestamp] = {} 430 csv[timestamp]['Date'] = date 431 csv[timestamp]['Time'] = localtime 432 csv[timestamp]['Attention'] = '' 433 csv[timestamp]['Meditation'] = '' 434 csv[timestamp]['Signal Level'] = '' 435 csv[timestamp]['Delta'] = '' 436 csv[timestamp]['Theta'] = '' 437 csv[timestamp]['Low Alpha'] = '' 438 csv[timestamp]['High Alpha'] = '' 439 csv[timestamp]['Low Beta'] = '' 440 csv[timestamp]['High Beta'] = '' 441 csv[timestamp]['Low Gamma'] = '' 442 csv[timestamp]['Mid Gamma'] = '' 443 444 for header in customDataHeaders: 445 csv[timestamp][header] = '' 446 447 448 if 'eSense' in packet.keys(): 449 if 'attention' in packet['eSense'].keys(): 450 csv[timestamp]['Attention'] = packet['eSense']['attention'] 451 if 'meditation' in packet['eSense'].keys(): 452 csv[timestamp]['Meditation'] = packet['eSense']['meditation'] 453 454 if 'eegPower' in packet.keys(): 455 if 'delta' in packet['eegPower'].keys(): 456 csv[timestamp]['Delta'] = packet['eegPower']['delta'] 457 if 'theta' in packet['eegPower'].keys(): 458 csv[timestamp]['Theta'] = packet['eegPower']['theta'] 459 if 'lowAlpha' in packet['eegPower'].keys(): 460 csv[timestamp]['Low Alpha'] = packet['eegPower']['lowAlpha'] 461 if 'highAlpha' in packet['eegPower'].keys(): 462 csv[timestamp]['High Alpha'] = packet['eegPower']['highAlpha'] 463 if 'lowBeta' in packet['eegPower'].keys(): 464 csv[timestamp]['Low Beta'] = packet['eegPower']['lowBeta'] 465 if 'highBeta' in packet['eegPower'].keys(): 466 csv[timestamp]['High Beta'] = packet['eegPower']['highBeta'] 467 if 'lowGamma' in packet['eegPower'].keys(): 468 csv[timestamp]['Low Gamma'] = packet['eegPower']['lowGamma'] 469 if 'highGamma' in packet['eegPower'].keys(): 470 csv[timestamp]['Mid Gamma'] = packet['eegPower']['highGamma'] 471 472 if 'poorSignalLevel' in packet.keys(): 473 csv[timestamp]['Signal Level'] = packet['poorSignalLevel'] 474 475 for header in customDataHeaders: 476 if 'custom' in packet.keys() and \ 477 header in packet['custom'].keys(): 478 csv[timestamp][header] = packet['custom'][header] 479 480 481 if scrub_data: 482 csv = self.scrubData(csv, truncate_csv_timezone) 483 484 485 output = headers 486 487 csv_keys = csv.keys() 488 csv_keys.sort() 489 490 for key in csv_keys: 491 492 row = '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % \ 493 (csv[key]['Date'], \ 494 csv[key]['Time'], \ 495 csv[key]['Attention'], \ 496 csv[key]['Meditation'], \ 497 csv[key]['Signal Level'], \ 498 csv[key]['Delta'], \ 499 csv[key]['Theta'], \ 500 csv[key]['Low Alpha'], \ 501 csv[key]['High Alpha'], \ 502 csv[key]['Low Beta'], \ 503 csv[key]['High Beta'], \ 504 csv[key]['Low Gamma'], \ 505 csv[key]['Mid Gamma']) 506 507 for header in customDataHeaders: 508 row = row + ',%s' % csv[key][header] 509 510 row = row + '\n' 511 512 output = output + row 513 514 515 return(output) 516 517 518 ################################################################## 519 520 def scrubData(self, csv, truncate_csv_timezone=False): 521 522 # If there are missing packets, repeat a given packet once per missing 523 # second until there is a gap between 1 and 2 seconds, in which case 524 # produce a final duplicate packet at the mid-point between the packets 525 526 if self.DEBUG: 527 print "INFO: Scrubbing Data" 528 529 last_time = None 530 last_recorded_time = None 531 532 output = {} 533 534 csv_keys = csv.keys() 535 csv_keys.sort() 536 537 for key in csv_keys: 538 539 timestamp = key 540 541 if csv[key]['Attention'] == '': 542 continue 543 544 if last_time == None: 545 # First entry in log 546 last_time = timestamp 547 last_recorded_time = timestamp 548 output[key] = csv[key] 549 continue 550 551 else: 552 553 #time_difference = timestamp - last_time 554 time_difference = timestamp - last_recorded_time 555 556 if (time_difference <= 1) and \ 557 (time_difference >= PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD): 558 # Skip packets within the correct time threshold 559 last_time = timestamp 560 last_recorded_time = timestamp 561 output[key] = csv[key] 562 continue 563 564 else: 565 566 if self.DEBUG > 1: 567 print "time_difference:", 568 print time_difference 569 print "timestamp:", 570 print self.parseTimeStamp(timestamp)[-1].split(' ')[0] 571 print "last_time:", 572 print self.parseTimeStamp(last_time)[-1].split(' ')[0] 573 print "last_recorded_time:", 574 print self.parseTimeStamp(last_recorded_time)[-1].split(' ')[0] 575 576 577 new_packet = csv[key].copy() 578 579 if time_difference >= 2: 580 581 ##new_time = last_time + 1 582 #new_time = last_recorded_time + 1 583 584 count = int(time_difference) 585 while count >= 1: 586 new_packet = csv[key].copy() 587 new_time = last_recorded_time + 1 588 (date, formatted_new_time) = self.parseTimeStamp(new_time, \ 589 truncate_time_zone=truncate_csv_timezone) 590 new_packet['Time'] = formatted_new_time 591 last_recorded_time = new_time 592 last_time = timestamp 593 output[new_time] = new_packet 594 count = count - 1 595 continue 596 597 elif time_difference < PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD: 598 # Spread out "bunched up" packets 599 #new_time = last_time + 1 600 new_time = last_recorded_time + 1 601 602 603 elif (time_difference < 2) and (time_difference > 1): 604 605 #new_time = last_time + ((last_time - timestamp) / 2) 606 #new_time = last_recorded_time + ((last_recorded_time - timestamp) / 2) 607 #new_time = last_time + 1 608 new_time = last_recorded_time + 1 609 610 611 (date, formatted_new_time) = self.parseTimeStamp(new_time, \ 612 truncate_time_zone=truncate_csv_timezone) 613 614 new_packet['Time'] = formatted_new_time 615 616 #last_time = new_time 617 last_recorded_time = new_time 618 last_time = timestamp 619 output[new_time] = new_packet 620 621 if self.DEBUG > 1: 622 print "WARN: Scrubbing new packet:", 623 print new_packet 624 print 366 #if parent == None: 367 #if self.parent == None: 368 #parent = self 369 #else: 370 #parent = self.parent 371 372 #if source == None: 373 #if self.parent == None: 374 #source = self 375 #else: 376 #source = self.parent 377 378 #if target == None: 379 #if self.parent == None: 380 #target = self 381 #else: 382 #target = self.parent 383 384 #try: 385 #truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE 386 #except: 387 #truncate_csv_timezone = False 388 389 #try: 390 #scrub_data = target.configuration.EXPORT_CSV_SCRUB_DATA 391 #except: 392 #scrub_data = False 393 394 output = '' 395 396 if self.synapseServer != None: 397 #output = self.synapseServer.exportDataToCSV(parent=self.parent, source=self) 398 output = self.synapseServer.exportDataToCSV(parent=parent, source=source, target=target) 625 399 626 400 -
trunk/Puzzlebox/Synapse/ThinkGear/Server.py
r384 r386 130 130 DEFAULT_RESPONSE_MESSAGE = DEFAULT_SIGNAL_LEVEL_MESSAGE 131 131 132 PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD = 0.75 133 132 134 ##################################################################### 133 135 # Classes … … 177 179 178 180 181 self.customDataHeaders = 'Attention,Meditation,Signal Level,Delta,Theta,Low Alpha,High Alpha,Low Beta,High Beta,Low Gamma,Mid Gamma' 182 183 179 184 self.configureEEG() 180 185 … … 484 489 ################################################################## 485 490 491 def exportDataToCSV(self, parent=None, source=None, target=None): 492 493 if parent == None: 494 if self.parent == None: 495 parent = self 496 else: 497 parent = self.parent 498 499 if source == None: 500 if self.parent == None: 501 source = self 502 else: 503 source = self.parent 504 505 if target == None: 506 if self.parent == None: 507 target = self 508 else: 509 target = self.parent 510 511 try: 512 truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE 513 except: 514 truncate_csv_timezone = False 515 516 try: 517 scrub_data = target.configuration.EXPORT_CSV_SCRUB_DATA 518 except: 519 scrub_data = False 520 521 522 #headers = 'Date,Time' 523 #headers = 'Date,Time,Attention,Meditation,Signal Level,Delta,Theta,Low Alpha,High Alpha,Low Beta,High Beta,Low Gamma,Mid Gamma' 524 headers = self.customDataHeaders 525 526 customDataHeaders = [] 527 for header in parent.customDataHeaders: 528 customDataHeaders.append(header) 529 for plugin in parent.activePlugins: 530 for header in plugin.customDataHeaders: 531 customDataHeaders.append(header) 532 533 for each in customDataHeaders: 534 headers = headers + ',%s' % each 535 536 headers = headers + '\n' 537 538 csv = {} 539 540 for packet in source.packets['signals']: 541 542 543 if 'rawEeg' in packet.keys(): 544 continue 545 546 if packet['timestamp'] not in csv.keys(): 547 548 if 'blinkStrength' in packet.keys(): 549 # Skip any blink packets from log 550 continue 551 552 553 #print packet 554 timestamp = packet['timestamp'] 555 #(date, localtime) = self.parseTimeStamp(timestamp, \ 556 #truncate_time_zone=truncate_csv_timezone) 557 (date, localtime) = source.parseTimeStamp(timestamp, \ 558 truncate_time_zone=truncate_csv_timezone) 559 560 csv[timestamp] = {} 561 csv[timestamp]['Date'] = date 562 csv[timestamp]['Time'] = localtime 563 csv[timestamp]['Attention'] = '' 564 csv[timestamp]['Meditation'] = '' 565 csv[timestamp]['Signal Level'] = '' 566 csv[timestamp]['Delta'] = '' 567 csv[timestamp]['Theta'] = '' 568 csv[timestamp]['Low Alpha'] = '' 569 csv[timestamp]['High Alpha'] = '' 570 csv[timestamp]['Low Beta'] = '' 571 csv[timestamp]['High Beta'] = '' 572 csv[timestamp]['Low Gamma'] = '' 573 csv[timestamp]['Mid Gamma'] = '' 574 575 for header in customDataHeaders: 576 csv[timestamp][header] = '' 577 578 579 if 'eSense' in packet.keys(): 580 if 'attention' in packet['eSense'].keys(): 581 csv[timestamp]['Attention'] = packet['eSense']['attention'] 582 if 'meditation' in packet['eSense'].keys(): 583 csv[timestamp]['Meditation'] = packet['eSense']['meditation'] 584 585 if 'eegPower' in packet.keys(): 586 if 'delta' in packet['eegPower'].keys(): 587 csv[timestamp]['Delta'] = packet['eegPower']['delta'] 588 if 'theta' in packet['eegPower'].keys(): 589 csv[timestamp]['Theta'] = packet['eegPower']['theta'] 590 if 'lowAlpha' in packet['eegPower'].keys(): 591 csv[timestamp]['Low Alpha'] = packet['eegPower']['lowAlpha'] 592 if 'highAlpha' in packet['eegPower'].keys(): 593 csv[timestamp]['High Alpha'] = packet['eegPower']['highAlpha'] 594 if 'lowBeta' in packet['eegPower'].keys(): 595 csv[timestamp]['Low Beta'] = packet['eegPower']['lowBeta'] 596 if 'highBeta' in packet['eegPower'].keys(): 597 csv[timestamp]['High Beta'] = packet['eegPower']['highBeta'] 598 if 'lowGamma' in packet['eegPower'].keys(): 599 csv[timestamp]['Low Gamma'] = packet['eegPower']['lowGamma'] 600 if 'highGamma' in packet['eegPower'].keys(): 601 csv[timestamp]['Mid Gamma'] = packet['eegPower']['highGamma'] 602 603 if 'poorSignalLevel' in packet.keys(): 604 csv[timestamp]['Signal Level'] = packet['poorSignalLevel'] 605 606 for header in customDataHeaders: 607 if 'custom' in packet.keys() and \ 608 header in packet['custom'].keys(): 609 csv[timestamp][header] = packet['custom'][header] 610 611 612 if scrub_data: 613 csv = self.scrubData(csv, truncate_csv_timezone, source=source) 614 615 616 output = headers 617 618 csv_keys = csv.keys() 619 csv_keys.sort() 620 621 for key in csv_keys: 622 623 row = '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % \ 624 (csv[key]['Date'], \ 625 csv[key]['Time'], \ 626 csv[key]['Attention'], \ 627 csv[key]['Meditation'], \ 628 csv[key]['Signal Level'], \ 629 csv[key]['Delta'], \ 630 csv[key]['Theta'], \ 631 csv[key]['Low Alpha'], \ 632 csv[key]['High Alpha'], \ 633 csv[key]['Low Beta'], \ 634 csv[key]['High Beta'], \ 635 csv[key]['Low Gamma'], \ 636 csv[key]['Mid Gamma']) 637 638 for header in customDataHeaders: 639 row = row + ',%s' % csv[key][header] 640 641 row = row + '\n' 642 643 output = output + row 644 645 646 return(output) 647 648 649 ################################################################## 650 651 def scrubData(self, csv, truncate_csv_timezone=False, source=None): 652 653 # If there are missing packets, repeat a given packet once per missing 654 # second until there is a gap between 1 and 2 seconds, in which case 655 # produce a final duplicate packet at the mid-point between the packets 656 657 if self.DEBUG: 658 print "INFO: Scrubbing Data" 659 660 if source == None: 661 if self.parent == None: 662 source = self 663 else: 664 source = self.parent 665 666 last_time = None 667 last_recorded_time = None 668 669 output = {} 670 671 csv_keys = csv.keys() 672 csv_keys.sort() 673 674 for key in csv_keys: 675 676 timestamp = key 677 678 if csv[key]['Attention'] == '': 679 continue 680 681 if last_time == None: 682 # First entry in log 683 last_time = timestamp 684 last_recorded_time = timestamp 685 output[key] = csv[key] 686 continue 687 688 else: 689 690 #time_difference = timestamp - last_time 691 time_difference = timestamp - last_recorded_time 692 693 if (time_difference <= 1) and \ 694 (time_difference >= PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD): 695 # Skip packets within the correct time threshold 696 last_time = timestamp 697 last_recorded_time = timestamp 698 output[key] = csv[key] 699 continue 700 701 else: 702 703 if self.DEBUG > 1: 704 print "time_difference:", 705 print time_difference 706 print "timestamp:", 707 print source.parseTimeStamp(timestamp)[-1].split(' ')[0] 708 print "last_time:", 709 print source.parseTimeStamp(last_time)[-1].split(' ')[0] 710 print "last_recorded_time:", 711 print source.parseTimeStamp(last_recorded_time)[-1].split(' ')[0] 712 713 714 new_packet = csv[key].copy() 715 716 if time_difference >= 2: 717 718 ##new_time = last_time + 1 719 #new_time = last_recorded_time + 1 720 721 count = int(time_difference) 722 while count >= 1: 723 new_packet = csv[key].copy() 724 new_time = last_recorded_time + 1 725 (date, formatted_new_time) = source.parseTimeStamp(new_time, \ 726 truncate_time_zone=truncate_csv_timezone) 727 new_packet['Time'] = formatted_new_time 728 last_recorded_time = new_time 729 last_time = timestamp 730 output[new_time] = new_packet 731 count = count - 1 732 continue 733 734 elif time_difference < PACKET_MINIMUM_TIME_DIFFERENCE_THRESHOLD: 735 # Spread out "bunched up" packets 736 #new_time = last_time + 1 737 new_time = last_recorded_time + 1 738 739 740 elif (time_difference < 2) and (time_difference > 1): 741 742 #new_time = last_time + ((last_time - timestamp) / 2) 743 #new_time = last_recorded_time + ((last_recorded_time - timestamp) / 2) 744 #new_time = last_time + 1 745 new_time = last_recorded_time + 1 746 747 748 (date, formatted_new_time) = source.parseTimeStamp(new_time, \ 749 truncate_time_zone=truncate_csv_timezone) 750 751 new_packet['Time'] = formatted_new_time 752 753 #last_time = new_time 754 last_recorded_time = new_time 755 last_time = timestamp 756 output[new_time] = new_packet 757 758 if self.DEBUG > 1: 759 print "WARN: Scrubbing new packet:", 760 print new_packet 761 print 762 763 764 return(output) 765 766 767 ################################################################## 768 486 769 #def run(self): 487 770
Note: See TracChangeset
for help on using the changeset viewer.