22import numpy as np
33import pandas as pd
44
5- from layoutparser .elements import Interval , Rectangle , Quadrilateral , TextBlock , Layout , InvalidShapeError , NotSupportedShapeError
5+ from layoutparser .elements import (
6+ Interval ,
7+ Rectangle ,
8+ Quadrilateral ,
9+ TextBlock ,
10+ Layout ,
11+ InvalidShapeError ,
12+ NotSupportedShapeError ,
13+ )
614
715
816def test_interval ():
@@ -46,7 +54,7 @@ def test_quadrilateral():
4654
4755 points = np .array ([[2 , 2 ], [6 , 2 ], [6 , 7 ], [2 , 6 ]])
4856 q = Quadrilateral (points )
49- q .to_interval (axis = 'x' )
57+ q .to_interval (axis = "x" )
5058 q .to_rectangle ()
5159 assert q .shift (1 ) == Quadrilateral (points + 1 )
5260 assert q .shift ([1 , 2 ]) == Quadrilateral (points + np .array ([1 , 2 ]))
@@ -70,7 +78,7 @@ def test_quadrilateral():
7078
7179 q = Quadrilateral ([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ])
7280 assert (q .points == np .array ([[1 , 2 ], [3 , 4 ], [5 , 6 ], [7 , 8 ]])).all ()
73-
81+
7482 q = Quadrilateral ([[1 , 2 ], [3 , 4 ], [5 , 6 ], [7 , 8 ]])
7583 assert (q .points == np .array ([[1 , 2 ], [3 , 4 ], [5 , 6 ], [7 , 8 ]])).all ()
7684
@@ -173,25 +181,48 @@ def test_textblock():
173181
174182 t = TextBlock (q , score = 0.2 )
175183
176- # Additional test for shape conversion
177- assert TextBlock (i , id = 1 , type = 2 , text = "12" ).to_interval () == TextBlock (i , id = 1 , type = 2 , text = "12" )
178- assert TextBlock (i , id = 1 , type = 2 , text = "12" ).to_rectangle () == TextBlock (i .to_rectangle (), id = 1 , type = 2 , text = "12" )
179- assert TextBlock (i , id = 1 , type = 2 , text = "12" ).to_quadrilateral () == TextBlock (i .to_quadrilateral (), id = 1 , type = 2 , text = "12" )
180-
181- 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" )
182- 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" )
183- assert TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_rectangle () == TextBlock (r , id = 1 , type = 2 , parent = "a" )
184- assert TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_quadrilateral () == TextBlock (r .to_quadrilateral (), id = 1 , type = 2 , parent = "a" )
185-
186- 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" )
187- 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" )
188- assert TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_rectangle () == TextBlock (q .to_rectangle (), id = 1 , type = 2 , parent = "a" )
189- assert TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_quadrilateral () == TextBlock (q , id = 1 , type = 2 , parent = "a" )
184+ # Additional test for shape conversion
185+ assert TextBlock (i , id = 1 , type = 2 , text = "12" ).to_interval () == TextBlock (
186+ i , id = 1 , type = 2 , text = "12"
187+ )
188+ assert TextBlock (i , id = 1 , type = 2 , text = "12" ).to_rectangle () == TextBlock (
189+ i .to_rectangle (), id = 1 , type = 2 , text = "12"
190+ )
191+ assert TextBlock (i , id = 1 , type = 2 , text = "12" ).to_quadrilateral () == TextBlock (
192+ i .to_quadrilateral (), id = 1 , type = 2 , text = "12"
193+ )
194+
195+ assert TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_interval (axis = "x" ) == TextBlock (
196+ r .to_interval (axis = "x" ), id = 1 , type = 2 , parent = "a"
197+ )
198+ assert TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_interval (axis = "y" ) == TextBlock (
199+ r .to_interval (axis = "y" ), id = 1 , type = 2 , parent = "a"
200+ )
201+ assert TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_rectangle () == TextBlock (
202+ r , id = 1 , type = 2 , parent = "a"
203+ )
204+ assert TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_quadrilateral () == TextBlock (
205+ r .to_quadrilateral (), id = 1 , type = 2 , parent = "a"
206+ )
207+
208+ assert TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_interval (axis = "x" ) == TextBlock (
209+ q .to_interval (axis = "x" ), id = 1 , type = 2 , parent = "a"
210+ )
211+ assert TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_interval (axis = "y" ) == TextBlock (
212+ q .to_interval (axis = "y" ), id = 1 , type = 2 , parent = "a"
213+ )
214+ assert TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_rectangle () == TextBlock (
215+ q .to_rectangle (), id = 1 , type = 2 , parent = "a"
216+ )
217+ assert TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_quadrilateral () == TextBlock (
218+ q , id = 1 , type = 2 , parent = "a"
219+ )
190220
191221 with pytest .raises (ValueError ):
192222 TextBlock (q , id = 1 , type = 2 , parent = "a" ).to_interval ()
193223 TextBlock (r , id = 1 , type = 2 , parent = "a" ).to_interval ()
194224
225+
195226def test_layout ():
196227 i = Interval (4 , 5 , axis = "y" )
197228 q = Quadrilateral (np .array ([[2 , 2 ], [6 , 2 ], [6 , 7 ], [2 , 5 ]]))
@@ -241,6 +272,20 @@ def test_layout():
241272 l .page_data = {"width" : 200 , "height" : 400 }
242273 l + l2
243274
275+ # Test sort
276+ l = Layout ([i , i .shift (2 )])
277+ l .sort (key = lambda x : x .coordinates [1 ], reverse = True )
278+ assert l == Layout ([i .shift (2 ), i ])
279+
280+ l = Layout ([q , r , i ], page_data = {"width" : 200 , "height" : 400 })
281+ assert l .sort (key = lambda x : x .coordinates [0 ], inplace = False ) == Layout (
282+ [i , q , r ], page_data = {"width" : 200 , "height" : 400 }
283+ )
284+
285+ l = Layout ([q , t ])
286+ assert l .sort (key = lambda x : x .coordinates [0 ], inplace = False ) == Layout ([q , t ])
287+
288+
244289def test_shape_operations ():
245290 i_1 = Interval (1 , 2 , axis = "y" , canvas_height = 30 , canvas_width = 400 )
246291 i_2 = TextBlock (Interval (1 , 2 , axis = "x" ))
@@ -249,19 +294,19 @@ def test_shape_operations():
249294 r_1 = Rectangle (0.5 , 0.5 , 2.5 , 1.5 )
250295 r_2 = TextBlock (Rectangle (0.5 , 0.5 , 2 , 2.5 ))
251296
252- q_1 = Quadrilateral ([[1 ,1 ], [2.5 , 1.2 ], [2.5 , 3 ], [1.5 , 3 ]])
253- q_2 = TextBlock (Quadrilateral ([[0.5 , 0.5 ], [2 ,1 ], [1.5 , 2.5 ], [0.5 , 2 ]]))
297+ q_1 = Quadrilateral ([[1 , 1 ], [2.5 , 1.2 ], [2.5 , 3 ], [1.5 , 3 ]])
298+ q_2 = TextBlock (Quadrilateral ([[0.5 , 0.5 ], [2 , 1 ], [1.5 , 2.5 ], [0.5 , 2 ]]))
254299
255- # I and I in different axes
300+ # I and I in different axes
256301 assert i_1 .intersect (i_1 ) == i_1
257- assert i_1 .intersect (i_2 ) == Rectangle (1 ,1 , 2 , 2 )
258- assert i_1 .intersect (i_3 ) == i_1 # Ensure intersect copy the canvas size
302+ assert i_1 .intersect (i_2 ) == Rectangle (1 , 1 , 2 , 2 )
303+ assert i_1 .intersect (i_3 ) == i_1 # Ensure intersect copy the canvas size
259304
260305 assert i_1 .union (i_1 ) == i_1
261306 with pytest .raises (InvalidShapeError ):
262- assert i_1 .union (i_2 ) == Rectangle (1 ,1 , 2 , 2 )
307+ assert i_1 .union (i_2 ) == Rectangle (1 , 1 , 2 , 2 )
263308
264- # I and R in different axes
309+ # I and R in different axes
265310 assert i_1 .intersect (r_1 ) == Rectangle (0.5 , 1 , 2.5 , 1.5 )
266311 assert i_2 .intersect (r_1 ).block == Rectangle (1 , 0.5 , 2 , 1.5 )
267312 assert i_1 .union (r_1 ) == Rectangle (0.5 , 0.5 , 2.5 , 2 )
@@ -271,12 +316,12 @@ def test_shape_operations():
271316 with pytest .raises (NotSupportedShapeError ):
272317 i_1 .intersect (q_1 )
273318 i_1 .union (q_1 )
274-
319+
275320 # I and Q in different axes
276- assert i_1 .intersect (q_1 , strict = False ) == Rectangle (1 ,1 , 2.5 ,2 )
277- assert i_1 .union (q_1 , strict = False ) == Rectangle (1 ,1 , 2.5 ,3 )
321+ assert i_1 .intersect (q_1 , strict = False ) == Rectangle (1 , 1 , 2.5 , 2 )
322+ assert i_1 .union (q_1 , strict = False ) == Rectangle (1 , 1 , 2.5 , 3 )
278323 assert i_2 .intersect (q_1 , strict = False ).block == Rectangle (1 , 1 , 2 , 3 )
279- assert i_2 .union (q_1 , strict = False ).block == Rectangle (1 ,1 , 2.5 ,3 )
324+ assert i_2 .union (q_1 , strict = False ).block == Rectangle (1 , 1 , 2.5 , 3 )
280325
281326 # R and I
282327 assert r_1 .intersect (i_1 ) == i_1 .intersect (r_1 )
@@ -289,7 +334,7 @@ def test_shape_operations():
289334 with pytest .raises (NotSupportedShapeError ):
290335 r_1 .intersect (q_1 )
291336 r_1 .union (q_1 )
292-
337+
293338 assert r_1 .intersect (q_1 , strict = False ) == Rectangle (1 , 1 , 2.5 , 1.5 )
294339 assert r_1 .union (q_1 , strict = False ) == Rectangle (0.5 , 0.5 , 2.5 , 3 )
295340 assert r_1 .intersect (q_2 , strict = False ) == r_1 .intersect (q_2 .to_rectangle ())
@@ -300,7 +345,7 @@ def test_shape_operations():
300345 q_1 .intersect (i_1 )
301346 q_1 .intersect (r_1 )
302347 q_1 .intersect (q_2 )
303-
348+
304349 # Q and I
305350 assert q_1 .intersect (i_1 , strict = False ) == i_1 .intersect (q_1 , strict = False )
306351 assert q_1 .union (i_1 , strict = False ) == i_1 .union (q_1 , strict = False )
@@ -315,6 +360,7 @@ def test_shape_operations():
315360 assert q_1 .union (q_2 , strict = False ) == q_2 .union (q_1 , strict = False ).block
316361 assert q_1 .union (q_2 , strict = False ) == Rectangle (0.5 , 0.5 , 2.5 , 3 )
317362
363+
318364def test_dict ():
319365
320366 i = Interval (1 , 2 , "y" , canvas_height = 5 )
0 commit comments