Ejemplo n.º 1
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)
Ejemplo n.º 2
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])