Skip to content

Commit 04989cb

Browse files
fix(sampler): preserve promoted dependent first names
1 parent 7d8ec0d commit 04989cb

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

extropy/population/sampler/core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ def _sample_dependent_as_agent(
310310
agent["household_id"] = household_id
311311
agent["household_role"] = f"dependent_{dependent.relationship}"
312312
agent["relationship_to_primary"] = dependent.relationship
313+
dep_name = str(getattr(dependent, "name", "")).strip()
314+
if dep_name:
315+
agent["first_name"] = dep_name
316+
if parent.get("last_name"):
317+
agent["last_name"] = parent["last_name"]
313318

314319
# Copy household-scoped attributes from parent
315320
for attr in spec.attributes:

tests/test_agent_focus.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,28 @@ def test_different_focus_values(self):
518518
for focus in ["surgeons", "retired couples", "families", None]:
519519
spec = _make_household_spec(size=100, agent_focus=focus)
520520
assert spec.meta.agent_focus == focus
521+
522+
523+
class TestPromotedDependentNames:
524+
def test_promoted_dependents_preserve_household_names(self):
525+
spec = _make_household_spec(size=240, agent_focus="families")
526+
result = sample_population(spec, count=240, seed=42)
527+
528+
promoted = [
529+
a
530+
for a in result.agents
531+
if a.get("household_role", "").startswith("dependent_")
532+
]
533+
assert promoted, "Expected promoted dependents in families mode"
534+
535+
households = {h["id"]: h for h in getattr(result, "_households", [])}
536+
for dep in promoted:
537+
first_name = dep.get("first_name")
538+
assert first_name, "Promoted dependents must have first_name"
539+
540+
hh = households.get(dep["household_id"])
541+
assert hh is not None
542+
dependent_names = {d.get("name") for d in hh.get("dependent_data", [])}
543+
assert first_name in dependent_names, (
544+
"Promoted dependent names should stay aligned with generated dependent records"
545+
)

0 commit comments

Comments
 (0)