code: def test_normalize_polygons(self):
      def _assert_single_image_expected(inputs):
          # --> images None
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  inputs, shapes=None
              )
  
          # --> too many images
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  inputs, shapes=np.zeros((2, 1, 1, 3), dtype=np.uint8)
              )
  
          # --> too many images
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  inputs,
                  shapes=[
                      np.zeros((1, 1, 3), dtype=np.uint8),
                      np.zeros((1, 1, 3), dtype=np.uint8),
                  ],
              )
  
      coords1 = [(0, 0), (10, 0), (10, 10)]
      coords2 = [(5, 5), (15, 5), (15, 15)]
      coords3 = [(0, 0), (10, 0), (10, 10), (0, 10)]
      coords4 = [(5, 5), (15, 5), (15, 15), (5, 15)]
  
      coords1_kps = [ia.Keypoint(x=x, y=y) for x, y in coords1]
      coords2_kps = [ia.Keypoint(x=x, y=y) for x, y in coords2]
      coords3_kps = [ia.Keypoint(x=x, y=y) for x, y in coords3]
      coords4_kps = [ia.Keypoint(x=x, y=y) for x, y in coords4]
  
      coords1_arr = np.float32(coords1)
      coords2_arr = np.float32(coords2)
      coords3_arr = np.float32(coords3)
      coords4_arr = np.float32(coords4)
  
      # ----
      # None
      # ----
      polygons_norm = normalization.normalize_polygons(None)
      assert polygons_norm is None
  
      # ----
      # array
      # ----
      for dt in [
          np.dtype("float32"),
          np.dtype("int16"),
          np.dtype("uint16"),
      ]:
          polygons_norm = normalization.normalize_polygons(
              coords1_arr[np.newaxis, np.newaxis, ...].astype(dt),
              shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
          )
          assert isinstance(polygons_norm, list)
          assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
          assert len(polygons_norm[0].polygons) == 1
          assert np.allclose(
              polygons_norm[0].polygons[0].exterior, coords1_arr
          )
  
          polygons_norm = normalization.normalize_polygons(
              np.tile(
                  coords1_arr[np.newaxis, np.newaxis, ...].astype(dt),
                  (1, 5, 1, 1),
              ),
              shapes=np.zeros((1, 1, 1, 3), dtype=np.uint8),
          )
          assert isinstance(polygons_norm, list)
          assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
          assert len(polygons_norm[0].polygons) == 5
          assert np.allclose(
              polygons_norm[0].polygons[0].exterior, coords1_arr
          )
  
          # --> polygons for too many images
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  np.tile(
                      coords1_arr[np.newaxis, np.newaxis, ...].astype(
                          dt
                      ),
                      (2, 1, 1, 1),
                  ),
                  shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
              )
  
          # --> too few polygons
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  np.tile(
                      coords1_arr[np.newaxis, np.newaxis, ...].astype(
                          dt
                      ),
                      (1, 1, 1, 1),
                  ),
                  shapes=np.zeros((2, 1, 1, 3), dtype=np.uint8),
              )
  
          # --> wrong polygons shape
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  np.tile(
                      coords1_arr[np.newaxis, np.newaxis, ...].astype(
                          dt
                      ),
                      (1, 1, 1, 10),
                  ),
                  shapes=np.zeros((1, 1, 1, 3), dtype=np.uint8),
              )
  
          _assert_single_image_expected(
              coords1_arr[np.newaxis, np.newaxis, ...].astype(dt)
          )
  
      # ----
      # single Polygon instance
      # ----
      polygons_norm = normalization.normalize_polygons(
          ia.Polygon(coords1),
          shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 1
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
  
      _assert_single_image_expected(ia.Polygon(coords1))
  
      # ----
      # single PolygonsOnImage instance
      # ----
      polygons_norm = normalization.normalize_polygons(
          ia.PolygonsOnImage([ia.Polygon(coords1)], shape=(1, 1, 3)),
          shapes=None,
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 1
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
  
      # ----
      # empty iterable
      # ----
      polygons_norm = normalization.normalize_polygons([], shapes=None)
      assert polygons_norm is None
  
      # ----
      # iterable of array
      # ----
      for dt in [
          np.dtype("float32"),
          np.dtype("int16"),
          np.dtype("uint16"),
      ]:
          polygons_norm = normalization.normalize_polygons(
              [coords1_arr[np.newaxis, ...].astype(dt)],
              shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
          )
          assert isinstance(polygons_norm, list)
          assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
          assert len(polygons_norm[0].polygons) == 1
          assert np.allclose(
              polygons_norm[0].polygons[0].exterior, coords1_arr
          )
  
          polygons_norm = normalization.normalize_polygons(
              [
                  np.tile(
                      coords1_arr[np.newaxis, ...].astype(dt), (5, 1, 1)
                  )
              ],
              shapes=np.zeros((1, 1, 1, 3), dtype=np.uint8),
          )
          assert isinstance(polygons_norm, list)
          assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
          assert len(polygons_norm[0].polygons) == 5
          assert np.allclose(
              polygons_norm[0].polygons[0].exterior, coords1_arr
          )
  
          # --> polygons for too many images
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  [
                      coords1_arr[np.newaxis, ...].astype(dt),
                      coords2_arr[np.newaxis, ...].astype(dt),
                  ],
                  shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
              )
  
          # --> too few polygons
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  [coords1_arr[np.newaxis, ...].astype(dt)],
                  shapes=np.zeros((2, 1, 1, 3), dtype=np.uint8),
              )
  
          # --> wrong polygons shape
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  [
                      np.tile(
                          coords1_arr[np.newaxis, ...].astype(dt),
                          (1, 1, 10),
                      )
                  ],
                  shapes=np.zeros((1, 1, 1, 3), dtype=np.uint8),
              )
  
          _assert_single_image_expected(
              [coords1_arr[np.newaxis, ...].astype(dt)]
          )
  
      # ----
      # iterable of (x,y)
      # ----
      polygons_norm = normalization.normalize_polygons(
          coords1, shapes=[np.zeros((1, 1, 3), dtype=np.uint8)]
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 1
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
  
      # may only be used for single images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              coords1,
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of Keypoint
      # ----
      polygons_norm = normalization.normalize_polygons(
          coords1_kps, shapes=[np.zeros((1, 1, 3), dtype=np.uint8)]
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 1
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
  
      # may only be used for single images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              coords1_kps,
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of Polygon
      # ----
      polygons_norm = normalization.normalize_polygons(
          [ia.Polygon(coords1), ia.Polygon(coords2)],
          shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
      assert (
          polygons_norm[0].polygons[1].exterior_almost_equals(coords2)
      )
  
      # may only be used for single images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [ia.Polygon(coords1)],
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of PolygonsOnImage
      # ----
      polygons_norm = normalization.normalize_polygons(
          [
              ia.PolygonsOnImage(
                  [ia.Polygon(coords1)], shape=(1, 1, 3)
              ),
              ia.PolygonsOnImage(
                  [ia.Polygon(coords2)], shape=(1, 1, 3)
              ),
          ],
          shapes=None,
      )
      assert isinstance(polygons_norm, list)
  
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 1
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
  
      assert isinstance(polygons_norm[1], ia.PolygonsOnImage)
      assert len(polygons_norm[1].polygons) == 1
      assert (
          polygons_norm[1].polygons[0].exterior_almost_equals(coords2)
      )
  
      # ----
      # iterable of empty iterables
      # ----
      polygons_norm = normalization.normalize_polygons(
          [[]], shapes=[np.zeros((1, 1, 3), dtype=np.uint8)]
      )
      assert polygons_norm is None
  
      # ----
      # iterable of iterable of array
      # ----
      for dt in [
          np.dtype("float32"),
          np.dtype("int16"),
          np.dtype("uint16"),
      ]:
          polygons_norm = normalization.normalize_polygons(
              [[coords1_arr.astype(dt)]],
              shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
          )
          assert isinstance(polygons_norm, list)
          assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
          assert len(polygons_norm[0].polygons) == 1
          assert np.allclose(
              polygons_norm[0].polygons[0].exterior, coords1_arr
          )
  
          polygons_norm = normalization.normalize_polygons(
              [[np.copy(coords1_arr).astype(dt) for _ in sm.xrange(5)]],
              shapes=np.zeros((1, 1, 1, 3), dtype=np.uint8),
          )
          assert isinstance(polygons_norm, list)
          assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
          assert len(polygons_norm[0].polygons) == 5
          assert np.allclose(
              polygons_norm[0].polygons[0].exterior, coords1_arr
          )
  
          # --> polygons for too many images
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  [[coords1_arr.astype(dt)], [coords2_arr.astype(dt)]],
                  shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
              )
  
          # --> too few polygons
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  [[coords1_arr.astype(dt)]],
                  shapes=np.zeros((2, 1, 1, 3), dtype=np.uint8),
              )
  
          # --> wrong polygons shape
          with self.assertRaises(ValueError):
              _polygons_norm = normalization.normalize_polygons(
                  [[np.tile(coords1_arr.astype(dt), (1, 1, 10))]],
                  shapes=np.zeros((1, 1, 1, 3), dtype=np.uint8),
              )
  
          _assert_single_image_expected([[coords1_arr.astype(dt)]])
  
      # ----
      # iterable of iterable of (x,y)
      # ----
      polygons_norm = normalization.normalize_polygons(
          [coords1, coords2],
          shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
      assert (
          polygons_norm[0].polygons[1].exterior_almost_equals(coords2)
      )
  
      # --> images None
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [coords1, coords2], shapes=None
          )
  
      # --> different number of images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [coords1, coords2],
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of iterable of Keypoint
      # ----
      polygons_norm = normalization.normalize_polygons(
          [coords1_kps, coords2_kps],
          shapes=[np.zeros((1, 1, 3), dtype=np.uint8)],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
      assert (
          polygons_norm[0].polygons[1].exterior_almost_equals(coords2)
      )
  
      # --> images None
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [coords1_kps, coords2_kps], shapes=None
          )
  
      # --> different number of images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [coords1_kps, coords2_kps],
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of iterable of Polygon
      # ----
      polygons_norm = normalization.normalize_polygons(
          [
              [ia.Polygon(coords1), ia.Polygon(coords2)],
              [ia.Polygon(coords3), ia.Polygon(coords4)],
          ],
          shapes=[
              np.zeros((1, 1, 3), dtype=np.uint8),
              np.zeros((1, 1, 3), dtype=np.uint8),
          ],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
      assert isinstance(polygons_norm[1], ia.PolygonsOnImage)
  
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
      assert (
          polygons_norm[0].polygons[1].exterior_almost_equals(coords2)
      )
  
      assert len(polygons_norm[1].polygons) == 2
      assert (
          polygons_norm[1].polygons[0].exterior_almost_equals(coords3)
      )
      assert (
          polygons_norm[1].polygons[1].exterior_almost_equals(coords4)
      )
  
      # --> images None
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [
                  [ia.Polygon(coords1), ia.Polygon(coords2)],
                  [ia.Polygon(coords3), ia.Polygon(coords4)],
              ],
              shapes=None,
          )
  
      # --> different number of images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [
                  [ia.Polygon(coords1), ia.Polygon(coords2)],
                  [ia.Polygon(coords3), ia.Polygon(coords4)],
              ],
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of iterable of empty iterable
      # ----
      polygons_norm = normalization.normalize_polygons(
          [[[]]], shapes=[np.zeros((1, 1, 3), dtype=np.uint8)]
      )
      assert polygons_norm is None
  
      # ----
      # iterable of iterable of iterable of (x,y)
      # ----
      polygons_norm = normalization.normalize_polygons(
          [[coords1, coords2], [coords3, coords4]],
          shapes=[
              np.zeros((1, 1, 3), dtype=np.uint8),
              np.zeros((1, 1, 3), dtype=np.uint8),
          ],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
  
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
      assert (
          polygons_norm[0].polygons[1].exterior_almost_equals(coords2)
      )
  
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[1].polygons[0].exterior_almost_equals(coords3)
      )
      assert (
          polygons_norm[1].polygons[1].exterior_almost_equals(coords4)
      )
  
      # --> images None
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [[coords1, coords2]], shapes=None
          )
  
      # --> different number of images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [[coords1, coords2], [coords3]],
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
      # ----
      # iterable of iterable of iterable of Keypoint
      # ----
      polygons_norm = normalization.normalize_polygons(
          [[coords1_kps, coords2_kps], [coords3_kps, coords4_kps]],
          shapes=[
              np.zeros((1, 1, 3), dtype=np.uint8),
              np.zeros((1, 1, 3), dtype=np.uint8),
          ],
      )
      assert isinstance(polygons_norm, list)
      assert isinstance(polygons_norm[0], ia.PolygonsOnImage)
  
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[0].polygons[0].exterior_almost_equals(coords1)
      )
      assert (
          polygons_norm[0].polygons[1].exterior_almost_equals(coords2)
      )
  
      assert len(polygons_norm[0].polygons) == 2
      assert (
          polygons_norm[1].polygons[0].exterior_almost_equals(coords3)
      )
      assert (
          polygons_norm[1].polygons[1].exterior_almost_equals(coords4)
      )
  
      # --> images None
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [[coords1_kps, coords2_kps]], shapes=None
          )
  
      # --> different number of images
      with self.assertRaises(ValueError):
          _polygons_norm = normalization.normalize_polygons(
              [[coords1_kps, coords2_kps], [coords3_kps]],
              shapes=[
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
                  np.zeros((1, 1, 3), dtype=np.uint8),
              ],
          )
  
imports: from __future__ import print_function, division, absolute_import
  import sys
  import numpy
  import six.moves
  import imgaug
  import imgaug.augmentables.normalization
  from imgaug.testutils import reseed
label_desc: Use image node_id 14 for calling the TestNormalization obj's underlying member method code with example usage: obj.test_normalize_polygons() without return types