Пример #1
0
    def test_get_categorical_features_to_sampling(self):
        cat_example = tf.train.Example()
        cat_example.features.feature['non_numeric'].bytes_list.value.extend(
            [b'cat'])

        cow_example = tf.train.Example()
        cow_example.features.feature['non_numeric'].bytes_list.value.extend(
            [b'cow'])

        pony_example = tf.train.Example()
        pony_example.features.feature['non_numeric'].bytes_list.value.extend(
            [b'pony'])

        examples = [cat_example] * 4 + [cow_example] * 5 + [pony_example] * 10

        # If we stop sampling at the first 3 examples, the only example should be
        # cat example.
        data = inference_utils.get_categorical_features_to_sampling(
            examples[0:3], top_k=1)
        self.assertDictEqual({'non_numeric': {'samples': [b'cat']}}, data)

        # If we sample more examples, the top 2 examples should be cow and pony.
        data = inference_utils.get_categorical_features_to_sampling(
            examples[0:20], top_k=2)
        self.assertDictEqual({'non_numeric': {
            'samples': [b'pony', b'cow']
        }}, data)
Пример #2
0
    def _eligible_features_from_example_handler(self, request):
        """Returns a list of JSON objects for each feature in the example.

    Args:
      request: A request for features.

    Returns:
      A list with a JSON object for each feature.
      Numeric features are represented as {name: observedMin: observedMax:}.
      Categorical features are repesented as {name: samples:[]}.
    """
        features_dict = (
            inference_utils.get_numeric_features_to_observed_range(
                self.examples[0:NUM_EXAMPLES_TO_SCAN]))

        features_dict.update(
            inference_utils.get_categorical_features_to_sampling(
                self.examples[0:NUM_EXAMPLES_TO_SCAN], NUM_MUTANTS))

        # Massage the features_dict into a sorted list before returning because
        # Polymer dom-repeat needs a list.
        features_list = []
        for k, v in sorted(features_dict.items()):
            v['name'] = k
            features_list.append(v)

        return http_util.Respond(request, features_list, 'application/json')
  def _eligible_features_from_example_handler(self, request):
    """Returns a list of JSON objects for each feature in the example.

    Args:
      request: A request for features.

    Returns:
      A list with a JSON object for each feature.
      Numeric features are represented as {name: observedMin: observedMax:}.
      Categorical features are repesented as {name: samples:[]}.
    """
    features_dict = (
        inference_utils.get_numeric_features_to_observed_range(
            self.examples[0: NUM_EXAMPLES_TO_SCAN]))

    features_dict.update(
        inference_utils.get_categorical_features_to_sampling(
            self.examples[0: NUM_EXAMPLES_TO_SCAN], NUM_MUTANTS))

    # Massage the features_dict into a sorted list before returning because
    # Polymer dom-repeat needs a list.
    features_list = []
    for k, v in sorted(features_dict.items()):
      v['name'] = k
      features_list.append(v)

    return http_util.Respond(request, features_list, 'application/json')