Example #1
0
def CreateDataResponse(request):
  """Creates a DataResponse from a DataRequest.

  Args:
    request: A DataRequest instance received from client.
  Returns:
    A DataResponse instance and extra headers.
  """
  waveform_data_source = GetWaveformDataSource(request)
  pred_outputs = GetPredictionsOutputs(request)

  data_response = data_pb2.DataResponse()
  data_response.waveform_metadata.CopyFrom(waveform_data_service.GetMetadata(
      waveform_data_source, _MAX_SAMPLES_CLIENT))
  data_response.waveform_chunk.CopyFrom(waveform_data_service.GetChunk(
      waveform_data_source, request, _MAX_SAMPLES_CLIENT))

  if pred_outputs:
    waveforms_pred = prediction_data_service.PredictionDataService(
        pred_outputs, waveform_data_source, _MAX_SAMPLES_CLIENT)

    data_response.prediction_metadata.CopyFrom(waveforms_pred.GetMetadata())
    data_response.prediction_chunk.CopyFrom(waveforms_pred.GetChunk(request))

  # When only an SSTable file pattern is provided, the cache will return the TF
  # Example under the first iterated key.  Since the order of the keys is not
  # guaranteed, this response will not be cached as it is not idempotent.
  extra_headers = dict()
  no_cache = request.tf_ex_sstable_path and not request.sstable_key
  extra_headers['Cache-Control'] = 'no-cache' if no_cache else 'public'

  return data_response, extra_headers
Example #2
0
def CreateDataResponse(request):
    """Creates a DataResponse from a DataRequest.

  Args:
    request: A DataRequest instance received from client.
  Returns:
    A DataResponse instance.
  Raises:
    NotImplementedError: Try to load from SSTable or EDF
    IOError: No tf path provided
  """
    data_response = data_pb2.DataResponse()

    pred_outputs = None

    if request.tf_ex_sstable_path:
        raise NotImplementedError('Loading SSTables')

    elif request.edf_path:
        raise NotImplementedError('Loading EDF')

    elif request.tf_ex_file_path:
        tf_example = FetchTfExFromFile(request.tf_ex_file_path)
        waveform_data_source = TfExDataSourceConstructor(tf_example, '')

        if request.prediction_file_path:
            pred_outputs = FetchPredictionsFromFile(
                request.prediction_file_path)

    else:
        raise IOError('No path provided')

    data_response.waveform_metadata.CopyFrom(
        waveform_data_service.GetMetadata(waveform_data_source, _MAX_SAMPLES))
    data_response.waveform_chunk.CopyFrom(
        waveform_data_service.GetChunk(waveform_data_source, request,
                                       _MAX_SAMPLES))

    if pred_outputs:
        waveforms_pred = prediction_data_service.PredictionDataService(
            pred_outputs, waveform_data_source, _MAX_SAMPLES)

        data_response.prediction_metadata.CopyFrom(
            waveforms_pred.GetMetadata())
        data_response.prediction_chunk.CopyFrom(
            waveforms_pred.GetChunk(request))

    return data_response