예제 #1
0
    def get_event_data(self,
                       channels,
                       events,
                       start_time,
                       end_time,
                       buffer_time=0.0,
                       resampled_rate=None,
                       filt_freq=None,
                       filt_type='stop',
                       filt_order=4,
                       keep_buffer=False,
                       loop_axis=None,
                       num_mp_procs=0,
                       eoffset='eoffset',
                       eoffset_in_time=True):
        """
        Return an TimeSeries containing data for the specified channel
        in the form [events,duration].

        Parameters
        ----------
        channels: {int} or {dict}
            Channels from which to load data.
        events: {array_like} or {recarray}
            Array/list of event offsets (in time or samples as
            specified by eoffset_in_time; in time by default) into
            the data, specifying each event onset time.
        start_time: {float}
            Start of epoch to retrieve (in time-unit of the data).
        end_time: {float}
            End of epoch to retrieve (in time-unit of the data).
        buffer_time: {float},optional
            Extra buffer to add on either side of the event in order
            to avoid edge effects when filtering (in time unit of the
            data).
        resampled_rate: {float},optional
            New samplerate to resample the data to after loading.
        filt_freq: {array_like},optional
            The range of frequencies to filter (depends on the filter
            type.)
        filt_type = {scipy.signal.band_dict.keys()},optional
            Filter type.
        filt_order = {int},optional
            The order of the filter.
        keep_buffer: {boolean},optional
            Whether to keep the buffer when returning the data.
        eoffset_in_time: {boolean},optional        
            If True, the unit of the event offsets is taken to be
            time (unit of the data), otherwise samples.
        """

        # translate back to dur and offset
        dur = end_time - start_time
        offset = start_time
        buf = buffer_time

        # get the event offsets
        if ((not (hasattr(events, 'dtype') or hasattr(events, 'columns')))
                or (hasattr(events, 'dtype') and events.dtype.names is None)):
            # they just passed in a list
            event_offsets = events
        elif ((hasattr(events, 'dtype') and (eoffset in events.dtype.names))
              or (hasattr(events, 'columns') and (eoffset in events.columns))):
            event_offsets = events[eoffset]
        else:
            raise ValueError(eoffset + ' must be a valid fieldname ' +
                             'specifying the offset for the data.')

        # Sanity checks:
        if (dur < 0):
            raise ValueError('Duration must not be negative! ' +
                             'Specified duration: ' + str(dur))
        if (np.min(event_offsets) < 0):
            raise ValueError('Event offsets must not be negative!')

        # make sure the events are an actual array:
        event_offsets = np.asarray(event_offsets)
        if eoffset_in_time:
            # convert to samples
            event_offsets = np.atleast_1d(
                np.int64(np.round(event_offsets * self.samplerate)))

        # set event durations from rate
        # get the samplesize
        samplesize = 1. / self.samplerate

        # get the number of buffer samples
        buf_samp = int(np.ceil(buf / samplesize))

        # calculate the offset samples that contains the desired offset
        offset_samp = int(
            np.ceil((np.abs(offset) - samplesize * .5) / samplesize) *
            np.sign(offset))

        # finally get the duration necessary to cover the desired span
        #dur_samp = int(np.ceil((dur - samplesize*.5)/samplesize))
        dur_samp = (int(np.ceil(
            (dur + offset - samplesize * .5) / samplesize)) - offset_samp + 1)

        # add in the buffer
        dur_samp += 2 * buf_samp
        offset_samp -= buf_samp

        # check that we have all the data we need before every event:
        if (np.min(event_offsets + offset_samp) < 0):
            bad_evs = ((event_offsets + offset_samp) < 0)
            raise ValueError('The specified values for offset and buffer ' +
                             'require more data than is available before ' +
                             str(np.sum(bad_evs)) + ' of all ' +
                             str(len(bad_evs)) + ' events.')

        # process the channels
        if isinstance(channels, dict):
            # turn into indices
            ch_info = self.channels
            key = channels.keys()[0]
            channels = [
                np.nonzero(ch_info[key] == c)[0][0] for c in channels[key]
            ]
        elif isinstance(channels, str):
            # find that channel by name
            channels = np.nonzero(self.channels['name'] == channels)[0][0]
        if channels is None or len(np.atleast_1d(channels)) == 0:
            channels = np.arange(self.nchannels)
        channels = np.atleast_1d(channels)
        channels.sort()

        # load the timeseries (this must be implemented by subclasses)
        eventdata = self._load_data(channels, event_offsets, dur_samp,
                                    offset_samp)

        # calc the time range
        # get the samplesize
        samp_start = offset_samp * samplesize
        samp_end = samp_start + (dur_samp - 1) * samplesize
        time_range = np.linspace(samp_start, samp_end, dur_samp)

        # make it a timeseries
        dims = [
            Dim(self.channels[channels],
                'channels'),  # can index into channels
            Dim(events, 'events'),
            Dim(time_range, 'time')
        ]
        eventdata = TimeSeries(np.asarray(eventdata),
                               'time',
                               self.samplerate,
                               dims=dims)

        # filter if desired
        if not (filt_freq is None):
            # filter that data
            eventdata = eventdata.filtered(filt_freq,
                                           filt_type=filt_type,
                                           order=filt_order)

# resample if desired
        if (not (resampled_rate is None)
                and not (resampled_rate == eventdata.samplerate)):
            # resample the data
            eventdata = eventdata.resampled(resampled_rate,
                                            loop_axis=loop_axis,
                                            num_mp_procs=num_mp_procs)

        # remove the buffer and set the time range
        if buf > 0 and not (keep_buffer):
            # remove the buffer
            eventdata = eventdata.remove_buffer(buf)

        # return the timeseries
        return eventdata
예제 #2
0
파일: basewrapper.py 프로젝트: ctw/ptsa
    def get_event_data(self,channels,events,
                       start_time,end_time,buffer_time=0.0,
                       resampled_rate=None,
                       filt_freq=None,filt_type='stop',filt_order=4,
                       keep_buffer=False,
                       loop_axis=None,num_mp_procs=0,eoffset='eoffset',
                       eoffset_in_time=True):
        """
        Return an TimeSeries containing data for the specified channel
        in the form [events,duration].

        Parameters
        ----------
        channels: {int} or {dict}
            Channels from which to load data.
        events: {array_like} or {recarray}
            Array/list of event offsets (in time or samples as
            specified by eoffset_in_time; in time by default) into
            the data, specifying each event onset time.
        start_time: {float}
            Start of epoch to retrieve (in time-unit of the data).
        end_time: {float}
            End of epoch to retrieve (in time-unit of the data).
        buffer_time: {float},optional
            Extra buffer to add on either side of the event in order
            to avoid edge effects when filtering (in time unit of the
            data).
        resampled_rate: {float},optional
            New samplerate to resample the data to after loading.
        filt_freq: {array_like},optional
            The range of frequencies to filter (depends on the filter
            type.)
        filt_type = {scipy.signal.band_dict.keys()},optional
            Filter type.
        filt_order = {int},optional
            The order of the filter.
        keep_buffer: {boolean},optional
            Whether to keep the buffer when returning the data.
        eoffset_in_time: {boolean},optional        
            If True, the unit of the event offsets is taken to be
            time (unit of the data), otherwise samples.
        """

        # translate back to dur and offset
        dur = end_time - start_time
        offset = start_time
        buf = buffer_time

        # get the event offsets
        if ((not (hasattr(events,'dtype') or hasattr(events,'columns'))) or
            (hasattr(events,'dtype') and events.dtype.names is None)):
            # they just passed in a list
            event_offsets = events
        elif ((hasattr(events, 'dtype') and (eoffset in events.dtype.names)) or
              (hasattr(events, 'columns') and (eoffset in events.columns))):
            event_offsets = events[eoffset]
        else:
            raise ValueError(eoffset+' must be a valid fieldname '+
                             'specifying the offset for the data.')
        
        # Sanity checks:
        if(dur<0):
            raise ValueError('Duration must not be negative! '+
                             'Specified duration: '+str(dur))
        if(np.min(event_offsets)<0):
            raise ValueError('Event offsets must not be negative!')

        # make sure the events are an actual array:
        event_offsets = np.asarray(event_offsets)
        if eoffset_in_time:
            # convert to samples
            event_offsets = np.atleast_1d(np.int64(
                np.round(event_offsets*self.samplerate)))
        
        # set event durations from rate
        # get the samplesize
        samplesize = 1./self.samplerate

        # get the number of buffer samples
        buf_samp = int(np.ceil(buf/samplesize))

        # calculate the offset samples that contains the desired offset
        offset_samp = int(np.ceil((np.abs(offset)-samplesize*.5)/samplesize)*
                          np.sign(offset))

        # finally get the duration necessary to cover the desired span
        #dur_samp = int(np.ceil((dur - samplesize*.5)/samplesize))
        dur_samp = (int(np.ceil((dur+offset - samplesize*.5)/samplesize)) -
                    offset_samp + 1)
        
        # add in the buffer
        dur_samp += 2*buf_samp
        offset_samp -= buf_samp

        # check that we have all the data we need before every event:
        if(np.min(event_offsets+offset_samp)<0):
            bad_evs = ((event_offsets+offset_samp)<0)
            raise ValueError('The specified values for offset and buffer '+
                             'require more data than is available before '+
                             str(np.sum(bad_evs))+' of all '+
                             str(len(bad_evs))+' events.')

        # process the channels
        if isinstance(channels, dict):
            # turn into indices
            ch_info = self.channels
            key = channels.keys()[0]
            channels = [np.nonzero(ch_info[key]==c)[0][0] for c in channels[key]]
        elif isinstance(channels, str):
            # find that channel by name
            channels = np.nonzero(self.channels['name']==channels)[0][0]
        if channels is None or len(np.atleast_1d(channels))==0:
            channels = np.arange(self.nchannels)
        channels = np.atleast_1d(channels)
        channels.sort()

        # load the timeseries (this must be implemented by subclasses)
        eventdata = self._load_data(channels,event_offsets,dur_samp,offset_samp)

        # calc the time range
        # get the samplesize
        samp_start = offset_samp*samplesize
        samp_end = samp_start + (dur_samp-1)*samplesize
        time_range = np.linspace(samp_start,samp_end,dur_samp)

        # make it a timeseries
        dims = [Dim(self.channels[channels],'channels'),  # can index into channels
                Dim(events,'events'),
                Dim(time_range,'time')]
        eventdata = TimeSeries(np.asarray(eventdata),
                               'time',
                               self.samplerate,dims=dims)

	# filter if desired
	if not(filt_freq is None):
	    # filter that data
            eventdata = eventdata.filtered(filt_freq,
                                           filt_type=filt_type,
                                           order=filt_order)

	# resample if desired
	if (not(resampled_rate is None) and
            not(resampled_rate == eventdata.samplerate)):
	    # resample the data
            eventdata = eventdata.resampled(resampled_rate,
                                            loop_axis=loop_axis,
                                            num_mp_procs=num_mp_procs)

        # remove the buffer and set the time range
	if buf > 0 and not(keep_buffer):
	    # remove the buffer
            eventdata = eventdata.remove_buffer(buf)

        # return the timeseries
        return eventdata
예제 #3
0
파일: basewrapper.py 프로젝트: Jeff33/ptsa
    def get_event_data(self,channels,event_offsets,
                       start_time,end_time,buffer_time=0.0,
                       resampled_rate=None,
                       filt_freq=None,filt_type='stop',filt_order=4,
                       keep_buffer=False):
        """
        Return an TimeSeries containing data for the specified channel
        in the form [events,duration].

        Parameters
        ----------
        channels: {int}
            Channels from which to load data.
        event_offsets: {array_like}
            Array/list of event offsets (in samples) into the data,
            specifying each event onset time.
        start_time: {float}
            Start of epoch to retrieve (in time-unit of the data).
        end_time: {float}
            End of epoch to retrieve (in time-unit of the data).
        buffer_time: {float},optional
            Extra buffer to add on either side of the event in order
            to avoid edge effects when filtering (in time unit of the
            data).
        resampled_rate: {float},optional
            New samplerate to resample the data to after loading.
        filt_freq: {array_like},optional
            The range of frequencies to filter (depends on the filter
            type.)
        filt_type = {scipy.signal.band_dict.keys()},optional
            Filter type.
        filt_order = {int},optional
            The order of the filter.
        keep_buffer: {boolean},optional
            Whether to keep the buffer when returning the data.
        """

        # translate back to dur and offset
        dur = end_time - start_time
        offset = start_time
        buf = buffer_time
        
        # Sanity checks:
        if(dur<0):
            raise ValueError('Duration must not be negative! '+
                             'Specified duration: '+str(dur))
        if(np.min(event_offsets)<0):
            raise ValueError('Event offsets must not be negative!')

        # make sure the events are an actual array
        event_offsets = np.asarray(event_offsets)
        
        # set event durations from rate
        # get the samplesize
        samplesize = 1./self.samplerate

        # get the number of buffer samples
        buf_samp = int(np.ceil(buf/samplesize))

        # calculate the offset samples that contains the desired offset
        offset_samp = int(np.ceil((np.abs(offset)-samplesize*.5)/samplesize)*
                          np.sign(offset))

        # finally get the duration necessary to cover the desired span
        #dur_samp = int(np.ceil((dur - samplesize*.5)/samplesize))
        dur_samp = (int(np.ceil((dur+offset - samplesize*.5)/samplesize)) -
                    offset_samp + 1)
        
        # add in the buffer
        dur_samp += 2*buf_samp
        offset_samp -= buf_samp

        # check that we have all the data we need before every event:
        if(np.min(event_offsets+offset_samp)<0):
            bad_evs = ((event_offsets+offset_samp)<0)
            raise ValueError('The specified values for offset and buffer '+
                             'require more data than is available before '+
                             str(np.sum(bad_evs))+' of all '+
                             str(len(bad_evs))+' events.')

        # process the channels
        if channels is None or len(np.atleast_1d(channels))==0:
            channels = np.arange(self.nchannels)
        channels = np.atleast_1d(channels)

        # load the timeseries (this must be implemented by subclasses)
        eventdata = self._load_data(channels,event_offsets,dur_samp,offset_samp)

        # calc the time range
        # get the samplesize
        samp_start = offset_samp*samplesize
        samp_end = samp_start + (dur_samp-1)*samplesize
        time_range = np.linspace(samp_start,samp_end,dur_samp)

	# make it a timeseries
        dims = [Dim(channels,'channels'),
                Dim(event_offsets,'event_offsets'),
                Dim(time_range,'time')]
        eventdata = TimeSeries(np.asarray(eventdata),
                               'time',
                               self.samplerate,dims=dims)

	# filter if desired
	if not(filt_freq is None):
	    # filter that data
            eventdata = eventdata.filtered(filt_freq,
                                           filt_type=filt_type,
                                           order=filt_order)

	# resample if desired
	if (not(resampled_rate is None) and
            not(resampled_rate == eventdata.samplerate)):
	    # resample the data
            eventdata = eventdata.resampled(resampled_rate)

        # remove the buffer and set the time range
	if buf > 0 and not(keep_buffer):
	    # remove the buffer
            eventdata = eventdata.remove_buffer(buf)

        # return the timeseries
        return eventdata
예제 #4
0
    def get_event_data(self,
                       channels,
                       event_offsets,
                       start_time,
                       end_time,
                       buffer_time=0.0,
                       resampled_rate=None,
                       filt_freq=None,
                       filt_type='stop',
                       filt_order=4,
                       keep_buffer=False):
        """
        Return an TimeSeries containing data for the specified channel
        in the form [events,duration].

        Parameters
        ----------
        channels: {int}
            Channels from which to load data.
        event_offsets: {array_like}
            Array/list of event offsets (in samples) into the data,
            specifying each event onset time.
        start_time: {float}
            Start of epoch to retrieve (in time-unit of the data).
        end_time: {float}
            End of epoch to retrieve (in time-unit of the data).
        buffer_time: {float},optional
            Extra buffer to add on either side of the event in order
            to avoid edge effects when filtering (in time unit of the
            data).
        resampled_rate: {float},optional
            New samplerate to resample the data to after loading.
        filt_freq: {array_like},optional
            The range of frequencies to filter (depends on the filter
            type.)
        filt_type = {scipy.signal.band_dict.keys()},optional
            Filter type.
        filt_order = {int},optional
            The order of the filter.
        keep_buffer: {boolean},optional
            Whether to keep the buffer when returning the data.
        """

        # translate back to dur and offset
        dur = end_time - start_time
        offset = start_time
        buf = buffer_time

        # Sanity checks:
        if (dur < 0):
            raise ValueError('Duration must not be negative! ' +
                             'Specified duration: ' + str(dur))
        if (np.min(event_offsets) < 0):
            raise ValueError('Event offsets must not be negative!')

        # make sure the events are an actual array
        event_offsets = np.asarray(event_offsets)

        # set event durations from rate
        # get the samplesize
        samplesize = 1. / self.samplerate

        # get the number of buffer samples
        buf_samp = int(np.ceil(buf / samplesize))

        # calculate the offset samples that contains the desired offset
        offset_samp = int(
            np.ceil((np.abs(offset) - samplesize * .5) / samplesize) *
            np.sign(offset))

        # finally get the duration necessary to cover the desired span
        #dur_samp = int(np.ceil((dur - samplesize*.5)/samplesize))
        dur_samp = (int(np.ceil(
            (dur + offset - samplesize * .5) / samplesize)) - offset_samp + 1)

        # add in the buffer
        dur_samp += 2 * buf_samp
        offset_samp -= buf_samp

        # check that we have all the data we need before every event:
        if (np.min(event_offsets + offset_samp) < 0):
            bad_evs = ((event_offsets + offset_samp) < 0)
            raise ValueError('The specified values for offset and buffer ' +
                             'require more data than is available before ' +
                             str(np.sum(bad_evs)) + ' of all ' +
                             str(len(bad_evs)) + ' events.')

        # process the channels
        if channels is None or len(np.atleast_1d(channels)) == 0:
            channels = np.arange(self.nchannels)
        channels = np.atleast_1d(channels)

        # load the timeseries (this must be implemented by subclasses)
        eventdata = self._load_data(channels, event_offsets, dur_samp,
                                    offset_samp)

        # calc the time range
        # get the samplesize
        samp_start = offset_samp * samplesize
        samp_end = samp_start + (dur_samp - 1) * samplesize
        time_range = np.linspace(samp_start, samp_end, dur_samp)

        # make it a timeseries
        dims = [
            Dim(channels, 'channels'),
            Dim(event_offsets, 'event_offsets'),
            Dim(time_range, 'time')
        ]
        eventdata = TimeSeries(np.asarray(eventdata),
                               'time',
                               self.samplerate,
                               dims=dims)

        # filter if desired
        if not (filt_freq is None):
            # filter that data
            eventdata = eventdata.filtered(filt_freq,
                                           filt_type=filt_type,
                                           order=filt_order)

# resample if desired
        if (not (resampled_rate is None)
                and not (resampled_rate == eventdata.samplerate)):
            # resample the data
            eventdata = eventdata.resampled(resampled_rate)

        # remove the buffer and set the time range
        if buf > 0 and not (keep_buffer):
            # remove the buffer
            eventdata = eventdata.remove_buffer(buf)

        # return the timeseries
        return eventdata