Changeset 413
- Timestamp:
- 02/07/16 05:41:47 (7 years ago)
- Location:
- trunk/Puzzlebox/Synapse
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Puzzlebox/Synapse/Session.py
r411 r413 7 7 8 8 __changelog__ = """\ 9 Last Update: 201 6.01.269 Last Update: 2014.02.23 10 10 """ 11 11 … … 446 446 447 447 448 # TODO449 print "here"450 export_csv_training = True451 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_file458 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 468 448 ################################################################## 469 449 … … 724 704 return(output) 725 705 726 727 ##################################################################728 729 def exportTrainingDataToCSV(self, parent=None, source=None, target=None):730 731 # handle importing class from multiple sources732 if parent == None:733 if self.parent == None:734 parent = self735 else:736 parent = self.parent737 738 if source == None:739 if self.parent == None:740 source = self741 else:742 source = self.parent743 744 if target == None:745 if self.parent == None:746 target = self747 else:748 target = self.parent749 750 751 try:752 truncate_csv_timezone = target.configuration.EXPORT_CSV_TRUNCATE_TIMEZONE753 except:754 truncate_csv_timezone = False755 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.name779 for header in plugin.customDataHeaders:780 customDataHeaders.append(header)781 782 for each in customDataHeaders:783 headers = headers + ',%s' % each784 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 object797 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'] = date809 csv[timestamp]['Time'] = localtime810 csv[timestamp]['rawEeg'] = packet['rawEeg']811 812 813 csv[timestamp].update(self.collectProcessedData(parent, source, timestamp))814 815 816 output = headers817 818 timestamps = csv.keys()819 820 # Don't sort timestamps in order to better preserve the original raw signal821 #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 break842 843 844 845 846 row = row + '\n'847 848 output = output + row849 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 timestamp859 860 861 if self.DEBUG:862 print "Match Timestamp:",863 print match_timestamp864 865 866 packet_timestamps = []867 for packet in source.packets['signals']:868 869 if 'blinkStrength' in packet.keys():870 # Skip any blink packets from log871 continue872 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_timestamp886 887 #if (found == {}):888 #found = packet889 #continue890 891 # Keep searching until892 if (match_timestamp > timestamp):893 continue894 895 #found = packet896 897 if self.DEBUG:898 print "Matched Timestamp:",899 print timestamp900 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 each909 packet = each910 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.name919 try:920 data = plugin.processPacketForExport(packet=packet, output=data)921 if self.DEBUG > 2:922 print "INFO [Synapse:Session]: Export Successful"923 print plugin.name924 except Exception, e:925 if self.DEBUG:926 print "ERROR: [Synapse:Session] Exception calling processPacketForExport on",927 print plugin.name928 929 930 #print "INFO: data",931 #print data932 933 return data934 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'] = date948 #data['Time'] = localtime949 #if self.DEBUG:950 #print "WARN: Unmatched custom packet:",951 #print packet952 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 object962 ##if 'rawEeg' in packet.keys():963 ##continue964 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 packet970 ## skip empty packets971 #continue972 973 974 #print "packet:",975 #print packet976 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'] = date996 #csv[timestamp]['Time'] = localtime997 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.name1004 #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.name1009 #except Exception, e:1010 #if self.DEBUG:1011 #print "ERROR: [Synapse:Session] Exception calling processPacketForExport on",1012 #print plugin.name1013 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'] = date1027 #csv[timestamp]['Time'] = localtime1028 #if self.DEBUG:1029 #print "WARN: Unmatched custom packet:",1030 #print packet1031 1032 #csv[timestamp][header] = packet['custom'][header]1033 1034 706 1035 707 ################################################################# -
trunk/Puzzlebox/Synapse/ThinkGear/Protocol.py
r411 r413 86 86 import signal 87 87 import serial 88 import copy89 88 90 89 if ((sys.platform != 'win32') and \ … … 155 154 DEBUG_PACKET_COUNT = 1024 156 155 157 158 #CURRENT_SIGNAL = 0159 #CURRENT_ATTENTION = 0160 #CURRENT_MEDITATION = 0161 162 156 ##################################################################### 163 157 # Classes … … 191 185 self.data_packet['eegPower'] = {} 192 186 self.data_packet['eSense'] = {} 193 194 self.current_signal = 200195 self.current_attention = 0196 self.current_meditaiton = 0197 self.detection_threshold = 70198 self.current_detection = 0199 187 200 188 #self.packet_count = 0 … … 379 367 if (raw >= 32768): 380 368 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)397 369 398 370 … … 505 477 print poor_signal_quality 506 478 507 self.current_signal = copy.copy(poor_signal_quality)508 509 479 packet_update['poorSignalLevel'] = poor_signal_quality 510 511 self.current_signal = poor_signal_quality512 480 513 481 … … 518 486 print attention 519 487 520 self.current_attention = copy.copy(attention)521 522 if (attention > self.detection_threshold):523 self.current_detection = 1524 else:525 self.current_detection = 0526 527 488 packet_update['eSense'] = {} 528 489 packet_update['eSense']['attention'] = attention … … 534 495 print "meditation:", 535 496 print meditation 536 537 self.current_meditaiton = copy.copy(meditation)538 497 539 498 packet_update['eSense'] = {} … … 562 521 563 522 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)570 523 571 524 … … 939 892 def run(self): 940 893 941 try: 942 self.resetSession() 943 except Exception, e: 944 if self.DEBUG: 945 print "ERROR: self.resetSession():", 946 print e 894 self.resetSession() 947 895 948 896 if self.device != None and self.device.device != None: … … 1286 1234 self.buffer += byte 1287 1235 1288 except Exception, e:1236 except: 1289 1237 if self.DEBUG: 1290 print "ERROR: failed to read from serial device:", 1291 print e 1238 print "ERROR: failed to read from serial device" 1292 1239 break 1293 1240
Note: See TracChangeset
for help on using the changeset viewer.