Exemplo n.º 1
0
    def test_create_region(self):
        test_regions = []
        test_regions.append(Region([0], [5]))
        test_regions.append(Region([0, 0], [5, 5]))
        test_regions.append(Region([0, 5, 0], [5, 0, 5]))

        for i, region in enumerate(test_regions):
            #print(region)
            self.assertEqual(region.dimension, i + 1)
            self.assertEqual(region.dimension, len(region.factors))
            self.assertEqual(region.dimension, len(region.lower))
            self.assertEqual(region.dimension, len(region.upper))
            self.assertTrue(all([d == Interval(0, 5) for d in region.factors]))
            self.assertTrue(all([d == 0 for d in region.lower]))
            self.assertTrue(all([d == 5 for d in region.upper]))
            self.assertTrue(
                all([
                    region.lower[i] <= region.upper[i]
                    for i in range(region.dimension)
                ]))
            self.assertTrue(
                all([
                    region.lower[i] == region[i].lower
                    for i in range(region.dimension)
                ]))
            self.assertTrue(
                all([
                    region.upper[i] == region[i].upper
                    for i in range(region.dimension)
                ]))
Exemplo n.º 2
0
    def test_region_from_dict(self):
        test_region = Region([10] * 3, [50] * 3)
        objects = []
        objects.append({"lower": [10] * 3, "upper": [50] * 3})
        objects.append({"factors": [{"lower": 10, "upper": 50}] * 3})

        for object in objects:
            #print(f'{object}')
            self.assertEqual(test_region, Region.from_dict(object))
Exemplo n.º 3
0
 def test_region_project(self):
     dimlimit = 7
     test_interval = Interval(1, 5)
     region = Region([-5, 5, 0], [15, 10, 50])
     for d in range(1, dimlimit):
         new_region = region.project(d, test_interval)
         #print(f'{new_region}')
         self.assertEqual(new_region.dimension, d)
         for i in range(0, d):
             interval = region[i] if i < region.dimension else test_interval
             self.assertEqual(new_region[i], interval)
Exemplo n.º 4
0
 def test_region_from_intersect(self):
     regions = [Region([-i] * 2, [i] * 2) for i in range(5, 1, -1)]
     for i in range(1, len(regions)):
         expected_intersect = reduce(lambda a, b: a.get_intersection(b),
                                     regions[0:i + 1])
         intersect = Region.from_intersection(regions[0:i + 1])
         # print(f'Expected: {expected_intersect}')
         # print(f'Expected: {expected_intersect.originals}')
         # print(f'Actual: {intersect}')
         # print(f'Actual: {intersect.originals}')
         self.assertListEqual(expected_intersect.originals,
                              intersect.originals)
Exemplo n.º 5
0
 def setUp(self):
     self.test_regions = []
     self.test_regions.append(Region([0, 0], [5, 5]))
     self.test_regions.append(Region([2, 2], [5, 10]))
     self.test_regions.append(Region([1, 5], [3, 7]))
     self.test_regions.append(Region([-5, 5], [1, 7]))
     self.test_regions.append(Region([-5, 5], [2, 7]))
     self.overlaps = [
         [True, True, False, False, False],  #  [0, 0], [5, 5]
         [True, True, True, False, False],  #  [2, 2], [5, 10]
         [False, True, True, False, True],  #  [1, 5], [3, 7]
         [False, False, False, True, True],  # [-5, 5], [1, 7]
         [False, False, True, True, True]  # [-5, 5], [2, 7]
     ]
Exemplo n.º 6
0
    def test_init_generator(self):

        gen = RegionGenerator()
        self.assertEqual(gen.dimension, 2)
        gen = RegionGenerator(Interval(0, 1000))
        self.assertEqual(gen.dimension, 1)
        gen = RegionGenerator(Region([0, 0], [1000, 1000]))
        self.assertEqual(gen.dimension, 2)

        gen = RegionGenerator(sizepc=0.5)
        self.assertEqual(gen.sizepc.dimension, 2)
        gen = RegionGenerator(dimension=1, sizepc=Interval(0, 0.5))
        self.assertEqual(gen.sizepc.upper, [0.5])
        gen = RegionGenerator(sizepc=Region([0, 0], [0.5, 0.5]))
        self.assertEqual(gen.sizepc.upper, [0.5, 0.5])
Exemplo n.º 7
0
    def test_region_from_text(self):
        test_region = Region([10] * 3, [50] * 3)
        texts = []
        texts.append('{"lower": [10,10,10], "upper": [50,50,50]}')
        texts.append(
            '{"lower": [10,10,10], "upper": [50,50,50], "originals": ["1","2"]}'
        )
        texts.append(
            '{"factors":[{"lower": 10, "upper": 50},{"lower": 10, "upper": 50},{"lower": 10, "upper": 50}]}'
        )

        for text in texts:
            #print(f'text="{text[0]}"' if "'" in text else f"text='{text[0]}'")
            #print(f'format={text[1]}')
            self.assertEqual(test_region, Region.from_JSON(text))
Exemplo n.º 8
0
 def setUp(self):
     self.test_regions = []
     self.test_regions.append(Region([0, 0], [5, 5]))
     self.test_regions.append(Region([2, 2], [5, 10]))
     self.test_regions.append(Region([1, 5], [3, 7]))
     self.test_regions.append(Region([3, 5], [1, 7]))
     self.test_regions.append(Region([4, 8], [2, 7]))
     self.overlaps = [
         [True, True, False, False, False],  #  [0, 0], [5, 5]
         [True, True, True, False, False],  #  [2, 2], [5, 10]
         [False, True, True, False, True],  #  [1, 5], [3, 7]
         [False, False, False, True, True],  # [-5, 5], [1, 7]
         [False, False, True, True, True]  # [-5, 5], [2, 7]
     ]
     self.test_regionset = RegionSet(dimension=2)
     for r in self.test_regions:
         self.test_regionset.add(r)
Exemplo n.º 9
0
 def test_region_from_interval(self):
     ndimens = 5
     interval = Interval(-5, 5)
     for d in range(1, ndimens):
         region = Region.from_interval(interval, d)
         #print(f'{region}')
         self.assertEqual(region.dimension, d)
         for dimen in region.factors:
             self.assertEqual(dimen, interval)
Exemplo n.º 10
0
 def test_region_contains(self):
     region = Region([-5, 0], [15, 10])
     self.assertTrue(region.lower in region)
     self.assertTrue(region.upper in region)
     self.assertTrue(region.midpoint in region)
     self.assertTrue([v + 0.1 for v in region.lower] in region)
     self.assertTrue([v - 0.1 for v in region.upper] in region)
     self.assertTrue([region.lower[0] + 0.1, region.lower[1]] in region)
     self.assertTrue([region.lower[0], region.lower[1] + 0.1] in region)
     self.assertTrue([region.upper[0] - 0.1, region.upper[1]] in region)
     self.assertTrue([region.upper[0], region.upper[1] - 0.1] in region)
     self.assertFalse(region.contains(region.lower, inc_lower=False))
     self.assertFalse(region.contains(region.upper, inc_upper=False))
     self.assertFalse([v - 0.1 for v in region.lower] in region)
     self.assertFalse([v + 0.1 for v in region.upper] in region)
     self.assertFalse([region.lower[0] - 0.1, region.lower[1]] in region)
     self.assertFalse([region.lower[0], region.lower[1] - 0.1] in region)
     self.assertFalse([region.upper[0] + 0.1, region.upper[1]] in region)
     self.assertFalse([region.upper[0], region.upper[1] + 0.1] in region)
Exemplo n.º 11
0
    def test_store_region_set(self):

        gen = RegionGenerator()
        gen.store_regionset(100, "test.json")

        self.assertTrue(os.path.exists("test.json"))
        with open("test.json") as file:
            regionset = json.load(file)

            self.assertTrue('bounds' in regionset)
            self.assertTrue(len(regionset['regions']) == 100)
            for r in regionset['regions']:
                region = Region.from_dict(r)
                self.assertTrue(isinstance(region, Region))
                self.assertTrue(gen.bounds.encloses(region))
Exemplo n.º 12
0
 def test_region_from_intervals(self):
     ndimens = 5
     base_interval = Interval(1, 5)
     intervals = [
         Interval(base_interval.lower + d, base_interval.upper + d)
         for d in range(ndimens)
     ]
     region = Region.from_intervals(intervals)
     #print(f'{region}')
     self.assertEqual(region.dimension, ndimens)
     self.assertEqual(region.lower,
                      [base_interval.lower + d for d in range(ndimens)])
     self.assertEqual(region.upper,
                      [base_interval.upper + d for d in range(ndimens)])
     for d, dimension in enumerate(region.factors):
         self.assertEqual(dimension, intervals[d])
Exemplo n.º 13
0
    def test_region_getsetitem(self):
        data = {
            'data': 'value',
            'datalist': ['list', 'of', 'items'],
            'dataprop': 'dataprop'
        }
        intervals = [Interval(0, 10)] * 3
        region = Region.from_intervals(intervals, dataprop=data['dataprop'])

        def check_region(region: Region, intervals: List[Interval]):
            self.assertTrue(
                all([
                    region[i] == interval
                    for i, interval in enumerate(intervals)
                ]))
            self.assertEqual(list(map(lambda i: i.lower, intervals)),
                             region.lower)
            self.assertEqual(list(map(lambda i: i.upper, intervals)),
                             region.upper)

        check_region(region, intervals)
        #print(f'{region}')
        #print(f'{region.data}')
        #print(f'{asdict(region)}')
        intervals = [
            Interval(interval.lower, interval.upper + i)
            for i, interval in enumerate(intervals)
        ]
        for i, interval in enumerate(intervals):
            region[i] = interval
        check_region(region, intervals)

        region['data'] = data['data']
        region['datalist'] = data['datalist']
        #print(f'{region}')
        #print(f'{region.data}')
        #print(f'{asdict(region)}')
        self.assertEqual(data['data'], region['data'])
        self.assertEqual(data['datalist'], region['datalist'])
        self.assertEqual(data['dataprop'], region['dataprop'])
        self.assertDictEqual(data, region.data)
Exemplo n.º 14
0
    def test_region_encloses(self):
        region = Region([-5, 5], [0, 10])
        test_regions = []
        test_regions.append(Region([-5, 5], [0, 10]))
        test_regions.append(Region([-6, 4], [1, 11]))
        test_regions.append(Region([-4, 6], [-1, 9]))
        test_regions.append(Region([-4, 6], [1, 9]))
        test_regions.append(Region([-6, 5], [0, 10]))
        test_regions.append(Region([-2, 7], [-2, 7]))

        for subregion in test_regions:
            comparsion = all([
                region.lower[i] <= subregion.lower[i] <= subregion.upper[i] <=
                region.upper[i] for i in range(region.dimension)
            ])
            #print(f'{subregion} in\n{region}:')
            #print(f'  expect={comparsion}')
            #print(f'  actual={subregion in region}')
            self.assertEqual(subregion in region, comparsion)
Exemplo n.º 15
0
    def test_region_equality(self):
        test_regions = []
        test_regions.append(Region([-5, 0], [15, 10]))
        test_regions.append(Region([-5, 1], [15, 10]))
        test_regions.append(Region([-6, 0], [15, 10]))
        test_regions.append(Region([-5, 0], [14, 10]))
        test_regions.append(Region([-5, 0], [15, 11]))
        match_region = Region([-5, 0], [15, 10])

        for region in test_regions:
            self.assertNotEqual(region.id, match_region.id)
            if all([region.lower[i] == match_region.lower[i] and \
                    region.upper[i] == match_region.upper[i] for i in range(match_region.dimension)]):
                self.assertListEqual(region.lower, match_region.lower)
                self.assertListEqual(region.upper, match_region.upper)
                self.assertEqual(region, match_region)
Exemplo n.º 16
0
 def test_region_dimension_mismatch(self):
     with self.assertRaises(AssertionError):
         Region([0, 0, 0], [10, 10])