From 7c10d9a66fbe923e475eff6fc818feffd41db574 Mon Sep 17 00:00:00 2001 From: David Soergel Date: Mon, 30 May 2016 19:19:53 -0800 Subject: Inflow: rename Column to Series Change: 123605859 --- tensorflow/contrib/learn/BUILD | 4 +- .../learn/python/learn/dataframe/__init__.py | 6 +- .../contrib/learn/python/learn/dataframe/column.py | 96 ---------------------- .../learn/python/learn/dataframe/dataframe.py | 8 +- .../contrib/learn/python/learn/dataframe/series.py | 96 ++++++++++++++++++++++ .../learn/python/learn/dataframe/transform.py | 82 +++++++++--------- .../learn/python/learn/tests/dataframe/mocks.py | 6 +- .../python/learn/tests/dataframe/test_column.py | 71 ---------------- .../learn/tests/dataframe/test_csv_parser.py | 2 +- .../python/learn/tests/dataframe/test_dataframe.py | 18 ++-- .../learn/tests/dataframe/test_example_parser.py | 2 +- .../python/learn/tests/dataframe/test_series.py | 71 ++++++++++++++++ .../python/learn/tests/dataframe/test_transform.py | 24 +++--- 13 files changed, 243 insertions(+), 243 deletions(-) delete mode 100644 tensorflow/contrib/learn/python/learn/dataframe/column.py create mode 100644 tensorflow/contrib/learn/python/learn/dataframe/series.py delete mode 100644 tensorflow/contrib/learn/python/learn/tests/dataframe/test_column.py create mode 100644 tensorflow/contrib/learn/python/learn/tests/dataframe/test_series.py diff --git a/tensorflow/contrib/learn/BUILD b/tensorflow/contrib/learn/BUILD index 5b57df9a6a..a5cf12406a 100644 --- a/tensorflow/contrib/learn/BUILD +++ b/tensorflow/contrib/learn/BUILD @@ -88,9 +88,9 @@ py_test( ) py_test( - name = "test_column", + name = "test_series", size = "small", - srcs = ["python/learn/tests/dataframe/test_column.py"], + srcs = ["python/learn/tests/dataframe/test_series.py"], srcs_version = "PY2AND3", deps = [ ":learn", diff --git a/tensorflow/contrib/learn/python/learn/dataframe/__init__.py b/tensorflow/contrib/learn/python/learn/dataframe/__init__.py index 6886fbdc24..8daf8b1201 100644 --- a/tensorflow/contrib/learn/python/learn/dataframe/__init__.py +++ b/tensorflow/contrib/learn/python/learn/dataframe/__init__.py @@ -20,9 +20,9 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -from tensorflow.contrib.learn.python.learn.dataframe.column import Column -from tensorflow.contrib.learn.python.learn.dataframe.column import TransformedColumn from tensorflow.contrib.learn.python.learn.dataframe.dataframe import DataFrame +from tensorflow.contrib.learn.python.learn.dataframe.series import Series +from tensorflow.contrib.learn.python.learn.dataframe.series import TransformedSeries from tensorflow.contrib.learn.python.learn.dataframe.transform import parameter from tensorflow.contrib.learn.python.learn.dataframe.transform import Transform @@ -31,4 +31,4 @@ from tensorflow.contrib.learn.python.learn.dataframe.transforms.in_memory_source from tensorflow.contrib.learn.python.learn.dataframe.transforms.in_memory_source import PandasSource from tensorflow.contrib.learn.python.learn.dataframe.transforms.reader_source import ReaderSource -__all__ = ['Column', 'TransformedColumn', 'DataFrame', 'parameter', 'Transform'] +__all__ = ['Series', 'TransformedSeries', 'DataFrame', 'parameter', 'Transform'] diff --git a/tensorflow/contrib/learn/python/learn/dataframe/column.py b/tensorflow/contrib/learn/python/learn/dataframe/column.py deleted file mode 100644 index 23b881b524..0000000000 --- a/tensorflow/contrib/learn/python/learn/dataframe/column.py +++ /dev/null @@ -1,96 +0,0 @@ -# pylint: disable=g-bad-file-header -# Copyright 2016 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. -# ============================================================================== - -"""A Column represents a deferred Tensor computation in a DataFrame.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from abc import ABCMeta - - -class Column(object): - """A single output column. - - Represents the deferred construction of a graph that computes the column - values. - - Note every `Column` should be a `TransformedColumn`, except when mocked. - """ - - __metaclass__ = ABCMeta - - def build(self, cache): - """Returns a Tensor.""" - raise NotImplementedError() - - -class TransformedColumn(Column): - """A `Column` that results from applying a `Transform` to a list of inputs.""" - - def __init__(self, input_columns, transform, output_name): - super(TransformedColumn, self).__init__() - self._input_columns = input_columns - self._transform = transform - self._output_name = output_name - - if output_name is None: - raise ValueError("output_name must be provided") - - if len(input_columns) != transform.input_valency: - raise ValueError("Expected %s input Columns but received %s." % - (transform.input_valency, len(input_columns))) - - self._repr = TransformedColumn.make_repr( - self._input_columns, self._transform, self._output_name) - - def build(self, cache=None): - if cache is None: - cache = {} - all_outputs = self._transform.apply_transform(self._input_columns, cache) - return getattr(all_outputs, self._output_name) - - def __repr__(self): - return self._repr - - # Note we need to generate column reprs from Transform, without needing the - # columns themselves. So we just make this public. Alternatively we could - # create throwaway columns just in order to call repr() on them. - @staticmethod - def make_repr(input_columns, transform, output_name): - """Generate a key for caching Tensors produced for a TransformedColumn. - - Generally we a need a deterministic unique key representing which transform - was applied to which inputs, and which output was selected. - - Args: - input_columns: the input `Columns` for the `Transform` - transform: the `Transform` being applied - output_name: the name of the specific output from the `Transform` that is - to be cached - - Returns: - A string suitable for use as a cache key for Tensors produced via a - TransformedColumn - """ - input_column_keys = [repr(column) for column in input_columns] - input_column_keys_joined = ", ".join(input_column_keys) - - return "%s(%s)[%s]" % ( - repr(transform), input_column_keys_joined, output_name) - - diff --git a/tensorflow/contrib/learn/python/learn/dataframe/dataframe.py b/tensorflow/contrib/learn/python/learn/dataframe/dataframe.py index 81e451463d..1e1c2856c6 100644 --- a/tensorflow/contrib/learn/python/learn/dataframe/dataframe.py +++ b/tensorflow/contrib/learn/python/learn/dataframe/dataframe.py @@ -23,7 +23,7 @@ from __future__ import print_function from abc import ABCMeta import collections -from .column import Column +from .series import Series from .transform import Transform @@ -62,7 +62,7 @@ class DataFrame(object): if not isinstance(k, str): raise TypeError("The only supported type for keys is string; got %s" % type(k)) - if isinstance(v, Column): + if isinstance(v, Series): s = v elif isinstance(v, Transform) and v.input_valency() == 0: s = v() @@ -74,7 +74,7 @@ class DataFrame(object): # s = series.NumpySeries(v) else: raise TypeError( - "Column in assignment must be an inflow.Column, pandas.Series or a" + "Column in assignment must be an inflow.Series, pandas.Series or a" " numpy array; got type '%s'." % type(v).__name__) self._columns[k] = s @@ -116,7 +116,7 @@ class DataFrame(object): def __setitem__(self, key, value): if isinstance(key, str): key = [key] - if isinstance(value, Column): + if isinstance(value, Series): value = [value] self.assign(**dict(zip(key, value))) diff --git a/tensorflow/contrib/learn/python/learn/dataframe/series.py b/tensorflow/contrib/learn/python/learn/dataframe/series.py new file mode 100644 index 0000000000..98c365c6cf --- /dev/null +++ b/tensorflow/contrib/learn/python/learn/dataframe/series.py @@ -0,0 +1,96 @@ +# pylint: disable=g-bad-file-header +# Copyright 2016 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. +# ============================================================================== + +"""A Series represents a deferred Tensor computation in a DataFrame.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from abc import ABCMeta + + +class Series(object): + """A single output series. + + Represents the deferred construction of a graph that computes the series + values. + + Note every `Series` should be a `TransformedSeries`, except when mocked. + """ + + __metaclass__ = ABCMeta + + def build(self, cache): + """Returns a Tensor.""" + raise NotImplementedError() + + +class TransformedSeries(Series): + """A `Series` that results from applying a `Transform` to a list of inputs.""" + + def __init__(self, input_series, transform, output_name): + super(TransformedSeries, self).__init__() + self._input_series = input_series + self._transform = transform + self._output_name = output_name + + if output_name is None: + raise ValueError("output_name must be provided") + + if len(input_series) != transform.input_valency: + raise ValueError("Expected %s input Series but received %s." % + (transform.input_valency, len(input_series))) + + self._repr = TransformedSeries.make_repr( + self._input_series, self._transform, self._output_name) + + def build(self, cache=None): + if cache is None: + cache = {} + all_outputs = self._transform.apply_transform(self._input_series, cache) + return getattr(all_outputs, self._output_name) + + def __repr__(self): + return self._repr + + # Note we need to generate series reprs from Transform, without needing the + # series themselves. So we just make this public. Alternatively we could + # create throwaway series just in order to call repr() on them. + @staticmethod + def make_repr(input_series, transform, output_name): + """Generate a key for caching Tensors produced for a TransformedSeries. + + Generally we a need a deterministic unique key representing which transform + was applied to which inputs, and which output was selected. + + Args: + input_series: an iterable of input `Series` for the `Transform` + transform: the `Transform` being applied + output_name: the name of the specific output from the `Transform` that is + to be cached + + Returns: + A string suitable for use as a cache key for Tensors produced via a + TransformedSeries + """ + input_series_keys = [repr(series) for series in input_series] + input_series_keys_joined = ", ".join(input_series_keys) + + return "%s(%s)[%s]" % ( + repr(transform), input_series_keys_joined, output_name) + + diff --git a/tensorflow/contrib/learn/python/learn/dataframe/transform.py b/tensorflow/contrib/learn/python/learn/dataframe/transform.py index 9132c12c0f..ffeeca1a79 100644 --- a/tensorflow/contrib/learn/python/learn/dataframe/transform.py +++ b/tensorflow/contrib/learn/python/learn/dataframe/transform.py @@ -14,7 +14,7 @@ # limitations under the License. # ============================================================================== -"""A Transform takes a list of `Column` and returns a namedtuple of `Column`.""" +"""A Transform takes a list of `Series` and returns a namedtuple of `Series`.""" from __future__ import absolute_import from __future__ import division @@ -27,35 +27,35 @@ from abc import abstractproperty import collections import inspect -from .column import Column -from .column import TransformedColumn +from .series import Series +from .series import TransformedSeries -def _make_list_of_column(x): - """Converts `x` into a list of `Column` if possible. +def _make_list_of_series(x): + """Converts `x` into a list of `Series` if possible. Args: - x: a `Column`, a list of `Column` or `None`. + x: a `Series`, a list of `Series` or `None`. Returns: - `x` if it is a list of Column, `[x]` if `x` is a `Column`, `[]` if x is + `x` if it is a list of Series, `[x]` if `x` is a `Series`, `[]` if x is `None`. Raises: - TypeError: `x` is not a `Column` a list of `Column` or `None`. + TypeError: `x` is not a `Series` a list of `Series` or `None`. """ if x is None: return [] - elif isinstance(x, Column): + elif isinstance(x, Series): return [x] elif isinstance(x, (list, tuple)): for i, y in enumerate(x): - if not isinstance(y, Column): + if not isinstance(y, Series): raise TypeError( - "Expected a tuple or list of Columns; entry %s has type %s." % + "Expected a tuple or list of Series; entry %s has type %s." % (i, type(y).__name__)) return list(x) - raise TypeError("Expected a Column or list of Column; got %s" % + raise TypeError("Expected a Series or list of Series; got %s" % type(x).__name__) @@ -103,9 +103,9 @@ def parameter(func): class Transform(object): - """A function from a list of `Column` to a namedtuple of `Column`. + """A function from a list of `Series` to a namedtuple of `Series`. - Transforms map zero or more columns of a DataFrame to new columns. + Transforms map zero or more Series of a DataFrame to new Series. """ __metaclass__ = ABCMeta @@ -128,7 +128,7 @@ class Transform(object): @abstractproperty def input_valency(self): - """The number of `Column`s that the `Transform` should expect as input. + """The number of `Series` that the `Transform` should expect as input. `None` indicates that the transform can take a variable number of inputs. @@ -141,7 +141,7 @@ class Transform(object): @property def output_names(self): - """The names of `Column`s output by the `Transform`. + """The names of `Series` output by the `Transform`. This function should depend only on `@parameter`s of this `Transform`. @@ -152,7 +152,7 @@ class Transform(object): @abstractproperty def _output_names(self): - """The names of `Column`s output by the `Transform`. + """The names of `Series` output by the `Transform`. This function should depend only on `@parameter`s of this `Transform`. @@ -171,7 +171,7 @@ class Transform(object): instantiating an object of this type with corresponding values. Note this output type is used both for `__call__`, in which case the - values are `TransformedColumn`s, and for `apply_transform`, in which case + values are `TransformedSeries`, and for `apply_transform`, in which case the values are `Tensor`s. Returns: @@ -201,57 +201,57 @@ class Transform(object): "Expected a NamedTuple of Tensors with elements %s; got %s." % (self.output_names, type(output_tensors).__name__)) - def __call__(self, input_columns=None): - """Apply this `Transform` to the provided `Column`s, producing 'Column's. + def __call__(self, input_series=None): + """Apply this `Transform` to the provided `Series`, producing 'Series'. Args: - input_columns: None, a `Column`, or a list of input `Column`s, acting as + input_series: None, a `Series`, or a list of input `Series`, acting as positional arguments. Returns: - A namedtuple of the output Columns. + A namedtuple of the output `Series`. Raises: - ValueError: `input_columns` does not have expected length + ValueError: `input_series` does not have expected length """ - input_columns = _make_list_of_column(input_columns) - if len(input_columns) != self.input_valency: - raise ValueError("Expected %s input Columns but received %s." % - (self.input_valency, len(input_columns))) - output_columns = [TransformedColumn(input_columns, self, output_name) - for output_name in self.output_names] + input_series = _make_list_of_series(input_series) + if len(input_series) != self.input_valency: + raise ValueError("Expected %s input Series but received %s." % + (self.input_valency, len(input_series))) + output_series = [TransformedSeries(input_series, self, output_name) + for output_name in self.output_names] # pylint: disable=not-callable - return self.return_type(*output_columns) + return self.return_type(*output_series) - def apply_transform(self, input_columns, cache=None): - """Apply this `Transform` to the provided `Column`s, producing 'Tensor's. + def apply_transform(self, input_series, cache=None): + """Apply this `Transform` to the provided `Series`, producing 'Tensor's. Args: - input_columns: None, a `Column`, or a list of input `Column`s, acting as + input_series: None, a `Series`, or a list of input `Series`, acting as positional arguments. - cache: a dict from Column reprs to Tensors. + cache: a dict from Series reprs to Tensors. Returns: A namedtuple of the output Tensors. Raises: - ValueError: `input_columns` does not have expected length + ValueError: `input_series` does not have expected length """ # pylint: disable=not-callable if cache is None: cache = {} - if len(input_columns) != self.input_valency: - raise ValueError("Expected %s input Columns but received %s." % - (self.input_valency, len(input_columns))) - input_tensors = [input_column.build(cache) - for input_column in input_columns] + if len(input_series) != self.input_valency: + raise ValueError("Expected %s input Series but received %s." % + (self.input_valency, len(input_series))) + input_tensors = [series.build(cache) + for series in input_series] # Note we cache each output individually, not just the entire output # tuple. This allows using the graph as the cache, since it can sensibly # cache only individual Tensors. - output_reprs = [TransformedColumn.make_repr(input_columns, self, + output_reprs = [TransformedSeries.make_repr(input_series, self, output_name) for output_name in self.output_names] output_tensors = [cache.get(output_repr) for output_repr in output_reprs] diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/mocks.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/mocks.py index be3cc9454a..8f107214bc 100644 --- a/tensorflow/contrib/learn/python/learn/tests/dataframe/mocks.py +++ b/tensorflow/contrib/learn/python/learn/tests/dataframe/mocks.py @@ -25,11 +25,11 @@ from abc import ABCMeta from tensorflow.contrib.learn.python import learn -class MockColumn(learn.Column): - """A mock column for use in testing.""" +class MockSeries(learn.Series): + """A mock series for use in testing.""" def __init__(self, cachekey, mock_tensors): - super(MockColumn, self).__init__() + super(MockSeries, self).__init__() self._cachekey = cachekey self._mock_tensors = mock_tensors diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_column.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_column.py deleted file mode 100644 index 0121737e11..0000000000 --- a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_column.py +++ /dev/null @@ -1,71 +0,0 @@ -# pylint: disable=g-bad-file-header -# Copyright 2016 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 of the Column class.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from tensorflow.contrib.learn.python import learn -from tensorflow.contrib.learn.python.learn.tests.dataframe import mocks - - -class TransformedColumnTest(tf.test.TestCase): - """Test of `TransformedColumn`.""" - - def test_repr(self): - col = learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], - mocks.MockTwoOutputTransform("thb", "nth", "snt"), "qux") - - # note params are sorted by name - expected = ("MockTransform({'param_one': 'thb', 'param_three': 'snt', " - "'param_two': 'nth'})" - "(foobar)[qux]") - self.assertEqual(expected, repr(col)) - - def test_build_no_output(self): - def create_no_output_column(): - return learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], - mocks.MockZeroOutputTransform("thb", "nth"), None) - - self.assertRaises(ValueError, create_no_output_column) - - def test_build_single_output(self): - col = learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], - mocks.MockOneOutputTransform("thb", "nth"), "out1") - - result = col.build() - expected = "Fake Tensor 1" - self.assertEqual(expected, result) - - def test_build_multiple_output(self): - col = learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], - mocks.MockTwoOutputTransform("thb", "nth", "snt"), "out2") - - result = col.build() - expected = "Fake Tensor 2" - self.assertEqual(expected, result) - - -if __name__ == "__main__": - tf.test.main() diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_csv_parser.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_csv_parser.py index 50612cd762..4b9177ab78 100644 --- a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_csv_parser.py +++ b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_csv_parser.py @@ -32,7 +32,7 @@ class CSVParserTestCase(tf.test.TestCase): default_values=["", "", 1.4]) csv_lines = ["one,two,2.5", "four,five,6.0"] csv_input = tf.constant(csv_lines, dtype=tf.string, shape=[len(csv_lines)]) - csv_column = mocks.MockColumn("csv", csv_input) + csv_column = mocks.MockSeries("csv", csv_input) expected_output = [np.array([b"one", b"four"]), np.array([b"two", b"five"]), np.array([2.5, 6.0])] diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_dataframe.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_dataframe.py index 5968d777ee..7cb9c2ce2e 100644 --- a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_dataframe.py +++ b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_dataframe.py @@ -29,14 +29,14 @@ from tensorflow.contrib.learn.python.learn.tests.dataframe import mocks def setup_test_df(): """Create a dataframe populated with some test columns.""" df = learn.DataFrame() - df["a"] = learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], + df["a"] = learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], mocks.MockTwoOutputTransform("iue", "eui", "snt"), "out1") - df["b"] = learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], + df["b"] = learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], mocks.MockTwoOutputTransform("iue", "eui", "snt"), "out2") - df["c"] = learn.TransformedColumn( - [mocks.MockColumn("foobar", [])], + df["c"] = learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], mocks.MockTwoOutputTransform("iue", "eui", "snt"), "out1") return df @@ -61,7 +61,7 @@ class DataFrameTest(tf.test.TestCase): def test_set_item_column(self): df = setup_test_df() self.assertEqual(3, len(df)) - col1 = mocks.MockColumn("QuackColumn", []) + col1 = mocks.MockSeries("QuackColumn", []) df["quack"] = col1 self.assertEqual(4, len(df)) col2 = df["quack"] @@ -70,8 +70,8 @@ class DataFrameTest(tf.test.TestCase): def test_set_item_column_multi(self): df = setup_test_df() self.assertEqual(3, len(df)) - col1 = mocks.MockColumn("QuackColumn", []) - col2 = mocks.MockColumn("MooColumn", []) + col1 = mocks.MockSeries("QuackColumn", []) + col2 = mocks.MockSeries("MooColumn", []) df["quack", "moo"] = [col1, col2] self.assertEqual(5, len(df)) col3 = df["quack"] diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_example_parser.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_example_parser.py index adfaec1541..c4ccf6ba9a 100644 --- a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_example_parser.py +++ b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_example_parser.py @@ -71,7 +71,7 @@ class ExampleParserTestCase(tf.test.TestCase): " } " " } " "} ", self.example2) - self.example_column = mocks.MockColumn( + self.example_column = mocks.MockSeries( "example", tf.constant( [self.example1.SerializeToString(), diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_series.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_series.py new file mode 100644 index 0000000000..71af6bce38 --- /dev/null +++ b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_series.py @@ -0,0 +1,71 @@ +# pylint: disable=g-bad-file-header +# Copyright 2016 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 of the Series class.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import tensorflow as tf + +from tensorflow.contrib.learn.python import learn +from tensorflow.contrib.learn.python.learn.tests.dataframe import mocks + + +class TransformedSeriesTest(tf.test.TestCase): + """Test of `TransformedSeries`.""" + + def test_repr(self): + col = learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], + mocks.MockTwoOutputTransform("thb", "nth", "snt"), "qux") + + # note params are sorted by name + expected = ("MockTransform({'param_one': 'thb', 'param_three': 'snt', " + "'param_two': 'nth'})" + "(foobar)[qux]") + self.assertEqual(expected, repr(col)) + + def test_build_no_output(self): + def create_no_output_series(): + return learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], + mocks.MockZeroOutputTransform("thb", "nth"), None) + + self.assertRaises(ValueError, create_no_output_series) + + def test_build_single_output(self): + col = learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], + mocks.MockOneOutputTransform("thb", "nth"), "out1") + + result = col.build() + expected = "Fake Tensor 1" + self.assertEqual(expected, result) + + def test_build_multiple_output(self): + col = learn.TransformedSeries( + [mocks.MockSeries("foobar", [])], + mocks.MockTwoOutputTransform("thb", "nth", "snt"), "out2") + + result = col.build() + expected = "Fake Tensor 2" + self.assertEqual(expected, result) + + +if __name__ == "__main__": + tf.test.main() diff --git a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_transform.py b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_transform.py index c04cbf7f74..b524c73e5c 100644 --- a/tensorflow/contrib/learn/python/learn/tests/dataframe/test_transform.py +++ b/tensorflow/contrib/learn/python/learn/tests/dataframe/test_transform.py @@ -23,7 +23,7 @@ from __future__ import print_function import tensorflow as tf from tensorflow.contrib.learn.python import learn -from tensorflow.contrib.learn.python.learn.dataframe.transform import _make_list_of_column +from tensorflow.contrib.learn.python.learn.dataframe.transform import _make_list_of_series from tensorflow.contrib.learn.python.learn.tests.dataframe import mocks @@ -31,17 +31,17 @@ class TransformTest(tf.test.TestCase): """Tests of the Transform class.""" def test_make_list_of_column(self): - col1 = mocks.MockColumn("foo", []) - col2 = mocks.MockColumn("bar", []) + col1 = mocks.MockSeries("foo", []) + col2 = mocks.MockSeries("bar", []) - self.assertEqual([], _make_list_of_column(None)) - self.assertEqual([col1], _make_list_of_column(col1)) - self.assertEqual([col1], _make_list_of_column([col1])) - self.assertEqual([col1, col2], _make_list_of_column([col1, col2])) - self.assertEqual([col1, col2], _make_list_of_column((col1, col2))) + self.assertEqual([], _make_list_of_series(None)) + self.assertEqual([col1], _make_list_of_series(col1)) + self.assertEqual([col1], _make_list_of_series([col1])) + self.assertEqual([col1, col2], _make_list_of_series([col1, col2])) + self.assertEqual([col1, col2], _make_list_of_series((col1, col2))) def test_cache(self): - z = mocks.MockColumn("foobar", []) + z = mocks.MockSeries("foobar", []) t = mocks.MockTwoOutputTransform("thb", "nth", "snt") cache = {} t.apply_transform([z], cache) @@ -78,14 +78,14 @@ class TransformTest(tf.test.TestCase): def test_call(self): t = mocks.MockTwoOutputTransform("a", "b", "c") # MockTwoOutputTransform has input valency 1 - input1 = mocks.MockColumn("foobar", []) + input1 = mocks.MockSeries("foobar", []) out1, out2 = t([input1]) # pylint: disable=not-callable - self.assertEqual(learn.TransformedColumn, type(out1)) + self.assertEqual(learn.TransformedSeries, type(out1)) # self.assertEqual(out1.transform, t) # self.assertEqual(out1.output_name, "output1") - self.assertEqual(learn.TransformedColumn, type(out2)) + self.assertEqual(learn.TransformedSeries, type(out2)) # self.assertEqual(out2.transform, t) # self.assertEqual(out2.output_name, "output2") -- cgit v1.2.3