def _examples_from_path_handler(self, request): """Returns JSON of the specified examples. Args: request: A request that should contain 'examples_path' and 'max_examples'. Returns: JSON of up to max_examples of the examples in the path. """ start_example = (int(request.args.get('start_example')) if request.args.get('start_example') else 0) if not start_example: examples_count = int(request.args.get('max_examples')) examples_path = request.args.get('examples_path') sampling_odds = float(request.args.get('sampling_odds')) self.example_class = (tf.train.SequenceExample if request.args.get('sequence_examples') == 'true' else tf.train.Example) try: platform_utils.throw_if_file_access_not_allowed( examples_path, self._logdir, self._wit_data_dir) example_strings = platform_utils.example_protos_from_path( examples_path, examples_count, parse_examples=False, sampling_odds=sampling_odds, example_class=self.example_class) self.examples = [ self.example_class.FromString(ex) for ex in example_strings ] self.generate_sprite(example_strings) self.updated_example_indices = set(range(len(self.examples))) except common_utils.InvalidUserInputError as e: logger.error('Data loading error: %s', e.message) return http_util.Respond(request, e.message, 'application/json', code=400) except Exception as e: return http_util.Respond(request, str(e), 'application/json', code=400) # Split examples from start_example to + max_examples # Send next start_example if necessary end_example = start_example + MAX_EXAMPLES_TO_SEND json_examples = [ json_format.MessageToJson(example) for example in self.examples[start_example:end_example] ] if end_example >= len(self.examples): end_example = -1 return http_util.Respond( request, { 'examples': json_examples, 'sprite': True if (self.sprite and not start_example) else False, 'next': end_example }, 'application/json')
def test_example_protos_from_path_get_all_in_file(self): cns_path = os.path.join(tf.compat.v1.test.get_temp_dir(), 'dummy_example') example = test_utils.make_fake_example() test_utils.write_out_examples([example], cns_path) dummy_examples = platform_utils.example_protos_from_path(cns_path) self.assertEqual(1, len(dummy_examples)) self.assertEqual(example, dummy_examples[0])
def test_example_protos_from_path_get_two(self): cns_path = os.path.join(tf.compat.v1.test.get_temp_dir(), 'dummy_example') example_one = test_utils.make_fake_example(1) example_two = test_utils.make_fake_example(2) example_three = test_utils.make_fake_example(3) test_utils.write_out_examples( [example_one, example_two, example_three], cns_path) dummy_examples = platform_utils.example_protos_from_path(cns_path, 2) self.assertEqual(2, len(dummy_examples)) self.assertEqual(example_one, dummy_examples[0]) self.assertEqual(example_two, dummy_examples[1])
def test_example_protos_from_path_use_wildcard(self): cns_path = os.path.join(tf.compat.v1.test.get_temp_dir(), 'wildcard_example1') example1 = test_utils.make_fake_example(1) test_utils.write_out_examples([example1], cns_path) cns_path = os.path.join(tf.compat.v1.test.get_temp_dir(), 'wildcard_example2') example2 = test_utils.make_fake_example(2) test_utils.write_out_examples([example2], cns_path) wildcard_path = os.path.join(tf.compat.v1.test.get_temp_dir(), 'wildcard_example*') dummy_examples = platform_utils.example_protos_from_path(wildcard_path) self.assertEqual(2, len(dummy_examples))
def _examples_from_path_handler(self, request): """Returns JSON of the specified examples. Args: request: A request that should contain 'examples_path' and 'max_examples'. Returns: JSON of up to max_examlpes of the examples in the path. """ examples_count = int(request.args.get('max_examples')) examples_path = request.args.get('examples_path') sampling_odds = float(request.args.get('sampling_odds')) self.example_class = (tf.train.SequenceExample if request.args.get('sequence_examples') == 'true' else tf.train.Example) try: platform_utils.throw_if_file_access_not_allowed( examples_path, self._logdir, self._wit_data_dir) example_strings = platform_utils.example_protos_from_path( examples_path, examples_count, parse_examples=False, sampling_odds=sampling_odds, example_class=self.example_class) self.examples = [ self.example_class.FromString(ex) for ex in example_strings ] self.generate_sprite(example_strings) json_examples = [ json_format.MessageToJson(example) for example in self.examples ] self.updated_example_indices = set(range(len(json_examples))) return http_util.Respond( request, { 'examples': json_examples, 'sprite': True if self.sprite else False }, 'application/json') except common_utils.InvalidUserInputError as e: logger.error('Data loading error: %s', e.message) return http_util.Respond(request, {'error': e.message}, 'application/json', code=400)
def test_example_proto_from_path_if_does_not_exist(self): cns_path = os.path.join(tf.compat.v1.test.get_temp_dir(), 'does_not_exist') with self.assertRaises(common_utils.InvalidUserInputError): platform_utils.example_protos_from_path(cns_path)