From 936e5937220f18591a04c3baa91ad0f5aaacb171 Mon Sep 17 00:00:00 2001 From: Jianwei Xie Date: Fri, 3 Mar 2017 09:10:25 -0800 Subject: [PATCH] Does pandas import check in each caller file directly to avoid flaky tests. Change: 149118694 --- .../learn/python/learn/learn_io/pandas_io.py | 9 ++++-- tensorflow/python/estimator/BUILD | 17 ++-------- .../python/estimator/inputs/__init__.py | 1 - .../python/estimator/inputs/pandas_import.py | 32 ------------------- .../python/estimator/inputs/pandas_io.py | 12 ++++++- .../python/estimator/inputs/pandas_io_test.py | 9 ++++-- .../inputs/queues/feeding_functions.py | 9 ++++-- .../inputs/queues/feeding_functions_test.py | 9 ++++-- .../queues/feeding_queue_runner_test.py | 9 ++++-- 9 files changed, 48 insertions(+), 59 deletions(-) delete mode 100644 tensorflow/python/estimator/inputs/pandas_import.py diff --git a/tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py b/tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py index d516754fca4..e7fb830568c 100644 --- a/tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py +++ b/tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py @@ -19,12 +19,17 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.estimator.inputs.pandas_io import pandas_input_fn # pylint: disable=unused-import -if HAS_PANDAS: +try: # pylint: disable=g-import-not-at-top import pandas as pd + HAS_PANDAS = True +except IOError: + # Pandas writes a temporary file during import. If it fails, don't use pandas. + HAS_PANDAS = False +except ImportError: + HAS_PANDAS = False PANDAS_DTYPES = { 'int8': 'int', diff --git a/tensorflow/python/estimator/BUILD b/tensorflow/python/estimator/BUILD index c5a339d6d0b..61651c0a2fb 100644 --- a/tensorflow/python/estimator/BUILD +++ b/tensorflow/python/estimator/BUILD @@ -163,7 +163,6 @@ py_library( srcs_version = "PY2AND3", deps = [ ":numpy_io", - ":pandas_import", ":pandas_io", ], ) @@ -190,20 +189,11 @@ py_test( ], ) -py_library( - name = "pandas_import", - srcs = ["inputs/pandas_import.py"], - srcs_version = "PY2AND3", -) - py_library( name = "pandas_io", srcs = ["inputs/pandas_io.py"], srcs_version = "PY2AND3", - deps = [ - ":inputs_queues", - ":pandas_import", - ], + deps = [":inputs_queues"], ) py_test( @@ -228,10 +218,7 @@ py_library( "inputs/queues/feeding_queue_runner.py", ], srcs_version = "PY2AND3", - deps = [ - ":pandas_import", - "//tensorflow/python:training", - ], + deps = ["//tensorflow/python:training"], ) py_test( diff --git a/tensorflow/python/estimator/inputs/__init__.py b/tensorflow/python/estimator/inputs/__init__.py index c7bfbf562db..2e5987fe4f7 100644 --- a/tensorflow/python/estimator/inputs/__init__.py +++ b/tensorflow/python/estimator/inputs/__init__.py @@ -19,5 +19,4 @@ from __future__ import division from __future__ import print_function from tensorflow.python.estimator.inputs.numpy_io import numpy_input_fn -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.estimator.inputs.pandas_io import pandas_input_fn diff --git a/tensorflow/python/estimator/inputs/pandas_import.py b/tensorflow/python/estimator/inputs/pandas_import.py deleted file mode 100644 index 6f78a168470..00000000000 --- a/tensorflow/python/estimator/inputs/pandas_import.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2017 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. -# ============================================================================== -"""Handles pandas import for tensorflow.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as _ # pylint: disable=unused-import - -try: - # pylint: disable=g-import-not-at-top - # pylint: disable=unused-import - import pandas as _ - HAS_PANDAS = True -except IOError: - # Pandas writes a temporary file during import. If it fails, don't use pandas. - HAS_PANDAS = False -except ImportError: - HAS_PANDAS = False diff --git a/tensorflow/python/estimator/inputs/pandas_io.py b/tensorflow/python/estimator/inputs/pandas_io.py index 914845ba6a2..a1e418f487c 100644 --- a/tensorflow/python/estimator/inputs/pandas_io.py +++ b/tensorflow/python/estimator/inputs/pandas_io.py @@ -20,9 +20,19 @@ from __future__ import division from __future__ import print_function import numpy as np -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.estimator.inputs.queues import feeding_functions +try: + # pylint: disable=g-import-not-at-top + # pylint: disable=unused-import + import pandas as pd + HAS_PANDAS = True +except IOError: + # Pandas writes a temporary file during import. If it fails, don't use pandas. + HAS_PANDAS = False +except ImportError: + HAS_PANDAS = False + def pandas_input_fn(x, y=None, diff --git a/tensorflow/python/estimator/inputs/pandas_io_test.py b/tensorflow/python/estimator/inputs/pandas_io_test.py index 2e1fee4dd8e..e6934d3ff95 100644 --- a/tensorflow/python/estimator/inputs/pandas_io_test.py +++ b/tensorflow/python/estimator/inputs/pandas_io_test.py @@ -21,15 +21,20 @@ from __future__ import print_function import numpy as np from tensorflow.python.estimator.inputs import pandas_io -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.framework import errors from tensorflow.python.platform import test from tensorflow.python.training import coordinator from tensorflow.python.training import queue_runner_impl -if HAS_PANDAS: +try: # pylint: disable=g-import-not-at-top import pandas as pd + HAS_PANDAS = True +except IOError: + # Pandas writes a temporary file during import. If it fails, don't use pandas. + HAS_PANDAS = False +except ImportError: + HAS_PANDAS = False class PandasIoTest(test.TestCase): diff --git a/tensorflow/python/estimator/inputs/queues/feeding_functions.py b/tensorflow/python/estimator/inputs/queues/feeding_functions.py index aa39958559a..9da2bce0f81 100644 --- a/tensorflow/python/estimator/inputs/queues/feeding_functions.py +++ b/tensorflow/python/estimator/inputs/queues/feeding_functions.py @@ -22,7 +22,6 @@ import collections import random import numpy as np -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.estimator.inputs.queues import feeding_queue_runner as fqr from tensorflow.python.framework import dtypes from tensorflow.python.framework import errors @@ -34,9 +33,15 @@ from tensorflow.python.platform import tf_logging as logging from tensorflow.python.summary import summary from tensorflow.python.training import queue_runner -if HAS_PANDAS: +try: # pylint: disable=g-import-not-at-top import pandas as pd + HAS_PANDAS = True +except IOError: + # Pandas writes a temporary file during import. If it fails, don't use pandas. + HAS_PANDAS = False +except ImportError: + HAS_PANDAS = False def _get_integer_indices_for_next_batch( diff --git a/tensorflow/python/estimator/inputs/queues/feeding_functions_test.py b/tensorflow/python/estimator/inputs/queues/feeding_functions_test.py index ad27d990ea7..0e602d3f331 100644 --- a/tensorflow/python/estimator/inputs/queues/feeding_functions_test.py +++ b/tensorflow/python/estimator/inputs/queues/feeding_functions_test.py @@ -22,13 +22,18 @@ import collections import numpy as np -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.estimator.inputs.queues import feeding_functions as ff from tensorflow.python.platform import test -if HAS_PANDAS: +try: # pylint: disable=g-import-not-at-top import pandas as pd + HAS_PANDAS = True +except IOError: + # Pandas writes a temporary file during import. If it fails, don't use pandas. + HAS_PANDAS = False +except ImportError: + HAS_PANDAS = False def vals_to_list(a): diff --git a/tensorflow/python/estimator/inputs/queues/feeding_queue_runner_test.py b/tensorflow/python/estimator/inputs/queues/feeding_queue_runner_test.py index c8d20970c53..6292eb7da1b 100644 --- a/tensorflow/python/estimator/inputs/queues/feeding_queue_runner_test.py +++ b/tensorflow/python/estimator/inputs/queues/feeding_queue_runner_test.py @@ -21,16 +21,21 @@ from __future__ import print_function import numpy as np from tensorflow.python.client import session -from tensorflow.python.estimator.inputs.pandas_import import HAS_PANDAS from tensorflow.python.estimator.inputs.queues import feeding_functions as ff from tensorflow.python.framework import ops from tensorflow.python.platform import test from tensorflow.python.training import coordinator from tensorflow.python.training import queue_runner_impl -if HAS_PANDAS: +try: # pylint: disable=g-import-not-at-top import pandas as pd + HAS_PANDAS = True +except IOError: + # Pandas writes a temporary file during import. If it fails, don't use pandas. + HAS_PANDAS = False +except ImportError: + HAS_PANDAS = False def get_rows(array, row_indices):