Exemplo n.º 1
0
    def query(self, watch_key, time_indices=None, slicing=None, mapping=None):
        """Query tensor store for a given watch_key.

        Args:
          watch_key: The watch key to query.
          time_indices: A numpy-style slicing string for time indices. E.g.,
            `-1`, `:-2`, `[::2]`. If not provided (`None`), will use -1.
          slicing: A numpy-style slicing string for individual time steps.
          mapping: An mapping string or a list of them. Supported mappings:
            `{None, 'image/png', 'health-pill'}`.

        Returns:
          The potentially sliced values as a nested list of values or its mapped
            format. A `list` of nested `list` of values.

        Raises:
          ValueError: If the shape of the sliced array is incompatible with mapping
            mode. Or if the mapping type is invalid.
        """
        if watch_key not in self._tensor_data:
            raise KeyError("watch_key not found: %s" % watch_key)

        if time_indices is None:
            time_indices = "-1"
        time_slicing = tensor_helper.parse_time_indices(time_indices)
        all_time_indices = list(range(self._tensor_data[watch_key].num_total()))
        sliced_time_indices = all_time_indices[time_slicing]
        if not isinstance(sliced_time_indices, list):
            sliced_time_indices = [sliced_time_indices]

        recombine_and_map = False
        step_mapping = mapping
        if len(sliced_time_indices) > 1 and mapping not in (None,):
            recombine_and_map = True
            step_mapping = None

        output = []
        for index in sliced_time_indices:
            value = self._tensor_data[watch_key].query(index)[0]
            if value is not None and not isinstance(
                value, debug_data.InconvertibleTensorProto
            ):
                output.append(
                    tensor_helper.array_view(
                        value, slicing=slicing, mapping=step_mapping
                    )[2]
                )
            else:
                output.append(None)

        if recombine_and_map:
            if mapping == "image/png":
                output = tensor_helper.array_to_base64_png(output)
            elif mapping and mapping != "none":
                logger.warn(
                    "Unsupported mapping mode after recomining time steps: %s",
                    mapping,
                )
        return output
Exemplo n.º 2
0
 def testConvertHealthy2DNestedList(self):
   x = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]
   encoded_x = tensor_helper.array_to_base64_png(x)
   decoded_x = im_util.decode_png(base64.b64decode(encoded_x))
   self.assertEqual((4, 4, 3), decoded_x.shape)
   decoded_flat = decoded_x.flatten()
   self.assertEqual(0, np.min(decoded_flat))
   self.assertEqual(255, np.max(decoded_flat))
Exemplo n.º 3
0
 def testConvertHealthy2DArray(self):
   x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
   encoded_x = tensor_helper.array_to_base64_png(x)
   decoded_x = im_util.decode_png(base64.b64decode(encoded_x))
   self.assertEqual((3, 3, 3), decoded_x.shape)
   decoded_flat = decoded_x.flatten()
   self.assertEqual(0, np.min(decoded_flat))
   self.assertEqual(255, np.max(decoded_flat))
Exemplo n.º 4
0
 def testConvertHealthy2DNestedList(self):
     x = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]
     encoded_x = tensor_helper.array_to_base64_png(x)
     decoded_x = im_util.decode_png(base64.b64decode(encoded_x))
     self.assertEqual((4, 4, 3), decoded_x.shape)
     decoded_flat = decoded_x.flatten()
     self.assertEqual(0, np.min(decoded_flat))
     self.assertEqual(255, np.max(decoded_flat))
Exemplo n.º 5
0
 def testConvertHealthy2DArray(self):
     x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
     encoded_x = tensor_helper.array_to_base64_png(x)
     decoded_x = im_util.decode_png(base64.b64decode(encoded_x))
     self.assertEqual((3, 3, 3), decoded_x.shape)
     decoded_flat = decoded_x.flatten()
     self.assertEqual(0, np.min(decoded_flat))
     self.assertEqual(255, np.max(decoded_flat))
Exemplo n.º 6
0
  def query(self,
            watch_key,
            time_indices=None,
            slicing=None,
            mapping=None):
    """Query tensor store for a given watch_key.

    Args:
      watch_key: The watch key to query.
      time_indices: A numpy-style slicing string for time indices. E.g.,
        `-1`, `:-2`, `[::2]`. If not provided (`None`), will use -1.
      slicing: A numpy-style slicing string for individual time steps.
      mapping: An mapping string or a list of them. Supported mappings:
        `{None, 'image/png', 'health-pill'}`.

    Returns:
      The potentially sliced values as a nested list of values or its mapped
        format. A `list` of nested `list` of values.

    Raises:
      ValueError: If the shape of the sliced array is incompatible with mapping
        mode. Or if the mapping type is invalid.
    """
    if watch_key not in self._tensor_data:
      raise KeyError("watch_key not found: %s" % watch_key)

    if time_indices is None:
      time_indices = '-1'
    time_slicing = tensor_helper.parse_time_indices(time_indices)
    all_time_indices = list(range(self._tensor_data[watch_key].num_total()))
    sliced_time_indices = all_time_indices[time_slicing]
    if not isinstance(sliced_time_indices, list):
      sliced_time_indices = [sliced_time_indices]

    recombine_and_map = False
    step_mapping = mapping
    if len(sliced_time_indices) > 1 and mapping not in (None, ):
      recombine_and_map = True
      step_mapping = None

    output = []
    for index in sliced_time_indices:
      value = self._tensor_data[watch_key].query(index)[0]
      if (value is not None and
          not isinstance(value, debug_data.InconvertibleTensorProto)):
        output.append(tensor_helper.array_view(
            value, slicing=slicing, mapping=step_mapping)[2])
      else:
        output.append(None)

    if recombine_and_map:
      if mapping == 'image/png':
        output = tensor_helper.array_to_base64_png(output)
      elif mapping and mapping != 'none':
        tf.logging.warn(
            'Unsupported mapping mode after recomining time steps: %s',
            mapping)
    return output
Exemplo n.º 7
0
 def testImagePngMappingRaisesExceptionForNonRank2Array(self):
   x = np.ones([2, 2, 2])
   with six.assertRaisesRegex(
       self, ValueError, r"Expected rank-2 array; received rank-3 array"):
     tensor_helper.array_to_base64_png(x)
Exemplo n.º 8
0
 def testImagePngMappingRaisesExceptionForNonRank2Array(self):
     x = np.ones([2, 2, 2])
     with six.assertRaisesRegex(
             self, ValueError,
             r"Expected rank-2 array; received rank-3 array"):
         tensor_helper.array_to_base64_png(x)