def test_rectangle(): r = Rectangle(1, 2, 3, 4) r.to_interval(axis="x") r.to_quadrilateral() assert r.pad(left=1, right=5, top=2, bottom=4) == Rectangle(0, 0, 8, 8) assert r.shift([1, 2]) == Rectangle(2, 4, 4, 6) assert r.shift(1) == Rectangle(2, 3, 4, 5) assert r.scale([3, 2]) == Rectangle(3, 4, 9, 8) assert r.scale(2) == Rectangle(2, 4, 6, 8) assert r.area == 4 img = np.random.randint(12, 24, (40, 40)) r.crop_image(img).shape == (2, 2)
def test_rectangle_relations(): i = Interval(4, 5, axis="y") q = Quadrilateral(np.array([[2, 2], [6, 2], [6, 7], [2, 5]])) r = Rectangle(3, 3, 5, 6) assert not r.is_in(q) assert r.is_in(q, soft_margin={"bottom": 1}) assert r.is_in(q.to_rectangle()) assert r.is_in(q.to_interval()) # convert to absolute then convert back to relative assert r.condition_on(i).relative_to(i) == r assert r.condition_on(r).relative_to(r) == r assert r.condition_on(q).relative_to(q) == r.to_quadrilateral() # convert to relative then convert back to absolute assert r.relative_to(i).condition_on(i) == r assert r.relative_to(r).condition_on(r) == r assert r.relative_to(q).condition_on(q) == r.to_quadrilateral()
def test_layout(): i = Interval(4, 5, axis="y") q = Quadrilateral(np.array([[2, 2], [6, 2], [6, 7], [2, 5]])) r = Rectangle(3, 3, 5, 6) t = TextBlock(i, id=1, type=2, text="12") l = Layout([i, q, r]) l.get_texts() l.condition_on(i) l.relative_to(q) l.filter_by(t) l.is_in(r) assert l.get_homogeneous_blocks() == [ i.to_quadrilateral(), q, r.to_quadrilateral() ] i2 = TextBlock(i, id=1, type=2, text="12") r2 = TextBlock(r, id=1, type=2, parent="a") q2 = TextBlock(q, id=1, type=2, next="a") l2 = Layout([i2, r2, q2], page_data={"width": 200, "height": 200}) l2.get_texts() l2.get_info("next") l2.condition_on(i) l2.relative_to(q) l2.filter_by(t) l2.is_in(r) l2.scale(4) l2.shift(4) l2.pad(left=2) # Test slicing function homogeneous_blocks = l2[:2].get_homogeneous_blocks() assert homogeneous_blocks[0].block == i.to_rectangle() assert homogeneous_blocks[1].block == r # Test appending and extending assert l + [i2] == Layout([i, q, r, i2]) assert l + l == Layout([i, q, r] * 2) l.append(i) assert l == Layout([i, q, r, i]) l2.extend([q]) assert l2 == Layout([i2, r2, q2, q], page_data={ "width": 200, "height": 200 }) # Test addition l + l2 with pytest.raises(ValueError): l.page_data = {"width": 200, "height": 400} l + l2
def test_textblock(): i = Interval(4, 5, axis="y") q = Quadrilateral(np.array([[2, 2], [6, 2], [6, 7], [2, 5]])) r = Rectangle(3, 3, 5, 6) t = TextBlock(i, id=1, type=2, text="12") assert (t.relative_to(q).condition_on(q).block == i.put_on_canvas( q).to_quadrilateral()) t.area t = TextBlock(r, id=1, type=2, parent="a") assert t.relative_to(i).condition_on(i).block == r t.area t = TextBlock(q, id=1, type=2, parent="a") assert t.relative_to(r).condition_on(r).block == q t.area # Ensure the operations did not change the object itself assert t == TextBlock(q, id=1, type=2, parent="a") t1 = TextBlock(q, id=1, type=2, parent="a") t2 = TextBlock(i, id=1, type=2, text="12") t1.relative_to(t2) assert t2.is_in(t1) t = TextBlock(q, score=0.2) # Additional test for shape conversion assert TextBlock(i, id=1, type=2, text="12").to_interval() == TextBlock(i, id=1, type=2, text="12") assert TextBlock(i, id=1, type=2, text="12").to_rectangle() == TextBlock(i.to_rectangle(), id=1, type=2, text="12") assert TextBlock(i, id=1, type=2, text="12").to_quadrilateral() == TextBlock( i.to_quadrilateral(), id=1, type=2, text="12") assert TextBlock(r, id=1, type=2, parent="a").to_interval(axis="x") == TextBlock( r.to_interval(axis="x"), id=1, type=2, parent="a") assert TextBlock(r, id=1, type=2, parent="a").to_interval(axis="y") == TextBlock( r.to_interval(axis="y"), id=1, type=2, parent="a") assert TextBlock(r, id=1, type=2, parent="a").to_rectangle() == TextBlock(r, id=1, type=2, parent="a") assert TextBlock(r, id=1, type=2, parent="a").to_quadrilateral() == TextBlock( r.to_quadrilateral(), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_interval(axis="x") == TextBlock( q.to_interval(axis="x"), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_interval(axis="y") == TextBlock( q.to_interval(axis="y"), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_rectangle() == TextBlock(q.to_rectangle(), id=1, type=2, parent="a") assert TextBlock(q, id=1, type=2, parent="a").to_quadrilateral() == TextBlock(q, id=1, type=2, parent="a") with pytest.raises(ValueError): TextBlock(q, id=1, type=2, parent="a").to_interval() TextBlock(r, id=1, type=2, parent="a").to_interval()