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)))
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)
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
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])
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])
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)
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))
def test_regionset_filter(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) filter_bound = Region([5] * 2, [10] * 2) filtered = regionset.filter(filter_bound) self.assertEqual(filter_bound, filtered.bounds) for region in regionset: #print(f'{region}: {region in filtered}') self.assertEqual(filter_bound.encloses(region), region in filtered) for region in filtered: #print(f'{region}') self.assertTrue(filter_bound.encloses(region))
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)
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 ]
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])
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} for nregions in [pow(10, n) for n in range(1, 4)]: for sizepc in [0.01, 0.05, *([] if nregions > 100 else [0.1])]: sizerng = Region([0] * 2, [sizepc] * 2) regions = RegionSet.from_random(nregions, bounds, sizepc=sizerng, precision=1) self.regions[f'{nregions},{sizepc:.2f}'] = regions
def test_nxgraph_sweepctor_random(self): regions = RegionSet.from_random(100, Region([0] * 3, [100] * 3)) nxgraphsweepln = self._nxgraphctor(regions) nxgraphmdsweepln = self._nxgraphmdctor(regions) self._check_nxgraph(nxgraphsweepln, nxgraphmdsweepln)