1818
1919from tests .test_annotation_stores import cell_polygon
2020from tiatoolbox import utils
21+ from tiatoolbox .annotation .storage import SQLiteStore
2122from tiatoolbox .models .architecture import fetch_pretrained_weights
2223from tiatoolbox .utils import misc
2324from tiatoolbox .utils .exceptions import FileNotSupportedError
@@ -734,6 +735,7 @@ def test_sub_pixel_read_incorrect_read_func_return() -> None:
734735 image = np .ones ((10 , 10 ))
735736
736737 def read_func (* args : tuple , ** kwargs : dict ) -> np .ndarray : # noqa: ARG001
738+ """Dummy read function for tests."""
737739 return np .ones ((5 , 5 ))
738740
739741 with pytest .raises (ValueError , match = "incorrect size" ):
@@ -752,6 +754,7 @@ def test_sub_pixel_read_empty_read_func_return() -> None:
752754 image = np .ones ((10 , 10 ))
753755
754756 def read_func (* args : tuple , ** kwargs : dict ) -> np .ndarray : # noqa: ARG001
757+ """Dummy read function for tests."""
755758 return np .ones ((0 , 0 ))
756759
757760 with pytest .raises (ValueError , match = "is empty" ):
@@ -1624,3 +1627,69 @@ def test_imwrite(tmp_path: Path) -> NoReturn:
16241627 tmp_path / "thisfolderdoesnotexist" / "test_imwrite.jpg" ,
16251628 img ,
16261629 )
1630+
1631+
1632+ def test_patch_pred_store () -> None :
1633+ """Test patch_pred_store."""
1634+ # Define a mock patch_output
1635+ patch_output = {
1636+ "predictions" : [1 , 0 , 1 ],
1637+ "coordinates" : [(0 , 0 , 1 , 1 ), (1 , 1 , 2 , 2 ), (2 , 2 , 3 , 3 )],
1638+ "other" : "other" ,
1639+ }
1640+
1641+ store = misc .patch_pred_store (patch_output , (1.0 , 1.0 ))
1642+
1643+ # Check that its an SQLiteStore containing the expected annotations
1644+ assert isinstance (store , SQLiteStore )
1645+ assert len (store ) == 3
1646+ for annotation in store .values ():
1647+ assert annotation .geometry .area == 1
1648+ assert annotation .properties ["type" ] in [0 , 1 ]
1649+ assert "other" not in annotation .properties
1650+
1651+ patch_output .pop ("coordinates" )
1652+ # check correct error is raised if coordinates are missing
1653+ with pytest .raises (ValueError , match = "coordinates" ):
1654+ misc .patch_pred_store (patch_output , (1.0 , 1.0 ))
1655+
1656+
1657+ def test_patch_pred_store_cdict () -> None :
1658+ """Test patch_pred_store with a class dict."""
1659+ # Define a mock patch_output
1660+ patch_output = {
1661+ "predictions" : [1 , 0 , 1 ],
1662+ "coordinates" : [(0 , 0 , 1 , 1 ), (1 , 1 , 2 , 2 ), (2 , 2 , 3 , 3 )],
1663+ "probabilities" : [[0.1 , 0.9 ], [0.9 , 0.1 ], [0.4 , 0.6 ]],
1664+ "labels" : [1 , 0 , 1 ],
1665+ "other" : "other" ,
1666+ }
1667+ class_dict = {0 : "class0" , 1 : "class1" }
1668+ store = misc .patch_pred_store (patch_output , (1.0 , 1.0 ), class_dict = class_dict )
1669+
1670+ # Check that its an SQLiteStore containing the expected annotations
1671+ assert isinstance (store , SQLiteStore )
1672+ assert len (store ) == 3
1673+ for annotation in store .values ():
1674+ assert annotation .geometry .area == 1
1675+ assert annotation .properties ["label" ] in ["class0" , "class1" ]
1676+ assert annotation .properties ["type" ] in ["class0" , "class1" ]
1677+ assert "other" not in annotation .properties
1678+
1679+
1680+ def test_patch_pred_store_sf () -> None :
1681+ """Test patch_pred_store with scale factor."""
1682+ # Define a mock patch_output
1683+ patch_output = {
1684+ "predictions" : [1 , 0 , 1 ],
1685+ "coordinates" : [(0 , 0 , 1 , 1 ), (1 , 1 , 2 , 2 ), (2 , 2 , 3 , 3 )],
1686+ "probabilities" : [[0.1 , 0.9 ], [0.9 , 0.1 ], [0.4 , 0.6 ]],
1687+ "labels" : [1 , 0 , 1 ],
1688+ }
1689+ store = misc .patch_pred_store (patch_output , (2.0 , 2.0 ))
1690+
1691+ # Check that its an SQLiteStore containing the expected annotations
1692+ assert isinstance (store , SQLiteStore )
1693+ assert len (store ) == 3
1694+ for annotation in store .values ():
1695+ assert annotation .geometry .area == 4
0 commit comments