forked from tensorflow/models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
106 lines (85 loc) · 3.65 KB
/
test_utils.py
File metadata and controls
106 lines (85 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Copyright 2018 The TensorFlow Authors All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utility methods for accessing and operating on test data."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from absl import flags
import tensorflow as tf
from google.protobuf import text_format
import input as seq2species_input
from protos import seq2label_pb2
FLAGS = flags.FLAGS
# Target names included in the example inputs.
TEST_TARGETS = ['test_target_1', 'test_target_2']
def _as_bytes_feature(in_string):
"""Converts the given string to a tf.train.BytesList feature.
Args:
in_string: string to be converted to BytesList Feature.
Returns:
The TF BytesList Feature representing the given string.
"""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[in_string]))
def create_tmp_train_file(num_examples,
read_len,
characters=seq2species_input.DNA_BASES,
name='test.tfrecord'):
"""Write a test TFRecord of input examples to temporary test directory.
The generated input examples are test tf.train.Example protos, each comprised
of a toy sequence of length read_len and non-meaningful labels for targets in
TEST_TARGETS.
Args:
num_examples: int; number of examples to write to test input file.
read_len: int; length of test read sequences.
characters: string; set of characters from which to construct test reads.
Defaults to canonical DNA bases.
name: string; filename for the test input file.
Returns:
Full path to the generated temporary test input file.
"""
tmp_path = os.path.join(FLAGS.test_tmpdir, name)
with tf.python_io.TFRecordWriter(tmp_path) as writer:
for i in xrange(num_examples):
char = characters[i % len(characters)]
features_dict = {'sequence': _as_bytes_feature(char * read_len)}
for target_name in TEST_TARGETS:
nonsense_label = _as_bytes_feature(str(i))
features_dict[target_name] = nonsense_label
tf_features = tf.train.Features(feature=features_dict)
example = tf.train.Example(features=tf_features)
writer.write(example.SerializeToString())
return tmp_path
def create_tmp_metadata(num_examples, read_len):
"""Write a test Seq2LabelDatasetInfo test proto to temporary test directory.
Args:
num_examples: int; number of example labels to write into test metadata.
read_len: int; length of test read sequences.
Returns:
Full path to the generated temporary test file containing the
Seq2LabelDatasetInfo text proto.
"""
dataset_info = seq2label_pb2.Seq2LabelDatasetInfo(
read_length=read_len,
num_examples=num_examples,
read_stride=1,
dataset_path='test.tfrecord')
for target in TEST_TARGETS:
dataset_info.labels.add(
name=target, values=[str(i) for i in xrange(num_examples)])
tmp_path = os.path.join(FLAGS.test_tmpdir, 'test.pbtxt')
with tf.gfile.GFile(tmp_path, 'w') as f:
f.write(text_format.MessageToString(dataset_info))
return tmp_path