def test_region_invert_compare_steps(): """-r and field()-r should ideally filter the same way but they are potentially different. """ shape = "rect(100, 100, 104, 106)" r = Region("field()").subtract(Region(shape)) check_region_invert_compare(r)
def test_region_mask_match_file(tmp_path): """It shoudn't matter where the region comes from""" f = tmp_path / 'reg.src' f.write_text(normalized_string) r = Region(str(f), True) m = r.mask(xm, ym) assert all(val == 1 for val in m)
def test_region_mask_match_file(tmpdir): """It shoudn't matter where the region comes from""" f = tmpdir.join("reg") f.write(normalized_string) r = Region(str(f), True) m = r.mask(xm, ym) assert all(val == 1 for val in m)
def test_region_mask_match_swapped(): """All points are included This is just to check the ordering is handled correctly for test_region_mask_keywords """ r = Region(region_string) m = r.mask(ym, xm) assert m == pytest.approx([1, 0, 1])
def test_region_union(): """Adding two regions together""" r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r = Region(r1).union(Region(r2)) r2 = r2.replace('rect', 'rectangle') expected = r1.capitalize() + '|' + r2.capitalize() assert expected == str(r)
def test_region_ignore_kwargs(): """Removing a regions with keyword arguments""" r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r = Region(r1).combine(exclude=1, region=Region(r2)) r2 = r2.replace('rect', 'rectangle') expected = r1.capitalize() + '&!' + r2.capitalize() assert expected == str(r)
def test_region_combine_kwargs(): """Adding two regions together with keyword arguments""" r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r = Region(r1).combine(exclude=0, region=Region(r2)) r2 = r2.replace('rect', 'rectangle') expected = r1.capitalize() + '&' + r2.capitalize() assert expected == str(r)
def test_region_ignore_overlap(): """Ignoring a region which does overlap.""" r1 = 'circle(10,10,10)' r2 = 'rect(5,5,7,10)' r = Region(r1).subtract(Region(r2)) r2 = r2.replace('rect', 'rectangle') expected = r1.capitalize() + '&!' + r2.capitalize() assert expected == str(r)
def test_region_combine_combined(): """Check multiple calls to union regions""" r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r3 = 'ellipse(70,70,10,12,45)' rcomb = Region(r1).union(Region(r2)) rcomb = rcomb.union(Region(r3)) r2 = r2.replace('rect', 'rectangle') expected = '|'.join([x.capitalize() for x in [r1, r2, r3]]) assert expected == str(rcomb)
def test_complex_region_manual_explicit(): """Based on #1245. Here we create the region like test_complex_region_file_explicit """ rmanual = Region(COMPLEX_REGION[0] + COMPLEX_REGION[2]) rmanual = rmanual.union(Region(COMPLEX_REGION[1] + COMPLEX_REGION[2])) assert str( rmanual ) == 'Circle(50,50,30)&!RotBox(30,30,10,5,45)|Ellipse(40,75,30,20,320)&!RotBox(30,30,10,5,45)' check_complex_region_1245(rmanual)
def test_region_ignore_no_overlap(): """Ignoring a region which doesn't overlap. It looks like the region lib doesn't try to simplify the combination of shapes. """ r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r = Region(r1).combine(Region(r2), True) r2 = r2.replace('rect', 'rectangle') expected = r1.capitalize() + '&!' + r2.capitalize() assert expected == str(r)
def test_region_empty_none(): """The empty region (given explicitly) - can no-longer create""" with pytest.raises(TypeError) as te: r = Region(None) assert str(te.value) == "argument 1 must be str, not None"
def test_region_file(tmpdir): """Can we create a region from a file?""" f = tmpdir.join('reg') f.write(normalized_string) r = Region(str(f), True) assert normalized_string == str(r)
def test_region_file(tmp_path): """Can we create a region from a file?""" f = tmp_path / 'reg' f.write_text(normalized_string) r = Region(str(f), True) assert normalized_string == str(r)
def test_region_string(): """We can create with a string. Note the region gets normalized. """ r = Region(region_string) assert normalized_string == str(r)
def test_complex_region_file_explicit(tmp_path): """Based on #1245. Here we are explicit in our region shape - that is a - c b - c although technically we could have used a - c b as c does not overlap a """ regfile = tmp_path / 'test.reg' regstr = "\n".join([ COMPLEX_REGION[0] + COMPLEX_REGION[2], COMPLEX_REGION[1] + COMPLEX_REGION[2] ]) regfile.write_text(regstr) rfile = Region(str(regfile), True) assert str( rfile ) == 'Circle(50,50,30)&!RotBox(30,30,10,5,45)|Ellipse(40,75,30,20,320)&!RotBox(30,30,10,5,45)' check_complex_region_1245(rfile)
def test_region_combine_invalid(ignore, arg): """Check we error out if not sent a region.""" r = Region('field()') method = r.subtract if ignore else r.union with pytest.raises(TypeError): method(arg)
def test_complex_region_file_implicit(tmp_path): """Based on #1245. Unlike test_complex_region_file_explicit the region file is just a b -c Note that this actualy behaves as if we've just ignored the "-c" filter (e.g. a dmcopy with this filter file ignores the -c expression) because c does not overlap b [or whatever logic is used by CIAO region expressions] """ regfile = tmp_path / 'test.reg' regstr = "\n".join(COMPLEX_REGION) regfile.write_text(regstr) rfile = Region(str(regfile), True) assert str( rfile ) == 'Circle(50,50,30)|Ellipse(40,75,30,20,320)&!RotBox(30,30,10,5,45)' check_complex_region_1245_no_exclude(rfile)
def test_region_string_invalid(wrong): """Check we error out with invalid input""" with pytest.raises(ValueError) as exc: Region(wrong) emsg = f"unable to parse region string: '{wrong}'" assert str(exc.value) == emsg
def test_region_ignore_combined(): """Check multiple calls to region_combine I am not sure I agree with the result but I just want to check the results. """ r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r3 = 'ellipse(70,70,10,12,45)' rcomb = Region(r1).combine(Region(r2), True) rcomb = rcomb.combine(Region(r3)) r2 = '!' + r2.replace('rect', 'rectangle').capitalize() expected = '&'.join([r1.capitalize(), r2, r3.capitalize()]) assert expected == str(rcomb)
def test_region_empty(): """The empty region - can no-longer create""" with pytest.raises(TypeError) as te: r = Region() assert str( te.value) == "function missing required argument 'region' (pos 1)"
def test_region_invert_compare_inone(): """-r and field()-r should ideally filter the same way but they are potentially different. """ shape = "rect(100, 100, 104, 106)" r = Region(f"field()-{shape}") check_region_invert_compare(r)
def test_region_string_invalid(): """Check we error out with invalid input""" wrong = 'circle 100 200 30' with pytest.raises(ValueError) as exc: Region(wrong) emsg = 'unable to parse region string: ' + wrong assert str(exc.value) == emsg
def test_region_ignore_combined(): """Check multiple calls to union/subtract I am not sure I agree with the result but I just want to check the results. """ # Note r2 does not overlap r1 r1 = 'circle(10,10,10)' r2 = 'rect(100,100,200,200)' r3 = 'ellipse(70,70,10,12,45)' rcomb = Region(r1).subtract(Region(r2)) rcomb = rcomb.union(Region(r3)) expected = '|'.join([r1.capitalize(), r3.capitalize()]) assert expected == str(rcomb)
def check_ignore_ignore2(d): """Check removing the shapes works as expected.""" shape1 = 'ellipse(4260,3840,3,2,0)' d.notice2d(shape1, ignore=True) mask1 = ~Region(shape1).mask(d.x0, d.x1).astype(np.bool) assert d.mask == pytest.approx(mask1) shape2 = 'rect(4258,3830,4264,3841)' d.notice2d(shape2, ignore=True) mask2 = ~Region(shape2).mask(d.x0, d.x1).astype(np.bool) assert d.mask == pytest.approx(mask1 & mask2) shape2 = shape2.replace('rect', 'rectangle') expected = '!' + shape1.capitalize() + '&!' + shape2.capitalize() assert d.get_filter() == expected
def test_region_file_invalid(tmp_path): """What happens if the file is invalid. Note that the file parser apparently fails if there are spaces, so I use region_string here in case the parser ever gets improved, in which case this test will need a new invalid file. """ f = tmp_path / 'src.reg' f.write_text(region_string) with pytest.raises(ValueError) as exc: Region(str(f), True) emsg = 'unable to read region from: {}'.format(f) assert str(exc.value) == emsg
def test_region_invert(): """Can we invert a region?""" x = np.asarray([50, 95, 105]) y = np.asarray([50, 105, 115]) r = Region("box(100,100,10,20)") assert r.mask(x, y) == pytest.approx([0, 1, 0]) r.invert() assert str(r) == "!Box(100,100,10,20)" assert r.mask(x, y) == pytest.approx([1, 0, 1])
def test_region_invert_empty(): """Can we invert an empty region?""" x = np.asarray([50, 95, 105]) y = np.asarray([50, 105, 115]) r = Region() assert r.mask(x, y) == pytest.approx([0, 0, 0]) r.invert() assert str(r) == '' assert r.mask(x, y) == pytest.approx([0, 0, 0])
def test_complex_region_manual_implicit(): """Based on #1245. Here we create the region like test_complex_region_file_implicit """ rmanual = Region(COMPLEX_REGION[0]) rmanual = rmanual.union(Region(COMPLEX_REGION[1])) rmanual = rmanual.subtract(Region(COMPLEX_REGION[2][1:])) # Note how the rotbox is only applied to the first shape (which it overlaps), not # the second one. assert str( rmanual ) == 'Circle(50,50,30)&!RotBox(30,30,10,5,45)|Ellipse(40,75,30,20,320)' check_complex_region_1245(rmanual)
def test_region_string(self): r = Region(self.region_string) self.assertEqual(self.normalized_string, str(r))