import unittest

import numpy as np

from chainer import testing
from chainer.testing import attr

from chainercv.datasets import coco_instance_segmentation_label_names
from chainercv.datasets import COCOInstanceSegmentationDataset
from chainercv.utils import assert_is_instance_segmentation_dataset


@testing.parameterize(*testing.product({
    'split': ['train', 'val', 'minival', 'valminusminival'],
    'use_crowded': [False, True],
    'return_crowded': [False, True],
    'return_area': [False, True]
}))
class TestCOCOInstanceSegmentationDataset(unittest.TestCase):

    def setUp(self):
        self.dataset = COCOInstanceSegmentationDataset(
            split=self.split,
            use_crowded=self.use_crowded, return_crowded=self.return_crowded,
            return_area=self.return_area)

    @attr.slow
    def test_coco_instance_segmentation_dataset(self):
        assert_is_instance_segmentation_dataset(
            self.dataset,
            len(coco_instance_segmentation_label_names),
            n_example=10)

        if self.return_crowded:
            for _ in range(10):
                i = np.random.randint(0, len(self.dataset))
                _, bbox, _, _, crowded = self.dataset[i][:5]
                self.assertIsInstance(crowded, np.ndarray)
                self.assertEqual(crowded.dtype, np.bool)
                self.assertEqual(crowded.shape, (bbox.shape[0],))

                if not self.use_crowded:
                    np.testing.assert_equal(crowded, 0)

        if self.return_area:
            for _ in range(10):
                i = np.random.randint(0, len(self.dataset))
                example = self.dataset[i]
                area = example[-1]
                self.assertIsInstance(area, np.ndarray)
                self.assertEqual(area.dtype, np.float32)


testing.run_module(__name__, __file__)

@@ -1048,23 +1048,20 @@
 .return_
-crowded
+area
 :%0A      
@@ -1169,27 +1169,21 @@
  _, 
-bbox, _, _, crowded
+mask, _, area
  = s
@@ -1202,9 +1202,9 @@
 i%5D%5B:
-5
+4
 %5D%0A  
@@ -1239,23 +1239,20 @@
 nstance(
-crowded
+area
 , np.nda
@@ -1290,23 +1290,20 @@
 rtEqual(
-crowded
+area
 .dtype, 
@@ -1309,12 +1309,15 @@
  np.
-bool
+float32
 )%0A  
@@ -1347,23 +1347,20 @@
 rtEqual(
-crowded
+area
 .shape, 
@@ -1364,12 +1364,12 @@
 e, (
-bbox
+mask
 .sha
@@ -1390,125 +1390,30 @@
     
-  
-      if not self.use_crowded:%0A                    np.testing.assert_equal(crowded, 0)%0A%0A        if self.return_area
+if self.return_crowded
 :%0A  
@@ -1560,20 +1560,58 @@
         
-area
+crowded = example%5B-1%5D%0A                mask
  = examp
@@ -1605,33 +1605,32 @@
  mask = example%5B
--
 1%5D%0A             
@@ -1646,36 +1646,39 @@
 ssertIsInstance(
-area
+crowded
 , np.ndarray)%0A  
@@ -1700,36 +1700,39 @@
 elf.assertEqual(
-area
+crowded
 .dtype, np.float
@@ -1726,23 +1726,184 @@
 ype, np.
-float32
+bool)%0A                self.assertEqual(crowded.shape, (mask.shape%5B0%5D,))%0A%0A                if not self.use_crowded:%0A                    np.testing.assert_equal(crowded, 0
 )%0A%0A%0Atest

fix test_coco_instance_segmentation_dataset