Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions sdk/python/feast/labeling/conflict_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def resolve_conflicts(
)
elif conflict_policy == ConflictPolicy.MAJORITY_VOTE:
return _resolve_majority_vote(
df, join_key_columns, feature_name_columns, timestamp_field
df, join_key_columns, feature_name_columns, timestamp_field, labeler_field
)
else:
return _resolve_last_write_wins(df, join_key_columns, timestamp_field)
Expand Down Expand Up @@ -104,11 +104,13 @@ def _resolve_majority_vote(
join_key_columns: List[str],
feature_name_columns: List[str],
timestamp_field: str,
labeler_field: str,
) -> pd.DataFrame:
"""For each entity, pick the most common value per feature column.

Uses the most frequent value across all labelers for each feature.
Ties are broken by recency (latest timestamp wins).
Ties are broken by recency (latest timestamp wins). The resolved row has no
single labeler, so its labeler field is set to "majority_vote".
"""
if not feature_name_columns:
return _resolve_last_write_wins(df, join_key_columns, timestamp_field)
Expand Down Expand Up @@ -143,8 +145,8 @@ def _resolve_majority_vote(
row[col] = top_value

row[timestamp_field] = group[timestamp_field].max()
if "labeler" in group.columns and "labeler" not in join_key_columns:
row["labeler"] = "majority_vote"
if labeler_field in group.columns and labeler_field not in join_key_columns:
row[labeler_field] = "majority_vote"

resolved_rows.append(row)

Expand Down
58 changes: 58 additions & 0 deletions sdk/python/tests/unit/test_label_conflict_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Unit tests for the MAJORITY_VOTE resolution of LabelView labels."""

from datetime import datetime

import pandas as pd
import pytest

from feast.labeling.conflict_policy import ConflictPolicy
from feast.labeling.conflict_resolver import resolve_conflicts


def _labels(labeler_column, **extra_columns):
return pd.DataFrame(
{
"chunk_id": ["c1", "c1", "c1"],
"label": ["good", "good", "bad"],
labeler_column: ["alice", "bob", "alice"],
"ts": [datetime(2026, 1, day) for day in (1, 2, 3)],
**extra_columns,
}
)


def _resolve(df, labeler_field, feature_names):
return resolve_conflicts(
df=df,
join_key_columns=["chunk_id"],
feature_name_columns=feature_names,
timestamp_field="ts",
labeler_field=labeler_field,
conflict_policy=ConflictPolicy.MAJORITY_VOTE,
)


@pytest.mark.parametrize("labeler_field", ["labeler", "annotator"])
def test_majority_vote_sets_the_labeler_field(labeler_field):
# the labeler field is part of the schema of a LabelView
result = _resolve(_labels(labeler_field), labeler_field, ["label", labeler_field])

assert result["label"].tolist() == ["good"]
assert result[labeler_field].tolist() == ["majority_vote"]


@pytest.mark.parametrize("labeler_field", ["labeler", "annotator"])
def test_majority_vote_keeps_the_labeler_column(labeler_field):
df = _labels(labeler_field)
result = _resolve(df, labeler_field, ["label"])

assert result.columns.tolist() == df.columns.tolist()
assert result[labeler_field].tolist() == ["majority_vote"]


def test_majority_vote_resolves_a_label_called_labeler():
df = _labels("annotator", labeler=["vendor_a", "vendor_a", "vendor_b"])
result = _resolve(df, "annotator", ["label", "annotator", "labeler"])

assert result["labeler"].tolist() == ["vendor_a"]
assert result["annotator"].tolist() == ["majority_vote"]
Loading