From f6427b81993dfad13970ff6db973f98f2b397c0f Mon Sep 17 00:00:00 2001 From: abhayrajjais01 Date: Sat, 7 Mar 2026 16:39:35 +0530 Subject: [PATCH] Fix utility warning bugs: replace raise DeprecationWarning with warnings.warn, fix len(string) check --- src/deepforest/utilities.py | 29 +++++++++++++++++++------- tests/test_utilities.py | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/deepforest/utilities.py b/src/deepforest/utilities.py index f467bbc6c..7ea26d202 100644 --- a/src/deepforest/utilities.py +++ b/src/deepforest/utilities.py @@ -224,13 +224,27 @@ def shapefile_to_annotations( convert_point: bool = False, label: str | None = None, ) -> gpd.GeoDataFrame: - raise DeprecationWarning( - "shapefile_to_annotations is deprecated, use read_file instead" + warnings.warn( + "shapefile_to_annotations is deprecated, use read_file instead", + DeprecationWarning, + stacklevel=2, ) if buffer_size is not None and convert_point: - raise DeprecationWarning( - "buffer_size argument is deprecated, use convert_point_to_bbox instead" + warnings.warn( + "buffer_size argument is deprecated, use convert_point_to_bbox instead", + DeprecationWarning, + stacklevel=2, + ) + + if isinstance(shapefile, str): + shapefile_path = shapefile + shapefile = gpd.read_file(shapefile_path) + shapefile = DeepForest_DataFrame(shapefile) + shapefile = __assign_image_path__(shapefile, image_path=rgb) + shapefile = __check_and_assign_label__(shapefile, label=label) + shapefile = __assign_root_dir__( + input=shapefile_path, gdf=shapefile, root_dir=root_dir ) return __shapefile_to_annotations__(shapefile) @@ -247,12 +261,13 @@ def __assign_image_path__(gdf, image_path: str) -> str: pass else: if "image_path" in gdf.columns: - existing_image_path = gdf.image_path.unique()[0] - if len(existing_image_path) > 1: + existing_image_paths = gdf.image_path.unique() + if len(existing_image_paths) > 1: warnings.warn( - f"Multiple image_paths found in dataframe: {existing_image_path}, overriding and assigning {image_path} to all rows!", + f"Multiple image_paths found in dataframe: {existing_image_paths}, overriding and assigning {image_path} to all rows!", stacklevel=2, ) + existing_image_path = existing_image_paths[0] if existing_image_path != image_path: warnings.warn( f"Image path {existing_image_path} found in dataframe, overriding and assigning {image_path} to all rows!", diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 0be8a665b..63c848883 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -155,6 +155,47 @@ def test_read_file_shapefile_without_image_path(tmp_path): assert "label" in result.columns assert hasattr(result, "root_dir") +def test_shapefile_to_annotations_deprecation_warning(tmp_path): + """shapefile_to_annotations should emit DeprecationWarning, not raise it.""" + import warnings + sample_geometry = [geometry.Point(404211.9 + 10, 3285102 + 20)] + labels = ["Tree"] + df = pd.DataFrame({"geometry": sample_geometry, "label": labels}) + gdf = gpd.GeoDataFrame(df, geometry="geometry", crs="EPSG:32617") + gdf["geometry"] = [geometry.box(left, bottom, right, top) for left, bottom, right, top in + gdf.geometry.buffer(0.5).bounds.values] + gdf["image_path"] = os.path.basename(get_data("OSBS_029.tif")) + shp_path = str(tmp_path / "annotations.shp") + gdf.to_file(shp_path) + + with pytest.warns(DeprecationWarning, match="shapefile_to_annotations is deprecated"): + result = utilities.shapefile_to_annotations(shp_path, root_dir=os.path.dirname(get_data("OSBS_029.tif"))) + assert result is not None + +def test_assign_image_path_warns_on_override(): + """__assign_image_path__ should warn when overriding an existing image_path.""" + import warnings + sample_geometry = [geometry.box(0, 0, 1, 1)] + df = pd.DataFrame({"geometry": sample_geometry, "label": ["Tree"], "image_path": ["old_image.tif"]}) + gdf = gpd.GeoDataFrame(df, geometry="geometry") + + with pytest.warns(UserWarning, match="overriding and assigning"): + utilities.__assign_image_path__(gdf, image_path="new_image.tif") + +def test_assign_image_path_warns_multiple_paths(): + """__assign_image_path__ should warn when multiple image_paths exist.""" + import warnings + sample_geometry = [geometry.box(0, 0, 1, 1), geometry.box(2, 2, 3, 3)] + df = pd.DataFrame({ + "geometry": sample_geometry, + "label": ["Tree", "Tree"], + "image_path": ["image_a.tif", "image_b.tif"] + }) + gdf = gpd.GeoDataFrame(df, geometry="geometry") + + with pytest.warns(UserWarning, match="Multiple image_paths found"): + utilities.__assign_image_path__(gdf, image_path="override.tif") + def test_shapefile_to_annotations_invalid_epsg(tmp_path): sample_geometry = [geometry.Point(404211.9 + 10, 3285102 + 20), geometry.Point(404211.9 + 20, 3285102 + 20)] labels = ["Tree", "Tree"]