Changeset 411
- Timestamp:
- 01/27/16 01:10:35 (5 years ago)
- Location:
- trunk/Puzzlebox/Synapse
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Puzzlebox/Synapse/Session.py
r410 r411 7 7 8 8 __changelog__ = """\ 9 Last Update: 201 4.02.239 Last Update: 2016.01.26 10 10 """ 11 11 … … 446 446 447 447 448 # TODO 449 print "here" 450 export_csv_training = True 451 if export_csv_training: 452 453 output_file = output_file.replace('.csv', '-trainingEeg.csv') 454 455 if self.DEBUG: 456 print "Writing file:", 457 print output_file 458 459 outputData = self.exportTrainingDataToCSV(parent=parent, source=source, target=target) 460 461 if outputData != None: 462 463 file = open(str(output_file), 'w') 464 file.write(outputData) 465 file.close() 466 467 448 468 ################################################################## 449 469 … … 704 724 return(output) 705 725 726 727 ################################################################## 728 729 def exportTrainingDataToCSV(self, parent=None, source=None, target=None): 730 731 # handle importing class from multiple sources 732 if parent == None: 733 if self.parent == None: 734 parent = self 735 else: 736 parent = self.parent 737 738 if source == None: 739 if self.parent == None: 740 source = self 741 else: 742 source = self.parent 743 744 if target == None: 745 if self.parent == None: 746 target = self 747 else: 748 target = self.parent 749 750 751 try: 752 truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE 753 except: 754 truncate_csv_timezone = False 755 756 757 if source.packets['rawEeg'] == []: 758 if self.DEBUG: 759 print "WARNING: No raw EEG packets" 760 return(None) 761 else: 762 if self.DEBUG: 763 print "INFO: Raw EEG packets:", 764 print len(source.packets['rawEeg']) 765 766 767 #headers = 'Date,Time,Raw EEG' 768 #headers = headers + '\n' 769 770 771 headers = 'Date,Time' 772 773 774 customDataHeaders = [] 775 for header in parent.customDataHeaders: 776 customDataHeaders.append(header) 777 for plugin in parent.activePlugins: 778 #print plugin.name 779 for header in plugin.customDataHeaders: 780 customDataHeaders.append(header) 781 782 for each in customDataHeaders: 783 headers = headers + ',%s' % each 784 785 786 headers = headers +',%s' % 'Raw EEG' 787 788 789 headers = headers + '\n' 790 791 792 csv = {} 793 794 for packet in source.packets['rawEeg']: 795 796 # NOTE: Move this to ThinkGear Server object 797 if 'rawEeg' in packet.keys(): 798 799 if packet['timestamp'] not in csv.keys(): 800 801 802 timestamp = packet['timestamp'] 803 804 (date, localtime) = source.parseTimeStamp(timestamp, \ 805 truncate_time_zone=truncate_csv_timezone) 806 807 csv[timestamp] = {} 808 csv[timestamp]['Date'] = date 809 csv[timestamp]['Time'] = localtime 810 csv[timestamp]['rawEeg'] = packet['rawEeg'] 811 812 813 csv[timestamp].update(self.collectProcessedData(parent, source, timestamp)) 814 815 816 output = headers 817 818 timestamps = csv.keys() 819 820 # Don't sort timestamps in order to better preserve the original raw signal 821 #timestamps.sort() 822 823 for timestamp in timestamps: 824 825 row = '%s,%s,%s' % \ 826 (csv[timestamp]['Date'], \ 827 csv[timestamp]['Time'], \ 828 csv[timestamp]['rawEeg']) 829 830 831 832 for header in customDataHeaders: 833 if header in csv[timestamp].keys(): 834 row = row + ',%s' % csv[timestamp][header] 835 else: 836 #row = row + ',' 837 row = '' 838 if self.DEBUG > 1: 839 print "WARN: empty signals packet:", 840 print csv[timestamp] 841 break 842 843 844 845 846 row = row + '\n' 847 848 output = output + row 849 850 851 return(output) 852 853 854 ################################################################# 855 856 def collectProcessedData(self, parent, source, match_timestamp): 857 858 # Return processed and custom data fields for this timestamp 859 860 861 if self.DEBUG: 862 print "Match Timestamp:", 863 print match_timestamp 864 865 866 packet_timestamps = [] 867 for packet in source.packets['signals']: 868 869 if 'blinkStrength' in packet.keys(): 870 # Skip any blink packets from log 871 continue 872 873 packet_timestamps.append(packet['timestamp']) 874 875 876 packet_timestamps.sort() 877 878 879 #found = {} 880 881 for timestamp in packet_timestamps: 882 883 if self.DEBUG: 884 print "--> Search Timestamp:", 885 print match_timestamp 886 887 #if (found == {}): 888 #found = packet 889 #continue 890 891 # Keep searching until 892 if (match_timestamp > timestamp): 893 continue 894 895 #found = packet 896 897 if self.DEBUG: 898 print "Matched Timestamp:", 899 print timestamp 900 901 902 packet = {} 903 904 for each in source.packets['signals']: 905 if each['timestamp'] == timestamp: 906 #if self.DEBUG: 907 #print "INFO: Packet Found:", 908 #print each 909 packet = each 910 911 912 data = {} 913 914 for plugin in parent.activePlugins: 915 if plugin.customDataHeaders != []: 916 if self.DEBUG > 2: 917 print "INFO: [Synapse:Session] Exporting:", 918 print plugin.name 919 try: 920 data = plugin.processPacketForExport(packet=packet, output=data) 921 if self.DEBUG > 2: 922 print "INFO [Synapse:Session]: Export Successful" 923 print plugin.name 924 except Exception, e: 925 if self.DEBUG: 926 print "ERROR: [Synapse:Session] Exception calling processPacketForExport on", 927 print plugin.name 928 929 930 #print "INFO: data", 931 #print data 932 933 return data 934 935 936 #for header in customDataHeaders: 937 938 #if 'custom' in packet.keys() and \ 939 #header in packet['custom'].keys(): 940 941 #timestamp = packet['timestamp'] 942 #(date, localtime) = source.parseTimeStamp(timestamp, \ 943 #truncate_time_zone=truncate_csv_timezone) 944 945 #if timestamp not in csv.keys(): 946 #data = {} 947 #data['Date'] = date 948 #data['Time'] = localtime 949 #if self.DEBUG: 950 #print "WARN: Unmatched custom packet:", 951 #print packet 952 953 #data[header] = packet['custom'][header] 954 955 956 957 958 #for packet in source.packets['signals']: 959 960 961 ## NOTE: Move this to ThinkGear Server object 962 ##if 'rawEeg' in packet.keys(): 963 ##continue 964 965 966 #if 'timestamp' not in packet.keys() and len(packet.keys()) == 1: 967 #if self.DEBUG: 968 #print "WARN: Skipping empty packet:", 969 #print packet 970 ## skip empty packets 971 #continue 972 973 974 #print "packet:", 975 #print packet 976 977 #timestamp = packet['timestamp'] 978 ##(date, localtime) = self.parseTimeStamp(timestamp, \ 979 ##truncate_time_zone=truncate_csv_timezone) 980 #(date, localtime) = source.parseTimeStamp(timestamp, \ 981 #truncate_time_zone=truncate_csv_timezone) 982 983 #if timestamp not in csv.keys(): 984 985 986 987 988 ##timestamp = packet['timestamp'] 989 ###(date, localtime) = self.parseTimeStamp(timestamp, \ 990 ###truncate_time_zone=truncate_csv_timezone) 991 ##(date, localtime) = source.parseTimeStamp(timestamp, \ 992 ##truncate_time_zone=truncate_csv_timezone) 993 994 #csv[timestamp] = {} 995 #csv[timestamp]['Date'] = date 996 #csv[timestamp]['Time'] = localtime 997 998 999 #for plugin in parent.activePlugins: 1000 #if plugin.customDataHeaders != []: 1001 #if self.DEBUG > 2: 1002 #print "INFO: [Synapse:Session] Exporting:", 1003 #print plugin.name 1004 #try: 1005 #csv[timestamp] = plugin.processPacketForExport(packet=packet, output=csv[timestamp]) 1006 #if self.DEBUG > 2: 1007 #print "INFO [Synapse:Session]: Export Successful" 1008 #print plugin.name 1009 #except Exception, e: 1010 #if self.DEBUG: 1011 #print "ERROR: [Synapse:Session] Exception calling processPacketForExport on", 1012 #print plugin.name 1013 1014 1015 #for header in customDataHeaders: 1016 1017 #if 'custom' in packet.keys() and \ 1018 #header in packet['custom'].keys(): 1019 1020 #timestamp = packet['timestamp'] 1021 #(date, localtime) = source.parseTimeStamp(timestamp, \ 1022 #truncate_time_zone=truncate_csv_timezone) 1023 1024 #if timestamp not in csv.keys(): 1025 #csv[timestamp] = {} 1026 #csv[timestamp]['Date'] = date 1027 #csv[timestamp]['Time'] = localtime 1028 #if self.DEBUG: 1029 #print "WARN: Unmatched custom packet:", 1030 #print packet 1031 1032 #csv[timestamp][header] = packet['custom'][header] 1033 706 1034 707 1035 ################################################################# -
trunk/Puzzlebox/Synapse/ThinkGear/Protocol.py
r390 r411 86 86 import signal 87 87 import serial 88 import copy 88 89 89 90 if ((sys.platform != 'win32') and \ … … 154 155 DEBUG_PACKET_COUNT = 1024 155 156 157 158 #CURRENT_SIGNAL = 0 159 #CURRENT_ATTENTION = 0 160 #CURRENT_MEDITATION = 0 161 156 162 ##################################################################### 157 163 # Classes … … 185 191 self.data_packet['eegPower'] = {} 186 192 self.data_packet['eSense'] = {} 193 194 self.current_signal = 200 195 self.current_attention = 0 196 self.current_meditaiton = 0 197 self.detection_threshold = 70 198 self.current_detection = 0 187 199 188 200 #self.packet_count = 0 … … 367 379 if (raw >= 32768): 368 380 raw = raw - 65536 381 382 383 384 #print "%s,%s,%s,%s,%s" % (time.time(), 385 #CURRENT_SIGNAL, 386 #CURRENT_ATTENTION, 387 #CURRENT_MEDITATION, 388 #raw) 389 390 if (self.current_signal == 0): 391 print "%s,%i,%i,%i,%s,%i" % (time.time(), 392 self.current_signal, 393 self.current_attention, 394 self.current_meditaiton, 395 raw, 396 self.current_detection) 369 397 370 398 … … 477 505 print poor_signal_quality 478 506 507 self.current_signal = copy.copy(poor_signal_quality) 508 479 509 packet_update['poorSignalLevel'] = poor_signal_quality 510 511 self.current_signal = poor_signal_quality 480 512 481 513 … … 486 518 print attention 487 519 520 self.current_attention = copy.copy(attention) 521 522 if (attention > self.detection_threshold): 523 self.current_detection = 1 524 else: 525 self.current_detection = 0 526 488 527 packet_update['eSense'] = {} 489 528 packet_update['eSense']['attention'] = attention … … 495 534 print "meditation:", 496 535 print meditation 536 537 self.current_meditaiton = copy.copy(meditation) 497 538 498 539 packet_update['eSense'] = {} … … 521 562 522 563 packet_update['rawEeg'] = raw_eeg_value 564 565 #print "%s,%i,%i,%i,%s" % (time.time(), 566 #self.current_signal, 567 #self.current_attention, 568 #self.current_meditaiton, 569 #raw_eeg_value) 523 570 524 571 … … 892 939 def run(self): 893 940 894 self.resetSession() 941 try: 942 self.resetSession() 943 except Exception, e: 944 if self.DEBUG: 945 print "ERROR: self.resetSession():", 946 print e 895 947 896 948 if self.device != None and self.device.device != None: … … 1234 1286 self.buffer += byte 1235 1287 1236 except :1288 except Exception, e: 1237 1289 if self.DEBUG: 1238 print "ERROR: failed to read from serial device" 1290 print "ERROR: failed to read from serial device:", 1291 print e 1239 1292 break 1240 1293
Note: See TracChangeset
for help on using the changeset viewer.