Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

evaluate_boxes() raises TypeError when ground truth annotations have a non-default index and no predictions exist for that image. The issue occurs at lines 239-250 where Series with mismatched indices are passed to pd.DataFrame().

# This fails before the fix
ground_truth = gpd.GeoDataFrame(
    {"image_path": ["img.jpg", "img.jpg"], "label": ["Tree", "Tree"], ...},
    index=[100, 200]  # Non-default index
)
predictions = gpd.GeoDataFrame({"image_path": ["other.jpg"], ...})
evaluate_boxes(predictions, ground_truth)  # TypeError

Changes:

  • Reset group index before DataFrame construction in the empty predictions branch (line 240), matching the existing pattern in the else branch (line 258)
  • Add test case test_evaluate_boxes_no_predictions_for_image() with non-default indices

The fix ensures Series alignment by converting group to a default integer index before mixing with newly created Series.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: /usr/bin/curl curl -LsSf REDACTED (dns block)
  • huggingface.co
    • Triggering command: /usr/bin/python python -m pytest tests/test_evaluate.py -v (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Evaluate boxes fails when there are some ground that have predictions, but others do not.</issue_title>
<issue_description>

Issue Summary

When evaluate_boxes encounters ground truth annotations for an image but no predictions for that image, it raises a TypeError during DataFrame construction at lines 239-250 in deepforest/evaluate.py.

Minimal Reproducible Example

import pandas as pd
import geopandas as gpd
from shapely.geometry import box
from deepforest.evaluate import evaluate_boxes

# Create ground truth with annotations for an image
ground_truth = gpd.GeoDataFrame(
    {
        "image_path": ["image1.jpg", "image1.jpg"],
        "label": ["Tree", "Tree"],
        "xmin": [10, 50],
        "ymin": [10, 50],
        "xmax": [30, 70],
        "ymax": [30, 70],
    }
)
ground_truth["geometry"] = ground_truth.apply(
    lambda row: box(row["xmin"], row["ymin"], row["xmax"], row["ymax"]), axis=1
)

# Create empty predictions (no predictions for image1.jpg)
predictions = gpd.GeoDataFrame(
    {
        "image_path": ["image2.jpg"],  # Different image
        "label": ["Tree"],
        "xmin": [10],
        "ymin": [10],
        "xmax": [30],
        "ymax": [30],
        "score": [0.9],
    }
)
predictions["geometry"] = predictions.apply(
    lambda row: box(row["xmin"], row["ymin"], row["xmax"], row["ymax"]), axis=1
)

# This raises TypeError
results = evaluate_boxes(
    predictions=predictions,
    ground_df=ground_truth,
    iou_threshold=0.4
)

Error Traceback

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../pandas/core/frame.py", line 782, in __init__
    mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
  File ".../pandas/core/internals/construction.py", line 503, in dict_to_mgr
    return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
  File ".../pandas/core/internals/construction.py", line 114, in arrays_to_mgr
    ...
TypeError: ...

Root Cause

The issue occurs in evaluate_boxes function at lines 239-250. When image_predictions.empty is True, the code attempts to create a DataFrame using group.index.values from a groupby operation. The index from groupby may not align properly with the DataFrame constructor, especially when the group has a non-standard index structure.

Proposed Fix

Reset the index of group before using it in DataFrame construction:

# If empty, add to list without computing IoU
if image_predictions.empty:
    # Reset index to ensure proper alignment with DataFrame construction
    group = group.reset_index(drop=True)
    result = pd.DataFrame(
        {
            "truth_id": group.index.values,
            "prediction_id": pd.Series([None] * len(group), dtype="object"),
            "IoU": pd.Series([0.0] * len(group), dtype="float64"),
            "predicted_label": pd.Series([None] * len(group), dtype="object"),
            "score": pd.Series([None] * len(group), dtype="float64"),
            "match": pd.Series([False] * len(group), dtype="bool"),
            "true_label": group.label.astype("object"),
            "geometry": group.geometry,
        }
    )

This fix ensures that group.index.values produces a clean integer index that aligns properly with the other Series in the DataFrame constructor.

Agent statement

I identified the error and found the trace, but could not identify which error within the set on L240 that caused the error. I asked the agent to create the situation I knew was causing the error and document a reproducible example, which I tested. </issue_description>

<agent_instructions>Check the logic, if reasonable, follow the proposed fix, and add the reproducible example as a new test. Keep it short and minimal. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix TypeError in evaluate_boxes with missing predictions Fix evaluate_boxes TypeError when ground truth has non-default index Dec 5, 2025
Copilot AI requested a review from bw4sz December 5, 2025 19:39
@bw4sz bw4sz force-pushed the copilot/fix-evaluate-boxes-type-error branch from 56ff3ee to 82a30bf Compare December 8, 2025 17:05
@bw4sz bw4sz force-pushed the copilot/fix-evaluate-boxes-type-error branch from 82a30bf to 02f7fc8 Compare December 8, 2025 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluate boxes fails when there are some ground that have predictions, but others do not.

2 participants