/
csv_coder.py
315 lines (255 loc) · 10.4 KB
/
csv_coder.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Copyright 2017 Google Inc. 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.
"""Coder classes for encoding/decoding CSV into tf.Transform datasets."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import csv
# GOOGLE-INITIALIZATION
import numpy as np
import six
from six import moves
import tensorflow as tf
from tensorflow_transform.tf_metadata import schema_utils
from tensorflow.python.util import deprecation # pylint: disable=g-direct-tensorflow-import
# This is in agreement with Tensorflow conversions for Unicode values for both
# Python 2 and 3 (and also works for non-Unicode objects).
# TODO(b/123241312): Remove this fn since we will only support bytes input.
def _to_bytes(x):
"""Converts x to bytes."""
return tf.compat.as_bytes(x)
def _to_string(x):
"""Converts x to string.
This will return Unicode for Py3. This is needed as a pre-processing step
before calling csv reader/writer since it only supports Unicode for Py3.
Args:
x: The data to be converted.
Returns:
Unicode representation for Py3.
"""
return tf.compat.as_str_any(x)
def _elements_to_bytes(x):
if isinstance(x, (list, np.ndarray)):
return list(map(_to_bytes, x))
return _to_bytes(x)
# TODO(b/119621361): Consider harmonizing _make_cast_fn() for all coders.
def _make_cast_fn(dtype):
"""Return a function to extract the typed value from the feature.
For performance reasons it is preferred to have the cast fn
constructed once (for each handler).
Args:
dtype: The type of the Tensorflow feature.
Returns:
A function to extract the value field from a string depending on dtype.
"""
if dtype.is_integer:
# In Python 2, if the value is too large to fit into an int, int(..) returns
# a long, but ints are cheaper to use when possible.
return int
elif dtype.is_floating:
return float
else:
return _elements_to_bytes
class _FixedLenFeatureHandler(object):
"""Handler for `FixedLenFeature` values.
`FixedLenFeature` values will be parsed as a scalar or an array of the
corresponding dtype. In case the value is missing the default_value will
be returned. If the default value is not present a ValueError will be raised.
"""
def __init__(self, name, feature_spec, index, encoder=None):
self._name = name
self._cast_fn = _make_cast_fn(feature_spec.dtype)
self._default_value = feature_spec.default_value
self._index = index
self._encoder = encoder
self._np_dtype = feature_spec.dtype.as_numpy_dtype
self._shape = feature_spec.shape
self._rank = len(feature_spec.shape)
self._size = 1
for dim in feature_spec.shape:
self._size *= dim
@property
def name(self):
return self._name
def encode_value(self, string_list, values):
"""Encode the value of this feature into the CSV line."""
if self._rank == 0:
flattened_values = [values]
elif self._rank == 1:
# Short-circuit the reshaping logic needed for rank > 1.
flattened_values = values
else:
flattened_values = np.asarray(values, dtype=self._np_dtype).reshape(-1)
if len(flattened_values) != self._size:
raise ValueError(
'FixedLenFeature "{}" got wrong number of values. Expected {} but '
'got {}'.format(self._name, self._size, len(flattened_values)))
if self._encoder:
string_list[self._index] = self._encoder.encode_record(flattened_values)
else:
string_list[self._index] = _to_string(flattened_values[0])
class _VarLenFeatureHandler(object):
"""Handler for `VarLenFeature` values.
`VarLenFeature` values will be parsed as an array of values of the
corresponding dtype. In case the value is missing an empty array
will be returned.
"""
def __init__(self, name, dtype, index, encoder=None):
self._name = name
self._cast_fn = _make_cast_fn(dtype)
self._np_dtype = dtype.as_numpy_dtype
self._index = index
self._encoder = encoder
@property
def name(self):
return self._name
def encode_value(self, string_list, values):
"""Encode the value of this feature into the CSV line."""
if self._encoder:
string_list[self._index] = self._encoder.encode_record(values)
else:
string_list[self._index] = _to_string(values[0]) if values else ''
class EncodeError(Exception):
"""Base encode error."""
pass
_DECODE_DEPRECATION_MESSAGE = 'TFXIO should be used to decode CSV. '
'For a reference, take a look at the get_started.md guide for details.'
class CsvCoder(object):
"""A coder to encode CSV formatted data."""
class _WriterWrapper(object):
"""A wrapper for csv.writer to make it picklable."""
def __init__(self, delimiter):
"""Initializes the writer wrapper.
Args:
delimiter: A one-character string used to separate fields.
"""
self._state = (delimiter)
self._buffer = moves.cStringIO()
# Since we use self._writer to encode individual rows, we set
# lineterminator='' so that self._writer doesn't add a newline.
self._writer = csv.writer(
self._buffer, lineterminator='', delimiter=delimiter)
def encode_record(self, record):
"""Converts the record to bytes.
Since csv writer only supports Unicode for PY3, we need to convert them
conditionally before calling csv writer. We always return result in bytes
format to be consistent with current behavior.
Args:
record: The data to be converted.
Returns:
Bytes representation input.
"""
self._writer.writerow([_to_string(x) for x in record])
result = tf.compat.as_bytes(self._buffer.getvalue())
# Reset the buffer.
self._buffer.seek(0)
self._buffer.truncate(0)
return result
def __getstate__(self):
return self._state
def __setstate__(self, state):
self.__init__(*state)
def __init__(self,
column_names,
schema,
delimiter=',',
secondary_delimiter=None,
multivalent_columns=None):
"""Initializes CsvCoder.
Args:
column_names: Tuple of strings. Order must match the order in the file.
schema: A `Schema` proto.
delimiter: A one-character string used to separate fields.
secondary_delimiter: A one-character string used to separate values within
the same field.
multivalent_columns: A list of names for multivalent columns that need to
be split based on secondary delimiter.
Raises:
ValueError: If `schema` is invalid.
"""
self._column_names = column_names
self._schema = schema
self._delimiter = delimiter
self._secondary_delimiter = secondary_delimiter
self._encoder = self._WriterWrapper(delimiter)
if multivalent_columns is None:
multivalent_columns = []
self._multivalent_columns = multivalent_columns
if secondary_delimiter:
secondary_encoder = self._WriterWrapper(secondary_delimiter)
elif multivalent_columns:
raise ValueError(
'secondary_delimiter unspecified for multivalent columns "{}"'.format(
multivalent_columns))
secondary_encoder_by_name = {
name: secondary_encoder for name in multivalent_columns
}
indices_by_name = {
name: index for index, name in enumerate(self._column_names)
}
def index(name):
index = indices_by_name.get(name)
if index is None:
raise ValueError('Column not found: "{}"'.format(name))
else:
return index
self._feature_handlers = []
for name, feature_spec in six.iteritems(
schema_utils.schema_as_feature_spec(schema).feature_spec):
if isinstance(feature_spec, tf.io.FixedLenFeature):
self._feature_handlers.append(
_FixedLenFeatureHandler(name, feature_spec, index(name),
secondary_encoder_by_name.get(name)))
elif isinstance(feature_spec, tf.io.VarLenFeature):
self._feature_handlers.append(
_VarLenFeatureHandler(name, feature_spec.dtype, index(name),
secondary_encoder_by_name.get(name)))
elif isinstance(feature_spec, tf.io.SparseFeature):
self._feature_handlers.append(
_VarLenFeatureHandler(feature_spec.index_key, tf.int64,
index(feature_spec.index_key),
secondary_encoder_by_name.get(name)))
self._feature_handlers.append(
_VarLenFeatureHandler(feature_spec.value_key, feature_spec.dtype,
index(feature_spec.value_key),
secondary_encoder_by_name.get(name)))
else:
raise ValueError(
'feature_spec should be one of tf.FixedLenFeature, '
'tf.VarLenFeature or tf.SparseFeature: {!r} was {!r}'.format(
name, type(feature_spec)))
def __reduce__(self):
return self.__class__, (self._column_names, self._schema, self._delimiter,
self._secondary_delimiter,
self._multivalent_columns)
def encode(self, instance):
"""Encode a tf.transform encoded dict to a csv-formatted string.
Args:
instance: A python dictionary where the keys are the column names and the
values are fixed len or var len encoded features.
Returns:
A csv-formatted string. The order of the columns is given by column_names.
"""
string_list = [None] * len(self._column_names)
for feature_handler in self._feature_handlers:
try:
feature_handler.encode_value(string_list,
instance[feature_handler.name])
except TypeError as e:
raise TypeError('{} while encoding feature "{}"'.format(
e, feature_handler.name))
return self._encoder.encode_record(string_list)
@deprecation.deprecated(None, _DECODE_DEPRECATION_MESSAGE)
def decode(self, csv_string):
raise NotImplementedError(_DECODE_DEPRECATION_MESSAGE)