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" ):
@@ -1642,3 +1645,69 @@ def test_imwrite(tmp_path: Path) -> NoReturn:
16421645 tmp_path / "thisfolderdoesnotexist" / "test_imwrite.jpg" ,
16431646 img ,
16441647 )
1648+
1649+
1650+ def test_patch_pred_store () -> None :
1651+ """Test patch_pred_store."""
1652+ # Define a mock patch_output
1653+ patch_output = {
1654+ "predictions" : [1 , 0 , 1 ],
1655+ "coordinates" : [(0 , 0 , 1 , 1 ), (1 , 1 , 2 , 2 ), (2 , 2 , 3 , 3 )],
1656+ "other" : "other" ,
1657+ }
1658+
1659+ store = misc .patch_pred_store (patch_output , (1.0 , 1.0 ))
1660+
1661+ # Check that its an SQLiteStore containing the expected annotations
1662+ assert isinstance (store , SQLiteStore )
1663+ assert len (store ) == 3
1664+ for annotation in store .values ():
1665+ assert annotation .geometry .area == 1
1666+ assert annotation .properties ["type" ] in [0 , 1 ]
1667+ assert "other" not in annotation .properties
1668+
1669+ patch_output .pop ("coordinates" )
1670+ # check correct error is raised if coordinates are missing
1671+ with pytest .raises (ValueError , match = "coordinates" ):
1672+ misc .patch_pred_store (patch_output , (1.0 , 1.0 ))
1673+
1674+
1675+ def test_patch_pred_store_cdict () -> None :
1676+ """Test patch_pred_store with a class dict."""
1677+ # Define a mock patch_output
1678+ patch_output = {
1679+ "predictions" : [1 , 0 , 1 ],
1680+ "coordinates" : [(0 , 0 , 1 , 1 ), (1 , 1 , 2 , 2 ), (2 , 2 , 3 , 3 )],
1681+ "probabilities" : [[0.1 , 0.9 ], [0.9 , 0.1 ], [0.4 , 0.6 ]],
1682+ "labels" : [1 , 0 , 1 ],
1683+ "other" : "other" ,
1684+ }
1685+ class_dict = {0 : "class0" , 1 : "class1" }
1686+ store = misc .patch_pred_store (patch_output , (1.0 , 1.0 ), class_dict = class_dict )
1687+
1688+ # Check that its an SQLiteStore containing the expected annotations
1689+ assert isinstance (store , SQLiteStore )
1690+ assert len (store ) == 3
1691+ for annotation in store .values ():
1692+ assert annotation .geometry .area == 1
1693+ assert annotation .properties ["label" ] in ["class0" , "class1" ]
1694+ assert annotation .properties ["type" ] in ["class0" , "class1" ]
1695+ assert "other" not in annotation .properties
1696+
1697+
1698+ def test_patch_pred_store_sf () -> None :
1699+ """Test patch_pred_store with scale factor."""
1700+ # Define a mock patch_output
1701+ patch_output = {
1702+ "predictions" : [1 , 0 , 1 ],
1703+ "coordinates" : [(0 , 0 , 1 , 1 ), (1 , 1 , 2 , 2 ), (2 , 2 , 3 , 3 )],
1704+ "probabilities" : [[0.1 , 0.9 ], [0.9 , 0.1 ], [0.4 , 0.6 ]],
1705+ "labels" : [1 , 0 , 1 ],
1706+ }
1707+ store = misc .patch_pred_store (patch_output , (2.0 , 2.0 ))
1708+
1709+ # Check that its an SQLiteStore containing the expected annotations
1710+ assert isinstance (store , SQLiteStore )
1711+ assert len (store ) == 3
1712+ for annotation in store .values ():
1713+ assert annotation .geometry .area == 4
0 commit comments