diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 704bbc73..b972afaa 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -87,7 +87,11 @@ def merge_data( Returns: A single data payload containing the merged data, preserving the original data - types (list or np.ndarray). + types (list or np.ndarray). + + Raises: + ValueError: If data values within a single object have different lengths or if + dictionaries have different keys. """ if not data_list: return {} @@ -96,6 +100,12 @@ def merge_data( if not all(keys_set == all_keys_sets[0] for keys_set in all_keys_sets): raise ValueError("All data dictionaries must have the same keys to merge.") + for data in data_list: + lengths = [len(value) for value in data.values()] + if len(set(lengths)) > 1: + raise ValueError( + "All data values within a single object must have equal length.") + merged_data = {key: [] for key in all_keys_sets[0]} for data in data_list: diff --git a/test/detection/test_core.py b/test/detection/test_core.py index b21423e9..3b9a53f1 100644 --- a/test/detection/test_core.py +++ b/test/detection/test_core.py @@ -344,6 +344,35 @@ def test_equal( {}, DoesNotRaise(), ), # two empty data dicts + ( + [ + {"test_1": []}, + ], + {"test_1": []}, + DoesNotRaise(), + ), # single data dict with a single field name and empty list values + ( + [ + {"test_1": np.array([])}, + ], + {"test_1": np.array([])}, + DoesNotRaise(), + ), # single data dict with a single field name and empty np.array values + ( + [ + {"test_1": [1, 2, 3]}, + ], + {"test_1": [1, 2, 3]}, + DoesNotRaise(), + ), # single data dict with a single field name and list values + ( + [ + {"test_1": []}, + {"test_1": [3, 2, 1]}, + ], + {"test_1": [3, 2, 1]}, + DoesNotRaise(), + ), # two data dicts with the same field name and empty and list values ( [ {"test_1": [1, 2, 3]}, @@ -407,6 +436,22 @@ def test_equal( }, DoesNotRaise(), ), # two data dicts with the same field names and mixed values + ( + [ + {"test_1": np.array([1, 2, 3])}, + {"test_1": np.array([[3, 2, 1]])}, + ], + None, + pytest.raises(ValueError), + ), # two data dicts with the same field name and 1D and 2D arrays values + ( + [ + {"test_1": np.array([1, 2, 3]), "test_2": np.array(["a", "b"])}, + {"test_1": np.array([3, 2, 1]), "test_2": np.array(["c", "b", "a"])}, + ], + None, + pytest.raises(ValueError), + ), # two data dicts with the same field name and different length arrays values ], ) def test_merge_data(