Skip to content

Support YTA arrival observation modes in get_prob_dist_by_service #209

Description

@zmek

Feature: Support YTA arrival observation modes in get_prob_dist_by_service

Summary

Extend get_prob_dist_by_service so yet-to-arrive bed-demand PMFs can be built with observation_mode="arrived_in_window" (and optionally arrived_and_admitted_in_window) using an inpatient_arrivals frame — the same helper already used for ED-current and departures.

Simplify notebook 4d by replacing its hand-rolled YTA PMF loop with this API.

Problem

The evaluate layer supports arrived_in_window with inpatient_arrivals, but get_prob_dist_by_service does not:

  • validate_observation_mode_for_component only allows admitted_at_some_point and admitted_in_window for component="arrivals".
  • Those modes require ed_visits, which is wrong for YTA and direct-admission flows.

Notebook 4d works around this with nested loops calling yta_model.predict and count_observed per specialty and date, while ED-current PMFs in the same section already use get_prob_dist_by_service.

Proposed changes

aggregate.py — add inpatient_arrivals parameter and extend frame requirements:

component observation_mode Required frame
arrivals admitted_at_some_point, admitted_in_window ed_visits
arrivals arrived_in_window, arrived_and_admitted_in_window inpatient_arrivals
departures departed_in_window inpatient_visits

Update validate_observation_mode_for_component in observations.py accordingly. Pass inpatient_arrivals through to count_observed for observed counts in returned leaves.

For YTA-only FlowSelection (include_ed_yta=True, include_ed_current=False):

  • Do not require ed_visits.
  • Do not require curve parameters x1y2 unless parametric in-window weighting is active.
  • Keep using build_service_data / DemandPredictor so predicted PMFs match production.

Notebook 4d — replace the hand-rolled YTA block in section 2b with:

flow_sel_ed_yta = FlowSelection.custom(
    include_ed_current=False,
    include_ed_yta=True,
    include_non_ed_yta=False,
    include_elective_yta=False,
    include_transfers_in=False,
    include_departures=False,
)

ed_yta_by_specialty = get_prob_dist_by_service(
    ed_visits=None,
    snapshot_dates=eval_snapshot_dates,
    prediction_time=prediction_time,
    models=service_models,
    specialties=specialties,
    prediction_window=prediction_window,
    flow_selection=flow_sel_ed_yta,
    component="arrivals",
    observation_mode="arrived_in_window",
    inpatient_arrivals=eval_inpatient_arrivals_df,
    x1=x1, y1=y1, x2=x2, y2=y2,
    verbose=False,
)

Acceptance criteria

  • arrived_in_window works on get_prob_dist_by_service with inpatient_arrivals and without ed_visits.
  • admitted_in_window still requires ed_visits (regression).
  • Returned leaves have correct agg_predicted and agg_observed for a minimal YTA fixture.
  • Notebook 4d section 2b uses the new API; hand-rolled YTA loop removed.
  • Tests added in tests/test_aggregate.py; CI green.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions