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_nxgraph_sweepctor_graph(self): dimension = self.test_regions[0].dimension nxgraph = NxGraph(dimension=dimension) self._naive_ctor(nxgraph) regionset = RegionSet(dimension=dimension) regionset.streamadd(self.test_regions) nxgraphsweepln = self._nxgraphctor(regionset) nxgraphmdsweepln = self._nxgraphmdctor(regionset) G = nxgraph.G S = nxgraphsweepln.G M = nxgraphmdsweepln.G self.assertEqual(nxgraph.dimension, nxgraphsweepln.dimension) self.assertEqual(nxgraph.dimension, nxgraphmdsweepln.dimension) for N in [S, M]: for node, region in G.nodes(data='region'): self.assertTrue(node in N.node) self.assertEqual(region, N.node[node]['region']) for (u, v, aregion) in G.edges(data='intersect'): self.assertTrue((u, v) in N.edges) bregion = N.edges[u, v]['intersect'] self.assertEqual(aregion, bregion) self.assertTrue('intersect' in aregion) self.assertTrue('intersect' in bregion) aintersect = aregion['intersect'] bintersect = bregion['intersect'] self.assertTrue(all([r in bintersect for r in aintersect]))
def context(cls, ctx: Context) -> Context: """ Given a context object (a collection of Regions or a Region intersection graph), converts it into the other object type. Args: ctx: The object to be converted. Returns: The converted object. """ assert isinstance(ctx, (RegionSet, NxGraph)) if isinstance(ctx, RegionSet): return NxGraphSweepCtor.prepare(ctx)() else: regions = RegionSet(dimension=ctx.dimension, id=ctx.id) regions.streamadd([region for n, region, _ in ctx.regions]) return regions
def test_nxgraph_mdsweepctor(self): dimension = self.test_regions[0].dimension regionset = RegionSet(dimension=dimension) regionset.streamadd(self.test_regions) nxgraph = self._nxgraphmdctor(regionset) with StringIO() as output: for json_graph in ['node_link', 'adjacency']: options = {'json_graph': json_graph, 'compact': True} output.truncate(0) NxGraph.to_output(nxgraph, self._reset_output(output), options=options) #json_output = output.getvalue() #print(f'{json_graph}:') #print(f'{json_output}') newgraph = NxGraph.from_source(self._reset_output(output)) self._check_nxgraph(nxgraph, newgraph)
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 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