aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/data/python/kernel_tests/serialization/csv_dataset_serialization_test.py
blob: 247f2046ea313f97bdbda1674765f12406258509 (plain)
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
# 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.
# ==============================================================================
"""Tests for the CsvDataset serialization."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gzip
import os

from tensorflow.contrib.data.python.kernel_tests.serialization import dataset_serialization_test_base
from tensorflow.contrib.data.python.ops import readers
from tensorflow.python.platform import test


class CsvDatasetSerializationTest(
    dataset_serialization_test_base.DatasetSerializationTestBase):

  def setUp(self):
    self._num_cols = 7
    self._num_rows = 10
    self._num_epochs = 14
    self._num_outputs = self._num_rows * self._num_epochs

    inputs = [
        ",".join(str(self._num_cols * j + i)
                 for i in range(self._num_cols))
        for j in range(self._num_rows)
    ]
    contents = "\n".join(inputs).encode("utf-8")

    self._filename = os.path.join(self.get_temp_dir(), "file.csv")
    self._compressed = os.path.join(self.get_temp_dir(),
                                    "comp.csv")  # GZip compressed

    with open(self._filename, "wb") as f:
      f.write(contents)
    with gzip.GzipFile(self._compressed, "wb") as f:
      f.write(contents)

  def ds_func(self, **kwargs):
    compression_type = kwargs.get("compression_type", None)
    if compression_type == "GZIP":
      filename = self._compressed
    elif compression_type is None:
      filename = self._filename
    else:
      raise ValueError("Invalid compression type:", compression_type)

    return readers.CsvDataset(filename, **kwargs).repeat(self._num_epochs)

  def testSerializationCore(self):
    defs = [[0]] * self._num_cols
    self.run_core_tests(
        lambda: self.ds_func(record_defaults=defs, buffer_size=2),
        lambda: self.ds_func(record_defaults=defs, buffer_size=12),
        self._num_outputs)


if __name__ == "__main__":
  test.main()