<commit_before># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may noa 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.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
from tensorflow_addons.image import median_filter2d
from tensorflow_addons.utils import test_utils


@test_utils.run_all_in_graph_and_eager_modes
class MedianFilter2dTest(tf.test.TestCase):
    def _validate_median_filter2d(self,
                                  inputs,
                                  expected_values,
                                  filter_shape=(3, 3)):

        values_op = median_filter2d(inputs)
        with self.test_session(use_gpu=False) as sess:
            if tf.executing_eagerly():
                expected_values = expected_values.numpy()
                values = values_op.numpy()
            else:
                expected_values = expected_values.eval()
                values = values_op.eval()
            self.assertShapeEqual(values, inputs)
            self.assertShapeEqual(expected_values, values_op)
            self.assertAllClose(expected_values, values)

    def test_filter_tuple(self):
        tf_img = tf.zeros([3, 4, 3], tf.int32)

        with self.assertRaisesRegexp(TypeError,
                                     'Filter shape must be a tuple'):
            median_filter2d(tf_img, 3)
            median_filter2d(tf_img, 3.5)
            median_filter2d(tf_img, 'dt')
            median_filter2d(tf_img, None)

        filter_shape = (3, 3, 3)
        msg = 'Filter shape must be a tuple of 2 integers. ' \
              'Got %s values in tuple' % len(filter_shape)
        with self.assertRaisesRegexp(ValueError, msg):
            median_filter2d(tf_img, filter_shape)

        with self.assertRaisesRegexp(TypeError,
                                     'Size of the filter must be Integers'):
            median_filter2d(tf_img, (3.5, 3))
            median_filter2d(tf_img, (None, 3))

    def test_filter_value(self):
        tf_img = tf.zeros([3, 4, 3], tf.int32)

        with self.assertRaises(ValueError):
            median_filter2d(tf_img, (4, 3))

    def test_dimension(self):
        tf.compat.v1.disable_eager_execution()
        tf_img = tf.compat.v1.placeholder(tf.int32, shape=[3, 4, None])
        tf_img1 = tf.compat.v1.placeholder(tf.int32, shape=[3, None, 4])
        tf_img2 = tf.compat.v1.placeholder(tf.int32, shape=[None, 3, 4])

        with self.assertRaises(TypeError):
            median_filter2d(tf_img)
            median_filter2d(tf_img1)
            median_filter2d(tf_img2)

    def test_image_vs_filter(self):
        tf_img = tf.zeros([3, 4, 3], tf.int32)
        m = tf_img.shape[0]
        no = tf_img.shape[1]
        ch = tf_img.shape[2]
        filter_shape = (3, 5)
        with self.assertRaises(ValueError):
            median_filter2d(tf_img, filter_shape)

    def test_three_channels(self):
        tf_img = [[[0.32801723, 0.08863795, 0.79119259],
                   [0.35526001, 0.79388736, 0.55435993],
                   [0.11607035, 0.55673079, 0.99473371]],
                  [[0.53240645, 0.74684819, 0.33700031],<commit_msg>fix test cases (#202)<commit_after># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may noa 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.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
from tensorflow_addons.image import median_filter2d
from tensorflow_addons.utils import test_utils


class MedianFilter2dTest(tf.test.TestCase):
    def _validate_median_filter2d(self,
                                  inputs,
                                  expected_values,
                                  filter_shape=(3, 3)):
        output = median_filter2d(inputs, filter_shape)
        self.assertAllClose(output, expected_values)

    @test_utils.run_in_graph_and_eager_modes
    def test_filter_tuple(self):
        tf_img = tf.zeros([3, 4, 3], tf.int32)

        for filter_shape in [3, 3.5, 'dt', None]:
            with self.assertRaisesRegexp(TypeError,
                                         'Filter shape must be a tuple'):
                median_filter2d(tf_img, filter_shape)

        filter_shape = (3, 3, 3)
        msg = ('Filter shape must be a tuple of 2 integers. '
               'Got %s values in tuple' % len(filter_shape))
        with self.assertRaisesRegexp(ValueError, msg):
            median_filter2d(tf_img, filter_shape)

        msg = 'Size of the filter must be Integers'
        for filter_shape in [(3.5, 3), (None, 3)]:
            with self.assertRaisesRegexp(TypeError, msg):
                median_filter2d(tf_img, filter_shape)

    @test_utils.run_in_graph_and_eager_modes
    def test_filter_value(self):
        tf_img = tf.zeros([3, 4, 3], tf.int32)

        with self.assertRaises(ValueError):
            median_filter2d(tf_img, (4, 3))

    @test_utils.run_deprecated_v1
    def test_dimension(self):
        for image_shape in [(3, 4, None), (3, None, 4), (None, 3, 4)]:
            with self.assertRaises(TypeError):
                tf_img = tf.compat.v1.placeholder(tf.int32, shape=image_shape)
                median_filter2d(tf_img)

    @test_utils.run_in_graph_and_eager_modes
    def test_image_vs_filter(self):
        tf_img = tf.zeros([3, 4, 3], tf.int32)
        filter_shape = (3, 5)
        with self.assertRaises(ValueError):
            median_filter2d(tf_img, filter_shape)

    @test_utils.run_in_graph_and_eager_modes
    def test_three_channels(self):
        tf_img = [[[0.32801723, 0.08863795, 0.79119259],
                   [0.35526001, 0.79388736, 0.55435993],
                   [0.11607035, 0.55673079, 0.99473371]],
                  [[0.53240645, 0.74684819, 0.33700031],
{'fraction_non_alphanumeric': 0.11096811096811096, 'fraction_numerical': 0.051194051194051196, 'mean_word_length': 2.4987493746873435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6874483', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2081, 'n_words': 537}