def _handleEvent(self, event): try: eventClass = None if self.checkForExperimentAndSessionIDs(event) is False: return False etype = event[DeviceEvent.EVENT_TYPE_ID_INDEX] # print2err("*** ",DeviceEvent.EVENT_TYPE_ID_INDEX, '_handleEvent: ',etype,' : event list: ',event) eventClass = EventConstants.getClass(etype) etable = self.TABLES[eventClass.IOHUB_DATA_TABLE] event[DeviceEvent. EVENT_EXPERIMENT_ID_INDEX] = self.active_experiment_id event[DeviceEvent.EVENT_SESSION_ID_INDEX] = self.active_session_id np_array = N.array([ tuple(event), ], dtype=eventClass.NUMPY_DTYPE) etable.append(np_array) self.bufferedFlush() except Exception: print2err("Error saving event: ", event) printExceptionDetailsToStdErr()
def _handleEvents(self, events): # saves many events to pytables table at once. # EVENTS MUST ALL BE OF SAME TYPE!!!!! try: #ioHub.print2err("_handleEvent: ",self.active_experiment_id,self.active_session_id) if self.checkForExperimentAndSessionIDs(len(events)) is False: return False event = events[0] etype = event[DeviceEvent.EVENT_TYPE_ID_INDEX] #ioHub.print2err("etype: ",etype) eventClass = EventConstants.getClass(etype) etable = self.TABLES[eventClass.IOHUB_DATA_TABLE] #ioHub.print2err("eventClass: etable",eventClass,etable) np_events = [] for event in events: event[DeviceEvent. EVENT_EXPERIMENT_ID_INDEX] = self.active_experiment_id event[DeviceEvent. EVENT_SESSION_ID_INDEX] = self.active_session_id np_events.append(tuple(event)) np_array = N.array(np_events, dtype=eventClass.NUMPY_DTYPE) #ioHub.print2err('np_array:',np_array) etable.append(np_array) self.bufferedFlush(len(np_events)) except ioHubError as e: print2err(e) except Exception: printExceptionDetailsToStdErr()
def _handleEvents(self, events): try: if self.checkForExperimentAndSessionIDs(len(events)) is False: return False event = events[0] etype = event[DeviceEvent.EVENT_TYPE_ID_INDEX] eventClass = EventConstants.getClass(etype) etable = self.TABLES[eventClass.IOHUB_DATA_TABLE] np_events = [] for event in events: event[DeviceEvent. EVENT_EXPERIMENT_ID_INDEX] = self.active_experiment_id event[DeviceEvent. EVENT_SESSION_ID_INDEX] = self.active_session_id np_events.append(tuple(event)) np_array = np.array(np_events, dtype=eventClass.NUMPY_DTYPE) #ioHub.print2err('np_array:',np_array) etable.append(np_array) self.bufferedFlush(len(np_events)) except ioHubError as e: print2err(e) except Exception: printExceptionDetailsToStdErr()
def _convertToMonoAveraged(self, prev_event, current_event): mono_evt=[] binoc_field_names = EventConstants.getClass(EventConstants.BINOCULAR_EYE_SAMPLE).CLASS_ATTRIBUTE_NAMES #print2err("binoc_field_names: ",len(binoc_field_names),"\n",binoc_field_names) status = current_event[binoc_field_names.index('status')] for field in self.io_event_fields: if field in binoc_field_names: mono_evt.append(current_event[binoc_field_names.index(field)]) elif field == 'eye': mono_evt.append(LEFT_EYE) elif field.endswith('_type'): mono_evt.append(int(current_event[binoc_field_names.index('left_%s'%(field))])) else: #print2err("binoc status: ",status) if status == 0: lfv = float(current_event[binoc_field_names.index('left_%s'%(field))]) rfv = float(current_event[binoc_field_names.index('right_%s'%(field))]) mono_evt.append((lfv+rfv)/2.0) elif status == 2: mono_evt.append(float(current_event[binoc_field_names.index('left_%s'%(field))])) elif status == 20: mono_evt.append(float(current_event[binoc_field_names.index('right_%s'%(field))])) elif status == 22: # both eyes have missing data, so use data from left eye (does not really matter) mono_evt.append(float(current_event[binoc_field_names.index('left_%s'%(field))])) else: ValueError("Unknown Sample Status: %d"%(status)) mono_evt[self.io_event_fields.index('type')] = EventConstants.MONOCULAR_EYE_SAMPLE if self.isValidSample(mono_evt): self._convertPosToAngles(mono_evt) if prev_event: self._addVelocity(prev_event, mono_evt) return mono_evt
def processDeviceEvents(self): for device in self.devices: try: events = device._getNativeEventBuffer() while len(events) > 0: evt = events.popleft() e = device._getIOHubEventObject(evt) if e is not None: for l in device._getEventListeners( e[DeviceEvent.EVENT_TYPE_ID_INDEX]): l._handleEvent(e) filtered_events = [] for filter in device._filters.values(): filtered_events.extend(filter._removeOutputEvents()) for i in range(len(filtered_events)): e = filtered_events[i] for l in device._getEventListeners( e[DeviceEvent.EVENT_TYPE_ID_INDEX]): l._handleEvent(e) except Exception: printExceptionDetailsToStdErr() print2err("Error in processDeviceEvents: ", device, " : ", len(events), " : ", e) print2err( "Event type ID: ", e[DeviceEvent.EVENT_TYPE_ID_INDEX], " : ", EventConstants.getName(e[DeviceEvent.EVENT_TYPE_ID_INDEX])) print2err("--------------------------------------")
def _handleEvents(self, events): # saves many events to pytables table at once. # EVENTS MUST ALL BE OF SAME TYPE!!!!! try: #ioHub.print2err("_handleEvent: ",self.active_experiment_id,self.active_session_id) if self.checkForExperimentAndSessionIDs(len(events)) is False: return False event=events[0] etype=event[DeviceEvent.EVENT_TYPE_ID_INDEX] #ioHub.print2err("etype: ",etype) eventClass=EventConstants.getClass(etype) etable=self.TABLES[eventClass.IOHUB_DATA_TABLE] #ioHub.print2err("eventClass: etable",eventClass,etable) np_events=[] for event in events: event[DeviceEvent.EVENT_EXPERIMENT_ID_INDEX]=self.active_experiment_id event[DeviceEvent.EVENT_SESSION_ID_INDEX]=self.active_session_id np_events.append(tuple(event)) np_array= N.array(np_events,dtype=eventClass.NUMPY_DTYPE) #ioHub.print2err('np_array:',np_array) etable.append(np_array) self.bufferedFlush(len(np_events)) except ioHubError, e: print2err(e)
def _handleEvents(self, events): try: if self.checkForExperimentAndSessionIDs(len(events)) is False: return False event = events[0] etype = event[DeviceEvent.EVENT_TYPE_ID_INDEX] eventClass = EventConstants.getClass(etype) etable = self.TABLES[eventClass.IOHUB_DATA_TABLE] np_events = [] for event in events: event[DeviceEvent.EVENT_EXPERIMENT_ID_INDEX] = self.active_experiment_id event[DeviceEvent.EVENT_SESSION_ID_INDEX] = self.active_session_id np_events.append(tuple(event)) np_array = np.array(np_events, dtype=eventClass.NUMPY_DTYPE) #ioHub.print2err('np_array:',np_array) etable.append(np_array) self.bufferedFlush(len(np_events)) except ioHubError as e: print2err(e) except Exception: printExceptionDetailsToStdErr()
def initializeForSampleType(self,in_evt): self.sample_type = MONOCULAR_EYE_SAMPLE #in_evt[DeviceEvent.EVENT_TYPE_ID_INDEX] #print2err("self.sample_type: ",self.sample_type,", ",EventConstants.getName(self.sample_type)) self.io_sample_class = EventConstants.getClass(self.sample_type) self.io_event_fields = self.io_sample_class.CLASS_ATTRIBUTE_NAMES #print2err("self.io_sample_class: ",self.io_sample_class,", ",len(self.io_event_fields),"\n>>",self.io_event_fields) self.io_event_ix = self.io_sample_class.CLASS_ATTRIBUTE_NAMES.index if in_evt[DeviceEvent.EVENT_TYPE_ID_INDEX] == BINOCULAR_EYE_SAMPLE: self.convertEvent = self._convertToMonoAveraged self.isValidSample = lambda x: x[self.io_event_ix('status')] != 22 else: self.convertEvent = self._convertMonoFields self.isValidSample = lambda x: x[self.io_event_ix('status')] == 0
def _handleEvent(self, event): try: if self.checkForExperimentAndSessionIDs(event) is False: return False etype = event[DeviceEvent.EVENT_TYPE_ID_INDEX] eventClass = EventConstants.getClass(etype) etable = self.TABLES[eventClass.IOHUB_DATA_TABLE] event[DeviceEvent.EVENT_EXPERIMENT_ID_INDEX] = self.active_experiment_id event[DeviceEvent.EVENT_SESSION_ID_INDEX] = self.active_session_id np_array = np.array([tuple(event), ], dtype=eventClass.NUMPY_DTYPE) etable.append(np_array) self.bufferedFlush() except Exception: print2err("Error saving event: ", event) printExceptionDetailsToStdErr()
def printEventTypesWithDataDemo(): # Create an instance of the ExperimentDataAccessUtility class # for the selected DataStore file. This allows us to access data # in the file based on Device Event names and attributes. # experiment_data=ExperimentDataAccessUtility('..\hdf5_files' , 'events.hdf5') # Get any event tables that have >=1 event saved in them # events_by_type=experiment_data.getEventsByType() # print out info on each table # for event_id, event_gen in events_by_type.iteritems(): event_constant=EventConstants.getName(event_id) print "{0} ({1}): {2}".format(event_constant,event_gen.table.nrows,event_gen) # Close the HDF5 File # experiment_data.close()
def _processDeviceEventIteration(self): for device in self.devices: try: events = device._getNativeEventBuffer() #if events and len(events)>0: # ioHub.print2err("_processDeviceEventIteration.....", device._event_listeners) while len(events) > 0: evt = events.popleft() e = device._getIOHubEventObject(evt) if e is not None: for l in device._getEventListeners( e[DeviceEvent.EVENT_TYPE_ID_INDEX]): l._handleEvent(e) except: printExceptionDetailsToStdErr() print2err("Error in processDeviceEvents: ", device, " : ", len(events), " : ", e) print2err( "Event type ID: ", e[DeviceEvent.EVENT_TYPE_ID_INDEX], " : ", EventConstants.getName(e[DeviceEvent.EVENT_TYPE_ID_INDEX])) print2err("--------------------------------------")
def _processDeviceEventIteration(self): for device in self.devices: try: events=device._getNativeEventBuffer() #if events and len(events)>0: # ioHub.print2err("_processDeviceEventIteration.....", device._event_listeners) while len(events)>0: evt=events.popleft() e=device._getIOHubEventObject(evt) if e is not None: for l in device._getEventListeners(e[DeviceEvent.EVENT_TYPE_ID_INDEX]): l._handleEvent(e) except: printExceptionDetailsToStdErr() print2err("Error in processDeviceEvents: ", device, " : ", len(events), " : ", e) print2err("Event type ID: ",e[DeviceEvent.EVENT_TYPE_ID_INDEX], " : " , EventConstants.getName(e[DeviceEvent.EVENT_TYPE_ID_INDEX])) print2err("--------------------------------------")
def createNewMonitoredDevice(self,device_class_name,deviceConfig): #print2err("#### createNewMonitoredDevice: ",device_class_name) self._all_device_config_errors=dict() try: device_instance=None device_config=None device_event_ids=None event_classes=None device_instance_and_config=self.addDeviceToMonitor(device_class_name,deviceConfig) if device_instance_and_config: device_instance,device_config,device_event_ids,event_classes=device_instance_and_config DeviceConstants.addClassMapping(device_instance.__class__) EventConstants.addClassMappings(device_instance.__class__,device_event_ids,event_classes) else: print2err('## Device was not started by the ioHub Server: ',device_class_name) raise ioHubError("Device config validation failed") except: print2err("Error during device creation ....") printExceptionDetailsToStdErr() raise ioHubError("Error during device creation ....") # Update DataStore Structure if required. if psychopy.iohub._DATA_STORE_AVAILABLE: try: if self.emrt_file is not None: self.emrt_file.updateDataStoreStructure(device_instance,event_classes) except: print2err("Error while updating datastore for device addition:",device_instance,device_event_ids) printExceptionDetailsToStdErr() self.log("Adding ioServer and DataStore event listeners......") # add event listeners for saving events if psychopy.iohub._DATA_STORE_AVAILABLE and self.emrt_file is not None: if device_config['save_events']: device_instance._addEventListener(self.emrt_file,device_event_ids) self.log("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids)) #print2err("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids)) else: #print2err("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,)) self.log("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,)) else: #print2err("DataStore Not Evabled. No events will be saved.") self.log("DataStore Not Enabled. No events will be saved.") # Add Device Monitor for Keyboard or Mouse device type deviceDict=ioServer.deviceDict iohub=self if device_class_name in ('Mouse','Keyboard'): if Computer.system == 'win32': if self._hookDevice is None: iohub.log("Creating pyHook Monitors....") #print2err("Creating pyHook Monitor....") class pyHookDevice(object): def __init__(self): import pyHook self._hookManager=pyHook.HookManager() self._mouseHooked=False self._keyboardHooked=False if device_class_name == 'Mouse': #print2err("Hooking Mouse.....") self._hookManager.MouseAll = deviceDict['Mouse']._nativeEventCallback self._hookManager.HookMouse() self._mouseHooked=True elif device_class_name == 'Keyboard': #print2err("Hooking Keyboard.....") self._hookManager.KeyAll = deviceDict['Keyboard']._nativeEventCallback self._hookManager.HookKeyboard() self._keyboardHooked=True #iohub.log("WindowsHook PumpEvents Periodic Timer Created.") def _poll(self): import pythoncom # PumpWaitingMessages returns 1 if a WM_QUIT message was received, else 0 if pythoncom.PumpWaitingMessages() == 1: raise KeyboardInterrupt() #print2err("Creating pyHook Monitor......") self._hookDevice=pyHookDevice() hookMonitor=DeviceMonitor(self._hookDevice,0.00375) self.deviceMonitors.append(hookMonitor) #print2err("Created pyHook Monitor.") else: #print2err("UPDATING pyHook Monitor....") if device_class_name == 'Mouse' and self._hookDevice._mouseHooked is False: #print2err("Hooking Mouse.....") self._hookDevice._hookManager.MouseAll = deviceDict['Mouse']._nativeEventCallback self._hookDevice._hookManager.HookMouse() self._hookDevice._mouseHooked=True elif device_class_name == 'Keyboard' and self._hookDevice._keyboardHooked is False: #print2err("Hooking Keyboard.....") self._hookDevice._hookManager.KeyAll = deviceDict['Keyboard']._nativeEventCallback self._hookDevice._hookManager.HookKeyboard() self._hookDevice._keyboardHooked=True #print2err("Finished Updating pyHook Monitor.") elif Computer.system == 'linux2': # TODO: consider switching to xlib-ctypes implementation of xlib # https://github.com/garrybodsworth/pyxlib-ctypes from .devices import pyXHook if self._hookManager is None: #iohub.log("Creating pyXHook Monitors....") self._hookManager = pyXHook.HookManager() self._hookManager._mouseHooked=False self._hookManager._keyboardHooked=False if device_class_name == 'Keyboard': #print2err("Hooking Keyboard.....") self._hookManager.HookKeyboard() self._hookManager.KeyDown = deviceDict['Keyboard']._nativeEventCallback self._hookManager.KeyUp = deviceDict['Keyboard']._nativeEventCallback self._hookManager._keyboardHooked=True elif device_class_name == 'Mouse': #print2err("Hooking Mouse.....") self._hookManager.HookMouse() self._hookManager.MouseAllButtonsDown = deviceDict['Mouse']._nativeEventCallback self._hookManager.MouseAllButtonsUp = deviceDict['Mouse']._nativeEventCallback self._hookManager.MouseAllMotion = deviceDict['Mouse']._nativeEventCallback self._hookManager._mouseHooked=True #print2err("Starting pyXHook.HookManager.....") self._hookManager.start() #iohub.log("pyXHook Thread Created.") #print2err("pyXHook.HookManager thread created.") else: #iohub.log("Updating pyXHook Monitor....") if device_class_name == 'Keyboard' and self._hookManager._keyboardHooked is False: #print2err("Hooking Keyboard.....") self._hookManager.HookKeyboard() self._hookManager.KeyDown = deviceDict['Keyboard']._nativeEventCallback self._hookManager.KeyUp = deviceDict['Keyboard']._nativeEventCallback self._hookManager._keyboardHooked=True if device_class_name == 'Mouse' and self._hookManager._mouseHooked is False: #print2err("Hooking Mouse.....") self._hookManager.HookMouse() self._hookManager.MouseAllButtonsDown = deviceDict['Mouse']._nativeEventCallback self._hookManager.MouseAllButtonsUp = deviceDict['Mouse']._nativeEventCallback self._hookManager.MouseAllMotion = deviceDict['Mouse']._nativeEventCallback self._hookManager._mouseHooked=True #iohub.log("Finished Updating pyXHook Monitor....") else: # OSX if self._hookDevice is None: self._hookDevice=[] if device_class_name == 'Mouse' and 'Mouse' not in self._hookDevice: #print2err("Hooking OSX Mouse.....") mouseHookMonitor=DeviceMonitor(deviceDict['Mouse'],0.004) self.deviceMonitors.append(mouseHookMonitor) deviceDict['Mouse']._CGEventTapEnable(deviceDict['Mouse']._tap, True) self._hookDevice.append('Mouse') #print2err("Done Hooking OSX Mouse.....") if device_class_name == 'Keyboard' and 'Keyboard' not in self._hookDevice: #print2err("Hooking OSX Keyboard.....") kbHookMonitor=DeviceMonitor(deviceDict['Keyboard'],0.004) self.deviceMonitors.append(kbHookMonitor) deviceDict['Keyboard']._CGEventTapEnable(deviceDict['Keyboard']._tap, True) self._hookDevice.append('Keyboard') #print2err("DONE Hooking OSX Keyboard.....") return [device_class_name, device_config['name'], device_instance._getRPCInterface()]
def __init__(self, rootScriptPathDir, config=None): self._session_id=None self._experiment_id=None self.log("Server Time Offset: {0}".format(Computer.globalClock.getLastResetTime())) self._hookManager=None self.emrt_file=None self.config=config self.devices=[] self.deviceMonitors=[] self.sessionInfoDict=None self.experimentInfoList=None self.filterLookupByInput={} self.filterLookupByOutput={} self.filterLookupByName={} self._hookDevice=None ioServer.eventBuffer=deque(maxlen=config.get('global_event_buffer',2048)) self._running=True # start UDP service self.udpService=udpServer(self,':%d'%config.get('udp_port',9000)) try: # initial dataStore setup if 'data_store' in config and psychopy.iohub._DATA_STORE_AVAILABLE: experiment_datastore_config=config.get('data_store') default_datastore_config_path=os.path.join(IO_HUB_DIRECTORY,'datastore','default_datastore.yaml') #print2err('default_datastore_config_path: ',default_datastore_config_path) _dslabel,default_datastore_config=load(file(default_datastore_config_path,'r'), Loader=Loader).popitem() for default_key,default_value in default_datastore_config.iteritems(): if default_key not in experiment_datastore_config: experiment_datastore_config[default_key]=default_value if experiment_datastore_config.get('enable', True): #print2err("Creating ioDataStore....") resultsFilePath=rootScriptPathDir self.createDataStoreFile(experiment_datastore_config.get('filename','events')+'.hdf5',resultsFilePath,'a',experiment_datastore_config) #print2err("Created ioDataStore.") except: print2err("Error during ioDataStore creation....") printExceptionDetailsToStdErr() #built device list and config from initial yaml config settings try: for iodevice in config.get('monitor_devices',()): for device_class_name,deviceConfig in iodevice.iteritems(): #print2err("======================================================") #print2err("Started load process for: {0}".format(device_class_name)) self.createNewMonitoredDevice(device_class_name,deviceConfig) except: print2err("Error during device creation ....") printExceptionDetailsToStdErr() raise ioHubError("Error during device creation ....") # Add PubSub device listeners to other event types try: for d in self.devices: if d.__class__.__name__ == "EventPublisher": monitored_event_ids=d._event_listeners.keys() for eid in monitored_event_ids: event_device_class=EventConstants.getClass(eid).PARENT_DEVICE for ed in self.devices: if ed.__class__ == event_device_class: ed._addEventListener(d,[eid,]) break except Exception, e: print2err("Error PubSub Device listener association ....") printExceptionDetailsToStdErr() raise e
def getEventDetails(session_path, frame_event_lookup_file, filters, batch_size): session_folder = os.path.normpath(os.path.abspath(session_path)) datastore_reader = openDataStoreReader(session_folder) frame_events_lookup = np.load( os.path.join(session_folder, frame_event_lookup_file))['events'] event_types = filters.get('event_type', np.unique(frame_events_lookup['event_type'])) for etype in event_types: filters['event_type'] = [ etype, ] filtered_table_rows = Picker(frame_events_lookup).pick_data( 'event_table_row_index', **filters) event_table = datastore_reader.getEventTable(etype) while len(filtered_table_rows) > 0: row_nums = [int(i) for i in filtered_table_rows[:batch_size]] yield event_table[row_nums] filtered_table_rows = filtered_table_rows[batch_size:] datastore_reader.close() if __name__ == '__main__': for event_details_batch in getEventDetails(SESSION_PATH, FRAME_EVENT_FILE, FILTERS, BATCH_SIZE): for event in event_details_batch: print EventConstants.getName( event['type']), event['time'], event['filter_id']
[s.draw() for s in STIM_LIST] flip_time = window.flip() # Update text fields based on all Keyboard Event types that have occurred # since the last call to getEvents of clearEvents(). This means that the most # recent event in the list of a given event type is what you will see. # for event in keyboard.getEvents(): #print 'KB EVT: ', event, '\n' key_text_stim.setText(event.key) ucode_stim.setText('{0:#06x} = {1}'.format(event.ucode, unichr(event.ucode))) modifiers_stim.setText(str(event.modifiers)) if event.type == EventConstants.KEYBOARD_CHAR: keypress_duration_stim.setText("%.6f" % (event.duration)) event_type_stim.setText(EventConstants.getName(event.type)) demo_timeout_start = event.time if (event.key.lower() == 'q' and ('CONTROL_LEFT' in event.modifiers or 'CONTROL_LEFT' in event.modifiers)): QUIT_EXP = True break if flip_time - demo_timeout_start > 15.0: print "Ending Demo Due to 15 Seconds of Inactivity." break # Send a message to the iohub with the message text that a flip # occurred and what the mouse position was. Since we know the
# Redraw stim and window flip... # [s.draw() for s in STIM_LIST] flip_time=window.flip() # Update text fields based on all Keyboard Event types that have occurred # since the last call to getEvents of clearEvents(). This means that the most # recent event in the list of a given event type is what you will see. # for event in keyboard.getEvents(): key_text_stim.setText(event.key) ucode_stim.setText('{0:#06x} = {1}'.format(event.ucode,unichr(event.ucode))) modifiers_stim.setText(str(event.modifiers)) if event.type == EventConstants.KEYBOARD_CHAR: keypress_duration_stim.setText("%.6f"%(event.duration)) event_type_stim.setText(EventConstants.getName(event.type)) if event.key.lower()=='q' and ('CONTROL_LEFT' in event.modifiers or 'CONTROL_LEFT' in event.modifiers): QUIT_EXP=True break # Send a message to the iohub with the message text that a flip # occurred and what the mouse position was. Since we know the # psychopy-iohub time the flip occurred on, we can set that directly # in the event. #self.hub.sendMessageEvent("Flip %s"%(str(currentPosition),), sec_time=flip_time) # Done demo loop, cleanup explicitly # io.quit()
def _processDeviceEventIteration(self): for device in self.devices: try: events = device._getNativeEventBuffer() while len(events) > 0: evt = events.popleft() e = device._getIOHubEventObject(evt) if e is not None: for l in device._getEventListeners(e[DeviceEvent.EVENT_TYPE_ID_INDEX]): l._handleEvent(e) filtered_events = [] for filter in device._filters.values(): filtered_events.extend(filter._removeOutputEvents()) for i in range(len(filtered_events)): e = filtered_events[i] for l in device._getEventListeners(e[DeviceEvent.EVENT_TYPE_ID_INDEX]): l._handleEvent(e) except: printExceptionDetailsToStdErr() print2err("Error in processDeviceEvents: ", device, " : ", len(events), " : ", e) print2err("Event type ID: ",e[DeviceEvent.EVENT_TYPE_ID_INDEX], " : " , EventConstants.getName(e[DeviceEvent.EVENT_TYPE_ID_INDEX])) print2err("--------------------------------------")
def __init__(self, rootScriptPathDir, config=None): self._session_id = None self._experiment_id = None self.log("Server Time Offset: {0}".format( Computer.globalClock.getLastResetTime())) self._hookManager = None self.emrt_file = None self.config = config self.devices = [] self.deviceMonitors = [] self.sessionInfoDict = None self.experimentInfoList = None self.filterLookupByInput = {} self.filterLookupByOutput = {} self.filterLookupByName = {} self._hookDevice = None ioServer.eventBuffer = deque( maxlen=config.get('global_event_buffer', 2048)) self._running = True # start UDP service self.udpService = udpServer(self, ':%d' % config.get('udp_port', 9000)) try: # initial dataStore setup if 'data_store' in config and psychopy.iohub._DATA_STORE_AVAILABLE: experiment_datastore_config = config.get('data_store') default_datastore_config_path = os.path.join( IO_HUB_DIRECTORY, 'datastore', 'default_datastore.yaml') #print2err('default_datastore_config_path: ',default_datastore_config_path) _dslabel, default_datastore_config = load( file(default_datastore_config_path, 'r'), Loader=Loader).popitem() for default_key, default_value in default_datastore_config.iteritems( ): if default_key not in experiment_datastore_config: experiment_datastore_config[ default_key] = default_value if experiment_datastore_config.get('enable', True): #print2err("Creating ioDataStore....") resultsFilePath = rootScriptPathDir self.createDataStoreFile( experiment_datastore_config.get('filename', 'events') + '.hdf5', resultsFilePath, 'a', experiment_datastore_config) #print2err("Created ioDataStore.") except: print2err("Error during ioDataStore creation....") printExceptionDetailsToStdErr() #built device list and config from initial yaml config settings try: for iodevice in config.get('monitor_devices', ()): for device_class_name, deviceConfig in iodevice.iteritems(): #print2err("======================================================") #print2err("Started load process for: {0}".format(device_class_name)) self.createNewMonitoredDevice(device_class_name, deviceConfig) except: print2err("Error during device creation ....") printExceptionDetailsToStdErr() raise ioHubError("Error during device creation ....") # Add PubSub device listeners to other event types try: for d in self.devices: if d.__class__.__name__ == "EventPublisher": monitored_event_ids = d._event_listeners.keys() for eid in monitored_event_ids: event_device_class = EventConstants.getClass( eid).PARENT_DEVICE for ed in self.devices: if ed.__class__ == event_device_class: ed._addEventListener(d, [ eid, ]) break except Exception, e: print2err("Error PubSub Device listener association ....") printExceptionDetailsToStdErr() raise e
def createNewMonitoredDevice(self, device_class_name, deviceConfig): #print2err("#### createNewMonitoredDevice: ",device_class_name) self._all_device_config_errors = dict() try: device_instance = None device_config = None device_event_ids = None event_classes = None device_instance_and_config = self.addDeviceToMonitor( device_class_name, deviceConfig) if device_instance_and_config: device_instance, device_config, device_event_ids, event_classes = device_instance_and_config DeviceConstants.addClassMapping(device_instance.__class__) EventConstants.addClassMappings(device_instance.__class__, device_event_ids, event_classes) else: print2err('## Device was not started by the ioHub Server: ', device_class_name) raise ioHubError("Device config validation failed") except: print2err("Error during device creation ....") printExceptionDetailsToStdErr() raise ioHubError("Error during device creation ....") # Update DataStore Structure if required. if psychopy.iohub._DATA_STORE_AVAILABLE: try: if self.emrt_file is not None: self.emrt_file.updateDataStoreStructure( device_instance, event_classes) except: print2err( "Error while updating datastore for device addition:", device_instance, device_event_ids) printExceptionDetailsToStdErr() self.log("Adding ioServer and DataStore event listeners......") # add event listeners for saving events if psychopy.iohub._DATA_STORE_AVAILABLE and self.emrt_file is not None: if device_config['save_events']: device_instance._addEventListener(self.emrt_file, device_event_ids) self.log( "DataStore listener for device added: device: %s eventIDs: %s" % (device_instance.__class__.__name__, device_event_ids)) #print2err("DataStore listener for device added: device: %s eventIDs: %s"%(device_instance.__class__.__name__,device_event_ids)) else: #print2err("DataStore saving disabled for device: %s"%(device_instance.__class__.__name__,)) self.log("DataStore saving disabled for device: %s" % (device_instance.__class__.__name__, )) else: #print2err("DataStore Not Evabled. No events will be saved.") self.log("DataStore Not Enabled. No events will be saved.") # Add Device Monitor for Keyboard or Mouse device type deviceDict = ioServer.deviceDict iohub = self if device_class_name in ('Mouse', 'Keyboard'): if Computer.system == 'win32': if self._hookDevice is None: iohub.log("Creating pyHook Monitors....") #print2err("Creating pyHook Monitor....") class pyHookDevice(object): def __init__(self): import pyHook self._hookManager = pyHook.HookManager() self._mouseHooked = False self._keyboardHooked = False if device_class_name == 'Mouse': #print2err("Hooking Mouse.....") self._hookManager.MouseAll = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager.HookMouse() self._mouseHooked = True elif device_class_name == 'Keyboard': #print2err("Hooking Keyboard.....") self._hookManager.KeyAll = deviceDict[ 'Keyboard']._nativeEventCallback self._hookManager.HookKeyboard() self._keyboardHooked = True #iohub.log("WindowsHook PumpEvents Periodic Timer Created.") def _poll(self): import pythoncom # PumpWaitingMessages returns 1 if a WM_QUIT message was received, else 0 if pythoncom.PumpWaitingMessages() == 1: raise KeyboardInterrupt() #print2err("Creating pyHook Monitor......") self._hookDevice = pyHookDevice() hookMonitor = DeviceMonitor( self._hookDevice, self.config.get('windows_msgpump_interval', 0.00375)) self.deviceMonitors.append(hookMonitor) #print2err("Created pyHook Monitor.") else: #print2err("UPDATING pyHook Monitor....") if device_class_name == 'Mouse' and self._hookDevice._mouseHooked is False: #print2err("Hooking Mouse.....") self._hookDevice._hookManager.MouseAll = deviceDict[ 'Mouse']._nativeEventCallback self._hookDevice._hookManager.HookMouse() self._hookDevice._mouseHooked = True elif device_class_name == 'Keyboard' and self._hookDevice._keyboardHooked is False: #print2err("Hooking Keyboard.....") self._hookDevice._hookManager.KeyAll = deviceDict[ 'Keyboard']._nativeEventCallback self._hookDevice._hookManager.HookKeyboard() self._hookDevice._keyboardHooked = True #print2err("Finished Updating pyHook Monitor.") elif Computer.system == 'linux2': # TODO: consider switching to xlib-ctypes implementation of xlib # https://github.com/garrybodsworth/pyxlib-ctypes from .devices import pyXHook if self._hookManager is None: #iohub.log("Creating pyXHook Monitors....") self._hookManager = pyXHook.HookManager() self._hookManager._mouseHooked = False self._hookManager._keyboardHooked = False if device_class_name == 'Keyboard': #print2err("Hooking Keyboard.....") self._hookManager.HookKeyboard() self._hookManager.KeyDown = deviceDict[ 'Keyboard']._nativeEventCallback self._hookManager.KeyUp = deviceDict[ 'Keyboard']._nativeEventCallback self._hookManager._keyboardHooked = True elif device_class_name == 'Mouse': #print2err("Hooking Mouse.....") self._hookManager.HookMouse() self._hookManager.MouseAllButtonsDown = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager.MouseAllButtonsUp = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager.MouseAllMotion = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager._mouseHooked = True #print2err("Starting pyXHook.HookManager.....") self._hookManager.start() #iohub.log("pyXHook Thread Created.") #print2err("pyXHook.HookManager thread created.") else: #iohub.log("Updating pyXHook Monitor....") if device_class_name == 'Keyboard' and self._hookManager._keyboardHooked is False: #print2err("Hooking Keyboard.....") self._hookManager.HookKeyboard() self._hookManager.KeyDown = deviceDict[ 'Keyboard']._nativeEventCallback self._hookManager.KeyUp = deviceDict[ 'Keyboard']._nativeEventCallback self._hookManager._keyboardHooked = True if device_class_name == 'Mouse' and self._hookManager._mouseHooked is False: #print2err("Hooking Mouse.....") self._hookManager.HookMouse() self._hookManager.MouseAllButtonsDown = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager.MouseAllButtonsUp = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager.MouseAllMotion = deviceDict[ 'Mouse']._nativeEventCallback self._hookManager._mouseHooked = True #iohub.log("Finished Updating pyXHook Monitor....") else: # OSX if self._hookDevice is None: self._hookDevice = [] if device_class_name == 'Mouse' and 'Mouse' not in self._hookDevice: #print2err("Hooking OSX Mouse.....") mouseHookMonitor = DeviceMonitor(deviceDict['Mouse'], 0.004) self.deviceMonitors.append(mouseHookMonitor) deviceDict['Mouse']._CGEventTapEnable( deviceDict['Mouse']._tap, True) self._hookDevice.append('Mouse') #print2err("Done Hooking OSX Mouse.....") if device_class_name == 'Keyboard' and 'Keyboard' not in self._hookDevice: #print2err("Hooking OSX Keyboard.....") kbHookMonitor = DeviceMonitor(deviceDict['Keyboard'], 0.004) self.deviceMonitors.append(kbHookMonitor) deviceDict['Keyboard']._CGEventTapEnable( deviceDict['Keyboard']._tap, True) self._hookDevice.append('Keyboard') #print2err("DONE Hooking OSX Keyboard.....") return [ device_class_name, device_config['name'], device_instance._getRPCInterface() ]
import os from common import openDataStoreReader, Picker import numpy as np SESSION_PATH = r'..\..\Results\Default_Experiment\s1' FRAME_EVENT_FILE = r'session_vframe_events.npz' FILTERS = dict(event_type=[EventConstants.MONOCULAR_EYE_SAMPLE,], video_id=[0,], filter_id=[23,]) BATCH_SIZE = 100 def getEventDetails(session_path, frame_event_lookup_file, filters, batch_size): session_folder = os.path.normpath(os.path.abspath(session_path)) datastore_reader = openDataStoreReader(session_folder) frame_events_lookup = np.load(os.path.join(session_folder, frame_event_lookup_file))['events'] event_types = filters.get('event_type', np.unique(frame_events_lookup['event_type'])) for etype in event_types: filters['event_type'] = [etype,] filtered_table_rows = Picker(frame_events_lookup).pick_data('event_table_row_index', **filters) event_table = datastore_reader.getEventTable(etype) while len(filtered_table_rows) > 0: row_nums = [int(i) for i in filtered_table_rows[:batch_size]] yield event_table[row_nums] filtered_table_rows = filtered_table_rows[batch_size:] datastore_reader.close() if __name__ == '__main__': for event_details_batch in getEventDetails(SESSION_PATH, FRAME_EVENT_FILE, FILTERS, BATCH_SIZE): for event in event_details_batch: print EventConstants.getName(event['type']), event['time'], event['filter_id']