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
14 changes: 13 additions & 1 deletion src/auto_bayesian/materialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def materialize_project(
left_on=relation.parent_key,
right_on=relation.parent_key,
)
count_columns = [
aggregate.output_name(relation.child)
for aggregate in relation.aggregations
if aggregate.op in ("count", "nunique")
]
if count_columns:
# A parent with no child rows has a count of 0, not a missing
# value. Left NaN, these zeros would later be binned as
# "__missing__" and become indistinguishable from genuinely
# absent data. Other ops (mean, latest, ...) stay NaN, since
# they are truly undefined over an empty set.
parent[count_columns] = parent[count_columns].fillna(0)
if relation.sequence_features:
seq = _compute_sequence_features(child, relation, project.tables[relation.child])
parent = parent.merge(
Expand Down Expand Up @@ -257,4 +269,4 @@ def _compute_acceleration(gaps: pd.Series) -> float:
if older_mean == 0 and recent_mean == 0:
return 0.0
denom = older_mean if older_mean != 0 else 1.0
return float((older_mean - recent_mean) / denom)
return float((older_mean - recent_mean) / denom)
50 changes: 50 additions & 0 deletions tests/test_materialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,53 @@ def test_materialize_project_joins_and_aggregates() -> None:
assert lead_2["interaction_count"] == 2
assert lead_2["channel_count"] == 2
assert lead_2["latest_channel"] == "meeting"


def test_one_to_many_count_is_zero_for_parents_without_children(tmp_path: Path) -> None:
import pandas as pd

from auto_bayesian import build_project

parents = pd.DataFrame(
{"tender_id": ["T1", "T2", "T3"], "won": ["1", "0", "0"]}
)
children = pd.DataFrame(
{
"bid_id": ["B1", "B2", "B3"],
"tender_id": ["T1", "T1", "T2"],
"bidder": ["a", "b", "a"],
"price": [0.9, 1.1, 0.95],
}
)
project = build_project(
root=tmp_path,
root_table="tenders",
target_column="won",
positive_label="1",
output_dir=tmp_path / "artifacts",
tables=[
{"name": "tenders", "primary_key": "tender_id"},
{"name": "bids", "primary_key": "bid_id"},
],
relations=[
{
"parent": "tenders",
"child": "bids",
"parent_key": "tender_id",
"child_key": "tender_id",
"kind": "one_to_many",
"aggregations": [
{"op": "count", "name": "bid_count"},
{"column": "bidder", "op": "nunique", "name": "bidder_count"},
{"column": "price", "op": "mean", "name": "mean_price"},
],
}
],
)
frame = materialize_project(project, tables={"tenders": parents, "bids": children})
childless = frame.loc[frame["tender_id"] == "T3"].iloc[0]

assert childless["bid_count"] == 0
assert childless["bidder_count"] == 0
assert pd.isna(childless["mean_price"])
assert frame.loc[frame["tender_id"] == "T1"].iloc[0]["bid_count"] == 2
Loading