Beispiel #1
0
def _CreateChannelData(data_source, request, max_samples):
    """Returns a list of channel names and a dictionary of their values.

  Args:
    data_source: The DataSource for the waveform data.
    request: A DataRequest proto instance.
    max_samples: The maximum number of samples in one channel response.
  Returns:
    A dictionary of channel names mapped to the requested time slice of their
    data and an ordered list of the channel names.
  Raises:
    ValueError: Too many feature keys provided (only handles raw features or
    subtraction of two features).
  """
    channel_indices = _GetChannelIndicesInChannelDataIdList(
        request.channel_data_ids)
    single_channel_data = data_source.GetChannelData(
        channel_indices, request.chunk_start, request.chunk_duration_secs)

    subsampling = utils.GetSubsamplingRate(
        len(single_channel_data.values()[0]), max_samples)

    req_channel_data = {}
    channel_names = []
    for channel_data_id in request.channel_data_ids:
        if channel_data_id.HasField('bipolar_channel'):
            primary_index = channel_data_id.bipolar_channel.index
            primary_data = _FilterData(single_channel_data[str(primary_index)],
                                       primary_index, data_source, request)
            ref_index = channel_data_id.bipolar_channel.referential_index
            ref_data = _FilterData(single_channel_data[str(ref_index)],
                                   ref_index, data_source, request)
            channel_data = [
                reference - primary
                for (primary, reference) in zip(primary_data, ref_data)
            ]
            channel_name = '-'.join(
                data_source.GetChannelName(index)
                for index in [primary_index, ref_index])
        elif channel_data_id.HasField('single_channel'):
            index = channel_data_id.single_channel.index
            channel_data = _FilterData(single_channel_data[str(index)], index,
                                       data_source, request)
            channel_name = data_source.GetChannelName(index)
        else:
            raise ValueError('Unfamiliary channel type %s' % channel_data_id)
        req_channel_data[channel_name] = channel_data[::subsampling]
        channel_names.append(channel_name)

    return req_channel_data, channel_names
Beispiel #2
0
def _CreateChannelData(data_source,
                       channel_data_ids,
                       low_cut,
                       high_cut,
                       notch,
                       start=0,
                       duration=None,
                       max_samples=None):
    """Returns a list of channel names and a dictionary of their values.

  Args:
    data_source: The DataSource for the waveform data.
    channel_data_ids: ChannelDataIds list.
    low_cut: lower frequency to apply a band-pass filter.
    high_cut: higher frequency to apply a band-pass filter.
    notch: frequency to apply a notch filter.
    start: start time to crop the data, relative to the start of the file (in
      seconds). Defaults to the start of the file.
    duration: duration to crop from the data, in seconds. If None, will get the
      whole file data.
    max_samples: The maximum number of samples in one channel response.
      If None, there is no maximum limit.
  Returns:
    A dictionary of channel names mapped to the requested time slice of their
    data and an ordered list of the channel names.
  Raises:
    ValueError: Too many feature keys provided (only handles raw features or
    subtraction of two features).
  """
    if duration is None:
        duration = data_source.GetLength()

    channel_indices = _GetChannelIndicesInChannelDataIdList(channel_data_ids)
    single_channel_data = data_source.GetChannelData(channel_indices, start,
                                                     duration)

    subsampling = 1 if max_samples is None else utils.GetSubsamplingRate(
        len(list(single_channel_data.values())[0]), max_samples)

    def _GetFilteredData(index):
        """Wrapper to call _FilterData function.

    Args:
      index: the index for the selected channel.
    Returns:
      Filtered data for the selected channel.
    """
        return _FilterData(single_channel_data[str(index)], index, data_source,
                           low_cut, high_cut, notch)

    req_channel_data = {}
    channel_names = []
    for channel_data_id in channel_data_ids:
        if channel_data_id.HasField('bipolar_channel'):
            primary_index = channel_data_id.bipolar_channel.index
            primary_data = _GetFilteredData(primary_index)
            ref_index = channel_data_id.bipolar_channel.referential_index
            ref_data = _GetFilteredData(ref_index)
            channel_data = [
                reference - primary
                for (primary, reference) in zip(primary_data, ref_data)
            ]
            channel_name = '-'.join(
                data_source.GetChannelName(index)
                for index in [primary_index, ref_index])
        elif channel_data_id.HasField('single_channel'):
            index = channel_data_id.single_channel.index
            channel_data = _GetFilteredData(index)
            channel_name = data_source.GetChannelName(index)
        else:
            raise ValueError('Unfamiliary channel type %s' % channel_data_id)
        req_channel_data[channel_name] = channel_data[::subsampling]
        channel_names.append(channel_name)

    return req_channel_data, channel_names