Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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:
            print2err("Error saving event: ", event)
            printExceptionDetailsToStdErr()
Example #4
0
    def getEventTable(self, event_type):
        """
        Returns the DataStore table that contains events of the specified type. 
        
        **Docstr TBC.**
        """
        if self.hdfFile:
            klassTables = self.hdfFile.root.class_table_mapping
            deviceEventTable = None
            event_column = None
            event_value = None

            if isinstance(event_type, basestring):
                if event_type.find('Event') >= 0:
                    event_column = 'class_name'
                    event_value = event_type
                else:
                    event_value = ''
                    tokens = event_type.split('_')
                    for t in tokens:
                        event_value += t[0].upper() + t[1:].lower()
                    event_value = event_type + 'Event'
                event_value = '"%s"' % (event_value)
            elif isinstance(event_type, (int, long)):
                event_column = 'class_id'
                event_value = event_type
            else:
                iohub.print2err(
                    "getEventTable error: event_type arguement must be a string or and int"
                )
                return None

            result = [
                row.fetch_all_fields() for row in klassTables.where(
                    '({0} == {1}) & (class_type_id == 1)'.format(
                        event_column, event_value))
            ]
            if len(result) != 1:
                iohub.print2err(
                    "event_type_id passed to getEventAttribute can only return one row from CLASS_MAPPINGS: ",
                    len(result))
                return None

            tablePathString = result[0][3]
            return self.hdfFile.getNode(tablePathString)
        return None
Example #5
0
      def addClassMappings(cls,device_class,device_event_ids,event_classes):
          if cls._classes is None:
              cls._classes={}
  
          #import iohub
          #iohub.print2err("Adding Device Event Mappings for device: ",device_class.__name__)
 
          for event_id in device_event_ids:
              event_constant_string=cls.getName(event_id)
              import iohub
              event_class=None
              for event_class in event_classes.values():
                  if event_class.EVENT_TYPE_ID == event_id:
                      cls._classes[event_id]=event_class
                      cls._classes[event_class]=event_id
                      #iohub.print2err("\tAdding Event Class Mapping: ",event_constant_string, " = ",event_id)
                      break
              
              if event_id not in cls._classes.keys():
                      iohub.print2err("\t*** ERROR ADDING EVENT CLASSS MAPPING: Could not find class: ",event_constant_string, " = ",event_id)
Example #6
0
 def getEventTable(self,event_type):
     """
     Returns the DataStore table that contains events of the specified type. 
     
     **Docstr TBC.**
     """
     if self.hdfFile:
         klassTables=self.hdfFile.root.class_table_mapping
         deviceEventTable=None
         event_column=None
         event_value=None
         
         if isinstance(event_type,basestring):
             if event_type.find('Event')>=0:
                 event_column='class_name'
                 event_value=event_type
             else:
                 event_value=''
                 tokens=event_type.split('_')
                 for t in tokens:
                     event_value+=t[0].upper()+t[1:].lower()
                 event_value=event_type+'Event'         
             event_value='"%s"'%(event_value)
         elif isinstance(event_type,(int,long)):
             event_column='class_id'
             event_value=event_type
         else:
             iohub.print2err("getEventTable error: event_type arguement must be a string or and int")
             return None
         
         result=[row.fetch_all_fields() for row in klassTables.where('({0} == {1}) & (class_type_id == 1)'.format(event_column,event_value))]
         if len(result)!= 1:
             iohub.print2err("event_type_id passed to getEventAttribute can only return one row from CLASS_MAPPINGS: ",len(result))
             return None
             
         tablePathString=result[0][3]
         return self.hdfFile.getNode(tablePathString)
     return None
Example #7
0
        def addClassMappings(cls, device_class, device_event_ids,
                             event_classes):
            if cls._classes is None:
                cls._classes = {}

            #import iohub
            #iohub.print2err("Adding Device Event Mappings for device: ",device_class.__name__)

            for event_id in device_event_ids:
                event_constant_string = cls.getName(event_id)
                import iohub
                event_class = None
                for event_class in event_classes.values():
                    if event_class.EVENT_TYPE_ID == event_id:
                        cls._classes[event_id] = event_class
                        cls._classes[event_class] = event_id
                        #iohub.print2err("\tAdding Event Class Mapping: ",event_constant_string, " = ",event_id)
                        break

                if event_id not in cls._classes.keys():
                    iohub.print2err(
                        "\t*** ERROR ADDING EVENT CLASSS MAPPING: Could not find class: ",
                        event_constant_string, " = ", event_id)
Example #8
0
    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:
            print2err("Error saving event: ",event)
            printExceptionDetailsToStdErr()
Example #9
0
class ioHubpyTablesFile():
    def __init__(self, fileName, folderPath, fmode='a', ioHubsettings=None):
        self.fileName = fileName
        self.folderPath = folderPath
        self.filePath = os.path.join(folderPath, fileName)

        self.settings = ioHubsettings

        self.active_experiment_id = None
        self.active_session_id = None

        self.flushCounter = self.settings.get('flush_interval', 32)
        self._eventCounter = 0

        self.TABLES = dict()
        self._eventGroupMappings = dict()
        self.emrtFile = openFile(self.filePath, mode=fmode)

        atexit.register(close_open_data_files, False)

        if len(self.emrtFile.title) == 0:
            self.buildOutTemplate()
            self.flush()
        else:
            self.loadTableMappings()

    def updateDataStoreStructure(self, device_instance, event_class_dict):
        dfilter = Filters(complevel=0,
                          complib='zlib',
                          shuffle=False,
                          fletcher32=False)

        def eventTableLabel2ClassName(event_table_label):
            tokens = str(event_table_label[0] + event_table_label[1:].lower() +
                         'Event').split('_')
            return ''.join([t[0].upper() + t[1:] for t in tokens])

        for event_cls_name, event_cls in event_class_dict.iteritems():
            if event_cls.IOHUB_DATA_TABLE:
                event_table_label = event_cls.IOHUB_DATA_TABLE
                if event_table_label not in self.TABLES:
                    self.TABLES[event_table_label] = self.emrtFile.createTable(
                        self._eventGroupMappings[event_table_label],
                        eventTableLabel2ClassName(event_table_label),
                        event_cls.NUMPY_DTYPE,
                        title="%s %s Data" %
                        (device_instance.__class__.__name__,
                         eventTableLabel2ClassName(event_table_label)),
                        filters=dfilter.copy())
                    self.flush()

                self.addClassMapping(event_cls, self.TABLES[event_table_label])

    def loadTableMappings(self):
        # create meta-data tables

        self._buildEventGroupMappingDict()

        self.TABLES[
            'EXPERIMENT_METADETA'] = self.emrtFile.root.data_collection.experiment_meta_data
        self.TABLES[
            'SESSION_METADETA'] = self.emrtFile.root.data_collection.session_meta_data
        self.TABLES[
            'CLASS_TABLE_MAPPINGS'] = self.emrtFile.root.class_table_mapping

        # create tables dict of hdf5 path mappings

        try:
            self.TABLES[
                'KEYBOARD_KEY'] = self.emrtFile.root.data_collection.events.keyboard.KeyboardKeyEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'KEYBOARD_CHAR'] = self.emrtFile.root.data_collection.events.keyboard.KeyboardCharEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'MOUSE_INPUT'] = self.emrtFile.root.data_collection.events.mouse.MouseInputEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'GAMEPAD_STATE_CHANGE'] = self.emrtFile.root.data_collection.events.gamepad.GamepadStateChangeEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'MESSAGE'] = self.emrtFile.root.data_collection.events.experiment.MessageEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'LOG'] = self.emrtFile.root.data_collection.events.experiment.LogEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'MULTI_CHANNEL_ANALOG_INPUT'] = self.emrtFile.root.data_collection.events.analog_input.MultiChannelAnalogInputEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'MONOCULAR_EYE_SAMPLE'] = self.emrtFile.root.data_collection.events.eyetracker.MonocularEyeSampleEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'BINOCULAR_EYE_SAMPLE'] = self.emrtFile.root.data_collection.events.eyetracker.BinocularEyeSampleEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'FIXATION_START'] = self.emrtFile.root.data_collection.events.eyetracker.FixationStartEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'FIXATION_END'] = self.emrtFile.root.data_collection.events.eyetracker.FixationEndEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'SACCADE_START'] = self.emrtFile.root.data_collection.events.eyetracker.SaccadeStartEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'SACCADE_END'] = self.emrtFile.root.data_collection.events.eyetracker.SaccadeEndEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'BLINK_START'] = self.emrtFile.root.data_collection.events.eyetracker.BlinkStartEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

        try:
            self.TABLES[
                'BLINK_END'] = self.emrtFile.root.data_collection.events.eyetracker.BlinkEndEvent
        except:
            # Just means the table for this event type has not been created as the event type is not being recorded
            pass

    def buildOutTemplate(self):
        self.emrtFile.title = DATA_FILE_TITLE
        self.emrtFile.FILE_VERSION = FILE_VERSION
        self.emrtFile.SCHEMA_DESIGNER = SCHEMA_AUTHORS
        self.emrtFile.SCHEMA_MODIFIED = SCHEMA_MODIFIED_DATE

        #CREATE GROUPS

        self.emrtFile.createGroup(
            self.emrtFile.root,
            'analysis',
            title=
            'Data Analysis Files, notebooks, scripts and saved results tables.'
        )
        self.emrtFile.createGroup(
            self.emrtFile.root,
            'data_collection',
            title='Data Collected from Experiment Sessions')
        self.flush()

        self.emrtFile.createGroup(
            self.emrtFile.root.data_collection,
            'events',
            title='Events that occurred and were saved during the experiments.'
        )

        self.emrtFile.createGroup(
            self.emrtFile.root.data_collection,
            'condition_variables',
            title=
            'Experiment DV and IVs used during and experiment session, or calculated and stored. In general, each row represents one trial of an experiment session.'
        )
        self.flush()

        self.TABLES['CLASS_TABLE_MAPPINGS'] = self.emrtFile.createTable(
            self.emrtFile.root,
            'class_table_mapping',
            ClassTableMappings,
            title='Mapping of ioObjects Classes to ioHub tables')

        self.TABLES['EXPERIMENT_METADETA'] = self.emrtFile.createTable(
            self.emrtFile.root.data_collection,
            'experiment_meta_data',
            ExperimentMetaData,
            title='Different Experiments Paradigms that have been run')
        self.TABLES['SESSION_METADETA'] = self.emrtFile.createTable(
            self.emrtFile.root.data_collection,
            'session_meta_data',
            SessionMetaData,
            title='Session run for the various experiments.')
        self.flush()

        self.emrtFile.createGroup(self.emrtFile.root.data_collection.events,
                                  'experiment',
                                  title='Experiment Generated Events')
        self.emrtFile.createGroup(self.emrtFile.root.data_collection.events,
                                  'keyboard',
                                  title='Keyboard Created Events')
        self.emrtFile.createGroup(self.emrtFile.root.data_collection.events,
                                  'mouse',
                                  title='Mouse Device Created Events')
        self.emrtFile.createGroup(self.emrtFile.root.data_collection.events,
                                  'gamepad',
                                  title='GamePad Created Events')
        self.emrtFile.createGroup(self.emrtFile.root.data_collection.events,
                                  'analog_input',
                                  title='AnalogInput Device Created Events')
        self.emrtFile.createGroup(self.emrtFile.root.data_collection.events,
                                  'eyetracker',
                                  title='Eye Tracker Generated Events')
        self.flush()

        self._buildEventGroupMappingDict()

    def _buildEventGroupMappingDict(self):
        self._eventGroupMappings[
            'KEYBOARD_KEY'] = self.emrtFile.root.data_collection.events.keyboard
        self._eventGroupMappings[
            'KEYBOARD_CHAR'] = self.emrtFile.root.data_collection.events.keyboard
        self._eventGroupMappings[
            'MOUSE_INPUT'] = self.emrtFile.root.data_collection.events.mouse
        self._eventGroupMappings[
            'GAMEPAD_STATE_CHANGE'] = self.emrtFile.root.data_collection.events.gamepad
        self._eventGroupMappings[
            'MULTI_CHANNEL_ANALOG_INPUT'] = self.emrtFile.root.data_collection.events.analog_input
        self._eventGroupMappings[
            'MESSAGE'] = self.emrtFile.root.data_collection.events.experiment
        self._eventGroupMappings[
            'LOG'] = self.emrtFile.root.data_collection.events.experiment
        self._eventGroupMappings[
            'MONOCULAR_EYE_SAMPLE'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'BINOCULAR_EYE_SAMPLE'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'FIXATION_START'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'FIXATION_END'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'SACCADE_START'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'SACCADE_END'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'BLINK_START'] = self.emrtFile.root.data_collection.events.eyetracker
        self._eventGroupMappings[
            'BLINK_END'] = self.emrtFile.root.data_collection.events.eyetracker

    def addClassMapping(self, ioClass, ctable):
        names = [
            x['class_id'] for x in self.TABLES['CLASS_TABLE_MAPPINGS'].where(
                "(class_id == %d)" % (ioClass.EVENT_TYPE_ID))
        ]
        if len(names) == 0:
            trow = self.TABLES['CLASS_TABLE_MAPPINGS'].row
            trow['class_id'] = ioClass.EVENT_TYPE_ID
            trow['class_type_id'] = 1  # Device or Event etc.
            trow['class_name'] = ioClass.__name__
            trow['table_path'] = ctable._v_pathname
            trow.append()
            self.flush()

    def createOrUpdateExperimentEntry(self, experimentInfoList):
        #ioHub.print2err("createOrUpdateExperimentEntry called with: ",experimentInfoList)
        experiment_metadata = self.TABLES['EXPERIMENT_METADETA']

        result = [
            row for row in experiment_metadata.iterrows()
            if row['code'] == experimentInfoList[1]
        ]
        if len(result) > 0:
            result = result[0]
            self.active_experiment_id = result['experiment_id']
            return self.active_experiment_id

        max_id = 0
        id_col = experiment_metadata.col('experiment_id')

        if len(id_col) > 0:
            max_id = N.amax(id_col)

        self.active_experiment_id = max_id + 1
        experimentInfoList[0] = self.active_experiment_id
        experiment_metadata.append([
            experimentInfoList,
        ])
        self.flush()
        #ioHub.print2err("Experiment ID set to: ",self.active_experiment_id)
        return self.active_experiment_id

    def createExperimentSessionEntry(self, sessionInfoDict):
        #ioHub.print2err("createExperimentSessionEntry called with: ",sessionInfoDict)
        session_metadata = self.TABLES['SESSION_METADETA']

        max_id = 0
        id_col = session_metadata.col('session_id')
        if len(id_col) > 0:
            max_id = N.amax(id_col)

        self.active_session_id = int(max_id + 1)

        values = (self.active_session_id, self.active_experiment_id,
                  sessionInfoDict['code'], sessionInfoDict['name'],
                  sessionInfoDict['comments'],
                  sessionInfoDict['user_variables'])
        session_metadata.append([
            values,
        ])
        self.flush()

        #ioHub.print2err("Session ID set to: ",self.active_session_id)
        return self.active_session_id

    def _initializeConditionVariableTable(self, experiment_id, np_dtype):
        experimentConditionVariableTable = None
        self._EXP_COND_DTYPE = N.dtype(np_dtype)
        try:
            expCondTableName = "EXP_CV_%d" % (experiment_id)
            experimentConditionVariableTable = self.emrtFile.root.data_collection.condition_variables._f_getChild(
                expCondTableName)
            self.TABLES['EXP_CV'] = experimentConditionVariableTable
        except NoSuchNodeError, nsne:
            try:
                experimentConditionVariableTable = self.emrtFile.createTable(
                    self.emrtFile.root.data_collection.condition_variables,
                    expCondTableName,
                    self._EXP_COND_DTYPE,
                    title='Condition Variables for Experiment id %d' %
                    (experiment_id))
                self.TABLES['EXP_CV'] = experimentConditionVariableTable
                self.emrtFile.flush()
            except:
                printExceptionDetailsToStdErr()
                return False
        except Exception:
            print2err(
                'Error getting experimentConditionVariableTable for experiment %d, table name: %s'
                % (experiment_id, expCondTableName))
            printExceptionDetailsToStdErr()
            return False