Exemplo n.º 1
0
    def test_region_from_object(self):
        test_region = Region([10] * 3, [50] * 3)
        objects = []
        objects.append({"lower": [10] * 3, "upper": [50] * 3})
        objects.append({"dimension": 3, "lower": 10, "upper": [50] * 3})
        objects.append({"dimension": 3, "lower": 10, "upper": 50})
        objects.append({"dimension": 3, "lower": [10] * 3, "upper": 50})
        objects.append({"dimension": 3, "interval": [10, 50]})
        objects.append({"dimension": 3, "interval": (10, 50)})
        objects.append({
            "dimension": 3,
            "interval": {
                "lower": 10,
                "upper": 50
            }
        })
        objects.append({"dimensions": [[10, 50]] * 3})
        objects.append({"dimensions": [(10, 50)] * 3})
        objects.append({"dimensions": [{"lower": 10, "upper": 50}] * 3})
        objects.append([[10, 50]] * 3)
        objects.append([(10, 50)] * 3)
        objects.append([{"lower": 10, "upper": 50}] * 3)
        objects.append((3, [10, 50]))
        objects.append((3, (10, 50)))
        objects.append((3, {"lower": 10, "upper": 50}))

        for object in objects:
            #print(f'{object}')
            self.assertEqual(test_region, Region.from_object(object))
Exemplo n.º 2
0
    def test_regionset_tofrom_output(self):
        nregions = 10
        bounds = Region([0] * 2, [100] * 2)
        sizepc = Region([0] * 2, [0.5] * 2)
        regionset = RegionSet.from_random(nregions,
                                          bounds,
                                          sizepc=sizepc,
                                          precision=1)

        with StringIO() as output:
            RegionSet.to_output(regionset, output, options={'compact': True})
            before = output.getvalue()
            #print(before)
            output.seek(0)
            newregionset = RegionSet.from_source(output, 'json')
            self._test_regionset(newregionset, nregions, bounds, regionset)

            output.truncate(0)
            output.seek(0)
            RegionSet.to_output(newregionset,
                                output,
                                options={'compact': True})
            after = output.getvalue()
            #print(after)
            self.assertEqual(before, after)
Exemplo n.º 3
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]))
Exemplo n.º 4
0
    def test_regionsweep_random(self):
        regionset = RegionSet.from_random(30,
                                          Region([0] * 3, [100] * 3),
                                          sizepc=Region([0] * 3, [0.5] * 3),
                                          precision=0)
        actuals = []
        #for region in regionset: print(f'{region}')
        for i in range(regionset.dimension):
            #print(f'Dimension: {i}')
            expect = regionset.overlaps(i)
            actual = self._evaluate_regionsweep(regionset, i)
            #for pair in expect: print(f'Expect {i}: {pair[0].id} {pair[1].id}')
            #for pair in actual: print(f'Actual {i}: {pair[0].id} {pair[1].id}')
            for pair in expect:
                #passed = "Passed" if pair in actual else "Failed"
                #print(f'{passed} {i}: {pair[0].id} {pair[1].id}')
                self.assertTrue(pair in actual)
            self.assertEqual(len(expect), len(actual))
            actuals.append(actual)

        self.assertTrue(all([len(actual) for actual in actuals]))
        for pair in actuals[0]:
            for d in range(1, regionset.dimension):
                self.assertTrue(pair in actuals[d]
                                or (pair[1], pair[0]) in actuals[d])
Exemplo n.º 5
0
    def construct_regions(self, experiment: Experiment, nregions: int,
                          sizepc: float, dimension: int) -> RegionSet:
        """
    Construct a random collection of Regions for the given Experiment.

    Args:
      experiment:   The experiment for this set of Regions.
      nregions:     The number of Regions in this set.
      sizepc:       The maximum size of Regions as a percent
                    of the bounding Region.
      dimension:    The dimensionality of Regions.

    Returns:
      The newly constructed, randomly generated
      collection of Regions.
    """
        bounds = Region([0] * dimension,
                        [experiment.data['maxbound']] * dimension)
        sizepr = Region([0] * dimension, [sizepc] * dimension)
        regions = RegionSet.from_random(nregions, bounds=bounds, sizepc=sizepr)

        self.output_log(experiment, {
            'nregions': nregions,
            'sizepc': sizepc,
            'dimension': dimension
        })

        return regions
Exemplo n.º 6
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]}', 'json'))
        texts.append(('{"dimension": 3, "lower": 10, "upper": 50}', 'literal'))
        texts.append(('{"dimension": 3, "lower": 10, "upper": 50}', 'json'))
        texts.append(('{"dimension": 3, "interval": [10, 50]}', 'json'))
        texts.append(('{"dimension": 3, "interval": [10, 50]}', 'literal'))
        texts.append(('{"dimension": 3, "interval": (10, 50)}', 'literal'))
        texts.append(('[[10, 50],[10, 50],[10, 50]]', 'json'))
        texts.append(('[[10, 50],[10, 50],[10, 50]]', 'literal'))
        texts.append(('[(10, 50),(10, 50),(10, 50)]', 'literal'))
        texts.append((
            '[{"lower": 10, "upper": 50},{"lower": 10, "upper": 50},{"lower": 10, "upper": 50}]',
            'json'))
        texts.append((
            '[{"lower": 10, "upper": 50},{"lower": 10, "upper": 50},{"lower": 10, "upper": 50}]',
            'literal'))
        texts.append(('(3, [10, 50])', 'literal'))
        texts.append(('(3, (10, 50))', 'literal'))

        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_text(*text))
Exemplo n.º 7
0
  def test_regiontimeln_ordering(self):
    regions = RegionSet(dimension=2)
    regions.add(Region([0, 0], [3, 5], 'A'))
    regions.add(Region([3, 1], [5, 5], 'B'))
    regions.add(Region([2, 5], [6, 5], 'C'))
    bbox = regions.bbox
    oracle = [
      [("Init" , 0, bbox),
       ("Begin", 0, regions[0]), ("Begin", 2, regions[2]),
       ("End"  , 3, regions[0]), ("Begin", 3, regions[1]),
       ("End"  , 5, regions[1]), ("End"  , 6, regions[2]),
       ("Done" , 6, bbox)],
      [("Init" , 0, bbox),
       ("Begin", 0, regions[0]), ("Begin", 1, regions[1]),
       ("End"  , 5, regions[0]), ("End"  , 5, regions[1]),
       ("Begin", 5, regions[2]), ("End"  , 5, regions[2]),
       ("Done" , 5, bbox)]
    ]

    for d in range(regions.dimension):
      for i, event in enumerate(regions.timeline.events(d)):
        #print(f'{d},{i}: {event}')
        self.assertEqual(event.kind, RegionEvtKind[oracle[d][i][0]])
        self.assertEqual(event.when, float(oracle[d][i][1]))
        if event.kind == RegionEvtKind.Begin or event.kind == RegionEvtKind.End:
          self.assertIs(event.context, oracle[d][i][2])
          self.assertTrue(-1 <= event.order <= 1)
        elif event.kind == RegionEvtKind.Init:
          self.assertEqual(i, 0)
          self.assertEqual(event.order, -2)
        else:
          self.assertEqual(event.order, 2)
Exemplo n.º 8
0
 def test_create_regionset(self):
     bounds = Region([0, 0], [100, 100])
     regionset = RegionSet(bounds=bounds)
     regions = bounds.random_regions(10)
     for region in regions:
         regionset.add(region)
     self._test_regionset(regionset, len(regions), bounds, regions)
Exemplo n.º 9
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.dimensions))
            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.dimensions]))
            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.º 10
0
    def test_regionset_subset(self):
        nregions = 50
        bounds = Region([0] * 2, [10] * 2)
        sizepc = Region([0] * 2, [0.5] * 2)
        regionset = RegionSet.from_random(nregions,
                                          bounds,
                                          sizepc=sizepc,
                                          precision=1)
        subset = ['A', 'C', 'E', 'G', 'I', 'K'
                  ] + [regionset[r] for r in ['AA', 'P', 'Q', 'R']]
        subsetted = regionset.subset(subset)

        self.assertEqual(regionset.bounds, subsetted.bounds)
        self.assertGreaterEqual(len(regionset), len(subsetted))
        self.assertEqual(len(subset), len(subsetted))
        for r in subset:
            if isinstance(r, str):
                #print(f'{r}: {r in subsetted} {subsetted[r]}')
                self.assertIn(r, subsetted)
                self.assertIs(regionset[r], subsetted[r])
            else:
                #print(f'{r.id}: {r in subsetted} {r}')
                self.assertIsInstance(r, Region)
                self.assertIn(r, subsetted)
                self.assertIs(regionset[r.id], subsetted[r.id])
Exemplo n.º 11
0
 def test_regionset_from_random(self):
     nregions = 50
     bounds = Region([0] * 2, [10] * 2)
     sizepc = Region([0] * 2, [0.5] * 2)
     regionset = RegionSet.from_random(nregions,
                                       bounds,
                                       sizepc=sizepc,
                                       precision=1)
     self._test_regionset(regionset, nregions, bounds, regionset)
Exemplo n.º 12
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.º 13
0
 def _test_regionset(self, regionset: RegionSet, nregions: int,
                     bounds: Region, regions: Iterable[Region]):
     #print(f'{regionset}')
     self.assertEqual(regionset.length, nregions)
     self.assertTrue(bounds.encloses(regionset.minbounds))
     for i, region in enumerate(regions):
         #print(f'{region}')
         self.assertEqual(region, regionset[i])
         self.assertEqual(region, regionset[region.id])
         self.assertTrue(region in regionset)
         self.assertTrue(region.id in regionset)
         self.assertTrue(bounds.encloses(region))
Exemplo n.º 14
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.º 15
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.intersect(b, 'aggregate'), regions[0:i + 1])
         intersect = Region.from_intersect(regions[0:i + 1], True)
         #print(f'Expected: {expected_intersect}')
         #print(f'Expected["intersect"]: {expected_intersect["intersect"]}')
         #print(f'Actual: {intersect}')
         #print(f'Actual["intersect"]: {intersect["intersect"]}')
         self.assertEqual(regions[i], intersect)
         self.assertListEqual(regions[0:i + 1], intersect['intersect'])
         self.assertEqual(expected_intersect, intersect)
         self.assertListEqual(expected_intersect['intersect'],
                              intersect['intersect'])
Exemplo n.º 16
0
    def test_regionsweep_simple(self):
        regionset = RegionSet(dimension=2)
        regionset.add(Region([0, 0], [3, 5]))
        regionset.add(Region([3, 1], [5, 5]))
        regionset.add(Region([2, 4], [6, 6]))

        for i in range(regionset.dimension):
            expect = regionset.overlaps(i)
            actual = self._evaluate_regionsweep(regionset, i)
            #for pair in expect: print(f'Expect {i}:\t{pair[0]}\n\t{pair[1]}')
            #for pair in actual: print(f'Actual {i}:\t{pair[0]}\n\t{pair[1]}')
            for pair in expect:
                #passed = "Passed" if pair in actual else "Failed"
                #print(f'{passed} {i}: {pair[0]} {pair[1]}')
                self.assertTrue(pair in actual)
            self.assertEqual(len(expect), len(actual))
Exemplo n.º 17
0
    def on_completed(self):
        """
    The Event handler when no more Events.
    Subscription completed and finalize the overlaps.

    The overlaps that have same 'count' as the Region dimensionality, replace
    'count' with Region intersection between the two Regions for that edge.
    The overlaps that have 'count' that is less than the Region
    dimensionality, remove the edge.

    Overrides:
      Subscriber.on_completed
    """
        G = self.G
        for a, b, intersect, data in list(G.overlaps):
            # Buffer G.overlaps into a list to prevent 'dictionary changed size
            # during iteration' runtime error.
            if intersect is not None:
                continue

            if 'dimensions' in data:
                dim = data['dimensions']
                assert isinstance(dim, List) and len(dim) == self.dimension
                if all([isinstance(d, Interval) for d in dim]):
                    intersect = Region.from_intervals(dim)
                    intersect['intersect'] = [G.region(a), G.region(b)]
                    G.put_overlap((a, b), intersect=intersect)
                    continue

            # 'dimensions' not in data OR any([d is None for d in dim])
            del G[a, b]
Exemplo n.º 18
0
    def test_region_linked_union(self):
        base_region = Region([0] * 2, [1] * 2)
        regions = [Region([0, i], [1, i + 1]) for i in range(1, 5)]
        ref_region = base_region
        agg_region = base_region
        agg_regions = [base_region]

        for region in regions:
            old_region = ref_region
            agg_regions.append(region)
            ref_region = ref_region.union(region, 'reference')
            agg_region = agg_region.union(region, 'aggregate')
            #print(f'{asdict(ref_region)}')
            #print(f'{asdict(agg_region)}')
            self.assertListEqual([old_region, region], ref_region['union'])
            self.assertListEqual(agg_regions, agg_region['union'])
Exemplo n.º 19
0
    def test_region_linked_intersect(self):
        base_region = Region([0] * 2, [10] * 2)
        regions = [Region([0] * 2, [i] * 2) for i in range(5, 1, -1)]
        ref_region = base_region
        agg_region = base_region
        agg_regions = [base_region]

        for region in regions:
            old_region = ref_region
            agg_regions.append(region)
            ref_region = ref_region.intersect(region, 'reference')
            agg_region = agg_region.intersect(region, 'aggregate')
            #print(f'{asdict(ref_region)}')
            #print(f'{asdict(agg_region)}')
            self.assertListEqual([old_region, region], ref_region['intersect'])
            self.assertListEqual(agg_regions, agg_region['intersect'])
Exemplo n.º 20
0
    def generate(cls, output: FileIO, kind: str, nregions: int, dimension: int,
                 **kwargs):
        """
    Randomly generate a new collection or intersection graph of Regions. \f

    Args:
      output:
        The file to save the newly generated set
        or intersection graph of Regions.

      kind:       The output object type.
      nregions:   The number of Regions to be generated.
      dimension:  The number of dimensions for each Region.
      kwargs:     Additional arguments.

    Keyword Args:
      id:
        The unique identifier for the output.
      bounds:
        The minimum + maximum bounding value for
        each dimension that all Region must be
        enclosed within.
      sizepc:
        The minimum + maximum size of each Region
        as a percent of the bounding Region that
        all Region must be enclosed within.
      precision:
        The number of digits after the decimal pt
        in each Region's lower + upper values.
      colored:
        Boolean flag for whether to color code the
        connected Regions and save the associated
        colors in each Region's data properties.
    """
        kwargs['bounds'] = Region.from_object((dimension, kwargs['bounds']))
        kwargs['sizepc'] = Region.from_object((dimension, kwargs['sizepc']))

        colored = kwargs.pop('colored', False)
        regions = RegionSet.from_random(nregions, **kwargs)
        bundle = cls.bundle(regions)

        if colored:
            cls.colorize_components(bundle)

        cls.write(output, cls.unbundle(bundle, kind))
Exemplo n.º 21
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.dimensions:
             self.assertEqual(dimen, interval)
Exemplo n.º 22
0
 def test_region_random_points(self):
     region2d = Region([-5, 0], [15, 10])
     region3d = Region([-5, 0, 0], [15, 10, 50])
     points2d = region2d.random_points(5)
     points3d = region3d.random_points(5)
     #print(f'{region2d}: random={points2d}')
     #print(f'{region3d}: random={points3d}')
     for point in points2d:
         self.assertTrue(region2d.contains(list(point), inc_upper=False))
     for point in points3d:
         self.assertTrue(region3d.contains(list(point), inc_upper=False))
Exemplo n.º 23
0
    def test_regionset_iteration(self):
        regionset = RegionSet.from_random(100,
                                          Region([0] * 2, [10] * 2),
                                          sizepc=Region([0] * 2, [0.5] * 2))

        for region in regionset:
            self.assertIsInstance(region, Region)
            self.assertIn(region, regionset)

        for region in regionset.keys():
            self.assertIsInstance(region, str)
            self.assertIn(region, regionset)

        for rid, region in regionset.items():
            self.assertIsInstance(rid, str)
            self.assertIsInstance(region, Region)
            self.assertIn(rid, regionset)
            self.assertIn(region, regionset)
            self.assertEqual(rid, region.id)
Exemplo n.º 24
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.º 25
0
    def get_edge_color(r: Region):
        a, b = r['intersect']
        r_color = r.getdata('color')
        a_color = a.getdata('color')
        b_color = b.getdata('color')

        if r_color:
            return r_color
        if a_color and a_color == b_color:
            return a_color
        else:
            return black
Exemplo n.º 26
0
    def test_regionset_merge(self):
        nregions = 50
        sizepc = Region([0] * 2, [0.5] * 2)
        first = RegionSet.from_random(nregions,
                                      Region([0] * 2, [10] * 2),
                                      sizepc=sizepc,
                                      precision=1)
        second = RegionSet.from_random(nregions,
                                       Region([0] * 2, [100] * 2),
                                       sizepc=sizepc,
                                       precision=1)
        third = RegionSet(dimension=2)
        third.streamadd([
            Region([-1] * 2, [10] * 2),
            Region([-10, 0], [0, 100]),
            Region([-100, -10], [10, 10])
        ])

        merged = RegionSet.from_merge([first, second, third])
        regionkeys = []

        for region in merged:
            regionkeys.append(region.id)
        for regions in [first, second, third]:
            self.assertTrue(merged.bounds.encloses(regions.bbox))

        self.assertEqual(len(first) + len(second) + len(third), len(merged))
        self.assertEqual(len(regionkeys), len(set(regionkeys)))
Exemplo n.º 27
0
  def test_regiontimeln_event_create(self):
    self.assertEqual(RegionEvtKind.Begin, RegionEvtKind['Begin'])
    self.assertEqual(RegionEvtKind.End,   RegionEvtKind['End'])
    
    region = Region([0]*2, [1]*2)
    lower = [RegionEvent(RegionEvtKind.Begin, region, i) for i in range(region.dimension)]
    upper = [RegionEvent(RegionEvtKind.End,   region, i) for i in range(region.dimension)]

    self.assertTrue(all([RegionEvtKind.Begin == e.kind for e in lower]))
    self.assertTrue(all([RegionEvtKind.End   == e.kind for e in upper]))
    self.assertTrue(all([e.when == region[i].lower for i, e in enumerate(lower)]))
    self.assertTrue(all([e.when == region[i].upper for i, e in enumerate(upper)]))
    self.assertTrue(all([e.context is region for e in upper + lower]))
Exemplo n.º 28
0
    def setUp(self):
        definedset = RegionSet(dimension=2)
        definedset.streamadd([
            Region([0, 0], [5, 5], 'A'),
            Region([2, 2], [5, 10], 'B'),
            Region([1, 5], [3, 7], 'C'),
            Region([3, 3], [4, 7], 'D'),
            Region([-5, 5], [1, 7], 'E'),
            Region([-5, 5], [2, 7], 'F'),
            Region([3, 4], [5, 6], 'G')
        ])

        bounds = Region([0] * 2, [1000] * 2)

        self.regions = {'definedset': definedset}
        self.subsets = {
            'definedset': {
                'G1': ['A', 'C', 'E', 'G'],
                'G2': ['B', 'D', 'F'],
                'G3': ['A', 'B', 'C', 'D']
            }
        }

        for nregions in [pow(10, n) for n in range(1, 4)]:
            for sizepc in [0.01, 0.05, 0.1]:
                name = f'{nregions},{sizepc:.2f}'
                sizepcr = Region([0] * 2, [sizepc] * 2)
                regions = RegionSet.from_random(nregions,
                                                bounds,
                                                sizepc=sizepcr,
                                                precision=1)

                self.regions[name] = regions
                self.subsets[name] = {}

                for subsetpc in [0.01, 0.05, 0.1, 0.15, 0.2]:
                    size = round(subsetpc * len(regions))
                    if 0 < size < len(regions):
                        subname = f'{subsetpc:.2f}'
                        shuffled = regions.shuffle()
                        self.subsets[name][subname] = [
                            r.id for i, r in enumerate(shuffled) if i < size
                        ]
Exemplo n.º 29
0
    def test_regionset_tofrom_output_backlinks(self):
        nregions = 10
        bounds = Region([0] * 2, [100] * 2)
        sizepc = Region([0] * 2, [0.5] * 2)
        regionset = RegionSet.from_random(nregions,
                                          bounds,
                                          sizepc=sizepc,
                                          precision=1)
        regions = []

        for first in regionset:
            for second in regionset:
                if first is not second:
                    regions.append(first.union(second, 'reference'))
                    if first.overlaps(second):
                        regions.append(first.intersect(second, 'reference'))

        for region in regions:
            #print(f'{region}')
            regionset.add(region)

        with StringIO() as output:
            RegionSet.to_output(regionset, output, options={'compact': True})
            #print(output.getvalue())
            output.seek(0)
            newregionset = RegionSet.from_source(output, 'json')

            for region in regions:
                for field in ['intersect', 'union']:
                    if field in region:
                        self.assertTrue(field in newregionset[region.id])
                        self.assertTrue(
                            all([
                                isinstance(r, Region)
                                for r in newregionset[region.id][field]
                            ]))
                        self.assertListEqual(region[field],
                                             newregionset[region.id][field])
Exemplo n.º 30
0
 def test_region_random_regions(self):
     region = Region([-5, 0], [15, 10])
     randoms = region.random_regions(5, Region([0.25, 0.25], [0.75, 0.75]))
     randoms += region.random_regions(5,
                                      Region([0.25, 0.25], [0.75, 0.75]),
                                      precision=0)
     #print(f'{region}:')
     for subregion in randoms:
         #print(f'- {subregion}')
         self.assertTrue(subregion in region)